Skip to content

Commit 48d0898

Browse files
authored
Merge pull request #3803 from Flow-Launcher/CustomQueryHotkeySetting_refactor
Refactor CustomQueryHotkeySetting control
2 parents 7a031e3 + 4d5a8cc commit 48d0898

File tree

6 files changed

+103
-57
lines changed

6 files changed

+103
-57
lines changed
Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,32 @@
1-
using Flow.Launcher.Plugin;
1+
using System;
2+
using Flow.Launcher.Plugin;
23

34
namespace Flow.Launcher.Infrastructure.UserSettings
45
{
56
public class CustomPluginHotkey : BaseModel
67
{
78
public string Hotkey { get; set; }
89
public string ActionKeyword { get; set; }
10+
11+
public CustomPluginHotkey(string hotkey, string actionKeyword)
12+
{
13+
Hotkey = hotkey;
14+
ActionKeyword = actionKeyword;
15+
}
16+
17+
public override bool Equals(object other)
18+
{
19+
if (other is CustomPluginHotkey otherHotkey)
20+
{
21+
return Hotkey == otherHotkey.Hotkey && ActionKeyword == otherHotkey.ActionKeyword;
22+
}
23+
24+
return false;
25+
}
26+
27+
public override int GetHashCode()
28+
{
29+
return HashCode.Combine(Hotkey, ActionKeyword);
30+
}
931
}
1032
}

Flow.Launcher/CustomQueryHotkeySetting.xaml

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,8 @@
119119
Grid.Column="1"
120120
Margin="10"
121121
HorizontalAlignment="Stretch"
122-
VerticalAlignment="Center" />
122+
VerticalAlignment="Center"
123+
Text="{Binding ActionKeyword}" />
123124
<Button
124125
x:Name="btnTestActionKeyword"
125126
Grid.Row="1"
@@ -150,7 +151,20 @@
150151
Margin="5 0 10 0"
151152
Click="btnAdd_OnClick"
152153
Style="{StaticResource AccentButtonStyle}">
153-
<TextBlock x:Name="lblAdd" Text="{DynamicResource done}" />
154+
<Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
155+
<TextBlock
156+
x:Name="tbAdd"
157+
HorizontalAlignment="Center"
158+
VerticalAlignment="Center"
159+
Text="{DynamicResource done}"
160+
Visibility="Collapsed" />
161+
<TextBlock
162+
x:Name="tbUpdate"
163+
HorizontalAlignment="Center"
164+
VerticalAlignment="Center"
165+
Text="{DynamicResource update}"
166+
Visibility="Collapsed" />
167+
</Grid>
154168
</Button>
155169
</StackPanel>
156170
</Border>

Flow.Launcher/CustomQueryHotkeySetting.xaml.cs

Lines changed: 25 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,52 @@
1-
using System.Collections.ObjectModel;
2-
using System.Linq;
3-
using System.Windows;
4-
using System.Windows.Input;
1+
using System.Windows;
52
using System.Windows.Controls;
6-
using Flow.Launcher.Helper;
3+
using System.Windows.Input;
74
using Flow.Launcher.Infrastructure.UserSettings;
85

