Skip to content

fix(theming): set theme before raising ActualThemeChanged event (backport #23197) - #23246

Merged
agneszitte merged 1 commit into
release/stable/6.6from
mergify/bp/release/stable/6.6/pr-23197
May 11, 2026
Merged

agneszitte merged 1 commit into
release/stable/6.6from
mergify/bp/release/stable/6.6/pr-23197

Conversation

@mergify

@mergify mergify Bot commented May 11, 2026

Copy link
Copy Markdown
Contributor

Summary

Follow-up to #23178 Related to #23177

Moves SetTheme(theme) before both PropagateThemeToChildren and RaiseActualThemeChanged() in NotifyThemeChangedCore so that ActualTheme returns the new value inside ActualThemeChanged event handlers.

What was wrong

After PR #23178 (related to previous #22803 and #22887), SetTheme(theme) was called after RaiseActualThemeChanged(). This meant that reading ActualTheme inside an ActualThemeChanged handler returned the old/stale theme instead of the new one.

See, related C# Markup Canary Tests failing for more context

WinUI alignment

WinUI's framework.cpp calls CUIElement::NotifyThemeChangedCore(theme) (which sets the theme) before RaiseActiveThemeChangedEventIfChanging(theme) (which fires the event). This fix aligns Uno with that ordering.

Why moving SetTheme earlier is safe

  • oldTheme is already captured at the top of NotifyThemeChangedCore
  • UpdateThemeBindings and PropagateThemeToChildren use the passed theme parameter, not the stored theme
  • Children read their own _theme field, not the parent's

Tests added

  • When_ActualThemeChanged_Fires_ActualTheme_Returns_New_Value — verifies parent and child ActualTheme return Dark inside ActualThemeChanged handler when RequestedTheme is set to Dark, and that the parent's ActualTheme is already updated when observed from the child's handler
  • When_AppTheme_Changes_ActualTheme_Returns_New_Value_In_Handler — verifies ActualTheme returns Dark during app-level theme change via ThemeHelper.UseApplicationDarkTheme() (exercises root→child propagation path)
  • When_ThemeResource_Resolved_During_ActualThemeChanged_Returns_New_Value — verifies that a local ThemeDictionaries resource resolves to the new theme's value when looked up via ActualTheme inside ActualThemeChanged (mimics the C# Markup ThemeBindingProvider pattern that triggered the original failure)
    This is an automatic backport of pull request fix(theming): set theme before raising ActualThemeChanged event #23197 done by Mergify.

Reorder SetRequestedTheme to apply the new theme value before propagating
ActualThemeChanged to children, ensuring handlers observe the correct
ActualTheme and resolve the right ThemeResource values.

- Move SetTheme call before RaiseActualThemeChangedOnChildren
- Add runtime tests covering event ordering, stale-theme regression,
  and ThemeResource resolution inside the handler

Fixes #23177

(cherry picked from commit aca45d8)
@agneszitte
agneszitte merged commit d435356 into release/stable/6.6 May 11, 2026
8 of 11 checks passed
@agneszitte
agneszitte deleted the mergify/bp/release/stable/6.6/pr-23197 branch May 11, 2026 17:20
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.

1 participant