|
1 | 1 | using System; |
2 | 2 | using System.Runtime.InteropServices; |
3 | | -using Windows.Foundation; |
4 | 3 | using Windows.Graphics; |
5 | 4 | using Windows.System; |
6 | 5 | using Windows.UI.Core; |
| 6 | +using Coder.Desktop.App.Controls; |
7 | 7 | using Coder.Desktop.App.Models; |
8 | 8 | using Coder.Desktop.App.Services; |
9 | 9 | using Coder.Desktop.App.Views.Pages; |
@@ -48,6 +48,7 @@ public TrayWindow(IRpcController rpcController, ICredentialManager credentialMan |
48 | 48 | AppWindow.Hide(); |
49 | 49 | SystemBackdrop = new DesktopAcrylicBackdrop(); |
50 | 50 | Activated += Window_Activated; |
| 51 | + RootFrame.SizeChanged += RootFrame_SizeChanged; |
51 | 52 |
|
52 | 53 | rpcController.StateChanged += RpcController_StateChanged; |
53 | 54 | credentialManager.CredentialsChanged += CredentialManager_CredentialsChanged; |
@@ -120,55 +121,31 @@ public void SetRootFrame(Page page) |
120 | 121 | return; |
121 | 122 | } |
122 | 123 |
|
123 | | - if (ReferenceEquals(page, RootFrame.Content)) return; |
124 | | - |
125 | | - if (page.Content is not FrameworkElement newElement) |
126 | | - throw new Exception("Failed to get Page.Content as FrameworkElement on RootFrame navigation"); |
127 | | - newElement.SizeChanged += Content_SizeChanged; |
128 | | - |
129 | | - // Unset the previous event listener. |
130 | | - if (RootFrame.Content is Page { Content: FrameworkElement oldElement }) |
131 | | - oldElement.SizeChanged -= Content_SizeChanged; |
132 | | - |
133 | | - // Swap them out and reconfigure the window. |
134 | | - // We don't use RootFrame.Navigate here because it doesn't let you |
135 | | - // instantiate the page yourself. We also don't need forwards/backwards |
136 | | - // capabilities. |
137 | | - RootFrame.Content = page; |
138 | | - ResizeWindow(); |
139 | | - MoveWindow(); |
| 124 | + RootFrame.SetPage(page); |
140 | 125 | } |
141 | 126 |
|
142 | | - private void Content_SizeChanged(object sender, SizeChangedEventArgs e) |
| 127 | + private void RootFrame_SizeChanged(object sender, SizedFrameEventArgs e) |
143 | 128 | { |
144 | | - ResizeWindow(); |
| 129 | + ResizeWindow(e.NewSize.Height); |
145 | 130 | MoveWindow(); |
146 | 131 | } |
147 | 132 |
|
148 | 133 | private void ResizeWindow() |
149 | 134 | { |
150 | | - if (RootFrame.Content is not Page { Content: FrameworkElement frameworkElement }) |
151 | | - throw new Exception("Failed to get Content as FrameworkElement for window"); |
152 | | - |
153 | | - // Measure the desired size of the content |
154 | | - frameworkElement.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity)); |
155 | | - |
156 | | - // Adjust the AppWindow size |
157 | | - var scale = GetDisplayScale(); |
158 | | - var height = (int)(frameworkElement.ActualHeight * scale); |
159 | | - var width = (int)(WIDTH * scale); |
160 | | - AppWindow.Resize(new SizeInt32(width, height)); |
| 135 | + ResizeWindow(RootFrame.GetContentSize().Height); |
161 | 136 | } |
162 | 137 |
|
163 | | - private double GetDisplayScale() |
| 138 | + private void ResizeWindow(double height) |
164 | 139 | { |
165 | | - var hwnd = WindowNative.GetWindowHandle(this); |
166 | | - var dpi = NativeApi.GetDpiForWindow(hwnd); |
167 | | - if (dpi == 0) return 1; // assume scale of 1 |
168 | | - return dpi / 96.0; // 96 DPI == 1 |
| 140 | + if (height <= 0) height = 100; // will be resolved next frame typically |
| 141 | + |
| 142 | + var scale = DisplayScale.WindowScale(this); |
| 143 | + var newWidth = (int)(WIDTH * scale); |
| 144 | + var newHeight = (int)(height * scale); |
| 145 | + AppWindow.Resize(new SizeInt32(newWidth, newHeight)); |
169 | 146 | } |
170 | 147 |
|
171 | | - public void MoveResizeAndActivate() |
| 148 | + private void MoveResizeAndActivate() |
172 | 149 | { |
173 | 150 | SaveCursorPos(); |
174 | 151 | ResizeWindow(); |
@@ -268,9 +245,6 @@ public static class NativeApi |
268 | 245 | [DllImport("user32.dll")] |
269 | 246 | public static extern bool SetForegroundWindow(IntPtr hwnd); |
270 | 247 |
|
271 | | - [DllImport("user32.dll")] |
272 | | - public static extern int GetDpiForWindow(IntPtr hwnd); |
273 | | - |
274 | 248 | public struct POINT |
275 | 249 | { |
276 | 250 | public int X; |
|
0 commit comments