From 336e9726829a0df0246c65e80b01869f2d1d59f0 Mon Sep 17 00:00:00 2001 From: Yusyuriv Date: Fri, 3 May 2024 05:05:41 +0600 Subject: [PATCH] Fully functional Hotkey pane in the settings window --- Flow.Launcher/CustomShortcutSetting.xaml.cs | 2 +- .../SettingsPaneGeneralViewModel.cs | 10 +- .../ViewModels/SettingsPaneHotkeyViewModel.cs | 134 +++ .../Views/SettingsPaneHotkey.xaml | 841 +++++++++--------- .../Views/SettingsPaneHotkey.xaml.cs | 31 +- .../SettingPages/Views/SettingsPaneTheme.xaml | 8 + 6 files changed, 558 insertions(+), 468 deletions(-) create mode 100644 Flow.Launcher/SettingPages/ViewModels/SettingsPaneHotkeyViewModel.cs diff --git a/Flow.Launcher/CustomShortcutSetting.xaml.cs b/Flow.Launcher/CustomShortcutSetting.xaml.cs index 097d6a53b..880c49787 100644 --- a/Flow.Launcher/CustomShortcutSetting.xaml.cs +++ b/Flow.Launcher/CustomShortcutSetting.xaml.cs @@ -47,7 +47,7 @@ namespace Flow.Launcher } // Check if key is modified or adding a new one if (((update && originalKey != Key) || !update) - && viewModel.ShortcutExists(Key)) + && (viewModel?.ShortcutExists(Key) ?? false)) // TODO Remove this check after removing unused code from SettingWindow.xaml.cs { MessageBox.Show(InternationalizationManager.Instance.GetTranslation("duplicateShortcut")); return; diff --git a/Flow.Launcher/SettingPages/ViewModels/SettingsPaneGeneralViewModel.cs b/Flow.Launcher/SettingPages/ViewModels/SettingsPaneGeneralViewModel.cs index e405ccc96..a9718a0ac 100644 --- a/Flow.Launcher/SettingPages/ViewModels/SettingsPaneGeneralViewModel.cs +++ b/Flow.Launcher/SettingPages/ViewModels/SettingsPaneGeneralViewModel.cs @@ -7,7 +7,6 @@ using Flow.Launcher.Core; using Flow.Launcher.Core.Configuration; using Flow.Launcher.Core.Resource; using Flow.Launcher.Helper; -using Flow.Launcher.Infrastructure; using Flow.Launcher.Infrastructure.UserSettings; using Flow.Launcher.Plugin; using Flow.Launcher.Plugin.SharedModels; @@ -16,7 +15,7 @@ namespace Flow.Launcher.SettingPages.ViewModels; public partial class SettingsPaneGeneralViewModel : BaseModel { - public Settings Settings { get; set; } + public Settings Settings { get; } private readonly Updater _updater; private readonly IPortable _portable; @@ -150,13 +149,6 @@ public partial class SettingsPaneGeneralViewModel : BaseModel .Select(v => v.ToString()) .ToList(); - public List OpenResultModifiersList => new List - { - KeyConstant.Alt, - KeyConstant.Ctrl, - $"{KeyConstant.Ctrl}+{KeyConstant.Alt}" - }; - public List Languages => InternationalizationManager.Instance.LoadAvailableLanguages(); public IEnumerable MaxResultsRange => Enumerable.Range(2, 16); diff --git a/Flow.Launcher/SettingPages/ViewModels/SettingsPaneHotkeyViewModel.cs b/Flow.Launcher/SettingPages/ViewModels/SettingsPaneHotkeyViewModel.cs new file mode 100644 index 000000000..f56c4a971 --- /dev/null +++ b/Flow.Launcher/SettingPages/ViewModels/SettingsPaneHotkeyViewModel.cs @@ -0,0 +1,134 @@ +using System.Windows; +using CommunityToolkit.Mvvm.Input; +using Flow.Launcher.Core.Resource; +using Flow.Launcher.Helper; +using Flow.Launcher.Infrastructure; +using Flow.Launcher.Infrastructure.Hotkey; +using Flow.Launcher.Infrastructure.UserSettings; +using Flow.Launcher.Plugin; + +namespace Flow.Launcher.SettingPages.ViewModels; + +public partial class SettingsPaneHotkeyViewModel : BaseModel +{ + public Settings Settings { get; } + + public CustomPluginHotkey SelectedCustomPluginHotkey { get; set; } + public CustomShortcutModel SelectedCustomShortcut { get; set; } + + public string[] OpenResultModifiersList => new[] + { + KeyConstant.Alt, + KeyConstant.Ctrl, + $"{KeyConstant.Ctrl}+{KeyConstant.Alt}" + }; + + public SettingsPaneHotkeyViewModel(Settings settings) + { + Settings = settings; + } + + [RelayCommand] + private void SetTogglingHotkey(HotkeyModel hotkey) + { + HotKeyMapper.SetHotkey(hotkey, HotKeyMapper.OnToggleHotkey); + } + + [RelayCommand] + private void CustomHotkeyDelete() + { + var item = SelectedCustomPluginHotkey; + if (item is null) + { + MessageBox.Show(InternationalizationManager.Instance.GetTranslation("pleaseSelectAnItem")); + return; + } + + var result = MessageBox.Show( + string.Format( + InternationalizationManager.Instance.GetTranslation("deleteCustomHotkeyWarning"), item.Hotkey + ), + InternationalizationManager.Instance.GetTranslation("delete"), + MessageBoxButton.YesNo + ); + + if (result is MessageBoxResult.Yes) + { + Settings.CustomPluginHotkeys.Remove(item); + HotKeyMapper.RemoveHotkey(item.Hotkey); + } + } + + [RelayCommand] + private void CustomHotkeyEdit() + { + var item = SelectedCustomPluginHotkey; + if (item is null) + { + MessageBox.Show(InternationalizationManager.Instance.GetTranslation("pleaseSelectAnItem")); + return; + } + + var window = new CustomQueryHotkeySetting(null, Settings); + window.UpdateItem(item); + window.ShowDialog(); + } + + [RelayCommand] + private void CustomHotkeyAdd() + { + new CustomQueryHotkeySetting(null, Settings).ShowDialog(); + } + + [RelayCommand] + private void CustomShortcutDelete() + { + var item = SelectedCustomShortcut; + if (item is null) + { + MessageBox.Show(InternationalizationManager.Instance.GetTranslation("pleaseSelectAnItem")); + return; + } + + var result = MessageBox.Show( + string.Format( + InternationalizationManager.Instance.GetTranslation("deleteCustomShortcutWarning"), item.Key, item.Value + ), + InternationalizationManager.Instance.GetTranslation("delete"), + MessageBoxButton.YesNo + ); + + if (result is MessageBoxResult.Yes) + { + Settings.CustomShortcuts.Remove(item); + } + } + + [RelayCommand] + private void CustomShortcutEdit() + { + var item = SelectedCustomShortcut; + if (item is null) + { + MessageBox.Show(InternationalizationManager.Instance.GetTranslation("pleaseSelectAnItem")); + return; + } + + var window = new CustomShortcutSetting(item.Key, item.Value, null); + if (window.ShowDialog() is not true) return; + + var index = Settings.CustomShortcuts.IndexOf(item); + Settings.CustomShortcuts[index] = new CustomShortcutModel(window.Key, window.Value); + } + + [RelayCommand] + private void CustomShortcutAdd() + { + var window = new CustomShortcutSetting(null); + if (window.ShowDialog() is true) + { + var shortcut = new CustomShortcutModel(window.Key, window.Value); + Settings.CustomShortcuts.Add(shortcut); + } + } +} diff --git a/Flow.Launcher/SettingPages/Views/SettingsPaneHotkey.xaml b/Flow.Launcher/SettingPages/Views/SettingsPaneHotkey.xaml index 7afb7bdc7..170b596f0 100644 --- a/Flow.Launcher/SettingPages/Views/SettingsPaneHotkey.xaml +++ b/Flow.Launcher/SettingPages/Views/SettingsPaneHotkey.xaml @@ -1,472 +1,433 @@ - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + Title="{DynamicResource OpenContainFolderHotkey}" + Icon="" + Type="Inside"> + + + + + + + + + + + + + HotkeySettings="{Binding Settings}" + DefaultHotkey="Ctrl+I" + Hotkey="{Binding Settings.OpenContextMenuHotkey}" + ValidateKeyGesture="False" /> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - + + + + + + + + - + + + + + + + + + + + + + + + + + + + + + + - + + + + + + + + + + + + + + + + + + + + - - - - - - - - + HorizontalAlignment="Right" + VerticalAlignment="Top" + Orientation="Horizontal"> +