Skip to content

Commit

Permalink
Improve image button rendering at high dpi
Browse files Browse the repository at this point in the history
  • Loading branch information
cyanfish committed Aug 29, 2024
1 parent dc9e442 commit ba74dc7
Showing 1 changed file with 21 additions and 16 deletions.
37 changes: 21 additions & 16 deletions NAPS2.Lib.WinForms/EtoForms/WinForms/WinFormsEtoPlatform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public override void ConfigureImageButton(Button button, ButtonFlags flags)
{
if (string.IsNullOrEmpty(button.Text))
{
button.MinimumSize = MinImageOnlyButtonSize;
AttachDpiDependency(button, scale => button.MinimumSize = Size.Round(MinImageOnlyButtonSize * scale));
var native = (WF.Button) button.ToNative();
native.TextImageRelation = WF.TextImageRelation.Overlay;
native.ImageAlign = SD.ContentAlignment.MiddleCenter;
Expand All @@ -49,7 +49,7 @@ public override void ConfigureImageButton(Button button, ButtonFlags flags)

bool largeText = flags.HasFlag(ButtonFlags.LargeText);
bool largeIcon = flags.HasFlag(ButtonFlags.LargeIcon);
button.MinimumSize = MinImageButtonSize;
AttachDpiDependency(button, scale => button.MinimumSize = Size.Round(MinImageButtonSize * scale));
if (button.ImagePosition == ButtonImagePosition.Left)
{
var native = (WF.Button) button.ToNative()!;
Expand All @@ -63,21 +63,26 @@ public override void ConfigureImageButton(Button button, ButtonFlags flags)
}

var imageWidth = largeIcon ? 32 : 16;
var textWidth = WF.TextRenderer.MeasureText(native.Text, native.Font).Width;
native.AutoSize = false;

if (largeText)
{
native.Padding = native.Padding with { Left = IMAGE_PADDING, Right = IMAGE_PADDING };
}
else
AttachDpiDependency(button, scale =>
{
var widthWithoutRightPadding = imageWidth + textWidth + IMAGE_PADDING + 15;
button.Width = Math.Max(widthWithoutRightPadding + IMAGE_PADDING,
ButtonHandler.DefaultMinimumSize.Width);
var rightPadding = IMAGE_PADDING + (native.Width - widthWithoutRightPadding - IMAGE_PADDING) / 2;
native.Padding = native.Padding with { Left = IMAGE_PADDING, Right = rightPadding };
}
var textWidth = WF.TextRenderer.MeasureText(native.Text, native.Font).Width;
int p = (int) Math.Round(IMAGE_PADDING * scale);
if (largeText)
{
native.Padding = native.Padding with { Left = p, Right = p };
}
else
{
var widthWithoutRightPadding = p + textWidth + (int) Math.Round((imageWidth + 15) * scale);
var width = Math.Max(widthWithoutRightPadding + p,
(int) Math.Round(ButtonHandler.DefaultMinimumSize.Width * scale));
button.Width = width;
var rightPadding = p + (width - widthWithoutRightPadding - p) / 2;
native.Padding = native.Padding with { Left = p, Right = rightPadding };
}
});
}
}

Expand Down Expand Up @@ -167,9 +172,9 @@ public override SizeF GetPreferredSize(Control control, SizeF availableSpace)
return GetPreferredSize(content, availableSpace);
}
}
var native = control.ToNative();
if (control.GetType() == typeof(Slider))
{
var native = control.ToNative();
var size = (SizeF) native.PreferredSize.ToEto();
// Work around a WinForms bug where the preferred size of a Slider/WF.TrackBar is based on the primary
// screen DPI, not the DPI of the screen it's actually on
Expand All @@ -183,7 +188,7 @@ public override SizeF GetPreferredSize(Control control, SizeF availableSpace)
}
var preferredSize = SizeF.Max(
base.GetPreferredSize(control, availableSpace),
control.ToNative().PreferredSize.ToEto());
native.PreferredSize.ToEto());
if (control.GetType() == typeof(DropDown))
{
// Work around a WinForms bug where the preferred height of a DropDown is incorrect
Expand Down

0 comments on commit ba74dc7

Please sign in to comment.