Separate custom & built-in shortcuts

This commit is contained in:
Vic 2022-10-15 14:18:54 +08:00
parent cde272ae4d
commit 23ecd39dc7
7 changed files with 36 additions and 62 deletions

View file

@ -3,35 +3,13 @@ using System.Text.Json.Serialization;
namespace Flow.Launcher.Infrastructure.UserSettings namespace Flow.Launcher.Infrastructure.UserSettings
{ {
public abstract class ShortcutBaseModel
public class CustomShortcutModel
{ {
public string Key { get; set; } 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] [JsonIgnore]
public Func<string> Expand { get; set; } = () => { return ""; }; public Func<string> 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<string> expand)
{
Key = key;
Value = description;
CanBeEdited = false;
Expand = expand;
}
public override bool Equals(object obj) public override bool Equals(object obj)
{ {
return obj is CustomShortcutModel other && return obj is CustomShortcutModel other &&
@ -42,6 +20,19 @@ namespace Flow.Launcher.Infrastructure.UserSettings
{ {
return HashCode.Combine(Key); 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) 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); return new CustomShortcutModel(shortcut.Key, shortcut.Value);
} }
} }
public class BuiltinShortcutModel : ShortcutBaseModel
{
public string Description { get; set; }
public BuiltinShortcutModel(string key, string description, Func<string> expand)
{
Key = key;
Description = description;
Expand = expand ?? (() => { return ""; });
}
}
} }

View file

@ -180,8 +180,8 @@ namespace Flow.Launcher.Infrastructure.UserSettings
public ObservableCollection<CustomShortcutModel> CustomShortcuts { get; set; } = new ObservableCollection<CustomShortcutModel>(); public ObservableCollection<CustomShortcutModel> CustomShortcuts { get; set; } = new ObservableCollection<CustomShortcutModel>();
[JsonIgnore] [JsonIgnore]
public ObservableCollection<CustomShortcutModel> BuiltinShortcuts { get; set; } = new ObservableCollection<CustomShortcutModel>() { public ObservableCollection<BuiltinShortcutModel> BuiltinShortcuts { get; set; } = new ObservableCollection<BuiltinShortcutModel>() {
new CustomShortcutModel("{clipboard}", "Get text from clipboard.", Clipboard.GetText) new BuiltinShortcutModel("{clipboard}", "Get text from clipboard.", Clipboard.GetText)
}; };
public bool DontPromptUpdateMsg { get; set; } public bool DontPromptUpdateMsg { get; set; }

View file

@ -42,7 +42,7 @@ namespace Flow.Launcher
MessageBox.Show(InternationalizationManager.Instance.GetTranslation("emptyShortcut")); MessageBox.Show(InternationalizationManager.Instance.GetTranslation("emptyShortcut"));
return; 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")); MessageBox.Show(InternationalizationManager.Instance.GetTranslation("dulplicateShortcut"));
return; return;

View file

@ -217,7 +217,6 @@
<system:String x:Key="customeQueryShortcutTips">Enter a shortcut that automatically expands to the specified query.</system:String> <system:String x:Key="customeQueryShortcutTips">Enter a shortcut that automatically expands to the specified query.</system:String>
<system:String x:Key="dulplicateShortcut">Shortcut is dulplicate, please enter a new Shortcut or edit the existing one.</system:String> <system:String x:Key="dulplicateShortcut">Shortcut is dulplicate, please enter a new Shortcut or edit the existing one.</system:String>
<system:String x:Key="emptyShortcut">Shortcut and/or its expansion is empty.</system:String> <system:String x:Key="emptyShortcut">Shortcut and/or its expansion is empty.</system:String>
<system:String x:Key="uneditableShortcut">This shortcut cannot be deleted or edited.</system:String>
<!-- Hotkey Control --> <!-- Hotkey Control -->
<system:String x:Key="hotkeyUnavailable">Hotkey Unavailable</system:String> <system:String x:Key="hotkeyUnavailable">Hotkey Unavailable</system:String>

View file

