From d85ccb9c58b085c4038e4f63b41999eba963e0fe Mon Sep 17 00:00:00 2001 From: Jessica Dene Earley-Cha <12740421+chatasweetie@users.noreply.github.com> Date: Tue, 2 Sep 2025 17:53:53 -0700 Subject: [PATCH] [CmdPal] Restore focus to More button after context menu closes via escape button (#41364) ## Summary of the Pull Request ## PR Checklist - [x] Closes: #41363 - [ ] **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 ## Detailed Description of the Pull Request / Additional comments ## Validation Steps Performed https://github.com/user-attachments/assets/374fd1bc-8e62-4117-a613-f0d35678e3ed --- .../Controls/CommandBar.xaml.cs | 9 +++++++++ .../Controls/ContextMenu.xaml | 1 + .../Controls/ContextMenu.xaml.cs | 19 +++++++++++++++++++ 3 files changed, 29 insertions(+) diff --git a/src/modules/cmdpal/Microsoft.CmdPal.UI/Controls/CommandBar.xaml.cs b/src/modules/cmdpal/Microsoft.CmdPal.UI/Controls/CommandBar.xaml.cs index f4a0dc3d43..5666381d82 100644 --- a/src/modules/cmdpal/Microsoft.CmdPal.UI/Controls/CommandBar.xaml.cs +++ b/src/modules/cmdpal/Microsoft.CmdPal.UI/Controls/CommandBar.xaml.cs @@ -134,6 +134,15 @@ public sealed partial class CommandBar : UserControl, WeakReferenceMessenger.Default.Send(new OpenContextMenuMessage(null, null, null, ContextMenuFilterLocation.Bottom)); } + /// + /// Sets focus to the "More" button after closing the context menu, + /// keeping keyboard navigation intuitive. + /// + public void FocusMoreCommandsButton() + { + MoreCommandsButton?.Focus(FocusState.Programmatic); + } + private void ContextMenuFlyout_Opened(object sender, object e) { // We need to wait until our flyout is opened to try and toss focus diff --git a/src/modules/cmdpal/Microsoft.CmdPal.UI/Controls/ContextMenu.xaml b/src/modules/cmdpal/Microsoft.CmdPal.UI/Controls/ContextMenu.xaml index e23fb815b0..5636de9aca 100644 --- a/src/modules/cmdpal/Microsoft.CmdPal.UI/Controls/ContextMenu.xaml +++ b/src/modules/cmdpal/Microsoft.CmdPal.UI/Controls/ContextMenu.xaml @@ -15,6 +15,7 @@ xmlns:ui="using:CommunityToolkit.WinUI" xmlns:viewModels="using:Microsoft.CmdPal.UI.ViewModels" Background="Transparent" + PreviewKeyDown="UserControl_PreviewKeyDown" mc:Ignorable="d"> diff --git a/src/modules/cmdpal/Microsoft.CmdPal.UI/Controls/ContextMenu.xaml.cs b/src/modules/cmdpal/Microsoft.CmdPal.UI/Controls/ContextMenu.xaml.cs index f2c2ad2f4c..afc2d190ef 100644 --- a/src/modules/cmdpal/Microsoft.CmdPal.UI/Controls/ContextMenu.xaml.cs +++ b/src/modules/cmdpal/Microsoft.CmdPal.UI/Controls/ContextMenu.xaml.cs @@ -3,6 +3,7 @@ // See the LICENSE file in the project root for more information. using CommunityToolkit.Mvvm.Messaging; +using CommunityToolkit.WinUI; using Microsoft.CmdPal.Core.ViewModels; using Microsoft.CmdPal.Core.ViewModels.Messages; using Microsoft.CmdPal.UI.Messages; @@ -115,6 +116,24 @@ public sealed partial class ContextMenu : UserControl, } } + /// + /// Handles Escape to close the context menu and return focus to the "More" button. + /// + private void UserControl_PreviewKeyDown(object sender, KeyRoutedEventArgs e) + { + if (e.Key == VirtualKey.Escape) + { + // Close the context menu (if not already handled) + WeakReferenceMessenger.Default.Send(new CloseContextMenuMessage()); + + // Find the parent CommandBar and set focus to MoreCommandsButton + var parent = this.FindParent(); + parent?.FocusMoreCommandsButton(); + + e.Handled = true; + } + } + private void ViewModel_PropertyChanged(object? sender, System.ComponentModel.PropertyChangedEventArgs e) { var prop = e.PropertyName;