Repository navigation
Refresh resolved theme brushes when WinUI applies a contrast theme - #1755
Alexandre Zollinger Chohfi (azchohfi) merged 3 commits into
Conversation
…1506) ThemeRef.Resolve already asks WinUI for the brush (#1633), so it picks the HighContrast dictionary while a contrast theme is on. What was missing is a re-render once WinUI has switched: ActualThemeChanged does not fire when a contrast theme is turned on, ColorValuesChanged arrives before WinUI applies it, and a window host without a chart never subscribed to it. Each host now merges a small theme probe (a brush with a {ThemeResource} reference into Default/Light/HighContrast colors) into its tree: the content root for ReactorHost, the control itself for ReactorHostControl. WinUI updates the probe when it applies a theme; on a later dispatcher turn the host checks whether the contrast state changed and runs a resource-refresh pass, which reaches memoized subtrees too. The design follows #1509's probe. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
- A probe that replaces another (root swap, or a root after an empty render) starts from the contrast state the host last rendered with and checks on its first turn, so a switch the old probe had not reported still refreshes. - Static ReaderForTest seam; fixtures cover the pending-check root swap, the empty-render detach, check coalescing, ThemeRef.Resolve in a render, and skip on a desktop in a contrast theme. - Regenerate the ThemeRef reference page; correct the design-doc sentence about ColorValuesChanged. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
🔵 Needs a closer look
WinUI brush resolution and refresh timing during contrast on/off and startup in contrast still require Windows desktop validation, since tests simulate contrast reads and only exercise real Light/Dark transitions.
No open issues 🎉
What changed in this PR
Updates Reactor’s WinUI hosts to refresh resolved theme brushes after WinUI applies a contrast-theme switch. This addresses the refresh gap in #1506, building on the resolution changes in #1633 without changing public APIs.
Changes:
- Adds per-host contrast probes that trigger cache invalidation and resource refreshes, including memoized subtrees.
- Handles probe cleanup and root replacement, with three selftest fixtures covering theme updates and refresh behavior.
- Updates API documentation, design guidance, and the changelog.
Manual validation with a real Windows contrast-theme switch remains pending.
| File | Description |
|---|---|
| tests/Reactor.AppTests.Host/SelfTest/SelfTestFixtureRegistry.cs | Registers the three contrast-refresh fixtures. |
| tests/Reactor.AppTests.Host/SelfTest/Fixtures/HighContrastRefreshFixtures.cs | Tests probe updates, host refreshes, and lifecycle handling. |
| src/Reactor/Hosting/ReactorHostControl.cs | Adds contrast probing and refresh handling to embedded hosts. |
| src/Reactor/Hosting/ReactorHost.cs | Adds window-host refreshes and probe state handoff across root changes. |
| src/Reactor/Hosting/HighContrastProbe.cs | Detects applied contrast-state changes through WinUI theme resources. |
| src/Reactor/Core/Theme.cs | Documents contrast behavior in ThemeRef.Resolve XML comments. |
| skills/design-docs/theme-aware-resources.md | Explains contrast resolution and refresh timing. |
| docs/guide/reference/theming/ThemeRef.md | Synchronizes generated reference documentation. |
| CHANGELOG.md | Records the contrast-theme refresh fix. |
Review effort: Balanced
📦 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 / |
🧪 Merged coverageCoverage for
No coverage change beyond the noise floor. ✅ ✅ higher / |
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
🔵 Needs a closer look
An unexplained 2.6-second contrast-switch delay and transient white frame tied to WinUI theme-walk and dispatcher timing require human accessibility/runtime validation on an interactive Windows desktop.
No open issues 🎉
Review effort: Balanced
Agent assessment
Self-reported assessment, not an independent review.
Assessed revision:
2bdfbb2d5187d6d181531f8895e394ef67248c9f↑ 20 new selftest checks pass, and mutation checks on the refresh, the dedupe, check coalescing and the root-swap handoff each turn checks red.
↑ Reactor.Tests and the Theme, Hosting, RenderErrorHandler and A11y_FocusTrap selftests pass.
↓ The override followed about 2.6 s after the built-in button when contrast was turned on, and the cause of that lag isn't established.
↓ CI on this head is pending.
ReactorHostsubscribes toColorValuesChangedonly inInitChartingState, and WinUI raisesActualThemeChangedonly when leaving a contrast theme.↑ The real switch confirms that resolution since #1633 picks contrast brushes, including at startup.
ButtonBackgroundoverride withTheme.CardBackground) and fixes all three cases with a real contrast theme.Summary
Brushes that Reactor resolves itself didn't follow a Windows contrast theme turned on or off while the app ran. That covers
.Resources(...)overrides withThemetokens,ThemeRef.Resolvein a render, and validation styling. Since #1633,ThemeRefresolution goes through WinUI and already picks theHighContrastdictionary, so what was missing was a re-render once WinUI has switched:ActualThemeChangeddoesn't fire when a contrast theme is turned on.UISettings.ColorValuesChangedfires before WinUI applies the switch, so a render it triggers still gets the old brushes.ReactorHostsubscribed toColorValuesChangedonly after a chart started.Each host now merges a small probe into its tree. It's a resource dictionary with a
Default,LightandHighContrastcolor, plus a brush bound to that color with{ThemeResource}.ReactorHostputs it on the content root, andReactorHostControlputs it on the control itself. WinUI updates the brush when it applies a theme. On a later dispatcher turn the host checks whether the contrast state changed. If it did, the host clears the resolution cache and runs a resource-refresh pass, the same pass asTheme.NotifyResourcesChanged, so memoized subtrees follow too. A probe that replaces another, after a root swap or an empty render, starts from the state the host last rendered with. That way a switch the old probe never reported still refreshes the host.The probe design comes from #1509 by Niels Laute (@niels9001), including its finding that a probe must not subscribe to the root's
Loaded. That PR predates #1633 and also changed how resolution reads the theme dictionaries. #1633 replaced that part, so this PR only carries the refresh half.Linked issue / spec
Fixes #1506. Follows #1633. Related to #1509.
Test plan
ReactorWindowwithout a chart, built from 507e3f2, showingButton("Built-in")andButton("Override").Resources(r => r.Set("ButtonBackground", Theme.CardBackground)). It logged the brush each button draws every 500 ms. The theme was High Contrast Black, and the original settings were restored afterwards. 2bdfbb2 only adds logging to two catch blocks.#B3FFFFFFto#FF202020at 23:48:30.8. The override followed to#FF202020at 23:48:33.5, about 2.6 s later. Main never switches it.#FF202020from the first sample. Not red.#B3FFFFFFat 23:49:17.8. The override showed#FFFFFFFFfor one sample, then#B3FFFFFFfrom 23:49:19.0.tests/Reactor.AppTests.Host/SelfTest/Fixtures/HighContrastRefreshFixtures.cs, 20 checks. They play the contrast read throughHighContrastProbe.ReaderForTest, plus one real WinUI theme walk:.Resources(...)override and aThemeRef.Resolvecall in a render. The negative control is a plain re-render, which leaves both stale. Two callbacks in one theme walk cause one check, the same state twice doesn't refresh, and switching off refreshes again.RequestRender: memo checks fail.HCHostCtrl_OneCheckPerThemeWalkfails.dotnet run --project tests/Reactor.AppTests.Host --no-build -c Debug -p:Platform=x64 -- --self-test --filter <f>forHosting_,Theme,RenderErrorHandlerandA11y_FocusTrap: 0 failures each.WindowLevel_RuntimeFlip_Topmost, which passes on its own (--filter "WindowLevel": 0 failures).dotnet test tests/Reactor.Tests --no-build -p:Platform=x64: 16,661 passed, 19 skipped, 0 failed. After the logging change,--filter-class "*SwallowedErrorAudit*": 11 passed.src/Reactorandtests/Reactor.AppTests.Host: 0 errors, no compiler warnings. I didn't run the fullReactor.slnxRelease build locally.dotnet run --project tools/Reactor.DocPipeline -- compile --reference-only: regeneratedThemeRef.md.Risk / breaking changes
ReactorHostControl, gets one extra merged dictionary inResources(keysReactorHighContrastProbeColorandReactorHighContrastProbeBrush). It moves with the root. If app code replaces the root's wholeResourcesdictionary, the probe goes with it and contrast refresh stops for that root. Reactor itself never does that.