| 1 | using System.Drawing; |
| 2 | |
| 3 | namespace Cage.Windows; |
| 4 | |
| 5 | internal sealed record WindowPlacement(int X, int Y, int Width, int Height, int Dpi, bool Maximized); |
| 6 | |
| 7 | internal static class WindowLayout |
| 8 | { |
| 9 | public static Size Minimum(Rectangle work, int dpi) => new( |
| 10 | Math.Min(work.Width, (int)Math.Round(820 * dpi / 96.0)), |
| 11 | Math.Min(work.Height, (int)Math.Round(580 * dpi / 96.0))); |
| 12 | |
| 13 | public static Rectangle Bounds(Rectangle work, int dpi, WindowPlacement? saved) |
| 14 | { |
| 15 | var minimum = Minimum(work, dpi); |
| 16 | int width, height, x, y; |
| 17 | if (saved is { Width: > 0, Height: > 0, Dpi: > 0 }) |
| 18 | { |
| 19 | width = (int)Math.Round(saved.Width * dpi / (double)saved.Dpi); |
| 20 | height = (int)Math.Round(saved.Height * dpi / (double)saved.Dpi); |
| 21 | x = saved.X; y = saved.Y; |
| 22 | } |
| 23 | else |
| 24 | { |
| 25 | var scale = Math.Min(work.Width * .85 / 1280, work.Height * .85 / 850); |
| 26 | width = (int)Math.Round(1280 * scale); height = (int)Math.Round(850 * scale); |
| 27 | x = work.Left + (work.Width - width) / 2; |
| 28 | y = work.Top + (work.Height - height) / 2; |
| 29 | } |
| 30 | width = Math.Clamp(width, minimum.Width, work.Width); |
| 31 | height = Math.Clamp(height, minimum.Height, work.Height); |
| 32 | return new Rectangle(Math.Clamp(x, work.Left, work.Right - width), |
| 33 | Math.Clamp(y, work.Top, work.Bottom - height), width, height); |
| 34 | } |
| 35 | } |