Skip to content

fix(themeservice): apply theme to own root element, not XamlRoot content - #3121

Merged
vatsashah45 merged 5 commits into
mainfrom
dev/vs/3120-themeservice-root-element
Jul 10, 2026
Merged

vatsashah45 merged 5 commits into
mainfrom
dev/vs/3120-themeservice-root-element

Conversation

@vatsashah45

@vatsashah45 vatsashah45 commented Jun 29, 2026 •

Copy link
Copy Markdown
Contributor

GitHub Issue: closes #3120

PR Type

  • Bugfix

What is the current behavior?

Uno.Extensions.Toolkit.ThemeService (consumed via UseThemeSwitching / IThemeService, registered as ScopedThemeService) applies the theme to RootElement.XamlRoot.Content — the root visual of the XamlRoot — rather than to RootElement itself (the element captured from window.Content).

When an app's content is hosted under a XamlRoot it does not own — e.g. a secondary app loaded into a collectible AssemblyLoadContext whose Window.Content is re-parented into a host's single shared XamlRoot — XamlRoot.Content resolves to the host's root visual. Toggling the theme then re-themes the host while the hosted app does not change. As a side effect IsDark reads the unchanged RootElement.ActualTheme, so ThemeChanged never fires either.

The class was internally inconsistent: it read (IsDark) and observed (ActualThemeChanged) theme from RootElement, but wrote to RootElement.XamlRoot.Content. Standalone apps were unaffected because there window.Content == XamlRoot.Content, so the bug was invisible.

What is the new behavior?

InternalSetThemeOnUIThread now targets RootElement directly:

- var rootElement = RootElement?.XamlRoot?.Content as FrameworkElement;
- if (rootElement is null) { return false; }
+ var rootElement = RootElement as FrameworkElement;
+ if (rootElement?.XamlRoot is null) { return false; }

RequestedTheme/ElementTheme cascades to the subtree, so the app's own root (and only it) is themed; an ancestor host is untouched. The XamlRoot null-check is retained as the "element realized yet?" gate that drives the Loaded-retry in InitializeAsync, 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 IsDark and ThemeChanged work 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 a host → app tree into the test window so the app root has a real XamlRoot whose Content is the host, then asserts SetThemeAsync(Dark) themes the app root (not the host) and that ThemeChanged fires. Fails on main (theme lands on the host), passes with the fix. The four existing ThemeService tests (bare, unrealized elements with a null XamlRoot) are unchanged — they still hit the retained gate.

Note: these are [RunsOnUIThread] runtime tests with no dotnet test entry point; they execute in the runtime-test stages.

Other information

- `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
vatsashah45 force-pushed the dev/vs/3120-themeservice-root-element branch from 8721c74 to 7d271ee Compare June 29, 2026 20:01
vatsashah45 and others added 3 commits June 29, 2026 17:06
- 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]>
@nickrandolph
nickrandolph changed the base branch from main to dev/vs/3120-themeservice-spec June 30, 2026 05:49
@mergify

mergify Bot commented Jun 30, 2026

Copy link
Copy Markdown

Tick the box to add this pull request to the merge queue (same as @mergifyio queue).

  • Queue this pull request

- 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
Base automatically changed from dev/vs/3120-themeservice-spec to main June 30, 2026 07:26
@nickrandolph
nickrandolph enabled auto-merge June 30, 2026 07:27
@vatsashah45 vatsashah45 self-assigned this Jun 30, 2026
@vatsashah45

Copy link
Copy Markdown
Contributor Author

/version-bump

@vatsashah45
vatsashah45 merged commit f3a5f9d into main Jul 10, 2026
18 of 19 checks passed
@vatsashah45
vatsashah45 deleted the dev/vs/3120-themeservice-root-element branch July 10, 2026 19:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ThemeService sets RequestedTheme on XamlRoot's content instead of its own root element (breaks theme switching when hosted in foreign XamlRoot / ALC)

3 participants