Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable the use of Win hotkey to trigger flow #3262

Merged
merged 5 commits into from
Feb 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Flow.Launcher/Flow.Launcher.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="ChefKeys" Version="0.1.2" />
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.4.0" />
<PackageReference Include="Fody" Version="6.5.4">
<PrivateAssets>all</PrivateAssets>
Expand Down
39 changes: 39 additions & 0 deletions Flow.Launcher/Helper/HotKeyMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
using Flow.Launcher.Core.Resource;
using Flow.Launcher.ViewModel;
using Flow.Launcher.Core;
using ChefKeys;
using System.Globalization;

namespace Flow.Launcher.Helper;

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

internal static void OnToggleHotkeyWithChefKeys()
{
if (!_mainViewModel.ShouldIgnoreHotkeys())
_mainViewModel.ToggleFlowLauncher();
}

private static void SetHotkey(string hotkeyStr, EventHandler<HotkeyEventArgs> action)
{
if (hotkeyStr == "LWin" || hotkeyStr == "RWin")
{
SetWithChefKeys(hotkeyStr);
return;
}

var hotkey = new HotkeyModel(hotkeyStr);
SetHotkey(hotkey, action);
}

private static void SetWithChefKeys(string hotkeyStr)
{
ChefKeysManager.RegisterHotkey(hotkeyStr, hotkeyStr, OnToggleHotkeyWithChefKeys);
ChefKeysManager.Start();
}

internal static void SetHotkey(HotkeyModel hotkey, EventHandler<HotkeyEventArgs> action)
{
string hotkeyStr = hotkey.ToString();

if (hotkeyStr == "LWin" || hotkeyStr == "RWin")
{
SetWithChefKeys(hotkeyStr);
return;
}

try
{
HotkeyManager.Current.AddOrReplace(hotkeyStr, hotkey.CharKey, hotkey.ModifierKeys, action);
Expand All @@ -52,12 +79,24 @@ internal static void SetHotkey(HotkeyModel hotkey, EventHandler<HotkeyEventArgs>

internal static void RemoveHotkey(string hotkeyStr)
{
if (hotkeyStr == "LWin" || hotkeyStr == "RWin")
{
RemoveWithChefKeys(hotkeyStr);
return;
}

if (!string.IsNullOrEmpty(hotkeyStr))
{
HotkeyManager.Current.Remove(hotkeyStr);
}
}

private static void RemoveWithChefKeys(string hotkeyStr)
{
ChefKeysManager.UnregisterHotkey(hotkeyStr);
ChefKeysManager.Stop();
}

internal static void LoadCustomPluginHotkey()
{
if (_settings.CustomPluginHotkeys == null)
Expand Down
11 changes: 10 additions & 1 deletion Flow.Launcher/HotkeyControl.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,16 @@ private void SetHotkey(HotkeyModel keyModel, bool triggerValidate = true)
{
if (triggerValidate)
{
bool hotkeyAvailable = CheckHotkeyAvailability(keyModel, ValidateKeyGesture);
bool hotkeyAvailable = false;
// TODO: This is a temporary way to enforce changing only the open flow hotkey to Win, and will be removed by PR #3157
if (keyModel.ToString() == "LWin" || keyModel.ToString() == "RWin")
{
hotkeyAvailable = true;
}
else
{
hotkeyAvailable = CheckHotkeyAvailability(keyModel, ValidateKeyGesture);
}

if (!hotkeyAvailable)
{
Expand Down
29 changes: 27 additions & 2 deletions Flow.Launcher/HotkeyControlDialog.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using System.Windows;
using System.Windows.Input;
using ChefKeys;
using Flow.Launcher.Core.Resource;
using Flow.Launcher.Helper;
using Flow.Launcher.Infrastructure.Hotkey;
Expand Down Expand Up @@ -33,6 +34,8 @@ public enum EResultType
public string ResultValue { get; private set; } = string.Empty;
public static string EmptyHotkey => InternationalizationManager.Instance.GetTranslation("none");

private static bool isOpenFlowHotkey;

public HotkeyControlDialog(string hotkey, string defaultHotkey, IHotkeySettings hotkeySettings, string windowTitle = "")
{
WindowTitle = windowTitle switch
Expand All @@ -46,6 +49,14 @@ public HotkeyControlDialog(string hotkey, string defaultHotkey, IHotkeySettings
SetKeysToDisplay(CurrentHotkey);

InitializeComponent();

// TODO: This is a temporary way to enforce changing only the open flow hotkey to Win, and will be removed by PR #3157
isOpenFlowHotkey = _hotkeySettings.RegisteredHotkeys
.Any(x => x.DescriptionResourceKey == "flowlauncherHotkey"
&& x.Hotkey.ToString() == hotkey);

ChefKeysManager.StartMenuEnableBlocking = true;
ChefKeysManager.Start();
}

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

private void Cancel(object sender, RoutedEventArgs routedEventArgs)
{
ChefKeysManager.StartMenuEnableBlocking = false;
ChefKeysManager.Stop();

ResultType = EResultType.Cancel;
Hide();
}

private void Save(object sender, RoutedEventArgs routedEventArgs)
{
ChefKeysManager.StartMenuEnableBlocking = false;
ChefKeysManager.Stop();

if (KeysToDisplay.Count == 1 && KeysToDisplay[0] == EmptyHotkey)
{
ResultType = EResultType.Delete;
Expand All @@ -85,6 +102,9 @@ private void OnPreviewKeyDown(object sender, KeyEventArgs e)
//when alt is pressed, the real key should be e.SystemKey
Key key = e.Key == Key.System ? e.SystemKey : e.Key;

if (ChefKeysManager.StartMenuBlocked && key.ToString() == ChefKeysManager.StartMenuSimulatedKey)
return;

SpecialKeyState specialKeyState = GlobalHotkey.CheckModifiers();

var hotkeyModel = new HotkeyModel(
Expand Down Expand Up @@ -168,8 +188,13 @@ private void SetKeysToDisplay(HotkeyModel? hotkey)
}
}

private static bool CheckHotkeyAvailability(HotkeyModel hotkey, bool validateKeyGesture) =>
hotkey.Validate(validateKeyGesture) && HotKeyMapper.CheckAvailability(hotkey);
private static bool CheckHotkeyAvailability(HotkeyModel hotkey, bool validateKeyGesture)
{
if (isOpenFlowHotkey && (hotkey.ToString() == "LWin" || hotkey.ToString() == "RWin"))
return true;

return hotkey.Validate(validateKeyGesture) && HotKeyMapper.CheckAvailability(hotkey);
}

private void Overwrite(object sender, RoutedEventArgs e)
{
Expand Down
Loading