Skip to content

Fix history results clear logic & Make sure results cleared #3525

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Flow.Launcher/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@
{
_settings = Ioc.Default.GetRequiredService<Settings>();
_theme = Ioc.Default.GetRequiredService<Theme>();
_viewModel = Ioc.Default.GetRequiredService<MainViewModel>();

Check warning on line 83 in Flow.Launcher/MainWindow.xaml.cs

View workflow job for this annotation

GitHub Actions / Check Spelling

`Ioc` is not a recognized word. (unrecognized-spelling)
DataContext = _viewModel;

Check warning on line 84 in Flow.Launcher/MainWindow.xaml.cs

View workflow job for this annotation

GitHub Actions / Check Spelling

`Ioc` is not a recognized word. (unrecognized-spelling)

InitializeComponent();
UpdatePosition();
Expand All @@ -105,10 +105,10 @@
Win32Helper.DisableControlBox(this);
}

private async void OnLoaded(object sender, RoutedEventArgs _)
private void OnLoaded(object sender, RoutedEventArgs _)
{
// Check first launch
if (_settings.FirstLaunch)

Check warning on line 111 in Flow.Launcher/MainWindow.xaml.cs

View workflow job for this annotation

GitHub Actions / Check Spelling

`Wnd` is not a recognized word. (unrecognized-spelling)
{
// Set First Launch to false
_settings.FirstLaunch = false;
Expand Down Expand Up @@ -332,7 +332,7 @@
}

_hwndSource = null;
}

Check warning on line 335 in Flow.Launcher/MainWindow.xaml.cs

View workflow job for this annotation

GitHub Actions / Check Spelling

`Wnd` is not a recognized word. (unrecognized-spelling)

private void OnLocationChanged(object sender, EventArgs e)
{
Expand Down Expand Up @@ -456,7 +456,7 @@

private void OnMouseDown(object sender, MouseButtonEventArgs e)
{
if (e.ChangedButton == MouseButton.Left) DragMove();

Check warning on line 459 in Flow.Launcher/MainWindow.xaml.cs

View workflow job for this annotation

GitHub Actions / Check Spelling

`VSTHRD` is not a recognized word. (unrecognized-spelling)
}

#endregion
Expand All @@ -471,7 +471,7 @@

if (_settings.UseAnimation)
await Task.Delay(100);

Check warning on line 474 in Flow.Launcher/MainWindow.xaml.cs

View workflow job for this annotation

GitHub Actions / Check Spelling

`VSTHRD` is not a recognized word. (unrecognized-spelling)
App.API.OpenSettingDialog();
}

Expand All @@ -483,13 +483,13 @@

private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
if (msg == Win32Helper.WM_ENTERSIZEMOVE)

Check warning on line 486 in Flow.Launcher/MainWindow.xaml.cs

View workflow job for this annotation

GitHub Actions / Check Spelling

`VSTHRD` is not a recognized word. (unrecognized-spelling)
{
_initialWidth = (int)Width;
_initialHeight = (int)Height;

Check warning on line 490 in Flow.Launcher/MainWindow.xaml.cs

View workflow job for this annotation

GitHub Actions / Check Spelling

`Wnd` is not a recognized word. (unrecognized-spelling)
handled = true;
}

Check warning on line 492 in Flow.Launcher/MainWindow.xaml.cs

View workflow job for this annotation

GitHub Actions / Check Spelling

`Wnd` is not a recognized word. (unrecognized-spelling)
else if (msg == Win32Helper.WM_EXITSIZEMOVE)
{
if (_initialHeight != (int)Height)
Expand Down Expand Up @@ -560,7 +560,7 @@
animationSoundWMP.Position = TimeSpan.Zero;
animationSoundWMP.Volume = _settings.SoundVolume / 100.0;
animationSoundWMP.Play();
}

Check warning on line 563 in Flow.Launcher/MainWindow.xaml.cs

View workflow job for this annotation

GitHub Actions / Check Spelling

`WMP` is not a recognized word. (unrecognized-spelling)
else
{
animationSoundWPF.Play();
Expand Down
69 changes: 53 additions & 16 deletions Flow.Launcher/ViewModel/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -1246,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;
}

Expand All @@ -1284,8 +1273,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<PluginPair> plugins = Array.Empty<PluginPair>();
if (currentIsHomeQuery)
{
Expand Down Expand Up @@ -1342,6 +1329,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)
{
Expand Down Expand Up @@ -1376,6 +1364,24 @@ private async Task QueryResultsAsync(bool searchDelay, bool isReQuery = false, b
// nothing to do here
}

// 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
if (!resultsCleared)
{
ClearResults();
return;
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this really necessary? This could cause unexplained/unexpected flicker

Copy link
Member Author

@Jack251970 Jack251970 May 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We must need it. This is used to make sure results are cleared when there are not any update tasks.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Think about one situation. Home page feature is off and you just change query text from e to empty. You will find the results will not be cleared because there are not any update tasks. So we should force call Results.Clear() here when it is not be called in QueryResult or QueryHistory functions.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When you say not any update task are you referring to one example where you toggle off home page in settings and results are not cleared?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me give you one demo video

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jjw24 It seems that I have issues when uploading video here. Please see more in Discord.

if (currentCancellationToken.IsCancellationRequested) return;

// this should happen once after all queries are done so progress bar should continue
Expand All @@ -1389,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}>");
Expand Down Expand Up @@ -1439,6 +1462,7 @@ 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)))
Expand All @@ -1458,8 +1482,14 @@ 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;
resultsCleared = true;

if (!_resultsUpdateChannelWriter.TryWrite(new ResultsForUpdate(results, _historyMetadata, query,
token)))
token, reSelect, shouldClearExistingResults)))
{
App.API.LogError(ClassName, "Unable to add item to Result Update Queue");
}
Expand Down Expand Up @@ -1558,6 +1588,13 @@ private async Task BuildQueryAsync(IEnumerable<BaseBuiltinShortcutModel> builtIn
/// <returns>True if the existing results should be cleared, false otherwise.</returns>
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)
{
Expand Down
Loading