mirror of
https://github.com/Flow-Launcher/Flow.Launcher.git
synced 2026-03-11 08:54:32 +00:00
Save & restore old command
This commit is contained in:
parent
724b8a72cb
commit
c7de03bbec
1 changed files with 27 additions and 6 deletions
|
|
@ -23,6 +23,8 @@ internal static class HotKeyMapper
|
||||||
private static Settings _settings;
|
private static Settings _settings;
|
||||||
private static MainViewModel _mainViewModel;
|
private static MainViewModel _mainViewModel;
|
||||||
|
|
||||||
|
private static readonly Dictionary<HotkeyModel, ICommand> _windowHotkeyEvents = new();
|
||||||
|
|
||||||
internal static void Initialize()
|
internal static void Initialize()
|
||||||
{
|
{
|
||||||
_mainViewModel = Ioc.Default.GetRequiredService<MainViewModel>();
|
_mainViewModel = Ioc.Default.GetRequiredService<MainViewModel>();
|
||||||
|
|
@ -204,9 +206,7 @@ internal static class HotKeyMapper
|
||||||
if (hotkeyModels.Count == 0) return;
|
if (hotkeyModels.Count == 0) return;
|
||||||
if (Application.Current?.MainWindow is MainWindow window)
|
if (Application.Current?.MainWindow is MainWindow window)
|
||||||
{
|
{
|
||||||
var command = BuildCommand(hotkeyModels);
|
// Cache the command for the hotkey if it already exists
|
||||||
|
|
||||||
// Remove any existing key binding with the same gesture to avoid duplication
|
|
||||||
var keyGesture = hotkey.ToKeyGesture();
|
var keyGesture = hotkey.ToKeyGesture();
|
||||||
var existingBinding = window.InputBindings
|
var existingBinding = window.InputBindings
|
||||||
.OfType<KeyBinding>()
|
.OfType<KeyBinding>()
|
||||||
|
|
@ -214,13 +214,22 @@ internal static class HotKeyMapper
|
||||||
kb.Gesture is KeyGesture keyGesture1 &&
|
kb.Gesture is KeyGesture keyGesture1 &&
|
||||||
keyGesture.Key == keyGesture1.Key &&
|
keyGesture.Key == keyGesture1.Key &&
|
||||||
keyGesture.Modifiers == keyGesture1.Modifiers);
|
keyGesture.Modifiers == keyGesture1.Modifiers);
|
||||||
|
|
||||||
if (existingBinding != null)
|
if (existingBinding != null)
|
||||||
{
|
{
|
||||||
throw new InvalidOperationException($"Key binding with gesture {hotkey} already exists");
|
// If the hotkey exists, remove the old command
|
||||||
|
if (_windowHotkeyEvents.ContainsKey(hotkey))
|
||||||
|
{
|
||||||
|
window.InputBindings.Remove(existingBinding);
|
||||||
|
}
|
||||||
|
// If the hotkey does not exist, save the old command
|
||||||
|
else
|
||||||
|
{
|
||||||
|
_windowHotkeyEvents[hotkey] = existingBinding.Command;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create and add the new key binding
|
// Create and add the new key binding
|
||||||
|
var command = BuildCommand(hotkey, hotkeyModels);
|
||||||
var keyBinding = new KeyBinding(command, keyGesture);
|
var keyBinding = new KeyBinding(command, keyGesture);
|
||||||
window.InputBindings.Add(keyBinding);
|
window.InputBindings.Add(keyBinding);
|
||||||
}
|
}
|
||||||
|
|
@ -238,10 +247,15 @@ internal static class HotKeyMapper
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static ICommand BuildCommand(List<(PluginMetadata Metadata, SearchWindowPluginHotkey PluginHotkey)> hotkeyModels)
|
private static ICommand BuildCommand(HotkeyModel hotkey, List<(PluginMetadata Metadata, SearchWindowPluginHotkey PluginHotkey)> hotkeyModels)
|
||||||
{
|
{
|
||||||
return new RelayCommand(() =>
|
return new RelayCommand(() =>
|
||||||
{
|
{
|
||||||
|
if (_windowHotkeyEvents.TryGetValue(hotkey, out var existingCommand))
|
||||||
|
{
|
||||||
|
existingCommand.Execute(null);
|
||||||
|
}
|
||||||
|
|
||||||
foreach (var hotkeyModel in hotkeyModels)
|
foreach (var hotkeyModel in hotkeyModels)
|
||||||
{
|
{
|
||||||
var metadata = hotkeyModel.Metadata;
|
var metadata = hotkeyModel.Metadata;
|
||||||
|
|
@ -273,6 +287,13 @@ internal static class HotKeyMapper
|
||||||
{
|
{
|
||||||
window.InputBindings.Remove(existingBinding);
|
window.InputBindings.Remove(existingBinding);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Restore the command if it exists
|
||||||
|
if (_windowHotkeyEvents.TryGetValue(hotkey, out var command))
|
||||||
|
{
|
||||||
|
var keyBinding = new KeyBinding(command, keyGesture);
|
||||||
|
window.InputBindings.Add(keyBinding);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue