|
| 1 | +using static DevWinUI.NativeValues; |
| 2 | + |
| 3 | +namespace DevWinUI; |
| 4 | + |
| 5 | +public partial class WindowManager : IDisposable |
| 6 | +{ |
| 7 | + private readonly Window _window; |
| 8 | + private readonly WindowMessageMonitor _monitor; |
| 9 | + public double Width |
| 10 | + { |
| 11 | + get => _window.AppWindow.Size.Width / (WindowHelper.GetDpiForWindow(_window) / 96d); |
| 12 | + set => WindowHelper.SetWindowSize(_window, value, Height); |
| 13 | + } |
| 14 | + |
| 15 | + public double Height |
| 16 | + { |
| 17 | + get => _window.AppWindow.Size.Height / (WindowHelper.GetDpiForWindow(_window) / 96d); |
| 18 | + set => WindowHelper.SetWindowSize(_window, Width, value); |
| 19 | + } |
| 20 | + |
| 21 | + private double _minWidth = 136; |
| 22 | + public double MinWidth |
| 23 | + { |
| 24 | + get => _minWidth; |
| 25 | + set |
| 26 | + { |
| 27 | + _minWidth = value; |
| 28 | + if (Width < value) |
| 29 | + Width = value; |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + private double _minHeight = 39; |
| 34 | + public double MinHeight |
| 35 | + { |
| 36 | + get => _minHeight; |
| 37 | + set |
| 38 | + { |
| 39 | + _minHeight = value; |
| 40 | + if (Height < value) |
| 41 | + Height = value; |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + private double _maxWidth = 0; |
| 46 | + |
| 47 | + public double MaxWidth |
| 48 | + { |
| 49 | + get => _maxWidth; |
| 50 | + set |
| 51 | + { |
| 52 | + if (value < 0) throw new ArgumentOutOfRangeException(nameof(value)); |
| 53 | + _maxWidth = value; |
| 54 | + if (value > 0 && Width > value) |
| 55 | + Width = value; |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + private double _maxHeight = 0; |
| 60 | + |
| 61 | + public double MaxHeight |
| 62 | + { |
| 63 | + get => _maxHeight; |
| 64 | + set |
| 65 | + { |
| 66 | + if (value < 0) throw new ArgumentOutOfRangeException(nameof(value)); |
| 67 | + _maxHeight = value; |
| 68 | + if (value > 0 && Height > value) |
| 69 | + Height = value; |
| 70 | + } |
| 71 | + } |
| 72 | + public WindowManager(Window window) |
| 73 | + { |
| 74 | + _window = window; |
| 75 | + |
| 76 | + _monitor = new WindowMessageMonitor(_window); |
| 77 | + _monitor.WindowMessageReceived -= OnWindowMessageReceived; |
| 78 | + _monitor.WindowMessageReceived += OnWindowMessageReceived; |
| 79 | + } |
| 80 | + |
| 81 | + private unsafe void OnWindowMessageReceived(object sender, WindowMessageEventArgs e) |
| 82 | + { |
| 83 | + if (e.MessageType == (uint)NativeValues.WindowMessage.WM_GETMINMAXINFO) |
| 84 | + { |
| 85 | + MINMAXINFO* rect2 = (MINMAXINFO*)e.Message.LParam; |
| 86 | + var currentDpi = WindowHelper.GetDpiForWindow(_window); |
| 87 | + |
| 88 | + // Restrict min-size |
| 89 | + rect2->ptMinTrackSize.X = (int)(Math.Max(MinWidth * (currentDpi / 96f), rect2->ptMinTrackSize.X)); |
| 90 | + rect2->ptMinTrackSize.Y = (int)(Math.Max(MinHeight * (currentDpi / 96f), rect2->ptMinTrackSize.Y)); |
| 91 | + // Restrict max-size |
| 92 | + if (!double.IsNaN(MaxWidth) && MaxWidth > 0) |
| 93 | + rect2->ptMaxTrackSize.X = (int)(Math.Min(Math.Max(MaxWidth, MinWidth) * (currentDpi / 96f), rect2->ptMaxTrackSize.X)); |
| 94 | + if (!double.IsNaN(MaxHeight) && MaxHeight > 0) |
| 95 | + rect2->ptMaxTrackSize.Y = (int)(Math.Min(Math.Max(MaxHeight, MinHeight) * (currentDpi / 96f), rect2->ptMaxTrackSize.Y)); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + public void Dispose() |
| 100 | + { |
| 101 | + _monitor.WindowMessageReceived -= OnWindowMessageReceived; |
| 102 | + _monitor.Dispose(); |
| 103 | + } |
| 104 | +} |
0 commit comments