Skip to content

Commit a06cb43

Browse files
authored
Use new model to clear results & Fix clear existing results when using IResultUpdate (#3588)
1 parent a68c69b commit a06cb43

File tree

6 files changed

+63
-33
lines changed

6 files changed

+63
-33
lines changed

Flow.Launcher.Core/Plugin/QueryBuilder.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ public static Query Build(string text, Dictionary<string, PluginPair> nonGlobalP
1616
Search = string.Empty,
1717
RawQuery = string.Empty,
1818
SearchTerms = Array.Empty<string>(),
19-
ActionKeyword = string.Empty
19+
ActionKeyword = string.Empty,
20+
IsHomeQuery = true
2021
};
2122
}
2223

@@ -53,7 +54,8 @@ public static Query Build(string text, Dictionary<string, PluginPair> nonGlobalP
5354
Search = search,
5455
RawQuery = rawQuery,
5556
SearchTerms = searchTerms,
56-
ActionKeyword = actionKeyword
57+
ActionKeyword = actionKeyword,
58+
IsHomeQuery = false
5759
};
5860
}
5961
}

Flow.Launcher.Plugin/Query.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ public class Query
2121
/// </summary>
2222
public bool IsReQuery { get; internal set; } = false;
2323

24+
/// <summary>
25+
/// Determines whether the query is a home query.
26+
/// </summary>
27+
public bool IsHomeQuery { get; internal init; } = false;
28+
2429
/// <summary>
2530
/// Search part of a query.
2631
/// This will not include action keyword if exclusive plugin gets it, otherwise it should be same as RawQuery.

Flow.Launcher/MainWindow.xaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public MainWindow()
101101

102102
private void ThemeManager_ActualApplicationThemeChanged(ModernWpf.ThemeManager sender, object args)
103103
{
104-
_theme.RefreshFrameAsync();
104+
_ = _theme.RefreshFrameAsync();
105105
}
106106

107107
private void OnSourceInitialized(object sender, EventArgs e)

Flow.Launcher/ViewModel/MainViewModel.cs

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,26 @@ async Task UpdateActionAsync()
216216
while (channelReader.TryRead(out var item))
217217
{
218218
if (!item.Token.IsCancellationRequested)
219+
{
220+
// Indicate if to clear existing results so to show only ones from plugins with action keywords
221+
var query = item.Query;
222+
var currentIsHomeQuery = query.IsHomeQuery;
223+
var shouldClearExistingResults = ShouldClearExistingResultsForQuery(query, currentIsHomeQuery);
224+
_lastQuery = item.Query;
225+
_previousIsHomeQuery = currentIsHomeQuery;
226+
227+
// If the queue already has the item, we need to pass the shouldClearExistingResults flag
228+
if (queue.TryGetValue(item.ID, out var existingItem))
229+
{
230+
item.ShouldClearExistingResults = shouldClearExistingResults || existingItem.ShouldClearExistingResults;
231+
}
232+
else
233+
{
234+
item.ShouldClearExistingResults = shouldClearExistingResults;
235+
}
236+
219237
queue[item.ID] = item;
238+
}
220239
}
221240

222241
UpdateResultView(queue.Values);
@@ -268,6 +287,8 @@ public void RegisterResultsUpdatedEvent()
268287

269288
if (token.IsCancellationRequested) return;
270289

290+
App.API.LogDebug(ClassName, $"Update results for plugin <{pair.Metadata.Name}>");
291+
271292
if (!_resultsUpdateChannelWriter.TryWrite(new ResultsForUpdate(resultsCopy, pair.Metadata, e.Query,
272293
token)))
273294
{
@@ -1262,7 +1283,7 @@ private async Task QueryResultsAsync(bool searchDelay, bool isReQuery = false, b
12621283

12631284
App.API.LogDebug(ClassName, $"Start query with ActionKeyword <{query.ActionKeyword}> and RawQuery <{query.RawQuery}>");
12641285

1265-
var currentIsHomeQuery = query.RawQuery == string.Empty;
1286+
var currentIsHomeQuery = query.IsHomeQuery;
12661287

12671288
_updateSource?.Dispose();
12681289

@@ -1436,13 +1457,8 @@ await PluginManager.QueryHomeForPluginAsync(plugin, query, token) :
14361457

14371458
App.API.LogDebug(ClassName, $"Update results for plugin <{plugin.Metadata.Name}>");
14381459

1439-
// Indicate if to clear existing results so to show only ones from plugins with action keywords
1440-
var shouldClearExistingResults = ShouldClearExistingResultsForQuery(query, currentIsHomeQuery);
1441-
_lastQuery = query;
1442-
_previousIsHomeQuery = currentIsHomeQuery;
1443-
14441460
if (!_resultsUpdateChannelWriter.TryWrite(new ResultsForUpdate(resultsCopy, plugin.Metadata, query,
1445-
token, reSelect, shouldClearExistingResults)))
1461+
token, reSelect)))
14461462
{
14471463
App.API.LogError(ClassName, "Unable to add item to Result Update Queue");
14481464
}
@@ -1459,13 +1475,8 @@ void QueryHistoryTask(CancellationToken token)
14591475

14601476
App.API.LogDebug(ClassName, $"Update results for history");
14611477

