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
@@ -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 Down Expand Up @@ -79,33 +79,22 @@ public override void RenderControl(Graphics graphics)
ToggleSwitchMetrics metrics = ToggleSwitchMetrics.Create(Control);
Size textSize = TextRenderer.MeasureText(Control.Text, Control.Font);
Rectangle contentBounds = ToggleSwitchMetrics.GetContentBounds(Control);
int totalHeight = Math.Max(textSize.Height, metrics.SwitchHeight);
int contentTop = contentBounds.Top + Math.Max(0, (contentBounds.Height - totalHeight) / 2);
int textY = contentTop + ((totalHeight - textSize.Height) / 2);
Rectangle switchBounds = GetSwitchBounds(
Control,
RtlTranslatedCheckAlign,
metrics,
textSize);
Rectangle textBounds = GetTextBounds(contentBounds, switchBounds, metrics, RtlTranslatedCheckAlign);

Comment thread
SimonZhao888 marked this conversation as resolved.
graphics.Clear(Control.BackColor);
PaintControlBackground(graphics);

if (contentBounds.Width <= 0 || contentBounds.Height <= 0)
{
return;
}

if (IsSwitchOnRight(RtlTranslatedCheckAlign))
{
int textX = Math.Max(contentBounds.Left, switchBounds.Left - metrics.TextGap - textSize.Width);
RenderSwitch(graphics, switchBounds, metrics);
RenderText(graphics, new Point(textX, textY));
}
else
{
RenderSwitch(graphics, switchBounds, metrics);
RenderText(graphics, new Point(contentBounds.Left + metrics.SwitchWidth + metrics.TextGap, textY));
}
RenderSwitch(graphics, switchBounds, metrics);
RenderText(graphics, textBounds);

if (Control.Focused && ShowFocusCues)
{
Expand Down Expand Up @@ -165,23 +154,86 @@ private static Rectangle GetSwitchBounds(
int totalHeight = Math.Max(textSize.Height, metrics.SwitchHeight);
int contentTop = contentBounds.Top + Math.Max(0, (contentBounds.Height - totalHeight) / 2);
int switchY = contentTop + ((totalHeight - metrics.SwitchHeight) / 2);
int switchX = IsSwitchOnRight(checkAlign)
? Math.Max(contentBounds.Left, contentBounds.Right - metrics.SwitchWidth)
: contentBounds.Left;
int minimumSwitchX = contentBounds.Left;
int maximumSwitchX = Math.Max(minimumSwitchX, contentBounds.Right - metrics.SwitchWidth);
int edgeInset = Math.Max(1, (metrics.BorderThickness / 2) + 1);
int targetSwitchX = IsSwitchOnRight(checkAlign)
? maximumSwitchX - edgeInset
: minimumSwitchX + edgeInset;
int switchX = Math.Max(minimumSwitchX, Math.Min(maximumSwitchX, targetSwitchX));

return new Rectangle(switchX, switchY, metrics.SwitchWidth, metrics.SwitchHeight);
}

private void RenderText(Graphics graphics, Point position)
internal static Rectangle GetTextBounds(
Control control,
ContentAlignment checkAlign,
ToggleSwitchMetrics metrics)
{
Size textSize = TextRenderer.MeasureText(control.Text, control.Font);
Rectangle contentBounds = ToggleSwitchMetrics.GetContentBounds(control);
Rectangle switchBounds = GetSwitchBounds(control, checkAlign, metrics, textSize);
return GetTextBounds(contentBounds, switchBounds, metrics, checkAlign);
}

private static Rectangle GetTextBounds(
Rectangle contentBounds,
Rectangle switchBounds,
ToggleSwitchMetrics metrics,
ContentAlignment checkAlign)
{
int thresholdExtension = Math.Max(1, metrics.BorderThickness / 2) + 1;
if (IsSwitchOnRight(checkAlign))
{
int right = Math.Min(
contentBounds.Right,
switchBounds.Left - Math.Max(0, metrics.TextGap - thresholdExtension));
right = Math.Max(contentBounds.Left, right);
return Rectangle.FromLTRB(contentBounds.Left, contentBounds.Top, right, contentBounds.Bottom);
}

int left = Math.Max(
contentBounds.Left,
switchBounds.Right + Math.Max(0, metrics.TextGap - thresholdExtension));
left = Math.Min(contentBounds.Right, left);
return Rectangle.FromLTRB(left, contentBounds.Top, contentBounds.Right, contentBounds.Bottom);
}

private void RenderText(Graphics graphics, Rectangle textBounds)
{
if (textBounds.Width <= 0 || textBounds.Height <= 0)
{
return;
}

TextFormatFlags flags = GetTextFormatFlags();

TextRenderer.DrawText(
graphics,
Control.Text,
Control.Font,
position,
GetTextColor());
textBounds,
GetTextColor(),
flags);
}

private TextFormatFlags GetTextFormatFlags() => Control switch
{
Forms.CheckBox { FlatStyle: FlatStyle.System } checkBox => ControlPaint.CreateTextFormatFlags(
checkBox,
checkBox.TextAlign,
checkBox.ShowToolTip,
checkBox.UseMnemonic),
Forms.RadioButton { FlatStyle: FlatStyle.System } radioButton => ControlPaint.CreateTextFormatFlags(
radioButton,
radioButton.TextAlign,
radioButton.ShowToolTip,
radioButton.UseMnemonic),
Forms.CheckBox checkBox => checkBox.CreateTextFormatFlags(),
Forms.RadioButton radioButton => radioButton.CreateTextFormatFlags(),
_ => TextFormatFlags.WordBreak | TextFormatFlags.TextBoxControl
};

