Skip to content

Commit 8d23f98

Browse files
authored
Merge pull request #565 from Flow-Launcher/fix_hotkey_change
Fix flow's hotkey not working correctly after changing in settings
2 parents 2a07599 + 4ba8296 commit 8d23f98

File tree

9 files changed

+160
-200
lines changed

9 files changed

+160
-200
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/CustomQueryHotkeySetting.xaml.cs

Lines changed: 5 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
using Flow.Launcher.Core.Resource;
2+
using Flow.Launcher.Helper;
23
using Flow.Launcher.Infrastructure.Hotkey;
34
using Flow.Launcher.Infrastructure.UserSettings;
4-
using NHotkey;
5-
using NHotkey.Wpf;
65
using System;
76
using System.Collections.ObjectModel;
87
using System.Linq;
@@ -52,11 +51,7 @@ private void btnAdd_OnClick(object sender, RoutedEventArgs e)
5251
};
5352
_settings.CustomPluginHotkeys.Add(pluginHotkey);
5453

55-
SetHotkey(ctlHotkey.CurrentHotkey, delegate
56-
{
57-
App.API.ChangeQuery(pluginHotkey.ActionKeyword);
58-
ShowMainWindow();
59-
});
54+
HotKeyMapper.SetCustomQueryHotkey(pluginHotkey);
6055
}
6156
else
6257
{
@@ -69,22 +64,11 @@ private void btnAdd_OnClick(object sender, RoutedEventArgs e)
6964
updateCustomHotkey.ActionKeyword = tbAction.Text;
7065
updateCustomHotkey.Hotkey = ctlHotkey.CurrentHotkey.ToString();
7166
//remove origin hotkey
72-
RemoveHotkey(oldHotkey);
73-
SetHotkey(new HotkeyModel(updateCustomHotkey.Hotkey), delegate
74-
{
75-
App.API.ChangeQuery(updateCustomHotkey.ActionKeyword);
76-
ShowMainWindow();
77-
});
67+
HotKeyMapper.RemoveHotkey(oldHotkey);
68+
HotKeyMapper.SetCustomQueryHotkey(updateCustomHotkey);
7869
}
7970

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

9074
public void UpdateItem(CustomPluginHotkey item)
@@ -107,28 +91,8 @@ private void BtnTestActionKeyword_OnClick(object sender, RoutedEventArgs e)
10791
{
10892
App.API.ChangeQuery(tbAction.Text);
10993
Application.Current.MainWindow.Visibility = Visibility.Visible;
110-
}
111-
112-
private void RemoveHotkey(string hotkeyStr)
113-
{
114-
if (!string.IsNullOrEmpty(hotkeyStr))
115-
{
116-
HotkeyManager.Current.Remove(hotkeyStr);
117-
}
118-
}
94+
Application.Current.MainWindow.Focus();
11995

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-
}
13296
}
13397

13498
private void cmdEsc_OnPress(object sender, ExecutedRoutedEventArgs e)

Flow.Launcher/Helper/HotKeyMapper.cs

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
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+
LoadCustomPluginHotkey();
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+
UpdateLastQUeryMode();
61+
62+
mainViewModel.ToggleFlowLauncher();
63+
e.Handled = true;
64+
}
65+
}
66+
67+
/// <summary>
68+
/// Checks if Flow Launcher should ignore any hotkeys
69+
/// </summary>
70+
private static bool ShouldIgnoreHotkeys()
71+
{
72+
return settings.IgnoreHotkeysOnFullscreen && WindowsInteropHelper.IsWindowFullscreen();
73+
}
74+
75+
private static void UpdateLastQUeryMode()
76+
{
77+
switch(settings.LastQueryMode)
78+
{
79+
case LastQueryMode.Empty:
80+
mainViewModel.ChangeQueryText(string.Empty);
81+
break;
82+
case LastQueryMode.Preserved:
83+
mainViewModel.LastQuerySelected = true;
84+
break;
85+
case LastQueryMode.Selected:
86+
mainViewModel.LastQuerySelected = false;
87+
break;
88+
default:
89+
throw new ArgumentException($"wrong LastQueryMode: <{settings.LastQueryMode}>");
90+
91+
}
92+
}
93+
94+
internal static void LoadCustomPluginHotkey()
95+
{
96+
if (settings.CustomPluginHotkeys == null)
97+
return;
98+
99+
foreach (CustomPluginHotkey hotkey in settings.CustomPluginHotkeys)
100+
{
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;
111+
112+
mainViewModel.MainWindowVisibility = Visibility.Visible;
113+
mainViewModel.ChangeQueryText(hotkey.ActionKeyword);
114+
});
115+
}
116+
117+
internal static bool CheckAvailability(HotkeyModel currentHotkey)
118+
{
119+
try
120+
{
121+
HotkeyManager.Current.AddOrReplace("HotkeyAvailabilityTest", currentHotkey.CharKey, currentHotkey.ModifierKeys, (sender, e) => { });
122+
123+
return true;
124+
}
125+
catch
126+
{
127+
}
128+
finally
129+
{
130+
HotkeyManager.Current.Remove("HotkeyAvailabilityTest");
131+
}
132+
133+
return false;
134+
}
135+
}
136+
}

