Skip to content

Fix ToggleSwitch checked thumb color in light and dark modes - #15270

Merged
SimonZhao888 merged 8 commits into
dotnet:mainfrom
LeafShi1:Fix_15125_update_color_of_ToggleSwitchthumb
Nov 10, 2026
Merged

SimonZhao888 merged 8 commits into
dotnet:mainfrom
LeafShi1:Fix_15125_update_color_of_ToggleSwitchthumb

Conversation

@LeafShi1

@LeafShi1 LeafShi1 commented Oct 15, 2026 •

Copy link
Copy Markdown
Member

Fixes #15125

Root cause

The ToggleSwitch renderer selected the thumb color dynamically based on the current track color. As a result, the checked thumb could become black or white depending on the configured accent color rather than following the Windows theme-specific color behavior.

This caused inconsistent rendering across accent colors: the checked thumb could appear black in Light Mode or white in Dark Mode.

Proposed changes

  • Update the toggle switch thumb and unchecked border colors for light and dark modes, and refine the unchecked track color in light mode.
  • Preserve the checked thumb color during focus transitions.
  • Adjust the checked track accent color when needed to maintain at least a 3:1 contrast ratio against the thumb after focus shading is applied.
  • Use the same color for the track and border in the checked state.
  • Calculate the thumb-to-track contrast using the base colors, excluding focus shadows.
  • When the slider is at either the left or right endpoint, there is a 1-logical-pixel gap from the corresponding inner border;,this gap scales with DPI.

Customer Impact

  • Applications using Appearance.ToggleSwitch will render checked CheckBox and RadioButton thumbs consistently with the corresponding Windows color mode.

Regression?

  • No

Risk

  • Minimal

Screenshots

Before

The checked thumb color was dynamically selected according to its contrast with the track.

LightMode:

LightMode_beforeChanges.mp4

DarkMode:

DarkMode_beforeChanges.mp4

After

Light Mode always renders the checked thumb in white. Dark Mode always renders the checked thumb in black.

LightMode:
image

DarkMode:
image

Test methodology

  • Manually

Test environment(s)

  • .net 12.0.0-beta.26513.107

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.

🟡 Changes recommended

Focus shading and contrast regressions remain unresolved, and the new rendering behavior lacks targeted regression tests.

3 open findings
What changed in this PR

Updates the shared ToggleSwitch renderer for CheckBox and RadioButton to address #15125 with theme-specific checked-thumb colors.

Changes:

  • Uses white checked thumbs in light mode and black in dark mode.
  • Blends unchecked and checked thumb colors during transitions.
  • Preserves High Contrast and disabled-state rendering.
File Description
src/​System.Windows.Forms/​System/​Windows/​Forms/​Rendering/​CheckBox/​AnimatedToggleSwitchRenderer.cs Changes thumb-color selection and animation blending.

🧠 Review effort: Balanced


💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@codecov

codecov Bot commented Oct 15, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 37.84861%. Comparing base (c4891de) to head (1810759).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@              Coverage Diff              @@
##                main      #15270   +/-   ##
=============================================
  Coverage   37.84861%   37.84861%           
=============================================
  Files            246         246           
  Lines           9789        9789           
  Branches        1031        1031           
=============================================
  Hits            3705        3705           
  Misses          5920        5920           
  Partials         164         164           
Flag Coverage Δ
Debug 37.84861% <ø> (ø)
production 40.04608% <ø> (ø)
test 20.64923% <ø> (ø)
unit 40.04608% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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.

🟡 Changes recommended

The contrast calculation permits unfocused checked thumbs to fall below the intended 3:1 minimum.

1 open finding
3 resolved since last review

🧠 Review effort: Balanced

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.

🟡 Changes recommended

The spacing assertion contradicts the implementation, and focus shading can reduce thumb contrast below 3:1.

2 open issues
1 resolved since last review

Review effort: Balanced

@SimonZhao888

Copy link
Copy Markdown
Member

This PR is primarily described as a color fix, but GetThumbBounds also changes the thumb's resting positions and reduces its travel distance. Could we either document why this geometry change is required for the color fix?

And should add coverage for 100%, 150%, and 200% DPI, RTL, hover, and unusually small switch bounds.

@LeafShi1

Copy link
Copy Markdown
Member Author

This PR is primarily described as a color fix, but GetThumbBounds also changes the thumb's resting positions and reduces its travel distance. Could we either document why this geometry change is required for the color fix?

And should add coverage for 100%, 150%, and 200% DPI, RTL, hover, and unusually small switch bounds.

The geometry change is intentional and related to the contrast fix. Without the inset, the thumb can touch the edge of the track and become directly adjacent to the parent control’s background. In dark mode, this can leave insufficient contrast between the thumb and that background. The inset keeps track color between them, and the resulting geometry is also intended to more closely match the Windows toggle-switch UI.

I’ve added automated coverage for 100%, 150%, and 200% DPI, LTR/RTL, checked/unchecked states, hover, intermediate animation positions, and unusually small control bounds. The tests verify resting positions, travel distance, and the preserved gap.

@SimonZhao888 SimonZhao888 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks good to me!

@LeafShi1 LeafShi1 added the waiting-review This item is waiting on review by one or more members of team label Oct 20, 2026
@KlausLoeffelmann

Copy link
Copy Markdown
Member

Yes - the inset was off, my bad, and I absolute 2nd it!
Merge this as soon as possible, and we would need a backport to .NET 11.

Could you also do me one favor:
We need to speed-up the Animation for the Buttons. They just take too long for the state change.
I think, we can pretty much half the time they need. I don't know when this regressed - but I did not notice since earlier.

@KlausLoeffelmann KlausLoeffelmann left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Perfect, thanks!

@SimonZhao888
SimonZhao888 merged commit 64b6545 into dotnet:main Nov 10, 2026
8 checks passed
@dotnet-policy-service dotnet-policy-service Bot removed the waiting-review This item is waiting on review by one or more members of team label Nov 10, 2026
SimonZhao888 pushed a commit to KlausLoeffelmann/winforms that referenced this pull request Nov 10, 2026
…15270)

* Fix ToggleSwitch checked thumb color in light and dark modes

* Add unite test and update code floowing by the suggestion

* In the "checked" state, the border and the track use the same color

* Determine the contrast between the slider and the track based on the color excluding any focus shadows

* When the slider is at either the left or right endpoint, there is a 2-logical-pixel gap from the corresponding inner border; this gap scales with DPI.

* Ensure focus-shaded track meets minimum contrast

* Add DPI tests

* Fix test failure
@LeafShi1
LeafShi1 deleted the Fix_15125_update_color_of_ToggleSwitchthumb branch November 11, 2026 01:44
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Nov 11, 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.

CheckBox and RadioButton ToggleSwitch render the thumb in black instead of white when checked in non-Dark Mode

4 participants