2022-08-26 18:01:50 +02:00
|
|
|
|
// Copyright (c) Microsoft Corporation
|
|
|
|
|
|
// The Microsoft Corporation licenses this file to you under the MIT license.
|
|
|
|
|
|
// See the LICENSE file in the project root for more information.
|
|
|
|
|
|
|
|
|
|
|
|
using System;
|
2022-12-02 15:45:49 +01:00
|
|
|
|
using System.ComponentModel;
|
2022-08-26 18:01:50 +02:00
|
|
|
|
using System.Windows;
|
2022-12-14 12:17:38 +02:00
|
|
|
|
using PowerAccent.Core.Services;
|
2022-08-26 18:01:50 +02:00
|
|
|
|
using Point = PowerAccent.Core.Point;
|
|
|
|
|
|
using Size = PowerAccent.Core.Size;
|
|
|
|
|
|
|
|
|
|
|
|
namespace PowerAccent.UI;
|
|
|
|
|
|
|
2022-12-02 15:45:49 +01:00
|
|
|
|
public partial class Selector : Window, IDisposable, INotifyPropertyChanged
|
2022-08-26 18:01:50 +02:00
|
|
|
|
{
|
2022-12-02 15:45:49 +01:00
|
|
|
|
private readonly Core.PowerAccent _powerAccent = new ();
|
|
|
|
|
|
|
|
|
|
|
|
private Visibility _characterNameVisibility = Visibility.Visible;
|
|
|
|
|
|
|
2022-12-16 11:36:55 +02:00
|
|
|
|
private int _selectedIndex = 0;
|
|
|
|
|
|
|
2022-12-02 15:45:49 +01:00
|
|
|
|
public event PropertyChangedEventHandler PropertyChanged;
|
|
|
|
|
|
|
|
|
|
|
|
public Visibility CharacterNameVisibility
|
|
|
|
|
|
{
|
|
|
|
|
|
get
|
|
|
|
|
|
{
|
|
|
|
|
|
return _characterNameVisibility;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
set
|
|
|
|
|
|
{
|
|
|
|
|
|
_characterNameVisibility = value;
|
|
|
|
|
|
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(CharacterNameVisibility)));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2022-08-26 18:01:50 +02:00
|
|
|
|
|
|
|
|
|
|
public Selector()
|
|
|
|
|
|
{
|
|
|
|
|
|
InitializeComponent();
|
|
|
|
|
|
Application.Current.MainWindow.ShowActivated = false;
|
|
|
|
|
|
Application.Current.MainWindow.Topmost = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected override void OnSourceInitialized(EventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
base.OnSourceInitialized(e);
|
|
|
|
|
|
_powerAccent.OnChangeDisplay += PowerAccent_OnChangeDisplay;
|
|
|
|
|
|
_powerAccent.OnSelectCharacter += PowerAccent_OnSelectionCharacter;
|
|
|
|
|
|
this.Visibility = Visibility.Hidden;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-11-18 00:15:53 +08:00
|
|
|
|
private void PowerAccent_OnSelectionCharacter(int index, string character)
|
2022-08-26 18:01:50 +02:00
|
|
|
|
{
|
2022-12-16 11:36:55 +02:00
|
|
|
|
_selectedIndex = index;
|
|
|
|
|
|
characters.SelectedIndex = _selectedIndex;
|
|
|
|
|
|
characterName.Text = _powerAccent.CharacterDescriptions[_selectedIndex];
|
2022-08-26 18:01:50 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-11-18 00:15:53 +08:00
|
|
|
|
private void PowerAccent_OnChangeDisplay(bool isActive, string[] chars)
|
2022-08-26 18:01:50 +02:00
|
|
|
|
{
|
2022-12-02 15:45:49 +01:00
|
|
|
|
CharacterNameVisibility = _powerAccent.ShowUnicodeDescription ? Visibility.Visible : Visibility.Collapsed;
|
|
|
|
|
|
|
2022-08-26 18:01:50 +02:00
|
|
|
|
if (isActive)
|
|
|
|
|
|
{
|
|
|
|
|
|
characters.ItemsSource = chars;
|
2022-12-16 11:36:55 +02:00
|
|
|
|
characters.SelectedIndex = _selectedIndex;
|
2022-12-05 00:28:05 +02:00
|
|
|
|
this.UpdateLayout(); // Required for filling the actual width/height before positioning.
|
|
|
|
|
|
SetWindowPosition();
|
2022-12-14 12:17:38 +02:00
|
|
|
|
SetWindowAlignment();
|
2022-12-05 00:28:05 +02:00
|
|
|
|
Show();
|
2022-08-26 18:01:50 +02:00
|
|
|
|
Microsoft.PowerToys.Telemetry.PowerToysTelemetry.Log.WriteEvent(new PowerAccent.Core.Telemetry.PowerAccentShowAccentMenuEvent());
|
|
|
|
|
|
}
|
2022-12-05 00:28:05 +02:00
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
Hide();
|
|
|
|
|
|
}
|
2022-08-26 18:01:50 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void MenuExit_Click(object sender, RoutedEventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
Application.Current.Shutdown();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-12-05 00:28:05 +02:00
|
|
|
|
private void SetWindowPosition()
|
2022-08-26 18:01:50 +02:00
|
|
|
|
{
|
2022-12-05 00:28:05 +02:00
|
|
|
|
Size windowSize = new (((System.Windows.Controls.Panel)Application.Current.MainWindow.Content).ActualWidth, ((System.Windows.Controls.Panel)Application.Current.MainWindow.Content).ActualHeight);
|
|
|
|
|
|
Point position = _powerAccent.GetDisplayCoordinates(windowSize);
|
2022-08-26 18:01:50 +02:00
|
|
|
|
this.Left = position.X;
|
|
|
|
|
|
this.Top = position.Y;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-12-14 12:17:38 +02:00
|
|
|
|
private void SetWindowAlignment()
|
|
|
|
|
|
{
|
|
|
|
|
|
gridBorder.HorizontalAlignment = _powerAccent.GetToolbarPosition() switch
|
|
|
|
|
|
{
|
|
|
|
|
|
Position.Left or Position.TopLeft or Position.BottomLeft => HorizontalAlignment.Left,
|
|
|
|
|
|
Position.Right or Position.TopRight or Position.BottomRight => HorizontalAlignment.Right,
|
|
|
|
|
|
Position.Center or Position.Top or Position.Bottom => HorizontalAlignment.Center,
|
|
|
|
|
|
_ => HorizontalAlignment.Center
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-08-26 18:01:50 +02:00
|
|
|
|
protected override void OnClosed(EventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
_powerAccent.Dispose();
|
|
|
|
|
|
base.OnClosed(e);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
|
{
|
|
|
|
|
|
GC.SuppressFinalize(this);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|