Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,10 @@ private void PaintCore(PaintEventArgs e)

PaintImage(e, layout);

Color preferredTextColor = Control.ShouldSerializeForeColor()
Color preferredTextColor = Control.ShouldSerializeForeColor() || Control.ForeColor != Forms.Control.DefaultForeColor
? Control.ForeColor
: Application.IsDarkModeEnabled
? Color.FromArgb(0xF0, 0xF0, 0xF0)
? DarkModeButtonColors.DefaultColors.AcceptButtonTextColor // Use the default accept button text color in dark mode for checkboxes.
: SystemColors.WindowText;
Color disabledTextBackColor = Control.ShouldSerializeBackColor()
&& Control.BackColor.A == byte.MaxValue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ private Color GetButtonTextColor(
}

bool useEffectiveForeColor = _modern
? Control.ShouldSerializeForeColor()
? Control.ShouldSerializeForeColor() || Control.ForeColor != Forms.Control.DefaultForeColor
: Control.ForeColor != Forms.Control.DefaultForeColor;

if (useEffectiveForeColor)
Expand Down Expand Up @@ -95,11 +95,11 @@ private Color GetButtonBackColor(PushButtonState state)
}
else
{
bool hasExplicitBackColor = Control.ShouldSerializeBackColor();
bool hasUsableAmbientBackColor = !Control.BackColor.HasTransparency()
&& Control.BackColor != Forms.Control.DefaultBackColor;
bool hasCustomBackColor = _modern
? Control.ShouldSerializeBackColor()
: Control.BackColor != Forms.Control.DefaultBackColor;

if (hasExplicitBackColor || hasUsableAmbientBackColor)
if (hasCustomBackColor)
{
backColor = ButtonDarkModeRenderer.GetBackgroundColor(
state,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,10 @@ private void PaintCore(PaintEventArgs e)

PaintImage(e, layout);

Color preferredTextColor = Control.ShouldSerializeForeColor()
Color preferredTextColor = Control.ShouldSerializeForeColor() || Control.ForeColor != Forms.Control.DefaultForeColor
? Control.ForeColor
: Application.IsDarkModeEnabled
? Color.FromArgb(0xF0, 0xF0, 0xF0)
? DarkModeButtonColors.DefaultColors.AcceptButtonTextColor // Use the default accept button text color in dark mode for radio buttons.
: SystemColors.WindowText;
Color disabledTextBackColor = Control.ShouldSerializeBackColor()
&& Control.BackColor.A == byte.MaxValue
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Licensed to the .NET Foundation under one or more agreements.
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Drawing;
Expand All @@ -9,7 +9,7 @@
namespace System.Windows.Forms.Rendering.Button;

/// <summary>
/// Drives and renders a <see cref="Forms.ButtonBase"/> whose <see cref="Forms.ButtonBase.FlatStyle"/> is
/// Drives and renders a <see cref="ButtonBase"/> whose <see cref="ButtonBase.FlatStyle"/> is
/// <see cref="FlatStyle.Popup"/> when modern visual styles or dark mode are active, using the concave key-cap
/// look of <see cref="PopupButtonKeyCapRenderer"/>.
/// </summary>
Expand Down Expand Up @@ -119,6 +119,7 @@ public override void RenderControl(Graphics graphics)
Color faceColor;
Color foreColor;
Color borderColor;
bool useAutomaticForeColor = false;

if (highContrast)
{
Expand All @@ -138,9 +139,8 @@ public override void RenderControl(Graphics graphics)

faceColor = PopupButtonColorMath.Blend(baseColor, hoverColor, _hoverCurrent);
faceColor = PopupButtonColorMath.Blend(faceColor, pressedColor, _pressCurrent);
bool useAutomaticForeColor = button.EffectiveVisualStylesModeInternal >= VisualStylesMode.Net11
? !button.ShouldSerializeForeColor()
: button.ForeColor == Forms.Control.DefaultForeColor;
useAutomaticForeColor = button.ForeColor == Control.DefaultForeColor
&& !button.ShouldSerializeForeColor();
foreColor = !useAutomaticForeColor
? button.ForeColor
: _baseColorRenderer.GetTextColor(state, button.IsDefault, faceColor);
Expand All @@ -157,9 +157,7 @@ public override void RenderControl(Graphics graphics)
BackColor = faceColor,
ForeColor = foreColor,
SurfaceColor = button.Parent?.BackColor ?? button.BackColor,
UseAutomaticForeColor = button.EffectiveVisualStylesModeInternal >= VisualStylesMode.Net11
? !button.ShouldSerializeForeColor()
: button.ForeColor == Forms.Control.DefaultForeColor,
UseAutomaticForeColor = useAutomaticForeColor,
BorderColor = borderColor,
BorderWidth = flatAppearance.BorderSize,
Enabled = button.Enabled,
Expand Down Expand Up @@ -233,12 +231,14 @@ public override void RenderControl(Graphics graphics)

internal (Color BaseColor, Color HoverColor, Color PressedColor) GetStateColors()
{
Forms.ButtonBase button = Button;
ButtonBase button = Button;
FlatButtonAppearance flatAppearance = button.FlatAppearance;
_baseColorRenderer.DeviceDpi = button.DeviceDpi;
_baseColorRenderer.FlatAppearance = flatAppearance;

bool hasCustomBackColor = button.BackColor != Forms.Control.DefaultBackColor;
bool hasCustomBackColor = button.EffectiveVisualStylesModeInternal >= VisualStylesMode.Net11
? button.ShouldSerializeBackColor()
: button.BackColor != Control.DefaultBackColor;
Color baseColor = hasCustomBackColor
? button.BackColor
: _baseColorRenderer.GetBackgroundColor(PushButtonState.Normal, isDefault: false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -814,6 +814,32 @@ public void ButtonDarkModeAdapter_InteractionPaintStateStartsColorAnimation()
Assert.True(animator.IsRunning);
}

[WinFormsFact]
public void ButtonDarkModeAdapter_InheritedBackColor_UsesThemeStateColorInNet11()
{
using Panel parent = new() { BackColor = Color.Red };
using Button button = new()
{
FlatStyle = FlatStyle.Standard,
VisualStylesMode = VisualStylesMode.Net11
};
parent.Controls.Add(button);
ButtonInternal.ButtonDarkModeAdapter adapter = new(button);
dynamic accessor = adapter.TestAccessor.Dynamic;
ModernButtonDarkModeRenderer neutralRenderer = new()
{
DeviceDpi = button.DeviceDpi,
FlatAppearance = button.FlatAppearance
};

Color actual = (Color)accessor.GetButtonBackColor(VisualStyles.PushButtonState.Normal);

Assert.Equal(
neutralRenderer.GetBackgroundColor(VisualStyles.PushButtonState.Normal, isDefault: false),
actual);
Assert.False(button.ShouldSerializeBackColor());
}

public static TheoryData<Type, FlatStyle, ContentAlignment, TextImageRelation> ModernImageLayoutData
{
get
Expand Down Expand Up @@ -1276,7 +1302,7 @@ public void ButtonDarkModeAdapter_ExplicitForeColor_IsPreserved()
}

[WinFormsFact]
public void ButtonDarkModeAdapter_InheritedForeColor_UsesAutomaticContrast()
public void ButtonDarkModeAdapter_InheritedForeColor_IsPreservedInNet11()
{
using Panel parent = new() { ForeColor = Color.Red };
using Button button = new()
Expand All @@ -1295,7 +1321,7 @@ public void ButtonDarkModeAdapter_InheritedForeColor_UsesAutomaticContrast()
VisualStyles.PushButtonState.Normal,
Color.White);

Assert.Equal(Color.Black, actual);
Assert.Equal(Color.Red, actual);
Assert.False(button.ShouldSerializeForeColor());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,37 @@ public void AnimatedPopupButtonRenderer_CustomBackColor_DerivesNeutralStateColor
Assert.Equal(PopupButtonColorMath.TowardsContrast(button.BackColor, 0.12f), pressedColor);
}

[WinFormsFact]
public void AnimatedPopupButtonRenderer_InheritedBackColor_UsesThemeStateColorsInNet11()
{
using Panel parent = new() { BackColor = Color.Red };
using Button button = new()
{
FlatStyle = FlatStyle.Popup,
VisualStylesMode = VisualStylesMode.Net11
};
parent.Controls.Add(button);
using AnimatedPopupButtonRenderer renderer = new(button);
ModernButtonDarkModeRenderer neutralRenderer = new()
{
DeviceDpi = button.DeviceDpi,
FlatAppearance = button.FlatAppearance
};

(Color baseColor, Color hoverColor, Color pressedColor) = renderer.GetStateColors();

Assert.Equal(
neutralRenderer.GetBackgroundColor(VisualStyles.PushButtonState.Normal, isDefault: false),
baseColor);
Assert.Equal(
neutralRenderer.GetBackgroundColor(VisualStyles.PushButtonState.Hot, isDefault: false),
hoverColor);
Assert.Equal(
neutralRenderer.GetBackgroundColor(VisualStyles.PushButtonState.Pressed, isDefault: false),
pressedColor);
Assert.False(button.ShouldSerializeBackColor());
}

[WinFormsFact]
public void PopupButtonKeyCapRenderer_FocusedDefault_RendersWithoutThrow()
{
Expand Down