mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-05 02:36:19 +02:00
Settings: Settings search fixes (#41381)
<!-- Enter a brief description/summary of your PR here. What does it fix/what does it change/how was it tested (even manually, if necessary)? --> ## Summary of the Pull Request Fix 3 issues <!-- Please review the items on the PR checklist before submitting--> ## PR Checklist - [x] Closes: #41369, #41374, #41380 - [ ] **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 https://github.com/user-attachments/assets/0e0df9fb-5aca-4b26-9d53-e6ddc49cab04 --------- Co-authored-by: Niels Laute <niels.laute@live.nl> Co-authored-by: Jiří Polášek <me@jiripolasek.com>
This commit is contained in:
@@ -80,6 +80,9 @@ public abstract partial class NavigablePage : Page
|
||||
return;
|
||||
}
|
||||
|
||||
// Attempt to set keyboard focus so that screen readers announce the element and keyboard users land directly on it.
|
||||
TrySetFocus(target);
|
||||
|
||||
// Get the visual and compositor
|
||||
var visual = ElementCompositionPreview.GetElementVisual(target);
|
||||
var compositor = visual.Compositor;
|
||||
@@ -113,9 +116,129 @@ public abstract partial class NavigablePage : Page
|
||||
ElementCompositionPreview.SetElementChildVisual(target, null);
|
||||
}
|
||||
|
||||
private static void TrySetFocus(FrameworkElement target)
|
||||
{
|
||||
try
|
||||
{
|
||||
// Prefer Control.Focus when available.
|
||||
if (target is Control ctrl)
|
||||
{
|
||||
// Ensure it can receive focus.
|
||||
if (!ctrl.IsTabStop)
|
||||
{
|
||||
ctrl.IsTabStop = true;
|
||||
}
|
||||
|
||||
ctrl.Focus(FocusState.Programmatic);
|
||||
}
|
||||
|
||||
// Target is not a Control. Find first focusable descendant Control.
|
||||
var focusCandidate = FindFirstFocusableDescendant(target);
|
||||
if (focusCandidate != null)
|
||||
{
|
||||
if (!focusCandidate.IsTabStop)
|
||||
{
|
||||
focusCandidate.IsTabStop = true;
|
||||
}
|
||||
|
||||
focusCandidate.Focus(FocusState.Programmatic);
|
||||
return;
|
||||
}
|
||||
|
||||
// Fallback: attempt to focus parent control if no descendant found.
|
||||
if (target.Parent is Control parent)
|
||||
{
|
||||
if (!parent.IsTabStop)
|
||||
{
|
||||
parent.IsTabStop = true;
|
||||
}
|
||||
|
||||
parent.Focus(FocusState.Programmatic);
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
// Swallow focus exceptions; not critical. Could log if logging enabled.
|
||||
// Leave the default focus as it is.
|
||||
}
|
||||
}
|
||||
|
||||
private static Control FindFirstFocusableDescendant(FrameworkElement root)
|
||||
{
|
||||
if (root == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var queue = new System.Collections.Generic.Queue<DependencyObject>();
|
||||
queue.Enqueue(root);
|
||||
while (queue.Count > 0)
|
||||
{
|
||||
var current = queue.Dequeue();
|
||||
if (current is Control c && c.IsEnabled && c.Visibility == Visibility.Visible)
|
||||
{
|
||||
return c;
|
||||
}
|
||||
|
||||
int count = VisualTreeHelper.GetChildrenCount(current);
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
queue.Enqueue(VisualTreeHelper.GetChild(current, i));
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
protected FrameworkElement FindElementByName(string name)
|
||||
{
|
||||
var element = this.FindName(name) as FrameworkElement;
|
||||
return element;
|
||||
if (element != null)
|
||||
{
|
||||
return element;
|
||||
}
|
||||
|
||||
if (this.Content is DependencyObject root)
|
||||
{
|
||||
var found = FindInDescendants(root, name);
|
||||
if (found != null)
|
||||
{
|
||||
return found;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private static FrameworkElement FindInDescendants(DependencyObject root, string name)
|
||||
{
|
||||
if (root == null || string.IsNullOrEmpty(name))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var queue = new System.Collections.Generic.Queue<DependencyObject>();
|
||||
queue.Enqueue(root);
|
||||
while (queue.Count > 0)
|
||||
{
|
||||
var current = queue.Dequeue();
|
||||
if (current is FrameworkElement fe)
|
||||
{
|
||||
var local = fe.FindName(name) as FrameworkElement;
|
||||
if (local != null)
|
||||
{
|
||||
return local;
|
||||
}
|
||||
}
|
||||
|
||||
int count = VisualTreeHelper.GetChildrenCount(current);
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
var child = VisualTreeHelper.GetChild(current, i);
|
||||
queue.Enqueue(child);
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user