1462-
// Indicate if to clear existing results so to show only ones from plugins with action keywords
1463-
var shouldClearExistingResults = ShouldClearExistingResultsForQuery(query, currentIsHomeQuery);
1464-
_lastQuery = query;
1465-
_previousIsHomeQuery = currentIsHomeQuery;
1466-
14671478
if (!_resultsUpdateChannelWriter.TryWrite(new ResultsForUpdate(results, _historyMetadata, query,
1468-
token, reSelect, shouldClearExistingResults)))
1479+
token, reSelect)))
14691480
{
14701481
App.API.LogError(ClassName, "Unable to add item to Result Update Queue");
14711482
}
@@ -1865,6 +1876,7 @@ public void UpdateResultView(ICollection<ResultsForUpdate> resultsForUpdates)
18651876
{
18661877
if (!resultsForUpdates.Any())
18671878
return;
1879+
18681880
CancellationToken token;
18691881

18701882
try

Flow.Launcher/ViewModel/ResultsForUpdate.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public record struct ResultsForUpdate(
1010
Query Query,
1111
CancellationToken Token,
1212
bool ReSelectFirstResult = true,
13-
bool shouldClearExistingResults = false)
13+
bool ShouldClearExistingResults = false)
1414
{
1515
public string ID { get; } = Metadata.ID;
1616
}

Flow.Launcher/ViewModel/ResultsViewModel.cs

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ public class ResultsViewModel : BaseModel
1717
{
1818
#region Private Fields
1919

20+
private readonly string ClassName = nameof(ResultsViewModel);
21+
2022
public ResultCollection Results { get; }
2123

2224
private readonly object _collectionLock = new();
@@ -187,11 +189,9 @@ public void AddResults(List<Result> newRawResults, string resultId)
187189
/// </summary>
188190
public void AddResults(ICollection<ResultsForUpdate> resultsForUpdates, CancellationToken token, bool reselect = true)
189191
{
192+
// Since NewResults may need to clear existing results, do not check token cancellation after this point
190193
var newResults = NewResults(resultsForUpdates);
191194

192-
if (token.IsCancellationRequested)
193-
return;
194-
195195
UpdateResults(newResults, reselect, token);
196196
}
197197

@@ -240,16 +240,20 @@ private List<ResultViewModel> NewResults(List<Result> newRawResults, string resu
240240
private List<ResultViewModel> NewResults(ICollection<ResultsForUpdate> resultsForUpdates)
241241
{
242242
if (!resultsForUpdates.Any())
243+
{
244+
App.API.LogDebug(ClassName, "No results for updates, returning existing results");
243245
return Results;
246+
}
244247

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

247-
if (resultsForUpdates.Any(x => x.shouldClearExistingResults))
250+
if (resultsForUpdates.Any(x => x.ShouldClearExistingResults))
248251
{
249-
App.API.LogDebug("NewResults", $"Existing results are cleared for query");
252+
App.API.LogDebug(ClassName, $"Existing results are cleared for query");
250253
return newResults.OrderByDescending(rv => rv.Result.Score).ToList();
251254
}
252255

256+
App.API.LogDebug(ClassName, $"Keeping existing results for {resultsForUpdates.Count} queries");
253257
return Results.Where(r => r?.Result != null && resultsForUpdates.All(u => u.ID != r.Result.PluginID))
254258
.Concat(newResults)
255259
.OrderByDescending(rv => rv.Result.Score)
@@ -293,34 +297,32 @@ public class ResultCollection : List<ResultViewModel>, INotifyCollectionChanged
293297
{
294298
private long editTime = 0;
295299

296-
private CancellationToken _token;
297-
298300
public event NotifyCollectionChangedEventHandler CollectionChanged;
299301

300302
protected void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
301303
{
302304
CollectionChanged?.Invoke(this, e);
303305
}
304306

305-
public void BulkAddAll(List<ResultViewModel> resultViews)
307+
private void BulkAddAll(List<ResultViewModel> resultViews, CancellationToken token = default)
306308
{
307309
AddRange(resultViews);
308310

309311
// can return because the list will be cleared next time updated, which include a reset event
310-
if (_token.IsCancellationRequested)
312+
if (token.IsCancellationRequested)
311313
return;
312314

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

318-
private void AddAll(List<ResultViewModel> Items)
320+
private void AddAll(List<ResultViewModel> Items, CancellationToken token = default)
319321
{
320322
for (int i = 0; i < Items.Count; i++)
321323
{
322324
var item = Items[i];
323-
if (_token.IsCancellationRequested)
325+
if (token.IsCancellationRequested)
324326
return;
325327
Add(item);
326328
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, item, i));
@@ -342,21 +344,30 @@ public void RemoveAll(int Capacity = 512)
342344
/// <param name="newItems"></param>
343345
public void Update(List<ResultViewModel> newItems, CancellationToken token = default)
344346
{
345-
_token = token;
346-
if (Count == 0 && newItems.Count == 0 || _token.IsCancellationRequested)
347+
// Since NewResults may need to clear existing results, so we cannot check token cancellation here
348+
if (Count == 0 && newItems.Count == 0)
347349
return;
348350

349351
if (editTime < 10 || newItems.Count < 30)
350352
{
351353
if (Count != 0) RemoveAll(newItems.Count);
352-
AddAll(newItems);
354+
355+
// After results are removed, we need to check the token cancellation
356+
// so that we will not add new items from the cancelled queries
357+
if (token.IsCancellationRequested) return;
358+
359+
AddAll(newItems, token);
353360
editTime++;
354-
return;
355361
}
356362
else
357363
{
358364
Clear();
359-
BulkAddAll(newItems);
365+
366+
// After results are removed, we need to check the token cancellation
367+
// so that we will not add new items from the cancelled queries
368+
if (token.IsCancellationRequested) return;
369+
370+
BulkAddAll(newItems, token);
360371
if (Capacity > 8000 && newItems.Count < 3000)
361372
{
362373
Capacity = newItems.Count;

0 commit comments

Comments
 (0)