Skip to content

Commit 4353e98

Browse files
committed
Added search suggestions to Omnibar
1 parent 101a91f commit 4353e98

File tree

6 files changed

+91
-2
lines changed

6 files changed

+91
-2
lines changed

src/Files.App/Data/Contracts/IGeneralSettingsService.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ public interface IGeneralSettingsService : IBaseSettingsService, INotifyProperty
5050
/// </summary>
5151
List<string> PathHistoryList { get; set; }
5252

53+
/// <summary>
54+
/// A list containing previous search queries.
55+
/// </summary>
56+
List<string> PreviousSearchQueriesList { get; set; }
57+
5358
/// <summary>
5459
/// Gets or sets a value indicating which date and time format to use.
5560
/// </summary>

src/Files.App/Services/Settings/GeneralSettingsService.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ public List<string> PathHistoryList
6565
set => Set(value);
6666
}
6767

68+
public List<string> PreviousSearchQueriesList
69+
{
70+
get => Get<List<string>>(null);
71+
set => Set(value);
72+
}
73+
6874
public DateTimeFormats DateTimeFormat
6975
{
7076
get => Get(DateTimeFormats.Application);

src/Files.App/Services/Settings/UserSettingsService.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ public override object ExportSettings()
7474
export.Remove(nameof(GeneralSettingsService.LastSessionTabList));
7575
export.Remove(nameof(GeneralSettingsService.LastCrashedTabList));
7676
export.Remove(nameof(GeneralSettingsService.PathHistoryList));
77+
export.Remove(nameof(GeneralSettingsService.PreviousSearchQueriesList));
7778

7879
return JsonSettingsSerializer.SerializeToJson(export);
7980
}

src/Files.App/UserControls/NavigationToolbar.xaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@
3030
<converters:NullToVisibilityCollapsedConverter x:Key="NullToVisibilityCollapsedConverter" />
3131
<converters1:BoolNegationConverter x:Key="BoolNegationConverter" />
3232
<converters:VisibilityInvertConverter x:Key="VisibilityInvertConverter" />
33+
<converters1:BoolToObjectConverter
34+
x:Key="SearchSuggestionGlyphConverter"
35+
FalseValue="&#xE7C3;"
36+
TrueValue="&#xE81C;" />
3337

3438
<ResourceDictionary.MergedDictionaries>
3539
<ResourceDictionary Source="ms-appx:///UserControls/KeyboardShortcut/KeyboardShortcut.xaml" />

src/Files.App/UserControls/NavigationToolbar.xaml.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,19 @@ await DialogDisplayHelper.ShowDialogAsync(Strings.InvalidCommand.GetLocalizedRes
316316
// Search mode
317317
else if (mode == OmnibarSearchMode)
318318
{
319-
ContentPageContext.ShellPage?.SubmitSearch(args.Text);
319+
var shellPage = ContentPageContext.ShellPage;
320+
321+
if (args.Item is SuggestionModel item && !string.IsNullOrWhiteSpace(item.ItemPath) && shellPage is not null)
322+
await NavigationHelpers.OpenPath(item.ItemPath, shellPage);
323+
else
324+
{
325+
var searchQuery = args.Item is SuggestionModel x && !string.IsNullOrWhiteSpace(x.Name)
326+
? x.Name
327+
: args.Text;
328+
329+
shellPage?.SubmitSearch(searchQuery); // use the resolved shellPage for consistency
330+
ViewModel.SaveSearchQueryToList(searchQuery);
331+
}
320332
}
321333
}
322334

@@ -338,6 +350,7 @@ await DispatcherQueue.EnqueueOrInvokeAsync(() =>
338350
}
339351
else if (Omnibar.CurrentSelectedMode == OmnibarSearchMode)
340352
{
353+
await ViewModel.PopulateOmnibarSuggestionsForSearchMode();
341354
}
342355
}
343356

@@ -456,7 +469,12 @@ await DispatcherQueue.EnqueueOrInvokeAsync(() =>
456469
}
457470
else if (e.NewMode == OmnibarSearchMode)
458471
{
472+
if (!ViewModel.InstanceViewModel.IsPageTypeSearchResults)
473+
ViewModel.OmnibarSearchModeText = string.Empty;
474+
else
475+
ViewModel.OmnibarSearchModeText = ViewModel.InstanceViewModel.CurrentSearchQuery;
459476

477+
await ViewModel.PopulateOmnibarSuggestionsForSearchMode();
460478
}
461479
}
462480

