CmdPal: Hide search box on content pages [experiment] (#41479)

## Summary of the Pull Request

CmdPal now displays the search box only on pages that derive from
ListPage. On ContentPage (forms, etc.), the search box is hidden.

- Moves keyboard shortcut handling from SearchBox to ShellPage so
shortcuts are always handled.
- Keeps the search box hidden/disabled to preserve layout metrics. 
- Refines focus management to prevent focus jumps during navigation. 
- For ContentPage page's content gains focus automatically (not just
form inputs, but now markdown content as well - so you can scroll
immediately, for example).
- Adds accessibility (a11y) tweaks: when navigating to content pages
without a visible search box, sets an explicit focus target so screen
readers announce a meaningful element. Screen reader will now announce
navigation to the page.
- Adds a title to the main list page - "Home".


https://github.com/user-attachments/assets/f60d0826-df1f-468e-8e41-0266cd27878b


<!-- Please review the items on the PR checklist before submitting-->
## PR Checklist

- [x] Closes: #38967
- [ ] **Communication:** I've discussed this with core contributors
already. If the work hasn't been agreed, this work might be rejected
- [ ] **Tests:** Added/updated and all pass
- [ ] **Localization:** All end-user-facing strings can be localized
- [ ] **Dev docs:** Added/updated
- [ ] **New binaries:** Added on the required places
- [ ] [JSON for
signing](https://github.com/microsoft/PowerToys/blob/main/.pipelines/ESRPSigning_core.json)
for new binaries
- [ ] [WXS for
installer](https://github.com/microsoft/PowerToys/blob/main/installer/PowerToysSetup/Product.wxs)
for new binaries and localization folder
- [ ] [YML for CI
pipeline](https://github.com/microsoft/PowerToys/blob/main/.pipelines/ci/templates/build-powertoys-steps.yml)
for new test projects
- [ ] [YML for signed
pipeline](https://github.com/microsoft/PowerToys/blob/main/.pipelines/release.yml)
- [ ] **Documentation updated:** If checked, please file a pull request
on [our docs
repo](https://github.com/MicrosoftDocs/windows-uwp/tree/docs/hub/powertoys)
and link it here: #xxx

<!-- Provide a more detailed description of the PR, other things fixed,
or any additional comments/features here -->
## Detailed Description of the Pull Request / Additional comments

<!-- Describe how you validated the behavior. Add automated tests
wherever possible, but list manual validation steps taken as well -->
## Validation Steps Performed
This commit is contained in:
Jiří Polášek
2025-09-26 23:00:53 +02:00
committed by GitHub
parent e1681ec08f
commit 2b6c5d2cdd
11 changed files with 204 additions and 54 deletions

View File

@@ -3,12 +3,15 @@
// See the LICENSE file in the project root for more information.
using System.ComponentModel;
using System.Globalization;
using System.Text;
using CommunityToolkit.Mvvm.Messaging;
using CommunityToolkit.WinUI;
using ManagedCommon;
using Microsoft.CmdPal.Core.ViewModels;
using Microsoft.CmdPal.Core.ViewModels.Messages;
using Microsoft.CmdPal.UI.Events;
using Microsoft.CmdPal.UI.Helpers;
using Microsoft.CmdPal.UI.Messages;
using Microsoft.CmdPal.UI.Settings;
using Microsoft.CmdPal.UI.ViewModels;
@@ -17,9 +20,12 @@ using Microsoft.Extensions.DependencyInjection;
using Microsoft.PowerToys.Telemetry;
using Microsoft.UI.Dispatching;
using Microsoft.UI.Input;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Automation.Peers;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media.Animation;
using Windows.UI.Core;
using DispatcherQueue = Microsoft.UI.Dispatching.DispatcherQueue;
using VirtualKey = Windows.System.VirtualKey;
@@ -55,6 +61,8 @@ public sealed partial class ShellPage : Microsoft.UI.Xaml.Controls.Page,
private readonly ToastWindow _toast = new();
private readonly CompositeFormat _pageNavigatedAnnouncement;
private SettingsWindow? _settingsWindow;
public ShellViewModel ViewModel { get; private set; } = App.Current.Services.GetService<ShellViewModel>()!;
@@ -84,9 +92,13 @@ public sealed partial class ShellPage : Microsoft.UI.Xaml.Controls.Page,
WeakReferenceMessenger.Default.Register<NavigateToPageMessage>(this);
AddHandler(PreviewKeyDownEvent, new KeyEventHandler(ShellPage_OnPreviewKeyDown), true);
AddHandler(KeyDownEvent, new KeyEventHandler(ShellPage_OnKeyDown), false);
AddHandler(PointerPressedEvent, new PointerEventHandler(ShellPage_OnPointerPressed), true);
RootFrame.Navigate(typeof(LoadingPage), ViewModel);
var pageAnnouncementFormat = ResourceLoaderInstance.GetString("ScreenReader_Announcement_NavigatedToPage0");
_pageNavigatedAnnouncement = CompositeFormat.Parse(pageAnnouncementFormat);
}
public void Receive(NavigateBackMessage message)
@@ -134,9 +146,6 @@ public sealed partial class ShellPage : Microsoft.UI.Xaml.Controls.Page,
PowerToysTelemetry.Log.WriteEvent(new OpenPage(RootFrame.BackStackDepth));
// Refocus on the Search for continual typing on the next search request
SearchBox.Focus(Microsoft.UI.Xaml.FocusState.Programmatic);
if (!ViewModel.IsNested)
{
// todo BODGY
@@ -441,6 +450,78 @@ public sealed partial class ShellPage : Microsoft.UI.Xaml.Controls.Page,
// We just need to reconcile our loading systems a bit more in the future.
ViewModel.CurrentPage = page;
}
if (e.Content is Page element)
{
element.Loaded += FocusAfterLoaded;
}
}
private void FocusAfterLoaded(object sender, RoutedEventArgs e)
{
var page = (Page)sender;
page.Loaded -= FocusAfterLoaded;
AnnounceNavigationToPage(page);
var shouldSearchBoxBeVisible = ViewModel.CurrentPage?.HasSearchBox ?? false;
if (shouldSearchBoxBeVisible || page is not ContentPage)
{
ViewModel.IsSearchBoxVisible = shouldSearchBoxBeVisible;
SearchBox.Focus(FocusState.Programmatic);
SearchBox.SelectSearch();
}
else
{
_ = Task.Run(async () =>
{
await page.DispatcherQueue.EnqueueAsync(async () =>
{
// I hate this so much, but it can take a while for the page to be ready to accept focus;
// focusing page with MarkdownTextBlock takes up to 5 attempts (* 100ms delay between attempts)
for (var i = 0; i < 10; i++)
{
if (FocusManager.FindFirstFocusableElement(page) is FrameworkElement frameworkElement)
{
var set = frameworkElement.Focus(FocusState.Programmatic);
if (set)
{
break;
}
}
await Task.Delay(100);
}
// Update the search box visibility based on the current page:
// - We do this here after navigation so the focus is not jumping around too much,
// it messes with screen readers if we do it too early
// - Since this should hide the search box on content pages, it's not a problem if we
// wait for the code above to finish trying to focus the content
ViewModel.IsSearchBoxVisible = ViewModel.CurrentPage?.HasSearchBox ?? false;
});
});
}
}
private void AnnounceNavigationToPage(Page page)
{
var pageTitle = page switch
{
ListPage listPage => listPage.ViewModel?.Title,
ContentPage contentPage => contentPage.ViewModel?.Title,
_ => null,
};
if (string.IsNullOrEmpty(pageTitle))
{
pageTitle = ResourceLoaderInstance.GetString("UntitledPageTitle");
}
var announcement = string.Format(CultureInfo.CurrentCulture, _pageNavigatedAnnouncement.Format, pageTitle);
UIHelper.AnnounceActionForAccessibility(RootFrame, announcement, "CommandPalettePageNavigatedTo");
}
/// <summary>
@@ -466,11 +547,54 @@ public sealed partial class ShellPage : Microsoft.UI.Xaml.Controls.Page,
}
}
private void ShellPage_OnPreviewKeyDown(object sender, KeyRoutedEventArgs e)
private static void ShellPage_OnPreviewKeyDown(object sender, KeyRoutedEventArgs e)
{
if (e.Key == VirtualKey.Left && e.KeyStatus.IsMenuKeyDown)
{
WeakReferenceMessenger.Default.Send<NavigateBackMessage>(new());
e.Handled = true;
}
else
{
var ctrlPressed = InputKeyboardSource.GetKeyStateForCurrentThread(VirtualKey.Control).HasFlag(CoreVirtualKeyStates.Down);
var altPressed = InputKeyboardSource.GetKeyStateForCurrentThread(VirtualKey.Menu).HasFlag(CoreVirtualKeyStates.Down);
var shiftPressed = InputKeyboardSource.GetKeyStateForCurrentThread(VirtualKey.Shift).HasFlag(CoreVirtualKeyStates.Down);
var winPressed = InputKeyboardSource.GetKeyStateForCurrentThread(VirtualKey.LeftWindows).HasFlag(CoreVirtualKeyStates.Down) ||
InputKeyboardSource.GetKeyStateForCurrentThread(VirtualKey.RightWindows).HasFlag(CoreVirtualKeyStates.Down);
// The CommandBar is responsible for handling all the item keybindings,
// since the bound context item may need to then show another
// context menu
TryCommandKeybindingMessage msg = new(ctrlPressed, altPressed, shiftPressed, winPressed, e.Key);
WeakReferenceMessenger.Default.Send(msg);
e.Handled = msg.Handled;
}
}
private static void ShellPage_OnKeyDown(object sender, KeyRoutedEventArgs e)
{
var ctrlPressed = InputKeyboardSource.GetKeyStateForCurrentThread(VirtualKey.Control).HasFlag(CoreVirtualKeyStates.Down);
if (ctrlPressed && e.Key == VirtualKey.Enter)
{
// ctrl+enter
WeakReferenceMessenger.Default.Send<ActivateSecondaryCommandMessage>();
e.Handled = true;
}
else if (e.Key == VirtualKey.Enter)
{
WeakReferenceMessenger.Default.Send<ActivateSelectedListItemMessage>();
e.Handled = true;
}
else if (ctrlPressed && e.Key == VirtualKey.K)
{
// ctrl+k
WeakReferenceMessenger.Default.Send<OpenContextMenuMessage>(new OpenContextMenuMessage(null, null, null, ContextMenuFilterLocation.Bottom));
e.Handled = true;
}
else if (e.Key == VirtualKey.Escape)
{
WeakReferenceMessenger.Default.Send<NavigateBackMessage>(new());
e.Handled = true;
}
}