Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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,14 +79,12 @@ public override void RenderControl(Graphics graphics)
ToggleSwitchMetrics metrics = ToggleSwitchMetrics.Create(Control);
Size textSize = TextRenderer.MeasureText(Control.Text, Control.Font);
Rectangle contentBounds = ToggleSwitchMetrics.GetContentBounds(Control);

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.

ToggleSwitchMetrics.GetPreferredSize still measures with the default TextRenderer.MeasureText(control.Text, control.Font) flags, while this path now measures and renders with flags that honor UseMnemonic. When UseMnemonic is false, a literal & is included during rendering but can still be treated as a mnemonic marker during preferred-size calculation. As a result, an auto-sized ToggleSwitch can be too narrow and clip or wrap the text (the separate text bounds should still prevent overlap with the switch). Could preferred-size calculation use the same flags, with a regression test such as Text = "A&B" and UseMnemonic = false?

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);

Expand All @@ -97,14 +95,13 @@ public override void RenderControl(Graphics graphics)

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));
RenderText(graphics, textBounds);
}
else
{
RenderSwitch(graphics, switchBounds, metrics);
RenderText(graphics, new Point(contentBounds.Left + metrics.SwitchWidth + metrics.TextGap, textY));
RenderText(graphics, textBounds);
}
Comment thread
SimonZhao888 marked this conversation as resolved.
Outdated

if (Control.Focused && ShowFocusCues)
Expand Down Expand Up @@ -165,23 +162,76 @@ 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 checkBox => checkBox.CreateTextFormatFlags(),
Forms.RadioButton radioButton => radioButton.CreateTextFormatFlags(),
_ => TextFormatFlags.WordBreak | TextFormatFlags.TextBoxControl
};

internal Color GetTextColor()
=> Control.Enabled
? Control.ForeColor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -773,9 +773,21 @@ 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);
const int EdgeTolerance = 2;
if (switchOnRight)
{
Assert.InRange(
switchBounds.Right,
contentBounds.Right - EdgeTolerance,
contentBounds.Right);
}
else
{
Assert.InRange(
switchBounds.Left,
contentBounds.Left,
contentBounds.Left + EdgeTolerance);
}
Comment thread
SimonZhao888 marked this conversation as resolved.
Outdated
}

[WinFormsTheory]
Expand Down