@@ -486,7 +504,7 @@ await DispatcherQueue.EnqueueOrInvokeAsync(() =>
486504
}
487505
else if (Omnibar.CurrentSelectedMode == OmnibarSearchMode)
488506
{
489-
507+
await ViewModel.PopulateOmnibarSuggestionsForSearchMode();
490508
}
491509
}
492510
}

src/Files.App/ViewModels/UserControls/NavigationToolbarViewModel.cs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1035,6 +1035,18 @@ private void SavePathToHistory(string path)
10351035
UserSettingsService.GeneralSettingsService.PathHistoryList = pathHistoryList;
10361036
}
10371037

1038+
public void SaveSearchQueryToList(string searchQuery)
1039+
{
1040+
var previousSearchQueriesList = UserSettingsService.GeneralSettingsService.PreviousSearchQueriesList?.ToList() ?? [];
1041+
previousSearchQueriesList.Remove(searchQuery);
1042+
previousSearchQueriesList.Insert(0, searchQuery);
1043+
1044+
if (previousSearchQueriesList.Count > MaxSuggestionsCount)
1045+
UserSettingsService.GeneralSettingsService.PreviousSearchQueriesList = previousSearchQueriesList.RemoveFrom(MaxSuggestionsCount + 1);
1046+
else
1047+
UserSettingsService.GeneralSettingsService.PreviousSearchQueriesList = previousSearchQueriesList;
1048+
}
1049+
10381050
private static async Task<bool> LaunchApplicationFromPath(string currentInput, string workingDir)
10391051
{
10401052
var args = CommandLineParser.SplitArguments(currentInput);
@@ -1243,6 +1255,49 @@ public void PopulateOmnibarSuggestionsForCommandPaletteMode()
12431255
}
12441256
}
12451257

1258+
public async Task PopulateOmnibarSuggestionsForSearchMode()
1259+
{
1260+
if (ContentPageContext.ShellPage is null)
1261+
return;
1262+
1263+
List<SuggestionModel> newSuggestions = [];
1264+
1265+
if (string.IsNullOrWhiteSpace(OmnibarSearchModeText))
1266+
{
1267+
var previousSearchQueries = UserSettingsService.GeneralSettingsService.PreviousSearchQueriesList;
1268+
if (previousSearchQueries is not null)
1269+
newSuggestions.AddRange(previousSearchQueries.Select(query => new SuggestionModel(query, true)));
1270+
}
1271+
else
1272+
{
1273+
var search = new FolderSearch
1274+
{
1275+
Query = OmnibarSearchModeText,
1276+
Folder = ContentPageContext.ShellPage.ShellViewModel.WorkingDirectory,
1277+
MaxItemCount = 10,
1278+
};
1279+
1280+
var results = await search.SearchAsync();
1281+
newSuggestions.AddRange(results.Select(result => new SuggestionModel(result)));
1282+
}
1283+
1284+
// Remove outdated suggestions
1285+
var toRemove = OmnibarSearchModeSuggestionItems
1286+
.Where(existing => !newSuggestions.Any(newItem => newItem.ItemPath == existing.ItemPath))
1287+
.ToList();
1288+
1289+
foreach (var item in toRemove)
1290+
OmnibarSearchModeSuggestionItems.Remove(item);
1291+
1292+
// Add new suggestions
1293+
var toAdd = newSuggestions
1294+
.Where(newItem => !OmnibarSearchModeSuggestionItems.Any(existing => existing.Name == newItem.Name));
1295+
1296+
foreach (var item in toAdd)
1297+
OmnibarSearchModeSuggestionItems.Add(item);
1298+
}
1299+
1300+
12461301
[Obsolete("Remove once Omnibar goes out of experimental.")]
12471302
public async Task SetAddressBarSuggestionsAsync(AutoSuggestBox sender, IShellPage shellpage)
12481303
{

0 commit comments

Comments
 (0)