diff --git a/Flow.Launcher/CustomShortcutSetting.xaml.cs b/Flow.Launcher/CustomShortcutSetting.xaml.cs index 45ec8871b..104f6d4f9 100644 --- a/Flow.Launcher/CustomShortcutSetting.xaml.cs +++ b/Flow.Launcher/CustomShortcutSetting.xaml.cs @@ -1,7 +1,6 @@ using Flow.Launcher.Core.Resource; -using Flow.Launcher.Infrastructure.UserSettings; +using Flow.Launcher.ViewModel; using System; -using System.Linq; using System.Windows; using System.Windows.Input; @@ -9,24 +8,26 @@ namespace Flow.Launcher { public partial class CustomShortcutSetting : Window { - private Settings _settings; - private bool update = false; + private SettingWindowViewModel viewModel; public string Key { get; set; } public string Value { get; set; } - public CustomShortcutModel ShortCut; + private string originalKey { get; init; } = null; + private string originalValue { get; init; } = null; + bool update = false; - public CustomShortcutSetting(Settings settings) + public CustomShortcutSetting(SettingWindowViewModel vm) { - _settings = settings; + viewModel = vm; InitializeComponent(); } - public CustomShortcutSetting(CustomShortcutModel shortcut, Settings settings) + public CustomShortcutSetting(string key, string value, SettingWindowViewModel vm) { - Key = shortcut.Key; - Value = shortcut.Value; - ShortCut = shortcut; - _settings = settings; + viewModel = vm; + Key = key; + Value = value; + originalKey = key; + originalValue = value; update = true; InitializeComponent(); } @@ -39,32 +40,19 @@ namespace Flow.Launcher private void BtnAdd_OnClick(object sender, RoutedEventArgs e) { - bool modified = false; if (String.IsNullOrEmpty(Key) || String.IsNullOrEmpty(Value)) { MessageBox.Show(InternationalizationManager.Instance.GetTranslation("emptyShortcut")); return; } - if (!update) + // Check if key is modified or adding a new one + if (((update && originalKey != Key) || !update) + && viewModel.ShortcutExists(Key)) { - ShortCut = new CustomShortcutModel(Key, Value); - if (_settings.CustomShortcuts.Any(x => x.Key == Key) || _settings.BuiltinShortcuts.Any(x => x.Key == Key)) - { - MessageBox.Show(InternationalizationManager.Instance.GetTranslation("duplicateShortcut")); - return; - } - modified = true; + MessageBox.Show(InternationalizationManager.Instance.GetTranslation("duplicateShortcut")); + return; } - else - { - if (ShortCut.Key != Key && _settings.CustomShortcuts.Any(x => x.Key == Key) || _settings.BuiltinShortcuts.Any(x => x.Key == Key)) - { - MessageBox.Show(InternationalizationManager.Instance.GetTranslation("duplicateShortcut")); - return; - } - modified = ShortCut.Key != Key || ShortCut.Value != Value; - } - DialogResult = modified; + DialogResult = !update || originalKey != Key || originalValue != Value; Close(); } diff --git a/Flow.Launcher/ViewModel/SettingWindowViewModel.cs b/Flow.Launcher/ViewModel/SettingWindowViewModel.cs index 2703aa70d..103150d04 100644 --- a/Flow.Launcher/ViewModel/SettingWindowViewModel.cs +++ b/Flow.Launcher/ViewModel/SettingWindowViewModel.cs @@ -685,12 +685,12 @@ namespace Flow.Launcher.ViewModel public CustomPluginHotkey SelectedCustomPluginHotkey { get; set; } - public CustomShortcutModel? SelectedCustomShortcut { get; set; } - #endregion #region shortcut + public CustomShortcutModel? SelectedCustomShortcut { get; set; } + public void DeleteSelectedCustomShortcut() { var item = SelectedCustomShortcut; @@ -700,11 +700,10 @@ namespace Flow.Launcher.ViewModel return; } - string deleteWarning = - string.Format(InternationalizationManager.Instance.GetTranslation("deleteCustomShortcutWarning"), + string deleteWarning = string.Format( + InternationalizationManager.Instance.GetTranslation("deleteCustomShortcutWarning"), item?.Key, item?.Value); - if ( - MessageBox.Show(deleteWarning, InternationalizationManager.Instance.GetTranslation("delete"), + if (MessageBox.Show(deleteWarning, InternationalizationManager.Instance.GetTranslation("delete"), MessageBoxButton.YesNo) == MessageBoxResult.Yes) { Settings.CustomShortcuts.Remove(item); @@ -720,7 +719,7 @@ namespace Flow.Launcher.ViewModel return false; } - var shortcutSettingWindow = new CustomShortcutSetting(item, Settings); + var shortcutSettingWindow = new CustomShortcutSetting(item.Key, item.Value, this); if (shortcutSettingWindow.ShowDialog() == true) { item.Key = shortcutSettingWindow.Key; @@ -732,13 +731,19 @@ namespace Flow.Launcher.ViewModel public void AddCustomShortcut() { - var shortcutSettingWindow = new CustomShortcutSetting(Settings); + var shortcutSettingWindow = new CustomShortcutSetting(this); if (shortcutSettingWindow.ShowDialog() == true) { - Settings.CustomShortcuts.Add(shortcutSettingWindow.ShortCut); + var shortcut = new CustomShortcutModel(shortcutSettingWindow.Key, shortcutSettingWindow.Value); + Settings.CustomShortcuts.Add(shortcut); } } + public bool ShortcutExists(string key) + { + return Settings.CustomShortcuts.Any(x => x.Key == key) || Settings.BuiltinShortcuts.Any(x => x.Key == key); + } + #endregion #region about