Skip to content

Refresh resolved theme brushes when WinUI applies a contrast theme - #1755

Merged
Alexandre Zollinger Chohfi (azchohfi) merged 3 commits into
mainfrom
azchohfi-high-contrast-theme-overrides
Nov 27, 2026
Merged

Alexandre Zollinger Chohfi (azchohfi) merged 3 commits into
mainfrom
azchohfi-high-contrast-theme-overrides

Conversation

@azchohfi

@azchohfi Alexandre Zollinger Chohfi (azchohfi) commented Nov 27, 2026 •

Copy link
Copy Markdown
Collaborator

Agent assessment

Self-reported assessment, not an independent review.

Assessed revision: 2bdfbb2d5187d6d181531f8895e394ef67248c9f

Dimension Assessment Rationale
Change confidence 8.5/10 ↑ A real contrast switch passed all three cases on the desktop.
↑ 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.
Diagnosis confidence 9/10 ↑ On main, ReactorHost subscribes to ColorValuesChanged only in InitChartingState, and WinUI raises ActualThemeChanged only when leaving a contrast theme.
↑ The real switch confirms that resolution since #1633 picks contrast brushes, including at startup.
Evidence fit 9/10 ↑ This reproduces the issue's repro (a ButtonBackground override with Theme.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 with Theme tokens, ThemeRef.Resolve in a render, and validation styling. Since #1633, ThemeRef resolution goes through WinUI and already picks the HighContrast dictionary, so what was missing was a re-render once WinUI has switched:

  • ActualThemeChanged doesn't fire when a contrast theme is turned on.
  • UISettings.ColorValuesChanged fires before WinUI applies the switch, so a render it triggers still gets the old brushes.
  • A window ReactorHost subscribed to ColorValuesChanged only after a chart started.

