Skip to content

Commit 48fb0dc

Browse files
committed
move and centralise the hotkey management calls
1 parent b39d21c commit 48fb0dc

File tree

4 files changed

+120
-127
lines changed

4 files changed

+120
-127
lines changed

Flow.Launcher/App.xaml.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ await Stopwatch.NormalAsync("|App.OnStartup|Startup cost", async () =>
6868

6969
PluginManager.LoadPlugins(_settings.PluginSettings);
7070
_mainVM = new MainViewModel(_settings);
71+
72+
HotKeyMapper.Initialize(_mainVM);
73+
7174
API = new PublicAPIInstance(_settingsVM, _mainVM, _alphabet);
7275

7376
Http.API = API;

Flow.Launcher/Helper/HotKeyMapper.cs

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
using Flow.Launcher.Infrastructure.Hotkey;
2+
using Flow.Launcher.Infrastructure.UserSettings;
3+
using System;
4+
using NHotkey;
5+
using NHotkey.Wpf;
6+
using Flow.Launcher.Core.Resource;
7+
using System.Windows;
8+
using Flow.Launcher.ViewModel;
9+
10+
namespace Flow.Launcher.Helper
11+
{
12+
internal static class HotKeyMapper
13+
{
14+
private static Settings settings;
15+
private static MainViewModel mainViewModel;
16+
17+
internal static void Initialize(MainViewModel mainVM)
18+
{
19+
mainViewModel = mainVM;
20+
settings = mainViewModel._settings;
21+
22+
SetHotkey(settings.Hotkey, OnHotkey);
23+
SetCustomPluginHotkey();
24+
}
25+
26+
private static void SetHotkey(string hotkeyStr, EventHandler<HotkeyEventArgs> action)
27+
{
28+
var hotkey = new HotkeyModel(hotkeyStr);
29+
SetHotkey(hotkey, action);
30+
}
31+
32+
internal static void SetHotkey(HotkeyModel hotkey, EventHandler<HotkeyEventArgs> action)
33+
{
34+
string hotkeyStr = hotkey.ToString();
35+
try
36+
{
37+
HotkeyManager.Current.AddOrReplace(hotkeyStr, hotkey.CharKey, hotkey.ModifierKeys, action);
38+
}
39+
catch (Exception)
40+
{
41+
string errorMsg =
42+
string.Format(InternationalizationManager.Instance.GetTranslation("registerHotkeyFailed"),
43+
hotkeyStr);
44+
MessageBox.Show(errorMsg);
45+
}
46+
}
47+
48+
internal static void RemoveHotkey(string hotkeyStr)
49+
{
50+
if (!string.IsNullOrEmpty(hotkeyStr))
51+
{
52+
HotkeyManager.Current.Remove(hotkeyStr);
53+
}
54+
}
55+
56+
internal static void OnHotkey(object sender, HotkeyEventArgs e)
57+
{
58+
if (!ShouldIgnoreHotkeys())
59+
{
60+
if (settings.LastQueryMode == LastQueryMode.Empty)
61+
{
62+
mainViewModel.ChangeQueryText(string.Empty);
63+
}
64+
else if (settings.LastQueryMode == LastQueryMode.Preserved)
65+
{
66+
mainViewModel.LastQuerySelected = true;
67+
}
68+
else if (settings.LastQueryMode == LastQueryMode.Selected)
69+
{
70+
mainViewModel.LastQuerySelected = false;
71+
}
72+
else
73+
{
74+
throw new ArgumentException($"wrong LastQueryMode: <{settings.LastQueryMode}>");
75+
}
76+
77+
mainViewModel.ToggleFlowLauncher();
78+
e.Handled = true;
79+
}
80+
}
81+
82+
/// <summary>
83+
/// Checks if Flow Launcher should ignore any hotkeys
84+
/// </summary>
85+
/// <returns></returns>
86+
private static bool ShouldIgnoreHotkeys()
87+
{
88+
//double if to omit calling win32 function
89+
if (settings.IgnoreHotkeysOnFullscreen)
90+
if (WindowsInteropHelper.IsWindowFullscreen())
91+
return true;
92+
93+
return false;
94+
}
95+
96+
private static void SetCustomPluginHotkey()
97+
{
98+
if (settings.CustomPluginHotkeys == null) return;
99+
foreach (CustomPluginHotkey hotkey in settings.CustomPluginHotkeys)
100+
{
101+
SetHotkey(hotkey.Hotkey, (s, e) =>
102+
{
103+
if (ShouldIgnoreHotkeys()) return;
104+
mainViewModel.MainWindowVisibility = Visibility.Visible;
105+
mainViewModel.ChangeQueryText(hotkey.ActionKeyword);
106+
});
107+
}
108+
}
109+
}
110+
}

Flow.Launcher/SettingWindow.xaml.cs

Lines changed: 5 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,14 @@
55
using System.Windows.Interop;
66
using System.Windows.Navigation;
77
using Microsoft.Win32;
8-
using NHotkey;
9-
using NHotkey.Wpf;
108
using Flow.Launcher.Core.Plugin;
119
using Flow.Launcher.Core.Resource;
1210
using Flow.Launcher.Infrastructure;
13-
using Flow.Launcher.Infrastructure.Hotkey;
1411
using Flow.Launcher.Infrastructure.UserSettings;
1512
using Flow.Launcher.Plugin;
1613
using Flow.Launcher.Plugin.SharedCommands;
1714
using Flow.Launcher.ViewModel;
15+
using Flow.Launcher.Helper;
1816

1917
namespace Flow.Launcher
2018
{
@@ -127,42 +125,10 @@ void OnHotkeyChanged(object sender, EventArgs e)
127125
{
128126
if (HotkeyControl.CurrentHotkeyAvailable)
129127
{
130-
SetHotkey(HotkeyControl.CurrentHotkey, (o, args) =>
131-
{
132-
if (!Application.Current.MainWindow.IsVisible)
133-
{
134-
Application.Current.MainWindow.Visibility = Visibility.Visible;
135-
}
136-
else
137-
{
138-
Application.Current.MainWindow.Visibility = Visibility.Hidden;
139-
}
140-
});
141-
RemoveHotkey(settings.Hotkey);
142-
settings.Hotkey = HotkeyControl.CurrentHotkey.ToString();
143-
}
144-
}
145128

146-
void SetHotkey(HotkeyModel hotkey, EventHandler<HotkeyEventArgs> action)
147-
{
148-
string hotkeyStr = hotkey.ToString();
149-
try
150-
{
151-
HotkeyManager.Current.AddOrReplace(hotkeyStr, hotkey.CharKey, hotkey.ModifierKeys, action);
152-
}
153-
catch (Exception)
154-
{
155-
string errorMsg =
156-
string.Format(InternationalizationManager.Instance.GetTranslation("registerHotkeyFailed"), hotkeyStr);
157-
MessageBox.Show(errorMsg);
158-
}
159-
}
160-
161-
void RemoveHotkey(string hotkeyStr)
162-
{
163-
if (!string.IsNullOrEmpty(hotkeyStr))
164-
{
165-
HotkeyManager.Current.Remove(hotkeyStr);
129+
HotKeyMapper.SetHotkey(HotkeyControl.CurrentHotkey, HotKeyMapper.OnHotkey);
130+
HotKeyMapper.RemoveHotkey(settings.Hotkey);
131+
settings.Hotkey = HotkeyControl.CurrentHotkey.ToString();
166132
}
167133
}
168134

@@ -183,7 +149,7 @@ private void OnDeleteCustomHotkeyClick(object sender, RoutedEventArgs e)
183149
MessageBoxButton.YesNo) == MessageBoxResult.Yes)
184150
{
185151
settings.CustomPluginHotkeys.Remove(item);
186-
RemoveHotkey(item.Hotkey);
152+
HotKeyMapper.RemoveHotkey(item.Hotkey);
187153
}
188154
}
189155

