Repository navigation
fix(themeservice): apply theme to own root element, not XamlRoot content - #3121
Merged
Merged
Conversation
2 tasks done
- `ThemeService` wrote `RequestedTheme` to `RootElement.XamlRoot.Content` (the XamlRoot's root visual) instead of `RootElement` itself - under a foreign/shared XamlRoot (e.g. an app hosted in a collectible ALC whose Window.Content is re-parented into a host) this re-themed the host instead of the app, and `ThemeChanged` never fired - target `RootElement` directly; retain the XamlRoot null-check as the "element realized" gate so standalone behavior is unchanged - add red/fix/green runtime test for the foreign-XamlRoot topology Fixes #3120 Co-Authored-By: Claude Opus 4.8 <[email protected]>
vatsashah45
force-pushed
the
dev/vs/3120-themeservice-root-element
branch
from
June 29, 2026 20:01
8721c74 to
7d271ee
Compare
- log at Debug when the realization gate skips a theme set (the silent no-op #3120 was about) and when a theme is applied, so the field can tell "applied, no flip" from "silently did nothing" - await ThemeChanged instead of snapshotting it, so the test does not depend on ActualTheme propagating synchronously within SetThemeAsync - assert the theme cascades into the app's own subtree, not the host - reword a now-stale comment on the existing explicit-theme test Refs #3120 Co-Authored-By: Claude Opus 4.8 <[email protected]>
…guard - ran Given_ThemeService on the Skia runtime-test head; red/fix/green confirmed - the `== Dark` snapshot raced ThemeService's own ActualThemeChanged -> InternalSetThemeAsync re-entrancy under the synchronous test dispatcher (settles RequestedTheme off Dark in a live tree), so it failed on the fix too - assert the stable invariant the fix actually changes: the host (XamlRoot owner) is NOT re-themed, and the theme lands on the app's own root element - verified: main logic -> host re-themed -> RED; fix -> host untouched -> GREEN (5/5) Refs #3120 Co-Authored-By: Claude Opus 4.8 <[email protected]>
- complements the write-target guard: with the real dispatcher (not the synchronous fake), the hosted app's subtree settles on Dark while the host that owns the XamlRoot stays Light - confirms the synchronous-dispatcher "settles off Dark" was a test-harness artifact, not a fix problem - verified on the Skia runtime-test head: both foreign-XamlRoot guards RED on main logic, GREEN on the fix (6/6) Refs #3120 Co-Authored-By: Claude Opus 4.8 <[email protected]>
|
Tick the box to add this pull request to the merge queue (same as
|
nickrandolph
approved these changes
Jun 30, 2026
- always use braced blocks for if statements (no single-line if-then), per @nickrandolph review on #3121 - behavior unchanged; rebuilt + reran Given_ThemeService on the Skia head (6/6 green) Refs #3120
nickrandolph
approved these changes
Jun 30, 2026
nickrandolph
enabled auto-merge
June 30, 2026 07:27
Contributor
Author
|
/version-bump |
auto-merge was automatically disabled
June 30, 2026 17:10
Pull request was closed
vatsashah45
enabled auto-merge
June 30, 2026 18:53
This was referenced Jul 10, 2026
kazo0
approved these changes
Jul 10, 2026
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 #3120
PR Type
What is the current behavior?
Uno.Extensions.Toolkit.ThemeService(consumed viaUseThemeSwitching/IThemeService, registered asScopedThemeService) applies the theme toRootElement.XamlRoot.Content— the root visual of the XamlRoot — rather than toRootElementitself (the element captured fromwindow.Content).When an app's content is hosted under a
XamlRootit does not own — e.g. a secondary app loaded into a collectibleAssemblyLoadContextwhoseWindow.Contentis re-parented into a host's single sharedXamlRoot—XamlRoot.Contentresolves to the host's root visual. Toggling the theme then re-themes the host while the hosted app does not change. As a side effectIsDarkreads the unchangedRootElement.ActualTheme, soThemeChangednever fires either.The class was internally inconsistent: it read (
IsDark) and observed (ActualThemeChanged) theme fromRootElement, but wrote toRootElement.XamlRoot.Content. Standalone apps were unaffected because therewindow.Content == XamlRoot.Content, so the bug was invisible.What is the new behavior?
InternalSetThemeOnUIThreadnow targetsRootElementdirectly:RequestedTheme/ElementThemecascades to the subtree, so the app's own root (and only it) is themed; an ancestor host is untouched. TheXamlRootnull-check is retained as the "element realized yet?" gate that drives theLoaded-retry inInitializeAsync, so the only behavioral change is where the theme is written when the element is realized — standalone behavior is unchanged.Read / write / observe now all act on the same element, so
IsDarkandThemeChangedwork correctly in the hosted case too.PR Checklist
Tests
Red/fix/green runtime test added to
Given_ThemeService.cs(Uno.Extensions.Core.UI.Tests):When_HostedUnderForeignXamlRoot_Then_ThemeAppliesToOwnRoot_NotHost. It loads ahost → apptree into the test window so the app root has a realXamlRootwhoseContentis the host, then assertsSetThemeAsync(Dark)themes the app root (not the host) and thatThemeChangedfires. Fails onmain(theme lands on the host), passes with the fix. The four existingThemeServicetests (bare, unrealized elements with a nullXamlRoot) are unchanged — they still hit the retained gate.Other information
Uno.Toolkit.UI.SystemThemeHelper.SetApplicationTheme(XamlRoot?, ElementTheme)has the sameroot.Content.RequestedThemeshape, but it only receives aXamlRoot(no element to scope to), is in a different repo, and is not in this code path — out of scope here.