Skip to content

Commit f657494

Browse files
committed
Workaround for ActionContext compatibility
1 parent e6fb766 commit f657494

File tree

1 file changed

+93
-14
lines changed

1 file changed

+93
-14
lines changed

Flow.Launcher/Helper/HotKeyMapper.cs

Lines changed: 93 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,14 @@ internal static class HotKeyMapper
2727
private static Settings _settings;
2828
private static MainViewModel _mainViewModel;
2929

30-
// Registered hotkeys for ActionContext
31-
private static List<RegisteredHotkeyData> _actionContextRegisteredHotkeys;
32-
3330
#region Initialization
3431

3532
internal static void Initialize()
3633
{
3734
_mainViewModel = Ioc.Default.GetRequiredService<MainViewModel>();
3835
_settings = Ioc.Default.GetService<Settings>();
3936

37+
InitializeActionContextHotkeys();
4038
InitializeRegisteredHotkeys();
4139

4240
_settings.PropertyChanged += Settings_PropertyChanged;
@@ -46,14 +44,6 @@ internal static void Initialize()
4644

4745
private static void InitializeRegisteredHotkeys()
4846
{
49-
// Fixed hotkeys for ActionContext
50-
_actionContextRegisteredHotkeys = new List<RegisteredHotkeyData>
51-
{
52-
new(RegisteredHotkeyType.CtrlShiftEnter, HotkeyType.SearchWindow, "Ctrl+Shift+Enter", "HotkeyCtrlShiftEnterDesc", _mainViewModel.OpenResultCommand),
53-
new(RegisteredHotkeyType.CtrlEnter, HotkeyType.SearchWindow, "Ctrl+Enter", "OpenContainFolderHotkey", _mainViewModel.OpenResultCommand),
54-
new(RegisteredHotkeyType.AltEnter, HotkeyType.SearchWindow, "Alt+Enter", "HotkeyOpenResult", _mainViewModel.OpenResultCommand),
55-
};
56-
5747
// Fixed hotkeys & Editable hotkeys
5848
var list = new List<RegisteredHotkeyData>
5949
{
@@ -348,7 +338,7 @@ private static RegisteredHotkeyData GetRegisteredHotkeyData(HotkeyModel hotkey,
348338
PluginManager.ChangePluginHotkey(metadata, pluginHotkey, HotkeyModel.Empty);
349339
}
350340
} : null;
351-
return new(RegisteredHotkeyType.PluginWindowHotkey, HotkeyType.SearchWindow, hotkey, "pluginHotkey", WindowPluginHotkeyCommand, new WindowPluginHotkeyPair(windowHotkeys), removeHotkeysAction);
341+
return new(RegisteredHotkeyType.PluginWindowHotkey, HotkeyType.SearchWindow, hotkey, "pluginHotkey", WindowPluginHotkeyCommand, new WindowPluginHotkeyPair(hotkey, windowHotkeys), removeHotkeysAction);
352342
}
353343

354344
private static RegisteredHotkeyData SearchRegisteredHotkeyData(PluginMetadata metadata, GlobalPluginHotkey globalPluginHotkey)
@@ -475,7 +465,11 @@ kb.Gesture is KeyGesture keyGesture1 &&
475465
keyGesture.Modifiers == keyGesture1.Modifiers);
476466
if (existingBinding != null)
477467
{
478-
throw new InvalidOperationException($"Windows key {hotkey} already exists");
468+
// If the hotkey is not a hotkey for ActionContext events, throw an exception to avoid duplicates
469+
if (!IsActionContextEvent(window, existingBinding, hotkey))
470+
{
471+
throw new InvalidOperationException($"Windows key {hotkey} already exists");
472+
}
479473
}
480474

481475
// Add the new hotkey binding
@@ -558,6 +552,9 @@ kb.Gesture is KeyGesture keyGesture1 &&
558552
{
559553
window.InputBindings.Remove(existingBinding);
560554
}
555+
556+
// Restore the key binding for ActionContext events
557+
RestoreActionContextEvent(hotkey, keyGesture);
561558
}
562559
}
563560
catch (Exception e)
@@ -672,7 +669,13 @@ private static void WindowPluginHotkey(WindowPluginHotkeyPair pair)
672669
// TODO: Remove return to skip other commands
673670
if (pluginHotkey.Action.Invoke(selectedResult))
674671
App.API.HideMainWindow();
672+
673+
// Return after invoking the first matching hotkey action so that we will not invoke action context event
674+
return;
675675
}
676+
677+
// When no plugin hotkey action is invoked, invoke the action context event
678+
InvokeActionContextEvent(pair.Hotkey);
676679
}
677680
}
678681

@@ -701,6 +704,78 @@ internal static bool CheckAvailability(HotkeyModel currentHotkey)
701704

702705
#endregion
703706