Flow.Launcher/ViewModel/MainViewModel.cs

Lines changed: 2 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public class MainViewModel : BaseModel, ISavable
3939
private readonly FlowLauncherJsonStorage<History> _historyItemsStorage;
4040
private readonly FlowLauncherJsonStorage<UserSelectedRecord> _userSelectedRecordStorage;
4141
private readonly FlowLauncherJsonStorage<TopMostRecord> _topMostRecordStorage;
42-
private readonly Settings _settings;
42+
internal readonly Settings _settings;
4343
private readonly History _history;
4444
private readonly UserSelectedRecord _userSelectedRecord;
4545
private readonly TopMostRecord _topMostRecord;
@@ -80,8 +80,6 @@ public MainViewModel(Settings settings)
8080
RegisterViewUpdate();
8181
RegisterResultsUpdatedEvent();
8282

83-
SetHotkey(_settings.Hotkey, OnHotkey);
84-
SetCustomPluginHotkey();
8583
SetOpenResultModifiers();
8684
}
8785

@@ -659,96 +657,12 @@ private bool HistorySelected()
659657

660658
#region Hotkey
661659

662-
private void SetHotkey(string hotkeyStr, EventHandler<HotkeyEventArgs> action)
663-
{
664-
var hotkey = new HotkeyModel(hotkeyStr);
665-
SetHotkey(hotkey, action);
666-
}
667-
668-
private void SetHotkey(HotkeyModel hotkey, EventHandler<HotkeyEventArgs> action)
669-
{
670-
string hotkeyStr = hotkey.ToString();
671-
try
672-
{
673-
HotkeyManager.Current.AddOrReplace(hotkeyStr, hotkey.CharKey, hotkey.ModifierKeys, action);
674-
}
675-
catch (Exception)
676-
{
677-
string errorMsg =
678-
string.Format(InternationalizationManager.Instance.GetTranslation("registerHotkeyFailed"),
679-
hotkeyStr);
680-
MessageBox.Show(errorMsg);
681-
}
682-
}
683-
684-
public void RemoveHotkey(string hotkeyStr)
685-
{
686-
if (!string.IsNullOrEmpty(hotkeyStr))
687-
{
688-
HotkeyManager.Current.Remove(hotkeyStr);
689-
}
690-
}
691-
692-
/// <summary>
693-
/// Checks if Flow Launcher should ignore any hotkeys
694-
/// </summary>
695-
/// <returns></returns>
696-
private bool ShouldIgnoreHotkeys()
697-
{
698-
//double if to omit calling win32 function
699-
if (_settings.IgnoreHotkeysOnFullscreen)
700-
if (WindowsInteropHelper.IsWindowFullscreen())
701-
return true;
702-
703-
return false;
704-
}
705-
706-
private void SetCustomPluginHotkey()
707-
{
708-
if (_settings.CustomPluginHotkeys == null) return;
709-
foreach (CustomPluginHotkey hotkey in _settings.CustomPluginHotkeys)
710-
{
711-
SetHotkey(hotkey.Hotkey, (s, e) =>
712-
{
713-
if (ShouldIgnoreHotkeys()) return;
714-
MainWindowVisibility = Visibility.Visible;
715-
ChangeQueryText(hotkey.ActionKeyword);
716-
});
717-
}
718-
}
719-
720660
private void SetOpenResultModifiers()
721661
{
722662
OpenResultCommandModifiers = _settings.OpenResultModifiers ?? DefaultOpenResultModifiers;
723663
}
724664

725-
private void OnHotkey(object sender, HotkeyEventArgs e)
726-
{
727-
if (!ShouldIgnoreHotkeys())
728-
{
729-
if (_settings.LastQueryMode == LastQueryMode.Empty)
730-
{
731-
ChangeQueryText(string.Empty);
732-
}
733-
else if (_settings.LastQueryMode == LastQueryMode.Preserved)
734-
{
735-
LastQuerySelected = true;
736-
}
737-
else if (_settings.LastQueryMode == LastQueryMode.Selected)
738-
{
739-
LastQuerySelected = false;
740-
}
741-
else
742-
{
743-
throw new ArgumentException($"wrong LastQueryMode: <{_settings.LastQueryMode}>");
744-
}
745-
746-
ToggleFlowLauncher();
747-
e.Handled = true;
748-
}
749-
}
750-
751-
private void ToggleFlowLauncher()
665+
internal void ToggleFlowLauncher()
752666
{
753667
if (MainWindowVisibility != Visibility.Visible)
754668
{

0 commit comments

Comments
 (0)