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

Fix issues with DPI scaling that were making PI offcenter and too large #3170

Merged
merged 4 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 2 additions & 0 deletions tools/PI/DevHome.PI/NativeMethods.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ GetMonitorInfo
GetMonitorInfo
GetOpenFileName
GetProcessInformation
GetScaleFactorForMonitor
GetTopWindow
GetWindowInfo
GetWindowLong
Expand Down Expand Up @@ -68,6 +69,7 @@ UnregisterHotKey
WaitForSingleObject
WindowFromPoint
CF_*
DEVICE_SCALE_FACTOR
DWMWA_*
DWMWINDOWATTRIBUTE
EVENT_*
Expand Down
19 changes: 12 additions & 7 deletions tools/PI/DevHome.PI/Views/BarWindowHorizontal.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.ComponentModel;
using System.Linq;
using System.Runtime.InteropServices;
using DevHome.Common.Extensions;
using DevHome.PI.Controls;
using DevHome.PI.Helpers;
Expand All @@ -15,8 +16,11 @@
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Windows.Foundation;
using Windows.UI.WindowManagement;
using Windows.UI.WindowManagement;
using Windows.Win32;
using Windows.Win32.Foundation;
using Windows.Win32.Graphics.Gdi;
using Windows.Win32.UI.Shell.Common;
using WinRT.Interop;
using WinUIEx;
using static DevHome.PI.Helpers.WindowHelper;
Expand Down Expand Up @@ -109,8 +113,9 @@ private void MainPanel_Loaded(object sender, RoutedEventArgs e)
SetRequestedTheme(t.Theme);

// Calculate the DPI scale.
var dpiWindow = HwndExtensions.GetDpiForWindow(ThisHwnd);
_dpiScale = dpiWindow / 96.0;
var monitor = PInvoke.MonitorFromWindow(ThisHwnd, MONITOR_FROM_FLAGS.MONITOR_DEFAULTTONEAREST);
PInvoke.GetScaleFactorForMonitor(monitor, out DEVICE_SCALE_FACTOR scaleFactor).ThrowOnFailure();
_dpiScale = (double)scaleFactor / 100;

SetDefaultPosition();

Expand Down Expand Up @@ -141,21 +146,21 @@ private void SetDefaultPosition()
_monitorRect = GetMonitorRectForWindow(_viewModel.ApplicationHwnd ?? ThisHwnd);
var screenWidth = _monitorRect.right - _monitorRect.left;
this.Move(
(int)(((screenWidth - Width) / 2) * _dpiScale) + _monitorRect.left,
(int)(_WindowPositionOffsetY * _dpiScale));
(int)((screenWidth - (Width * _dpiScale)) / 2) + _monitorRect.left,
(int)_WindowPositionOffsetY);

// Get the saved settings for the ExpandedView size. On first run, this will be
// the default 0,0, so we'll set the size proportional to the monitor size.
// Subsequently, it will be whatever size the user sets.
var settingSize = Settings.Default.ExpandedLargeSize;
if (settingSize.Width == 0)
{
settingSize.Width = _monitorRect.Width * 2 / 3;
settingSize.Width = (int)((_monitorRect.Width * 2) / (3 * _dpiScale));
}

if (settingSize.Height == 0)
{
settingSize.Height = _monitorRect.Height * 3 / 4;
settingSize.Height = (int)((_monitorRect.Height * 3) / (4 * _dpiScale));
}

Settings.Default.ExpandedLargeSize = settingSize;
Expand Down
Loading