Skip to content

Commit 8c31039

Browse files
committed
refactor- point CustomQueryHotkey to HotkeyMapper
1 parent b96a02b commit 8c31039

File tree

3 files changed

+26
-53
lines changed

3 files changed

+26
-53
lines changed

Flow.Launcher/CustomQueryHotkeySetting.xaml.cs

Lines changed: 4 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Flow.Launcher.Core.Resource;
2+
using Flow.Launcher.Helper;
23
using Flow.Launcher.Infrastructure.Hotkey;
34
using Flow.Launcher.Infrastructure.UserSettings;
45
using NHotkey;
@@ -52,11 +53,7 @@ private void btnAdd_OnClick(object sender, RoutedEventArgs e)
5253
};
5354
_settings.CustomPluginHotkeys.Add(pluginHotkey);
5455

55-
SetHotkey(ctlHotkey.CurrentHotkey, delegate
56-
{
57-
App.API.ChangeQuery(pluginHotkey.ActionKeyword);
58-
ShowMainWindow();
59-
});
56+
HotKeyMapper.SetCustomQueryHotkey(pluginHotkey);
6057
}
6158
else
6259
{
@@ -69,22 +66,11 @@ private void btnAdd_OnClick(object sender, RoutedEventArgs e)
6966
updateCustomHotkey.ActionKeyword = tbAction.Text;
7067
updateCustomHotkey.Hotkey = ctlHotkey.CurrentHotkey.ToString();
7168
//remove origin hotkey
72-
RemoveHotkey(oldHotkey);
73-
SetHotkey(new HotkeyModel(updateCustomHotkey.Hotkey), delegate
74-
{
75-
App.API.ChangeQuery(updateCustomHotkey.ActionKeyword);
76-
ShowMainWindow();
77-
});
69+
HotKeyMapper.RemoveHotkey(oldHotkey);
70+
HotKeyMapper.SetCustomQueryHotkey(updateCustomHotkey);
7871
}
7972

8073
Close();
81-
82-
static void ShowMainWindow()
83-
{
84-
Window mainWindow = Application.Current.MainWindow;
85-
mainWindow.Visibility = Visibility.Visible;
86-
mainWindow.Focus();
87-
}
8874
}
8975

9076
public void UpdateItem(CustomPluginHotkey item)
@@ -109,28 +95,6 @@ private void BtnTestActionKeyword_OnClick(object sender, RoutedEventArgs e)
10995
Application.Current.MainWindow.Visibility = Visibility.Visible;
11096
}
11197

112-
private void RemoveHotkey(string hotkeyStr)
113-
{
114-
if (!string.IsNullOrEmpty(hotkeyStr))
115-
{
116-
HotkeyManager.Current.Remove(hotkeyStr);
117-
}
118-
}
119-
120-
private void SetHotkey(HotkeyModel hotkey, EventHandler<HotkeyEventArgs> action)
121-
{
122-
string hotkeyStr = hotkey.ToString();
123-
try
124-
{
125-
HotkeyManager.Current.AddOrReplace(hotkeyStr, hotkey.CharKey, hotkey.ModifierKeys, action);
126-
}
127-
catch (Exception)
128-
{
129-
string errorMsg = string.Format(InternationalizationManager.Instance.GetTranslation("registerHotkeyFailed"), hotkeyStr);
130-
MessageBox.Show(errorMsg);
131-
}
132-
}
133-
13498
private void cmdEsc_OnPress(object sender, ExecutedRoutedEventArgs e)
13599
{
136100
Close();

Flow.Launcher/Helper/HotKeyMapper.cs

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ internal static void Initialize(MainViewModel mainVM)
2020
settings = mainViewModel._settings;
2121

2222
SetHotkey(settings.Hotkey, OnHotkey);
23-
SetCustomPluginHotkey();
23+
LoadCustomPluginHotkey();
2424
}
2525

2626
private static void SetHotkey(string hotkeyStr, EventHandler<HotkeyEventArgs> action)
@@ -91,22 +91,29 @@ private static void UpdateLastQUeryMode()
9191
}
9292
}
9393

94-
private static void SetCustomPluginHotkey()
94+
internal static void LoadCustomPluginHotkey()
9595
{
96-
if (settings.CustomPluginHotkeys == null) return;
96+
if (settings.CustomPluginHotkeys == null)
97+
return;
98+
9799
foreach (CustomPluginHotkey hotkey in settings.CustomPluginHotkeys)
98100
{
99-
SetHotkey(hotkey.Hotkey, (s, e) =>
100-
{
101-
if (ShouldIgnoreHotkeys())
102-
return;
101+
SetCustomQueryHotkey(hotkey);
102+
}
103+
}
104+
105+
internal static void SetCustomQueryHotkey(CustomPluginHotkey hotkey)
106+
{
107+
SetHotkey(hotkey.Hotkey, (s, e) =>
108+
{
109+
if (ShouldIgnoreHotkeys())
110+
return;
103111

104-
UpdateLastQUeryMode();
112+
UpdateLastQUeryMode();
105113

106-
mainViewModel.MainWindowVisibility = Visibility.Visible;
107-
mainViewModel.ChangeQueryText(hotkey.ActionKeyword);
108-
});
109-
}
114+
mainViewModel.MainWindowVisibility = Visibility.Visible;
115+
mainViewModel.ChangeQueryText(hotkey.ActionKeyword);
116+
});
110117
}
111118
}
112119
}

Flow.Launcher/HotkeyControl.xaml.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ public partial class HotkeyControl : UserControl
2121
protected virtual void OnHotkeyChanged()
2222
{
2323
EventHandler handler = HotkeyChanged;
24-
if (handler != null) handler(this, EventArgs.Empty);
24+
25+
if (handler != null)
26+
handler(this, EventArgs.Empty);
2527
}
2628

2729
public HotkeyControl()

0 commit comments

Comments
 (0)