Skip to content

Commit

Permalink
Fix preferred size at high dpi for dropdowns and sliders
Browse files Browse the repository at this point in the history
  • Loading branch information
cyanfish committed Aug 29, 2024
1 parent d986f3a commit dc9e442
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions NAPS2.Lib.WinForms/EtoForms/WinForms/WinFormsEtoPlatform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,14 +169,27 @@ public override SizeF GetPreferredSize(Control control, SizeF availableSpace)
}
if (control.GetType() == typeof(Slider))
{
var size = control.ToNative().PreferredSize.ToEto();
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
var scaleDelta = (native.FindForm()!.DeviceDpi / 96f) / (Screen.PrimaryScreen.RealDPI / 72f);
size *= scaleDelta;
// We also want to correct for the idea that sliders should be fully scalable in orthogonal directions
// depending on the orientation
return ((Slider) control).Orientation == Orientation.Horizontal
? new SizeF(size.Height, size.Height)
: new SizeF(size.Width, size.Width);
? new SizeF(size.Height * scaleDelta, size.Height * scaleDelta)
: new SizeF(size.Width * scaleDelta, size.Width * scaleDelta);
}
return SizeF.Max(
var preferredSize = SizeF.Max(
base.GetPreferredSize(control, availableSpace),
control.ToNative().PreferredSize.ToEto());
if (control.GetType() == typeof(DropDown))
{
// Work around a WinForms bug where the preferred height of a DropDown is incorrect
preferredSize.Height = control.Height;
}
return preferredSize;
}

public override SizeF GetWrappedSize(Control control, int defaultWidth)
Expand Down

0 comments on commit dc9e442

Please sign in to comment.