707+
#region Action Context Hotkey (Obsolete)
708+
709+
[Obsolete("ActionContext support is deprecated and will be removed in a future release. Please use IPluginHotkey instead.")]
710+
private static List<RegisteredHotkeyData> _actionContextRegisteredHotkeys;
711+
712+
[Obsolete("ActionContext support is deprecated and will be removed in a future release. Please use IPluginHotkey instead.")]
713+
private static readonly Dictionary<HotkeyModel, (ICommand Command, object Parameter)> _actionContextHotkeyEvents = new();
714+
715+
[Obsolete("ActionContext support is deprecated and will be removed in a future release. Please use IPluginHotkey instead.")]
716+
private static void InitializeActionContextHotkeys()
717+
{
718+
// Fixed hotkeys for ActionContext
719+
_actionContextRegisteredHotkeys = new List<RegisteredHotkeyData>
720+
{
721+
new(RegisteredHotkeyType.CtrlShiftEnter, HotkeyType.SearchWindow, "Ctrl+Shift+Enter", "HotkeyCtrlShiftEnterDesc", _mainViewModel.OpenResultCommand),
722+
new(RegisteredHotkeyType.CtrlEnter, HotkeyType.SearchWindow, "Ctrl+Enter", "OpenContainFolderHotkey", _mainViewModel.OpenResultCommand),
723+
new(RegisteredHotkeyType.AltEnter, HotkeyType.SearchWindow, "Alt+Enter", "HotkeyOpenResult", _mainViewModel.OpenResultCommand),
724+
};
725+
726+
// Register ActionContext hotkeys and they will be cached and restored in _actionContextHotkeyEvents
727+
foreach (var hotkey in _actionContextRegisteredHotkeys)
728+
{
729+
_actionContextHotkeyEvents[hotkey.Hotkey] = (hotkey.Command, hotkey.CommandParameter);
730+
SetWindowHotkey(hotkey);
731+
}
732+
}
733+
734+
[Obsolete("ActionContext support is deprecated and will be removed in a future release. Please use IPluginHotkey instead.")]
735+
private static bool IsActionContextEvent(MainWindow window, KeyBinding existingBinding, HotkeyModel hotkey)
736+
{
737+
// Check if this hotkey is a hotkey for ActionContext events
738+
if (!_actionContextHotkeyEvents.ContainsKey(hotkey) &&
739+
_actionContextHotkeyEvents[hotkey].Command == existingBinding.Command ||
740+
_actionContextHotkeyEvents[hotkey].Parameter == existingBinding.CommandParameter)
741+
{
742+
// If the hotkey is not for ActionContext events, return false
743+
return true;
744+
}
745+
746+
return false;
747+
}
748+
749+
[Obsolete("ActionContext support is deprecated and will be removed in a future release. Please use IPluginHotkey instead.")]
750+
private static void RestoreActionContextEvent(HotkeyModel hotkey, KeyGesture keyGesture)
751+
{
752+
// Restore the ActionContext event by adding the key binding back
753+
if (_actionContextHotkeyEvents.TryGetValue(hotkey, out var actionContextItem))
754+
{
755+
if (Application.Current?.MainWindow is MainWindow window)
756+
{
757+
var keyBinding = new KeyBinding
758+
{
759+
Gesture = keyGesture,
760+
Command = actionContextItem.Command,
761+
CommandParameter = actionContextItem.Parameter
762+
};
763+
window.InputBindings.Add(keyBinding);
764+
}
765+
}
766+
}
767+
768+
[Obsolete("ActionContext support is deprecated and will be removed in a future release. Please use IPluginHotkey instead.")]
769+
private static void InvokeActionContextEvent(HotkeyModel hotkey)
770+
{
771+
if (_actionContextHotkeyEvents.TryGetValue(hotkey, out var actionContextItem))
772+
{
773+
actionContextItem.Command.Execute(actionContextItem.Parameter);
774+
}
775+
}
776+
777+
#endregion
778+
704779
#region Private Classes
705780

706781
private class GlobalPluginHotkeyPair
@@ -718,10 +793,14 @@ public GlobalPluginHotkeyPair(PluginMetadata metadata, GlobalPluginHotkey global
718793

719794
private class WindowPluginHotkeyPair
720795
{
796+
[Obsolete("ActionContext support is deprecated and will be removed in a future release. Please use IPluginHotkey instead.")]
797+
public HotkeyModel Hotkey { get; }
798+
721799
public List<(PluginMetadata Metadata, SearchWindowPluginHotkey PluginHotkey)> HotkeyModels { get; }
722800

723-
public WindowPluginHotkeyPair(List<(PluginMetadata Metadata, SearchWindowPluginHotkey PluginHotkey)> hotkeys)
801+
public WindowPluginHotkeyPair(HotkeyModel hotkey, List<(PluginMetadata Metadata, SearchWindowPluginHotkey PluginHotkey)> hotkeys)
724802
{
803+
Hotkey = hotkey;
725804
HotkeyModels = hotkeys;
726805
}
727806
}

0 commit comments

Comments
 (0)