Revise Hotkey control logic to remove hotkey at focus and add it back at lost focus.

This commit is contained in:
Kevin Zhang 2021-12-03 13:36:08 -06:00
parent ef7f471d50
commit 35838b23af
5 changed files with 46 additions and 20 deletions

View file

@ -8,6 +8,7 @@ using Flow.Launcher.Core.Resource;
using Flow.Launcher.Helper;
using Flow.Launcher.Infrastructure.Hotkey;
using Flow.Launcher.Plugin;
using System.Threading;
namespace Flow.Launcher
{
@ -25,8 +26,14 @@ namespace Flow.Launcher
InitializeComponent();
}
private CancellationTokenSource hotkeyUpdateSource;
void TbHotkey_OnPreviewKeyDown(object sender, KeyEventArgs e)
{
hotkeyUpdateSource?.Cancel();
hotkeyUpdateSource?.Dispose();
hotkeyUpdateSource = new();
var token = hotkeyUpdateSource.Token;
e.Handled = true;
tbMsg.Visibility = Visibility.Hidden;
@ -52,7 +59,8 @@ namespace Flow.Launcher
Dispatcher.InvokeAsync(async () =>
{
await Task.Delay(500);
SetHotkey(hotkeyModel);
if (!token.IsCancellationRequested)
SetHotkey(hotkeyModel);
});
}
@ -78,7 +86,6 @@ namespace Flow.Launcher
}
tbMsg.Visibility = Visibility.Visible;
OnHotkeyChanged();
Keyboard.ClearFocus();
}
}
@ -89,9 +96,6 @@ namespace Flow.Launcher
private bool CheckHotkeyAvailability() => HotKeyMapper.CheckAvailability(CurrentHotkey);
public new bool IsFocused
{
get { return tbHotkey.IsFocused; }
}
public new bool IsFocused => tbHotkey.IsFocused;
}
}
}

View file

@ -113,7 +113,9 @@
x:Name="HotkeyControl"
Width="300"
Height="35"
Margin="-206,10,0,0" />
Margin="-206,10,0,0"
GotFocus="HotkeyControl_OnGotFocus"
LostFocus="HotkeyControl_OnLostFocus"/>
</StackPanel>
</StackPanel>

View file

@ -1,6 +1,8 @@
using Flow.Launcher.Helper;
using Flow.Launcher.Infrastructure.Hotkey;
using Flow.Launcher.Infrastructure.UserSettings;
using System;
using System.Windows;
using System.Windows.Navigation;
namespace Flow.Launcher.Resources.Pages
@ -17,16 +19,23 @@ namespace Flow.Launcher.Resources.Pages
throw new ArgumentException("Unexpected Parameter setting.");
InitializeComponent();
HotkeyControl.SetHotkey(new Infrastructure.Hotkey.HotkeyModel(Settings.Hotkey));
HotkeyControl.HotkeyChanged += (_, _) =>
HotkeyControl.SetHotkey(new Infrastructure.Hotkey.HotkeyModel(Settings.Hotkey), false);
}
private void HotkeyControl_OnGotFocus(object sender, RoutedEventArgs args)
{
HotKeyMapper.RemoveHotkey(Settings.Hotkey);
}
private void HotkeyControl_OnLostFocus(object sender, RoutedEventArgs args)
{
if (HotkeyControl.CurrentHotkeyAvailable)
{
if (HotkeyControl.CurrentHotkeyAvailable)
{
HotKeyMapper.SetHotkey(HotkeyControl.CurrentHotkey, HotKeyMapper.OnToggleHotkey);
HotKeyMapper.RemoveHotkey(Settings.Hotkey);
Settings.Hotkey = HotkeyControl.CurrentHotkey.ToString();
}
};
HotKeyMapper.SetHotkey(HotkeyControl.CurrentHotkey, HotKeyMapper.OnToggleHotkey);
Settings.Hotkey = HotkeyControl.CurrentHotkey.ToString();
}
else
{
HotKeyMapper.SetHotkey(new HotkeyModel(Settings.Hotkey), HotKeyMapper.OnToggleHotkey);
}
}
}
}
}

View file

@ -2018,7 +2018,8 @@
Margin="0,0,0,0"
HorizontalAlignment="Right"
HorizontalContentAlignment="Right"
HotkeyChanged="OnHotkeyChanged"
LostFocus="OnHotkeyControlFocusLost"
GotFocus="OnHotkeyControlFocused"
Loaded="OnHotkeyControlLoaded" />
<TextBlock Style="{StaticResource Glyph}">

View file

@ -3,6 +3,7 @@ using Flow.Launcher.Core.Plugin;
using Flow.Launcher.Core.Resource;
using Flow.Launcher.Helper;
using Flow.Launcher.Infrastructure;
using Flow.Launcher.Infrastructure.Hotkey;
using Flow.Launcher.Infrastructure.UserSettings;
using Flow.Launcher.Plugin;
using Flow.Launcher.Plugin.SharedCommands;
@ -128,7 +129,12 @@ namespace Flow.Launcher
HotkeyControl.SetHotkey(viewModel.Settings.Hotkey, false);
}
void OnHotkeyChanged(object sender, EventArgs e)
private void OnHotkeyControlFocused(object sender, EventArgs e)
{
HotKeyMapper.RemoveHotkey(settings.Hotkey);
}
private void OnHotkeyControlFocusLost(object sender, EventArgs e)
{
if (HotkeyControl.CurrentHotkeyAvailable)
{
@ -136,6 +142,10 @@ namespace Flow.Launcher
HotKeyMapper.RemoveHotkey(settings.Hotkey);
settings.Hotkey = HotkeyControl.CurrentHotkey.ToString();
}
else
{
HotKeyMapper.SetHotkey(new HotkeyModel(settings.Hotkey), HotKeyMapper.OnToggleHotkey);
}
}
private void OnDeleteCustomHotkeyClick(object sender, RoutedEventArgs e)