Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Peek] Add scale awareness to window centering #22541

Merged
merged 1 commit into from
Dec 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions src/modules/peek/Peek.UI/Extensions/WindowExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible for the baseline dpi to not be 96 for some monitors? Seems like you can also use this api alternatively to avoid dealing with DPI at all: https://learn.microsoft.com/en-us/windows/win32/api/shellscalingapi/nf-shellscalingapi-getscalefactorformonitor


return scalingFactor;
}
}
}
15 changes: 12 additions & 3 deletions src/modules/peek/Peek.UI/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ namespace Peek.UI
/// </summary>
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();
Expand Down Expand Up @@ -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();
}
Expand Down
3 changes: 2 additions & 1 deletion src/modules/peek/Peek.UI/NativeMethods.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
MonitorFromWindow
GetMonitorInfo
GetMonitorInfo
GetDpiForWindow