{{ message }}
fix: require live-tree membership for UIElement focusability#23645
Draft
morning4coffe-dev wants to merge 1 commit into
Draft
fix: require live-tree membership for UIElement focusability#23645morning4coffe-dev wants to merge 1 commit into
morning4coffe-dev wants to merge 1 commit into
Conversation
WinUI's CUIElement::IsFocusable requires IsActive() (element in the live visual tree), but Uno's port had that check commented out. This let Focus() and FindFirstFocusableElement succeed against detached elements, so a stale deferred focus call (e.g. from app code reacting to a view that already left the tree) could steal focus out of a TabFocusNavigation="Cycle" scope with no way back in. Add IsActiveOrAttachedUnderActiveAncestor(), checked under __CROSSRUNTIME__ in UIElement.IsFocusable and Hyperlink.IsFocusable. The ancestor walk (not a bare IsActiveInVisualTree check) is required because Uno's enhanced lifecycle activates freshly attached children one tick after they get a live parent, while WinUI enters them synchronously - a bare check regresses same-tick focus scenarios like a ComboBox focusing its item container right when the dropdown opens.
Contributor
Contributor
There was a problem hiding this comment.
Pull request overview
This PR aligns Uno’s focusability behavior with WinUI by requiring live-tree membership (or being newly attached under an active ancestor) for an element to be considered focusable on crossruntime targets, preventing detached elements from stealing focus or being returned by focus-candidate searches.
Changes:
- Gate
UIElement.IsFocusablewith a new crossruntime live-tree check (IsActiveOrAttachedUnderActiveAncestor()). - Apply the same live-tree gating to
Hyperlink.IsFocusable()via the containingFrameworkElement. - Add runtime tests covering focus and focus-candidate behavior for detached subtrees, including a
TabFocusNavigation=Cycleregression scenario.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| src/Uno.UI/UI/Xaml/UIElement.FocusMixins.cs | Adds crossruntime live-tree/active-ancestor gating to IsFocusable and introduces IsActiveOrAttachedUnderActiveAncestor(). |
| src/Uno.UI/UI/Xaml/Documents/Hyperlink.mux.cs | Ensures hyperlinks respect the same crossruntime live-tree focusability rules via the containing element. |
| src/Uno.UI.RuntimeTests/Tests/Windows_UI_Xaml_Input/Given_FocusManager_DetachedElements.cs | Adds WinUI-parity runtime tests validating focus behavior for detached elements/subtrees and tab-cycling scope integrity. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Contributor
|
🤖 Your WebAssembly Skia Sample App stage site is ready! Visit it here: https://unowasmprstaging.z20.web.core.windows.net/pr-23645/wasm-skia-net9/index.html |
Contributor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

GitHub Issue: closes #
PR Type:
🐞 Bugfix
What changed? 🚀
TEMP, looking into implementing is active.
WinUI's CUIElement::IsFocusable requires IsActive() (element in the live visual tree), but Uno's port had that check commented out. This let Focus() and FindFirstFocusableElement succeed against detached elements, so a stale deferred focus call (e.g. from app code reacting to a view that already left the tree) could steal focus out of a TabFocusNavigation="Cycle" scope with no way back in.
Add IsActiveOrAttachedUnderActiveAncestor(), checked under CROSSRUNTIME in UIElement.IsFocusable and Hyperlink.IsFocusable. The ancestor walk (not a bare IsActiveInVisualTree check) is required because Uno's enhanced lifecycle activates freshly attached children one tick after they get a live parent, while WinUI enters them synchronously - a bare check regresses same-tick focus scenarios like a ComboBox focusing its item container right when the dropdown opens.
PR Checklist ✅
Screenshots Compare Test Runresults.