Skip to content

Commit 4ce591f

Browse files
authored
Merge pull request #2648 from Yusyuriv/new-hotkey-control
Improve Hotkey System
2 parents 2d12561 + 55f2b6a commit 4ce591f

28 files changed

+2398
-941
lines changed

Flow.Launcher.Infrastructure/Hotkey/HotkeyModel.cs

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
namespace Flow.Launcher.Infrastructure.Hotkey
88
{
9-
public class HotkeyModel
9+
public record struct HotkeyModel
1010
{
1111
public bool Alt { get; set; }
1212
public bool Shift { get; set; }
@@ -17,8 +17,7 @@ public class HotkeyModel
1717

1818
private static readonly Dictionary<Key, string> specialSymbolDictionary = new Dictionary<Key, string>
1919
{
20-
{Key.Space, "Space"},
21-
{Key.Oem3, "~"}
20+
{ Key.Space, "Space" }, { Key.Oem3, "~" }
2221
};
2322

2423
public ModifierKeys ModifierKeys
@@ -30,18 +29,22 @@ public ModifierKeys ModifierKeys
3029
{
3130
modifierKeys |= ModifierKeys.Alt;
3231
}
32+
3333
if (Shift)
3434
{
3535
modifierKeys |= ModifierKeys.Shift;
3636
}
37+
3738
if (Win)
3839
{
3940
modifierKeys |= ModifierKeys.Windows;
4041
}
42+
4143
if (Ctrl)
4244
{
4345
modifierKeys |= ModifierKeys.Control;
4446
}
47+
4548
return modifierKeys;
4649
}
4750
}
@@ -66,31 +69,37 @@ private void Parse(string hotkeyString)
6669
{
6770
return;
6871
}
72+
6973
List<string> keys = hotkeyString.Replace(" ", "").Split('+').ToList();
7074
if (keys.Contains("Alt"))
7175
{
7276
Alt = true;
7377
keys.Remove("Alt");
7478
}
79+
7580
if (keys.Contains("Shift"))
7681
{
7782
Shift = true;
7883
keys.Remove("Shift");
7984
}
85+
8086
if (keys.Contains("Win"))
8187
{
8288
Win = true;
8389
keys.Remove("Win");
8490
}
91+
8592
if (keys.Contains("Ctrl"))
8693
{
8794
Ctrl = true;
8895
keys.Remove("Ctrl");
8996
}
97+
9098
if (keys.Count == 1)
9199
{
92100
string charKey = keys[0];
93-
KeyValuePair<Key, string>? specialSymbolPair = specialSymbolDictionary.FirstOrDefault(pair => pair.Value == charKey);
101+
KeyValuePair<Key, string>? specialSymbolPair =
102+
specialSymbolDictionary.FirstOrDefault(pair => pair.Value == charKey);
94103
if (specialSymbolPair.Value.Value != null)
95104
{
96105
CharKey = specialSymbolPair.Value.Key;
@@ -103,41 +112,46 @@ private void Parse(string hotkeyString)
103112
}
104113
catch (ArgumentException)
105114
{
106-
107115
}
108116
}
109117
}
110118
}
111119

112120
public override string ToString()
113121
{
114-
List<string> keys = new List<string>();
115-
if (Ctrl)
122+
return string.Join(" + ", EnumerateDisplayKeys());
123+
}
124+
125+
public IEnumerable<string> EnumerateDisplayKeys()
126+
{
127+
if (Ctrl && CharKey is not (Key.LeftCtrl or Key.RightCtrl))
116128
{
117-
keys.Add("Ctrl");
129+
yield return "Ctrl";
118130
}
119-
if (Alt)
131+
132+
if (Alt && CharKey is not (Key.LeftAlt or Key.RightAlt))
120133
{
121-
keys.Add("Alt");
134+
yield return "Alt";
122135
}
123-
if (Shift)
136+
137+
if (Shift && CharKey is not (Key.LeftShift or Key.RightShift))
124138
{
125-
keys.Add("Shift");
139+
yield return "Shift";
126140
}
127-
if (Win)
141+
142+
if (Win && CharKey is not (Key.LWin or Key.RWin))
128143
{
129-
keys.Add("Win");
144+
yield return "Win";
130145
}
131146

132147
if (CharKey != Key.None)
133148
{
134-
keys.Add(specialSymbolDictionary.ContainsKey(CharKey)
135-
? specialSymbolDictionary[CharKey]
136-
: CharKey.ToString());
149+
yield return specialSymbolDictionary.TryGetValue(CharKey, out var value)
150+
? value
151+
: CharKey.ToString();
137152
}
138-
return string.Join(" + ", keys);
139153
}
140-
154+
141155
/// <summary>
142156
/// Validate hotkey
143157
/// </summary>
@@ -164,11 +178,13 @@ public bool Validate(bool validateKeyGestrue = false)
164178
{
165179
KeyGesture keyGesture = new KeyGesture(CharKey, ModifierKeys);
166180
}
167-
catch (System.Exception e) when (e is NotSupportedException || e is InvalidEnumArgumentException)
181+
catch (System.Exception e) when
182+
(e is NotSupportedException || e is InvalidEnumArgumentException)
168183
{
169184
return false;
170185
}
171186
}
187+
172188
if (ModifierKeys == ModifierKeys.None)
173189
{
174190
return !IsPrintableCharacter(CharKey);
@@ -206,18 +222,6 @@ private static bool IsPrintableCharacter(Key key)
206222
key == Key.Decimal;
207223
}
208224

