mirror of
https://github.com/Flow-Launcher/Flow.Launcher.git
synced 2026-03-11 08:54:32 +00:00
Add window key registeration
This commit is contained in:
parent
f841175ccd
commit
8abd5318b3
3 changed files with 137 additions and 1 deletions
|
|
@ -6,6 +6,7 @@ using System.Linq;
|
|||
using System.Text.Json;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Input;
|
||||
using CommunityToolkit.Mvvm.DependencyInjection;
|
||||
using Flow.Launcher.Core.ExternalPlugins;
|
||||
using Flow.Launcher.Infrastructure;
|
||||
|
|
@ -39,6 +40,7 @@ namespace Flow.Launcher.Core.Plugin
|
|||
private static PluginsSettings Settings;
|
||||
private static List<PluginMetadata> _metadatas;
|
||||
private static readonly List<string> _modifiedPlugins = new();
|
||||
private static readonly Dictionary<KeyGesture, List<(PluginMetadata, SearchWindowPluginHotkey)>> _windowPluginHotkeys = new();
|
||||
|
||||
/// <summary>
|
||||
/// Directories that will hold Flow Launcher plugin directory
|
||||
|
|
@ -252,7 +254,9 @@ namespace Flow.Launcher.Core.Plugin
|
|||
_contextMenuPlugins = GetPluginsForInterface<IContextMenu>();
|
||||
_homePlugins = GetPluginsForInterface<IAsyncHomeQuery>();
|
||||
_hotkeyPlugins = GetPluginsForInterface<IPluginHotkey>();
|
||||
Settings.UpdatePluginHotkeyInfo(GetPluginHotkeyInfo());
|
||||
var pluginHotkeyInfo = GetPluginHotkeyInfo();
|
||||
Settings.UpdatePluginHotkeyInfo(pluginHotkeyInfo);
|
||||
InitializeWindowPluginHotkeys(pluginHotkeyInfo);
|
||||
|
||||
foreach (var plugin in AllPlugins)
|
||||
{
|
||||
|
|
@ -457,6 +461,39 @@ namespace Flow.Launcher.Core.Plugin
|
|||
return hotkeyPluginInfos;
|
||||
}
|
||||
|
||||
public static Dictionary<KeyGesture, List<(PluginMetadata Metadata, SearchWindowPluginHotkey SearchWindowPluginHotkey)>> GetWindowPluginHotkeys()
|
||||
{
|
||||
return _windowPluginHotkeys;
|
||||
}
|
||||
|
||||
private static void InitializeWindowPluginHotkeys(Dictionary<PluginPair, List<BasePluginHotkey>> pluginHotkeyInfo)
|
||||
{
|
||||
foreach (var info in pluginHotkeyInfo)
|
||||
{
|
||||
var pluginPair = info.Key;
|
||||
var hotkeyInfo = info.Value;
|
||||
var metadata = pluginPair.Metadata;
|
||||
foreach (var hotkey in hotkeyInfo)
|
||||
{
|
||||
if (hotkey.HotkeyType == HotkeyType.SearchWindow && hotkey is SearchWindowPluginHotkey searchWindowHotkey)
|
||||
{
|
||||
var hotkeySetting = metadata.PluginHotkeys.Find(h => h.Id == hotkey.Id)?.Hotkey ?? hotkey.DefaultHotkey;
|
||||
var converter = new KeyGestureConverter();
|
||||
var keyGesture = (KeyGesture)converter.ConvertFromString(hotkeySetting);
|
||||
if (keyGesture != null)
|
||||
{
|
||||
if (!_windowPluginHotkeys.TryGetValue(keyGesture, out var list))
|
||||
{
|
||||
list = new List<(PluginMetadata, SearchWindowPluginHotkey)>();
|
||||
_windowPluginHotkeys[keyGesture] = list;
|
||||
}
|
||||
list.Add((pluginPair.Metadata, searchWindowHotkey));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static bool ActionKeywordRegistered(string actionKeyword)
|
||||
{
|
||||
// this method is only checking for action keywords (defined as not '*') registration
|
||||
|
|
|
|||
|
|
@ -1,6 +1,11 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Windows;
|
||||
using System.Windows.Input;
|
||||
using ChefKeys;
|
||||
using CommunityToolkit.Mvvm.DependencyInjection;
|
||||
using CommunityToolkit.Mvvm.Input;
|
||||
using Flow.Launcher.Core.Plugin;
|
||||
using Flow.Launcher.Infrastructure.Hotkey;
|
||||
using Flow.Launcher.Infrastructure.UserSettings;
|
||||
|
|
@ -26,6 +31,7 @@ internal static class HotKeyMapper
|
|||
SetHotkey(_settings.Hotkey, OnToggleHotkey);
|
||||
LoadCustomPluginHotkey();
|
||||
LoadGlobalPluginHotkey();
|
||||
LoadWindowPluginHotkey();
|
||||
}
|
||||
|
||||
internal static void OnToggleHotkey(object sender, HotkeyEventArgs args)
|
||||
|
|
@ -175,6 +181,97 @@ internal static class HotKeyMapper
|
|||
});
|
||||
}
|
||||
|
||||
internal static void LoadWindowPluginHotkey()
|
||||
{
|
||||
var windowPluginHotkeys = PluginManager.GetWindowPluginHotkeys();
|
||||
foreach (var hotkey in windowPluginHotkeys)
|
||||
{
|
||||
var keyGesture = hotkey.Key;
|
||||
SetWindowHotkey(keyGesture, hotkey.Value);
|
||||
}
|
||||
}
|
||||
|
||||
private static void SetWindowHotkey(KeyGesture keyGesture, List<(PluginMetadata Metadata, SearchWindowPluginHotkey PluginHotkey)> hotkeyModels)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (Application.Current?.MainWindow is MainWindow window)
|
||||
{
|
||||
var command = BuildCommand(hotkeyModels);
|
||||
|
||||
// Remove any existing key binding with the same gesture to avoid duplication
|
||||
var existingBinding = window.InputBindings
|
||||
.OfType<KeyBinding>()
|
||||
.FirstOrDefault(kb => kb.Gesture == keyGesture);
|
||||
|
||||
if (existingBinding != null)
|
||||
{
|
||||
throw new InvalidOperationException($"Key binding with gesture {keyGesture} already exists");
|
||||
}
|
||||
|
||||
// Create and add the new key binding
|
||||
var keyBinding = new KeyBinding(command, keyGesture);
|
||||
window.InputBindings.Add(keyBinding);
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
App.API.LogError(ClassName,
|
||||
string.Format("Error registering window hotkey {2}: {0} \nStackTrace:{1}",
|
||||
e.Message,
|
||||
e.StackTrace,
|
||||
keyGesture.DisplayString));
|
||||
string errorMsg = string.Format(App.API.GetTranslation("registerWindowHotkeyFailed"), keyGesture.DisplayString);
|
||||
string errorMsgTitle = App.API.GetTranslation("MessageBoxTitle");
|
||||
App.API.ShowMsgBox(errorMsg, errorMsgTitle);
|
||||
}
|
||||
}
|
||||
|
||||
private static ICommand BuildCommand(List<(PluginMetadata Metadata, SearchWindowPluginHotkey PluginHotkey)> hotkeyModels)
|
||||
{
|
||||
return new RelayCommand(() =>
|
||||
{
|
||||
foreach (var hotkeyModel in hotkeyModels)
|
||||
{
|
||||
var metadata = hotkeyModel.Metadata;
|
||||
if (metadata.Disabled)
|
||||
continue;
|
||||
|
||||
var pluginHotkey = hotkeyModel.PluginHotkey;
|
||||
if (pluginHotkey.Action?.Invoke(_mainViewModel.Results.SelectedItem?.Result) ?? false)
|
||||
App.API.HideMainWindow();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
internal static void RemoveWindowHotkey(KeyGesture keyGesture)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (Application.Current?.MainWindow is MainWindow window)
|
||||
{
|
||||
// Find and remove the key binding with the specified gesture
|
||||
var existingBinding = window.InputBindings
|
||||
.OfType<KeyBinding>()
|
||||
.FirstOrDefault(kb => kb.Gesture == keyGesture);
|
||||
if (existingBinding != null)
|
||||
{
|
||||
window.InputBindings.Remove(existingBinding);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
App.API.LogError(ClassName,
|
||||
string.Format("Error removing window hotkey: {0} \nStackTrace:{1}",
|
||||
e.Message,
|
||||
e.StackTrace));
|
||||
string errorMsg = string.Format(App.API.GetTranslation("unregisterWindowHotkeyFailed"), keyGesture.DisplayString);
|
||||
string errorMsgTitle = App.API.GetTranslation("MessageBoxTitle");
|
||||
App.API.ShowMsgBox(errorMsg, errorMsgTitle);
|
||||
}
|
||||
}
|
||||
|
||||
internal static bool CheckAvailability(HotkeyModel currentHotkey)
|
||||
{
|
||||
try
|
||||
|
|
|
|||
|
|
@ -21,6 +21,8 @@
|
|||
<!-- MainWindow -->
|
||||
<system:String x:Key="registerHotkeyFailed">Failed to register hotkey "{0}". The hotkey may be in use by another program. Change to a different hotkey, or exit another program.</system:String>
|
||||
<system:String x:Key="unregisterHotkeyFailed">Failed to unregister hotkey "{0}". Please try again or see log for details</system:String>
|
||||
<system:String x:Key="registerWindowHotkeyFailed">Failed to register window hotkey "{0}". The hotkey may be in use by another program. Change to a different hotkey, or exit another program.</system:String>
|
||||
<system:String x:Key="unregisterWindowHotkeyFailed">Failed to unregister window hotkey "{0}". Please try again or see log for details</system:String>
|
||||
<system:String x:Key="MessageBoxTitle">Flow Launcher</system:String>
|
||||
<system:String x:Key="couldnotStartCmd">Could not start {0}</system:String>
|
||||
<system:String x:Key="invalidFlowLauncherPluginFileFormat">Invalid Flow Launcher plugin file format</system:String>
|
||||
|
|
|
|||
Loading…
Reference in a new issue