Skip to content

Commit 7c8a437

Browse files
authored
fix clearing of results logic & minor adjustment to results update (#3524)
1 parent 2fdab61 commit 7c8a437

File tree

3 files changed

+48
-30
lines changed

3 files changed

+48
-30
lines changed

Flow.Launcher/ViewModel/MainViewModel.cs

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public partial class MainViewModel : BaseModel, ISavable, IDisposable
3333

3434
private bool _isQueryRunning;
3535
private Query _lastQuery;
36-
private bool _lastIsHomeQuery;
36+
private bool _previousIsHomeQuery;
3737
private string _queryTextBeforeLeaveResults;
3838
private string _ignoredQueryText; // Used to ignore query text change when switching between context menu and query results
3939

@@ -1264,7 +1264,7 @@ private async Task QueryResultsAsync(bool searchDelay, bool isReQuery = false, b
12641264

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

1267-
var isHomeQuery = query.RawQuery == string.Empty;
1267+
var currentIsHomeQuery = query.RawQuery == string.Empty;
12681268

12691269
_updateSource?.Dispose();
12701270

@@ -1284,14 +1284,10 @@ private async Task QueryResultsAsync(bool searchDelay, bool isReQuery = false, b
12841284
// Update the query's IsReQuery property to true if this is a re-query
12851285
query.IsReQuery = isReQuery;
12861286

1287-
// handle the exclusiveness of plugin using action keyword
1288-
RemoveOldQueryResults(query, isHomeQuery);
1289-
1290-
_lastQuery = query;
1291-
_lastIsHomeQuery = isHomeQuery;
1287+
12921288

12931289
ICollection<PluginPair> plugins = Array.Empty<PluginPair>();
1294-
if (isHomeQuery)
1290+
if (currentIsHomeQuery)
12951291
{
12961292
if (Settings.ShowHomePage)
12971293
{
@@ -1347,7 +1343,7 @@ private async Task QueryResultsAsync(bool searchDelay, bool isReQuery = false, b
13471343
// plugins are ICollection, meaning LINQ will get the Count and preallocate Array
13481344

13491345
Task[] tasks;
1350-
if (isHomeQuery)
1346+
if (currentIsHomeQuery)
13511347
{
13521348
tasks = plugins.Select(plugin => plugin.Metadata.HomeDisabled switch
13531349
{
@@ -1397,7 +1393,7 @@ async Task QueryTaskAsync(PluginPair plugin, CancellationToken token)
13971393
{
13981394
App.API.LogDebug(ClassName, $"Wait for querying plugin <{plugin.Metadata.Name}>");
13991395

1400-
if (searchDelay && !isHomeQuery) // Do not delay for home query
1396+
if (searchDelay && !currentIsHomeQuery) // Do not delay for home query
14011397
{
14021398
var searchDelayTime = plugin.Metadata.SearchDelayTime ?? Settings.SearchDelayTime;
14031399

@@ -1410,7 +1406,7 @@ async Task QueryTaskAsync(PluginPair plugin, CancellationToken token)
14101406
// Task.Yield will force it to run in ThreadPool
14111407
await Task.Yield();
14121408

1413-
var results = isHomeQuery ?
1409+
var results = currentIsHomeQuery ?
14141410
await PluginManager.QueryHomeForPluginAsync(plugin, query, token) :
14151411
await PluginManager.QueryForPluginAsync(plugin, query, token);
14161412

@@ -1439,8 +1435,13 @@ await PluginManager.QueryHomeForPluginAsync(plugin, query, token) :
14391435

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

1438+
// Indicate if to clear existing results so to show only ones from plugins with action keywords
1439+
var shouldClearExistingResults = ShouldClearExistingResults(query, currentIsHomeQuery);
1440+
_lastQuery = query;
1441+
_previousIsHomeQuery = currentIsHomeQuery;
1442+
14421443
if (!_resultsUpdateChannelWriter.TryWrite(new ResultsForUpdate(resultsCopy, plugin.Metadata, query,
1443-
token, reSelect)))
1444+
token, reSelect, shouldClearExistingResults)))
14441445
{
14451446
App.API.LogError(ClassName, "Unable to add item to Result Update Queue");
14461447
}
@@ -1542,25 +1543,36 @@ private async Task BuildQueryAsync(IEnumerable<BaseBuiltinShortcutModel> builtIn
15421543
}
15431544
}
15441545

1545-
private void RemoveOldQueryResults(Query query, bool isHomeQuery)
1546+
/// <summary>
1547+
/// Determines whether the existing search results should be cleared based on the current query and the previous query type.
1548+
/// This is needed because of the design that treats plugins with action keywords and global action keywords separately. Results are gathered
1549+
/// either from plugins with matching action keywords or global action keyword, but not both. So when the current results are from plugins
1550+
/// with a matching action keyword and a new result set comes from a new query with the global action keyword, the existing results need to be cleared,
1551+
/// and vice versa. The same applies to home page query results.
1552+
///
1553+
/// There is no need to clear results from global action keyword if a new set of results comes along that is also from global action keywords.
1554+
/// This is because the removal of obsolete results is handled in ResultsViewModel.NewResults(ICollection<ResultsForUpdate>).
1555+
/// </summary>
1556+
/// <param name="query">The current query.</param>
1557+
/// <param name="currentIsHomeQuery">A flag indicating if the current query is a home query.</param>
1558+
/// <returns>True if the existing results should be cleared, false otherwise.</returns>
1559+
private bool ShouldClearExistingResults(Query query, bool currentIsHomeQuery)
15461560
{
1547-
// If last and current query are home query, we don't need to clear the results
1548-
if (_lastIsHomeQuery && isHomeQuery)
1561+
// If previous or current results are from home query, we need to clear them
1562+
if (_previousIsHomeQuery || currentIsHomeQuery)
15491563
{
1550-
return;
1564+
App.API.LogDebug(ClassName, $"Cleared old results");
1565+
return true;
15511566
}
1552-
// If last or current query is home query, we need to clear the results
1553-
else if (_lastIsHomeQuery || isHomeQuery)
1554-
{
1555-
App.API.LogDebug(ClassName, $"Remove old results");
1556-
Results.Clear();
1557-
}
1558-
// If last and current query are not home query, we need to check action keyword
1559-
else if (_lastQuery?.ActionKeyword != query?.ActionKeyword)
1567+
1568+
// If the last and current query are not home query type, we need to check the action keyword
1569+
if (_lastQuery?.ActionKeyword != query?.ActionKeyword)
15601570
{
1561-
App.API.LogDebug(ClassName, $"Remove old results");
1562-
Results.Clear();
1571+
App.API.LogDebug(ClassName, $"Cleared old results");
1572+
return true;
15631573
}
1574+
1575+
return false;
15641576
}
15651577

15661578
private Result ContextMenuTopMost(Result result)

Flow.Launcher/ViewModel/ResultsForUpdate.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ public record struct ResultsForUpdate(
99
PluginMetadata Metadata,
1010
Query Query,
1111
CancellationToken Token,
12-
bool ReSelectFirstResult = true)
12+
bool ReSelectFirstResult = true,
13+
bool shouldClearExistingResults = false)
1314
{
1415
public string ID { get; } = Metadata.ID;
1516
}

Flow.Launcher/ViewModel/ResultsViewModel.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -232,10 +232,15 @@ private List<ResultViewModel> NewResults(ICollection<ResultsForUpdate> resultsFo
232232
if (!resultsForUpdates.Any())
233233
return Results;
234234

235+
var newResults = resultsForUpdates.SelectMany(u => u.Results, (u, r) => new ResultViewModel(r, _settings));
236+
237+
if (resultsForUpdates.Any(x => x.shouldClearExistingResults))
238+
return newResults.OrderByDescending(rv => rv.Result.Score).ToList();
239+
235240
return Results.Where(r => r?.Result != null && resultsForUpdates.All(u => u.ID != r.Result.PluginID))
236-
.Concat(resultsForUpdates.SelectMany(u => u.Results, (u, r) => new ResultViewModel(r, _settings)))
237-
.OrderByDescending(rv => rv.Result.Score)
238-
.ToList();
241+
.Concat(newResults)
242+
.OrderByDescending(rv => rv.Result.Score)
243+
.ToList();
239244
}
240245
#endregion
241246

0 commit comments

Comments
 (0)