Skip to content

Commit

Permalink
[OOBE][WinUI3]Scale OOBE window correctly (#17962)
Browse files Browse the repository at this point in the history
* Scale OOBE window

* [oobe]React to dpi changes (#17967)

Co-authored-by: Jaime Bernardo <jaime@janeasystems.com>
  • Loading branch information
stefansjfw and jaimecbernardo authored Apr 29, 2022
1 parent 63d2a5d commit ae0bf84
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 8 deletions.
3 changes: 3 additions & 0 deletions src/settings-ui/Settings.UI/Helpers/NativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ public static class NativeMethods
[DllImport("user32.dll")]
public static extern bool ShowWindow(System.IntPtr hWnd, int nCmdShow);

[DllImport("user32.dll")]
public static extern int GetDpiForWindow(System.IntPtr hWnd);

[DllImport("user32.dll")]
public static extern bool AllowSetForegroundWindow(int dwProcessId);

Expand Down
50 changes: 42 additions & 8 deletions src/settings-ui/Settings.UI/OobeWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using System;
using interop;
using Microsoft.PowerToys.Settings.UI.Helpers;
using Microsoft.PowerToys.Settings.UI.OOBE.Enums;
using Microsoft.PowerToys.Settings.UI.OOBE.Views;
using Microsoft.UI;
Expand All @@ -21,28 +22,44 @@ public sealed partial class OobeWindow : Window
{
private PowerToysModules initialModule;

private const int ExpectedWidth = 1100;
private const int ExpectedHeight = 700;
private const int DefaultDPI = 96;
private int _currentDPI;
private WindowId _windowId;
private IntPtr _hWnd;
private AppWindow _appWindow;

public OobeWindow(PowerToysModules initialModule)
{
this.InitializeComponent();

// Set window icon
var hWnd = WinRT.Interop.WindowNative.GetWindowHandle(this);
WindowId windowId = Win32Interop.GetWindowIdFromWindow(hWnd);
AppWindow appWindow = AppWindow.GetFromWindowId(windowId);
appWindow.SetIcon("icon.ico");
_hWnd = WinRT.Interop.WindowNative.GetWindowHandle(this);
_windowId = Win32Interop.GetWindowIdFromWindow(_hWnd);
_appWindow = AppWindow.GetFromWindowId(_windowId);
_appWindow.SetIcon("icon.ico");

OverlappedPresenter presenter = appWindow.Presenter as OverlappedPresenter;
OverlappedPresenter presenter = _appWindow.Presenter as OverlappedPresenter;
presenter.IsResizable = false;
presenter.IsMinimizable = false;
presenter.IsMaximizable = false;

var dpi = NativeMethods.GetDpiForWindow(_hWnd);
_currentDPI = dpi;
float scalingFactor = (float)dpi / DefaultDPI;
int width = (int)(ExpectedWidth * scalingFactor);
int height = (int)(ExpectedHeight * scalingFactor);

SizeInt32 size;
size.Width = 1650;
size.Height = 1050;
appWindow.Resize(size);
size.Width = width;
size.Height = height;
_appWindow.Resize(size);

this.initialModule = initialModule;

this.SizeChanged += OobeWindow_SizeChanged;

ResourceLoader loader = ResourceLoader.GetForViewIndependentUse();
Title = loader.GetString("OobeWindow_Title");

Expand All @@ -67,6 +84,23 @@ public OobeWindow(PowerToysModules initialModule)
});
}

private void OobeWindow_SizeChanged(object sender, WindowSizeChangedEventArgs args)
{
var dpi = NativeMethods.GetDpiForWindow(_hWnd);
if (_currentDPI != dpi)
{
// Reacting to a DPI change. Should not cause a resize -> sizeChanged loop.
_currentDPI = dpi;
float scalingFactor = (float)dpi / DefaultDPI;
int width = (int)(ExpectedWidth * scalingFactor);
int height = (int)(ExpectedHeight * scalingFactor);
SizeInt32 size;
size.Width = width;
size.Height = height;
_appWindow.Resize(size);
}
}

private void Window_Closed(object sender, WindowEventArgs args)
{
App.ClearOobeWindow();
Expand Down

0 comments on commit ae0bf84

Please sign in to comment.