Merge pull request #3803 from Flow-Launcher/CustomQueryHotkeySetting_refactor

Refactor CustomQueryHotkeySetting control
This commit is contained in:
Jeremy Wu 2025-07-10 21:45:07 +10:00 committed by GitHub
commit 48d08984ec
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 104 additions and 58 deletions

View file

@ -1,4 +1,5 @@
using Flow.Launcher.Plugin; using System;
using Flow.Launcher.Plugin;
namespace Flow.Launcher.Infrastructure.UserSettings namespace Flow.Launcher.Infrastructure.UserSettings
{ {
@ -6,5 +7,26 @@ namespace Flow.Launcher.Infrastructure.UserSettings
{ {
public string Hotkey { get; set; } public string Hotkey { get; set; }
public string ActionKeyword { get; set; } public string ActionKeyword { get; set; }
public CustomPluginHotkey(string hotkey, string actionKeyword)
{
Hotkey = hotkey;
ActionKeyword = actionKeyword;
}
public override bool Equals(object other)
{
if (other is CustomPluginHotkey otherHotkey)
{
return Hotkey == otherHotkey.Hotkey && ActionKeyword == otherHotkey.ActionKeyword;
}
return false;
}
public override int GetHashCode()
{
return HashCode.Combine(Hotkey, ActionKeyword);
}
} }
} }

View file

@ -119,7 +119,8 @@
Grid.Column="1" Grid.Column="1"
Margin="10" Margin="10"
HorizontalAlignment="Stretch" HorizontalAlignment="Stretch"
VerticalAlignment="Center" /> VerticalAlignment="Center"
Text="{Binding ActionKeyword}" />
<Button <Button
x:Name="btnTestActionKeyword" x:Name="btnTestActionKeyword"
Grid.Row="1" Grid.Row="1"
@ -150,7 +151,20 @@
Margin="5 0 10 0" Margin="5 0 10 0"
Click="btnAdd_OnClick" Click="btnAdd_OnClick"
Style="{StaticResource AccentButtonStyle}"> Style="{StaticResource AccentButtonStyle}">
<TextBlock x:Name="lblAdd" Text="{DynamicResource done}" /> <Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<TextBlock
x:Name="tbAdd"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Text="{DynamicResource done}"
Visibility="Collapsed" />
<TextBlock
x:Name="tbUpdate"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Text="{DynamicResource update}"
Visibility="Collapsed" />
</Grid>
</Button> </Button>
</StackPanel> </StackPanel>
</Border> </Border>

View file