Flow.Launcher/HotkeyControl.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<Grid>
1111
<Grid.ColumnDefinitions>
1212
<ColumnDefinition Width="150" />
13-
<ColumnDefinition Width="120" />
13+
<ColumnDefinition Width="125" />
1414
</Grid.ColumnDefinitions>
1515
<TextBox x:Name="tbHotkey" TabIndex="100" VerticalContentAlignment="Center" Grid.Column="0"
1616
PreviewKeyDown="TbHotkey_OnPreviewKeyDown" input:InputMethod.IsInputMethodEnabled="False"/>

Flow.Launcher/HotkeyControl.xaml.cs

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
using System.Windows.Controls;
55
using System.Windows.Input;
66
using System.Windows.Media;
7-
using NHotkey.Wpf;
87
using Flow.Launcher.Core.Resource;
8+
using Flow.Launcher.Helper;
99
using Flow.Launcher.Infrastructure.Hotkey;
1010
using Flow.Launcher.Plugin;
1111

@@ -18,11 +18,7 @@ public partial class HotkeyControl : UserControl
1818

1919
public event EventHandler HotkeyChanged;
2020

21-
protected virtual void OnHotkeyChanged()
22-
{
23-
EventHandler handler = HotkeyChanged;
24-
if (handler != null) handler(this, EventArgs.Empty);
25-
}
21+
protected virtual void OnHotkeyChanged() => HotkeyChanged?.Invoke(this, EventArgs.Empty);
2622

2723
public HotkeyControl()
2824
{
@@ -90,24 +86,7 @@ public void SetHotkey(string keyStr, bool triggerValidate = true)
9086
SetHotkey(new HotkeyModel(keyStr), triggerValidate);
9187
}
9288

93-
private bool CheckHotkeyAvailability()
94-
{
95-
try
96-
{
97-
HotkeyManager.Current.AddOrReplace("HotkeyAvailabilityTest", CurrentHotkey.CharKey, CurrentHotkey.ModifierKeys, (sender, e) => { });
98-
99-
return true;
100-
}
101-
catch
102-
{
103-
}
104-
finally
105-
{
106-
HotkeyManager.Current.Remove("HotkeyAvailabilityTest");
107-
}
108-
109-
return false;
110-
}
89+
private bool CheckHotkeyAvailability() => HotKeyMapper.CheckAvailability(CurrentHotkey);
11190

11291
public new bool IsFocused
11392
{

Flow.Launcher/Languages/en.xaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
<system:String x:Key="newActionKeyword">New action keyword:</system:String>
5252
<system:String x:Key="currentPriority">Current Priority:</system:String>
5353
<system:String x:Key="newPriority">New Priority:</system:String>
54+
<system:String x:Key="priority">Priority:</system:String>
5455
<system:String x:Key="pluginDirectory">Plugin Directory</system:String>
5556
<system:String x:Key="author">Author</system:String>
5657
<system:String x:Key="plugin_init_time">Init time:</system:String>
@@ -73,6 +74,7 @@
7374
<system:String x:Key="openResultModifiers">Open Result Modifiers</system:String>
7475
<system:String x:Key="showOpenResultHotkey">Show Hotkey</system:String>
7576
<system:String x:Key="customQueryHotkey">Custom Query Hotkey</system:String>
77+
<system:String x:Key="customQuery">Query</system:String>
7678
<system:String x:Key="delete">Delete</system:String>
7779
<system:String x:Key="edit">Edit</system:String>
7880
<system:String x:Key="add">Add</system:String>
@@ -137,7 +139,7 @@
137139
<system:String x:Key="update">Update</system:String>
138140

139141
<!--Hotkey Control-->
140-
<system:String x:Key="hotkeyUnavailable">Hotkey unavailable</system:String>
142+
<system:String x:Key="hotkeyUnavailable">Hotkey Unavailable</system:String>
141143

142144
<!--Crash Reporter-->
143145
<system:String x:Key="reportWindow_version">Version</system:String>

Flow.Launcher/SettingWindow.xaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@
180180
Text="{Binding PluginPair.Metadata.Description}"
181181
Grid.Row="1" Opacity="0.5" Grid.Column="2" />
182182
<DockPanel Grid.ColumnSpan="2" Grid.Row="2" Margin="0 10 0 8" HorizontalAlignment="Right">
183-
<TextBlock Text="Priority" Margin="15,0,0,0" MaxWidth="100"/>
183+
<TextBlock Text="{DynamicResource priority}" Margin="15,0,0,0" MaxWidth="100"/>
184184
<TextBlock Text="{Binding Priority}"
185185
ToolTip="Change Plugin Results Priority"
186186
Margin="5 0 0 0" Cursor="Hand" Foreground="Blue"
@@ -365,7 +365,7 @@
365365
</DataTemplate>
366366
</GridViewColumn.CellTemplate>
367367
</GridViewColumn>
368-
<GridViewColumn Header="{DynamicResource actionKeywords}" Width="546">
368+
<GridViewColumn Header="{DynamicResource customQuery}" Width="546">
369369
<GridViewColumn.CellTemplate>
370370
<DataTemplate DataType="userSettings:CustomPluginHotkey">
371371
<TextBlock Text="{Binding ActionKeyword}" />

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

0 commit comments

Comments
 (0)