diff --git a/Flow.Launcher/App.xaml.cs b/Flow.Launcher/App.xaml.cs index 7b808dd5499..b3e9052fc54 100644 --- a/Flow.Launcher/App.xaml.cs +++ b/Flow.Launcher/App.xaml.cs @@ -68,6 +68,9 @@ await Stopwatch.NormalAsync("|App.OnStartup|Startup cost", async () => 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/CustomQueryHotkeySetting.xaml.cs b/Flow.Launcher/CustomQueryHotkeySetting.xaml.cs index 813c5e25474..b18bbcfadc1 100644 --- a/Flow.Launcher/CustomQueryHotkeySetting.xaml.cs +++ b/Flow.Launcher/CustomQueryHotkeySetting.xaml.cs @@ -1,8 +1,7 @@ using Flow.Launcher.Core.Resource; +using Flow.Launcher.Helper; using Flow.Launcher.Infrastructure.Hotkey; using Flow.Launcher.Infrastructure.UserSettings; -using NHotkey; -using NHotkey.Wpf; using System; using System.Collections.ObjectModel; using System.Linq; @@ -52,11 +51,7 @@ private void btnAdd_OnClick(object sender, RoutedEventArgs e) }; _settings.CustomPluginHotkeys.Add(pluginHotkey); - SetHotkey(ctlHotkey.CurrentHotkey, delegate - { - App.API.ChangeQuery(pluginHotkey.ActionKeyword); - ShowMainWindow(); - }); + HotKeyMapper.SetCustomQueryHotkey(pluginHotkey); } else { @@ -69,22 +64,11 @@ private void btnAdd_OnClick(object sender, RoutedEventArgs e) updateCustomHotkey.ActionKeyword = tbAction.Text; updateCustomHotkey.Hotkey = ctlHotkey.CurrentHotkey.ToString(); //remove origin hotkey - RemoveHotkey(oldHotkey); - SetHotkey(new HotkeyModel(updateCustomHotkey.Hotkey), delegate - { - App.API.ChangeQuery(updateCustomHotkey.ActionKeyword); - ShowMainWindow(); - }); + HotKeyMapper.RemoveHotkey(oldHotkey); + HotKeyMapper.SetCustomQueryHotkey(updateCustomHotkey); } Close(); - - static void ShowMainWindow() - { - Window mainWindow = Application.Current.MainWindow; - mainWindow.Visibility = Visibility.Visible; - mainWindow.Focus(); - } } public void UpdateItem(CustomPluginHotkey item) @@ -107,28 +91,8 @@ private void BtnTestActionKeyword_OnClick(object sender, RoutedEventArgs e) { App.API.ChangeQuery(tbAction.Text); Application.Current.MainWindow.Visibility = Visibility.Visible; - } - - private void RemoveHotkey(string hotkeyStr) - { - if (!string.IsNullOrEmpty(hotkeyStr)) - { - HotkeyManager.Current.Remove(hotkeyStr); - } - } + Application.Current.MainWindow.Focus(); - 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); - } } private void cmdEsc_OnPress(object sender, ExecutedRoutedEventArgs e) diff --git a/Flow.Launcher/Helper/HotKeyMapper.cs b/Flow.Launcher/Helper/HotKeyMapper.cs new file mode 100644 index 00000000000..d5fcabace05 --- /dev/null +++ b/Flow.Launcher/Helper/HotKeyMapper.cs @@ -0,0 +1,136 @@ +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); + LoadCustomPluginHotkey(); + } + + 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()) + { + UpdateLastQUeryMode(); + + mainViewModel.ToggleFlowLauncher(); + e.Handled = true; + } + } + + /// + /// Checks if Flow Launcher should ignore any hotkeys + /// + private static bool ShouldIgnoreHotkeys() + { + return settings.IgnoreHotkeysOnFullscreen && WindowsInteropHelper.IsWindowFullscreen(); + } + + private static void UpdateLastQUeryMode() + { + switch(settings.LastQueryMode) + { + case LastQueryMode.Empty: + mainViewModel.ChangeQueryText(string.Empty); + break; + case LastQueryMode.Preserved: + mainViewModel.LastQuerySelected = true; + break; + case LastQueryMode.Selected: + mainViewModel.LastQuerySelected = false; + break; + default: + throw new ArgumentException($"wrong LastQueryMode: <{settings.LastQueryMode}>"); + + } + } + + internal static void LoadCustomPluginHotkey() + { + if (settings.CustomPluginHotkeys == null) + return; + + foreach (CustomPluginHotkey hotkey in settings.CustomPluginHotkeys) + { + SetCustomQueryHotkey(hotkey); + } + } + + internal static void SetCustomQueryHotkey(CustomPluginHotkey hotkey) + { + SetHotkey(hotkey.Hotkey, (s, e) => + { + if (ShouldIgnoreHotkeys()) + return; + + mainViewModel.MainWindowVisibility = Visibility.Visible; + mainViewModel.ChangeQueryText(hotkey.ActionKeyword); + }); + } + + internal static bool CheckAvailability(HotkeyModel currentHotkey) + { + try + { + HotkeyManager.Current.AddOrReplace("HotkeyAvailabilityTest", currentHotkey.CharKey, currentHotkey.ModifierKeys, (sender, e) => { }); + + return true; + } + catch + { + } + finally + { + HotkeyManager.Current.Remove("HotkeyAvailabilityTest"); + } + + return false; + } + } +} diff --git a/Flow.Launcher/HotkeyControl.xaml b/Flow.Launcher/HotkeyControl.xaml index 0164d7dd72e..e732cbe97e9 100644 --- a/Flow.Launcher/HotkeyControl.xaml +++ b/Flow.Launcher/HotkeyControl.xaml @@ -10,7 +10,7 @@ - + diff --git a/Flow.Launcher/HotkeyControl.xaml.cs b/Flow.Launcher/HotkeyControl.xaml.cs index 5c7141d1c17..2b6e275df92 100644 --- a/Flow.Launcher/HotkeyControl.xaml.cs +++ b/Flow.Launcher/HotkeyControl.xaml.cs @@ -4,8 +4,8 @@ using System.Windows.Controls; using System.Windows.Input; using System.Windows.Media; -using NHotkey.Wpf; using Flow.Launcher.Core.Resource; +using Flow.Launcher.Helper; using Flow.Launcher.Infrastructure.Hotkey; using Flow.Launcher.Plugin; @@ -18,11 +18,7 @@ public partial class HotkeyControl : UserControl public event EventHandler HotkeyChanged; - protected virtual void OnHotkeyChanged() - { - EventHandler handler = HotkeyChanged; - if (handler != null) handler(this, EventArgs.Empty); - } + protected virtual void OnHotkeyChanged() => HotkeyChanged?.Invoke(this, EventArgs.Empty); public HotkeyControl() { @@ -90,24 +86,7 @@ public void SetHotkey(string keyStr, bool triggerValidate = true) SetHotkey(new HotkeyModel(keyStr), triggerValidate); } - private bool CheckHotkeyAvailability() - { - try - { - HotkeyManager.Current.AddOrReplace("HotkeyAvailabilityTest", CurrentHotkey.CharKey, CurrentHotkey.ModifierKeys, (sender, e) => { }); - - return true; - } - catch - { - } - finally - { - HotkeyManager.Current.Remove("HotkeyAvailabilityTest"); - } - - return false; - } + private bool CheckHotkeyAvailability() => HotKeyMapper.CheckAvailability(CurrentHotkey); public new bool IsFocused { diff --git a/Flow.Launcher/Languages/en.xaml b/Flow.Launcher/Languages/en.xaml index efb82b47813..90a920e3ed4 100644 --- a/Flow.Launcher/Languages/en.xaml +++ b/Flow.Launcher/Languages/en.xaml @@ -51,6 +51,7 @@ New action keyword: Current Priority: New Priority: + Priority: Plugin Directory Author Init time: @@ -73,6 +74,7 @@ Open Result Modifiers Show Hotkey Custom Query Hotkey + Query Delete Edit Add @@ -137,7 +139,7 @@ Update - Hotkey unavailable + Hotkey Unavailable Version diff --git a/Flow.Launcher/SettingWindow.xaml b/Flow.Launcher/SettingWindow.xaml index 9513cf41c29..478e114f9ff 100644 --- a/Flow.Launcher/SettingWindow.xaml +++ b/Flow.Launcher/SettingWindow.xaml @@ -180,7 +180,7 @@ Text="{Binding PluginPair.Metadata.Description}" Grid.Row="1" Opacity="0.5" Grid.Column="2" /> - + - + diff --git a/Flow.Launcher/SettingWindow.xaml.cs b/Flow.Launcher/SettingWindow.xaml.cs index 2823e4ddd8b..aa341f4316a 100644 --- a/Flow.Launcher/SettingWindow.xaml.cs +++ b/Flow.Launcher/SettingWindow.xaml.cs @@ -5,16 +5,14 @@ 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,42 +125,10 @@ void OnHotkeyChanged(object sender, EventArgs e) { 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); - 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); + HotKeyMapper.SetHotkey(HotkeyControl.CurrentHotkey, HotKeyMapper.OnHotkey); + HotKeyMapper.RemoveHotkey(settings.Hotkey); + settings.Hotkey = HotkeyControl.CurrentHotkey.ToString(); } } @@ -183,7 +149,7 @@ private void OnDeleteCustomHotkeyClick(object sender, RoutedEventArgs e) 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 2fd00224c0f..5b9706a32a5 100644 --- a/Flow.Launcher/ViewModel/MainViewModel.cs +++ b/Flow.Launcher/ViewModel/MainViewModel.cs @@ -6,8 +6,6 @@ using System.Threading.Tasks.Dataflow; using System.Windows; using System.Windows.Input; -using NHotkey; -using NHotkey.Wpf; using Flow.Launcher.Core.Plugin; using Flow.Launcher.Core.Resource; using Flow.Launcher.Helper; @@ -39,7 +37,7 @@ public class MainViewModel : BaseModel, ISavable 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 +78,6 @@ public MainViewModel(Settings settings) RegisterViewUpdate(); RegisterResultsUpdatedEvent(); - SetHotkey(_settings.Hotkey, OnHotkey); - SetCustomPluginHotkey(); SetOpenResultModifiers(); } @@ -569,7 +565,6 @@ async Task QueryTask(PluginPair plugin) } } - private void RemoveOldQueryResults(Query query) { if (_lastQuery.ActionKeyword != query.ActionKeyword) @@ -659,96 +654,12 @@ private bool HistorySelected() #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) { @@ -797,7 +708,6 @@ public void UpdateResultView(IEnumerable resultsForUpdates) } #endif - foreach (var metaResults in resultsForUpdates) { foreach (var result in metaResults.Results)