Skip to content

Commit eb2b8d0

Browse files
committed
Refactor CustomQueryHotkeySetting control
1 parent 7c41a37 commit eb2b8d0

File tree

4 files changed

+76
-54
lines changed

4 files changed

+76
-54
lines changed

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="lblAdd"
157+
HorizontalAlignment="Center"
158+
VerticalAlignment="Center"
159+
Text="{DynamicResource done}"
160+
Visibility="Collapsed" />
161+
<TextBlock
162+
x:Name="lblUpdate"
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+
lblAdd.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+
lblUpdate.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/Languages/en.xaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -429,13 +429,14 @@
429429
<system:String x:Key="customeQueryHotkeyTips">Press a custom hotkey to open Flow Launcher and input the specified query automatically.</system:String>
430430
<system:String x:Key="preview">Preview</system:String>
431431
<system:String x:Key="hotkeyIsNotUnavailable">Hotkey is unavailable, please select a new hotkey</system:String>
432-
<system:String x:Key="invalidPluginHotkey">Invalid plugin hotkey</system:String>
432+
<system:String x:Key="invalidPluginHotkey">Hotkey is invalid</system:String>
433433
<system:String x:Key="update">Update</system:String>
434434
<system:String x:Key="hotkeyRegTitle">Binding Hotkey</system:String>
435435
<system:String x:Key="hotkeyUnavailable">Current hotkey is unavailable.</system:String>
436436
<system:String x:Key="hotkeyUnavailableUneditable">This hotkey is reserved for "{0}" and can't be used. Please choose another hotkey.</system:String>
437437
<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>
438438
<system:String x:Key="hotkeyRegGuide">Press the keys you want to use for this function.</system:String>
439+
<system:String x:Key="emptyPluginHotkey">Hotkey and action keyword are empty</system:String>
439440

440441
<!-- Custom Query Shortcut Dialog -->
441442
<system:String x:Key="customeQueryShortcutTitle">Custom Query Shortcut</system:String>
@@ -444,6 +445,7 @@
444445
</system:String>
445446
<system:String x:Key="duplicateShortcut">Shortcut already exists, please enter a new Shortcut or edit the existing one.</system:String>
446447
<system:String x:Key="emptyShortcut">Shortcut and/or its expansion is empty.</system:String>
448+
<system:String x:Key="invalidShortcut">Shortcut is invalid</system:String>
447449

448450
<!-- Common Action -->
449451
<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)