Skip to content

fix: os theme switching#3037

Merged
kazo0 merged 3 commits intomainfrom
dev/mara/fix-os-theme
Mar 10, 2026
Merged

fix: os theme switching#3037
kazo0 merged 3 commits intomainfrom
dev/mara/fix-os-theme

Conversation

@rajamatt
Copy link
Contributor

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Refactoring (no functional changes, no api changes)

What is the current behavior?

When the user's saved theme preference is System (follow OS theme) and the OS theme changes (e.g. Light → Dark), the ElementThemeChanged handler in ThemeService calls InternalSetThemeAsync, which sets an explicit RequestedTheme (e.g. ElementTheme.Dark) on the root element. This overrides ElementTheme.Default, effectively "freezing" the app on that theme and preventing future OS theme changes from being tracked.

What is the new behavior?

When the saved theme is AppTheme.System and ElementThemeChanged fires, the handler now:

  1. Fires the ThemeChanged event with AppTheme.System (instead of the specific Dark/Light value)
  2. Returns early without calling InternalSetThemeAsync, preserving ElementTheme.Default on the root element so the app continues to follow the OS theme

A new runtime test project (Uno.Extensions.Core.WinUI.Tests) has been added with tests verifying:

  • ThemeChanged reports AppTheme.System (not Dark/Light) when following system theme
  • The saved theme setting is not overwritten during OS theme changes
  • Multiple OS theme changes continue to be tracked correctly
  • Explicit (non-System) saved themes do not take the System shortcut path

PR Checklist

Please check if your PR fulfills the following requirements:

  • Tested code with current supported SDKs
  • Docs have been added/updated which fit documentation template. (for bug fixes / features)
  • Unit Tests and/or UI Tests for the changes have been added (for bug fixes / features) (if applicable)
  • Wasm UI Tests are not showing unexpected any differences. Validate PR Screenshots Compare Test Run results.
  • Contains NO breaking changes
  • Updated the Release Notes
  • Associated with an issue (GitHub or internal)

Other information

The fix is a small early-return guard in ThemeService.ElementThemeChanged (11 lines added to ThemeService.cs). The test project follows the existing Navigation.UI.Tests pattern and is wired into the RuntimeTests runner.

Internal Issue (If applicable):

rajamatt and others added 2 commits March 9, 2026 21:05
Add a new test project Uno.Extensions.Core.WinUI.Tests with runtime
tests verifying the ElementThemeChanged fix for system theme tracking:

- When saved theme is System and OS theme changes, ThemeChanged reports
  AppTheme.System (not Dark/Light)
- Saved theme setting is not overwritten during OS theme changes
- Multiple OS theme changes continue reporting System correctly
- Explicit (non-System) saved themes do not take the System shortcut

Also adds InternalsVisibleTo for the test assembly and wires the test
project into the RuntimeTests runner.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copy link
Contributor

Copilot AI left a comment

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 fixes a bug where switching OS themes while the app is set to follow the system theme (AppTheme.System) would "freeze" the app on the detected theme. The root cause was that ElementThemeChanged called InternalSetThemeAsync, which set an explicit RequestedTheme, overriding ElementTheme.Default and preventing future OS theme tracking.

Changes:

  • Added an early-return guard in ThemeService.ElementThemeChanged that preserves ElementTheme.Default when the saved theme is System, firing ThemeChanged with AppTheme.System instead of calling InternalSetThemeAsync.
  • Added a new test project (Uno.Extensions.Core.WinUI.Tests) with unit tests verifying correct behavior for system and explicit theme scenarios.
  • Exposed internals to the new test project via InternalsVisibleTo.

Reviewed changes

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

Show a summary per file
File Description
ThemeService.cs Early-return guard in ElementThemeChanged to avoid overriding ElementTheme.Default when following system theme
Uno.Extensions.Core.WinUI.Tests.csproj New test project for Core UI extensions
Given_ThemeService.cs Runtime tests verifying the fix for system and explicit theme switching
SynchronousDispatcher.cs Test helper implementing IDispatcher synchronously
InMemorySettings.cs Test helper implementing ISettings with in-memory dictionary
Uno.Extensions.Core.WinUI.csproj Added InternalsVisibleTo for the new test project
Uno.Extensions.RuntimeTests.csproj Added project reference to the new test project

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

You can also share your feedback on Copilot code review. Take the survey.

@github-actions
Copy link

Azure Static Web Apps: Your stage site is ready! Visit it here: https://delightful-moss-0c5b8040f-3037.eastus2.azurestaticapps.net

@rajamatt rajamatt marked this pull request as ready for review March 10, 2026 12:59
@github-actions
Copy link

Azure Static Web Apps: Your stage site is ready! Visit it here: https://delightful-moss-0c5b8040f-3037.eastus2.azurestaticapps.net

@kazo0 kazo0 merged commit 2c0c5e0 into main Mar 10, 2026
15 checks passed
@kazo0 kazo0 deleted the dev/mara/fix-os-theme branch March 10, 2026 15:22
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.

3 participants