Skip to content

Commit 3abb2f0

Browse files
authored
Merge pull request #3553 from Flow-Launcher/fix_result_clearing_nonquery
Fix result clearing for non-query calls and home page + history results page toggling
2 parents 60de423 + 2770c6a commit 3abb2f0

File tree

4 files changed

+74
-27
lines changed

4 files changed

+74
-27
lines changed

Flow.Launcher.Infrastructure/UserSettings/Settings.cs

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,20 @@ public bool ShowHomePage
173173
}
174174
}
175175

176-
public bool ShowHistoryResultsForHomePage { get; set; } = false;
176+
private bool _showHistoryResultsForHomePage = false;
177+
public bool ShowHistoryResultsForHomePage
178+
{
179+
get => _showHistoryResultsForHomePage;
180+
set
181+
{
182+
if (_showHistoryResultsForHomePage != value)
183+
{
184+
_showHistoryResultsForHomePage = value;
185+
OnPropertyChanged();
186+
}
187+
}
188+
}
189+
177190
public int MaxHistoryResultsToShowForHomePage { get; set; } = 5;
178191

179192
public int CustomExplorerIndex { get; set; } = 0;
@@ -395,29 +408,29 @@ public List<RegisteredHotkeyData> RegisteredHotkeys
395408
var list = FixedHotkeys();
396409

397410
// Customizeable hotkeys
398-
if(!string.IsNullOrEmpty(Hotkey))
411+
if (!string.IsNullOrEmpty(Hotkey))
399412
list.Add(new(Hotkey, "flowlauncherHotkey", () => Hotkey = ""));
400-
if(!string.IsNullOrEmpty(PreviewHotkey))
413+
if (!string.IsNullOrEmpty(PreviewHotkey))
401414
list.Add(new(PreviewHotkey, "previewHotkey", () => PreviewHotkey = ""));
402-
if(!string.IsNullOrEmpty(AutoCompleteHotkey))
415+
if (!string.IsNullOrEmpty(AutoCompleteHotkey))
403416
list.Add(new(AutoCompleteHotkey, "autoCompleteHotkey", () => AutoCompleteHotkey = ""));
404-
if(!string.IsNullOrEmpty(AutoCompleteHotkey2))
417+
if (!string.IsNullOrEmpty(AutoCompleteHotkey2))
405418
list.Add(new(AutoCompleteHotkey2, "autoCompleteHotkey", () => AutoCompleteHotkey2 = ""));
406-
if(!string.IsNullOrEmpty(SelectNextItemHotkey))
419+
if (!string.IsNullOrEmpty(SelectNextItemHotkey))
407420
list.Add(new(SelectNextItemHotkey, "SelectNextItemHotkey", () => SelectNextItemHotkey = ""));
408-
if(!string.IsNullOrEmpty(SelectNextItemHotkey2))
421+
if (!string.IsNullOrEmpty(SelectNextItemHotkey2))
409422
list.Add(new(SelectNextItemHotkey2, "SelectNextItemHotkey", () => SelectNextItemHotkey2 = ""));
410-
if(!string.IsNullOrEmpty(SelectPrevItemHotkey))
423+
if (!string.IsNullOrEmpty(SelectPrevItemHotkey))
411424
list.Add(new(SelectPrevItemHotkey, "SelectPrevItemHotkey", () => SelectPrevItemHotkey = ""));
412-
if(!string.IsNullOrEmpty(SelectPrevItemHotkey2))
425+
if (!string.IsNullOrEmpty(SelectPrevItemHotkey2))
413426
list.Add(new(SelectPrevItemHotkey2, "SelectPrevItemHotkey", () => SelectPrevItemHotkey2 = ""));
414-
if(!string.IsNullOrEmpty(SettingWindowHotkey))
427+
if (!string.IsNullOrEmpty(SettingWindowHotkey))
415428
list.Add(new(SettingWindowHotkey, "SettingWindowHotkey", () => SettingWindowHotkey = ""));
416-
if(!string.IsNullOrEmpty(OpenContextMenuHotkey))
429+
if (!string.IsNullOrEmpty(OpenContextMenuHotkey))
417430
list.Add(new(OpenContextMenuHotkey, "OpenContextMenuHotkey", () => OpenContextMenuHotkey = ""));
418-
if(!string.IsNullOrEmpty(SelectNextPageHotkey))
431+
if (!string.IsNullOrEmpty(SelectNextPageHotkey))
419432
list.Add(new(SelectNextPageHotkey, "SelectNextPageHotkey", () => SelectNextPageHotkey = ""));
420-
if(!string.IsNullOrEmpty(SelectPrevPageHotkey))
433+
if (!string.IsNullOrEmpty(SelectPrevPageHotkey))
421434
list.Add(new(SelectPrevPageHotkey, "SelectPrevPageHotkey", () => SelectPrevPageHotkey = ""));
422435
if (!string.IsNullOrEmpty(CycleHistoryUpHotkey))
423436
list.Add(new(CycleHistoryUpHotkey, "CycleHistoryUpHotkey", () => CycleHistoryUpHotkey = ""));

