Skip to content

Commit 54a49d6

Browse files
authored
Enable the use of Win hotkey to trigger flow (#3262)
1 parent c137091 commit 54a49d6

File tree

4 files changed

+77
-3
lines changed

4 files changed

+77
-3
lines changed

Flow.Launcher/Flow.Launcher.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
</ItemGroup>
8484

8585
<ItemGroup>
86+
<PackageReference Include="ChefKeys" Version="0.1.2" />
8687
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.4.0" />
8788
<PackageReference Include="Fody" Version="6.5.4">
8889
<PrivateAssets>all</PrivateAssets>

Flow.Launcher/Helper/HotKeyMapper.cs

+39
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
using Flow.Launcher.Core.Resource;
77
using Flow.Launcher.ViewModel;
88
using Flow.Launcher.Core;
9+
using ChefKeys;
10+
using System.Globalization;
911

1012
namespace Flow.Launcher.Helper;
1113

@@ -29,15 +31,40 @@ internal static void OnToggleHotkey(object sender, HotkeyEventArgs args)
2931
_mainViewModel.ToggleFlowLauncher();
3032
}
3133

34+
internal static void OnToggleHotkeyWithChefKeys()
35+
{
36+
if (!_mainViewModel.ShouldIgnoreHotkeys())
37+
_mainViewModel.ToggleFlowLauncher();
38+
}
39+
3240
private static void SetHotkey(string hotkeyStr, EventHandler<HotkeyEventArgs> action)
3341
{
42+
if (hotkeyStr == "LWin" || hotkeyStr == "RWin")
43+
{
44+
SetWithChefKeys(hotkeyStr);
45+
return;
46+
}
47+
3448
var hotkey = new HotkeyModel(hotkeyStr);
3549
SetHotkey(hotkey, action);
3650
}
3751