@ -381,11 +381,6 @@ namespace Flow.Launcher
MessageBox.Show(InternationalizationManager.Instance.GetTranslation("pleaseSelectAnItem")); MessageBox.Show(InternationalizationManager.Instance.GetTranslation("pleaseSelectAnItem"));
return; return;
} }
else if (!item.CanBeEdited)
{
MessageBox.Show(InternationalizationManager.Instance.GetTranslation("uneditableShortcut"));
return;
}
string deleteWarning = string deleteWarning =
string.Format(InternationalizationManager.Instance.GetTranslation("deleteCustomShortcutWarning"), string.Format(InternationalizationManager.Instance.GetTranslation("deleteCustomShortcutWarning"),
@ -394,7 +389,7 @@ namespace Flow.Launcher
MessageBox.Show(deleteWarning, InternationalizationManager.Instance.GetTranslation("delete"), MessageBox.Show(deleteWarning, InternationalizationManager.Instance.GetTranslation("delete"),
MessageBoxButton.YesNo) == MessageBoxResult.Yes) MessageBoxButton.YesNo) == MessageBoxResult.Yes)
{ {
viewModel.RemoveShortcut(item); settings.CustomShortcuts.Remove(item);
} }
} }
@ -406,16 +401,12 @@ namespace Flow.Launcher
MessageBox.Show(InternationalizationManager.Instance.GetTranslation("pleaseSelectAnItem")); MessageBox.Show(InternationalizationManager.Instance.GetTranslation("pleaseSelectAnItem"));
return; return;
} }
else if (!item.CanBeEdited)
{
MessageBox.Show(InternationalizationManager.Instance.GetTranslation("uneditableShortcut"));
return;
}
var shortcutSettingWindow = new CustomShortcutSetting(item, settings); var shortcutSettingWindow = new CustomShortcutSetting(item, settings);
if (shortcutSettingWindow.ShowDialog() == true) 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); var shortcutSettingWindow = new CustomShortcutSetting(settings);
if (shortcutSettingWindow.ShowDialog() == true) if (shortcutSettingWindow.ShowDialog() == true)
{ {
viewModel.AddShortcut(shortcutSettingWindow.ShortCut); settings.CustomShortcuts.Add(shortcutSettingWindow.ShortCut);
} }
} }

View file

@ -662,7 +662,7 @@ namespace Flow.Launcher.ViewModel
} }
} }
private static Query ConstructQuery(string queryText, IEnumerable<CustomShortcutModel> customShortcuts, IEnumerable<CustomShortcutModel> builtInShortcuts) private static Query ConstructQuery(string queryText, IEnumerable<CustomShortcutModel> customShortcuts, IEnumerable<BuiltinShortcutModel> builtInShortcuts)
{ {
StringBuilder queryBuilder = new(queryText); StringBuilder queryBuilder = new(queryText);

View file

@ -44,7 +44,6 @@ namespace Flow.Launcher.ViewModel
break; break;
} }
}; };
ShortCuts = new ObservableCollection<CustomShortcutModel>(Settings.CustomShortcuts.Union(Settings.BuiltinShortcuts));
} }
public Settings Settings { get; set; } public Settings Settings { get; set; }
@ -181,7 +180,7 @@ namespace Flow.Launcher.ViewModel
public List<Language> Languages => _translater.LoadAvailableLanguages(); public List<Language> Languages => _translater.LoadAvailableLanguages();
public IEnumerable<int> MaxResultsRange => Enumerable.Range(2, 16); public IEnumerable<int> MaxResultsRange => Enumerable.Range(2, 16);
public ObservableCollection<CustomShortcutModel> ShortCuts { get; set; } = new ObservableCollection<CustomShortcutModel>(); public ObservableCollection<CustomShortcutModel> ShortCuts => Settings.CustomShortcuts;
public string TestProxy() public string TestProxy()
{ {
@ -538,30 +537,12 @@ namespace Flow.Launcher.ViewModel
#endregion #endregion
#region hotkey #region hotkey & shortcut
public CustomPluginHotkey SelectedCustomPluginHotkey { get; set; } public CustomPluginHotkey SelectedCustomPluginHotkey { get; set; }
public CustomShortcutModel? SelectedCustomShortcut { 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 #endregion
#region about #region about