Skip to content

Commit 2489bfa

Browse files
authored
Merge pull request #1474 from VictoriousRaptor/Shortcut
Custom Shortcut update
2 parents 609e8e8 + 951ba4c commit 2489bfa

File tree

9 files changed

+206
-97
lines changed

9 files changed

+206
-97
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
using System;
2+
using System.Text.Json.Serialization;
3+
4+
namespace Flow.Launcher.Infrastructure.UserSettings
5+
{
6+
public abstract class ShortcutBaseModel
7+
{
8+
public string Key { get; set; }
9+
10+
[JsonIgnore]
11+
public Func<string> Expand { get; set; } = () => { return ""; };
12+
13+
public override bool Equals(object obj)
14+
{
15+
return obj is ShortcutBaseModel other &&
16+
Key == other.Key;
17+
}
18+
19+
public override int GetHashCode()
20+
{
21+
return HashCode.Combine(Key);
22+
}
23+
}
24+
25+
public class CustomShortcutModel : ShortcutBaseModel
26+
{
27+
public string Value { get; set; }
28+
29+
[JsonConstructorAttribute]
30+
public CustomShortcutModel(string key, string value)
31+
{
32+
Key = key;
33+
Value = value;
34+
Expand = () => { return Value; };
35+
}
36+
37+
public void Deconstruct(out string key, out string value)
38+
{
39+
key = Key;
40+
value = Value;
41+
}
42+
43+
public static implicit operator (string Key, string Value)(CustomShortcutModel shortcut)
44+
{
45+
return (shortcut.Key, shortcut.Value);
46+
}
47+
48+
public static implicit operator CustomShortcutModel((string Key, string Value) shortcut)
49+
{
50+
return new CustomShortcutModel(shortcut.Key, shortcut.Value);
51+
}
52+
}
53+
54+
public class BuiltinShortcutModel : ShortcutBaseModel
55+
{
56+
public string Description { get; set; }
57+
58+
public BuiltinShortcutModel(string key, string description, Func<string> expand)
59+
{
60+
Key = key;
61+
Description = description;
62+
Expand = expand ?? (() => { return ""; });
63+
}
64+
}
65+
}

Flow.Launcher.Infrastructure/UserSettings/Settings.cs

Lines changed: 8 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Collections.ObjectModel;
44
using System.Drawing;
55
using System.Text.Json.Serialization;
6+
using System.Windows;
67
using Flow.Launcher.Plugin;
78
using Flow.Launcher.Plugin.SharedModels;
89
using Flow.Launcher;
@@ -176,6 +177,13 @@ public string QuerySearchPrecisionString
176177

177178
public ObservableCollection<CustomPluginHotkey> CustomPluginHotkeys { get; set; } = new ObservableCollection<CustomPluginHotkey>();
178179

180+
public ObservableCollection<CustomShortcutModel> CustomShortcuts { get; set; } = new ObservableCollection<CustomShortcutModel>();
181+
182+
[JsonIgnore]
183+
public ObservableCollection<BuiltinShortcutModel> BuiltinShortcuts { get; set; } = new ObservableCollection<BuiltinShortcutModel>() {
184+
new BuiltinShortcutModel("{clipboard}", "Get text from clipboard.", Clipboard.GetText) // TODO: translation?
185+
};
186+
179187
public bool DontPromptUpdateMsg { get; set; }
180188
public bool EnableUpdateLog { get; set; }
181189

@@ -204,7 +212,6 @@ public bool HideNotifyIcon
204212

205213
// This needs to be loaded last by staying at the bottom
206214
public PluginsSettings PluginSettings { get; set; } = new PluginsSettings();
207-
internal ObservableCollection<ShortCutModel> ShortCuts { get; set; } = new();
208215
}
209216