96
namespace Flow.Launcher
107
{
118
public partial class CustomQueryHotkeySetting : Window
129
{
13-
private readonly Settings _settings;
10+
public string Hotkey { get; set; } = string.Empty;
11+
public string ActionKeyword { get; set; } = string.Empty;
1412

15-
private bool update;
16-
private CustomPluginHotkey updateCustomHotkey;
13+
private readonly bool update;
14+
private readonly CustomPluginHotkey originalCustomHotkey;
1715

18-
public CustomQueryHotkeySetting(Settings settings)
16+
public CustomQueryHotkeySetting()
1917
{
20-
_settings = settings;
2118
InitializeComponent();
19+
tbAdd.Visibility = Visibility.Visible;
2220
}
2321

24-
private void BtnCancel_OnClick(object sender, RoutedEventArgs e)
22+
public CustomQueryHotkeySetting(CustomPluginHotkey hotkey)
2523
{
26-
Close();
24+
originalCustomHotkey = hotkey;
25+
update = true;
26+
ActionKeyword = originalCustomHotkey.ActionKeyword;
27+
InitializeComponent();
28+
tbUpdate.Visibility = Visibility.Visible;
29+
HotkeyControl.SetHotkey(originalCustomHotkey.Hotkey, false);
2730
}
2831

29-
private void btnAdd_OnClick(object sender, RoutedEventArgs e)
32+
private void BtnCancel_OnClick(object sender, RoutedEventArgs e)
3033
{
31-
if (!update)
32-
{
33-
_settings.CustomPluginHotkeys ??= new ObservableCollection<CustomPluginHotkey>();
34-
35-
var pluginHotkey = new CustomPluginHotkey
36-
{
37-
Hotkey = HotkeyControl.CurrentHotkey.ToString(), ActionKeyword = tbAction.Text
38-
};
39-
_settings.CustomPluginHotkeys.Add(pluginHotkey);
40-
41-
HotKeyMapper.SetCustomQueryHotkey(pluginHotkey);
42-
}
43-
else
44-
{
45-
var oldHotkey = updateCustomHotkey.Hotkey;
46-
updateCustomHotkey.ActionKeyword = tbAction.Text;
47-
updateCustomHotkey.Hotkey = HotkeyControl.CurrentHotkey.ToString();
48-
//remove origin hotkey
49-
HotKeyMapper.RemoveHotkey(oldHotkey);
50-
HotKeyMapper.SetCustomQueryHotkey(updateCustomHotkey);
51-
}
52-
34+
DialogResult = false;
5335
Close();
5436
}
5537

56-
public void UpdateItem(CustomPluginHotkey item)
38+
private void btnAdd_OnClick(object sender, RoutedEventArgs e)
5739
{
58-
updateCustomHotkey = _settings.CustomPluginHotkeys.FirstOrDefault(o =>
59-
o.ActionKeyword == item.ActionKeyword && o.Hotkey == item.Hotkey);
60-
if (updateCustomHotkey == null)
40+
Hotkey = HotkeyControl.CurrentHotkey.ToString();
41+
42+
if (string.IsNullOrEmpty(Hotkey) && string.IsNullOrEmpty(ActionKeyword))
6143
{
62-
App.API.ShowMsgBox(App.API.GetTranslation("invalidPluginHotkey"));
63-
Close();
44+
App.API.ShowMsgBox(App.API.GetTranslation("emptyPluginHotkey"));
6445
return;
6546
}
6647

67-
tbAction.Text = updateCustomHotkey.ActionKeyword;
68-
HotkeyControl.SetHotkey(updateCustomHotkey.Hotkey, false);
69-
update = true;
70-
lblAdd.Text = App.API.GetTranslation("update");
48+
DialogResult = !update || originalCustomHotkey.Hotkey != Hotkey || originalCustomHotkey.ActionKeyword != ActionKeyword;
49+
Close();
7150
}
7251

7352
private void BtnTestActionKeyword_OnClick(object sender, RoutedEventArgs e)
@@ -79,6 +58,7 @@ private void BtnTestActionKeyword_OnClick(object sender, RoutedEventArgs e)
7958

8059
private void cmdEsc_OnPress(object sender, ExecutedRoutedEventArgs e)
8160
{
61+
DialogResult = false;
8262
Close();
8363
}
8464

Flow.Launcher/CustomShortcutSetting.xaml.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,14 @@ private void BtnAdd_OnClick(object sender, RoutedEventArgs e)
4343
App.API.ShowMsgBox(App.API.GetTranslation("emptyShortcut"));
4444
return;
4545
}
46+
4647
// Check if key is modified or adding a new one
4748
if (((update && originalKey != Key) || !update) && _hotkeyVm.DoesShortcutExist(Key))
4849
{
4950
App.API.ShowMsgBox(App.API.GetTranslation("duplicateShortcut"));
5051
return;
5152
}
53+
5254
DialogResult = !update || originalKey != Key || originalValue != Value;
5355
Close();
5456
}

Flow.Launcher/Languages/en.xaml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<system:String x:Key="runtimeExecutableInvalidChooseDownload">
1313
Your selected {0} executable is invalid.
1414
{2}{2}
15-
Click yes if you would like select the {0} executable agian. Click no if you would like to download {1}
15+
Click yes if you would like select the {0} executable again. Click no if you would like to download {1}
1616
</system:String>
1717
<system:String x:Key="runtimePluginUnableToSetExecutablePath">Unable to set {0} executable path, please try from Flow's settings (scroll down to the bottom).</system:String>
1818
<system:String x:Key="failedToInitializePluginsTitle">Fail to Init Plugins</system:String>
@@ -410,7 +410,7 @@
410410
<system:String x:Key="fileManagerWindow">Select File Manager</system:String>
411411
<system:String x:Key="fileManager_learnMore">Learn more</system:String>
412412
<system:String x:Key="fileManager_tips">Please specify the file location of the file manager you using and add arguments as required. The &quot;%d&quot; represents the directory path to open for, used by the Arg for Folder field and for commands opening specific directories. The &quot;%f&quot; represents the file path to open for, used by the Arg for File field and for commands opening specific files.</system:String>
413-
<system:String x:Key="fileManager_tips2">For example, if the file manager uses a command such as &quot;totalcmd.exe /A c:\windows&quot; to open the c:\windows directory, the File Manager Path will be totalcmd.exe, and the Arg For Folder will be /A &quot;%d&quot;. Certain file managers like QTTabBar may just require a path to be supplied, in this instance use &quot;%d&quot; as the File Manager Path and leave the rest of the fileds blank.</system:String>
413+
<system:String x:Key="fileManager_tips2">For example, if the file manager uses a command such as &quot;totalcmd.exe /A c:\windows&quot; to open the c:\windows directory, the File Manager Path will be totalcmd.exe, and the Arg For Folder will be /A &quot;%d&quot;. Certain file managers like QTTabBar may just require a path to be supplied, in this instance use &quot;%d&quot; as the File Manager Path and leave the rest of the fields blank.</system:String>
414414
<system:String x:Key="fileManager_name">File Manager</system:String>
415415
<system:String x:Key="fileManager_profile_name">Profile Name</system:String>
416416
<system:String x:Key="fileManager_path">File Manager Path</system:String>
@@ -461,13 +461,14 @@
461461
<system:String x:Key="customeQueryHotkeyTips">Press a custom hotkey to open Flow Launcher and input the specified query automatically.</system:String>
462462
<system:String x:Key="preview">Preview</system:String>
463463
<system:String x:Key="hotkeyIsNotUnavailable">Hotkey is unavailable, please select a new hotkey</system:String>
464-
<system:String x:Key="invalidPluginHotkey">Invalid plugin hotkey</system:String>
464+
<system:String x:Key="invalidPluginHotkey">Hotkey is invalid</system:String>
465465
<system:String x:Key="update">Update</system:String>
466466
<system:String x:Key="hotkeyRegTitle">Binding Hotkey</system:String>
467467
<system:String x:Key="hotkeyUnavailable">Current hotkey is unavailable.</system:String>
468468
<system:String x:Key="hotkeyUnavailableUneditable">This hotkey is reserved for "{0}" and can't be used. Please choose another hotkey.</system:String>
469469
<system:String x:Key="hotkeyUnavailableEditable">This hotkey is already in use by "{0}". If you press "Overwrite", it will be removed from "{0}".</system:String>
470470
<system:String x:Key="hotkeyRegGuide">Press the keys you want to use for this function.</system:String>
471+
<system:String x:Key="emptyPluginHotkey">Hotkey and action keyword are empty</system:String>
471472

