diff --git a/Flow.Launcher.Infrastructure/UserSettings/CustomShortcutModel.cs b/Flow.Launcher.Infrastructure/UserSettings/CustomShortcutModel.cs new file mode 100644 index 000000000..dcb466538 --- /dev/null +++ b/Flow.Launcher.Infrastructure/UserSettings/CustomShortcutModel.cs @@ -0,0 +1,65 @@ +using System; +using System.Text.Json.Serialization; + +namespace Flow.Launcher.Infrastructure.UserSettings +{ + public abstract class ShortcutBaseModel + { + public string Key { get; set; } + + [JsonIgnore] + public Func Expand { get; set; } = () => { return ""; }; + + public override bool Equals(object obj) + { + return obj is ShortcutBaseModel other && + Key == other.Key; + } + + public override int GetHashCode() + { + return HashCode.Combine(Key); + } + } + + public class CustomShortcutModel : ShortcutBaseModel + { + public string Value { get; set; } + + [JsonConstructorAttribute] + public CustomShortcutModel(string key, string value) + { + Key = key; + Value = value; + Expand = () => { return Value; }; + } + + public void Deconstruct(out string key, out string value) + { + key = Key; + value = Value; + } + + public static implicit operator (string Key, string Value)(CustomShortcutModel shortcut) + { + return (shortcut.Key, shortcut.Value); + } + + public static implicit operator CustomShortcutModel((string Key, string Value) shortcut) + { + return new CustomShortcutModel(shortcut.Key, shortcut.Value); + } + } + + public class BuiltinShortcutModel : ShortcutBaseModel + { + public string Description { get; set; } + + public BuiltinShortcutModel(string key, string description, Func expand) + { + Key = key; + Description = description; + Expand = expand ?? (() => { return ""; }); + } + } +} diff --git a/Flow.Launcher.Infrastructure/UserSettings/Settings.cs b/Flow.Launcher.Infrastructure/UserSettings/Settings.cs index b56989e7d..2dd7a4fe9 100644 --- a/Flow.Launcher.Infrastructure/UserSettings/Settings.cs +++ b/Flow.Launcher.Infrastructure/UserSettings/Settings.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Collections.ObjectModel; using System.Drawing; using System.Text.Json.Serialization; +using System.Windows; using Flow.Launcher.Plugin; using Flow.Launcher.Plugin.SharedModels; using Flow.Launcher; @@ -176,6 +177,13 @@ namespace Flow.Launcher.Infrastructure.UserSettings public ObservableCollection CustomPluginHotkeys { get; set; } = new ObservableCollection(); + public ObservableCollection CustomShortcuts { get; set; } = new ObservableCollection(); + + [JsonIgnore] + public ObservableCollection BuiltinShortcuts { get; set; } = new ObservableCollection() { + new BuiltinShortcutModel("{clipboard}", "Get text from clipboard.", Clipboard.GetText) // TODO: translation? + }; + public bool DontPromptUpdateMsg { get; set; } public bool EnableUpdateLog { get; set; } @@ -204,7 +212,6 @@ namespace Flow.Launcher.Infrastructure.UserSettings // This needs to be loaded last by staying at the bottom public PluginsSettings PluginSettings { get; set; } = new PluginsSettings(); - internal ObservableCollection ShortCuts { get; set; } = new(); } public enum LastQueryMode @@ -220,44 +227,4 @@ namespace Flow.Launcher.Infrastructure.UserSettings Light, Dark } - - public struct ShortCutModel - { - public string Key { get; set; } - public string Value { get; set; } - - public ShortCutModel(string key, string value) - { - Key = key; - Value = value; - } - - public override bool Equals(object obj) - { - return obj is ShortCutModel other && - Key == other.Key && - Value == other.Value; - } - - public override int GetHashCode() - { - return HashCode.Combine(Key, Value); - } - - public void Deconstruct(out string key, out string value) - { - key = Key; - value = Value; - } - - public static implicit operator (string Key, string Value)(ShortCutModel value) - { - return (value.Key, value.Value); - } - - public static implicit operator ShortCutModel((string Key, string Value) value) - { - return new ShortCutModel(value.Key, value.Value); - } - } } diff --git a/Flow.Launcher/CustomShortcutSetting.xaml b/Flow.Launcher/CustomShortcutSetting.xaml index 839836b6a..ab50bd859 100644 --- a/Flow.Launcher/CustomShortcutSetting.xaml +++ b/Flow.Launcher/CustomShortcutSetting.xaml @@ -3,7 +3,7 @@ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:flowlauncher="clr-namespace:Flow.Launcher" - Title="{DynamicResource customeQueryHotkeyTitle}" + Title="{DynamicResource customeQueryShortcutTitle}" Width="530" Background="{DynamicResource PopuBGColor}" Foreground="{DynamicResource PopupTextColor}" @@ -67,13 +67,13 @@ FontFamily="Segoe UI" FontSize="20" FontWeight="SemiBold" - Text="{DynamicResource customeQueryHotkeyTitle}" + Text="{DynamicResource customQueryShortcut}" TextAlignment="Left" /> @@ -110,14 +110,25 @@ VerticalAlignment="Center" FontSize="14" Text="{DynamicResource customShortcutExpansion}" /> - - + LastChildFill="True"> + diff --git a/Flow.Launcher/CustomShortcutSetting.xaml.cs b/Flow.Launcher/CustomShortcutSetting.xaml.cs index feea064a4..524ea69ed 100644 --- a/Flow.Launcher/CustomShortcutSetting.xaml.cs +++ b/Flow.Launcher/CustomShortcutSetting.xaml.cs @@ -1,33 +1,31 @@ using Flow.Launcher.Core.Resource; -using Flow.Launcher.Helper; using Flow.Launcher.Infrastructure.UserSettings; -using System.Collections.ObjectModel; -using System.Linq; +using System; using System.Windows; using System.Windows.Input; -using System.Windows.Controls; -using System.Collections.Generic; namespace Flow.Launcher { public partial class CustomShortcutSetting : Window { - private SettingWindow _settingWidow; - private bool update; - private CustomPluginHotkey updateCustomHotkey; private Settings _settings; - + private bool update = false; public string Key { get; set; } public string Value { get; set; } - public ShortCutModel ShortCut => (Key, Value); - public CustomShortcutSetting() + public CustomShortcutModel ShortCut => (Key, Value); + + public CustomShortcutSetting(Settings settings) { + _settings = settings; InitializeComponent(); } - public CustomShortcutSetting((string, string) shortcut) + public CustomShortcutSetting(CustomShortcutModel shortcut, Settings settings) { - (Key, Value) = shortcut; + Key = shortcut.Key; + Value = shortcut.Value; + _settings = settings; + update = true; InitializeComponent(); } @@ -37,8 +35,18 @@ namespace Flow.Launcher Close(); } - private void btnAdd_OnClick(object sender, RoutedEventArgs e) + private void BtnAdd_OnClick(object sender, RoutedEventArgs e) { + if (String.IsNullOrEmpty(Key) || String.IsNullOrEmpty(Value)) + { + MessageBox.Show(InternationalizationManager.Instance.GetTranslation("emptyShortcut")); + return; + } + if (!update && (_settings.CustomShortcuts.Contains(new CustomShortcutModel(Key, Value)) || _settings.BuiltinShortcuts.Contains(new BuiltinShortcutModel(Key, Value, null)))) + { + MessageBox.Show(InternationalizationManager.Instance.GetTranslation("dulplicateShortcut")); + return; + } DialogResult = true; Close(); } @@ -48,5 +56,13 @@ namespace Flow.Launcher DialogResult = false; Close(); } + + private void BtnTestShortcut_OnClick(object sender, RoutedEventArgs e) + { + App.API.ChangeQuery(tbExpand.Text); + Application.Current.MainWindow.Show(); + Application.Current.MainWindow.Opacity = 1; + Application.Current.MainWindow.Focus(); + } } } diff --git a/Flow.Launcher/Languages/en.xaml b/Flow.Launcher/Languages/en.xaml index 69c0a73ae..7c01e680f 100644 --- a/Flow.Launcher/Languages/en.xaml +++ b/Flow.Launcher/Languages/en.xaml @@ -113,8 +113,9 @@ Select a modifier key to open selected result via keyboard. Show Hotkey Show result selection hotkey with results. - Custom Query Hotkey - Query + Custom Query Hotkey + Custom Query Shortcut + Query Shortcut Expanded Delete @@ -211,7 +212,13 @@ Invalid plugin hotkey Update - + + Custom Query Shortcut + Enter a shortcut that automatically expands to the specified query. + Shortcut is dulplicate, please enter a new Shortcut or edit the existing one. + Shortcut and/or its expansion is empty. + + Hotkey Unavailable diff --git a/Flow.Launcher/SettingWindow.xaml b/Flow.Launcher/SettingWindow.xaml index 47933f326..75f792180 100644 --- a/Flow.Launcher/SettingWindow.xaml +++ b/Flow.Launcher/SettingWindow.xaml @@ -2175,7 +2175,7 @@ VerticalAlignment="Center" FontSize="14" Foreground="{DynamicResource Color05B}" - Text="Shortcut" /> + Text="{DynamicResource customQueryShortcut}" /> - + - + @@ -2223,10 +2222,45 @@