210217
public enum LastQueryMode
@@ -220,44 +227,4 @@ public enum ColorSchemes
220227
Light,
221228
Dark
222229
}
223-
224-
public struct ShortCutModel
225-
{
226-
public string Key { get; set; }
227-
public string Value { get; set; }
228-
229-
public ShortCutModel(string key, string value)
230-
{
231-
Key = key;
232-
Value = value;
233-
}
234-
235-
public override bool Equals(object obj)
236-
{
237-
return obj is ShortCutModel other &&
238-
Key == other.Key &&
239-
Value == other.Value;
240-
}
241-
242-
public override int GetHashCode()
243-
{
244-
return HashCode.Combine(Key, Value);
245-
}
246-
247-
public void Deconstruct(out string key, out string value)
248-
{
249-
key = Key;
250-
value = Value;
251-
}
252-
253-
public static implicit operator (string Key, string Value)(ShortCutModel value)
254-
{
255-
return (value.Key, value.Value);
256-
}
257-
258-
public static implicit operator ShortCutModel((string Key, string Value) value)
259-
{
260-
return new ShortCutModel(value.Key, value.Value);
261-
}
262-
}
263230
}

Flow.Launcher/CustomShortcutSetting.xaml

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
44
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
55
xmlns:flowlauncher="clr-namespace:Flow.Launcher"
6-
Title="{DynamicResource customeQueryHotkeyTitle}"
6+
Title="{DynamicResource customeQueryShortcutTitle}"
77
Width="530"
88
Background="{DynamicResource PopuBGColor}"
99
Foreground="{DynamicResource PopupTextColor}"
@@ -67,13 +67,13 @@
6767
FontFamily="Segoe UI"
6868
FontSize="20"
6969
FontWeight="SemiBold"
70-
Text="{DynamicResource customeQueryHotkeyTitle}"
70+
Text="{DynamicResource customQueryShortcut}"
7171
TextAlignment="Left" />
7272
</StackPanel>
7373
<StackPanel>
7474
<TextBlock
7575
FontSize="14"
76-
Text="{DynamicResource customeQueryHotkeyTips}"
76+
Text="{DynamicResource customeQueryShortcutTips}"
7777
TextAlignment="Left"
7878
TextWrapping="WrapWithOverflow" />
7979
</StackPanel>
@@ -110,14 +110,25 @@
110110
VerticalAlignment="Center"
111111
FontSize="14"
112112
Text="{DynamicResource customShortcutExpansion}" />
113-
<TextBox
113+
114+
<DockPanel
114115
Grid.Row="1"
115116
Grid.Column="1"
116-
Margin="10"
117-
HorizontalAlignment="Stretch"
118-
VerticalAlignment="Center"
119-
Text="{Binding Value}"/>
120-
</Grid>
117+
LastChildFill="True">
118+
<Button
119+
x:Name="btnTestShortcut"
120+
Padding="10,5,10,5"
121+
Click="BtnTestShortcut_OnClick"
122+
Content="{DynamicResource preview}"
123+
DockPanel.Dock="Right" />
124+
<TextBox
125+
x:Name="tbExpand"
126+
Margin="10"
127+
HorizontalAlignment="Stretch"
128+
Text="{Binding Value}"
129+
VerticalAlignment="Center" />
130+
</DockPanel>
131+
</Grid>
121132
</StackPanel>
122133
</StackPanel>
123134
</StackPanel>
@@ -138,7 +149,7 @@
138149
x:Name="btnAdd"
139150
MinWidth="140"
140151
Margin="5,0,10,0"
141-
Click="btnAdd_OnClick"
152+
Click="BtnAdd_OnClick"
142153
Style="{StaticResource AccentButtonStyle}">
143154
<TextBlock x:Name="lblAdd" Text="{DynamicResource done}" />
144155
</Button>
Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,31 @@
11
using Flow.Launcher.Core.Resource;
2-
using Flow.Launcher.Helper;
32
using Flow.Launcher.Infrastructure.UserSettings;
4-
using System.Collections.ObjectModel;
5-
using System.Linq;
3+
using System;
64
using System.Windows;
75
using System.Windows.Input;
8-
using System.Windows.Controls;
9-
using System.Collections.Generic;
106

