Repository navigation
Resolve theme references from the HighContrast dictionary in contrast themes - #1509
Draft
Niels Laute (niels9001) wants to merge 7 commits into
Draft
Niels Laute (niels9001) wants to merge 7 commits into
Niels Laute (niels9001) wants to merge 7 commits into
Conversation
… themes
Reactor resolved ThemeRef brushes only from the Light/Dark/Default theme
dictionaries, so `.Resources(...)` theme overrides, `ThemeRef.Resolve(...)`
and Reactor's own resolved brushes kept their Light/Dark colors while a
Windows contrast theme was on. `Theme.*` modifiers were not affected: they
use native {ThemeResource} setters.
- ThemeRef resolution tries the "HighContrast" theme dictionary first while
a contrast theme is on, then falls back as before. The contrast setting is
read with SystemParametersInfo(SPI_GETHIGHCONTRAST) (no WinRT activation),
cached, re-read on every cache invalidation, and part of the cache key.
- ReactorWindow handles WM_SETTINGCHANGE / SPI_SETHIGHCONTRAST: when the
setting moved, Theme.NotifyResourcesChanged() refreshes every host. The
window host only listened to UISettings.ColorValuesChanged after a chart
had started. The existing ColorValuesChanged handlers use the same check.
- Tests: unit tests for the re-read and the one-refresh-per-change rule;
selftests for HighContrast-dictionary resolution (with fallback) and for a
`.Resources(...)` override following a WM_SETTINGCHANGE.
Fixes #1506
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Contributor
📦 Build metricsArtifact sizes for Packages (compressed .nupkg)
Assemblies in Microsoft.UI.Reactor
Assemblies in Microsoft.UI.Reactor.Advanced
Assemblies in Microsoft.UI.Reactor.Devtools
✅ smaller / |
| // The Fluent tokens themselves: XamlControlsResources ships a HighContrast dictionary. | ||
| var fluentContrast = ThemeRef.Resolve("CardBackgroundFillColorDefaultBrush", isDark: false); | ||
| H.Check("HC_On_FluentTokenUsesContrastBrush", | ||
| fluentLight is not null && fluentContrast is not null && !ReferenceEquals(fluentLight, fluentContrast), |
Contributor
🧪 Merged coverageCoverage for
No coverage change beyond the noise floor. ✅ ✅ higher / |
…nippet - A ReactorHost over a raw Window (tray flyout, app-built Window) has no ReactorWindow message monitor; it now watches WM_SETTINGCHANGE itself. New hosts read the contrast setting again instead of trusting a value cached for an earlier window. - The refresh dedupe tracks the setting the hosts were last fully refreshed for, not the last value any resolve saw: a partial re-render that already resolved with the new setting no longer suppresses the full refresh that memoized subtrees need. - HighContrastState.TryReadSystem makes a failing system call observable; a unit test checks it succeeds and agrees with AccessibilitySettings. - Selftests: the setting-change fixture no longer depends on the app theme and sends literal message values; new fixtures cover the raw-host monitor and an embedded ReactorHostControl refreshing a memoized subtree on ColorValuesChanged (with a negative control). - Move the new XML-doc sentence out of the theme-ref snippet region, so the compiled docs/guide/reactor-vs-xaml.md does not change. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
1 task done
6 of 8 tasks
11 of 12 tasks
The first version re-resolved theme references on WM_SETTINGCHANGE, before
WinUI had applied the contrast switch, and fell back to the Light, Dark and
Default dictionaries for keys without a contrast value. WinUI creates a theme
dictionary entry on its first read and resolves it for the theme it uses at
that moment, then keeps it for every element. Both paths could therefore fix
entries to the wrong theme's values: after a contrast theme was turned off in
an app that started with it, the overrides stayed red (#FF0000).
- Each host merges a small theme probe into its content root: a brush with a
{ThemeResource} reference that WinUI updates itself when it applies a
theme. HighContrastState follows what the probes report, so resolution and
the refresh happen after WinUI has switched. The system setting only stands
in until a probe has reported. The WM_SETTINGCHANGE hooks are removed.
- While WinUI is in a contrast theme, resolution reads only the HighContrast
dictionaries, and a key without a contrast value resolves to nothing. The
plain TryGetValue fallback is skipped then too, because it reads the theme
dictionaries of the theme WinUI uses.
- Unit tests and selftests for the new state, the probe and the resolver;
CHANGELOG and the high-contrast design note updated.
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
| if (dictionary is not null) | ||
| { | ||
| try { resources?.MergedDictionaries.Remove(dictionary); } | ||
| catch (global::System.Runtime.InteropServices.COMException) { } |
Review finding: a probe kept counting after its root left the live tree (empty content in ReactorHostControl, the built-in error panel, an unloaded or dropped host). WinUI no longer updates such a probe, so its last report kept HighContrastState on a stale value, and the next first-time dictionary reads could fix entries to the wrong theme. - A probe counts and reports only while its root is in the live tree. It is live on Loaded, and a tree check on a later turn decides at attach and after Unloaded: a raw host's root never gets Loaded, and its IsLoaded stays false. With no probe in the tree, the system setting stands in again. - ReactorHostControl drops the probe when its content becomes empty, and both hosts attach a new probe only when the root changes. - Selftests for both cases; the probe fixture checks each probe's own state, because other fixtures' probes come and go on later turns. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
| { | ||
| public override async Task RunAsync() | ||
| { | ||
| var host = new ReactorHostControl(); |
| _owner.Unloaded -= OnOwnerUnloaded; | ||
| _brush.UnregisterPropertyChangedCallback(SolidColorBrush.ColorProperty, _token); | ||
| try { _owner.Resources?.MergedDictionaries.Remove(_dictionary); } | ||
| catch (global::System.Runtime.InteropServices.COMException) { } |
Review finding on the previous commit: when a host swapped its root, the new probe counted only after its deferred tree check, but the old one was disposed at once. In a single-host app the count passed through zero, and the system setting stood in for that moment. That is exactly when it can be ahead of WinUI: a root that depends on the contrast setting swaps when Windows announces the switch. Reads of the HighContrast dictionary in that gap could fix its entries to light values. A probe that replaces a live one now counts at once. Its tree check still withdraws it if the new root is off the tree. New selftest check. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Niels Laute (niels9001)
added a commit
that referenced
this pull request
Nov 16, 2026
Brings the #1509 fix that follows contrast switches once WinUI applies them. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> # Conflicts: # tests/Reactor.AppTests.Host/SelfTest/SelfTestFixtureRegistry.cs
Niels Laute (niels9001)
added a commit
that referenced
this pull request
Nov 16, 2026
Brings the #1509 follow-up: a theme probe counts only while its root is in the live tree. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Niels Laute (niels9001)
added a commit
that referenced
this pull request
Nov 16, 2026
Brings the #1509 follow-up: a live theme probe hands over on a root swap. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
CI found that a raw host's root no longer loaded (A11y_FocusTrap_ContainerIsMountedFE). The probe subscribed to the root's Loaded and Unloaded events while the root entered the tree during a render, and that stops WinUI from raising Loaded for it at all, so IsLoaded stayed false and the app's own Loaded handlers on the root would never run. - The probe no longer subscribes to the root's events. It checks the tree itself on a later turn: at attach, and when its host says the tree may have changed (ReactorHostControl's own Loaded and Unloaded, and the built-in error panel taking the content's place). - Selftests: HC_RawHost_RootStillLoads pins the regression; the probe fixture polls for the live state, which a slow runner reaches later than one render. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Niels Laute (niels9001)
added a commit
that referenced
this pull request
Nov 16, 2026
Brings the #1509 fix that keeps the theme probe off the root's Loaded and Unloaded events. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
This branch has not been deployed
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.
Summary
While a Windows contrast theme was on, brushes that Reactor resolves itself kept their light or dark colors:
.Resources(...)theme overrides,ThemeRef.Resolve(...), and Reactor's own validation styling. The resolver only looked at theLight,DarkandDefaulttheme dictionaries.Theme.*modifiers such as.Background(Theme.CardBackground), andCard(), were not affected. They are applied as native{ThemeResource}style setters, and WinUI already resolves those for contrast themes.One WinUI behavior shapes the fix. WinUI creates a theme dictionary entry the first time something reads it, and resolves the references inside it for the theme WinUI uses at that moment. Then it keeps that entry for every element in the app. So when code reads a dictionary for another theme, that entry gets the wrong values, for the whole app. For example, the
Lightentry ofCardBackgroundFillColorDefaultBrush, read while WinUI is in a contrast theme, gets theHighContrastdictionary's#FF0000and stays red (see Screenshots).What changes:
ThemeRefresolution reads only theHighContrasttheme dictionaries, whateverisDarkasks for.Light,DarkorDefaultdictionary there, which could create those entries with contrast values. That fallback is gone.ResourceDictionary.TryGetValuealso reads the theme dictionaries of the theme it uses (a selftest pins this).Light,DefaultandHighContrastdictionaries define the same 2,683 keys. So everyTheme.*token and the validation brush have a contrast value, and only app-defined keys can resolve to nothing.WM_SETTINGCHANGE,UISettings.ColorValuesChanged,SPI_GETHIGHCONTRAST) before WinUI applies it. The first version of this PR refreshed onWM_SETTINGCHANGEand read the dictionaries in that gap: in an app that started with a contrast theme on, overrides turned red when it was turned off.{ThemeResource}reference to that color. This coversReactorHost(soReactorWindowand raw-window hosts) andReactorHostControl. WinUI updates the reference itself when it applies a theme. The probe then reports, on a later dispatcher turn, whether WinUI is in a contrast theme.Theme.NotifyResourcesChanged()re-renders every host, memoized subtrees included. Every host reports the same switch. Only the first report refreshes, and a first report before anything was resolved refreshes nothing.SystemParametersInfo(SPI_GETHIGHCONTRAST), cached and read again on every resolution-cache invalidation.WM_SETTINGCHANGEhooks of the first version are removed.skills/design-docs/theme-aware-resources.md.Screenshots
A light-theme app with three buttons: a built-in button,
.Background(Theme.CardBackground), and a.Resources(...)override ofButtonBackgroundwithTheme.CardBackground. The same code is built frommain(6889229) and from this PR (500ca22), with the Aquatic contrast theme. Each window shows its client area only.mainRow 4 on
main: the.Background(...)modifier is red as well. At startup,mainread theLightentry of the card brush while WinUI was in the contrast theme, and WinUI keeps that entry for the whole app. With this PR, all three buttons follow every switch.Linked issue / spec
Fixes #1506. This unblocks the high-contrast item of #1505 (SettingsCard and SettingsExpander).
Test plan
tests/Reactor.Tests/HighContrastThemeTests.cs:SystemParametersInfocall succeeds and agrees withAccessibilitySettings.HighContrast.tests/Reactor.AppTests.Host/SelfTest/Fixtures/HighContrastThemeFixtures.cs, 30 checks. Test seams play the contrast state, the setting Windows announces and what a probe reads, so the tests don't change the desktop.ThemeRef.Resolvereads only theHighContrastdictionary for light and dark requests, a key without a contrast value resolves to nothing, and the FluentCardBackgroundFillColorDefaultBrushresolves to its contrast brush. WinUI's plainTryGetValuereads the theme dictionaries, which is why the plain fallback is skipped.Loaded(see the CI finding below)..Resources(...)override keeps its colors when Windows announces a switch, and switches when aReactorWindow's probe reports that WinUI applied it, both ways. The same for a raw-windowReactorHost, and for an embeddedReactorHostControlincluding aMemosubtree. Negative control: a partial re-render leaves the memoized override stale.HC_Window_AnnouncedSwitchWaitsForWinUI,HC_Window_AnnouncedOffWaitsForWinUI,HC_RawHost_AppliedSwitchUsesContrast,HC_HostControl_MemoUsesContrast);HC_On_KeyWithoutContrastValueResolvesNothingfails;HC_Probe_CountsOnlyInTheLiveTreeandHC_Probe_GoesWithEmptyContentfail;HC_Probe_CountsOnlyInTheLiveTreefails;HC_Probe_ReplacementCountsAtOncefails;Loadedagain:HC_RawHost_RootStillLoadsfails.dotnet test tests/Reactor.Tests: passed (15,729 passed, 18 skipped, 0 failed).dotnet run --project tests/Reactor.AppTests.Host -- --self-test --filter "Theme_": 0 failures (158 checks).src/Reactor,tests/Reactor.Testsandtests/Reactor.AppTests.Host: 0 errors, 0 compiler warnings. Fortests/Reactor.Teststhe restore ran on its own, as in CI; a one-shot-c Releasebuild on this machine turns the proxied feed's NU1902/NU1903 package-audit warnings into errors. I did not run the fullReactor.slnxRelease build locally.code-reviewagent, in two rounds: two medium findings, both fixed.LoadedandUnloaded, and subscribing while the root enters the tree during a render stopped WinUI from raisingLoadedfor that root (A11y_FocusTrap_ContainerIsMountedFEfailed). Fixed in 500ca22: the probe subscribes to no root events, and the hosts ask it to check again instead.pr-reviewskill, run once on the first version:gpt-6.1-sol;.Resources(...)theme overrides is open, turn a contrast theme on and off (Settings > Accessibility > Contrast themes) and check that the overrides switch (checked with the demo in Screenshots, switching the contrast theme withSystemParametersInfo(SPI_SETHIGHCONTRAST)).Risk / breaking changes
ThemeRef.Resolve(key, isDark)returns the contrast brush while a contrast theme is on, as{ThemeResource}does. For a key without a contrast value it returnsnullduring a contrast theme; onmainit returns the light or dark brush. All built-inTheme.*tokens have contrast values.ReactorThemeProbeColorandReactorThemeProbeBrush) into its content root's resources, and moves it when the root changes. The probe subscribes to no events of the root: subscribing to a root'sLoadedwhile it enters the tree during a render stops WinUI from raisingLoadedfor that root.