From 944393c3846dcf366aee281ed86892b61da0b7fe Mon Sep 17 00:00:00 2001 From: Jack251970 <1160210343@qq.com> Date: Wed, 19 Mar 2025 18:22:43 +0800 Subject: [PATCH] Improve code quality --- Flow.Launcher/MainWindow.xaml.cs | 4 +- Flow.Launcher/ViewModel/MainViewModel.cs | 98 ++++++++++-------------- 2 files changed, 44 insertions(+), 58 deletions(-) diff --git a/Flow.Launcher/MainWindow.xaml.cs b/Flow.Launcher/MainWindow.xaml.cs index 1943c59a4..2ce3d1e95 100644 --- a/Flow.Launcher/MainWindow.xaml.cs +++ b/Flow.Launcher/MainWindow.xaml.cs @@ -388,13 +388,13 @@ namespace Flow.Launcher private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) { - if (Win32Helper.WM_ENTERSIZEMOVE((uint)msg)) + if (msg == Win32Helper.WM_ENTERSIZEMOVE) { _initialWidth = (int)Width; _initialHeight = (int)Height; handled = true; } - else if (Win32Helper.WM_EXITSIZEMOVE((uint)msg)) + else if (msg == Win32Helper.WM_EXITSIZEMOVE) { if (_initialHeight != (int)Height) { diff --git a/Flow.Launcher/ViewModel/MainViewModel.cs b/Flow.Launcher/ViewModel/MainViewModel.cs index e0b523960..46970a6a1 100644 --- a/Flow.Launcher/ViewModel/MainViewModel.cs +++ b/Flow.Launcher/ViewModel/MainViewModel.cs @@ -240,6 +240,19 @@ namespace Flow.Launcher.ViewModel } } + private async Task RegisterClockAndDateUpdateAsync() + { + var timer = new PeriodicTimer(TimeSpan.FromSeconds(1)); + // ReSharper disable once MethodSupportsCancellation + while (await timer.WaitForNextTickAsync().ConfigureAwait(false)) + { + if (Settings.UseClock) + ClockText = DateTime.Now.ToString(Settings.TimeFormat, CultureInfo.CurrentCulture); + if (Settings.UseDate) + DateText = DateTime.Now.ToString(Settings.DateFormat, CultureInfo.CurrentCulture); + } + } + [RelayCommand] private async Task ReloadPluginDataAsync() { @@ -284,7 +297,7 @@ namespace Flow.Launcher.ViewModel { if (_history.Items.Count > 0) { - ChangeQueryText(_history.Items[_history.Items.Count - lastHistoryIndex].Query.ToString()); + ChangeQueryText(_history.Items[^lastHistoryIndex].Query.ToString()); if (lastHistoryIndex < _history.Items.Count) { lastHistoryIndex++; @@ -297,7 +310,7 @@ namespace Flow.Launcher.ViewModel { if (_history.Items.Count > 0) { - ChangeQueryText(_history.Items[_history.Items.Count - lastHistoryIndex].Query.ToString()); + ChangeQueryText(_history.Items[^lastHistoryIndex].Query.ToString()); if (lastHistoryIndex > 1) { lastHistoryIndex--; @@ -348,12 +361,12 @@ namespace Flow.Launcher.ViewModel } else if (!string.IsNullOrEmpty(SelectedResults.SelectedItem?.QuerySuggestionText)) { - var defaultSuggestion = SelectedResults.SelectedItem.QuerySuggestionText; - // check if result.actionkeywordassigned is empty - if (!string.IsNullOrEmpty(result.ActionKeywordAssigned)) - { - autoCompleteText = $"{result.ActionKeywordAssigned} {defaultSuggestion}"; - } + //var defaultSuggestion = SelectedResults.SelectedItem.QuerySuggestionText; + //// check if result.actionkeywordassigned is empty + //if (!string.IsNullOrEmpty(result.ActionKeywordAssigned)) + //{ + // autoCompleteText = $"{result.ActionKeywordAssigned} {defaultSuggestion}"; + //} autoCompleteText = SelectedResults.SelectedItem.QuerySuggestionText; } @@ -532,19 +545,6 @@ namespace Flow.Launcher.ViewModel public string ClockText { get; private set; } public string DateText { get; private set; } - private async Task RegisterClockAndDateUpdateAsync() - { - var timer = new PeriodicTimer(TimeSpan.FromSeconds(1)); - // ReSharper disable once MethodSupportsCancellation - while (await timer.WaitForNextTickAsync().ConfigureAwait(false)) - { - if (Settings.UseClock) - ClockText = DateTime.Now.ToString(Settings.TimeFormat, CultureInfo.CurrentCulture); - if (Settings.UseDate) - DateText = DateTime.Now.ToString(Settings.DateFormat, CultureInfo.CurrentCulture); - } - } - public ResultsViewModel Results { get; private set; } public ResultsViewModel ContextMenu { get; private set; } @@ -751,7 +751,7 @@ namespace Flow.Launcher.ViewModel public string OpenResultCommandModifiers => Settings.OpenResultModifiers; - public string VerifyOrSetDefaultHotkey(string hotkey, string defaultHotkey) + private static string VerifyOrSetDefaultHotkey(string hotkey, string defaultHotkey) { try { @@ -780,8 +780,6 @@ namespace Flow.Launcher.ViewModel public string CycleHistoryUpHotkey => VerifyOrSetDefaultHotkey(Settings.CycleHistoryUpHotkey, "Alt+Up"); public string CycleHistoryDownHotkey => VerifyOrSetDefaultHotkey(Settings.CycleHistoryDownHotkey, "Alt+Down"); - public string Image => Constant.QueryTextBoxIconImagePath; - public bool StartWithEnglishMode => Settings.AlwaysStartEn; #endregion @@ -866,18 +864,6 @@ namespace Flow.Launcher.ViewModel } } - private void ToggleInternalPreview() - { - if (!InternalPreviewVisible) - { - ShowInternalPreview(); - } - else - { - HideInternalPreview(); - } - } - private void OpenExternalPreview(string path, bool sendFailToast = true) { _ = PluginManager.OpenExternalPreviewAsync(path, sendFailToast).ConfigureAwait(false); @@ -1368,28 +1354,28 @@ namespace Flow.Launcher.ViewModel } public void Show() -{ - Application.Current.Dispatcher.Invoke(() => - { - if (Application.Current.MainWindow is MainWindow mainWindow) { - // 📌 Remove DWM Cloak (Make the window visible normally) - Win32Helper.DWMSetCloakForWindow(mainWindow, false); + Application.Current.Dispatcher.Invoke(() => + { + if (Application.Current.MainWindow is MainWindow mainWindow) + { + // 📌 Remove DWM Cloak (Make the window visible normally) + Win32Helper.DWMSetCloakForWindow(mainWindow, false); - // 📌 Restore UI elements - mainWindow.ClockPanel.Visibility = Visibility.Visible; - //mainWindow.SearchIcon.Visibility = Visibility.Visible; - SearchIconVisibility = Visibility.Visible; + // 📌 Restore UI elements + mainWindow.ClockPanel.Visibility = Visibility.Visible; + //mainWindow.SearchIcon.Visibility = Visibility.Visible; + SearchIconVisibility = Visibility.Visible; + } + + // Update WPF properties + MainWindowVisibility = Visibility.Visible; + MainWindowOpacity = 1; + MainWindowVisibilityStatus = true; + VisibilityChanged?.Invoke(this, new VisibilityChangedEventArgs { IsVisible = true }); + }); } - // Update WPF properties - MainWindowVisibility = Visibility.Visible; - MainWindowOpacity = 1; - MainWindowVisibilityStatus = true; - VisibilityChanged?.Invoke(this, new VisibilityChangedEventArgs { IsVisible = true }); - }); -} - #pragma warning disable VSTHRD100 // Avoid async void methods public async void Hide() @@ -1456,7 +1442,8 @@ namespace Flow.Launcher.ViewModel Win32Helper.DWMSetCloakForWindow(mainWindow, true); } - await Task.Delay(50); + await Task.Delay(50); + // Update WPF properties //MainWindowOpacity = 0; MainWindowVisibilityStatus = false; @@ -1464,7 +1451,6 @@ namespace Flow.Launcher.ViewModel VisibilityChanged?.Invoke(this, new VisibilityChangedEventArgs { IsVisible = false }); } - #pragma warning restore VSTHRD100 // Avoid async void methods ///