Skip to content

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

Merged
MartinZikmund merged 1 commit into
masterfrom
dev/agzi/fix-actualtheme-event-ordering
May 5, 2026
Merged

MartinZikmund merged 1 commit into
masterfrom
dev/agzi/fix-actualtheme-event-ordering

Conversation

@agneszitte

@agneszitte agneszitte commented May 5, 2026 •

Copy link
Copy Markdown
Member

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)

Copilot AI review requested due to automatic review settings May 5, 2026 05:07

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates Uno's theming pipeline so FrameworkElement.ActualTheme is persisted before ActualThemeChanged is raised, aligning the event behavior with the intended WinUI ordering and addressing the follow-up regression from #23178 / issue #23177.

Changes:

  • Reorder NotifyThemeChangedCore so SetTheme(theme) runs before RaiseActualThemeChanged().
  • Add regression tests for element-level and application-level theme changes to verify ActualTheme during ActualThemeChanged.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.

File Description
src/Uno.UI/UI/Xaml/FrameworkElement.Theming.cs Adjusts theme-walk ordering so the stored theme is updated before the event is raised.
src/Uno.UI.RuntimeTests/Tests/Windows_UI_Xaml/Given_ElementTheme.cs Adds regression tests covering ActualThemeChanged behavior for subtree and app-level theme changes.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copilot AI review requested due to automatic review settings May 5, 2026 05:39

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@agneszitte
agneszitte force-pushed the dev/agzi/fix-actualtheme-event-ordering branch from 992e54d to aca45d8 Compare May 5, 2026 06:15
@agneszitte
agneszitte requested a review from Copilot May 5, 2026 06:15
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

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@unodevops

Copy link
Copy Markdown
Contributor

@MartinZikmund
MartinZikmund enabled auto-merge May 5, 2026 12:02
@MartinZikmund
MartinZikmund merged commit b4db8c0 into master May 5, 2026
49 checks passed
@MartinZikmund
MartinZikmund deleted the dev/agzi/fix-actualtheme-event-ordering branch May 5, 2026 12:02
MartinZikmund pushed a commit that referenced this pull request May 7, 2026
Adds runtime test covering the exact CSharpMarkup ThemeBindingProvider
pattern: app-level theme change → ActualThemeChanged fires → handler
reads ActualTheme to select theme dictionary → resolves resource.

This test targets the combination not covered by existing tests:
- When_AppTheme_Changes_ActualTheme_Returns_New_Value_In_Handler
  covers app-level theme (no resource resolution)
- When_ThemeResource_Resolved_During_ActualThemeChanged_Returns_New_Value
  covers resource resolution (local RequestedTheme only)

The new test validates both together, matching the real-world failure
that caused the CSharpMarkup canary regression.

Related to #23178, #23197
@agneszitte

Copy link
Copy Markdown
Member Author

@Mergifyio backport release/stable/6.6

@mergify

mergify Bot commented May 11, 2026 •

Copy link
Copy Markdown
Contributor

backport release/stable/6.6

✅ Backports have been created

Details

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.

4 participants