| 1 | // Copyright (C) Microsoft Corporation. All rights reserved. |
| 2 | |
| 3 | using System.Runtime.InteropServices; |
| 4 | using Microsoft.UI; |
| 5 | using Microsoft.UI.Xaml; |
| 6 | using Windows.UI; |
| 7 | using Windows.UI.ViewManagement; |
| 8 | |
| 9 | namespace WslSettings.Helpers; |
| 10 | |
| 11 | // Helper class to workaround custom title bar bugs. |
| 12 | // DISCLAIMER: The resource key names and color values used below are subject to change. Do not depend on them. |
| 13 | // https://github.com/microsoft/TemplateStudio/issues/4516 |
| 14 | internal class TitleBarHelper |
| 15 | { |
| 16 | private const int WAINACTIVE = 0x00; |
| 17 | private const int WAACTIVE = 0x01; |
| 18 | private const int WMACTIVATE = 0x0006; |
| 19 | |
| 20 | [DllImport("user32.dll")] |
| 21 | private static extern IntPtr GetActiveWindow(); |
| 22 | |
| 23 | [DllImport("user32.dll", CharSet = CharSet.Auto)] |
| 24 | private static extern IntPtr SendMessage(IntPtr hWnd, int msg, int wParam, IntPtr lParam); |
| 25 | |
| 26 | public static void UpdateTitleBar(Window window, ElementTheme theme) |
| 27 | { |
| 28 | if (window.ExtendsContentIntoTitleBar) |
| 29 | { |
| 30 | if (theme == ElementTheme.Default) |
| 31 | { |
| 32 | var uiSettings = new UISettings(); |
| 33 | var background = uiSettings.GetColorValue(UIColorType.Background); |
| 34 | |
| 35 | theme = background == Microsoft.UI.Colors.White ? ElementTheme.Light : ElementTheme.Dark; |
| 36 | } |
| 37 | |
| 38 | if (theme == ElementTheme.Default) |
| 39 | { |
| 40 | theme = Application.Current.RequestedTheme == ApplicationTheme.Light ? ElementTheme.Light : ElementTheme.Dark; |
| 41 | } |
| 42 | |
| 43 | window.AppWindow.TitleBar.ButtonForegroundColor = theme switch |
| 44 | { |
| 45 | ElementTheme.Dark => Microsoft.UI.Colors.White, |
| 46 | ElementTheme.Light => Microsoft.UI.Colors.Black, |
| 47 | _ => Microsoft.UI.Colors.Transparent |
| 48 | }; |
| 49 | |
| 50 | window.AppWindow.TitleBar.ButtonHoverForegroundColor = theme switch |
| 51 | { |
| 52 | ElementTheme.Dark => Microsoft.UI.Colors.White, |
| 53 | ElementTheme.Light => Microsoft.UI.Colors.Black, |
| 54 | _ => Microsoft.UI.Colors.Transparent |
| 55 | }; |
| 56 | |
| 57 | window.AppWindow.TitleBar.ButtonHoverBackgroundColor = theme switch |
| 58 | { |
| 59 | ElementTheme.Dark => Color.FromArgb(0x33, 0xFF, 0xFF, 0xFF), |
| 60 | ElementTheme.Light => Color.FromArgb(0x33, 0x00, 0x00, 0x00), |
| 61 | _ => Microsoft.UI.Colors.Transparent |
| 62 | }; |
| 63 | |
| 64 | window.AppWindow.TitleBar.ButtonPressedBackgroundColor = theme switch |
| 65 | { |
| 66 | ElementTheme.Dark => Color.FromArgb(0x66, 0xFF, 0xFF, 0xFF), |
| 67 | ElementTheme.Light => Color.FromArgb(0x66, 0x00, 0x00, 0x00), |
| 68 | _ => Microsoft.UI.Colors.Transparent |
| 69 | }; |
| 70 | |
| 71 | window.AppWindow.TitleBar.BackgroundColor = Microsoft.UI.Colors.Transparent; |
| 72 | |
| 73 | var hwnd = WinRT.Interop.WindowNative.GetWindowHandle(window); |
| 74 | if (hwnd == GetActiveWindow()) |
| 75 | { |
| 76 | SendMessage(hwnd, WMACTIVATE, WAINACTIVE, IntPtr.Zero); |
| 77 | SendMessage(hwnd, WMACTIVATE, WAACTIVE, IntPtr.Zero); |
| 78 | } |
| 79 | else |
| 80 | { |
| 81 | SendMessage(hwnd, WMACTIVATE, WAACTIVE, IntPtr.Zero); |
| 82 | SendMessage(hwnd, WMACTIVATE, WAINACTIVE, IntPtr.Zero); |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | public static void ApplySystemThemeToCaptionButtons(Window window) |
| 88 | { |
| 89 | var frame = App.AppTitlebar as FrameworkElement; |
| 90 | if (frame != null) |
| 91 | { |
| 92 | UpdateTitleBar(window, frame.ActualTheme); |
| 93 | } |
| 94 | } |
| 95 | } |