From 3718796da153d8582d66811502abf72c0d1e05d6 Mon Sep 17 00:00:00 2001 From: Jack251970 <1160210343@qq.com> Date: Sat, 10 May 2025 21:20:40 +0800 Subject: [PATCH 1/9] Fix history results clear logic --- Flow.Launcher/ViewModel/MainViewModel.cs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/Flow.Launcher/ViewModel/MainViewModel.cs b/Flow.Launcher/ViewModel/MainViewModel.cs index 0c299875fdf..eb67bef9be0 100644 --- a/Flow.Launcher/ViewModel/MainViewModel.cs +++ b/Flow.Launcher/ViewModel/MainViewModel.cs @@ -1284,8 +1284,6 @@ private async Task QueryResultsAsync(bool searchDelay, bool isReQuery = false, b // Update the query's IsReQuery property to true if this is a re-query query.IsReQuery = isReQuery; - - ICollection plugins = Array.Empty(); if (currentIsHomeQuery) { @@ -1458,8 +1456,13 @@ void QueryHistoryTask(CancellationToken token) App.API.LogDebug(ClassName, $"Update results for history"); + // Indicate if to clear existing results so to show only ones from plugins with action keywords + var shouldClearExistingResults = ShouldClearExistingResults(query, currentIsHomeQuery); + _lastQuery = query; + _previousIsHomeQuery = currentIsHomeQuery; + if (!_resultsUpdateChannelWriter.TryWrite(new ResultsForUpdate(results, _historyMetadata, query, - token))) + token, reSelect, shouldClearExistingResults))) { App.API.LogError(ClassName, "Unable to add item to Result Update Queue"); } From 54d50cfa76162e95a348fa91b116847356926cc7 Mon Sep 17 00:00:00 2001 From: Jack251970 <1160210343@qq.com> Date: Sun, 11 May 2025 14:38:54 +0800 Subject: [PATCH 2/9] Remove async --- Flow.Launcher/MainWindow.xaml.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Flow.Launcher/MainWindow.xaml.cs b/Flow.Launcher/MainWindow.xaml.cs index e243549e31e..863945d70fd 100644 --- a/Flow.Launcher/MainWindow.xaml.cs +++ b/Flow.Launcher/MainWindow.xaml.cs @@ -105,7 +105,7 @@ private void OnSourceInitialized(object sender, EventArgs e) Win32Helper.DisableControlBox(this); } - private async void OnLoaded(object sender, RoutedEventArgs _) + private void OnLoaded(object sender, RoutedEventArgs _) { // Check first launch if (_settings.FirstLaunch) From 5784f87fc7fbfe47279cdf004540d9cdd636bb4a Mon Sep 17 00:00:00 2001 From: Jack251970 <1160210343@qq.com> Date: Sun, 11 May 2025 14:46:51 +0800 Subject: [PATCH 3/9] Make sure results cleared when update tasks are not called --- Flow.Launcher/ViewModel/MainViewModel.cs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/Flow.Launcher/ViewModel/MainViewModel.cs b/Flow.Launcher/ViewModel/MainViewModel.cs index eb67bef9be0..c1d856f16c0 100644 --- a/Flow.Launcher/ViewModel/MainViewModel.cs +++ b/Flow.Launcher/ViewModel/MainViewModel.cs @@ -1340,6 +1340,7 @@ private async Task QueryResultsAsync(bool searchDelay, bool isReQuery = false, b // plugins are ICollection, meaning LINQ will get the Count and preallocate Array + var resultsCleared = false; Task[] tasks; if (currentIsHomeQuery) { @@ -1374,6 +1375,9 @@ private async Task QueryResultsAsync(bool searchDelay, bool isReQuery = false, b // nothing to do here } + // If results are not cleared, we need to clear the results + if (!resultsCleared) Results.Clear(); + if (currentCancellationToken.IsCancellationRequested) return; // this should happen once after all queries are done so progress bar should continue @@ -1443,6 +1447,11 @@ await PluginManager.QueryHomeForPluginAsync(plugin, query, token) : { App.API.LogError(ClassName, "Unable to add item to Result Update Queue"); } + else + { + // Only update the clear flag when we successfully write to the channel + resultsCleared = true; + } } void QueryHistoryTask(CancellationToken token) @@ -1466,6 +1475,11 @@ void QueryHistoryTask(CancellationToken token) { App.API.LogError(ClassName, "Unable to add item to Result Update Queue"); } + else + { + // Only update the clear flag when we successfully write to the channel + resultsCleared = true; + } } } From 3484d0896110b44b6177db51373eb739fe42c4a5 Mon Sep 17 00:00:00 2001 From: Jack251970 <1160210343@qq.com> Date: Sun, 11 May 2025 16:54:50 +0800 Subject: [PATCH 4/9] Fix result clear flag issue --- Flow.Launcher/ViewModel/MainViewModel.cs | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/Flow.Launcher/ViewModel/MainViewModel.cs b/Flow.Launcher/ViewModel/MainViewModel.cs index c1d856f16c0..1cbde808cf1 100644 --- a/Flow.Launcher/ViewModel/MainViewModel.cs +++ b/Flow.Launcher/ViewModel/MainViewModel.cs @@ -1447,11 +1447,8 @@ await PluginManager.QueryHomeForPluginAsync(plugin, query, token) : { App.API.LogError(ClassName, "Unable to add item to Result Update Queue"); } - else - { - // Only update the clear flag when we successfully write to the channel - resultsCleared = true; - } + + resultsCleared = true; } void QueryHistoryTask(CancellationToken token) @@ -1475,11 +1472,8 @@ void QueryHistoryTask(CancellationToken token) { App.API.LogError(ClassName, "Unable to add item to Result Update Queue"); } - else - { - // Only update the clear flag when we successfully write to the channel - resultsCleared = true; - } + + resultsCleared = true; } } From 021b4510a369d48874553d45d0a6932e39908f42 Mon Sep 17 00:00:00 2001 From: Jack251970 <1160210343@qq.com> Date: Sun, 11 May 2025 17:31:11 +0800 Subject: [PATCH 5/9] Improve code comments & Move flag position --- Flow.Launcher/ViewModel/MainViewModel.cs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/Flow.Launcher/ViewModel/MainViewModel.cs b/Flow.Launcher/ViewModel/MainViewModel.cs index 1cbde808cf1..907b350c234 100644 --- a/Flow.Launcher/ViewModel/MainViewModel.cs +++ b/Flow.Launcher/ViewModel/MainViewModel.cs @@ -1375,7 +1375,8 @@ private async Task QueryResultsAsync(bool searchDelay, bool isReQuery = false, b // nothing to do here } - // If results are not cleared, we need to clear the results + // If QueryTaskAsync or QueryHistoryTask is not called which means that results are not cleared + // we need to clear the results if (!resultsCleared) Results.Clear(); if (currentCancellationToken.IsCancellationRequested) return; @@ -1441,14 +1442,13 @@ await PluginManager.QueryHomeForPluginAsync(plugin, query, token) : var shouldClearExistingResults = ShouldClearExistingResults(query, currentIsHomeQuery); _lastQuery = query; _previousIsHomeQuery = currentIsHomeQuery; + resultsCleared = true; if (!_resultsUpdateChannelWriter.TryWrite(new ResultsForUpdate(resultsCopy, plugin.Metadata, query, token, reSelect, shouldClearExistingResults))) { App.API.LogError(ClassName, "Unable to add item to Result Update Queue"); } - - resultsCleared = true; } void QueryHistoryTask(CancellationToken token) @@ -1466,14 +1466,13 @@ void QueryHistoryTask(CancellationToken token) var shouldClearExistingResults = ShouldClearExistingResults(query, currentIsHomeQuery); _lastQuery = query; _previousIsHomeQuery = currentIsHomeQuery; + resultsCleared = true; if (!_resultsUpdateChannelWriter.TryWrite(new ResultsForUpdate(results, _historyMetadata, query, token, reSelect, shouldClearExistingResults))) { App.API.LogError(ClassName, "Unable to add item to Result Update Queue"); } - - resultsCleared = true; } } From 6e473c82dab89dd10d11ba2822cdfacf27541b56 Mon Sep 17 00:00:00 2001 From: Jack251970 <1160210343@qq.com> Date: Wed, 14 May 2025 15:21:40 +0800 Subject: [PATCH 6/9] Fix page flickering when typing is very fast --- Flow.Launcher/ViewModel/MainViewModel.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Flow.Launcher/ViewModel/MainViewModel.cs b/Flow.Launcher/ViewModel/MainViewModel.cs index 907b350c234..d65b9ecc909 100644 --- a/Flow.Launcher/ViewModel/MainViewModel.cs +++ b/Flow.Launcher/ViewModel/MainViewModel.cs @@ -1375,6 +1375,8 @@ private async Task QueryResultsAsync(bool searchDelay, bool isReQuery = false, b // nothing to do here } + if (currentCancellationToken.IsCancellationRequested) return; + // If QueryTaskAsync or QueryHistoryTask is not called which means that results are not cleared // we need to clear the results if (!resultsCleared) Results.Clear(); From c8989c39bc65e4383f8a1233b9dc73b9c9128be1 Mon Sep 17 00:00:00 2001 From: Jack251970 <1160210343@qq.com> Date: Wed, 14 May 2025 21:34:11 +0800 Subject: [PATCH 7/9] Fix results clear issue with one flag --- Flow.Launcher/ViewModel/MainViewModel.cs | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/Flow.Launcher/ViewModel/MainViewModel.cs b/Flow.Launcher/ViewModel/MainViewModel.cs index d65b9ecc909..d74c9514577 100644 --- a/Flow.Launcher/ViewModel/MainViewModel.cs +++ b/Flow.Launcher/ViewModel/MainViewModel.cs @@ -34,6 +34,7 @@ public partial class MainViewModel : BaseModel, ISavable, IDisposable private bool _isQueryRunning; private Query _lastQuery; private bool _previousIsHomeQuery; + private bool _needClearResults; private string _queryTextBeforeLeaveResults; private string _ignoredQueryText; // Used to ignore query text change when switching between context menu and query results @@ -1375,7 +1376,15 @@ private async Task QueryResultsAsync(bool searchDelay, bool isReQuery = false, b // nothing to do here } - if (currentCancellationToken.IsCancellationRequested) return; + // If the query is cancelled, part of results may be added to the results already. + // But we should not clear results now + // because typing very fast will cause many calls to Results.Clear() which leads to flickering issue + // Instead, we should clear the results next time we update the results + if (currentCancellationToken.IsCancellationRequested) + { + _needClearResults = true; + return; + } // If QueryTaskAsync or QueryHistoryTask is not called which means that results are not cleared // we need to clear the results @@ -1584,6 +1593,13 @@ private bool ShouldClearExistingResults(Query query, bool currentIsHomeQuery) return true; } + // If the results are not cleared temporarily, we need to clear this time + if (_needClearResults) + { + _needClearResults = false; + return true; + } + return false; } From 3436ac9280a6f0ad40bc41497899d87beb3a348a Mon Sep 17 00:00:00 2001 From: Jack251970 <1160210343@qq.com> Date: Wed, 14 May 2025 21:38:11 +0800 Subject: [PATCH 8/9] Adjust check position --- Flow.Launcher/ViewModel/MainViewModel.cs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/Flow.Launcher/ViewModel/MainViewModel.cs b/Flow.Launcher/ViewModel/MainViewModel.cs index d74c9514577..937f2211aa3 100644 --- a/Flow.Launcher/ViewModel/MainViewModel.cs +++ b/Flow.Launcher/ViewModel/MainViewModel.cs @@ -1579,6 +1579,13 @@ private async Task BuildQueryAsync(IEnumerable builtIn /// True if the existing results should be cleared, false otherwise. private bool ShouldClearExistingResults(Query query, bool currentIsHomeQuery) { + // If the results are not cleared temporarily, we need to clear this time + if (_needClearResults) + { + _needClearResults = false; + return true; + } + // If previous or current results are from home query, we need to clear them if (_previousIsHomeQuery || currentIsHomeQuery) { @@ -1593,13 +1600,6 @@ private bool ShouldClearExistingResults(Query query, bool currentIsHomeQuery) return true; } - // If the results are not cleared temporarily, we need to clear this time - if (_needClearResults) - { - _needClearResults = false; - return true; - } - return false; } From 08447dab45f8725389f3f0f0c1a879725b63f221 Mon Sep 17 00:00:00 2001 From: Jack251970 <1160210343@qq.com> Date: Thu, 15 May 2025 11:08:31 +0800 Subject: [PATCH 9/9] Move clear results to local function --- Flow.Launcher/ViewModel/MainViewModel.cs | 37 +++++++++++++++--------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/Flow.Launcher/ViewModel/MainViewModel.cs b/Flow.Launcher/ViewModel/MainViewModel.cs index 937f2211aa3..8aa41697432 100644 --- a/Flow.Launcher/ViewModel/MainViewModel.cs +++ b/Flow.Launcher/ViewModel/MainViewModel.cs @@ -1247,19 +1247,7 @@ private async Task QueryResultsAsync(bool searchDelay, bool isReQuery = false, b if (query == null) // shortcut expanded { - App.API.LogDebug(ClassName, $"Clear query results"); - - // Hide and clear results again because running query may show and add some results - Results.Visibility = Visibility.Collapsed; - Results.Clear(); - - // Reset plugin icon - PluginIconPath = null; - PluginIconSource = null; - SearchIconVisibility = Visibility.Visible; - - // Hide progress bar again because running query may set this to visible - ProgressBarVisibility = Visibility.Hidden; + ClearResults(); return; } @@ -1388,7 +1376,11 @@ private async Task QueryResultsAsync(bool searchDelay, bool isReQuery = false, b // If QueryTaskAsync or QueryHistoryTask is not called which means that results are not cleared // we need to clear the results - if (!resultsCleared) Results.Clear(); + if (!resultsCleared) + { + ClearResults(); + return; + } if (currentCancellationToken.IsCancellationRequested) return; @@ -1403,6 +1395,23 @@ private async Task QueryResultsAsync(bool searchDelay, bool isReQuery = false, b } // Local function + void ClearResults() + { + App.API.LogDebug(ClassName, $"Clear query results"); + + // Hide and clear results again because running query may show and add some results + Results.Visibility = Visibility.Collapsed; + Results.Clear(); + + // Reset plugin icon + PluginIconPath = null; + PluginIconSource = null; + SearchIconVisibility = Visibility.Visible; + + // Hide progress bar again because running query may set this to visible + ProgressBarVisibility = Visibility.Hidden; + } + async Task QueryTaskAsync(PluginPair plugin, CancellationToken token) { App.API.LogDebug(ClassName, $"Wait for querying plugin <{plugin.Metadata.Name}>");