/ src / settings-ui / Settings.UI / SettingsXAML / ScoobeWindow.xaml.cs
ScoobeWindow.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.Views;
  9  using Microsoft.UI;
 10  using Microsoft.UI.Windowing;
 11  using Microsoft.UI.Xaml;
 12  using PowerToys.Interop;
 13  using Windows.Graphics;
 14  using WinUIEx;
 15  using WinUIEx.Messaging;
 16  
 17  namespace Microsoft.PowerToys.Settings.UI
 18  {
 19      public sealed partial class ScoobeWindow : WindowEx, IDisposable
 20      {
 21          private const int ExpectedWidth = 1100;
 22          private const int ExpectedHeight = 700;
 23          private const int DefaultDPI = 96;
 24          private int _currentDPI;
 25          private WindowId _windowId;
 26          private IntPtr _hWnd;
 27          private AppWindow _appWindow;
 28          private bool disposedValue;
 29  
 30          public ScoobeWindow()
 31          {
 32              App.ThemeService.ThemeChanged += OnThemeChanged;
 33              App.ThemeService.ApplyTheme();
 34  
 35              this.InitializeComponent();
 36  
 37              _hWnd = WinRT.Interop.WindowNative.GetWindowHandle(this);
 38              _windowId = Win32Interop.GetWindowIdFromWindow(_hWnd);
 39              _appWindow = AppWindow.GetFromWindowId(_windowId);
 40              this.Activated += Window_Activated_SetIcon;
 41              this.ExtendsContentIntoTitleBar = true;
 42  
 43              var dpi = NativeMethods.GetDpiForWindow(_hWnd);
 44              _currentDPI = dpi;
 45              float scalingFactor = (float)dpi / DefaultDPI;
 46              int width = (int)(ExpectedWidth * scalingFactor);
 47              int height = (int)(ExpectedHeight * scalingFactor);
 48  
 49              SizeInt32 size;
 50              size.Width = width;
 51              size.Height = height;
 52              _appWindow.Resize(size);
 53  
 54              this.SizeChanged += ScoobeWindow_SizeChanged;
 55  
 56              var loader = Helpers.ResourceLoaderInstance.ResourceLoader;
 57              Title = loader.GetString("ScoobeWindow_Title");
 58  
 59              ScoobeShellPage.SetOpenMainWindowCallback((Type type) =>
 60              {
 61                  App.OpenSettingsWindow(type);
 62              });
 63          }
 64  
 65          private void Window_Activated_SetIcon(object sender, WindowActivatedEventArgs args)
 66          {
 67              // Set window icon
 68              _appWindow.SetIcon("Assets\\Settings\\icon.ico");
 69          }
 70  
 71          private void ScoobeWindow_SizeChanged(object sender, WindowSizeChangedEventArgs args)
 72          {
 73              var dpi = NativeMethods.GetDpiForWindow(_hWnd);
 74              if (_currentDPI != dpi)
 75              {
 76                  // Reacting to a DPI change. Should not cause a resize -> sizeChanged loop.
 77                  _currentDPI = dpi;
 78                  float scalingFactor = (float)dpi / DefaultDPI;
 79                  int width = (int)(ExpectedWidth * scalingFactor);
 80                  int height = (int)(ExpectedHeight * scalingFactor);
 81                  SizeInt32 size;
 82                  size.Width = width;
 83                  size.Height = height;
 84                  _appWindow.Resize(size);
 85              }
 86          }
 87  
 88          private void Window_Closed(object sender, WindowEventArgs args)
 89          {
 90              App.ClearScoobeWindow();
 91  
 92              var mainWindow = App.GetSettingsWindow();
 93              if (mainWindow != null)
 94              {
 95                  mainWindow.CloseHiddenWindow();
 96              }
 97  
 98              App.ThemeService.ThemeChanged -= OnThemeChanged;
 99          }
100  
101          private void OnThemeChanged(object sender, ElementTheme theme)
102          {
103              WindowHelper.SetTheme(this, theme);
104          }
105  
106          private void Dispose(bool disposing)
107          {
108              if (!disposedValue)
109              {
110                  disposedValue = true;
111              }
112          }
113  
114          public void Dispose()
115          {
116              // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
117              Dispose(disposing: true);
118              GC.SuppressFinalize(this);
119          }
120      }
121  }