Skip to content

Commit

Permalink
Mac: Fix vertical alignment of RadioButton and CheckBox on Big Sur
Browse files Browse the repository at this point in the history
  • Loading branch information
cwensley committed Apr 28, 2021
1 parent 8a4a521 commit 1758fc5
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 11 deletions.
25 changes: 14 additions & 11 deletions src/Eto.Mac/Forms/Controls/CheckBoxHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,27 +59,30 @@ public override NSControlSize ControlSize
}
}

nfloat ButtonOffset => (nfloat)Math.Max(0, Math.Ceiling((AttributedTitle.Size.Height - defaultHeight) / 2 - 1));
nfloat GetButtonOffset(CGRect rect)
{
// big sur and later calculate the position a wee differently..
if (MacVersion.IsAtLeast(10, 16))
return (nfloat)Math.Ceiling((rect.Height - AttributedTitle.Size.Height) / 2);

// catalina and older
return (nfloat)Math.Max(0, Math.Ceiling((AttributedTitle.Size.Height - defaultHeight) / 2 - 1)) + Offset;
}


public override CGRect DrawingRectForBounds(CGRect theRect)
{
var rect = base.DrawingRectForBounds(theRect);
if (!MacVersion.IsAtLeast(10, 16)) // big sur
{
rect.Y += ButtonOffset;
rect.Y += Offset;
}
// adjust drawing offset so the button goes in the right spot
rect.Y += GetButtonOffset(theRect);
return rect;
}

public override CGRect TitleRectForBounds(CGRect theRect)
{
var rect = base.TitleRectForBounds(theRect);
if (!MacVersion.IsAtLeast(10, 16)) // big sur
{
rect.Y -= ButtonOffset;
rect.Y -= Offset;
}
// adjust text offset so it goes back to where it should be
rect.Y -= GetButtonOffset(theRect);
return rect;
}
}
Expand Down
2 changes: 2 additions & 0 deletions test/Eto.Test/Sections/Controls/CheckBoxSection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public CheckBoxSection()
layout.Add(new CheckBox { Text = "With Larger Font", Font = SystemFonts.Label(40) });
layout.Add(new CheckBox { Text = "With Smaller Font", Font = SystemFonts.Label(6) });

layout.AddSeparateRow(new CheckBox { Text = "Should be aligned with text" }, new Panel { Size = new Size(50, 50), BackgroundColor = Colors.Green });

layout.Add(null, false, true);


Expand Down
2 changes: 2 additions & 0 deletions test/Eto.Test/Sections/Controls/RadioButtonSection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ public RadioButtonSection()
layout.Add(new RadioButton { Text = "With Larger Font", Font = SystemFonts.Label(40) });
layout.Add(new RadioButton { Text = "With Smaller Font", Font = SystemFonts.Label(6) });

layout.AddSeparateRow(new RadioButton { Text = "Should be aligned with text" }, new Panel { Size = new Size(50, 50), BackgroundColor = Colors.Green });

layout.Add(null, null, true);

Content = layout;
Expand Down

0 comments on commit 1758fc5

Please sign in to comment.