Each host now merges a small probe into its tree. It's a resource dictionary with a Default, Light and HighContrast color, plus a brush bound to that color with {ThemeResource}. ReactorHost puts it on the content root, and ReactorHostControl puts 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 as Theme.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

  • Real contrast switch, by hand with the user present. The app was a light-theme ReactorWindow without a chart, built from 507e3f2, showing Button("Built-in") and Button("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.
    1. Contrast turned on while the app runs: the built-in button went from #B3FFFFFF to #FF202020 at 23:48:30.8. The override followed to #FF202020 at 23:48:33.5, about 2.6 s later. Main never switches it.
    2. App started with contrast on: both buttons were #FF202020 from the first sample. Not red.
    3. Contrast turned off while the app runs: the built-in button went back to #B3FFFFFF at 23:49:17.8. The override showed #FFFFFFFF for one sample, then #B3FFFFFF from 23:49:19.0.
  • New selftests in tests/Reactor.AppTests.Host/SelfTest/Fixtures/HighContrastRefreshFixtures.cs, 20 checks. They play the contrast read through HighContrastProbe.ReaderForTest, plus one real WinUI theme walk:
    • WinUI updates the probe on a real Light/Dark switch, and that switch alone doesn't refresh the host.
    • Embedded host: a played switch refreshes a memoized .Resources(...) override and a ThemeRef.Resolve call 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.
    • Window host without a chart: same refresh. The probe moves with a root swap, and a switch still pending when the root is swapped still refreshes. An empty render removes the probe, and a switch made with no probe refreshes the next root. Dispose removes the probe.
    • The fixtures skip when the desktop itself is in a contrast theme.
  • Mutation checks, each reverted afterwards:
    • Refresh replaced with a plain RequestRender: memo checks fail.
    • State dedupe removed: the Light/Dark, one-check and same-state checks fail.
    • Check coalescing removed: HCHostCtrl_OneCheckPerThemeWalk fails.
    • State handoff on root swap removed: both root-swap checks fail.
  • dotnet run --project tests/Reactor.AppTests.Host --no-build -c Debug -p:Platform=x64 -- --self-test --filter <f> for Hosting_, Theme, RenderErrorHandler and A11y_FocusTrap: 0 failures each.
  • Full selftest run: 1 failure, 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.
  • Release builds of src/Reactor and tests/Reactor.AppTests.Host: 0 errors, no compiler warnings. I didn't run the full Reactor.slnx Release build locally.
  • dotnet run --project tools/Reactor.DocPipeline -- compile --reference-only: regenerated ThemeRef.md.

Risk / breaking changes

  • No public API change.
  • The window host's content root, and each ReactorHostControl, gets one extra merged dictionary in Resources (keys ReactorHighContrastProbeColor and ReactorHighContrastProbeBrush). It moves with the root. If app code replaces the root's whole Resources dictionary, the probe goes with it and contrast refresh stops for that root. Reactor itself never does that.
  • A contrast switch causes one full resource-refresh render per host, after WinUI applies it. A Light/Dark switch is unchanged.
  • Resolved brushes follow a contrast switch a moment after WinUI's own controls: about 2.6 s when turning contrast on and about 1.2 s when turning it off, in the run above. Turning it off also showed one white frame first. That's the old contrast brush re-resolving its system color before the refresh replaces it. The cause of the delay isn't established yet.

…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>

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.

🔵 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

@github-actions

github-actions Bot commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

📦 Build metrics

Artifact sizes for 2bdfbb2 vs the base branch (6af6d00).

Packages (compressed .nupkg)

Artifact base PR Δ
Microsoft.UI.Reactor.nupkg 2.15 MB 2.15 MB +2.76 KB (+0.13%) ⚠️
Microsoft.UI.Reactor.Advanced.nupkg 530.2 KB 530.2 KB +17 B (0.00%) ≈
Microsoft.UI.Reactor.Devtools.nupkg 290.3 KB 290.3 KB +14 B (0.00%) ≈

Assemblies in Microsoft.UI.Reactor

Artifact base PR Δ
Microsoft.UI.Reactor.dll 2.91 MB 2.92 MB +6.50 KB (+0.22%) ⚠️
Reactor.Analyzers.dll 467.0 KB 467.0 KB +0 B (0.00%) ≈
Reactor.Localization.Generator.dll 16.0 KB 16.0 KB +0 B (0.00%) ≈
Reactor.Wrappers.Abstractions.dll 10.5 KB 10.5 KB +0 B (0.00%) ≈
Reactor.Wrappers.Generator.dll 164.0 KB 164.0 KB +0 B (0.00%) ≈

Assemblies in Microsoft.UI.Reactor.Advanced

Artifact base PR Δ
Microsoft.UI.Reactor.Advanced.dll 1.08 MB 1.08 MB +0 B (0.00%) ≈

Assemblies in Microsoft.UI.Reactor.Devtools

Artifact base PR Δ
Microsoft.UI.Reactor.Devtools.dll 791.0 KB 791.0 KB +0 B (0.00%) ≈

✅ smaller / ⚠️ larger / ≈ within noise. Sizes come from a Release dotnet pack on the CI runner: packages are the compressed .nupkg download size, assemblies the uncompressed DLL inside it.
workflow run.

@github-actions

github-actions Bot commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

🧪 Merged coverage

Coverage for 2bdfbb2 vs the base branch (2a7fa33) — unit + selftest merged.

Metric base PR Δ
Line 89.13% 89.11% -0.02 pp ≈
Branch 44.46% (18786/42255) 44.43% (18785/42281) -0.03 pp ≈

No coverage change beyond the noise floor. ✅

✅ higher / ⚠️ lower / ≈ within noise. Δ is in percentage points; coverage is unit + selftest merged (Debug x64) on the CI runner. Cobertura reports attached to the workflow run as artifacts.

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

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.

🔵 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

@azchohfi Alexandre Zollinger Chohfi (azchohfi) added ready-for-review Agent work and technical checks complete; awaiting review, not approval or merge and removed agent-preparing Agent is addressing feedback or completing required validation and CI labels Nov 27, 2026
@azchohfi
Alexandre Zollinger Chohfi (azchohfi) merged commit 93ecabb into main Nov 27, 2026
54 checks passed
@azchohfi
Alexandre Zollinger Chohfi (azchohfi) deleted the azchohfi-high-contrast-theme-overrides branch November 27, 2026 08:07
@azchohfi Alexandre Zollinger Chohfi (azchohfi) removed the ready-for-review Agent work and technical checks complete; awaiting review, not approval or merge label Nov 27, 2026
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.

[Bug] .Resources(...) theme overrides and ThemeRef.Resolve ignore high contrast

2 participants