Skip to content

Commit ae8110f

Browse files
committed
Add WindowManager #132
1 parent b991181 commit ae8110f

File tree

2 files changed

+110
-0
lines changed

2 files changed

+110
-0
lines changed
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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+
}

dev/DevWinUI/Helpers/WindowHelper/WindowHelper.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -649,4 +649,10 @@ public static void SetRegion(Microsoft.UI.Xaml.Window window, ScreenRegion? regi
649649
PInvoke.DeleteObject(rgn);
650650
}
651651
}
652+
653+
public static void SetWindowSize(Microsoft.UI.Xaml.Window window, double width, double height)
654+
{
655+
var scale = WindowHelper.GetDpiForWindow(WindowNative.GetWindowHandle(window)) / 96f;
656+
window.AppWindow.Resize(new Windows.Graphics.SizeInt32((int)(width * scale), (int)(height * scale)));
657+
}
652658
}

0 commit comments

Comments
 (0)