Flow.Launcher/MainWindow.xaml.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ private void OnSourceInitialized(object sender, EventArgs e)
113113
Win32Helper.DisableControlBox(this);
114114
}
115115

116-
private async void OnLoaded(object sender, RoutedEventArgs _)
116+
private void OnLoaded(object sender, RoutedEventArgs _)
117117
{
118118
// Check first launch
119119
if (_settings.FirstLaunch)
@@ -283,6 +283,7 @@ private async void OnLoaded(object sender, RoutedEventArgs _)
283283
InitializeContextMenu();
284284
break;
285285
case nameof(Settings.ShowHomePage):
286+
case nameof(Settings.ShowHistoryResultsForHomePage):
286287
if (_viewModel.QueryResultsSelected() && string.IsNullOrEmpty(_viewModel.QueryText))
287288
{
288289
_viewModel.QueryResults();

Flow.Launcher/ViewModel/MainViewModel.cs

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ public void RegisterResultsUpdatedEvent()
265265

266266
if (token.IsCancellationRequested) return;
267267

268-
if (!_resultsUpdateChannelWriter.TryWrite(new ResultsForUpdate(resultsCopy, pair.Metadata, e.Query,
268+
if (!_resultsUpdateChannelWriter.TryWrite(new ResultsForUpdate(resultsCopy, pair.Metadata, e.Query,
269269
token)))
270270
{
271271
App.API.LogError(ClassName, "Unable to add item to Result Update Queue");
@@ -791,7 +791,7 @@ private ResultsViewModel SelectedResults
791791

792792
public Visibility ProgressBarVisibility { get; set; }
793793
public Visibility MainWindowVisibility { get; set; }
794-
794+
795795
// This is to be used for determining the visibility status of the main window instead of MainWindowVisibility
796796
// because it is more accurate and reliable representation than using Visibility as a condition check
797797
public bool MainWindowVisibilityStatus { get; set; } = true;
@@ -1068,7 +1068,7 @@ private bool CanExternalPreviewSelectedResult(out string path)
10681068
path = QueryResultsPreviewed() ? Results.SelectedItem?.Result?.Preview.FilePath : string.Empty;
10691069
return !string.IsNullOrEmpty(path);
10701070
}
1071-
1071+
10721072
private bool QueryResultsPreviewed()
10731073
{
10741074
var previewed = PreviewSelectedItem == Results.SelectedItem;
@@ -1278,8 +1278,6 @@ private async Task QueryResultsAsync(bool searchDelay, bool isReQuery = false, b
12781278
// Update the query's IsReQuery property to true if this is a re-query
12791279
query.IsReQuery = isReQuery;
12801280

1281-
1282-
12831281
ICollection<PluginPair> plugins = Array.Empty<PluginPair>();
12841282
if (currentIsHomeQuery)
12851283
{
@@ -1310,8 +1308,7 @@ private async Task QueryResultsAsync(bool searchDelay, bool isReQuery = false, b
13101308
}
13111309
}
13121310

1313-
var validPluginNames = plugins.Select(x => $"<{x.Metadata.Name}>");
1314-
App.API.LogDebug(ClassName, $"Valid <{plugins.Count}> plugins: {string.Join(" ", validPluginNames)}");
1311+
App.API.LogDebug(ClassName, $"Valid <{plugins.Count}> plugins: {string.Join(" ", plugins.Select(x => $"<{x.Metadata.Name}>"))}");
13151312

13161313
// Do not wait for performance improvement
13171314
/*if (string.IsNullOrEmpty(query.ActionKeyword))
@@ -1339,6 +1336,12 @@ private async Task QueryResultsAsync(bool searchDelay, bool isReQuery = false, b
13391336
Task[] tasks;
13401337
if (currentIsHomeQuery)
13411338
{
1339+
if (ShouldClearExistingResultsForNonQuery(plugins))
1340+
{
1341+
Results.Clear();
1342+
App.API.LogDebug(ClassName, $"Existing results are cleared for non-query");
1343+
}
1344+
13421345
tasks = plugins.Select(plugin => plugin.Metadata.HomeDisabled switch
13431346
{
13441347
false => QueryTaskAsync(plugin, currentCancellationToken),
@@ -1430,7 +1433,7 @@ await PluginManager.QueryHomeForPluginAsync(plugin, query, token) :
14301433
App.API.LogDebug(ClassName, $"Update results for plugin <{plugin.Metadata.Name}>");
14311434

14321435
// Indicate if to clear existing results so to show only ones from plugins with action keywords
1433-
var shouldClearExistingResults = ShouldClearExistingResults(query, currentIsHomeQuery);
1436+
var shouldClearExistingResults = ShouldClearExistingResultsForQuery(query, currentIsHomeQuery);
14341437
_lastQuery = query;
14351438
_previousIsHomeQuery = currentIsHomeQuery;
14361439

@@ -1452,8 +1455,13 @@ void QueryHistoryTask(CancellationToken token)
14521455

14531456
App.API.LogDebug(ClassName, $"Update results for history");
14541457

1458+
// Indicate if to clear existing results so to show only ones from plugins with action keywords
1459+
var shouldClearExistingResults = ShouldClearExistingResultsForQuery(query, currentIsHomeQuery);
1460+
_lastQuery = query;
1461+
_previousIsHomeQuery = currentIsHomeQuery;
1462+
14551463
if (!_resultsUpdateChannelWriter.TryWrite(new ResultsForUpdate(results, _historyMetadata, query,
1456-
token)))
1464+
token, reSelect, shouldClearExistingResults)))
14571465
{
14581466
App.API.LogError(ClassName, "Unable to add item to Result Update Queue");
14591467
}
@@ -1539,7 +1547,9 @@ private async Task BuildQueryAsync(IEnumerable<BaseBuiltinShortcutModel> builtIn
15391547

15401548
/// <summary>
15411549
/// Determines whether the existing search results should be cleared based on the current query and the previous query type.
1542-
/// This is needed because of the design that treats plugins with action keywords and global action keywords separately. Results are gathered
1550+
/// This is used to indicate to QueryTaskAsync or QueryHistoryTask whether to clear results. If both QueryTaskAsync and QueryHistoryTask
1551+
/// are not called then use ShouldClearExistingResultsForNonQuery instead.
1552+
/// This method needed because of the design that treats plugins with action keywords and global action keywords separately. Results are gathered
15431553
/// either from plugins with matching action keywords or global action keyword, but not both. So when the current results are from plugins
15441554
/// 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,
15451555
/// and vice versa. The same applies to home page query results.
@@ -1550,19 +1560,39 @@ private async Task BuildQueryAsync(IEnumerable<BaseBuiltinShortcutModel> builtIn
15501560
/// <param name="query">The current query.</param>
15511561
/// <param name="currentIsHomeQuery">A flag indicating if the current query is a home query.</param>
15521562
/// <returns>True if the existing results should be cleared, false otherwise.</returns>
1553-
private bool ShouldClearExistingResults(Query query, bool currentIsHomeQuery)
1563+
private bool ShouldClearExistingResultsForQuery(Query query, bool currentIsHomeQuery)
15541564
{
15551565
// If previous or current results are from home query, we need to clear them
15561566
if (_previousIsHomeQuery || currentIsHomeQuery)
15571567
{
1558-
App.API.LogDebug(ClassName, $"Cleared old results");
1568+
App.API.LogDebug(ClassName, $"Existing results should be cleared for query");
15591569
return true;
15601570
}
15611571

15621572
// If the last and current query are not home query type, we need to check the action keyword
15631573
if (_lastQuery?.ActionKeyword != query?.ActionKeyword)
15641574
{
1565-
App.API.LogDebug(ClassName, $"Cleared old results");
1575+
App.API.LogDebug(ClassName, $"Existing results should be cleared for query");
1576+
return true;
1577+
}
1578+
1579+
return false;
1580+
}
1581+
1582+
/// <summary>
1583+
/// Determines whether existing results should be cleared for non-query calls.
1584+
/// A non-query call is where QueryTaskAsync and QueryHistoryTask methods are both not called.
1585+
/// QueryTaskAsync and QueryHistoryTask both handle result updating (clearing if required) so directly calling
1586+
/// Results.Clear() is not required. However when both are not called, we need to directly clear results and this
1587+
/// method determines on the condition when clear results should happen.
1588+
/// </summary>
1589+
/// <param name="plugins">The collection of plugins to check.</param>
1590+
/// <returns>True if existing results should be cleared, false otherwise.</returns>
1591+
private bool ShouldClearExistingResultsForNonQuery(ICollection<PluginPair> plugins)
1592+
{
1593+
if (!Settings.ShowHistoryResultsForHomePage && (plugins.Count == 0 || plugins.All(x => x.Metadata.HomeDisabled == true)))
1594+
{
1595+
App.API.LogDebug(ClassName, $"Existing results should be cleared for non-query");
15661596
return true;
15671597
}
15681598

Flow.Launcher/ViewModel/ResultsViewModel.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,10 @@ private List<ResultViewModel> NewResults(ICollection<ResultsForUpdate> resultsFo
245245
var newResults = resultsForUpdates.SelectMany(u => u.Results, (u, r) => new ResultViewModel(r, _settings));
246246

247247
if (resultsForUpdates.Any(x => x.shouldClearExistingResults))
248+
{
249+
App.API.LogDebug("NewResults", $"Existing results are cleared for query");
248250
return newResults.OrderByDescending(rv => rv.Result.Score).ToList();
251+
}
249252

250253
return Results.Where(r => r?.Result != null && resultsForUpdates.All(u => u.ID != r.Result.PluginID))
251254
.Concat(newResults)

0 commit comments

Comments
 (0)