diff --git a/Flow.Launcher/App.xaml.cs b/Flow.Launcher/App.xaml.cs index 7b808dd54..b3e9052fc 100644 --- a/Flow.Launcher/App.xaml.cs +++ b/Flow.Launcher/App.xaml.cs @@ -68,6 +68,9 @@ namespace Flow.Launcher PluginManager.LoadPlugins(_settings.PluginSettings); _mainVM = new MainViewModel(_settings); + + HotKeyMapper.Initialize(_mainVM); + API = new PublicAPIInstance(_settingsVM, _mainVM, _alphabet); Http.API = API; diff --git a/Flow.Launcher/Helper/HotKeyMapper.cs b/Flow.Launcher/Helper/HotKeyMapper.cs new file mode 100644 index 000000000..b32b7edc4 --- /dev/null +++ b/Flow.Launcher/Helper/HotKeyMapper.cs @@ -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 action) + { + var hotkey = new HotkeyModel(hotkeyStr); + SetHotkey(hotkey, action); + } + + internal static void SetHotkey(HotkeyModel hotkey, EventHandler 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; + } + } + + /// + /// Checks if Flow Launcher should ignore any hotkeys + /// + /// + 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); + }); + } + } + } +} diff --git a/Flow.Launcher/SettingWindow.xaml.cs b/Flow.Launcher/SettingWindow.xaml.cs index 2823e4ddd..aa341f431 100644 --- a/Flow.Launcher/SettingWindow.xaml.cs +++ b/Flow.Launcher/SettingWindow.xaml.cs @@ -5,16 +5,14 @@ using System.Windows.Input; using System.Windows.Interop; using System.Windows.Navigation; using Microsoft.Win32; -using NHotkey; -using NHotkey.Wpf; using Flow.Launcher.Core.Plugin; using Flow.Launcher.Core.Resource; using Flow.Launcher.Infrastructure; -using Flow.Launcher.Infrastructure.Hotkey; using Flow.Launcher.Infrastructure.UserSettings; using Flow.Launcher.Plugin; using Flow.Launcher.Plugin.SharedCommands; using Flow.Launcher.ViewModel; +using Flow.Launcher.Helper; namespace Flow.Launcher { @@ -127,45 +125,13 @@ namespace Flow.Launcher { if (HotkeyControl.CurrentHotkeyAvailable) { - SetHotkey(HotkeyControl.CurrentHotkey, (o, args) => - { - if (!Application.Current.MainWindow.IsVisible) - { - Application.Current.MainWindow.Visibility = Visibility.Visible; - } - else - { - Application.Current.MainWindow.Visibility = Visibility.Hidden; - } - }); - RemoveHotkey(settings.Hotkey); + + HotKeyMapper.SetHotkey(HotkeyControl.CurrentHotkey, HotKeyMapper.OnHotkey); + HotKeyMapper.RemoveHotkey(settings.Hotkey); settings.Hotkey = HotkeyControl.CurrentHotkey.ToString(); } } - void SetHotkey(HotkeyModel hotkey, EventHandler 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) { var item = viewModel.SelectedCustomPluginHotkey; @@ -183,7 +149,7 @@ namespace Flow.Launcher MessageBoxButton.YesNo) == MessageBoxResult.Yes) { settings.CustomPluginHotkeys.Remove(item); - RemoveHotkey(item.Hotkey); + HotKeyMapper.RemoveHotkey(item.Hotkey); } } diff --git a/Flow.Launcher/ViewModel/MainViewModel.cs b/Flow.Launcher/ViewModel/MainViewModel.cs index 2fd00224c..7c71f3b25 100644 --- a/Flow.Launcher/ViewModel/MainViewModel.cs +++ b/Flow.Launcher/ViewModel/MainViewModel.cs @@ -39,7 +39,7 @@ namespace Flow.Launcher.ViewModel private readonly FlowLauncherJsonStorage _historyItemsStorage; private readonly FlowLauncherJsonStorage _userSelectedRecordStorage; private readonly FlowLauncherJsonStorage _topMostRecordStorage; - private readonly Settings _settings; + internal readonly Settings _settings; private readonly History _history; private readonly UserSelectedRecord _userSelectedRecord; private readonly TopMostRecord _topMostRecord; @@ -80,8 +80,6 @@ namespace Flow.Launcher.ViewModel RegisterViewUpdate(); RegisterResultsUpdatedEvent(); - SetHotkey(_settings.Hotkey, OnHotkey); - SetCustomPluginHotkey(); SetOpenResultModifiers(); } @@ -659,96 +657,12 @@ namespace Flow.Launcher.ViewModel #region Hotkey - private void SetHotkey(string hotkeyStr, EventHandler action) - { - var hotkey = new HotkeyModel(hotkeyStr); - SetHotkey(hotkey, action); - } - - private void SetHotkey(HotkeyModel hotkey, EventHandler 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); - } - } - - /// - /// Checks if Flow Launcher should ignore any hotkeys - /// - /// - 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() { OpenResultCommandModifiers = _settings.OpenResultModifiers ?? DefaultOpenResultModifiers; } - private void OnHotkey(object sender, HotkeyEventArgs e) - { - 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() + internal void ToggleFlowLauncher() { if (MainWindowVisibility != Visibility.Visible) {