From 566e35af1ee43498ba380332bacb0f205f1ff805 Mon Sep 17 00:00:00 2001 From: Niels Laute Date: Mon, 1 Sep 2025 15:25:11 +0200 Subject: [PATCH] [Registry Preview] Using TitleBar and AppWindow (#41418) ## Summary of the Pull Request - Using TitleBar instead of custom XAML - Using `AppWindow` (as part of WinUIEx) image ## PR Checklist - [x] Closes: #41414 - [ ] **Communication:** I've discussed this with core contributors already. If the work hasn't been agreed, this work might be rejected - [ ] **Tests:** Added/updated and all pass - [ ] **Localization:** All end-user-facing strings can be localized - [ ] **Dev docs:** Added/updated - [ ] **New binaries:** Added on the required places - [ ] [JSON for signing](https://github.com/microsoft/PowerToys/blob/main/.pipelines/ESRPSigning_core.json) for new binaries - [ ] [WXS for installer](https://github.com/microsoft/PowerToys/blob/main/installer/PowerToysSetup/Product.wxs) for new binaries and localization folder - [ ] [YML for CI pipeline](https://github.com/microsoft/PowerToys/blob/main/.pipelines/ci/templates/build-powertoys-steps.yml) for new test projects - [ ] [YML for signed pipeline](https://github.com/microsoft/PowerToys/blob/main/.pipelines/release.yml) - [ ] **Documentation updated:** If checked, please file a pull request on [our docs repo](https://github.com/MicrosoftDocs/windows-uwp/tree/docs/hub/powertoys) and link it here: #xxx ## Detailed Description of the Pull Request / Additional comments ## Validation Steps Performed --------- Co-authored-by: Jay <65828559+Jay-o-Way@users.noreply.github.com> --- .../RegistryPreview/MainWindow.Events.cs | 8 ++-- .../RegistryPreviewXAML/MainWindow.xaml | 39 +++++------------- .../RegistryPreviewXAML/MainWindow.xaml.cs | 41 +++++-------------- 3 files changed, 25 insertions(+), 63 deletions(-) diff --git a/src/modules/registrypreview/RegistryPreview/MainWindow.Events.cs b/src/modules/registrypreview/RegistryPreview/MainWindow.Events.cs index abcbbfd09b..fa283a153d 100644 --- a/src/modules/registrypreview/RegistryPreview/MainWindow.Events.cs +++ b/src/modules/registrypreview/RegistryPreview/MainWindow.Events.cs @@ -15,10 +15,10 @@ namespace RegistryPreview /// private void AppWindow_Closing(Microsoft.UI.Windowing.AppWindow sender, Microsoft.UI.Windowing.AppWindowClosingEventArgs args) { - jsonWindowPlacement.SetNamedValue("appWindow.Position.X", JsonValue.CreateNumberValue(appWindow.Position.X)); - jsonWindowPlacement.SetNamedValue("appWindow.Position.Y", JsonValue.CreateNumberValue(appWindow.Position.Y)); - jsonWindowPlacement.SetNamedValue("appWindow.Size.Width", JsonValue.CreateNumberValue(appWindow.Size.Width)); - jsonWindowPlacement.SetNamedValue("appWindow.Size.Height", JsonValue.CreateNumberValue(appWindow.Size.Height)); + jsonWindowPlacement.SetNamedValue("appWindow.Position.X", JsonValue.CreateNumberValue(AppWindow.Position.X)); + jsonWindowPlacement.SetNamedValue("appWindow.Position.Y", JsonValue.CreateNumberValue(AppWindow.Position.Y)); + jsonWindowPlacement.SetNamedValue("appWindow.Size.Width", JsonValue.CreateNumberValue(AppWindow.Size.Width)); + jsonWindowPlacement.SetNamedValue("appWindow.Size.Height", JsonValue.CreateNumberValue(AppWindow.Size.Height)); } /// diff --git a/src/modules/registrypreview/RegistryPreview/RegistryPreviewXAML/MainWindow.xaml b/src/modules/registrypreview/RegistryPreview/RegistryPreviewXAML/MainWindow.xaml index afe66038a0..2de21bfa64 100644 --- a/src/modules/registrypreview/RegistryPreview/RegistryPreviewXAML/MainWindow.xaml +++ b/src/modules/registrypreview/RegistryPreview/RegistryPreviewXAML/MainWindow.xaml @@ -15,38 +15,19 @@ - - - - - - - - - - - - + + + + + + - + \ No newline at end of file diff --git a/src/modules/registrypreview/RegistryPreview/RegistryPreviewXAML/MainWindow.xaml.cs b/src/modules/registrypreview/RegistryPreview/RegistryPreviewXAML/MainWindow.xaml.cs index 0a1083d00d..9874a150b0 100644 --- a/src/modules/registrypreview/RegistryPreview/RegistryPreviewXAML/MainWindow.xaml.cs +++ b/src/modules/registrypreview/RegistryPreview/RegistryPreviewXAML/MainWindow.xaml.cs @@ -6,6 +6,7 @@ using System; using ManagedCommon; using Microsoft.PowerToys.Telemetry; using Microsoft.UI; +using Microsoft.UI.Windowing; using Microsoft.UI.Xaml; using Microsoft.UI.Xaml.Controls; using Microsoft.UI.Xaml.Media; @@ -23,7 +24,6 @@ namespace RegistryPreview private const string APPNAME = "RegistryPreview"; // private members - private Microsoft.UI.Windowing.AppWindow appWindow; private JsonObject jsonWindowPlacement; private string settingsFolder = string.Empty; private string windowPlacementFile = "app-placement.json"; @@ -38,20 +38,15 @@ namespace RegistryPreview settingsFolder = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + @"\Microsoft\PowerToys\" + APPNAME; OpenWindowPlacementFile(settingsFolder, windowPlacementFile); - // Update the Win32 looking window with the correct icon (and grab the appWindow handle for later) - IntPtr windowHandle = this.GetWindowHandle(); - WindowId windowId = Win32Interop.GetWindowIdFromWindow(windowHandle); - appWindow = Microsoft.UI.Windowing.AppWindow.GetFromWindowId(windowId); - appWindow.SetIcon("Assets\\RegistryPreview\\RegistryPreview.ico"); - // TODO(stefan) - appWindow.Closing += AppWindow_Closing; - Activated += MainWindow_Activated; + AppWindow.Closing += AppWindow_Closing; // Extend the canvas to include the title bar so the app can support theming ExtendsContentIntoTitleBar = true; + IntPtr windowHandle = this.GetWindowHandle(); WindowHelpers.ForceTopBorder1PixelInsetOnWindows10(windowHandle); SetTitleBar(titleBar); + AppWindow.SetIcon("Assets\\RegistryPreview\\RegistryPreview.ico"); // if have settings, update the location of the window if (jsonWindowPlacement != null) @@ -66,7 +61,7 @@ namespace RegistryPreview // check to make sure the size values are reasonable before attempting to restore the last saved size if (size.Width >= 320 && size.Height >= 240) { - appWindow.Resize(size); + AppWindow.Resize(size); } } @@ -80,7 +75,7 @@ namespace RegistryPreview // check to make sure the move values are reasonable before attempting to restore the last saved location if (point.X >= 0 && point.Y >= 0) { - appWindow.Move(point); + AppWindow.Move(point); } } } @@ -92,20 +87,6 @@ namespace RegistryPreview PowerToysTelemetry.Log.WriteEvent(new RegistryPreviewEditorStartFinishEvent() { TimeStamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() }); } - private void MainWindow_Activated(object sender, WindowActivatedEventArgs args) - { - if (args.WindowActivationState == WindowActivationState.Deactivated) - { - titleBarText.Foreground = - (SolidColorBrush)Application.Current.Resources["WindowCaptionForegroundDisabled"]; - } - else - { - titleBarText.Foreground = - (SolidColorBrush)Application.Current.Resources["WindowCaptionForeground"]; - } - } - private void Grid_Loaded(object sender, RoutedEventArgs e) { MainGrid.Children.Add(MainPage); @@ -118,23 +99,23 @@ namespace RegistryPreview if (string.IsNullOrEmpty(filename)) { - titleBarText.Text = APPNAME; - appWindow.Title = APPNAME; + titleBar.Title = APPNAME; + AppWindow.Title = APPNAME; } else { string[] file = filename.Split('\\'); if (file.Length > 0) { - titleBarText.Text = file[file.Length - 1] + " - " + APPNAME; + titleBar.Title = file[file.Length - 1] + " - " + APPNAME; } else { - titleBarText.Text = filename + " - " + APPNAME; + titleBar.Title = filename + " - " + APPNAME; } // Continue to update the window's title, after updating the custom title bar - appWindow.Title = titleBarText.Text; + AppWindow.Title = titleBar.Title; } } }