mirror of
https://github.com/Flow-Launcher/Flow.Launcher.git
synced 2026-03-11 08:54:32 +00:00
move and centralise the hotkey management calls
This commit is contained in:
parent
b39d21c0e4
commit
48fb0dcc23
4 changed files with 120 additions and 127 deletions
|
|
@ -68,6 +68,9 @@ namespace Flow.Launcher
|
||||||
|
|
||||||
PluginManager.LoadPlugins(_settings.PluginSettings);
|
PluginManager.LoadPlugins(_settings.PluginSettings);
|
||||||
_mainVM = new MainViewModel(_settings);
|
_mainVM = new MainViewModel(_settings);
|
||||||
|
|
||||||
|
HotKeyMapper.Initialize(_mainVM);
|
||||||
|
|
||||||
API = new PublicAPIInstance(_settingsVM, _mainVM, _alphabet);
|
API = new PublicAPIInstance(_settingsVM, _mainVM, _alphabet);
|
||||||
|
|
||||||
Http.API = API;
|
Http.API = API;
|
||||||
|
|
|
||||||
110
Flow.Launcher/Helper/HotKeyMapper.cs
Normal file
110
Flow.Launcher/Helper/HotKeyMapper.cs
Normal file
|
|
@ -0,0 +1,110 @@
|
||||||
|
using Flow.Launcher.Infrastructure.Hotkey;
|
||||||
|
using Flow.Launcher.Infrastructure.UserSettings;
|
||||||
|
using System;
|
||||||
|
using NHotkey;
|
||||||
|
using NHotkey.Wpf;
|
||||||
|
using Flow.Launcher.Core.Resource;
|
||||||
|
using System.Windows;
|
||||||
|
using Flow.Launcher.ViewModel;
|
||||||
|
|
||||||
|
namespace Flow.Launcher.Helper
|
||||||
|
{
|
||||||
|
internal static class HotKeyMapper
|
||||||
|
{
|
||||||
|
private static Settings settings;
|
||||||
|
private static MainViewModel mainViewModel;
|
||||||
|
|
||||||
|
internal static void Initialize(MainViewModel mainVM)
|
||||||
|
{
|
||||||
|
mainViewModel = mainVM;
|
||||||
|
settings = mainViewModel._settings;
|
||||||
|
|
||||||
|
SetHotkey(settings.Hotkey, OnHotkey);
|
||||||
|
SetCustomPluginHotkey();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void SetHotkey(string hotkeyStr, EventHandler<HotkeyEventArgs> action)
|
||||||
|
{
|
||||||
|
var hotkey = new HotkeyModel(hotkeyStr);
|
||||||
|
SetHotkey(hotkey, action);
|
||||||
|
}
|
||||||
|
|
||||||
|
internal static void SetHotkey(HotkeyModel hotkey, EventHandler<HotkeyEventArgs> action)
|
||||||
|
{
|
||||||
|
string hotkeyStr = hotkey.ToString();
|
||||||
|
try
|
||||||
|
{
|
||||||
|
HotkeyManager.Current.AddOrReplace(hotkeyStr, hotkey.CharKey, hotkey.ModifierKeys, action);
|
||||||
|
}
|
||||||
|
catch (Exception)
|
||||||
|
{
|
||||||
|
string errorMsg =
|
||||||
|
string.Format(InternationalizationManager.Instance.GetTranslation("registerHotkeyFailed"),
|
||||||
|
hotkeyStr);
|
||||||
|
MessageBox.Show(errorMsg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
internal static void RemoveHotkey(string hotkeyStr)
|
||||||
|
{
|
||||||
|
if (!string.IsNullOrEmpty(hotkeyStr))
|
||||||
|
{
|
||||||
|
HotkeyManager.Current.Remove(hotkeyStr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
internal static void OnHotkey(object sender, HotkeyEventArgs e)
|
||||||
|
{
|
||||||
|
if (!ShouldIgnoreHotkeys())
|
||||||
|
{
|
||||||
|
if (settings.LastQueryMode == LastQueryMode.Empty)
|
||||||
|
{
|
||||||
|
mainViewModel.ChangeQueryText(string.Empty);
|
||||||
|
}
|
||||||
|
else if (settings.LastQueryMode == LastQueryMode.Preserved)
|
||||||
|
{
|
||||||
|
mainViewModel.LastQuerySelected = true;
|
||||||
|
}
|
||||||
|
else if (settings.LastQueryMode == LastQueryMode.Selected)
|
||||||
|
{
|
||||||
|
mainViewModel.LastQuerySelected = false;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw new ArgumentException($"wrong LastQueryMode: <{settings.LastQueryMode}>");
|
||||||
|
}
|
||||||
|
|
||||||
|
mainViewModel.ToggleFlowLauncher();
|
||||||
|
e.Handled = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Checks if Flow Launcher should ignore any hotkeys
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
private static bool ShouldIgnoreHotkeys()
|
||||||
|
{
|
||||||
|
//double if to omit calling win32 function
|
||||||
|
if (settings.IgnoreHotkeysOnFullscreen)
|
||||||
|
if (WindowsInteropHelper.IsWindowFullscreen())
|
||||||
|
return true;
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void SetCustomPluginHotkey()
|
||||||
|
{
|
||||||
|
if (settings.CustomPluginHotkeys == null) return;
|
||||||
|
foreach (CustomPluginHotkey hotkey in settings.CustomPluginHotkeys)
|
||||||
|
{
|
||||||
|
SetHotkey(hotkey.Hotkey, (s, e) =>
|
||||||
|
{
|
||||||
|
if (ShouldIgnoreHotkeys()) return;
|
||||||
|
mainViewModel.MainWindowVisibility = Visibility.Visible;
|
||||||
|
mainViewModel.ChangeQueryText(hotkey.ActionKeyword);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -5,16 +5,14 @@ using System.Windows.Input;
|
||||||
using System.Windows.Interop;
|
using System.Windows.Interop;
|
||||||
using System.Windows.Navigation;
|
using System.Windows.Navigation;
|
||||||
using Microsoft.Win32;
|
using Microsoft.Win32;
|
||||||
using NHotkey;
|
|
||||||
using NHotkey.Wpf;
|
|
||||||
using Flow.Launcher.Core.Plugin;
|
using Flow.Launcher.Core.Plugin;
|
||||||
using Flow.Launcher.Core.Resource;
|
using Flow.Launcher.Core.Resource;
|
||||||
using Flow.Launcher.Infrastructure;
|
using Flow.Launcher.Infrastructure;
|
||||||
using Flow.Launcher.Infrastructure.Hotkey;
|
|
||||||
using Flow.Launcher.Infrastructure.UserSettings;
|
using Flow.Launcher.Infrastructure.UserSettings;
|
||||||
using Flow.Launcher.Plugin;
|
using Flow.Launcher.Plugin;
|
||||||
using Flow.Launcher.Plugin.SharedCommands;
|
using Flow.Launcher.Plugin.SharedCommands;
|
||||||
using Flow.Launcher.ViewModel;
|
using Flow.Launcher.ViewModel;
|
||||||
|
using Flow.Launcher.Helper;
|
||||||
|
|
||||||
namespace Flow.Launcher
|
namespace Flow.Launcher
|
||||||
{
|
{
|
||||||
|
|
@ -127,45 +125,13 @@ namespace Flow.Launcher
|
||||||
{
|
{
|
||||||
if (HotkeyControl.CurrentHotkeyAvailable)
|
if (HotkeyControl.CurrentHotkeyAvailable)
|
||||||
{
|
{
|
||||||
SetHotkey(HotkeyControl.CurrentHotkey, (o, args) =>
|
|
||||||
{
|
HotKeyMapper.SetHotkey(HotkeyControl.CurrentHotkey, HotKeyMapper.OnHotkey);
|
||||||
if (!Application.Current.MainWindow.IsVisible)
|
HotKeyMapper.RemoveHotkey(settings.Hotkey);
|
||||||
{
|
|
||||||
Application.Current.MainWindow.Visibility = Visibility.Visible;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
Application.Current.MainWindow.Visibility = Visibility.Hidden;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
RemoveHotkey(settings.Hotkey);
|
|
||||||
settings.Hotkey = HotkeyControl.CurrentHotkey.ToString();
|
settings.Hotkey = HotkeyControl.CurrentHotkey.ToString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void SetHotkey(HotkeyModel hotkey, EventHandler<HotkeyEventArgs> action)
|
|
||||||
{
|
|
||||||
string hotkeyStr = hotkey.ToString();
|
|
||||||
try
|
|
||||||
{
|
|
||||||
HotkeyManager.Current.AddOrReplace(hotkeyStr, hotkey.CharKey, hotkey.ModifierKeys, action);
|
|
||||||
}
|
|
||||||
catch (Exception)
|
|
||||||
{
|
|
||||||
string errorMsg =
|
|
||||||
string.Format(InternationalizationManager.Instance.GetTranslation("registerHotkeyFailed"), hotkeyStr);
|
|
||||||
MessageBox.Show(errorMsg);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void RemoveHotkey(string hotkeyStr)
|
|
||||||
{
|
|
||||||
if (!string.IsNullOrEmpty(hotkeyStr))
|
|
||||||
{
|
|
||||||
HotkeyManager.Current.Remove(hotkeyStr);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void OnDeleteCustomHotkeyClick(object sender, RoutedEventArgs e)
|
private void OnDeleteCustomHotkeyClick(object sender, RoutedEventArgs e)
|
||||||
{
|
{
|
||||||
var item = viewModel.SelectedCustomPluginHotkey;
|
var item = viewModel.SelectedCustomPluginHotkey;
|
||||||
|
|
@ -183,7 +149,7 @@ namespace Flow.Launcher
|
||||||
MessageBoxButton.YesNo) == MessageBoxResult.Yes)
|
MessageBoxButton.YesNo) == MessageBoxResult.Yes)
|
||||||
{
|
{
|
||||||
settings.CustomPluginHotkeys.Remove(item);
|
settings.CustomPluginHotkeys.Remove(item);
|
||||||
RemoveHotkey(item.Hotkey);
|
HotKeyMapper.RemoveHotkey(item.Hotkey);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -39,7 +39,7 @@ namespace Flow.Launcher.ViewModel
|
||||||
private readonly FlowLauncherJsonStorage<History> _historyItemsStorage;
|
private readonly FlowLauncherJsonStorage<History> _historyItemsStorage;
|
||||||
private readonly FlowLauncherJsonStorage<UserSelectedRecord> _userSelectedRecordStorage;
|
private readonly FlowLauncherJsonStorage<UserSelectedRecord> _userSelectedRecordStorage;
|
||||||
private readonly FlowLauncherJsonStorage<TopMostRecord> _topMostRecordStorage;
|
private readonly FlowLauncherJsonStorage<TopMostRecord> _topMostRecordStorage;
|
||||||
private readonly Settings _settings;
|
internal readonly Settings _settings;
|
||||||
private readonly History _history;
|
private readonly History _history;
|
||||||
private readonly UserSelectedRecord _userSelectedRecord;
|
private readonly UserSelectedRecord _userSelectedRecord;
|
||||||
private readonly TopMostRecord _topMostRecord;
|
private readonly TopMostRecord _topMostRecord;
|
||||||
|
|
@ -80,8 +80,6 @@ namespace Flow.Launcher.ViewModel
|
||||||
RegisterViewUpdate();
|
RegisterViewUpdate();
|
||||||
RegisterResultsUpdatedEvent();
|
RegisterResultsUpdatedEvent();
|
||||||
|
|
||||||
SetHotkey(_settings.Hotkey, OnHotkey);
|
|
||||||
SetCustomPluginHotkey();
|
|
||||||
SetOpenResultModifiers();
|
SetOpenResultModifiers();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -659,96 +657,12 @@ namespace Flow.Launcher.ViewModel
|
||||||
|
|
||||||
#region Hotkey
|
#region Hotkey
|
||||||
|
|
||||||
private void SetHotkey(string hotkeyStr, EventHandler<HotkeyEventArgs> action)
|
|
||||||
{
|
|
||||||
var hotkey = new HotkeyModel(hotkeyStr);
|
|
||||||
SetHotkey(hotkey, action);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void SetHotkey(HotkeyModel hotkey, EventHandler<HotkeyEventArgs> action)
|
|
||||||
{
|
|
||||||
string hotkeyStr = hotkey.ToString();
|
|
||||||
try
|
|
||||||
{
|
|
||||||
HotkeyManager.Current.AddOrReplace(hotkeyStr, hotkey.CharKey, hotkey.ModifierKeys, action);
|
|
||||||
}
|
|
||||||
catch (Exception)
|
|
||||||
{
|
|
||||||
string errorMsg =
|
|
||||||
string.Format(InternationalizationManager.Instance.GetTranslation("registerHotkeyFailed"),
|
|
||||||
hotkeyStr);
|
|
||||||
MessageBox.Show(errorMsg);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void RemoveHotkey(string hotkeyStr)
|
|
||||||
{
|
|
||||||
if (!string.IsNullOrEmpty(hotkeyStr))
|
|
||||||
{
|
|
||||||
HotkeyManager.Current.Remove(hotkeyStr);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Checks if Flow Launcher should ignore any hotkeys
|
|
||||||
/// </summary>
|
|
||||||
/// <returns></returns>
|
|
||||||
private bool ShouldIgnoreHotkeys()
|
|
||||||
{
|
|
||||||
//double if to omit calling win32 function
|
|
||||||
if (_settings.IgnoreHotkeysOnFullscreen)
|
|
||||||
if (WindowsInteropHelper.IsWindowFullscreen())
|
|
||||||
return true;
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void SetCustomPluginHotkey()
|
|
||||||
{
|
|
||||||
if (_settings.CustomPluginHotkeys == null) return;
|
|
||||||
foreach (CustomPluginHotkey hotkey in _settings.CustomPluginHotkeys)
|
|
||||||
{
|
|
||||||
SetHotkey(hotkey.Hotkey, (s, e) =>
|
|
||||||
{
|
|
||||||
if (ShouldIgnoreHotkeys()) return;
|
|
||||||
MainWindowVisibility = Visibility.Visible;
|
|
||||||
ChangeQueryText(hotkey.ActionKeyword);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void SetOpenResultModifiers()
|
private void SetOpenResultModifiers()
|
||||||
{
|
{
|
||||||
OpenResultCommandModifiers = _settings.OpenResultModifiers ?? DefaultOpenResultModifiers;
|
OpenResultCommandModifiers = _settings.OpenResultModifiers ?? DefaultOpenResultModifiers;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnHotkey(object sender, HotkeyEventArgs e)
|
internal void ToggleFlowLauncher()
|
||||||
{
|
|
||||||
if (!ShouldIgnoreHotkeys())
|
|
||||||
{
|
|
||||||
if (_settings.LastQueryMode == LastQueryMode.Empty)
|
|
||||||
{
|
|
||||||
ChangeQueryText(string.Empty);
|
|
||||||
}
|
|
||||||
else if (_settings.LastQueryMode == LastQueryMode.Preserved)
|
|
||||||
{
|
|
||||||
LastQuerySelected = true;
|
|
||||||
}
|
|
||||||
else if (_settings.LastQueryMode == LastQueryMode.Selected)
|
|
||||||
{
|
|
||||||
LastQuerySelected = false;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
throw new ArgumentException($"wrong LastQueryMode: <{_settings.LastQueryMode}>");
|
|
||||||
}
|
|
||||||
|
|
||||||
ToggleFlowLauncher();
|
|
||||||
e.Handled = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void ToggleFlowLauncher()
|
|
||||||
{
|
{
|
||||||
if (MainWindowVisibility != Visibility.Visible)
|
if (MainWindowVisibility != Visibility.Visible)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue