Skip to content

Commit 35838b2

Browse files
committed
Revise Hotkey control logic to remove hotkey at focus and add it back at lost focus.
1 parent ef7f471 commit 35838b2

File tree

5 files changed

+46
-20
lines changed

5 files changed

+46
-20
lines changed

Flow.Launcher/HotkeyControl.xaml.cs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using Flow.Launcher.Helper;
99
using Flow.Launcher.Infrastructure.Hotkey;
1010
using Flow.Launcher.Plugin;
11+
using System.Threading;
1112

1213
namespace Flow.Launcher
1314
{
@@ -25,8 +26,14 @@ public HotkeyControl()
2526
InitializeComponent();
2627
}
2728

29+
private CancellationTokenSource hotkeyUpdateSource;
30+
2831
void TbHotkey_OnPreviewKeyDown(object sender, KeyEventArgs e)
2932
{
33+
hotkeyUpdateSource?.Cancel();
34+
hotkeyUpdateSource?.Dispose();
35+
hotkeyUpdateSource = new();
36+
var token = hotkeyUpdateSource.Token;
3037
e.Handled = true;
3138
tbMsg.Visibility = Visibility.Hidden;
3239

@@ -52,7 +59,8 @@ void TbHotkey_OnPreviewKeyDown(object sender, KeyEventArgs e)
5259
Dispatcher.InvokeAsync(async () =>
5360
{
5461
await Task.Delay(500);
55-
SetHotkey(hotkeyModel);
62+
if (!token.IsCancellationRequested)
63+
SetHotkey(hotkeyModel);
5664
});
5765
}
5866

@@ -78,7 +86,6 @@ public void SetHotkey(HotkeyModel keyModel, bool triggerValidate = true)
7886
}
7987
tbMsg.Visibility = Visibility.Visible;
8088
OnHotkeyChanged();
81-
Keyboard.ClearFocus();
8289
}
8390
}
8491

@@ -89,9 +96,6 @@ public void SetHotkey(string keyStr, bool triggerValidate = true)
8996

9097
private bool CheckHotkeyAvailability() => HotKeyMapper.CheckAvailability(CurrentHotkey);
9198

92-
public new bool IsFocused
93-
{
94-
get { return tbHotkey.IsFocused; }
95-
}
99+
public new bool IsFocused => tbHotkey.IsFocused;
96100
}
97-
}
101+
}

Flow.Launcher/Resources/Pages/WelcomePage2.xaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,9 @@
113113
x:Name="HotkeyControl"
114114
Width="300"
115115
Height="35"
116-
Margin="-206,10,0,0" />
116+
Margin="-206,10,0,0"
117+
GotFocus="HotkeyControl_OnGotFocus"
118+
LostFocus="HotkeyControl_OnLostFocus"/>
117119
</StackPanel>
118120

119121
</StackPanel>
Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using Flow.Launcher.Helper;
2+
using Flow.Launcher.Infrastructure.Hotkey;
23
using Flow.Launcher.Infrastructure.UserSettings;
34
using System;
5+
using System.Windows;
46
using System.Windows.Navigation;
57

68
namespace Flow.Launcher.Resources.Pages
@@ -17,16 +19,23 @@ protected override void OnNavigatedTo(NavigationEventArgs e)
1719
throw new ArgumentException("Unexpected Parameter setting.");
1820
InitializeComponent();
1921

20-
HotkeyControl.SetHotkey(new Infrastructure.Hotkey.HotkeyModel(Settings.Hotkey));
21-
HotkeyControl.HotkeyChanged += (_, _) =>
22+
HotkeyControl.SetHotkey(new Infrastructure.Hotkey.HotkeyModel(Settings.Hotkey), false);
23+
}
24+
private void HotkeyControl_OnGotFocus(object sender, RoutedEventArgs args)
25+
{
26+
HotKeyMapper.RemoveHotkey(Settings.Hotkey);
27+
}
28+
private void HotkeyControl_OnLostFocus(object sender, RoutedEventArgs args)
29+
{
30+
if (HotkeyControl.CurrentHotkeyAvailable)
31+
{
32+
HotKeyMapper.SetHotkey(HotkeyControl.CurrentHotkey, HotKeyMapper.OnToggleHotkey);
33+
Settings.Hotkey = HotkeyControl.CurrentHotkey.ToString();
34+
}
35+
else
2236
{
23-
if (HotkeyControl.CurrentHotkeyAvailable)
24-
{
25-
HotKeyMapper.SetHotkey(HotkeyControl.CurrentHotkey, HotKeyMapper.OnToggleHotkey);
26-
HotKeyMapper.RemoveHotkey(Settings.Hotkey);
27-
Settings.Hotkey = HotkeyControl.CurrentHotkey.ToString();
28-
}
29-
};
37+
HotKeyMapper.SetHotkey(new HotkeyModel(Settings.Hotkey), HotKeyMapper.OnToggleHotkey);
38+
}
3039
}
3140
}
32-
}
41+
}

Flow.Launcher/SettingWindow.xaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2018,7 +2018,8 @@
20182018
Margin="0,0,0,0"
20192019
HorizontalAlignment="Right"
20202020
HorizontalContentAlignment="Right"
2021-
HotkeyChanged="OnHotkeyChanged"
2021+
LostFocus="OnHotkeyControlFocusLost"
2022+
GotFocus="OnHotkeyControlFocused"
20222023
Loaded="OnHotkeyControlLoaded" />
20232024

20242025
<TextBlock Style="{StaticResource Glyph}">

Flow.Launcher/SettingWindow.xaml.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using Flow.Launcher.Core.Resource;
44
using Flow.Launcher.Helper;
55
using Flow.Launcher.Infrastructure;
6+
using Flow.Launcher.Infrastructure.Hotkey;
67
using Flow.Launcher.Infrastructure.UserSettings;
78
using Flow.Launcher.Plugin;
89
using Flow.Launcher.Plugin.SharedCommands;
@@ -128,14 +129,23 @@ private void OnHotkeyControlLoaded(object sender, RoutedEventArgs e)
128129
HotkeyControl.SetHotkey(viewModel.Settings.Hotkey, false);
129130
}
130131

131-
void OnHotkeyChanged(object sender, EventArgs e)
132+
private void OnHotkeyControlFocused(object sender, EventArgs e)
133+
{
134+
HotKeyMapper.RemoveHotkey(settings.Hotkey);
135+
}
136+
137+
private void OnHotkeyControlFocusLost(object sender, EventArgs e)
132138
{
133139
if (HotkeyControl.CurrentHotkeyAvailable)
134140
{
135141
HotKeyMapper.SetHotkey(HotkeyControl.CurrentHotkey, HotKeyMapper.OnToggleHotkey);
136142
HotKeyMapper.RemoveHotkey(settings.Hotkey);
137143
settings.Hotkey = HotkeyControl.CurrentHotkey.ToString();
138144
}
145+
else
146+
{
147+
HotKeyMapper.SetHotkey(new HotkeyModel(settings.Hotkey), HotKeyMapper.OnToggleHotkey);
148+
}
139149
}
140150

141151
private void OnDeleteCustomHotkeyClick(object sender, RoutedEventArgs e)

0 commit comments

Comments
 (0)