@ -1,73 +1,52 @@
using System.Collections.ObjectModel; using System.Windows;
using System.Linq;
using System.Windows;
using System.Windows.Input;
using System.Windows.Controls; using System.Windows.Controls;
using Flow.Launcher.Helper; using System.Windows.Input;
using Flow.Launcher.Infrastructure.UserSettings; using Flow.Launcher.Infrastructure.UserSettings;
namespace Flow.Launcher namespace Flow.Launcher
{ {
public partial class CustomQueryHotkeySetting : Window public partial class CustomQueryHotkeySetting : Window
{ {
private readonly Settings _settings; public string Hotkey { get; set; } = string.Empty;
public string ActionKeyword { get; set; } = string.Empty;
private bool update; private readonly bool update;
private CustomPluginHotkey updateCustomHotkey; private readonly CustomPluginHotkey originalCustomHotkey;
public CustomQueryHotkeySetting(Settings settings) public CustomQueryHotkeySetting()
{ {
_settings = settings;
InitializeComponent(); InitializeComponent();
tbAdd.Visibility = Visibility.Visible;
}
public CustomQueryHotkeySetting(CustomPluginHotkey hotkey)
{
originalCustomHotkey = hotkey;
update = true;
ActionKeyword = originalCustomHotkey.ActionKeyword;
InitializeComponent();
tbUpdate.Visibility = Visibility.Visible;
HotkeyControl.SetHotkey(originalCustomHotkey.Hotkey, false);
} }
private void BtnCancel_OnClick(object sender, RoutedEventArgs e) private void BtnCancel_OnClick(object sender, RoutedEventArgs e)
{ {
DialogResult = false;
Close(); Close();
} }
private void btnAdd_OnClick(object sender, RoutedEventArgs e) private void btnAdd_OnClick(object sender, RoutedEventArgs e)
{ {
if (!update) Hotkey = HotkeyControl.CurrentHotkey.ToString();
if (string.IsNullOrEmpty(Hotkey) && string.IsNullOrEmpty(ActionKeyword))
{ {
_settings.CustomPluginHotkeys ??= new ObservableCollection<CustomPluginHotkey>(); App.API.ShowMsgBox(App.API.GetTranslation("emptyPluginHotkey"));
var pluginHotkey = new CustomPluginHotkey
{
Hotkey = HotkeyControl.CurrentHotkey.ToString(), ActionKeyword = tbAction.Text
};
_settings.CustomPluginHotkeys.Add(pluginHotkey);
HotKeyMapper.SetCustomQueryHotkey(pluginHotkey);
}
else
{
var oldHotkey = updateCustomHotkey.Hotkey;
updateCustomHotkey.ActionKeyword = tbAction.Text;
updateCustomHotkey.Hotkey = HotkeyControl.CurrentHotkey.ToString();
//remove origin hotkey
HotKeyMapper.RemoveHotkey(oldHotkey);
HotKeyMapper.SetCustomQueryHotkey(updateCustomHotkey);
}
Close();
}
public void UpdateItem(CustomPluginHotkey item)
{
updateCustomHotkey = _settings.CustomPluginHotkeys.FirstOrDefault(o =>
o.ActionKeyword == item.ActionKeyword && o.Hotkey == item.Hotkey);
if (updateCustomHotkey == null)
{
App.API.ShowMsgBox(App.API.GetTranslation("invalidPluginHotkey"));
Close();
return; return;
} }
tbAction.Text = updateCustomHotkey.ActionKeyword; DialogResult = !update || originalCustomHotkey.Hotkey != Hotkey || originalCustomHotkey.ActionKeyword != ActionKeyword;
HotkeyControl.SetHotkey(updateCustomHotkey.Hotkey, false); Close();
update = true;
lblAdd.Text = App.API.GetTranslation("update");
} }
private void BtnTestActionKeyword_OnClick(object sender, RoutedEventArgs e) private void BtnTestActionKeyword_OnClick(object sender, RoutedEventArgs e)
@ -79,6 +58,7 @@ namespace Flow.Launcher
private void cmdEsc_OnPress(object sender, ExecutedRoutedEventArgs e) private void cmdEsc_OnPress(object sender, ExecutedRoutedEventArgs e)
{ {
DialogResult = false;
Close(); Close();
} }

View file

@ -43,12 +43,14 @@ namespace Flow.Launcher
App.API.ShowMsgBox(App.API.GetTranslation("emptyShortcut")); App.API.ShowMsgBox(App.API.GetTranslation("emptyShortcut"));
return; return;
} }
// Check if key is modified or adding a new one // Check if key is modified or adding a new one
if (((update && originalKey != Key) || !update) && _hotkeyVm.DoesShortcutExist(Key)) if (((update && originalKey != Key) || !update) && _hotkeyVm.DoesShortcutExist(Key))
{ {
App.API.ShowMsgBox(App.API.GetTranslation("duplicateShortcut")); App.API.ShowMsgBox(App.API.GetTranslation("duplicateShortcut"));
return; return;
} }
DialogResult = !update || originalKey != Key || originalValue != Value; DialogResult = !update || originalKey != Key || originalValue != Value;
Close(); Close();
} }

View file