117
namespace Flow.Launcher
128
{
139
public partial class CustomShortcutSetting : Window
1410
{
15-
private SettingWindow _settingWidow;
16-
private bool update;
17-
private CustomPluginHotkey updateCustomHotkey;
1811
private Settings _settings;
19-
12+
private bool update = false;
2013
public string Key { get; set; }
2114
public string Value { get; set; }
22-
public ShortCutModel ShortCut => (Key, Value);
23-
public CustomShortcutSetting()
15+
public CustomShortcutModel ShortCut => (Key, Value);
16+
17+
public CustomShortcutSetting(Settings settings)
2418
{
19+
_settings = settings;
2520
InitializeComponent();
2621
}
2722

28-
public CustomShortcutSetting((string, string) shortcut)
23+
public CustomShortcutSetting(CustomShortcutModel shortcut, Settings settings)
2924
{
30-
(Key, Value) = shortcut;
25+
Key = shortcut.Key;
26+
Value = shortcut.Value;
27+
_settings = settings;
28+
update = true;
3129
InitializeComponent();
3230
}
3331

@@ -37,8 +35,18 @@ private void BtnCancel_OnClick(object sender, RoutedEventArgs e)
3735
Close();
3836
}
3937

40-
private void btnAdd_OnClick(object sender, RoutedEventArgs e)
38+
private void BtnAdd_OnClick(object sender, RoutedEventArgs e)
4139
{
40+
if (String.IsNullOrEmpty(Key) || String.IsNullOrEmpty(Value))
41+
{
42+
MessageBox.Show(InternationalizationManager.Instance.GetTranslation("emptyShortcut"));
43+
return;
44+
}
45+
if (!update && (_settings.CustomShortcuts.Contains(new CustomShortcutModel(Key, Value)) || _settings.BuiltinShortcuts.Contains(new BuiltinShortcutModel(Key, Value, null))))
46+
{
47+
MessageBox.Show(InternationalizationManager.Instance.GetTranslation("dulplicateShortcut"));
48+
return;
49+
}
4250
DialogResult = true;
4351
Close();
4452
}
@@ -48,5 +56,13 @@ private void cmdEsc_OnPress(object sender, ExecutedRoutedEventArgs e)
4856
DialogResult = false;
4957
Close();
5058
}
59+
60+
private void BtnTestShortcut_OnClick(object sender, RoutedEventArgs e)
61+
{
62+
App.API.ChangeQuery(tbExpand.Text);
63+
Application.Current.MainWindow.Show();
64+
Application.Current.MainWindow.Opacity = 1;
65+
Application.Current.MainWindow.Focus();
66+
}
5167
}
5268
}

Flow.Launcher/Languages/en.xaml

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,9 @@
113113
<system:String x:Key="openResultModifiersToolTip">Select a modifier key to open selected result via keyboard.</system:String>
114114
<system:String x:Key="showOpenResultHotkey">Show Hotkey</system:String>
115115
<system:String x:Key="showOpenResultHotkeyToolTip">Show result selection hotkey with results.</system:String>
116-
<system:String x:Key="customQueryHotkey">Custom Query Hotkey</system:String>
117-
<system:String x:Key="customQuery">Query</system:String>
116+
<system:String x:Key="customQueryHotkey">Custom Query Hotkey</system:String>
117+
<system:String x:Key="customQueryShortcut">Custom Query Shortcut</system:String>
118+
<system:String x:Key="customQuery">Query</system:String>
118119
<system:String x:Key="customShortcut">Shortcut</system:String>
119120
<system:String x:Key="customShortcutExpansion">Expanded</system:String>
120121
<system:String x:Key="delete">Delete</system:String>
@@ -211,7 +212,13 @@
211212
<system:String x:Key="invalidPluginHotkey">Invalid plugin hotkey</system:String>
212213
<system:String x:Key="update">Update</system:String>
213214