209-
public override bool Equals(object obj)
210-
{
211-
if (obj is HotkeyModel other)
212-
{
213-
return ModifierKeys == other.ModifierKeys && CharKey == other.CharKey;
214-
}
215-
else
216-
{
217-
return false;
218-
}
219-
}
220-
221225
public override int GetHashCode()
222226
{
223227
return HashCode.Combine(ModifierKeys, CharKey);

Flow.Launcher.Infrastructure/UserSettings/Settings.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,16 @@ public class Settings : BaseModel
2020
public bool ShowOpenResultHotkey { get; set; } = true;
2121
public double WindowSize { get; set; } = 580;
2222
public string PreviewHotkey { get; set; } = $"F1";
23+
public string AutoCompleteHotkey { get; set; } = $"{KeyConstant.Ctrl} + Tab";
24+
public string AutoCompleteHotkey2 { get; set; } = $"";
25+
public string SelectNextItemHotkey { get; set; } = $"Tab";
26+
public string SelectNextItemHotkey2 { get; set; } = $"";
27+
public string SelectPrevItemHotkey { get; set; } = $"Shift + Tab";
28+
public string SelectPrevItemHotkey2 { get; set; } = $"";
29+
public string SelectNextPageHotkey { get; set; } = $"";
30+
public string SelectPrevPageHotkey { get; set; } = $"";
31+
public string OpenContextMenuHotkey { get; set; } = $"Ctrl+O";
32+
public string SettingWindowHotkey { get; set; } = $"Ctrl+I";
2333

2434
public string Language
2535
{

Flow.Launcher/CustomQueryHotkeySetting.xaml

Lines changed: 68 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22
x:Class="Flow.Launcher.CustomQueryHotkeySetting"
33
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
44
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
5+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
56
xmlns:flowlauncher="clr-namespace:Flow.Launcher"
67
Title="{DynamicResource customeQueryHotkeyTitle}"
78
Width="530"
89
Background="{DynamicResource PopuBGColor}"
10+
DataContext="{Binding RelativeSource={RelativeSource Self}}"
911
Foreground="{DynamicResource PopupTextColor}"
1012
Icon="Images\app.png"
1113
MouseDown="window_MouseDown"
@@ -60,94 +62,75 @@
6062
</Grid>
6163
</StackPanel>
6264
<StackPanel Margin="26,0,26,0">
63-
<StackPanel Grid.Row="0" Margin="0,0,0,12">
65+
<TextBlock
66+
Margin="0,0,0,12"
67+
FontSize="20"
68+
FontWeight="SemiBold"
69+
Text="{DynamicResource customeQueryHotkeyTitle}"
70+
TextAlignment="Left" />
71+
<TextBlock
72+
FontSize="14"
73+
Text="{DynamicResource customeQueryHotkeyTips}"
74+
TextAlignment="Left"
75+
TextWrapping="WrapWithOverflow" />
76+
<Image
77+
Width="478"
78+
Margin="0,20,0,0"
79+
Source="/Images/illustration_01.png" />
80+
81+
<Grid Width="478" Margin="0,20,0,0">
82+
<Grid.RowDefinitions>
83+
<RowDefinition />
84+
<RowDefinition />
85+
</Grid.RowDefinitions>
86+
<Grid.ColumnDefinitions>
87+
<ColumnDefinition Width="Auto" />
88+
<ColumnDefinition Width="*" />
89+
<ColumnDefinition Width="Auto" />
90+
</Grid.ColumnDefinitions>
91+
6492
<TextBlock
93+
Grid.Row="0"
6594
Grid.Column="0"
66-
Margin="0,0,0,0"
67-
FontSize="20"
68-
FontWeight="SemiBold"
69-
Text="{DynamicResource customeQueryHotkeyTitle}"
70-
TextAlignment="Left" />
71-
</StackPanel>
72-
<StackPanel>
95+
Margin="10"
96+
HorizontalAlignment="Left"
97+
VerticalAlignment="Center"
98+
FontSize="14"
99+
Text="{DynamicResource hotkey}" />
100+
<flowlauncher:HotkeyControl
101+
x:Name="HotkeyControl"
102+
Grid.Row="0"
103+
Grid.Column="1"
104+
Grid.ColumnSpan="2"
105+
Margin="10,0,10,0"
106+
HorizontalAlignment="Left"
107+
VerticalAlignment="Center"
108+
HorizontalContentAlignment="Left"
109+
DefaultHotkey="" />
73110
<TextBlock
111+
Grid.Row="1"
112+
Grid.Column="0"
113+
Margin="10"
114+
HorizontalAlignment="Left"
115+
VerticalAlignment="Center"
74116
FontSize="14"
75-
Text="{DynamicResource customeQueryHotkeyTips}"
76-
TextAlignment="Left"
77-
TextWrapping="WrapWithOverflow" />
78-
<Image
79-
Width="478"
80-
Margin="0,20,0,0"
81-
Source="/Images/illustration_01.png" />
82-
</StackPanel>
83-
84-
<StackPanel Margin="0,20,0,0" Orientation="Horizontal">
85-
<Grid Width="478">
86-
<Grid.RowDefinitions>
87-
<RowDefinition />
88-
<RowDefinition />
89-
</Grid.RowDefinitions>
90-
<Grid.ColumnDefinitions>
91-
<ColumnDefinition Width="Auto" />
92-
<ColumnDefinition Width="*" />
93-
</Grid.ColumnDefinitions>
94-
<TextBlock
95-
Grid.Row="0"
96-
Grid.Column="0"
97-
Margin="10"
98-
HorizontalAlignment="Left"
99-
VerticalAlignment="Center"
100-
FontSize="14"
101-
Text="{DynamicResource hotkey}" />
102-
<StackPanel
103-
Grid.Row="0"
104-
Grid.Column="1"
105-
Orientation="Horizontal">
106-
<flowlauncher:HotkeyControl
107-
x:Name="ctlHotkey"
108-
Grid.Column="1"
109-
Width="200"
110-
Height="36"
111-
Margin="10,0,10,0"
112-
HorizontalAlignment="Left"
113-
VerticalAlignment="Center"
114-
HorizontalContentAlignment="Left" />
115-
<TextBlock
116-
Grid.Row="1"
117-
Grid.Column="0"
118-
Margin="10"
119-
HorizontalAlignment="Left"
120-
VerticalAlignment="Center"
121-
FontSize="14"
122-
Text="{DynamicResource actionKeyword}" />
123-
</StackPanel>
124-
<TextBlock
125-
Grid.Row="1"
126-
Grid.Column="0"
127-
Margin="10"
128-
HorizontalAlignment="Left"
129-
VerticalAlignment="Center"
130-
FontSize="14"
131-
Text="{DynamicResource customQuery}" />
132-
<DockPanel
133-
Grid.Row="1"
134-
Grid.Column="1"
135-
LastChildFill="True">
136-
<Button
137-
x:Name="btnTestActionKeyword"
138-
Margin="0,0,10,0"
139-
Padding="10,5,10,5"
140-
Click="BtnTestActionKeyword_OnClick"
141-
Content="{DynamicResource preview}"
142-
DockPanel.Dock="Right" />
143-
<TextBox
144-
x:Name="tbAction"
145-
Margin="10"
146-
HorizontalAlignment="Stretch"
147-
VerticalAlignment="Center" />
148-
</DockPanel>
149-
</Grid>
150-
</StackPanel>
117+
Text="{DynamicResource customQuery}" />
118+
<TextBox
119+
x:Name="tbAction"
120+
Grid.Row="1"
121+
Grid.Column="1"
122+
Margin="10"
123+
HorizontalAlignment="Stretch"
124+
VerticalAlignment="Center" />
125+
<Button
126+
x:Name="btnTestActionKeyword"
127+
Grid.Row="1"
128+
Grid.Column="2"
129+
Margin="0,0,10,0"
130+
Padding="10,5,10,5"
131+
Click="BtnTestActionKeyword_OnClick"
132+
Content="{DynamicResource preview}" />
133+
</Grid>
151134
</StackPanel>
152135
</StackPanel>
153136
<Border
@@ -174,4 +157,4 @@
174157
</StackPanel>
175158
</Border>
176159
</Grid>
177-
</Window>
160+
</Window>

0 commit comments

Comments
 (0)