Skip to content

Use new model to clear results & Fix clear existing results when using IResultUpdate #3588

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

Merged
merged 19 commits into from
Jun 2, 2025
Merged
Show file tree
Hide file tree
Changes from 3 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
37 changes: 35 additions & 2 deletions Flow.Launcher/ViewModel/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@
private string _queryTextBeforeLeaveResults;
private string _ignoredQueryText; // Used to ignore query text change when switching between context menu and query results

private readonly object _shouldClearExistingResultsLock = new();
private bool _shouldClearExistingResults;

private readonly FlowLauncherJsonStorage<History> _historyItemsStorage;
private readonly FlowLauncherJsonStorage<UserSelectedRecord> _userSelectedRecordStorage;
private readonly FlowLauncherJsonStorageTopMostRecord _topMostRecord;
Expand Down Expand Up @@ -1434,11 +1437,19 @@

// Indicate if to clear existing results so to show only ones from plugins with action keywords
var shouldClearExistingResults = ShouldClearExistingResultsForQuery(query, currentIsHomeQuery);
if (shouldClearExistingResults)
{
// Setup the flag to clear existing results so that ResultsViewModel.NewResults will handle in the next update
lock (_shouldClearExistingResultsLock)
{
_shouldClearExistingResults = true;
}
}
_lastQuery = query;
_previousIsHomeQuery = currentIsHomeQuery;

if (!_resultsUpdateChannelWriter.TryWrite(new ResultsForUpdate(resultsCopy, plugin.Metadata, query,
token, reSelect, shouldClearExistingResults)))
token, reSelect)))
{
App.API.LogError(ClassName, "Unable to add item to Result Update Queue");
}
Expand All @@ -1457,11 +1468,19 @@

// Indicate if to clear existing results so to show only ones from plugins with action keywords
var shouldClearExistingResults = ShouldClearExistingResultsForQuery(query, currentIsHomeQuery);
if (shouldClearExistingResults)
{
// Setup the flag to clear existing results so that ResultsViewModel.NewResults will handle in the next update
lock (_shouldClearExistingResultsLock)
{
_shouldClearExistingResults = true;
}
}
_lastQuery = query;
_previousIsHomeQuery = currentIsHomeQuery;

if (!_resultsUpdateChannelWriter.TryWrite(new ResultsForUpdate(results, _historyMetadata, query,
token, reSelect, shouldClearExistingResults)))
token, reSelect)))
{
App.API.LogError(ClassName, "Unable to add item to Result Update Queue");
}
Expand Down Expand Up @@ -1696,6 +1715,20 @@
return selected;
}

internal bool CheckShouldClearExistingResultsAndReset()
{
lock (_shouldClearExistingResultsLock)
{
if (_shouldClearExistingResults)
{
_shouldClearExistingResults = false;
return true;
}

return false;
}
}

#endregion

#region Hotkey
Expand Down Expand Up @@ -1842,7 +1875,7 @@
VisibilityChanged?.Invoke(this, new VisibilityChangedEventArgs { IsVisible = false });
}

#pragma warning restore VSTHRD100 // Avoid async void methods

Check warning on line 1878 in Flow.Launcher/ViewModel/MainViewModel.cs

View workflow job for this annotation

GitHub Actions / Check Spelling

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

/// <summary>
/// Save history, user selected records and top most records
Expand Down
3 changes: 1 addition & 2 deletions Flow.Launcher/ViewModel/ResultsForUpdate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ public record struct ResultsForUpdate(
PluginMetadata Metadata,
Query Query,
CancellationToken Token,
bool ReSelectFirstResult = true,
bool shouldClearExistingResults = false)
bool ReSelectFirstResult = true)
{
public string ID { get; } = Metadata.ID;
}
Expand Down
14 changes: 10 additions & 4 deletions Flow.Launcher/ViewModel/ResultsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -187,11 +187,12 @@ public void AddResults(List<Result> newRawResults, string resultId)
/// </summary>
public void AddResults(ICollection<ResultsForUpdate> resultsForUpdates, CancellationToken token, bool reselect = true)
{
var newResults = NewResults(resultsForUpdates);

if (token.IsCancellationRequested)
return;

// Since NewResults may need to clear existing results, do not check token cancellation after this point
var newResults = NewResults(resultsForUpdates);

UpdateResults(newResults, reselect, token);
}

Expand Down Expand Up @@ -244,7 +245,8 @@ private List<ResultViewModel> NewResults(ICollection<ResultsForUpdate> resultsFo

var newResults = resultsForUpdates.SelectMany(u => u.Results, (u, r) => new ResultViewModel(r, _settings));

if (resultsForUpdates.Any(x => x.shouldClearExistingResults))
// If mainVM has flag to clear existing results, handle it here
if (_mainVM != null && _mainVM.CheckShouldClearExistingResultsAndReset())
{
App.API.LogDebug("NewResults", $"Existing results are cleared for query");
return newResults.OrderByDescending(rv => rv.Result.Score).ToList();
Expand Down Expand Up @@ -343,18 +345,22 @@ public void RemoveAll(int Capacity = 512)
public void Update(List<ResultViewModel> newItems, CancellationToken token = default)
{
_token = token;
if (Count == 0 && newItems.Count == 0 || _token.IsCancellationRequested)

// Since NewResults may need to clear existing results, do not check token cancellation here!
if (Count == 0 && newItems.Count == 0)
return;

if (editTime < 10 || newItems.Count < 30)
{
// RemoveAll will not check token cancellation, so it will clear existing results
if (Count != 0) RemoveAll(newItems.Count);
AddAll(newItems);
editTime++;
return;
}
else
{
// Clear will not check token cancellation, so it will clear existing results
Clear();
BulkAddAll(newItems);
if (Capacity > 8000 && newItems.Count < 3000)
Expand Down
Loading