52+
private static void SetWithChefKeys(string hotkeyStr)
53+
{
54+
ChefKeysManager.RegisterHotkey(hotkeyStr, hotkeyStr, OnToggleHotkeyWithChefKeys);
55+
ChefKeysManager.Start();
56+
}
57+
3858
internal static void SetHotkey(HotkeyModel hotkey, EventHandler<HotkeyEventArgs> action)
3959
{
4060
string hotkeyStr = hotkey.ToString();
61+
62+
if (hotkeyStr == "LWin" || hotkeyStr == "RWin")
63+
{
64+
SetWithChefKeys(hotkeyStr);
65+
return;
66+
}
67+
4168
try
4269
{
4370
HotkeyManager.Current.AddOrReplace(hotkeyStr, hotkey.CharKey, hotkey.ModifierKeys, action);
@@ -52,12 +79,24 @@ internal static void SetHotkey(HotkeyModel hotkey, EventHandler<HotkeyEventArgs>
5279

5380
internal static void RemoveHotkey(string hotkeyStr)
5481
{
82+
if (hotkeyStr == "LWin" || hotkeyStr == "RWin")
83+
{
84+
RemoveWithChefKeys(hotkeyStr);
85+
return;
86+
}
87+
5588
if (!string.IsNullOrEmpty(hotkeyStr))
5689
{
5790
HotkeyManager.Current.Remove(hotkeyStr);
5891
}
5992
}
6093

94+
private static void RemoveWithChefKeys(string hotkeyStr)
95+
{
96+
ChefKeysManager.UnregisterHotkey(hotkeyStr);
97+
ChefKeysManager.Stop();
98+
}
99+
61100
internal static void LoadCustomPluginHotkey()
62101
{
63102
if (_settings.CustomPluginHotkeys == null)

Flow.Launcher/HotkeyControl.xaml.cs

+10-1
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,16 @@ private void SetHotkey(HotkeyModel keyModel, bool triggerValidate = true)
154154
{
155155
if (triggerValidate)
156156
{
157-
bool hotkeyAvailable = CheckHotkeyAvailability(keyModel, ValidateKeyGesture);
157+
bool hotkeyAvailable = false;
158+
// TODO: This is a temporary way to enforce changing only the open flow hotkey to Win, and will be removed by PR #3157
159+
if (keyModel.ToString() == "LWin" || keyModel.ToString() == "RWin")
160+
{
161+
hotkeyAvailable = true;
162+
}
163+
else
164+
{
165+
hotkeyAvailable = CheckHotkeyAvailability(keyModel, ValidateKeyGesture);
166+
}
158167

159168
if (!hotkeyAvailable)
160169
{

Flow.Launcher/HotkeyControlDialog.xaml.cs

+27-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Linq;
44
using System.Windows;
55
using System.Windows.Input;
6+
using ChefKeys;
67
using Flow.Launcher.Core.Resource;
78
using Flow.Launcher.Helper;
89
using Flow.Launcher.Infrastructure.Hotkey;
@@ -33,6 +34,8 @@ public enum EResultType
3334
public string ResultValue { get; private set; } = string.Empty;
3435
public static string EmptyHotkey => InternationalizationManager.Instance.GetTranslation("none");
3536

37+
private static bool isOpenFlowHotkey;
38+
3639
public HotkeyControlDialog(string hotkey, string defaultHotkey, IHotkeySettings hotkeySettings, string windowTitle = "")
3740
{
3841
WindowTitle = windowTitle switch
@@ -46,6 +49,14 @@ public HotkeyControlDialog(string hotkey, string defaultHotkey, IHotkeySettings
4649
SetKeysToDisplay(CurrentHotkey);
4750

4851
InitializeComponent();
52+
53+
// TODO: This is a temporary way to enforce changing only the open flow hotkey to Win, and will be removed by PR #3157
54+
isOpenFlowHotkey = _hotkeySettings.RegisteredHotkeys
55+
.Any(x => x.DescriptionResourceKey == "flowlauncherHotkey"
56+
&& x.Hotkey.ToString() == hotkey);
57+
58+
ChefKeysManager.StartMenuEnableBlocking = true;
59+
ChefKeysManager.Start();
4960
}
5061

5162
private void Reset(object sender, RoutedEventArgs routedEventArgs)
@@ -61,12 +72,18 @@ private void Delete(object sender, RoutedEventArgs routedEventArgs)
6172

6273
private void Cancel(object sender, RoutedEventArgs routedEventArgs)
6374
{
75+
ChefKeysManager.StartMenuEnableBlocking = false;
76+
ChefKeysManager.Stop();
77+
6478
ResultType = EResultType.Cancel;
6579
Hide();
6680
}
6781

6882
private void Save(object sender, RoutedEventArgs routedEventArgs)
6983
{
84+
ChefKeysManager.StartMenuEnableBlocking = false;
85+
ChefKeysManager.Stop();
86+
7087
if (KeysToDisplay.Count == 1 && KeysToDisplay[0] == EmptyHotkey)
7188
{
7289
ResultType = EResultType.Delete;
@@ -85,6 +102,9 @@ private void OnPreviewKeyDown(object sender, KeyEventArgs e)
85102
//when alt is pressed, the real key should be e.SystemKey
86103
Key key = e.Key == Key.System ? e.SystemKey : e.Key;
87104

105+
if (ChefKeysManager.StartMenuBlocked && key.ToString() == ChefKeysManager.StartMenuSimulatedKey)
106+
return;
107+
88108
SpecialKeyState specialKeyState = GlobalHotkey.CheckModifiers();
89109

90110
var hotkeyModel = new HotkeyModel(
@@ -168,8 +188,13 @@ private void SetKeysToDisplay(HotkeyModel? hotkey)
168188
}
169189
}
170190

171-
private static bool CheckHotkeyAvailability(HotkeyModel hotkey, bool validateKeyGesture) =>
172-
hotkey.Validate(validateKeyGesture) && HotKeyMapper.CheckAvailability(hotkey);
191+
private static bool CheckHotkeyAvailability(HotkeyModel hotkey, bool validateKeyGesture)
192+
{
193+
if (isOpenFlowHotkey && (hotkey.ToString() == "LWin" || hotkey.ToString() == "RWin"))
194+
return true;
195+
196+
return hotkey.Validate(validateKeyGesture) && HotKeyMapper.CheckAvailability(hotkey);
197+
}
173198

174199
private void Overwrite(object sender, RoutedEventArgs e)
175200
{

0 commit comments

Comments
 (0)