Skip to content

Feature: Highlight search string in the command palette #13095

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 28, 2023
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
66 changes: 63 additions & 3 deletions src/Files.App/Data/Items/NavigationBarSuggestionItem.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright (c) 2023 Files Community
// Licensed under the MIT License. See the LICENSE.

using Microsoft.UI.Xaml.Media;

namespace Files.App.Data.Items
{
public class NavigationBarSuggestionItem : ObservableObject
Expand All @@ -18,7 +16,43 @@ public string? Text
public string? PrimaryDisplay
{
get => _PrimaryDisplay;
set => SetProperty(ref _PrimaryDisplay, value);
set
{
if (SetProperty(ref _PrimaryDisplay, value))
UpdatePrimaryDisplay();
}
}

private string? _SearchText;
public string? SearchText
{
get => _SearchText;
set
{
if (SetProperty(ref _SearchText, value))
UpdatePrimaryDisplay();
}
}

private string? _PrimaryDisplayPreMatched;
public string? PrimaryDisplayPreMatched
{
get => _PrimaryDisplayPreMatched;
private set => SetProperty(ref _PrimaryDisplayPreMatched, value);
}

private string? _PrimaryDisplayMatched;
public string? PrimaryDisplayMatched
{
get => _PrimaryDisplayMatched;
private set => SetProperty(ref _PrimaryDisplayMatched, value);
}

private string? _PrimaryDisplayPostMatched;
public string? PrimaryDisplayPostMatched
{
get => _PrimaryDisplayPostMatched;
private set => SetProperty(ref _PrimaryDisplayPostMatched, value);
}

private string? _SecondaryDisplay;
Expand All @@ -34,5 +68,31 @@ public string? SupplementaryDisplay
get => _SupplementaryDisplay;
set => SetProperty(ref _SupplementaryDisplay, value);
}

private void UpdatePrimaryDisplay()
{
if (SearchText is null || PrimaryDisplay is null)
{
PrimaryDisplayPreMatched = null;
PrimaryDisplayMatched = PrimaryDisplay;
PrimaryDisplayPostMatched = null;
}
else
{
var index = PrimaryDisplay.IndexOf(SearchText, StringComparison.OrdinalIgnoreCase);
if (index < 0)
{
PrimaryDisplayPreMatched = PrimaryDisplay;
PrimaryDisplayMatched = null;
PrimaryDisplayPostMatched = null;
}
else
{
PrimaryDisplayPreMatched = PrimaryDisplay.Substring(0, index);
PrimaryDisplayMatched = PrimaryDisplay.Substring(index, SearchText.Length);
PrimaryDisplayPostMatched = PrimaryDisplay.Substring(index + SearchText.Length);
}
}
}
}
}
7 changes: 3 additions & 4 deletions src/Files.App/UserControls/AddressToolbar.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,9 @@

<!-- Primary Display -->
<!-- This is used to display paths or command descriptions. -->
<TextBlock
Grid.Column="0"
Foreground="{ThemeResource TextFillColorPrimaryBrush}"
Text="{x:Bind PrimaryDisplay, Mode=OneWay}" />
<TextBlock Grid.Column="0" Foreground="{ThemeResource TextFillColorPrimaryBrush}">
<Run FontWeight="Normal" Text="{x:Bind PrimaryDisplayPreMatched, Mode=OneWay}" /><Run FontWeight="Bold" Text="{x:Bind PrimaryDisplayMatched, Mode=OneWay}" /><Run FontWeight="Normal" Text="{x:Bind PrimaryDisplayPostMatched, Mode=OneWay}" />
</TextBlock>

<!-- Supplementary Display -->
<!-- This is used to display command hotkeys. -->
Expand Down
12 changes: 9 additions & 3 deletions src/Files.App/ViewModels/UserControls/ToolbarViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media;
using System.IO;
using System.Windows.Input;
using Windows.ApplicationModel.DataTransfer;
Expand Down Expand Up @@ -792,6 +791,7 @@ public async Task SetAddressBarSuggestions(AutoSuggestBox sender, IShellPage she
Text = ">" + command.Code,
PrimaryDisplay = command.Description,
SupplementaryDisplay = command.HotKeyText,
SearchText = searchText,
}).ToList();
}
else
Expand Down Expand Up @@ -849,6 +849,7 @@ public async Task SetAddressBarSuggestions(AutoSuggestBox sender, IShellPage she
NavigationBarSuggestions[index].PrimaryDisplay = suggestions[index].PrimaryDisplay;
NavigationBarSuggestions[index].SecondaryDisplay = suggestions[index].SecondaryDisplay;
NavigationBarSuggestions[index].SupplementaryDisplay = suggestions[index].SupplementaryDisplay;
NavigationBarSuggestions[index].SearchText = suggestions[index].SearchText;
}
else
{
Expand All @@ -865,8 +866,13 @@ public async Task SetAddressBarSuggestions(AutoSuggestBox sender, IShellPage she
foreach (var s in NavigationBarSuggestions.ExceptBy(suggestions, x => x.PrimaryDisplay).ToList())
NavigationBarSuggestions.Remove(s);

foreach (var s in suggestions.ExceptBy(NavigationBarSuggestions, x => x.PrimaryDisplay).ToList())
NavigationBarSuggestions.Insert(suggestions.IndexOf(s), s);
for (int index = 0; index < suggestions.Count; index++)
{
if (NavigationBarSuggestions.Count > index && NavigationBarSuggestions[index].PrimaryDisplay == suggestions[index].PrimaryDisplay)
NavigationBarSuggestions[index].SearchText = suggestions[index].SearchText;
else
NavigationBarSuggestions.Insert(index, suggestions[index]);
}
}

return true;
Expand Down