@ -12,7 +12,7 @@
<system:String x:Key="runtimeExecutableInvalidChooseDownload"> <system:String x:Key="runtimeExecutableInvalidChooseDownload">
Your selected {0} executable is invalid. Your selected {0} executable is invalid.
{2}{2} {2}{2}
Click yes if you would like select the {0} executable agian. Click no if you would like to download {1} Click yes if you would like select the {0} executable again. Click no if you would like to download {1}
</system:String> </system:String>
<system:String x:Key="runtimePluginUnableToSetExecutablePath">Unable to set {0} executable path, please try from Flow's settings (scroll down to the bottom).</system:String> <system:String x:Key="runtimePluginUnableToSetExecutablePath">Unable to set {0} executable path, please try from Flow's settings (scroll down to the bottom).</system:String>
<system:String x:Key="failedToInitializePluginsTitle">Fail to Init Plugins</system:String> <system:String x:Key="failedToInitializePluginsTitle">Fail to Init Plugins</system:String>
@ -410,7 +410,7 @@
<system:String x:Key="fileManagerWindow">Select File Manager</system:String> <system:String x:Key="fileManagerWindow">Select File Manager</system:String>
<system:String x:Key="fileManager_learnMore">Learn more</system:String> <system:String x:Key="fileManager_learnMore">Learn more</system:String>
<system:String x:Key="fileManager_tips">Please specify the file location of the file manager you using and add arguments as required. The &quot;%d&quot; represents the directory path to open for, used by the Arg for Folder field and for commands opening specific directories. The &quot;%f&quot; represents the file path to open for, used by the Arg for File field and for commands opening specific files.</system:String> <system:String x:Key="fileManager_tips">Please specify the file location of the file manager you using and add arguments as required. The &quot;%d&quot; represents the directory path to open for, used by the Arg for Folder field and for commands opening specific directories. The &quot;%f&quot; represents the file path to open for, used by the Arg for File field and for commands opening specific files.</system:String>
<system:String x:Key="fileManager_tips2">For example, if the file manager uses a command such as &quot;totalcmd.exe /A c:\windows&quot; to open the c:\windows directory, the File Manager Path will be totalcmd.exe, and the Arg For Folder will be /A &quot;%d&quot;. Certain file managers like QTTabBar may just require a path to be supplied, in this instance use &quot;%d&quot; as the File Manager Path and leave the rest of the fileds blank.</system:String> <system:String x:Key="fileManager_tips2">For example, if the file manager uses a command such as &quot;totalcmd.exe /A c:\windows&quot; to open the c:\windows directory, the File Manager Path will be totalcmd.exe, and the Arg For Folder will be /A &quot;%d&quot;. Certain file managers like QTTabBar may just require a path to be supplied, in this instance use &quot;%d&quot; as the File Manager Path and leave the rest of the fields blank.</system:String>
<system:String x:Key="fileManager_name">File Manager</system:String> <system:String x:Key="fileManager_name">File Manager</system:String>
<system:String x:Key="fileManager_profile_name">Profile Name</system:String> <system:String x:Key="fileManager_profile_name">Profile Name</system:String>
<system:String x:Key="fileManager_path">File Manager Path</system:String> <system:String x:Key="fileManager_path">File Manager Path</system:String>
@ -461,13 +461,14 @@
<system:String x:Key="customeQueryHotkeyTips">Press a custom hotkey to open Flow Launcher and input the specified query automatically.</system:String> <system:String x:Key="customeQueryHotkeyTips">Press a custom hotkey to open Flow Launcher and input the specified query automatically.</system:String>
<system:String x:Key="preview">Preview</system:String> <system:String x:Key="preview">Preview</system:String>
<system:String x:Key="hotkeyIsNotUnavailable">Hotkey is unavailable, please select a new hotkey</system:String> <system:String x:Key="hotkeyIsNotUnavailable">Hotkey is unavailable, please select a new hotkey</system:String>
<system:String x:Key="invalidPluginHotkey">Invalid plugin hotkey</system:String> <system:String x:Key="invalidPluginHotkey">Hotkey is invalid</system:String>
<system:String x:Key="update">Update</system:String> <system:String x:Key="update">Update</system:String>
<system:String x:Key="hotkeyRegTitle">Binding Hotkey</system:String> <system:String x:Key="hotkeyRegTitle">Binding Hotkey</system:String>
<system:String x:Key="hotkeyUnavailable">Current hotkey is unavailable.</system:String> <system:String x:Key="hotkeyUnavailable">Current hotkey is unavailable.</system:String>
<system:String x:Key="hotkeyUnavailableUneditable">This hotkey is reserved for "{0}" and can't be used. Please choose another hotkey.</system:String> <system:String x:Key="hotkeyUnavailableUneditable">This hotkey is reserved for "{0}" and can't be used. Please choose another hotkey.</system:String>
<system:String x:Key="hotkeyUnavailableEditable">This hotkey is already in use by "{0}". If you press "Overwrite", it will be removed from "{0}".</system:String> <system:String x:Key="hotkeyUnavailableEditable">This hotkey is already in use by "{0}". If you press "Overwrite", it will be removed from "{0}".</system:String>
<system:String x:Key="hotkeyRegGuide">Press the keys you want to use for this function.</system:String> <system:String x:Key="hotkeyRegGuide">Press the keys you want to use for this function.</system:String>
<system:String x:Key="emptyPluginHotkey">Hotkey and action keyword are empty</system:String>
<!-- Custom Query Shortcut Dialog --> <!-- Custom Query Shortcut Dialog -->
<system:String x:Key="customeQueryShortcutTitle">Custom Query Shortcut</system:String> <system:String x:Key="customeQueryShortcutTitle">Custom Query Shortcut</system:String>
@ -476,6 +477,7 @@
</system:String> </system:String>
<system:String x:Key="duplicateShortcut">Shortcut already exists, please enter a new Shortcut or edit the existing one.</system:String> <system:String x:Key="duplicateShortcut">Shortcut already exists, please enter a new Shortcut or edit the existing one.</system:String>
<system:String x:Key="emptyShortcut">Shortcut and/or its expansion is empty.</system:String> <system:String x:Key="emptyShortcut">Shortcut and/or its expansion is empty.</system:String>
<system:String x:Key="invalidShortcut">Shortcut is invalid</system:String>
<!-- Common Action --> <!-- Common Action -->
<system:String x:Key="commonSave">Save</system:String> <system:String x:Key="commonSave">Save</system:String>

