diff --git a/Flow.Launcher/ViewModel/MainViewModel.cs b/Flow.Launcher/ViewModel/MainViewModel.cs index 72c15faef..c0465b074 100644 --- a/Flow.Launcher/ViewModel/MainViewModel.cs +++ b/Flow.Launcher/ViewModel/MainViewModel.cs @@ -1,15 +1,14 @@ ο»Ώusing System; using System.Collections.Generic; using System.ComponentModel; -using System.Diagnostics; using System.Globalization; -using System.Windows.Input; using System.Linq; using System.Text; using System.Threading; using System.Threading.Channels; using System.Threading.Tasks; using System.Windows; +using System.Windows.Input; using System.Windows.Media; using System.Windows.Threading; using CommunityToolkit.Mvvm.DependencyInjection; @@ -631,7 +630,15 @@ namespace Flow.Launcher.ViewModel /// Force query even when Query Text doesn't change public void ChangeQueryText(string queryText, bool isReQuery = false) { - Application.Current.Dispatcher.Invoke(() => + _ = ChangeQueryTextAsync(queryText, isReQuery); + } + + /// + /// Async version of + /// + private async Task ChangeQueryTextAsync(string queryText, bool isReQuery = false) + { + await Application.Current.Dispatcher.InvokeAsync(async () => { BackToQueryResults(); @@ -645,7 +652,7 @@ namespace Flow.Launcher.ViewModel } else if (isReQuery) { - Query(isReQuery: true); + await QueryAsync(isReQuery: true); } QueryTextCursorMovedToEnd = true; @@ -1017,10 +1024,15 @@ namespace Flow.Launcher.ViewModel #region Query private void Query(bool isReQuery = false) + { + _ = QueryAsync(isReQuery); + } + + private async Task QueryAsync(bool isReQuery = false) { if (QueryResultsSelected()) { - _ = QueryResultsAsync(isReQuery); + await QueryResultsAsync(isReQuery); } else if (ContextMenuSelected()) { @@ -1054,10 +1066,10 @@ namespace Flow.Launcher.ViewModel ( r => { - var match = StringMatcher.FuzzySearch(query, r.Title); + var match = App.API.FuzzySearch(query, r.Title); if (!match.IsSearchPrecisionScoreMet()) { - match = StringMatcher.FuzzySearch(query, r.SubTitle); + match = App.API.FuzzySearch(query, r.SubTitle); } if (!match.IsSearchPrecisionScoreMet()) return false; @@ -1099,7 +1111,7 @@ namespace Flow.Launcher.ViewModel Action = _ => { SelectedResults = Results; - ChangeQueryText(h.Query); + App.API.ChangeQuery(h.Query); return false; } }; @@ -1110,8 +1122,8 @@ namespace Flow.Launcher.ViewModel { var filtered = results.Where ( - r => StringMatcher.FuzzySearch(query, r.Title).IsSearchPrecisionScoreMet() || - StringMatcher.FuzzySearch(query, r.SubTitle).IsSearchPrecisionScoreMet() + r => App.API.FuzzySearch(query, r.Title).IsSearchPrecisionScoreMet() || + App.API.FuzzySearch(query, r.SubTitle).IsSearchPrecisionScoreMet() ).ToList(); History.AddResults(filtered, id); } @@ -1430,28 +1442,23 @@ namespace Flow.Launcher.ViewModel #region Public Methods - public void Show() +#pragma warning disable VSTHRD100 // Avoid async void methods + + public async void Show() { - Application.Current.Dispatcher.Invoke(() => + await Application.Current.Dispatcher.InvokeAsync(() => { if (Application.Current.MainWindow is MainWindow mainWindow) { // πŸ“Œ Remove DWM Cloak (Make the window visible normally) Win32Helper.DWMSetCloakForWindow(mainWindow, false); - - //Clock and SearchIcon hide when show situation - if(Settings.UseAnimation) - { - mainWindow.ClockPanel.Opacity = 0; - mainWindow.SearchIcon.Opacity = 0; - } - else - { - mainWindow.ClockPanel.Opacity = 1; - mainWindow.SearchIcon.Opacity = 1; - } - if (mainWindow.QueryTextBox.Text.Length != 0) + // Clock and SearchIcon hide when show situation + var opacity = Settings.UseAnimation ? 0.0 : 1.0; + mainWindow.ClockPanel.Opacity = opacity; + mainWindow.SearchIcon.Opacity = opacity; + + if (QueryText.Length != 0) { mainWindow.ClockPanel.Visibility = Visibility.Collapsed; } @@ -1459,6 +1466,7 @@ namespace Flow.Launcher.ViewModel { mainWindow.ClockPanel.Visibility = Visibility.Visible; } + if (PluginIconSource != null) { mainWindow.SearchIcon.Opacity = 0; @@ -1467,30 +1475,28 @@ namespace Flow.Launcher.ViewModel { SearchIconVisibility = Visibility.Visible; } + // πŸ“Œ Restore UI elements //mainWindow.SearchIcon.Visibility = Visibility.Visible; if (Settings.UseAnimation) { - Application.Current.Dispatcher.BeginInvoke(() => - mainWindow.WindowAnimation()); + mainWindow.WindowAnimation(); } } + }, DispatcherPriority.Render); - // 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 }); - if (StartWithEnglishMode) - { - Win32Helper.SwitchToEnglishKeyboardLayout(true); - } - }); + if (StartWithEnglishMode) + { + Win32Helper.SwitchToEnglishKeyboardLayout(true); + } } -#pragma warning disable VSTHRD100 // Avoid async void methods - public async void Hide() { lastHistoryIndex = 1; @@ -1505,59 +1511,47 @@ namespace Flow.Launcher.ViewModel SelectedResults = Results; } - // πŸ“Œ Immediately apply text reset + force UI update - /*if (Settings.LastQueryMode == LastQueryMode.Empty) - { - ChangeQueryText(string.Empty); - await Task.Delay(1); // Wait for one frame to ensure UI reflects changes - Application.Current.Dispatcher.Invoke(Application.Current.MainWindow.UpdateLayout); // Force UI update - }*/ - switch (Settings.LastQueryMode) { case LastQueryMode.Empty: - ChangeQueryText(string.Empty); - await Task.Delay(1); + await ChangeQueryTextAsync(string.Empty); break; case LastQueryMode.Preserved: case LastQueryMode.Selected: - LastQuerySelected = (Settings.LastQueryMode == LastQueryMode.Preserved); + LastQuerySelected = Settings.LastQueryMode == LastQueryMode.Preserved; break; - case LastQueryMode.ActionKeywordPreserved: case LastQueryMode.ActionKeywordSelected: var newQuery = _lastQuery.ActionKeyword; + if (!string.IsNullOrEmpty(newQuery)) newQuery += " "; - ChangeQueryText(newQuery); + await ChangeQueryTextAsync(newQuery); if (Settings.LastQueryMode == LastQueryMode.ActionKeywordSelected) LastQuerySelected = false; break; } - if (Application.Current.MainWindow is MainWindow mainWindow) + await Application.Current.Dispatcher.InvokeAsync(() => { - // πŸ“Œ Set Opacity of icon and clock to 0 and apply Visibility.Hidden - if(Settings.UseAnimation) + if (Application.Current.MainWindow is MainWindow mainWindow) { - mainWindow.ClockPanel.Opacity = 0; - mainWindow.SearchIcon.Opacity = 0; - } - else - { - mainWindow.ClockPanel.Opacity = 1; - mainWindow.SearchIcon.Opacity = 1; - } - mainWindow.ClockPanel.Visibility = Visibility.Hidden; - SearchIconVisibility = Visibility.Hidden; + // πŸ“Œ Set Opacity of icon and clock to 0 and apply Visibility.Hidden + var opacity = Settings.UseAnimation ? 0.0 : 1.0; + mainWindow.ClockPanel.Opacity = opacity; + mainWindow.SearchIcon.Opacity = opacity; + mainWindow.ClockPanel.Visibility = Visibility.Hidden; + SearchIconVisibility = Visibility.Hidden; - // Force UI update - mainWindow.ClockPanel.UpdateLayout(); - mainWindow.SearchIcon.UpdateLayout(); - // πŸ“Œ Apply DWM Cloak (Completely hide the window) - Win32Helper.DWMSetCloakForWindow(mainWindow, true); - } + // Force UI update + mainWindow.ClockPanel.UpdateLayout(); + mainWindow.SearchIcon.UpdateLayout(); + + // πŸ“Œ Apply DWM Cloak (Completely hide the window) + Win32Helper.DWMSetCloakForWindow(mainWindow, true); + } + }); if (StartWithEnglishMode) {