OobeWindow.xaml.cs
1 // Copyright (c) Microsoft Corporation 2 // The Microsoft Corporation licenses this file to you under the MIT license. 3 // See the LICENSE file in the project root for more information. 4 5 using System; 6 7 using Microsoft.PowerToys.Settings.UI.Helpers; 8 using Microsoft.PowerToys.Settings.UI.OOBE.Enums; 9 using Microsoft.PowerToys.Settings.UI.OOBE.Views; 10 using Microsoft.UI; 11 using Microsoft.UI.Windowing; 12 using Microsoft.UI.Xaml; 13 using PowerToys.Interop; 14 using Windows.Graphics; 15 using WinUIEx; 16 using WinUIEx.Messaging; 17 18 namespace Microsoft.PowerToys.Settings.UI 19 { 20 /// <summary> 21 /// An empty window that can be used on its own or navigated to within a Frame. 22 /// </summary> 23 public sealed partial class OobeWindow : WindowEx, IDisposable 24 { 25 private PowerToysModules initialModule; 26 27 private const int ExpectedWidth = 1100; 28 private const int ExpectedHeight = 700; 29 private const int DefaultDPI = 96; 30 private int _currentDPI; 31 private WindowId _windowId; 32 private IntPtr _hWnd; 33 private AppWindow _appWindow; 34 private bool disposedValue; 35 36 public OobeWindow(PowerToysModules initialModule) 37 { 38 App.ThemeService.ThemeChanged += OnThemeChanged; 39 App.ThemeService.ApplyTheme(); 40 41 this.InitializeComponent(); 42 43 _hWnd = WinRT.Interop.WindowNative.GetWindowHandle(this); 44 _windowId = Win32Interop.GetWindowIdFromWindow(_hWnd); 45 _appWindow = AppWindow.GetFromWindowId(_windowId); 46 this.Activated += Window_Activated_SetIcon; 47 this.ExtendsContentIntoTitleBar = true; 48 49 var dpi = NativeMethods.GetDpiForWindow(_hWnd); 50 _currentDPI = dpi; 51 float scalingFactor = (float)dpi / DefaultDPI; 52 int width = (int)(ExpectedWidth * scalingFactor); 53 int height = (int)(ExpectedHeight * scalingFactor); 54 55 SizeInt32 size; 56 size.Width = width; 57 size.Height = height; 58 _appWindow.Resize(size); 59 60 this.initialModule = initialModule; 61 62 this.SizeChanged += OobeWindow_SizeChanged; 63 64 var loader = ResourceLoaderInstance.ResourceLoader; 65 Title = loader.GetString("OobeWindow_Title"); 66 67 if (shellPage != null) 68 { 69 shellPage.NavigateToModule(this.initialModule); 70 } 71 72 OobeShellPage.SetRunSharedEventCallback(() => 73 { 74 return Constants.PowerLauncherSharedEvent(); 75 }); 76 77 OobeShellPage.SetColorPickerSharedEventCallback(() => 78 { 79 return Constants.ShowColorPickerSharedEvent(); 80 }); 81 82 OobeShellPage.SetOpenMainWindowCallback((Type type) => 83 { 84 App.OpenSettingsWindow(type); 85 }); 86 } 87 88 public void SetAppWindow(PowerToysModules module) 89 { 90 if (shellPage != null) 91 { 92 shellPage.NavigateToModule(module); 93 } 94 } 95 96 private void Window_Activated_SetIcon(object sender, WindowActivatedEventArgs args) 97 { 98 // Set window icon 99 _appWindow.SetIcon("Assets\\Settings\\icon.ico"); 100 } 101 102 private void OobeWindow_SizeChanged(object sender, WindowSizeChangedEventArgs args) 103 { 104 var dpi = NativeMethods.GetDpiForWindow(_hWnd); 105 if (_currentDPI != dpi) 106 { 107 // Reacting to a DPI change. Should not cause a resize -> sizeChanged loop. 108 _currentDPI = dpi; 109 float scalingFactor = (float)dpi / DefaultDPI; 110 int width = (int)(ExpectedWidth * scalingFactor); 111 int height = (int)(ExpectedHeight * scalingFactor); 112 SizeInt32 size; 113 size.Width = width; 114 size.Height = height; 115 _appWindow.Resize(size); 116 } 117 } 118 119 private void Window_Closed(object sender, WindowEventArgs args) 120 { 121 App.ClearOobeWindow(); 122 123 var mainWindow = App.GetSettingsWindow(); 124 if (mainWindow != null) 125 { 126 mainWindow.CloseHiddenWindow(); 127 } 128 129 App.ThemeService.ThemeChanged -= OnThemeChanged; 130 } 131 132 private void OnThemeChanged(object sender, ElementTheme theme) 133 { 134 WindowHelper.SetTheme(this, theme); 135 } 136 137 private void Dispose(bool disposing) 138 { 139 if (!disposedValue) 140 { 141 disposedValue = true; 142 } 143 } 144 145 public void Dispose() 146 { 147 // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method 148 Dispose(disposing: true); 149 GC.SuppressFinalize(this); 150 } 151 } 152 }