Skip to content

Commit 5879c84

Browse files
Show builtin vars in shortcut list
1 parent f3c4120 commit 5879c84

File tree

8 files changed

+79
-34
lines changed

8 files changed

+79
-34
lines changed

Flow.Launcher.Infrastructure/UserSettings/CustomShortcutModel.cs

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,46 @@
1-
using System;
1+
using NLog.Time;
2+
using System;
3+
using System.Security.Cryptography.X509Certificates;
4+
using System.Security.RightsManagement;
25
using System.Text.Json.Serialization;
36

47
namespace Flow.Launcher.Infrastructure.UserSettings
58
{
6-
public class CustomShortcutBaseModel
9+
10+
public class CustomShortcutModel
711
{
812
public string Key { get; set; }
13+
public string Value { get; set; }
14+
public bool CanBeEdited { get; private set; } // Can be edited by user via dialog
915

1016
[JsonIgnore]
1117
public Func<string> Expand { get; set; } = () => { return ""; };
1218

13-
public override bool Equals(object obj)
19+
public CustomShortcutModel(string key, string value)
1420
{
15-
return obj is CustomShortcutBaseModel other &&
16-
Key == other.Key;
21+
Key = key;
22+
Value = value;
23+
CanBeEdited = true;
24+
Expand = () => { return Value; };
1725
}
1826

19-
public override int GetHashCode()
27+
public CustomShortcutModel(string key, string description, Func<string> expand)
2028
{
21-
return HashCode.Combine(Key);
29+
Key = key;
30+
Value = description;
31+
CanBeEdited = false;
32+
Expand = expand;
2233
}
23-
};
2434

25-
public class CustomShortcutModel : CustomShortcutBaseModel
26-
{
27-
public string Value
35+
public override bool Equals(object obj)
2836
{
29-
get { return Expand(); }
30-
set { Expand = () => { return value; }; }
37+
return obj is CustomShortcutModel other &&
38+
Key == other.Key;
3139
}
3240

33-
public CustomShortcutModel(string key, string value)
41+
public override int GetHashCode()
3442
{
35-
Key = key;
36-
Value = value;
43+
return HashCode.Combine(Key);
3744
}
3845

3946
public void Deconstruct(out string key, out string value)

Flow.Launcher.Infrastructure/UserSettings/Settings.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
using System.Collections.ObjectModel;
44
using System.Drawing;
55
using System.Text.Json.Serialization;
6+
using System.Windows;
67
using Flow.Launcher.Plugin;
78
using Flow.Launcher.Plugin.SharedModels;
89
using Flow.Launcher;
910
using Flow.Launcher.ViewModel;
10-
1111
namespace Flow.Launcher.Infrastructure.UserSettings
1212
{
1313
public class Settings : BaseModel
@@ -175,8 +175,14 @@ public string QuerySearchPrecisionString
175175

176176

177177
public ObservableCollection<CustomPluginHotkey> CustomPluginHotkeys { get; set; } = new ObservableCollection<CustomPluginHotkey>();
178+
178179
public ObservableCollection<CustomShortcutModel> CustomShortcuts { get; set; } = new ObservableCollection<CustomShortcutModel>();
179180

181+
[JsonIgnore]
182+
public ObservableCollection<CustomShortcutModel> BuiltinShortcuts { get; set; } = new ObservableCollection<CustomShortcutModel>() {
183+
new CustomShortcutModel("{clipboard}", "Get text from clipboard.", Clipboard.GetText)
184+
};
185+
180186
public bool DontPromptUpdateMsg { get; set; }
181187
public bool EnableUpdateLog { get; set; }
182188

Flow.Launcher/CustomShortcutSetting.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@
149149
x:Name="btnAdd"
150150
MinWidth="140"
151151
Margin="5,0,10,0"
152-
Click="btnAdd_OnClick"
152+
Click="BtnAdd_OnClick"
153153
Style="{StaticResource AccentButtonStyle}">
154154
<TextBlock x:Name="lblAdd" Text="{DynamicResource done}" />
155155
</Button>

Flow.Launcher/CustomShortcutSetting.xaml.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ private void BtnCancel_OnClick(object sender, RoutedEventArgs e)
3434
Close();
3535
}
3636

37-
private void btnAdd_OnClick(object sender, RoutedEventArgs e)
37+
private void BtnAdd_OnClick(object sender, RoutedEventArgs e)
3838
{
39-
if (!update && _settings.CustomShortcuts.Contains(new CustomShortcutModel(Key, Value)))
39+
if (!update && (_settings.CustomShortcuts.Contains(new CustomShortcutModel(Key, Value)) || _settings.BuiltinShortcuts.Contains(new CustomShortcutModel(Key, Value))))
4040
{
4141
MessageBox.Show(InternationalizationManager.Instance.GetTranslation("dulplicateShortcut"));
4242
return;

Flow.Launcher/SettingWindow.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2223,7 +2223,7 @@
22232223
<Button
22242224
MinWidth="100"
22252225
Margin="10,10,0,10"
2226-
Click="OnAddCustomeShortCutClick"
2226+
Click="OnAddCustomShortCutClick"
22272227
Content="{DynamicResource add}" />
22282228
</StackPanel>
22292229
</StackPanel>

Flow.Launcher/SettingWindow.xaml.cs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,11 @@ 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("This shortcut cannot be deleted or edited."); // TODO
387+
return;
388+
}
384389

385390
string deleteWarning =
386391
string.Format(InternationalizationManager.Instance.GetTranslation("deleteCustomShortcutWarning"),
@@ -389,7 +394,7 @@ private void OnDeleteCustomShortCutClick(object sender, RoutedEventArgs e)
389394
MessageBox.Show(deleteWarning, InternationalizationManager.Instance.GetTranslation("delete"),
390395
MessageBoxButton.YesNo) == MessageBoxResult.Yes)
391396
{
392-
settings.CustomShortcuts.Remove(item);
397+
viewModel.RemoveShortcut(item);
393398
}
394399
}
395400

@@ -398,10 +403,15 @@ private void OnEditCustomShortCutClick(object sender, RoutedEventArgs e)
398403
var item = viewModel.SelectedCustomShortcut;
399404
if (item != null)
400405
{
406+
if (!item.CanBeEdited)
407+
{
408+
MessageBox.Show("This shortcut cannot be deleted or edited.");
409+
return;
410+
}
401411
var shortcutSettingWindow = new CustomShortcutSetting(item, settings);
402412
if (shortcutSettingWindow.ShowDialog() == true)
403413
{
404-
settings.CustomShortcuts[viewModel.SelectCustomShortcutIndex.Value] = shortcutSettingWindow.ShortCut;
414+
viewModel.EditShortcut(item);
405415
}
406416
}
407417
else
@@ -410,12 +420,12 @@ private void OnEditCustomShortCutClick(object sender, RoutedEventArgs e)
410420
}
411421
}
412422

