Skip to content

Detached elements can receive or retain keyboard focus #23750

Closed

Description

@morning4coffe-dev

Current behavior 🐛

On cross-runtime targets, a UIElement detached from the live visual tree can still pass focusability checks. A stale or deferred Focus() call can move keyboard focus to that detached element, and a focused element removed from the tree can continue holding focus.

Expected behavior 🎯

Only elements in the live visual tree, or freshly attached elements under an active ancestor during Uno's delayed lifecycle activation, should be focusable. Removing a focused subtree should not leave keyboard focus on a detached element.

How to reproduce it (as minimally and precisely as possible) 🔬

  1. Add a focusable element to the window and focus it.
  2. Remove its containing subtree from the visual tree.
  3. Call Focus() on the detached element, or inspect the currently focused element.
  4. Observe that the detached element can receive or retain focus.

The implementation branch includes runtime coverage for detached focus attempts, focus cleanup after removal, and focus-cycle behavior.

Workaround 🛠️

Application code can verify that an element is still loaded and attached before issuing deferred focus requests.

Renderer 🎨

  • Skia
  • Native

Affected platforms 📱💻🖥️

Skia cross-runtime targets, including Desktop and WebAssembly.

Uno.Sdk version (and other relevant versions) 📦

Current master.

IDE version 🧑💻

N/A

Anything else we need to know? 💬

WinUI requires an element to be active before it is focusable. Uno needs an ancestor-aware check because child lifecycle activation can occur one tick after attachment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    triage/untriaged

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions