Skip to content

Commit f78ad1c

Browse files
authored
Merge pull request #1932 from VictoriousRaptor/FixPreviewHotkey
Fix preview hotkey issue
2 parents b2516ed + 0754568 commit f78ad1c

File tree

4 files changed

+77
-12
lines changed

4 files changed

+77
-12
lines changed

Flow.Launcher.Infrastructure/Hotkey/HotkeyModel.cs

Lines changed: 49 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.ComponentModel;
34
using System.Linq;
45
using System.Windows.Input;
5-
using System.Windows.Navigation;
66

77
namespace Flow.Launcher.Infrastructure.Hotkey
88
{
@@ -99,7 +99,7 @@ private void Parse(string hotkeyString)
9999
{
100100
try
101101
{
102-
CharKey = (Key) Enum.Parse(typeof (Key), charKey);
102+
CharKey = (Key)Enum.Parse(typeof(Key), charKey);
103103
}
104104
catch (ArgumentException)
105105
{
@@ -137,8 +137,13 @@ public override string ToString()
137137
}
138138
return string.Join(" + ", keys);
139139
}
140-
141-
public bool Validate()
140+
141+
/// <summary>
142+
/// Validate hotkey
143+
/// </summary>
144+
/// <param name="validateKeyGestrue">Try to validate hotkey as a KeyGesture.</param>
145+
/// <returns></returns>
146+
public bool Validate(bool validateKeyGestrue = false)
142147
{
143148
switch (CharKey)
144149
{
@@ -150,21 +155,57 @@ public bool Validate()
150155
case Key.RightShift:
151156
case Key.LWin:
152157
case Key.RWin:
158+
case Key.None:
153159
return false;
154160
default:
161+
if (validateKeyGestrue)
162+
{
163+
try
164+
{
165+
KeyGesture keyGesture = new KeyGesture(CharKey, ModifierKeys);
166+
}
167+
catch (System.Exception e) when (e is NotSupportedException || e is InvalidEnumArgumentException)
168+
{
169+
return false;
170+
}
171+
}
155172
if (ModifierKeys == ModifierKeys.None)
156173
{
157-
return !((CharKey >= Key.A && CharKey <= Key.Z) ||
158-
(CharKey >= Key.D0 && CharKey <= Key.D9) ||
159-
(CharKey >= Key.NumPad0 && CharKey <= Key.NumPad9));
174+
return !IsPrintableCharacter(CharKey);
160175
}
161176
else
162177
{
163-
return CharKey != Key.None;
178+
return true;
164179
}
165180
}
166181
}
167182

183+
private static bool IsPrintableCharacter(Key key)
184+
{
185+
// https://stackoverflow.com/questions/11881199/identify-if-a-event-key-is-text-not-only-alphanumeric
186+
return (key >= Key.A && key <= Key.Z) ||
187+
(key >= Key.D0 && key <= Key.D9) ||
188+
(key >= Key.NumPad0 && key <= Key.NumPad9) ||
189+
key == Key.OemQuestion ||
190+
key == Key.OemQuotes ||
191+
key == Key.OemPlus ||
192+
key == Key.OemOpenBrackets ||
193+
key == Key.OemCloseBrackets ||
194+
key == Key.OemMinus ||
195+
key == Key.DeadCharProcessed ||
196+
key == Key.Oem1 ||
197+
key == Key.Oem7 ||
198+
key == Key.OemPeriod ||
199+
key == Key.OemComma ||
200+
key == Key.OemMinus ||
201+
key == Key.Add ||
202+
key == Key.Divide ||
203+
key == Key.Multiply ||
204+
key == Key.Subtract ||
205+
key == Key.Oem102 ||
206+
key == Key.Decimal;
207+
}
208+
168209
public override bool Equals(object obj)
169210
{
170211
if (obj is HotkeyModel other)

Flow.Launcher/HotkeyControl.xaml.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ public partial class HotkeyControl : UserControl
1919

2020
public event EventHandler HotkeyChanged;
2121

22+
/// <summary>
23+
/// Designed for Preview Hotkey and KeyGesture.
24+
/// </summary>
25+
public bool ValidateKeyGesture { get; set; } = false;
26+
2227
protected virtual void OnHotkeyChanged() => HotkeyChanged?.Invoke(this, EventArgs.Empty);
2328

2429
public HotkeyControl()
@@ -68,7 +73,7 @@ public async Task SetHotkeyAsync(HotkeyModel keyModel, bool triggerValidate = tr
6873

6974
if (triggerValidate)
7075
{
71-
bool hotkeyAvailable = CheckHotkeyAvailability(keyModel);
76+
bool hotkeyAvailable = CheckHotkeyAvailability(keyModel, ValidateKeyGesture);
7277
CurrentHotkeyAvailable = hotkeyAvailable;
7378
SetMessage(hotkeyAvailable);
7479
OnHotkeyChanged();
@@ -91,13 +96,13 @@ public async Task SetHotkeyAsync(HotkeyModel keyModel, bool triggerValidate = tr
9196
CurrentHotkey = keyModel;
9297
}
9398
}
94-
99+
95100
public Task SetHotkeyAsync(string keyStr, bool triggerValidate = true)
96101
{
97102
return SetHotkeyAsync(new HotkeyModel(keyStr), triggerValidate);
98103
}
99104

100-
private static bool CheckHotkeyAvailability(HotkeyModel hotkey) => hotkey.Validate() && HotKeyMapper.CheckAvailability(hotkey);
105+
private static bool CheckHotkeyAvailability(HotkeyModel hotkey, bool validateKeyGesture) => hotkey.Validate(validateKeyGesture) && HotKeyMapper.CheckAvailability(hotkey);
101106

102107
public new bool IsFocused => tbHotkey.IsFocused;
103108

Flow.Launcher/SettingWindow.xaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2448,6 +2448,7 @@
24482448
Width="300"
24492449
Height="35"
24502450
Margin="0,0,0,0"
2451+
ValidateKeyGesture="True"
24512452
HorizontalAlignment="Right"
24522453
HorizontalContentAlignment="Right"
24532454
Loaded="OnPreviewHotkeyControlLoaded"

Flow.Launcher/ViewModel/MainViewModel.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
using CommunityToolkit.Mvvm.Input;
2525
using System.Globalization;
2626
using System.Windows.Input;
27+
using System.ComponentModel;
2728

2829
namespace Flow.Launcher.ViewModel
2930
{
@@ -583,7 +584,24 @@ public double MainWindowWidth
583584

584585
public string OpenResultCommandModifiers => Settings.OpenResultModifiers;
585586

586-
public string PreviewHotkey => Settings.PreviewHotkey;
587+
public string PreviewHotkey
588+
{
589+
get
590+
{
591+
// TODO try to patch issue #1755
592+
// Added in v1.14.0, remove after v1.16.0.
593+
try
594+
{
595+
var converter = new KeyGestureConverter();
596+
var key = (KeyGesture)converter.ConvertFromString(Settings.PreviewHotkey);
597+
}
598+
catch (Exception e) when (e is NotSupportedException || e is InvalidEnumArgumentException)
599+
{
600+
Settings.PreviewHotkey = "F1";
601+
}
602+
return Settings.PreviewHotkey;
603+
}
604+
}
587605

588606
public string Image => Constant.QueryTextBoxIconImagePath;
589607

0 commit comments

Comments
 (0)