413-
private void OnAddCustomeShortCutClick(object sender, RoutedEventArgs e)
423+
private void OnAddCustomShortCutClick(object sender, RoutedEventArgs e)
414424
{
415425
var shortcutSettingWindow = new CustomShortcutSetting(settings);
416426
if (shortcutSettingWindow.ShowDialog() == true)
417427
{
418-
settings.CustomShortcuts.Add(shortcutSettingWindow.ShortCut);
428+
viewModel.AddShortcut(shortcutSettingWindow.ShortCut);
419429
}
420430
}
421431

Flow.Launcher/ViewModel/MainViewModel.cs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,7 @@ private async void QueryResults()
554554
return;
555555
}
556556

557-
var query = ConstructQuery(QueryText, _settings.CustomShortcuts);
557+
var query = ConstructQuery(QueryText, _settings.CustomShortcuts, _settings.BuiltinShortcuts);
558558

559559

560560
_updateSource?.Dispose();
@@ -662,21 +662,24 @@ async Task QueryTask(PluginPair plugin)
662662
}
663663
}
664664

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

669-
foreach (var (key, value) in shortcuts)
669+
foreach (var shortcut in customShortcuts)
670670
{
671-
if (queryBuilder.Equals(key))
671+
if (queryBuilder.Equals(shortcut.Key))
672672
{
673-
queryBuilder.Replace(key, value);
673+
queryBuilder.Replace(shortcut.Key, shortcut.Expand());
674674
}
675675

676-
queryBuilder.Replace('@' + key, value);
676+
queryBuilder.Replace('@' + shortcut.Key, shortcut.Expand());
677+
}
678+
679+
foreach (var shortcut in builtInShortcuts)
680+
{
681+
queryBuilder.Replace(shortcut.Key, shortcut.Expand());
677682
}
678-
679-
queryBuilder.Replace("{clipboard}", Clipboard.GetText());
680683

681684
var query = QueryBuilder.Build(queryBuilder.ToString().Trim(), PluginManager.NonGlobalPlugins);
682685
return query;

Flow.Launcher/ViewModel/SettingWindowViewModel.cs

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

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

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

185186
public string TestProxy()
186187
{
@@ -544,6 +545,24 @@ public FamilyTypeface SelectedResultFontFaces
544545
public CustomShortcutModel? SelectedCustomShortcut { get; set; }
545546
public int? SelectCustomShortcutIndex { get; set; }
546547

548+
public void AddShortcut(CustomShortcutModel shortcut)
549+
{
550+
Settings.CustomShortcuts.Add(shortcut);
551+
ShortCuts.Add(shortcut);
552+
}
553+
554+
public void EditShortcut(CustomShortcutModel shortcut)
555+
{
556+
Settings.CustomShortcuts[ShortCuts.IndexOf(shortcut)] = shortcut;
557+
ShortCuts[ShortCuts.IndexOf(shortcut)] = shortcut;
558+
}
559+
560+
public void RemoveShortcut(CustomShortcutModel shortcut)
561+
{
562+
Settings.CustomShortcuts.Remove(shortcut);
563+
ShortCuts.Remove(shortcut);
564+
}
565+
547566
#endregion
548567

549568
#region about

0 commit comments

Comments
 (0)