From d51398b95b4dd76931072ac7d127f6b6769f5ad2 Mon Sep 17 00:00:00 2001 From: Samuel Chapleau Date: Tue, 6 Dec 2022 16:10:07 -0800 Subject: [PATCH] Add scale awareness to window centering --- .../peek/Peek.UI/Extensions/WindowExtensions.cs | 15 ++++++++++++--- src/modules/peek/Peek.UI/MainWindow.xaml.cs | 15 ++++++++++++--- src/modules/peek/Peek.UI/NativeMethods.txt | 3 ++- 3 files changed, 26 insertions(+), 7 deletions(-) diff --git a/src/modules/peek/Peek.UI/Extensions/WindowExtensions.cs b/src/modules/peek/Peek.UI/Extensions/WindowExtensions.cs index 858e449676d..af3091e7ff4 100644 --- a/src/modules/peek/Peek.UI/Extensions/WindowExtensions.cs +++ b/src/modules/peek/Peek.UI/Extensions/WindowExtensions.cs @@ -4,8 +4,8 @@ namespace Peek.UI.Extensions { - using System.Drawing; using Microsoft.UI.Xaml; + using Windows.Foundation; using Windows.Win32; using Windows.Win32.Foundation; using Windows.Win32.Graphics.Gdi; @@ -20,10 +20,19 @@ public static Size GetMonitorSize(this Window window) MONITORINFO info = new (); info.cbSize = 40; PInvoke.GetMonitorInfo(hwndDesktop, ref info); - var monitorWidth = info.rcMonitor.left + info.rcMonitor.right; - var monitorHeight = info.rcMonitor.bottom + info.rcMonitor.top; + double monitorWidth = info.rcMonitor.left + info.rcMonitor.right; + double monitorHeight = info.rcMonitor.bottom + info.rcMonitor.top; return new Size(monitorWidth, monitorHeight); } + + public static double GetMonitorScale(this Window window) + { + var hwnd = new HWND(window.GetWindowHandle()); + var dpi = PInvoke.GetDpiForWindow(new HWND(hwnd)); + double scalingFactor = dpi / 96d; + + return scalingFactor; + } } } diff --git a/src/modules/peek/Peek.UI/MainWindow.xaml.cs b/src/modules/peek/Peek.UI/MainWindow.xaml.cs index 0ffc4af95ce..8ee2d66d039 100644 --- a/src/modules/peek/Peek.UI/MainWindow.xaml.cs +++ b/src/modules/peek/Peek.UI/MainWindow.xaml.cs @@ -21,6 +21,12 @@ namespace Peek.UI /// public sealed partial class MainWindow : WindowEx { + private const double MaxWindowToMonitorRatio = 0.80; + private const double MinWindowHeight = 500; + private const double MinWindowWidth = 680; + private const double WindowWidthContentPadding = 7; + private const double WindowHeightContentPadding = 16; + public MainWindow() { InitializeComponent(); @@ -72,14 +78,17 @@ private void FilePreviewer_PreviewSizeChanged(object sender, PreviewSizeChangedA // TODO: Use design-defined rules for adjusted window size var titleBarHeight = TitleBarControl.ActualHeight; - var maxContentSize = new Size(monitorSize.Width * 0.8, (monitorSize.Height - titleBarHeight) * 0.8); - var minContentSize = new Size(500, 500 - titleBarHeight); + var maxContentSize = new Size(monitorSize.Width * MaxWindowToMonitorRatio, (monitorSize.Height - titleBarHeight) * MaxWindowToMonitorRatio); + var minContentSize = new Size(MinWindowWidth, MinWindowHeight - titleBarHeight); var adjustedContentSize = requestedSize.Fit(maxContentSize, minContentSize); // TODO: Only re-center if window has not been resized by user (or use design-defined logic). // TODO: Investigate why portrait images do not perfectly fit edge-to-edge - this.CenterOnScreen(adjustedContentSize.Width, adjustedContentSize.Height + titleBarHeight); + var monitorScale = this.GetMonitorScale(); + var scaledWindowWidth = adjustedContentSize.Width / monitorScale; + var scaledWindowHeight = adjustedContentSize.Height / monitorScale; + this.CenterOnScreen(scaledWindowWidth + WindowHeightContentPadding, scaledWindowHeight + titleBarHeight + WindowWidthContentPadding); this.Show(); this.BringToFront(); } diff --git a/src/modules/peek/Peek.UI/NativeMethods.txt b/src/modules/peek/Peek.UI/NativeMethods.txt index bdccc2f7baf..b894c0f5eb6 100644 --- a/src/modules/peek/Peek.UI/NativeMethods.txt +++ b/src/modules/peek/Peek.UI/NativeMethods.txt @@ -1,2 +1,3 @@ MonitorFromWindow -GetMonitorInfo \ No newline at end of file +GetMonitorInfo +GetDpiForWindow \ No newline at end of file