Skip to content

A Swiss Army knife for developers.

Search in the navigation view works

Etienne Baudoux6b8d64ea45c0
diff --git a/src/Directory.Build.props b/src/Directory.Build.props
index 08ffb3b4d..842d14d06 100644
--- a/src/Directory.Build.props
+++ b/src/Directory.Build.props
@@ -41,6 +41,7 @@
<NuGet_MicrosoftNETCoreUniversalWindowsPlatform>6.2.12</NuGet_MicrosoftNETCoreUniversalWindowsPlatform>
<NuGet_MicrosoftToolkitMvvm>7.0.2</NuGet_MicrosoftToolkitMvvm>
<NuGet_MicrosoftUIXaml>2.6.2</NuGet_MicrosoftUIXaml>
+ <Nuget_MicrosoftXamlBehaviorsUwpManaged>2.0.1</Nuget_MicrosoftXamlBehaviorsUwpManaged>
<NuGet_MicrosoftWindowsSDKBuildTools>10.0.19041.8</NuGet_MicrosoftWindowsSDKBuildTools>
<NuGet_MSTestTestAdapter>2.1.1</NuGet_MSTestTestAdapter>
<NuGet_MSTestTestFramework>2.1.1</NuGet_MSTestTestFramework>
diff --git a/src/dev/impl/DevTools.Impl/Core/UI/Extensions/HighlighterExtension.cs b/src/dev/impl/DevTools.Impl/Core/UI/Extensions/HighlighterExtension.cs
new file mode 100644
index 000000000..5439d39c4
--- /dev/null
+++ b/src/dev/impl/DevTools.Impl/Core/UI/Extensions/HighlighterExtension.cs
@@ -0,0 +1,79 @@
+#nullable enable
+
+using DevTools.Providers;
+using System;
+using System.Collections.Generic;
+using Windows.UI;
+using Windows.UI.Xaml;
+using Windows.UI.Xaml.Controls;
+using Windows.UI.Xaml.Data;
+using Windows.UI.Xaml.Documents;
+using Windows.UI.Xaml.Markup;
+using Windows.UI.Xaml.Media;
+
+namespace DevTools.Impl.Core.UI.Extensions
+{
+ [MarkupExtensionReturnType(ReturnType = typeof(IList<TextHighlighter>))]
+ [Bindable]
+ public sealed class HighlighterExtension : MarkupExtension
+ {
+ /// <summary>
+ /// Identifies the KeyboardAccelerator attachted property. This enables animation, styling, binding, etc...
+ /// </summary>
+ public static readonly DependencyProperty HighlightersProperty
+ = DependencyProperty.RegisterAttached(
+ "Highlighters",
+ typeof(MatchSpan[]),
+ typeof(HighlighterExtension),
+ new PropertyMetadata(Array.Empty<MatchSpan>(), OnHighlightersChanged));
+
+ /// <summary>
+ /// Gets the value of the Highlighters attached property from the specified FrameworkElement.
+ /// </summary>
+ public static object GetHighlighters(DependencyObject obj)
+ {
+ return obj.GetValue(HighlightersProperty);
+ }
+
+ /// <summary>
+ /// Sets the value of the Highlighters attached property to the specified FrameworkElement.
+ /// </summary>
+ /// <param name="obj">The object on which to set the KeyboardAccelerator attached property.</param>
+ /// <param name="value">The property value to set.</param>
+ public static void SetHighlighters(DependencyObject obj, object value)
+ {
+ obj.SetValue(HighlightersProperty, value);
+ }
+
+ /// <summary>
+ /// Highlighters changed handler.
+ /// </summary>
+ /// <param name="d">FrameworkElement that changed its KeyboardAccelerator attached property.</param>
+ /// <param name="e">DependencyPropertyChangedEventArgs with the new and old value.</param>
+ private static void OnHighlightersChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
+ {
+ if (d is RichTextBlock source)
+ {
+ var value = (MatchSpan[])e.NewValue;
+ source.TextHighlighters.Clear();
+
+ TextHighlighter highlighter = new TextHighlighter()
+ {
+ Background = new SolidColorBrush(Colors.Yellow)
+ };
+
+ for (int i = 0; i < value.Length; i++)
+ {
+ highlighter.Ranges.Add(
+ new TextRange
+ {
+ StartIndex = value[i].StartPosition,
+ Length = value[i].Length
+ });
+ }
+
+ source.TextHighlighters.Add(highlighter);
+ }
+ }
+ }
+}
diff --git a/src/dev/impl/DevTools.Impl/DevTools.Impl.csproj b/src/dev/impl/DevTools.Impl/DevTools.Impl.csproj
index 826cec9d0..193cc7fdb 100644
--- a/src/dev/impl/DevTools.Impl/DevTools.Impl.csproj
+++ b/src/dev/impl/DevTools.Impl/DevTools.Impl.csproj
@@ -74,6 +74,7 @@
<Compile Include="..\..\shared\SharedAssemblyInfo.cs">
<Link>Properties\SharedAssemblyInfo.cs</Link>
</Compile>
+ <Compile Include="Core\UI\Extensions\HighlighterExtension.cs" />
<Compile Include="Dummy.cs" />
<Compile Include="Models\MatchedToolProviderViewData.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
@@ -92,6 +93,7 @@
<PackageReference Include="Microsoft.NETCore.UniversalWindowsPlatform" Version="$(NuGet_MicrosoftNETCoreUniversalWindowsPlatform)" />
<PackageReference Include="Microsoft.Toolkit.Mvvm" Version="$(NuGet_MicrosoftToolkitMvvm)" />
<PackageReference Include="Microsoft.UI.Xaml" Version="$(NuGet_MicrosoftUIXaml)" />
+ <PackageReference Include="Microsoft.Xaml.Behaviors.Uwp.Managed" Version="$(Nuget_MicrosoftXamlBehaviorsUwpManaged)" />
<PackageReference Include="System.Composition" Version="$(NuGet_SystemComposition)" />
</ItemGroup>
<ItemGroup>
diff --git a/src/dev/impl/DevTools.Impl/ViewModels/MainPageViewModel.cs b/src/dev/impl/DevTools.Impl/ViewModels/MainPageViewModel.cs
index 80ddfbf54..649a2595b 100644
--- a/src/dev/impl/DevTools.Impl/ViewModels/MainPageViewModel.cs
+++ b/src/dev/impl/DevTools.Impl/ViewModels/MainPageViewModel.cs
@@ -3,6 +3,7 @@
using DevTools.Impl.Models;
using DevTools.Providers;
using Microsoft.Toolkit.Mvvm.ComponentModel;
+using Microsoft.Toolkit.Mvvm.Input;
using System.Collections.ObjectModel;
using System.Composition;
@@ -14,20 +15,44 @@ namespace DevTools.Impl.ViewModels
{
private readonly IToolProviderFactory _toolProviderFactory;
+ /// <summary>
+ /// Items at the top of the NavigationView.
+ /// </summary>
internal ObservableCollection<MatchedToolProviderViewData> ToolsMenuItems { get; } = new();
+ /// <summary>
+ /// Items at the bottom of the NavigationView. That includes Settings.
+ /// </summary>
internal ObservableCollection<MatchedToolProviderViewData> FooterMenuItems { get; } = new();
+ /// <summary>
+ /// The search query in the search bar.
+ /// </summary>
+ internal string? SearchQuery { get; set; }
+
[ImportingConstructor]
public MainPageViewModel(IToolProviderFactory toolProviderFactory)
{
_toolProviderFactory = toolProviderFactory;
+ SearchBoxTextChangedCommand = new RelayCommand(ExecuteSearchBoxTextChangedCommand);
+
SetFooterMenuItems();
SearchTools(searchQuery: string.Empty);
}
- private void SearchTools(string searchQuery)
+ #region SearchBoxTextChangedCommand
+
+ internal IRelayCommand SearchBoxTextChangedCommand { get; }
+
+ private void ExecuteSearchBoxTextChangedCommand()
+ {
+ SearchTools(SearchQuery);
+ }
+
+ #endregion
+
+ private void SearchTools(string? searchQuery)
{
ToolsMenuItems.Clear();
foreach (MatchedToolProvider matchedToolProvider in _toolProviderFactory.GetTools(searchQuery))
diff --git a/src/dev/impl/DevTools.Impl/Views/MainPage.xaml b/src/dev/impl/DevTools.Impl/Views/MainPage.xaml
index 1a5b6649b..f55603b4a 100644
--- a/src/dev/impl/DevTools.Impl/Views/MainPage.xaml
+++ b/src/dev/impl/DevTools.Impl/Views/MainPage.xaml
@@ -6,8 +6,10 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:muxc="using:Microsoft.UI.Xaml.Controls"
- xmlns:animatedvisuals="using:Microsoft.UI.Xaml.Controls.AnimatedVisuals"
xmlns:models="using:DevTools.Impl.Models"
+ xmlns:i="using:Microsoft.Xaml.Interactivity"
+ xmlns:core="using:Microsoft.Xaml.Interactions.Core"
+ xmlns:ex="using:DevTools.Impl.Core.UI.Extensions"
mc:Ignorable="d"
muxc:BackdropMaterial.ApplyToRootOrPageBackground="True">
<Page.Resources>
@@ -69,12 +71,41 @@
<muxc:NavigationView.MenuItemTemplate>
<DataTemplate x:DataType="models:MatchedToolProviderViewData">
- <muxc:NavigationViewItem Content="{x:Bind ToolProvider.DisplayName}" Icon="{x:Bind Icon}"/>
+ <muxc:NavigationViewItem Icon="{x:Bind Icon}">
+ <muxc:NavigationViewItem.Content>
+ <RichTextBlock
+ IsTextSelectionEnabled="False"
+ TextWrapping="NoWrap"
+ TextTrimming="CharacterEllipsis"
+ ex:HighlighterExtension.Highlighters="{x:Bind MatchedSpans}">
+ <Paragraph>
+ <Run Text="{x:Bind ToolProvider.DisplayName}"/>
+ </Paragraph>
+ </RichTextBlock>
+ </muxc:NavigationViewItem.Content>
+ </muxc:NavigationViewItem>
</DataTemplate>
</muxc:NavigationView.MenuItemTemplate>
<muxc:NavigationView.AutoSuggestBox>
- <AutoSuggestBox QueryIcon="Find" PlaceholderText="Search" />
+ <AutoSuggestBox
+ x:Name="SearchBox"
+ QueryIcon="Find"
+ PlaceholderText="Search"
+ Text="{x:Bind ViewModel.SearchQuery, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
+ <i:Interaction.Behaviors>
+ <core:EventTriggerBehavior EventName="TextChanged">
+ <core:InvokeCommandAction
+ Command="{x:Bind ViewModel.SearchBoxTextChangedCommand}"/>
+ </core:EventTriggerBehavior>
+ </i:Interaction.Behaviors>
+ <AutoSuggestBox.KeyboardAccelerators>
+ <KeyboardAccelerator
+ Key="F"
+ Modifiers="Control"
+ Invoked="SearchBoxKeyboardAccelerator_Invoked"/>
+ </AutoSuggestBox.KeyboardAccelerators>
+ </AutoSuggestBox>
</muxc:NavigationView.AutoSuggestBox>
<Frame x:Name="contentFrame" />
diff --git a/src/dev/impl/DevTools.Impl/Views/MainPage.xaml.cs b/src/dev/impl/DevTools.Impl/Views/MainPage.xaml.cs
index 1846b4c77..1085add38 100644
--- a/src/dev/impl/DevTools.Impl/Views/MainPage.xaml.cs
+++ b/src/dev/impl/DevTools.Impl/Views/MainPage.xaml.cs
@@ -30,5 +30,10 @@ namespace DevTools.Impl.Views
base.OnNavigatedTo(e);
}
+
+ private void SearchBoxKeyboardAccelerator_Invoked(Windows.UI.Xaml.Input.KeyboardAccelerator sender, Windows.UI.Xaml.Input.KeyboardAcceleratorInvokedEventArgs args)
+ {
+ SearchBox.Focus(FocusState.Keyboard);
+ }
}
}
diff --git a/src/dev/impl/DevTools.Providers.Impl/ToolProviderFactory.cs b/src/dev/impl/DevTools.Providers.Impl/ToolProviderFactory.cs
index 1756fcf6b..f5055f5ea 100644
--- a/src/dev/impl/DevTools.Providers.Impl/ToolProviderFactory.cs
+++ b/src/dev/impl/DevTools.Providers.Impl/ToolProviderFactory.cs
@@ -21,17 +21,11 @@ namespace DevTools.Providers.Impl
public IEnumerable<MatchedToolProvider> GetTools(string? searchQuery)
{
- IOrderedEnumerable<Lazy<IToolProvider, ToolProviderMetadata>> orderedProviders
- = _providers
- .Where(item => !item.Metadata.IsFooterItem)
- .OrderBy(item => item.Metadata.Order ?? int.MaxValue)
- .ThenBy(item => item.Value.DisplayName)
- .ThenBy(item => item.Metadata.Name);
-
searchQuery = searchQuery?.Trim();
string[]? searchQueries = searchQuery?.Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
- foreach (Lazy<IToolProvider, ToolProviderMetadata> provider in orderedProviders)
+ var results = new List<(MatchedToolProvider, ToolProviderMetadata)>();
+ foreach (Lazy<IToolProvider, ToolProviderMetadata> provider in _providers.Where(item => !item.Metadata.IsFooterItem))
{
var matches = new List<MatchSpan>();
@@ -46,6 +40,7 @@ namespace DevTools.Providers.Impl
if (matchIndex > -1)
{
matches.Add(new MatchSpan(matchIndex, query.Length));
+ i = matchIndex + query.Length;
}
i++;
@@ -55,15 +50,27 @@ namespace DevTools.Providers.Impl
if (matches.Count > 0 || searchQueries?.Length == 0)
{
- yield return new MatchedToolProvider(provider.Value, matches.ToArray());
+ results.Add((new MatchedToolProvider(provider.Value, matches.ToArray()), provider.Metadata));
}
}
+
+ return
+ results
+ .OrderByDescending(item => item.Item1.MatchedSpans.Length)
+ .ThenBy(item => item.Item2.Order ?? int.MaxValue)
+ .ThenBy(item => item.Item1.ToolProvider.DisplayName)
+ .ThenBy(item => item.Item2.Name)
+ .Select(item => item.Item1);
}
public IEnumerable<MatchedToolProvider> GetFooterTools()
{
var matches = Array.Empty<MatchSpan>();
- foreach (Lazy<IToolProvider, ToolProviderMetadata> provider in _providers.Where(item => item.Metadata.IsFooterItem))
+ foreach (Lazy<IToolProvider, ToolProviderMetadata> provider
+ in _providers.Where(item => item.Metadata.IsFooterItem)
+ .OrderBy(item => item.Metadata.Order ?? int.MaxValue)
+ .ThenBy(item => item.Value.DisplayName)
+ .ThenBy(item => item.Metadata.Name))
{
yield return new MatchedToolProvider(provider.Value, matches);
}