472473
<!-- Custom Query Shortcut Dialog -->
473474
<system:String x:Key="customeQueryShortcutTitle">Custom Query Shortcut</system:String>
@@ -476,6 +477,7 @@
476477
</system:String>
477478
<system:String x:Key="duplicateShortcut">Shortcut already exists, please enter a new Shortcut or edit the existing one.</system:String>
478479
<system:String x:Key="emptyShortcut">Shortcut and/or its expansion is empty.</system:String>
480+
<system:String x:Key="invalidShortcut">Shortcut is invalid</system:String>
479481

480482
<!-- Common Action -->
481483
<system:String x:Key="commonSave">Save</system:String>

Flow.Launcher/SettingPages/ViewModels/SettingsPaneHotkeyViewModel.cs

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,33 @@ private void CustomHotkeyEdit()
6969
return;
7070
}
7171

72-
var window = new CustomQueryHotkeySetting(Settings);
73-
window.UpdateItem(item);
74-
window.ShowDialog();
72+
var settingItem = Settings.CustomPluginHotkeys.FirstOrDefault(o =>
73+
o.ActionKeyword == item.ActionKeyword && o.Hotkey == item.Hotkey);
74+
if (settingItem == null)
75+
{
76+
App.API.ShowMsgBox(App.API.GetTranslation("invalidPluginHotkey"));
77+
return;
78+
}
79+
80+
var window = new CustomQueryHotkeySetting(settingItem);
81+
if (window.ShowDialog() is not true) return;
82+
83+
var index = Settings.CustomPluginHotkeys.IndexOf(settingItem);
84+
Settings.CustomPluginHotkeys[index] = new CustomPluginHotkey(window.Hotkey, window.ActionKeyword);
85+
HotKeyMapper.RemoveHotkey(settingItem.Hotkey); // remove origin hotkey
86+
HotKeyMapper.SetCustomQueryHotkey(Settings.CustomPluginHotkeys[index]); // set new hotkey
7587
}
7688

7789
[RelayCommand]
7890
private void CustomHotkeyAdd()
7991
{
80-
new CustomQueryHotkeySetting(Settings).ShowDialog();
92+
var window = new CustomQueryHotkeySetting();
93+
if (window.ShowDialog() is true)
94+
{
95+
var customHotkey = new CustomPluginHotkey(window.Hotkey, window.ActionKeyword);
96+
Settings.CustomPluginHotkeys.Add(customHotkey);
97+
HotKeyMapper.SetCustomQueryHotkey(customHotkey); // set new hotkey
98+
}
8199
}
82100

83101
[RelayCommand]
@@ -114,10 +132,18 @@ private void CustomShortcutEdit()
114132
return;
115133
}
116134

117-
var window = new CustomShortcutSetting(item.Key, item.Value, this);
135+
var settingItem = Settings.CustomShortcuts.FirstOrDefault(o =>
136+
o.Key == item.Key && o.Value == item.Value);
137+
if (settingItem == null)
138+
{
139+
App.API.ShowMsgBox(App.API.GetTranslation("invalidShortcut"));
140+
return;
141+
}
142+
143+
var window = new CustomShortcutSetting(settingItem.Key, settingItem.Value, this);
118144
if (window.ShowDialog() is not true) return;
119145

120-
var index = Settings.CustomShortcuts.IndexOf(item);
146+
var index = Settings.CustomShortcuts.IndexOf(settingItem);
121147
Settings.CustomShortcuts[index] = new CustomShortcutModel(window.Key, window.Value);
122148
}
123149

0 commit comments

Comments
 (0)