diff --git a/Flow.Launcher.Infrastructure/UserSettings/CustomShortcutModel.cs b/Flow.Launcher.Infrastructure/UserSettings/CustomShortcutModel.cs index 11f30d2f6..a2a7100d4 100644 --- a/Flow.Launcher.Infrastructure/UserSettings/CustomShortcutModel.cs +++ b/Flow.Launcher.Infrastructure/UserSettings/CustomShortcutModel.cs @@ -3,35 +3,13 @@ using System.Text.Json.Serialization; namespace Flow.Launcher.Infrastructure.UserSettings { - - public class CustomShortcutModel + public abstract class ShortcutBaseModel { public string Key { get; set; } - public string Value { get; set; } - - [JsonIgnore] - public bool CanBeEdited { get; private set; } // Can be edited by user from settings window [JsonIgnore] public Func Expand { get; set; } = () => { return ""; }; - [JsonConstructorAttribute] - public CustomShortcutModel(string key, string value) - { - Key = key; - Value = value; - CanBeEdited = true; - Expand = () => { return Value; }; - } - - public CustomShortcutModel(string key, string description, Func expand) - { - Key = key; - Value = description; - CanBeEdited = false; - Expand = expand; - } - public override bool Equals(object obj) { return obj is CustomShortcutModel other && @@ -42,6 +20,19 @@ namespace Flow.Launcher.Infrastructure.UserSettings { return HashCode.Combine(Key); } + } + + public class CustomShortcutModel : ShortcutBaseModel + { + public string Value { get; set; } + + [JsonConstructorAttribute] + public CustomShortcutModel(string key, string value) + { + Key = key; + Value = value; + Expand = () => { return Value; }; + } public void Deconstruct(out string key, out string value) { @@ -59,4 +50,16 @@ namespace Flow.Launcher.Infrastructure.UserSettings return new CustomShortcutModel(shortcut.Key, shortcut.Value); } } + + public class BuiltinShortcutModel : ShortcutBaseModel + { + public string Description { get; set; } + + public BuiltinShortcutModel(string key, string description, Func expand) + { + Key = key; + Description = description; + Expand = expand ?? (() => { return ""; }); + } + } } diff --git a/Flow.Launcher.Infrastructure/UserSettings/Settings.cs b/Flow.Launcher.Infrastructure/UserSettings/Settings.cs index a9a69687c..5829fe540 100644 --- a/Flow.Launcher.Infrastructure/UserSettings/Settings.cs +++ b/Flow.Launcher.Infrastructure/UserSettings/Settings.cs @@ -180,8 +180,8 @@ namespace Flow.Launcher.Infrastructure.UserSettings public ObservableCollection CustomShortcuts { get; set; } = new ObservableCollection(); [JsonIgnore] - public ObservableCollection BuiltinShortcuts { get; set; } = new ObservableCollection() { - new CustomShortcutModel("{clipboard}", "Get text from clipboard.", Clipboard.GetText) + public ObservableCollection BuiltinShortcuts { get; set; } = new ObservableCollection() { + new BuiltinShortcutModel("{clipboard}", "Get text from clipboard.", Clipboard.GetText) }; public bool DontPromptUpdateMsg { get; set; } diff --git a/Flow.Launcher/CustomShortcutSetting.xaml.cs b/Flow.Launcher/CustomShortcutSetting.xaml.cs index aa029f7c3..524ea69ed 100644 --- a/Flow.Launcher/CustomShortcutSetting.xaml.cs +++ b/Flow.Launcher/CustomShortcutSetting.xaml.cs @@ -42,7 +42,7 @@ namespace Flow.Launcher MessageBox.Show(InternationalizationManager.Instance.GetTranslation("emptyShortcut")); return; } - if (!update && (_settings.CustomShortcuts.Contains(new CustomShortcutModel(Key, Value)) || _settings.BuiltinShortcuts.Contains(new CustomShortcutModel(Key, Value)))) + if (!update && (_settings.CustomShortcuts.Contains(new CustomShortcutModel(Key, Value)) || _settings.BuiltinShortcuts.Contains(new BuiltinShortcutModel(Key, Value, null)))) { MessageBox.Show(InternationalizationManager.Instance.GetTranslation("dulplicateShortcut")); return; diff --git a/Flow.Launcher/Languages/en.xaml b/Flow.Launcher/Languages/en.xaml index 292ae7e0d..7c01e680f 100644 --- a/Flow.Launcher/Languages/en.xaml +++ b/Flow.Launcher/Languages/en.xaml @@ -217,7 +217,6 @@ Enter a shortcut that automatically expands to the specified query. Shortcut is dulplicate, please enter a new Shortcut or edit the existing one. Shortcut and/or its expansion is empty. - This shortcut cannot be deleted or edited. Hotkey Unavailable diff --git a/Flow.Launcher/SettingWindow.xaml.cs b/Flow.Launcher/SettingWindow.xaml.cs index 0b31b5996..974da628e 100644 --- a/Flow.Launcher/SettingWindow.xaml.cs +++ b/Flow.Launcher/SettingWindow.xaml.cs @@ -381,11 +381,6 @@ namespace Flow.Launcher MessageBox.Show(InternationalizationManager.Instance.GetTranslation("pleaseSelectAnItem")); return; } - else if (!item.CanBeEdited) - { - MessageBox.Show(InternationalizationManager.Instance.GetTranslation("uneditableShortcut")); - return; - } string deleteWarning = string.Format(InternationalizationManager.Instance.GetTranslation("deleteCustomShortcutWarning"), @@ -394,7 +389,7 @@ namespace Flow.Launcher MessageBox.Show(deleteWarning, InternationalizationManager.Instance.GetTranslation("delete"), MessageBoxButton.YesNo) == MessageBoxResult.Yes) { - viewModel.RemoveShortcut(item); + settings.CustomShortcuts.Remove(item); } } @@ -406,16 +401,12 @@ namespace Flow.Launcher MessageBox.Show(InternationalizationManager.Instance.GetTranslation("pleaseSelectAnItem")); return; } - else if (!item.CanBeEdited) - { - MessageBox.Show(InternationalizationManager.Instance.GetTranslation("uneditableShortcut")); - return; - } var shortcutSettingWindow = new CustomShortcutSetting(item, settings); if (shortcutSettingWindow.ShowDialog() == true) { - viewModel.EditShortcut(item, shortcutSettingWindow.ShortCut); + settings.CustomShortcuts.Remove(item); + settings.CustomShortcuts.Add(shortcutSettingWindow.ShortCut); } } @@ -424,7 +415,7 @@ namespace Flow.Launcher var shortcutSettingWindow = new CustomShortcutSetting(settings); if (shortcutSettingWindow.ShowDialog() == true) { - viewModel.AddShortcut(shortcutSettingWindow.ShortCut); + settings.CustomShortcuts.Add(shortcutSettingWindow.ShortCut); } } diff --git a/Flow.Launcher/ViewModel/MainViewModel.cs b/Flow.Launcher/ViewModel/MainViewModel.cs index 016d06d77..fb852d71a 100644 --- a/Flow.Launcher/ViewModel/MainViewModel.cs +++ b/Flow.Launcher/ViewModel/MainViewModel.cs @@ -662,7 +662,7 @@ namespace Flow.Launcher.ViewModel } } - private static Query ConstructQuery(string queryText, IEnumerable customShortcuts, IEnumerable builtInShortcuts) + private static Query ConstructQuery(string queryText, IEnumerable customShortcuts, IEnumerable builtInShortcuts) { StringBuilder queryBuilder = new(queryText); diff --git a/Flow.Launcher/ViewModel/SettingWindowViewModel.cs b/Flow.Launcher/ViewModel/SettingWindowViewModel.cs index 53dad8cbd..9e23ec4df 100644 --- a/Flow.Launcher/ViewModel/SettingWindowViewModel.cs +++ b/Flow.Launcher/ViewModel/SettingWindowViewModel.cs @@ -44,7 +44,6 @@ namespace Flow.Launcher.ViewModel break; } }; - ShortCuts = new ObservableCollection(Settings.CustomShortcuts.Union(Settings.BuiltinShortcuts)); } public Settings Settings { get; set; } @@ -181,7 +180,7 @@ namespace Flow.Launcher.ViewModel public List Languages => _translater.LoadAvailableLanguages(); public IEnumerable MaxResultsRange => Enumerable.Range(2, 16); - public ObservableCollection ShortCuts { get; set; } = new ObservableCollection(); + public ObservableCollection ShortCuts => Settings.CustomShortcuts; public string TestProxy() { @@ -538,30 +537,12 @@ namespace Flow.Launcher.ViewModel #endregion - #region hotkey + #region hotkey & shortcut public CustomPluginHotkey SelectedCustomPluginHotkey { get; set; } public CustomShortcutModel? SelectedCustomShortcut { get; set; } - public void AddShortcut(CustomShortcutModel shortcut) - { - Settings.CustomShortcuts.Add(shortcut); - ShortCuts.Add(shortcut); - } - - public void EditShortcut(CustomShortcutModel oldShortcut, CustomShortcutModel newShortcut) - { - RemoveShortcut(oldShortcut); - AddShortcut(newShortcut); - } - - public void RemoveShortcut(CustomShortcutModel shortcut) - { - Settings.CustomShortcuts.Remove(shortcut); - ShortCuts.Remove(shortcut); - } - #endregion #region about