Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 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
37 changes: 23 additions & 14 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 @@ -293,34 +295,32 @@ public class ResultCollection : List<ResultViewModel>, INotifyCollectionChanged
{
private long editTime = 0;

private CancellationToken _token;

public event NotifyCollectionChangedEventHandler CollectionChanged;

protected void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
{
CollectionChanged?.Invoke(this, e);
}

public void BulkAddAll(List<ResultViewModel> resultViews)
private void BulkAddAll(List<ResultViewModel> resultViews, CancellationToken token = default)
{
AddRange(resultViews);

// can return because the list will be cleared next time updated, which include a reset event
if (_token.IsCancellationRequested)
if (token.IsCancellationRequested)
return;

// manually update event
// wpf use DirectX / double buffered already, so just reset all won't cause ui flickering
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
}

private void AddAll(List<ResultViewModel> Items)
private void AddAll(List<ResultViewModel> Items, CancellationToken token = default)
{
for (int i = 0; i < Items.Count; i++)
{
var item = Items[i];
if (_token.IsCancellationRequested)
if (token.IsCancellationRequested)
return;
Add(item);
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, item, i));
Expand All @@ -342,21 +342,30 @@ public void RemoveAll(int Capacity = 512)
/// <param name="newItems"></param>
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, so we cannot check token cancellation here
if (Count == 0 && newItems.Count == 0)
return;

if (editTime < 10 || newItems.Count < 30)
{
if (Count != 0) RemoveAll(newItems.Count);
AddAll(newItems);

// After results are removed, we need to check the token cancellation
// so that we will not add new items from the cancelled queries
if (token.IsCancellationRequested) return;

AddAll(newItems, token);
editTime++;
return;
}
else
{
Clear();
BulkAddAll(newItems);

// After results are removed, we need to check the token cancellation
// so that we will not add new items from the cancelled queries
if (token.IsCancellationRequested) return;

BulkAddAll(newItems, token);
if (Capacity > 8000 && newItems.Count < 3000)
{
Capacity = newItems.Count;
Expand Down
Loading