Skip to content

Commit 60412c2

Browse files
committed
Code quality
1 parent bd5142f commit 60412c2

File tree

4 files changed

+28
-17
lines changed

4 files changed

+28
-17
lines changed

Flow.Launcher.Infrastructure/QuickSwitch/QuickSwitch.cs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,10 @@ public static class QuickSwitch
2525

2626
private static UnhookWinEventSafeHandle _hookWinEventSafeHandle = null;
2727

28-
public static void Initialize(Action<HotkeyModel, EventHandler<HotkeyEventArgs>> setHotkeyAction)
28+
public static bool Initialize()
2929
{
3030
try
3131
{
32-
// Inspired from: https://github.com/citelao/dotnet_win32/blob/c830132d84eeed3a77e3a6e7f9ed6109258c7947/window_events/Program.cs
33-
// Here we use an UnhookWinEventSafeHandle as return value so the result is IDisposable and
34-
// can be cleaned up automatically for us.
3532
_hookWinEventSafeHandle = PInvoke.SetWinEventHook(
3633
PInvoke.EVENT_SYSTEM_FOREGROUND,
3734
PInvoke.EVENT_SYSTEM_FOREGROUND,
@@ -44,18 +41,22 @@ public static void Initialize(Action<HotkeyModel, EventHandler<HotkeyEventArgs>>
4441
if (_hookWinEventSafeHandle.IsInvalid)
4542
{
4643
Log.Error("Failed to set window event hook");
47-
return;
44+
return false;
4845
}
4946

50-
setHotkeyAction(new HotkeyModel("Alt+G"), (_, _) =>
51-
{
52-
NavigateDialogPath(_automation.ElementFromHandle(Win32Helper.GetForegroundWindow()));
53-
});
47+
return true;
5448
}
5549
catch (System.Exception e)
5650
{
5751
Log.Exception(nameof(QuickSwitch), "Failed to initialize QuickSwitch", e);
5852
}
53+
54+
return false;
55+
}
56+
57+
public static void OnToggleHotkey(object sender, HotkeyEventArgs args)
58+
{
59+
NavigateDialogPath(_automation.ElementFromHandle(Win32Helper.GetForegroundWindow()));
5960
}
6061

6162
private static void NavigateDialogPath(IUIAutomationElement window)

Flow.Launcher.Infrastructure/UserSettings/Settings.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ public void Save()
5353
public string SettingWindowHotkey { get; set; } = $"Ctrl+I";
5454
public string CycleHistoryUpHotkey { get; set; } = $"{KeyConstant.Alt} + Up";
5555
public string CycleHistoryDownHotkey { get; set; } = $"{KeyConstant.Alt} + Down";
56+
public string QuickSwitchHotkey { get; set; } = $"{KeyConstant.Alt} + G";
5657

5758
public string Language
5859
{

Flow.Launcher/App.xaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ protected virtual void Dispose(bool disposing)
334334
// since some resources owned by the thread need to be disposed.
335335
_mainWindow?.Dispatcher.Invoke(_mainWindow.Dispose);
336336
_mainVM?.Dispose();
337-
Infrastructure.QuickSwitch.QuickSwitch.Dispose();
337+
HotKeyMapper.Dispose();
338338
}
339339

340340
Log.Info("|App.Dispose|End Flow Launcher dispose ----------------------------------------------------");

Flow.Launcher/Helper/HotKeyMapper.cs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
using Flow.Launcher.Infrastructure.Hotkey;
1+
using System;
2+
using ChefKeys;
3+
using CommunityToolkit.Mvvm.DependencyInjection;
4+
using Flow.Launcher.Infrastructure.Hotkey;
5+
using Flow.Launcher.Infrastructure.Logger;
6+
using Flow.Launcher.Infrastructure.QuickSwitch;
27
using Flow.Launcher.Infrastructure.UserSettings;
3-
using System;
8+
using Flow.Launcher.ViewModel;
49
using NHotkey;
510
using NHotkey.Wpf;
6-
using Flow.Launcher.ViewModel;
7-
using ChefKeys;
8-
using Flow.Launcher.Infrastructure.Logger;
9-
using CommunityToolkit.Mvvm.DependencyInjection;
1011

1112
namespace Flow.Launcher.Helper;
1213

@@ -23,7 +24,15 @@ internal static void Initialize()
2324
SetHotkey(_settings.Hotkey, OnToggleHotkey);
2425
LoadCustomPluginHotkey();
2526

26-
Infrastructure.QuickSwitch.QuickSwitch.Initialize(SetHotkey);
27+
if (QuickSwitch.Initialize())
28+
{
29+
SetHotkey(_settings.QuickSwitchHotkey, QuickSwitch.OnToggleHotkey);
30+
}
31+
}
32+
33+
internal static void Dispose()
34+
{
35+
QuickSwitch.Dispose();
2736
}
2837

2938
internal static void OnToggleHotkey(object sender, HotkeyEventArgs args)

0 commit comments

Comments
 (0)