From 1758fc51a2444e5d708d7f863987593af54e21ce Mon Sep 17 00:00:00 2001 From: Curtis Wensley Date: Mon, 26 Apr 2021 19:20:36 -0700 Subject: [PATCH] Mac: Fix vertical alignment of RadioButton and CheckBox on Big Sur --- src/Eto.Mac/Forms/Controls/CheckBoxHandler.cs | 25 +++++++++++-------- .../Sections/Controls/CheckBoxSection.cs | 2 ++ .../Sections/Controls/RadioButtonSection.cs | 2 ++ 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/src/Eto.Mac/Forms/Controls/CheckBoxHandler.cs b/src/Eto.Mac/Forms/Controls/CheckBoxHandler.cs index 9e75456ee7..b2e28bc0fb 100644 --- a/src/Eto.Mac/Forms/Controls/CheckBoxHandler.cs +++ b/src/Eto.Mac/Forms/Controls/CheckBoxHandler.cs @@ -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; } } diff --git a/test/Eto.Test/Sections/Controls/CheckBoxSection.cs b/test/Eto.Test/Sections/Controls/CheckBoxSection.cs index 107e26996b..b26092be79 100644 --- a/test/Eto.Test/Sections/Controls/CheckBoxSection.cs +++ b/test/Eto.Test/Sections/Controls/CheckBoxSection.cs @@ -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); diff --git a/test/Eto.Test/Sections/Controls/RadioButtonSection.cs b/test/Eto.Test/Sections/Controls/RadioButtonSection.cs index 74ed16657d..4becf14233 100644 --- a/test/Eto.Test/Sections/Controls/RadioButtonSection.cs +++ b/test/Eto.Test/Sections/Controls/RadioButtonSection.cs @@ -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;