From 9a53015786139e5f26999eee111f7e371f497c51 Mon Sep 17 00:00:00 2001 From: Kevin Zhang Date: Tue, 19 Oct 2021 22:10:37 -0500 Subject: [PATCH] Refactor Code (Move all operation for mainviewmodel to ToggleFlowLauncher) --- Flow.Launcher/Helper/HotKeyMapper.cs | 7 ++- Flow.Launcher/SettingWindow.xaml.cs | 2 +- Flow.Launcher/ViewModel/MainViewModel.cs | 70 +++++++----------------- 3 files changed, 28 insertions(+), 51 deletions(-) diff --git a/Flow.Launcher/Helper/HotKeyMapper.cs b/Flow.Launcher/Helper/HotKeyMapper.cs index 1789b8548..e10f4d86b 100644 --- a/Flow.Launcher/Helper/HotKeyMapper.cs +++ b/Flow.Launcher/Helper/HotKeyMapper.cs @@ -21,10 +21,15 @@ namespace Flow.Launcher.Helper mainViewModel = mainVM; settings = mainViewModel._settings; - SetHotkey(settings.Hotkey, mainViewModel.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); diff --git a/Flow.Launcher/SettingWindow.xaml.cs b/Flow.Launcher/SettingWindow.xaml.cs index 26a0a840b..5e8e194d4 100644 --- a/Flow.Launcher/SettingWindow.xaml.cs +++ b/Flow.Launcher/SettingWindow.xaml.cs @@ -127,7 +127,7 @@ namespace Flow.Launcher if (HotkeyControl.CurrentHotkeyAvailable) { - HotKeyMapper.SetHotkey(HotkeyControl.CurrentHotkey, mainViewModel.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 f136ac279..5e56f06cf 100644 --- a/Flow.Launcher/ViewModel/MainViewModel.cs +++ b/Flow.Launcher/ViewModel/MainViewModel.cs @@ -52,8 +52,6 @@ namespace Flow.Launcher.ViewModel private ChannelWriter _resultsUpdateChannelWriter; private Task _resultsViewUpdateTask; - - #endregion #region Constructor @@ -112,7 +110,9 @@ namespace Flow.Launcher.ViewModel } Log.Error("MainViewModel", "Unexpected ResultViewUpdate ends"); - }; + } + + ; void continueAction(Task t) { @@ -693,7 +693,7 @@ namespace Flow.Launcher.ViewModel OpenResultCommandModifiers = _settings.OpenResultModifiers ?? DefaultOpenResultModifiers; } - public void ToggleFlowLauncher() + public async void ToggleFlowLauncher() { if (MainWindowVisibility != Visibility.Visible) { @@ -701,25 +701,24 @@ namespace Flow.Launcher.ViewModel } else { - if (_settings.LastQueryMode == LastQueryMode.Empty) + switch (_settings.LastQueryMode) { - Application.Current.MainWindow.Opacity = 0; // Trick for no delay - ClearQueryCommand.Execute(null); - Task.Run(() => - { - Thread.Sleep(100); - Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() => - { - MainWindowVisibility = Visibility.Collapsed; - Application.Current.MainWindow.Opacity = 1; - })); - }); - } - else - { - - MainWindowVisibility = Visibility.Collapsed; + 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; } } @@ -731,40 +730,13 @@ namespace Flow.Launcher.ViewModel } } - #endregion - public 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(); - } - } - /// /// Checks if Flow Launcher should ignore any hotkeys /// - public bool ShouldIgnoreHotkeys() + public bool ShouldIgnoreHotkeys() { return _settings.IgnoreHotkeysOnFullscreen && WindowsInteropHelper.IsWindowFullscreen(); }