Skip to content

Commit 23ecd39

Browse files
Separate custom & built-in shortcuts
1 parent cde272a commit 23ecd39

File tree

7 files changed

+36
-62
lines changed

7 files changed

+36
-62
lines changed

Flow.Launcher.Infrastructure/UserSettings/CustomShortcutModel.cs

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,13 @@
33

44
namespace Flow.Launcher.Infrastructure.UserSettings
55
{
6-
7-
public class CustomShortcutModel
6+
public abstract class ShortcutBaseModel
87
{
98
public string Key { get; set; }
10-
public string Value { get; set; }
11-
12-
[JsonIgnore]
13-
public bool CanBeEdited { get; private set; } // Can be edited by user from settings window
149

1510
[JsonIgnore]
1611
public Func<string> Expand { get; set; } = () => { return ""; };
1712

18-
[JsonConstructorAttribute]
19-
public CustomShortcutModel(string key, string value)
20-
{
21-
Key = key;
22-
Value = value;
23-
CanBeEdited = true;
24-
Expand = () => { return Value; };
25-
}
26-
27-
public CustomShortcutModel(string key, string description, Func<string> expand)
28-
{
29-
Key = key;
30-
Value = description;
31-
CanBeEdited = false;
32-
Expand = expand;
33-
}
34-
3513
public override bool Equals(object obj)
3614
{
3715
return obj is CustomShortcutModel other &&
@@ -42,6 +20,19 @@ public override int GetHashCode()
4220
{
4321
return HashCode.Combine(Key);
4422
}
23+
}
24+
25+
public class CustomShortcutModel : ShortcutBaseModel
26+
{
27+
public string Value { get; set; }
28+
29+
[JsonConstructorAttribute]
30+
public CustomShortcutModel(string key, string value)
31+
{
32+
Key = key;
33+
Value = value;
34+
Expand = () => { return Value; };
35+
}
4536

4637
public void Deconstruct(out string key, out string value)
4738
{
@@ -59,4 +50,16 @@ public static implicit operator CustomShortcutModel((string Key, string Value) s
5950
return new CustomShortcutModel(shortcut.Key, shortcut.Value);
6051
}
6152
}
53+
54+
public class BuiltinShortcutModel : ShortcutBaseModel
55+
{
56+
public string Description { get; set; }
57+
58+
public BuiltinShortcutModel(string key, string description, Func<string> expand)
59+
{
60+
Key = key;
61+
Description = description;
62+
Expand = expand ?? (() => { return ""; });
63+
}
64+
}
6265
}

Flow.Launcher.Infrastructure/UserSettings/Settings.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,8 @@ public string QuerySearchPrecisionString
180180
public ObservableCollection<CustomShortcutModel> CustomShortcuts { get; set; } = new ObservableCollection<CustomShortcutModel>();
181181

182182
[JsonIgnore]
183-
public ObservableCollection<CustomShortcutModel> BuiltinShortcuts { get; set; } = new ObservableCollection<CustomShortcutModel>() {
184-
new CustomShortcutModel("{clipboard}", "Get text from clipboard.", Clipboard.GetText)
183+
public ObservableCollection<BuiltinShortcutModel> BuiltinShortcuts { get; set; } = new ObservableCollection<BuiltinShortcutModel>() {
184+
new BuiltinShortcutModel("{clipboard}", "Get text from clipboard.", Clipboard.GetText)
185185
};
186186

187187
public bool DontPromptUpdateMsg { get; set; }

Flow.Launcher/CustomShortcutSetting.xaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ private void BtnAdd_OnClick(object sender, RoutedEventArgs e)
4242
MessageBox.Show(InternationalizationManager.Instance.GetTranslation("emptyShortcut"));
4343
return;
4444
}
45-
if (!update && (_settings.CustomShortcuts.Contains(new CustomShortcutModel(Key, Value)) || _settings.BuiltinShortcuts.Contains(new CustomShortcutModel(Key, Value))))
45+
if (!update && (_settings.CustomShortcuts.Contains(new CustomShortcutModel(Key, Value)) || _settings.BuiltinShortcuts.Contains(new BuiltinShortcutModel(Key, Value, null))))
4646
{
4747
MessageBox.Show(InternationalizationManager.Instance.GetTranslation("dulplicateShortcut"));
4848
return;

Flow.Launcher/Languages/en.xaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,6 @@
217217
<system:String x:Key="customeQueryShortcutTips">Enter a shortcut that automatically expands to the specified query.</system:String>
218218
<system:String x:Key="dulplicateShortcut">Shortcut is dulplicate, please enter a new Shortcut or edit the existing one.</system:String>
219219
<system:String x:Key="emptyShortcut">Shortcut and/or its expansion is empty.</system:String>
220-
<system:String x:Key="uneditableShortcut">This shortcut cannot be deleted or edited.</system:String>
221220

222221
<!-- Hotkey Control -->
223222
<system:String x:Key="hotkeyUnavailable">Hotkey Unavailable</system:String>

Flow.Launcher/SettingWindow.xaml.cs

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -381,11 +381,6 @@ private void OnDeleteCustomShortCutClick(object sender, RoutedEventArgs e)
381381
MessageBox.Show(InternationalizationManager.Instance.GetTranslation("pleaseSelectAnItem"));
382382
return;
383383
}
384-
else if (!item.CanBeEdited)
385-
{
386-
MessageBox.Show(InternationalizationManager.Instance.GetTranslation("uneditableShortcut"));
387-
return;
388-
}
389384

390385
string deleteWarning =
391386
string.Format(InternationalizationManager.Instance.GetTranslation("deleteCustomShortcutWarning"),
@@ -394,7 +389,7 @@ private void OnDeleteCustomShortCutClick(object sender, RoutedEventArgs e)
394389
MessageBox.Show(deleteWarning, InternationalizationManager.Instance.GetTranslation("delete"),
395390
MessageBoxButton.YesNo) == MessageBoxResult.Yes)
396391
{
397-
viewModel.RemoveShortcut(item);
392+
settings.CustomShortcuts.Remove(item);
398393
}
399394
}
400395

@@ -406,16 +401,12 @@ private void OnEditCustomShortCutClick(object sender, RoutedEventArgs e)
406401
MessageBox.Show(InternationalizationManager.Instance.GetTranslation("pleaseSelectAnItem"));
407402
return;
408403
}
409-
else if (!item.CanBeEdited)
410-
{
411-
MessageBox.Show(InternationalizationManager.Instance.GetTranslation("uneditableShortcut"));
412-
return;
413-
}
414404