internal Color GetTextColor()
=> Control.Enabled
? Control.ForeColor
Expand Down Expand Up @@ -329,6 +381,25 @@ private void EnsurePositionInitialized()
_positionInitialized = true;
}

private void PaintControlBackground(Graphics graphics)
{
if (Control.BackgroundImage is null && !Control.BackColor.HasTransparency())
{
graphics.Clear(Control.BackColor);
return;
}

Rectangle clipRectangle = Rectangle.Ceiling(graphics.ClipBounds);
clipRectangle.Intersect(Control.ClientRectangle);
if (clipRectangle.Width <= 0 || clipRectangle.Height <= 0)
{
return;
}

using PaintEventArgs paintEventArgs = new(graphics, clipRectangle);
Control.PaintBackground(paintEventArgs, clipRectangle);
}

private static float EaseOut(float value)
=> 1 - ((1 - value) * (1 - value));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -773,9 +773,51 @@ public void CheckBox_ToggleSwitch_CheckAlign_PositionsSwitch(
metrics);
Rectangle contentBounds = Rendering.CheckBox.ToggleSwitchMetrics.GetContentBounds(box);

Assert.Equal(
switchOnRight ? contentBounds.Right : contentBounds.Left,
switchOnRight ? switchBounds.Right : switchBounds.Left);
int edgeTolerance = Math.Max(1, (metrics.BorderThickness / 2) + 1);
if (switchOnRight)
{
Assert.InRange(
switchBounds.Right,
contentBounds.Right - edgeTolerance,
contentBounds.Right);
}
else
{
Assert.InRange(
switchBounds.Left,
contentBounds.Left,
contentBounds.Left + edgeTolerance);
}
}

[WinFormsFact]
public void CheckBox_ToggleSwitch_RtlLongText_AutoSizeFalse_TextBounds_DoNotOverlapSwitch()
{
using CheckBox box = new()
{
Appearance = Appearance.ToggleSwitch,
AutoSize = false,
RightToLeft = RightToLeft.Yes,
CheckAlign = ContentAlignment.MiddleLeft,
Text = "This is a very long toggle-switch label to verify RTL clipping does not overlap the switch glyph.",
Size = new Size(160, 30)
};

box.VisualStylesMode = VisualStylesMode.Net11;
box.CreateControl();

Rendering.CheckBox.ToggleSwitchMetrics metrics = Rendering.CheckBox.ToggleSwitchMetrics.Create(box);
Rectangle switchBounds = Rendering.CheckBox.AnimatedToggleSwitchRenderer.GetSwitchBounds(
box,
box.RtlTranslatedCheckAlign,
metrics);
Rectangle textBounds = Rendering.CheckBox.AnimatedToggleSwitchRenderer.GetTextBounds(
box,
box.RtlTranslatedCheckAlign,
metrics);
Rectangle overlap = Rectangle.Intersect(switchBounds, textBounds);

Assert.True(overlap.IsEmpty);
}

[WinFormsTheory]
Expand Down Expand Up @@ -950,6 +992,25 @@ public void CheckBox_ToggleSwitch_ThreeStateChange_UpdatesOwnerDraw()
Assert.False(box.GetStyle(ControlStyles.UserPaint));
}

[WinFormsFact]
public void CheckBox_ToggleSwitch_FlatStyleSystem_DrawToBitmap_DoesNotThrow()
{
using CheckBox box = new()
{
Appearance = Appearance.ToggleSwitch,
FlatStyle = FlatStyle.System,
VisualStylesMode = VisualStylesMode.Net11,
Text = "Toggle",
Size = new Size(120, 30)
};
using Bitmap bitmap = new(box.Width, box.Height);

Exception exception = Record.Exception(
() => box.DrawToBitmap(bitmap, new Rectangle(Point.Empty, box.Size)));

Assert.Null(exception);
}

private static int CountPixels(Bitmap bitmap, Color color)
{
int argb = color.ToArgb();
Expand Down Expand Up @@ -1377,10 +1438,10 @@ public void CheckBox_ProcessMnemonic_ValidCases(bool useMnemonic, char charCode,
}

[WinFormsTheory]
[InlineData(Appearance.Button, FlatStyle.Standard, "Test", 12, 8, 100, 20)]
[InlineData(Appearance.Normal, FlatStyle.System, "Test", 12, 8, 100, 20)]
[InlineData(Appearance.Normal, FlatStyle.Flat, "Test", 12, 8, 100, 20)]
[InlineData(Appearance.Normal, FlatStyle.Standard, "Test", 12, 8, 100, 20)]
[InlineData(Appearance.Button, FlatStyle.Standard, "Test", 12, 8, 100, 20)]
[InlineData(Appearance.Normal, FlatStyle.System, "Test", 12, 8, 100, 20)]
[InlineData(Appearance.Normal, FlatStyle.Flat, "Test", 12, 8, 100, 20)]
[InlineData(Appearance.Normal, FlatStyle.Standard, "Test", 12, 8, 100, 20)]
public void CheckBox_GetPreferredSizeCore_VariousStyles_ReturnsExpected(
Appearance appearance, FlatStyle flatStyle, string text, int fontSize, int padding, int width, int height)
{
Expand Down