From c9b5159ffd6279bb2ea4be94df6a20cc48b4eea9 Mon Sep 17 00:00:00 2001 From: Hongtao Zhang Date: Sat, 13 Apr 2024 12:49:57 -0500 Subject: [PATCH] fix some issues and change structure and verification place --- Flow.Launcher/HotkeyControl.xaml | 6 +-- .../ViewModel/HotkeyControlViewModel.cs | 31 ++++++++----- Flow.Launcher/ViewModel/MainViewModel.cs | 45 +++++-------------- 3 files changed, 35 insertions(+), 47 deletions(-) diff --git a/Flow.Launcher/HotkeyControl.xaml b/Flow.Launcher/HotkeyControl.xaml index 1a347ab13..488b80202 100644 --- a/Flow.Launcher/HotkeyControl.xaml +++ b/Flow.Launcher/HotkeyControl.xaml @@ -34,9 +34,9 @@ VerticalAlignment="Center" FontSize="13" FontWeight="SemiBold" - Foreground="{DynamicResource Color05B}" - Text="{DynamicResource flowlauncherPressHotkey}" - Visibility="Visible" /> + Foreground="{Binding MessageColor}" + Text="{Binding Message}" + Visibility="{Binding MessageVisibility}"/> diff --git a/Flow.Launcher/ViewModel/HotkeyControlViewModel.cs b/Flow.Launcher/ViewModel/HotkeyControlViewModel.cs index 10e6bdb82..7d90e50b1 100644 --- a/Flow.Launcher/ViewModel/HotkeyControlViewModel.cs +++ b/Flow.Launcher/ViewModel/HotkeyControlViewModel.cs @@ -2,6 +2,7 @@ using System.Collections; using System.Collections.Generic; using System.Collections.ObjectModel; +using System.ComponentModel; using System.Linq; using System.Threading.Tasks; using System.Windows; @@ -32,12 +33,10 @@ namespace Flow.Launcher.ViewModel public string DefaultHotkey { get; init; } - private void SetMessage(bool hotkeyAvailable) + private void SetMessage(string messageKey, bool error) { - Message = hotkeyAvailable - ? InternationalizationManager.Instance.GetTranslation("success") - : InternationalizationManager.Instance.GetTranslation("hotkeyUnavailable"); - MessageColor = hotkeyAvailable ? new SolidColorBrush(Colors.Green) : new SolidColorBrush(Colors.Red); + Message = InternationalizationManager.Instance.GetTranslation(messageKey); + MessageColor = error ? new SolidColorBrush(Colors.Green) : new SolidColorBrush(Colors.Red); MessageVisibility = true; } @@ -47,6 +46,7 @@ namespace Flow.Launcher.ViewModel Action? hotkeyDelegate = null) { Hotkey = hotkey; + CurrentHotkey = new HotkeyModel(hotkey); DefaultHotkey = defaultHotkey; ValidateKeyGesture = validateKeyGesture; HotkeyDelegate = hotkeyDelegate; @@ -104,8 +104,16 @@ namespace Flow.Launcher.ViewModel { Recording = false; - if (string.IsNullOrEmpty(Hotkey)) + try { + var converter = new KeyGestureConverter(); + var key = (KeyGesture)converter.ConvertFromString(CurrentHotkey.ToString())!; + } + catch (Exception e) when (e is NotSupportedException or InvalidEnumArgumentException) + { + SetMessage("Hotkey Invalid", true); + CurrentHotkey = new HotkeyModel(Hotkey); + SetKeysToDisplay(KeysToDisplay, CurrentHotkey); return; } @@ -120,8 +128,9 @@ namespace Flow.Launcher.ViewModel if (!string.IsNullOrEmpty(Hotkey)) HotKeyMapper.RemoveHotkey(Hotkey); Hotkey = DefaultHotkey; + CurrentHotkey = new HotkeyModel(Hotkey); - SetKeysToDisplay(KeysToDisplay, Hotkey.Split(KeySeparator)); + SetKeysToDisplay(KeysToDisplay, CurrentHotkey); await SetHotkeyAsync(Hotkey); } @@ -138,14 +147,14 @@ namespace Flow.Launcher.ViewModel { bool hotkeyAvailable = CheckHotkeyAvailability(keyModel, ValidateKeyGesture); CurrentHotkeyAvailable = hotkeyAvailable; - SetMessage(hotkeyAvailable); + SetMessage(hotkeyAvailable ? "success" : "hotkeyUnavailable", !hotkeyAvailable); HotkeyDelegate?.Invoke(keyModel); if (CurrentHotkeyAvailable) { CurrentHotkey = keyModel; Hotkey = keyModel.ToString(); - SetKeysToDisplay(KeysToDisplay, Hotkey.Split(KeySeparator)); + SetKeysToDisplay(KeysToDisplay, CurrentHotkey); } } else @@ -165,7 +174,7 @@ namespace Flow.Launcher.ViewModel SetKeysToDisplay(KeysToDisplay, new List()); } - private void SetKeysToDisplay(HotkeyModel hotkey) + private void SetKeysToDisplay(ICollection container, HotkeyModel hotkey) { KeysToDisplay.Clear(); if (hotkey.Alt) @@ -222,7 +231,7 @@ namespace Flow.Launcher.ViewModel public void KeyDown(HotkeyModel hotkeyModel) { CurrentHotkey = hotkeyModel; - SetKeysToDisplay(KeysToDisplay, hotkeyModel.ToString().Split(KeySeparator)); + SetKeysToDisplay(KeysToDisplay, CurrentHotkey); } } } diff --git a/Flow.Launcher/ViewModel/MainViewModel.cs b/Flow.Launcher/ViewModel/MainViewModel.cs index b368d876c..c8e7ba5e5 100644 --- a/Flow.Launcher/ViewModel/MainViewModel.cs +++ b/Flow.Launcher/ViewModel/MainViewModel.cs @@ -630,45 +630,24 @@ namespace Flow.Launcher.ViewModel public string OpenResultCommandModifiers => Settings.OpenResultModifiers; - public string PreviewHotkey + public string VerifyOrSetDefaultHotkey(string hotkey, string defaultHotkey) { - get + try { - // TODO try to patch issue #1755 - // Added in v1.14.0, remove after v1.16.0. - try - { - var converter = new KeyGestureConverter(); - var key = (KeyGesture)converter.ConvertFromString(Settings.PreviewHotkey); - } - catch (Exception e) when (e is NotSupportedException || e is InvalidEnumArgumentException) - { - Settings.PreviewHotkey = "F1"; - } - - return Settings.PreviewHotkey; + var converter = new KeyGestureConverter(); + var key = (KeyGesture)converter.ConvertFromString(hotkey); } - } - - public string AutoCompleteHotkey - { - get + catch (Exception e) when (e is NotSupportedException || e is InvalidEnumArgumentException) { - // TODO try to patch issue #1755 - // Added in v1.14.0, remove after v1.16.0. - try - { - var converter = new KeyGestureConverter(); - var key = (KeyGesture)converter.ConvertFromString(Settings.AutoCompleteHotkey); - } - catch (Exception e) when (e is NotSupportedException || e is InvalidEnumArgumentException) - { - Settings.AutoCompleteHotkey = "F2"; - } - - return Settings.AutoCompleteHotkey; + return defaultHotkey; } + + return hotkey; } + + public string PreviewHotkey => VerifyOrSetDefaultHotkey(Settings.PreviewHotkey, "F1"); + + public string AutoCompleteHotkey => VerifyOrSetDefaultHotkey(Settings.AutoCompleteHotkey, "Tab"); public string Image => Constant.QueryTextBoxIconImagePath;