214-
<!-- Hotkey Control -->
215+
<!-- Custom Query Shortcut Dialog -->
216+
<system:String x:Key="customeQueryShortcutTitle">Custom Query Shortcut</system:String>
217+
<system:String x:Key="customeQueryShortcutTips">Enter a shortcut that automatically expands to the specified query.</system:String>
218+
<system:String x:Key="dulplicateShortcut">Shortcut is dulplicate, please enter a new Shortcut or edit the existing one.</system:String>
219+
<system:String x:Key="emptyShortcut">Shortcut and/or its expansion is empty.</system:String>
220+
221+
<!-- Hotkey Control -->
215222
<system:String x:Key="hotkeyUnavailable">Hotkey Unavailable</system:String>
216223

217224
<!-- Crash Reporter -->

Flow.Launcher/SettingWindow.xaml

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2175,7 +2175,7 @@
21752175
VerticalAlignment="Center"
21762176
FontSize="14"
21772177
Foreground="{DynamicResource Color05B}"
2178-
Text="Shortcut" />
2178+
Text="{DynamicResource customQueryShortcut}" />
21792179
<ListView
21802180
MinHeight="185"
21812181
Margin="0,6,0,0"
@@ -2184,18 +2184,17 @@
21842184
BorderThickness="1"
21852185
ItemsSource="{Binding ShortCuts}"
21862186
SelectedItem="{Binding SelectedCustomShortcut}"
2187-
SelectedIndex="{Binding SelectCustomShortcutIndex}"
21882187
Style="{StaticResource {x:Static GridView.GridViewStyleKey}}">
21892188
<ListView.View>
21902189
<GridView>
2191-
<GridViewColumn Header="Key">
2190+
<GridViewColumn Header="{DynamicResource customShortcut}">
21922191
<GridViewColumn.CellTemplate>
21932192
<DataTemplate>
21942193
<TextBlock Text="{Binding Key}" />
21952194
</DataTemplate>
21962195
</GridViewColumn.CellTemplate>
21972196
</GridViewColumn>
2198-
<GridViewColumn Header="Mapped Value">
2197+
<GridViewColumn Header="{DynamicResource customShortcutExpansion}">
21992198
<GridViewColumn.CellTemplate>
22002199
<DataTemplate>
22012200
<TextBlock Text="{Binding Value}" />
@@ -2223,10 +2222,45 @@
22232222
<Button
22242223
MinWidth="100"
22252224
Margin="10,10,0,10"
2226-
Click="OnAddCustomeShortCutClick"
2225+
Click="OnAddCustomShortCutClick"
22272226
Content="{DynamicResource add}" />
22282227
</StackPanel>
2229-
</StackPanel>
2228+
2229+
<TextBlock
2230+
Margin="0,0,12,2"
2231+
Padding="0,12,0,0"
2232+
VerticalAlignment="Center"
2233+
FontSize="14"
2234+
Foreground="{DynamicResource Color05B}"
2235+
Text="Built-in Shortcuts" />
2236+
<ListView
2237+
MinHeight="185"
2238+
Margin="0,6,0,0"
2239+
Background="{DynamicResource Color02B}"
2240+
BorderBrush="DarkGray"
2241+
BorderThickness="1"
2242+
ItemsSource="{Binding Settings.BuiltinShortcuts}"
2243+
Style="{StaticResource {x:Static GridView.GridViewStyleKey}}">
2244+
<ListView.View>
2245+
<GridView>
2246+
<GridViewColumn Header="{DynamicResource customShortcut}">
2247+
<GridViewColumn.CellTemplate>
2248+
<DataTemplate>
2249+
<TextBlock Text="{Binding Key}" />
2250+
</DataTemplate>
2251+
</GridViewColumn.CellTemplate>
2252+
</GridViewColumn>
2253+
<GridViewColumn Header="Description">
2254+
<GridViewColumn.CellTemplate>
2255+
<DataTemplate>
2256+
<TextBlock Text="{Binding Description}" />
2257+
</DataTemplate>
2258+
</GridViewColumn.CellTemplate>
2259+
</GridViewColumn>
2260+
</GridView>
2261+
</ListView.View>
2262+
</ListView>
2263+
</StackPanel>
22302264
</Grid>
22312265
</Border>
22322266
</ScrollViewer>

0 commit comments

Comments
 (0)