415405
var shortcutSettingWindow = new CustomShortcutSetting(item, settings);
416406
if (shortcutSettingWindow.ShowDialog() == true)
417407
{
418-
viewModel.EditShortcut(item, shortcutSettingWindow.ShortCut);
408+
settings.CustomShortcuts.Remove(item);
409+
settings.CustomShortcuts.Add(shortcutSettingWindow.ShortCut);
419410
}
420411
}
421412

@@ -424,7 +415,7 @@ private void OnAddCustomShortCutClick(object sender, RoutedEventArgs e)
424415
var shortcutSettingWindow = new CustomShortcutSetting(settings);
425416
if (shortcutSettingWindow.ShowDialog() == true)
426417
{
427-
viewModel.AddShortcut(shortcutSettingWindow.ShortCut);
418+
settings.CustomShortcuts.Add(shortcutSettingWindow.ShortCut);
428419
}
429420
}
430421

Flow.Launcher/ViewModel/MainViewModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -662,7 +662,7 @@ async Task QueryTask(PluginPair plugin)
662662
}
663663
}
664664

665-
private static Query ConstructQuery(string queryText, IEnumerable<CustomShortcutModel> customShortcuts, IEnumerable<CustomShortcutModel> builtInShortcuts)
665+
private static Query ConstructQuery(string queryText, IEnumerable<CustomShortcutModel> customShortcuts, IEnumerable<BuiltinShortcutModel> builtInShortcuts)
666666
{
667667
StringBuilder queryBuilder = new(queryText);
668668

Flow.Launcher/ViewModel/SettingWindowViewModel.cs

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ public SettingWindowViewModel(Updater updater, IPortable portable)
4444
break;
4545
}
4646
};
47-
ShortCuts = new ObservableCollection<CustomShortcutModel>(Settings.CustomShortcuts.Union(Settings.BuiltinShortcuts));
4847
}
4948

5049
public Settings Settings { get; set; }
@@ -181,7 +180,7 @@ public List<string> QuerySearchPrecisionStrings
181180
public List<Language> Languages => _translater.LoadAvailableLanguages();
182181
public IEnumerable<int> MaxResultsRange => Enumerable.Range(2, 16);
183182

184-
public ObservableCollection<CustomShortcutModel> ShortCuts { get; set; } = new ObservableCollection<CustomShortcutModel>();
183+
public ObservableCollection<CustomShortcutModel> ShortCuts => Settings.CustomShortcuts;
185184

186185
public string TestProxy()
187186
{
@@ -538,30 +537,12 @@ public FamilyTypeface SelectedResultFontFaces
538537

539538
#endregion
540539

541-
#region hotkey
540+
#region hotkey & shortcut
542541

543542
public CustomPluginHotkey SelectedCustomPluginHotkey { get; set; }
544543

545544
public CustomShortcutModel? SelectedCustomShortcut { get; set; }
546545

547-
public void AddShortcut(CustomShortcutModel shortcut)
548-
{
549-
Settings.CustomShortcuts.Add(shortcut);
550-
ShortCuts.Add(shortcut);
551-
}
552-
553-
public void EditShortcut(CustomShortcutModel oldShortcut, CustomShortcutModel newShortcut)
554-
{
555-
RemoveShortcut(oldShortcut);
556-
AddShortcut(newShortcut);
557-
}
558-
559-
public void RemoveShortcut(CustomShortcutModel shortcut)
560-
{
561-
Settings.CustomShortcuts.Remove(shortcut);
562-
ShortCuts.Remove(shortcut);
563-
}
564-
565546
#endregion
566547

567548
#region about

0 commit comments

Comments
 (0)