diff --git a/Flow.Launcher/Helper/HotKeyMapper.cs b/Flow.Launcher/Helper/HotKeyMapper.cs index d5fcabace05..e10f4d86ba3 100644 --- a/Flow.Launcher/Helper/HotKeyMapper.cs +++ b/Flow.Launcher/Helper/HotKeyMapper.cs @@ -6,6 +6,8 @@ using Flow.Launcher.Core.Resource; using System.Windows; using Flow.Launcher.ViewModel; +using System.Threading.Tasks; +using System.Threading; namespace Flow.Launcher.Helper { @@ -19,10 +21,15 @@ internal static void Initialize(MainViewModel mainVM) mainViewModel = mainVM; settings = mainViewModel._settings; - SetHotkey(settings.Hotkey, OnHotkey); + SetHotkey(settings.Hotkey, OnToggleHotkey); LoadCustomPluginHotkey(); } + internal static void OnToggleHotkey(object sender, HotkeyEventArgs args) + { + mainViewModel.ToggleFlowLauncher(); + } + private static void SetHotkey(string hotkeyStr, EventHandler action) { var hotkey = new HotkeyModel(hotkeyStr); @@ -53,44 +60,6 @@ internal static void RemoveHotkey(string 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) @@ -106,7 +75,7 @@ internal static void SetCustomQueryHotkey(CustomPluginHotkey hotkey) { SetHotkey(hotkey.Hotkey, (s, e) => { - if (ShouldIgnoreHotkeys()) + if (mainViewModel.ShouldIgnoreHotkeys()) return; mainViewModel.MainWindowVisibility = Visibility.Visible; diff --git a/Flow.Launcher/MainWindow.xaml.cs b/Flow.Launcher/MainWindow.xaml.cs index e84a3148e2d..057c3f07d09 100644 --- a/Flow.Launcher/MainWindow.xaml.cs +++ b/Flow.Launcher/MainWindow.xaml.cs @@ -262,7 +262,7 @@ private void OnDeactivated(object sender, EventArgs e) { if (_settings.HideWhenDeactive) { - Hide(); + _viewModel.Hide(); } } diff --git a/Flow.Launcher/SettingWindow.xaml.cs b/Flow.Launcher/SettingWindow.xaml.cs index aa341f4316a..5e8e194d4da 100644 --- a/Flow.Launcher/SettingWindow.xaml.cs +++ b/Flow.Launcher/SettingWindow.xaml.cs @@ -23,6 +23,7 @@ public partial class SettingWindow public readonly IPublicAPI API; private Settings settings; private SettingWindowViewModel viewModel; + private static MainViewModel mainViewModel; public SettingWindow(IPublicAPI api, SettingWindowViewModel viewModel) { @@ -126,7 +127,7 @@ void OnHotkeyChanged(object sender, EventArgs e) if (HotkeyControl.CurrentHotkeyAvailable) { - HotKeyMapper.SetHotkey(HotkeyControl.CurrentHotkey, HotKeyMapper.OnHotkey); + HotKeyMapper.SetHotkey(HotkeyControl.CurrentHotkey, HotKeyMapper.OnToggleHotkey); HotKeyMapper.RemoveHotkey(settings.Hotkey); settings.Hotkey = HotkeyControl.CurrentHotkey.ToString(); } diff --git a/Flow.Launcher/ViewModel/MainViewModel.cs b/Flow.Launcher/ViewModel/MainViewModel.cs index 4212280bb3b..5e56f06cf7a 100644 --- a/Flow.Launcher/ViewModel/MainViewModel.cs +++ b/Flow.Launcher/ViewModel/MainViewModel.cs @@ -21,6 +21,8 @@ using System.Threading.Channels; using ISavable = Flow.Launcher.Plugin.ISavable; using System.Windows.Threading; +using NHotkey; + namespace Flow.Launcher.ViewModel { @@ -108,7 +110,9 @@ async Task updateAction() } Log.Error("MainViewModel", "Unexpected ResultViewUpdate ends"); - }; + } + + ; void continueAction(Task t) { @@ -145,6 +149,24 @@ private void RegisterResultsUpdatedEvent() } } + private void UpdateLastQUeryMode() + { + switch (_settings.LastQueryMode) + { + case LastQueryMode.Empty: + ChangeQueryText(string.Empty); + break; + case LastQueryMode.Preserved: + LastQuerySelected = true; + break; + case LastQueryMode.Selected: + LastQuerySelected = false; + break; + default: + throw new ArgumentException($"wrong LastQueryMode: <{_settings.LastQueryMode}>"); + + } + } private void InitializeKeyCommands() { @@ -156,7 +178,18 @@ private void InitializeKeyCommands() } else { - MainWindowVisibility = Visibility.Collapsed; + Hide(); + } + }); + + ClearQueryCommand = new RelayCommand(_ => + { + if (!string.IsNullOrEmpty(QueryText)) + { + ChangeQueryText(string.Empty); + + // Push Event to UI SystemQuery has changed + //OnPropertyChanged(nameof(SystemQueryText)); } }); @@ -194,7 +227,7 @@ private void InitializeKeyCommands() if (hideWindow) { - MainWindowVisibility = Visibility.Collapsed; + Hide(); } if (SelectedIsFromQueryResults()) @@ -244,7 +277,7 @@ private void InitializeKeyCommands() Owner = Application.Current.MainWindow }; - MainWindowVisibility = Visibility.Collapsed; + Hide(); PluginManager .ReloadData() @@ -335,7 +368,6 @@ private ResultsViewModel SelectedResults } public Visibility ProgressBarVisibility { get; set; } - public Visibility MainWindowVisibility { get; set; } public ICommand EscCommand { get; set; } @@ -349,6 +381,7 @@ private ResultsViewModel SelectedResults public ICommand LoadHistoryCommand { get; set; } public ICommand OpenResultCommand { get; set; } public ICommand ReloadPluginDataCommand { get; set; } + public ICommand ClearQueryCommand { get; private set; } public string OpenResultCommandModifiers { get; private set; } @@ -660,7 +693,7 @@ private void SetOpenResultModifiers() OpenResultCommandModifiers = _settings.OpenResultModifiers ?? DefaultOpenResultModifiers; } - internal void ToggleFlowLauncher() + public async void ToggleFlowLauncher() { if (MainWindowVisibility != Visibility.Visible) { @@ -668,12 +701,48 @@ internal void ToggleFlowLauncher() } else { + switch (_settings.LastQueryMode) + { + case LastQueryMode.Empty: + ChangeQueryText(string.Empty); + Application.Current.MainWindow.Opacity = 0; // Trick for no delay + await Task.Delay(100); + Application.Current.MainWindow.Opacity = 1; + break; + case LastQueryMode.Preserved: + LastQuerySelected = true; + break; + case LastQueryMode.Selected: + LastQuerySelected = false; + break; + default: + throw new ArgumentException($"wrong LastQueryMode: <{_settings.LastQueryMode}>"); + } MainWindowVisibility = Visibility.Collapsed; } } + public void Hide() + { + if (MainWindowVisibility != Visibility.Collapsed) + { + ToggleFlowLauncher(); + } + } + #endregion + + /// + /// Checks if Flow Launcher should ignore any hotkeys + /// + public bool ShouldIgnoreHotkeys() + { + return _settings.IgnoreHotkeysOnFullscreen && WindowsInteropHelper.IsWindowFullscreen(); + } + + + #region Public Methods public void Save()