From 85c43caf376930f999a84c77719278a94d9e6594 Mon Sep 17 00:00:00 2001 From: Saravanan G Date: Thu, 25 Jun 2026 22:37:07 +0530 Subject: [PATCH] Fix: Fixed issue where search queries continued running after exiting search or closing a tab --- .../UserControls/NavigationToolbar.xaml.cs | 5 +++ .../Utils/Storage/Search/FolderSearch.cs | 5 ++- .../NavigationToolbarViewModel.cs | 39 +++++++++++++++---- 3 files changed, 40 insertions(+), 9 deletions(-) diff --git a/src/Files.App/UserControls/NavigationToolbar.xaml.cs b/src/Files.App/UserControls/NavigationToolbar.xaml.cs index 7610789746b2..746d31075c4e 100644 --- a/src/Files.App/UserControls/NavigationToolbar.xaml.cs +++ b/src/Files.App/UserControls/NavigationToolbar.xaml.cs @@ -422,6 +422,11 @@ private async void Omnibar_IsFocusedChanged(Omnibar sender, OmnibarIsFocusedChan } else { + if (Omnibar.CurrentSelectedMode == OmnibarSearchMode) + { + ViewModel?.CancelSuggestionSearch(); + } + // When Omnibar loses focus, revert to Path Mode to display BreadcrumbBar Omnibar.CurrentSelectedMode = OmnibarPathMode; } diff --git a/src/Files.App/Utils/Storage/Search/FolderSearch.cs b/src/Files.App/Utils/Storage/Search/FolderSearch.cs index ab27e611fc8e..dcb61192fffd 100644 --- a/src/Files.App/Utils/Storage/Search/FolderSearch.cs +++ b/src/Files.App/Utils/Storage/Search/FolderSearch.cs @@ -92,6 +92,7 @@ public async Task SearchAsync(IList results, CancellationToken token } catch (OperationCanceledException) { + return; } catch (Exception e) { @@ -149,7 +150,7 @@ private async Task SearchAsync(BaseStorageFolder folder, IList resul var options = ToQueryOptions(); var queryResult = folder.CreateItemQueryWithOptions(options); - var items = await queryResult.GetItemsAsync(0, stepSize); + var items = await queryResult.GetItemsAsync(0, stepSize).AsTask(token); while (items.Count > 0) { @@ -178,7 +179,7 @@ private async Task SearchAsync(BaseStorageFolder folder, IList resul index += (uint)items.Count; stepSize = Math.Min(defaultStepSize, UsedMaxItemCount - (uint)results.Count); - items = await queryResult.GetItemsAsync(index, stepSize); + items = await queryResult.GetItemsAsync(index, stepSize).AsTask(token); } } diff --git a/src/Files.App/ViewModels/UserControls/NavigationToolbarViewModel.cs b/src/Files.App/ViewModels/UserControls/NavigationToolbarViewModel.cs index d6e260fc47ee..905c6bb3d029 100644 --- a/src/Files.App/ViewModels/UserControls/NavigationToolbarViewModel.cs +++ b/src/Files.App/ViewModels/UserControls/NavigationToolbarViewModel.cs @@ -44,6 +44,7 @@ public sealed partial class NavigationToolbarViewModel : ObservableObject, IAddr private string? _dragOverPath; private bool _lockFlag; private PointerRoutedEventArgs? _pointerRoutedEventArgs; + private CancellationTokenSource _suggestSearchCTS = new(); // Events @@ -1127,6 +1128,10 @@ public async Task PopulateOmnibarSuggestionsForSearchMode() return; } + _suggestSearchCTS.Cancel(); + _suggestSearchCTS = new CancellationTokenSource(); + var token = _suggestSearchCTS.Token; + List newSuggestions = []; if (string.IsNullOrWhiteSpace(OmnibarSearchModeText)) @@ -1137,17 +1142,30 @@ public async Task PopulateOmnibarSuggestionsForSearchMode() } else { - var search = new FolderSearch + try { - Query = OmnibarSearchModeText, - Folder = ContentPageContext.ShellPage.ShellViewModel.WorkingDirectory, - MaxItemCount = 10, - }; + await Task.Delay(200, token); - var results = await search.SearchAsync(); - newSuggestions.AddRange(results.Select(result => new SuggestionModel(result))); + var search = new FolderSearch + { + Query = OmnibarSearchModeText, + Folder = ContentPageContext.ShellPage.ShellViewModel.WorkingDirectory, + MaxItemCount = 10, + }; + + var results = new List(); + await search.SearchAsync(results, token); + newSuggestions.AddRange(results.Select(result => new SuggestionModel(result))); + } + catch (OperationCanceledException) + { + return; + } } + if (token.IsCancellationRequested) + return; + // Remove outdated suggestions var toRemove = OmnibarSearchModeSuggestionItems .Where(existing => !newSuggestions.Any(newItem => newItem.ItemPath == existing.ItemPath)) @@ -1211,10 +1229,17 @@ private void FolderSettings_PropertyChanged(object? sender, PropertyChangedEvent } } + public void CancelSuggestionSearch() + { + _suggestSearchCTS.Cancel(); + } + // Disposer public void Dispose() { + _suggestSearchCTS.Cancel(); + _suggestSearchCTS.Dispose(); UserSettingsService.OnSettingChangedEvent -= UserSettingsService_OnSettingChangedEvent; } }