29 lines
1.4 KiB
C#
29 lines
1.4 KiB
C#
namespace MatrixImageEditor;
|
|
|
|
internal static class ResponsiveLayout
|
|
{
|
|
public static void FitAndCenter(Form form, Size preferredLogical, Size minimumLogical, double workingAreaFraction, Screen? screen = null)
|
|
{
|
|
screen ??= Screen.FromControl(form);
|
|
var workingArea = screen.WorkingArea;
|
|
var scale = Math.Max(form.DeviceDpi, 96) / 96F;
|
|
var preferred = Scale(preferredLogical, scale);
|
|
var minimum = Scale(minimumLogical, scale);
|
|
var maximum = new Size(
|
|
Math.Max(320, (int)Math.Floor(workingArea.Width * workingAreaFraction)),
|
|
Math.Max(240, (int)Math.Floor(workingArea.Height * workingAreaFraction)));
|
|
|
|
var width = Math.Min(preferred.Width, maximum.Width);
|
|
var height = Math.Min(preferred.Height, maximum.Height);
|
|
form.MinimumSize = new Size(Math.Min(minimum.Width, maximum.Width), Math.Min(minimum.Height, maximum.Height));
|
|
form.Size = new Size(Math.Max(width, form.MinimumSize.Width), Math.Max(height, form.MinimumSize.Height));
|
|
form.Location = new Point(
|
|
workingArea.Left + Math.Max(0, (workingArea.Width - form.Width) / 2),
|
|
workingArea.Top + Math.Max(0, (workingArea.Height - form.Height) / 2));
|
|
}
|
|
|
|
private static Size Scale(Size logical, float scale) => new(
|
|
Math.Max(1, (int)Math.Round(logical.Width * scale)),
|
|
Math.Max(1, (int)Math.Round(logical.Height * scale)));
|
|
}
|