View file

@ -69,15 +69,33 @@ public partial class SettingsPaneHotkeyViewModel : BaseModel
return; return;
} }
var window = new CustomQueryHotkeySetting(Settings); var settingItem = Settings.CustomPluginHotkeys.FirstOrDefault(o =>
window.UpdateItem(item); o.ActionKeyword == item.ActionKeyword && o.Hotkey == item.Hotkey);
window.ShowDialog(); if (settingItem == null)
{
App.API.ShowMsgBox(App.API.GetTranslation("invalidPluginHotkey"));
return;
}
var window = new CustomQueryHotkeySetting(settingItem);
if (window.ShowDialog() is not true) return;
var index = Settings.CustomPluginHotkeys.IndexOf(settingItem);
Settings.CustomPluginHotkeys[index] = new CustomPluginHotkey(window.Hotkey, window.ActionKeyword);
HotKeyMapper.RemoveHotkey(settingItem.Hotkey); // remove origin hotkey
HotKeyMapper.SetCustomQueryHotkey(Settings.CustomPluginHotkeys[index]); // set new hotkey
} }
[RelayCommand] [RelayCommand]
private void CustomHotkeyAdd() private void CustomHotkeyAdd()
{ {
new CustomQueryHotkeySetting(Settings).ShowDialog(); var window = new CustomQueryHotkeySetting();
if (window.ShowDialog() is true)
{
var customHotkey = new CustomPluginHotkey(window.Hotkey, window.ActionKeyword);
Settings.CustomPluginHotkeys.Add(customHotkey);
HotKeyMapper.SetCustomQueryHotkey(customHotkey); // set new hotkey
}
} }
[RelayCommand] [RelayCommand]
@ -114,10 +132,18 @@ public partial class SettingsPaneHotkeyViewModel : BaseModel
return; return;
} }
var window = new CustomShortcutSetting(item.Key, item.Value, this); var settingItem = Settings.CustomShortcuts.FirstOrDefault(o =>
o.Key == item.Key && o.Value == item.Value);
if (settingItem == null)
{
App.API.ShowMsgBox(App.API.GetTranslation("invalidShortcut"));
return;
}
var window = new CustomShortcutSetting(settingItem.Key, settingItem.Value, this);
if (window.ShowDialog() is not true) return; if (window.ShowDialog() is not true) return;
var index = Settings.CustomShortcuts.IndexOf(item); var index = Settings.CustomShortcuts.IndexOf(settingItem);
Settings.CustomShortcuts[index] = new CustomShortcutModel(window.Key, window.Value); Settings.CustomShortcuts[index] = new CustomShortcutModel(window.Key, window.Value);
} }