Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions src/Files.App/UserControls/NavigationToolbar.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
5 changes: 3 additions & 2 deletions src/Files.App/Utils/Storage/Search/FolderSearch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ public async Task SearchAsync(IList<ListedItem> results, CancellationToken token
}
catch (OperationCanceledException)
{
return;
}
catch (Exception e)
{
Expand Down Expand Up @@ -149,7 +150,7 @@ private async Task SearchAsync(BaseStorageFolder folder, IList<ListedItem> 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)
{
Expand Down Expand Up @@ -178,7 +179,7 @@ private async Task SearchAsync(BaseStorageFolder folder, IList<ListedItem> 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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -1127,6 +1128,10 @@ public async Task PopulateOmnibarSuggestionsForSearchMode()
return;
}

_suggestSearchCTS.Cancel();
_suggestSearchCTS = new CancellationTokenSource();
var token = _suggestSearchCTS.Token;

List<SuggestionModel> newSuggestions = [];

if (string.IsNullOrWhiteSpace(OmnibarSearchModeText))
Expand All @@ -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<ListedItem>();
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))
Expand Down Expand Up @@ -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;
}
}
Expand Down
Loading