Skip to content

Commit b83e93b

Browse files
Fix bug where other buttons would fail due to wrong adapter request.
1 parent 00939d7 commit b83e93b

File tree

3 files changed

+16
-17
lines changed

3 files changed

+16
-17
lines changed

src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/ButtonBase.cs

+4-3
Original file line numberDiff line numberDiff line change
@@ -984,7 +984,6 @@ internal ButtonBaseAdapter Adapter
984984
break;
985985
case FlatStyle.Flat:
986986
_adapter = CreateFlatAdapter();
987-
;
988987
break;
989988
default:
990989
Debug.Fail($"Unsupported FlatStyle: \"{FlatStyle}\"");
@@ -1020,8 +1019,10 @@ internal virtual ButtonBaseAdapter CreateStandardAdapter()
10201019

10211020
internal virtual ButtonBaseAdapter CreateDarkModeAdapter()
10221021
{
1023-
Debug.Fail("Derived classes need to provide a meaningful implementation.");
1024-
return null;
1022+
// When a button-derived class does not have a dedicated DarkMode adapter implementation,
1023+
// we're falling back to the standard adapter, to not _force_ the derived class to implement
1024+
// a dark mode adapter.
1025+
return CreateStandardAdapter();
10251026
}
10261027

10271028
internal virtual StringFormat CreateStringFormat()

src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/ButtonInternal/ButtonDarkModeAdapter.cs

+7-13
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,6 @@ namespace System.Windows.Forms.ButtonInternal;
88

99
internal class ButtonDarkModeAdapter : ButtonBaseAdapter
1010
{
11-
// Magic numbers for PushButtonState mapping
12-
private const PushButtonState DisabledPushButtonState = PushButtonState.Disabled;
13-
private const PushButtonState NormalPushButtonState = PushButtonState.Normal;
14-
private const PushButtonState PressedPushButtonState = PushButtonState.Pressed;
15-
private const PushButtonState HotPushButtonState = PushButtonState.Hot;
16-
1711
internal ButtonDarkModeAdapter(ButtonBase control) : base(control) { }
1812

1913
internal override void PaintUp(PaintEventArgs e, CheckState state)
@@ -55,7 +49,7 @@ internal override void PaintDown(PaintEventArgs e, CheckState state)
5549
e.Graphics,
5650
Control.ClientRectangle,
5751
Control.FlatStyle,
58-
PressedPushButtonState,
52+
PushButtonState.Pressed,
5953
Control.IsDefault,
6054
Control.Focused,
6155
Control.ShowFocusCues,
@@ -84,7 +78,7 @@ internal override void PaintOver(PaintEventArgs e, CheckState state)
8478
e.Graphics,
8579
Control.ClientRectangle,
8680
Control.FlatStyle,
87-
HotPushButtonState,
81+
PushButtonState.Hot,
8882
Control.IsDefault,
8983
Control.Focused,
9084
Control.ShowFocusCues,
@@ -122,13 +116,13 @@ private ColorOptions PaintDarkModeRender(IDeviceContext deviceContext) =>
122116
private static PushButtonState ToPushButtonState(CheckState state, bool enabled)
123117
{
124118
return !enabled
125-
? DisabledPushButtonState
119+
? PushButtonState.Disabled
126120
: state switch
127121
{
128-
CheckState.Unchecked => NormalPushButtonState,
129-
CheckState.Checked => PressedPushButtonState,
130-
CheckState.Indeterminate => HotPushButtonState,
131-
_ => NormalPushButtonState
122+
CheckState.Unchecked => PushButtonState.Normal,
123+
CheckState.Checked => PushButtonState.Pressed,
124+
CheckState.Indeterminate => PushButtonState.Hot,
125+
_ => PushButtonState.Normal
132126
};
133127
}
134128
}

src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/DarkModeRenderer/ButtonDarkModeRenderer.cs

+5-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,11 @@ internal static partial class ButtonDarkModeRenderer
4141
/// </summary>
4242
public static void DrawButtonBorder(Graphics graphics, GraphicsPath path, Color borderColor, int borderWidth)
4343
{
44-
using var borderPen = borderColor.GetCachedPenScope(borderWidth);
44+
using var borderPen = new Pen(borderColor, borderWidth)
45+
{
46+
Alignment = PenAlignment.Inset // Align pen inward
47+
};
48+
4549
graphics.DrawPath(borderPen, path);
4650
}
4751

0 commit comments

Comments
 (0)