From 03c74590f583e508e7d3b36a86722bedce41257d Mon Sep 17 00:00:00 2001 From: Yusyuriv Date: Tue, 23 Apr 2024 09:04:37 +0600 Subject: [PATCH] Remove old HotkeyControl, rename HotkeyControl2 to HotkeyControl --- Flow.Launcher/CustomQueryHotkeySetting.xaml | 2 +- Flow.Launcher/HotkeyControl.xaml | 227 +++++------------- Flow.Launcher/HotkeyControl.xaml.cs | 199 +++++---------- Flow.Launcher/HotkeyControl2.xaml | 77 ------ Flow.Launcher/HotkeyControl2.xaml.cs | 193 --------------- ...l2Dialog.xaml => HotkeyControlDialog.xaml} | 2 +- ...og.xaml.cs => HotkeyControlDialog.xaml.cs} | 4 +- .../Resources/Pages/WelcomePage2.xaml | 2 +- Flow.Launcher/SettingWindow.xaml | 24 +- 9 files changed, 138 insertions(+), 592 deletions(-) delete mode 100644 Flow.Launcher/HotkeyControl2.xaml delete mode 100644 Flow.Launcher/HotkeyControl2.xaml.cs rename Flow.Launcher/{HotkeyControl2Dialog.xaml => HotkeyControlDialog.xaml} (99%) rename Flow.Launcher/{HotkeyControl2Dialog.xaml.cs => HotkeyControlDialog.xaml.cs} (95%) diff --git a/Flow.Launcher/CustomQueryHotkeySetting.xaml b/Flow.Launcher/CustomQueryHotkeySetting.xaml index de7b57b91..de724ea81 100644 --- a/Flow.Launcher/CustomQueryHotkeySetting.xaml +++ b/Flow.Launcher/CustomQueryHotkeySetting.xaml @@ -97,7 +97,7 @@ VerticalAlignment="Center" FontSize="14" Text="{DynamicResource hotkey}" /> - - - - - - - - - - - - - - - - - - - - - - - - - - + + diff --git a/Flow.Launcher/HotkeyControl.xaml.cs b/Flow.Launcher/HotkeyControl.xaml.cs index 01584ee27..546b252df 100644 --- a/Flow.Launcher/HotkeyControl.xaml.cs +++ b/Flow.Launcher/HotkeyControl.xaml.cs @@ -1,30 +1,38 @@ -using System; +#nullable enable + using System.Collections.ObjectModel; -using System.ComponentModel; -using System.Linq; -using System.Runtime.CompilerServices; +using System.Threading.Tasks; using System.Windows; -using System.Windows.Controls; using System.Windows.Input; -using System.Windows.Media; using Flow.Launcher.Core.Resource; using Flow.Launcher.Helper; using Flow.Launcher.Infrastructure.Hotkey; -using Flow.Launcher.Plugin; -using JetBrains.Annotations; - -#nullable enable namespace Flow.Launcher { - public partial class HotkeyControl : INotifyPropertyChanged + public partial class HotkeyControl { + public string WindowTitle { + get { return (string)GetValue(WindowTitleProperty); } + set { SetValue(WindowTitleProperty, value); } + } + + public static readonly DependencyProperty WindowTitleProperty = DependencyProperty.Register( + nameof(WindowTitle), + typeof(string), + typeof(HotkeyControl), + new PropertyMetadata(string.Empty) + ); + /// /// Designed for Preview Hotkey and KeyGesture. /// public static readonly DependencyProperty ValidateKeyGestureProperty = DependencyProperty.Register( - nameof(ValidateKeyGesture), typeof(bool), typeof(HotkeyControl), - new PropertyMetadata(default(bool))); + nameof(ValidateKeyGesture), + typeof(bool), + typeof(HotkeyControl), + new PropertyMetadata(default(bool)) + ); public bool ValidateKeyGesture { @@ -33,7 +41,11 @@ namespace Flow.Launcher } public static readonly DependencyProperty DefaultHotkeyProperty = DependencyProperty.Register( - nameof(DefaultHotkey), typeof(string), typeof(HotkeyControl), new PropertyMetadata(default(string))); + nameof(DefaultHotkey), + typeof(string), + typeof(HotkeyControl), + new PropertyMetadata(default(string)) + ); public string DefaultHotkey { @@ -54,7 +66,11 @@ namespace Flow.Launcher public static readonly DependencyProperty ChangeHotkeyProperty = DependencyProperty.Register( - nameof(ChangeHotkey), typeof(ICommand), typeof(HotkeyControl), new PropertyMetadata(default(ICommand))); + nameof(ChangeHotkey), + typeof(ICommand), + typeof(HotkeyControl), + new PropertyMetadata(default(ICommand)) + ); public ICommand? ChangeHotkey { @@ -64,8 +80,11 @@ namespace Flow.Launcher public static readonly DependencyProperty HotkeyProperty = DependencyProperty.Register( - nameof(Hotkey), typeof(string), typeof(HotkeyControl), - new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnHotkeyChanged)); + nameof(Hotkey), + typeof(string), + typeof(HotkeyControl), + new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnHotkeyChanged) + ); public string Hotkey { @@ -81,148 +100,50 @@ namespace Flow.Launcher SetKeysToDisplay(CurrentHotkey); } - /*------------------ New Logic Structure Part------------------------*/ - - private void OnPreviewKeyDown(object sender, KeyEventArgs e) - { - if (HotkeyBtn.IsChecked != true) - return; - e.Handled = true; - - //when alt is pressed, the real key should be e.SystemKey - Key key = e.Key == Key.System ? e.SystemKey : e.Key; - - SpecialKeyState specialKeyState = GlobalHotkey.CheckModifiers(); - - var hotkeyModel = new HotkeyModel( - specialKeyState.AltPressed, - specialKeyState.ShiftPressed, - specialKeyState.WinPressed, - specialKeyState.CtrlPressed, - key); - - CurrentHotkey = hotkeyModel; - SetKeysToDisplay(CurrentHotkey); - } - - - // public new bool IsFocused => tbHotkey.IsFocused; - - private void tbHotkey_LostFocus(object sender, RoutedEventArgs e) - { - // tbHotkey.Text = CurrentHotkey?.ToString() ?? ""; - // tbHotkey.Select(tbHotkey.Text.Length, 0); - } - - private void tbHotkey_GotFocus(object sender, RoutedEventArgs e) - { - ResetMessage(); - } - - private void ResetMessage() - { - tbMsg.Text = InternationalizationManager.Instance.GetTranslation("flowlauncherPressHotkey"); - tbMsg.SetResourceReference(TextBox.ForegroundProperty, "Color05B"); - } - - public event PropertyChangedEventHandler? PropertyChanged; - - protected virtual void OnPropertyChanged([CallerMemberName] string? propertyName = null) - { - PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); - } - - private static bool CheckHotkeyAvailability(HotkeyModel hotkey, bool validateKeyGesture) => hotkey.Validate(validateKeyGesture) && HotKeyMapper.CheckAvailability(hotkey); - public string? Message { get; set; } - public bool MessageVisibility { get; set; } - public SolidColorBrush? MessageColor { get; set; } + public string EmptyHotkey => InternationalizationManager.Instance.GetTranslation("none"); - - public bool CurrentHotkeyAvailable { get; private set; } - - - private void SetMessage(string messageKey, bool error) - { - Message = InternationalizationManager.Instance.GetTranslation(messageKey); - MessageColor = error ? new SolidColorBrush(Colors.Green) : new SolidColorBrush(Colors.Red); - MessageVisibility = true; - } - - - private string EmptyHotkeyKey = "none"; - public string EmptyHotkey => InternationalizationManager.Instance.GetTranslation(EmptyHotkeyKey); - private const string KeySeparator = " + "; - - public ObservableCollection KeysToDisplay { get; set; } = new ObservableCollection(); - - public bool IsEmpty => KeysToDisplay.Count == 0 || (KeysToDisplay.Count == 1 && KeysToDisplay[0] == EmptyHotkey); + public ObservableCollection KeysToDisplay { get; set; } = new(); public HotkeyModel CurrentHotkey { get; private set; } = new(false, false, false, false, Key.None); - public void StartRecording() + public void GetNewHotkey(object sender, RoutedEventArgs e) { - if (!HotkeyBtn.IsChecked ?? false) - { - return; - } + OpenHotkeyDialog(); + } + private async Task OpenHotkeyDialog() + { if (!string.IsNullOrEmpty(Hotkey)) { HotKeyMapper.RemoveHotkey(Hotkey); } - /* 1. Key Recording Start */ - /* 2. Key Display area clear - * 3. Key Display when typing*/ - } - private void StopRecording() - { - try + var dialog = new HotkeyControlDialog(Hotkey, DefaultHotkey, WindowTitle); + await dialog.ShowAsync(); + switch (dialog.ResultType) { - var converter = new KeyGestureConverter(); - _ = (KeyGesture)converter.ConvertFromString(CurrentHotkey.ToString())!; + case HotkeyControlDialog.EResultType.Cancel: + SetHotkey(Hotkey); + return; + case HotkeyControlDialog.EResultType.Save: + SetHotkey(dialog.ResultValue); + break; + case HotkeyControlDialog.EResultType.Delete: + Delete(); + break; } - catch (Exception e) when (e is NotSupportedException or InvalidEnumArgumentException) - { - SetMessage("Hotkey Invalid", true); - CurrentHotkey = new HotkeyModel(Hotkey); - SetKeysToDisplay(CurrentHotkey); - return; - } - - HotkeyBtn.IsChecked = false; - - SetHotkey(CurrentHotkey, true); - } - - private void ResetToDefault() - { - if (!string.IsNullOrEmpty(Hotkey)) - HotKeyMapper.RemoveHotkey(Hotkey); - Hotkey = DefaultHotkey; - CurrentHotkey = new HotkeyModel(Hotkey); - - SetKeysToDisplay(CurrentHotkey); - - SetHotkey(CurrentHotkey); - - HotkeyBtn.IsChecked = false; } private void SetHotkey(HotkeyModel keyModel, bool triggerValidate = true) { - // tbHotkey.Text = keyModel.ToString(); - // tbHotkey.Select(tbHotkey.Text.Length, 0); - if (triggerValidate) { bool hotkeyAvailable = CheckHotkeyAvailability(keyModel, ValidateKeyGesture); - SetMessage(hotkeyAvailable ? "success" : "hotkeyUnavailable", !hotkeyAvailable); if (!hotkeyAvailable) { @@ -246,7 +167,6 @@ namespace Flow.Launcher HotKeyMapper.RemoveHotkey(Hotkey); Hotkey = ""; SetKeysToDisplay(new HotkeyModel(false, false, false, false, Key.None)); - HotkeyBtn.IsChecked = false; } private void SetKeysToDisplay(HotkeyModel? hotkey) @@ -269,14 +189,5 @@ namespace Flow.Launcher { SetHotkey(new HotkeyModel(keyStr), triggerValidate); } - - - private void HotkeyBtn_OnChecked(object sender, RoutedEventArgs e) => StartRecording(); - - - private void ResetButton_OnClick(object sender, RoutedEventArgs e) => ResetToDefault(); - private void DeleteBtn_OnClick(object sender, RoutedEventArgs e) => Delete(); - - private void StopRecordingBtn_Click(object sender, RoutedEventArgs e) => StopRecording(); } } diff --git a/Flow.Launcher/HotkeyControl2.xaml b/Flow.Launcher/HotkeyControl2.xaml deleted file mode 100644 index 730c9604b..000000000 --- a/Flow.Launcher/HotkeyControl2.xaml +++ /dev/null @@ -1,77 +0,0 @@ - - - diff --git a/Flow.Launcher/HotkeyControl2.xaml.cs b/Flow.Launcher/HotkeyControl2.xaml.cs deleted file mode 100644 index 86973a754..000000000 --- a/Flow.Launcher/HotkeyControl2.xaml.cs +++ /dev/null @@ -1,193 +0,0 @@ -#nullable enable - -using System.Collections.ObjectModel; -using System.Threading.Tasks; -using System.Windows; -using System.Windows.Input; -using Flow.Launcher.Core.Resource; -using Flow.Launcher.Helper; -using Flow.Launcher.Infrastructure.Hotkey; - -namespace Flow.Launcher -{ - public partial class HotkeyControl2 - { - public string WindowTitle { - get { return (string)GetValue(WindowTitleProperty); } - set { SetValue(WindowTitleProperty, value); } - } - - public static readonly DependencyProperty WindowTitleProperty = DependencyProperty.Register( - nameof(WindowTitle), - typeof(string), - typeof(HotkeyControl2), - new PropertyMetadata(string.Empty) - ); - - /// - /// Designed for Preview Hotkey and KeyGesture. - /// - public static readonly DependencyProperty ValidateKeyGestureProperty = DependencyProperty.Register( - nameof(ValidateKeyGesture), - typeof(bool), - typeof(HotkeyControl2), - new PropertyMetadata(default(bool)) - ); - - public bool ValidateKeyGesture - { - get { return (bool)GetValue(ValidateKeyGestureProperty); } - set { SetValue(ValidateKeyGestureProperty, value); } - } - - public static readonly DependencyProperty DefaultHotkeyProperty = DependencyProperty.Register( - nameof(DefaultHotkey), - typeof(string), - typeof(HotkeyControl2), - new PropertyMetadata(default(string)) - ); - - public string DefaultHotkey - { - get { return (string)GetValue(DefaultHotkeyProperty); } - set { SetValue(DefaultHotkeyProperty, value); } - } - - private static void OnHotkeyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) - { - if (d is not HotkeyControl2 hotkeyControl) - { - return; - } - - hotkeyControl.SetKeysToDisplay(new HotkeyModel(hotkeyControl.Hotkey)); - hotkeyControl.CurrentHotkey = new HotkeyModel(hotkeyControl.Hotkey); - } - - - public static readonly DependencyProperty ChangeHotkeyProperty = DependencyProperty.Register( - nameof(ChangeHotkey), - typeof(ICommand), - typeof(HotkeyControl2), - new PropertyMetadata(default(ICommand)) - ); - - public ICommand? ChangeHotkey - { - get { return (ICommand)GetValue(ChangeHotkeyProperty); } - set { SetValue(ChangeHotkeyProperty, value); } - } - - - public static readonly DependencyProperty HotkeyProperty = DependencyProperty.Register( - nameof(Hotkey), - typeof(string), - typeof(HotkeyControl2), - new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnHotkeyChanged) - ); - - public string Hotkey - { - get { return (string)GetValue(HotkeyProperty); } - set { SetValue(HotkeyProperty, value); } - } - - public HotkeyControl2() - { - InitializeComponent(); - - HotkeyList.ItemsSource = KeysToDisplay; - SetKeysToDisplay(CurrentHotkey); - } - - private static bool CheckHotkeyAvailability(HotkeyModel hotkey, bool validateKeyGesture) => - hotkey.Validate(validateKeyGesture) && HotKeyMapper.CheckAvailability(hotkey); - - public string EmptyHotkey => InternationalizationManager.Instance.GetTranslation("none"); - - public ObservableCollection KeysToDisplay { get; set; } = new(); - - public HotkeyModel CurrentHotkey { get; private set; } = new(false, false, false, false, Key.None); - - - public void GetNewHotkey(object sender, RoutedEventArgs e) - { - OpenHotkeyDialog(); - } - - private async Task OpenHotkeyDialog() - { - if (!string.IsNullOrEmpty(Hotkey)) - { - HotKeyMapper.RemoveHotkey(Hotkey); - } - - var dialog = new HotkeyControl2Dialog(Hotkey, DefaultHotkey, WindowTitle); - await dialog.ShowAsync(); - switch (dialog.ResultType) - { - case HotkeyControl2Dialog.EResultType.Cancel: - SetHotkey(Hotkey); - return; - case HotkeyControl2Dialog.EResultType.Save: - SetHotkey(dialog.ResultValue); - break; - case HotkeyControl2Dialog.EResultType.Delete: - Delete(); - break; - } - } - - - private void SetHotkey(HotkeyModel keyModel, bool triggerValidate = true) - { - if (triggerValidate) - { - bool hotkeyAvailable = CheckHotkeyAvailability(keyModel, ValidateKeyGesture); - - if (!hotkeyAvailable) - { - return; - } - - Hotkey = keyModel.ToString(); - SetKeysToDisplay(CurrentHotkey); - ChangeHotkey?.Execute(keyModel); - } - else - { - Hotkey = keyModel.ToString(); - ChangeHotkey?.Execute(keyModel); - } - } - - public void Delete() - { - if (!string.IsNullOrEmpty(Hotkey)) - HotKeyMapper.RemoveHotkey(Hotkey); - Hotkey = ""; - SetKeysToDisplay(new HotkeyModel(false, false, false, false, Key.None)); - } - - private void SetKeysToDisplay(HotkeyModel? hotkey) - { - KeysToDisplay.Clear(); - - if (hotkey == null || hotkey == default(HotkeyModel)) - { - KeysToDisplay.Add(EmptyHotkey); - return; - } - - foreach (var key in hotkey.Value.EnumerateDisplayKeys()!) - { - KeysToDisplay.Add(key); - } - } - - public void SetHotkey(string? keyStr, bool triggerValidate = true) - { - SetHotkey(new HotkeyModel(keyStr), triggerValidate); - } - } -} diff --git a/Flow.Launcher/HotkeyControl2Dialog.xaml b/Flow.Launcher/HotkeyControlDialog.xaml similarity index 99% rename from Flow.Launcher/HotkeyControl2Dialog.xaml rename to Flow.Launcher/HotkeyControlDialog.xaml index 2eb62c419..6912a1999 100644 --- a/Flow.Launcher/HotkeyControl2Dialog.xaml +++ b/Flow.Launcher/HotkeyControlDialog.xaml @@ -1,5 +1,5 @@  InternationalizationManager.Instance.GetTranslation("none"); - public HotkeyControl2Dialog(string hotkey, string defaultHotkey, string windowTitle = "") + public HotkeyControlDialog(string hotkey, string defaultHotkey, string windowTitle = "") { WindowTitle = windowTitle switch { diff --git a/Flow.Launcher/Resources/Pages/WelcomePage2.xaml b/Flow.Launcher/Resources/Pages/WelcomePage2.xaml index 84615c155..b60b8e47e 100644 --- a/Flow.Launcher/Resources/Pages/WelcomePage2.xaml +++ b/Flow.Launcher/Resources/Pages/WelcomePage2.xaml @@ -110,7 +110,7 @@ FontSize="14" FontWeight="SemiBold" Text="{DynamicResource flowlauncherHotkey}" /> - - - - @@ -2771,7 +2771,7 @@ Title="{DynamicResource SettingWindowHotkey}" Icon="" Type="Inside"> - @@ -2793,7 +2793,7 @@ Title="{DynamicResource SelectNextPageHotkey}" Icon="" Type="Inside"> - @@ -2802,7 +2802,7 @@ Title="{DynamicResource SelectPrevPageHotkey}" Icon="" Type="Inside"> - @@ -2836,7 +2836,7 @@ Icon="" Sub="{DynamicResource autoCompleteHotkeyToolTip}"> - @@ -2845,7 +2845,7 @@ Title="{DynamicResource autoCompleteHotkey}" Sub="{DynamicResource AdditionalHotkeyToolTip}" Type="InsideFit"> - @@ -2858,7 +2858,7 @@ Margin="0,4,0,0" Icon=""> - @@ -2867,7 +2867,7 @@ Title="{DynamicResource SelectNextItemHotkey}" Sub="{DynamicResource AdditionalHotkeyToolTip}" Type="InsideFit"> - @@ -2879,7 +2879,7 @@ Margin="0,4,0,0" Icon=""> - @@ -2888,7 +2888,7 @@ Title="{DynamicResource SelectPrevItemHotkey}" Sub="{DynamicResource AdditionalHotkeyToolTip}" Type="InsideFit"> -