From 7226b05100b20955692ec83914644c07d035047f Mon Sep 17 00:00:00 2001 From: Ben Hillis Date: Tue, 18 Nov 2025 08:19:50 -0800 Subject: [PATCH] wslsettings: ensure selected setting is auto-expanded and selected (#13689) * wslsettings: ensure selected setting is auto-selected Implement keyboard focus management for SettingsExpander controls across settings pages. This resolves an accessibility issue reported internally. * add asserts --------- Co-authored-by: Ben Hillis --- .../wslsettings/Helpers/RuntimeHelper.cs | 37 +++++++++++++++++++ .../Views/Settings/DeveloperPage.xaml | 12 +++--- .../Views/Settings/DeveloperPage.xaml.cs | 9 +++++ .../Views/Settings/FileSystemPage.xaml | 4 +- .../Views/Settings/FileSystemPage.xaml.cs | 18 +++++++++ .../Views/Settings/MemAndProcPage.xaml | 16 ++++---- .../Views/Settings/MemAndProcPage.xaml.cs | 10 +++++ .../Views/Settings/NetworkingPage.xaml | 8 ++-- .../Views/Settings/NetworkingPage.xaml.cs | 8 ++++ .../Views/Settings/OptionalFeaturesPage.xaml | 4 +- .../Settings/OptionalFeaturesPage.xaml.cs | 8 ++++ 11 files changed, 112 insertions(+), 22 deletions(-) diff --git a/src/windows/wslsettings/Helpers/RuntimeHelper.cs b/src/windows/wslsettings/Helpers/RuntimeHelper.cs index 5a3592a..58e33de 100644 --- a/src/windows/wslsettings/Helpers/RuntimeHelper.cs +++ b/src/windows/wslsettings/Helpers/RuntimeHelper.cs @@ -1,7 +1,9 @@ // Copyright (C) Microsoft Corporation. All rights reserved. +using CommunityToolkit.WinUI.Controls; using Microsoft.UI.Xaml.Controls; using Microsoft.UI.Xaml.Input; +using System.Diagnostics; using System.Reflection; using System.Runtime.InteropServices; using System.Text; @@ -47,4 +49,39 @@ public class RuntimeHelper FindNextElementOptions fneo = new() { SearchRoot = button.XamlRoot.Content }; FocusManager.TryMoveFocus(FocusNavigationDirection.Previous, fneo); } + + public static void SetupSettingsExpanderFocusManagement(Microsoft.UI.Xaml.FrameworkElement expander, Microsoft.UI.Xaml.Controls.Control firstFocusableElement) + { + if (expander is CommunityToolkit.WinUI.Controls.SettingsExpander settingsExpander) + { + settingsExpander.RegisterPropertyChangedCallback(CommunityToolkit.WinUI.Controls.SettingsExpander.IsExpandedProperty, (sender, dp) => + { + if (sender is CommunityToolkit.WinUI.Controls.SettingsExpander se && se.IsExpanded) + { + System.EventHandler? layoutHandler = null; + layoutHandler = (s, e) => + { + se.LayoutUpdated -= layoutHandler; + firstFocusableElement.Focus(Microsoft.UI.Xaml.FocusState.Keyboard); + }; + + se.LayoutUpdated += layoutHandler; + } + }); + } + } + + public static void SetupExpanderFocusManagementByName(Microsoft.UI.Xaml.FrameworkElement parent, string expanderName, string textBoxName) + { + var expander = parent.FindName(expanderName) as Microsoft.UI.Xaml.FrameworkElement; + var textBox = parent.FindName(textBoxName) as Microsoft.UI.Xaml.Controls.Control; + + Debug.Assert(expander != null, $"Expander '{expanderName}' not found"); + Debug.Assert(textBox != null, $"TextBox '{textBoxName}' not found"); + + if (expander != null && textBox != null) + { + SetupSettingsExpanderFocusManagement(expander, textBox); + } + } } \ No newline at end of file diff --git a/src/windows/wslsettings/Views/Settings/DeveloperPage.xaml b/src/windows/wslsettings/Views/Settings/DeveloperPage.xaml index db83f8a..7c1280c 100644 --- a/src/windows/wslsettings/Views/Settings/DeveloperPage.xaml +++ b/src/windows/wslsettings/Views/Settings/DeveloperPage.xaml @@ -25,12 +25,12 @@ - + -