|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +#if WINAPPSDK |
| 6 | +using Microsoft.UI; |
| 7 | +using Microsoft.UI.Windowing; |
| 8 | +using Microsoft.UI.Xaml; |
| 9 | +using Microsoft.UI.Xaml.Controls; |
| 10 | +using System; |
| 11 | +using System.Collections.Generic; |
| 12 | +using System.Runtime.InteropServices; |
| 13 | +using WinRT.Interop; |
| 14 | + |
| 15 | +namespace CommunityToolkit.App.Shared.Controls; |
| 16 | + |
| 17 | +public partial class TitleBar : Control |
| 18 | +{ |
| 19 | + private AppWindow? appWindow; |
| 20 | + ColumnDefinition? LeftPaddingColumn; |
| 21 | + ColumnDefinition? BackButtonColumn; |
| 22 | + ColumnDefinition? MenuButtonColumn; |
| 23 | + ColumnDefinition? IconColumn; |
| 24 | + ColumnDefinition? TitleColumn; |
| 25 | + ColumnDefinition? SubtitleColumn; |
| 26 | + ColumnDefinition? LeftDragColumn; |
| 27 | + ColumnDefinition? ContentColumn; |
| 28 | + ColumnDefinition? FooterColumn; |
| 29 | + ColumnDefinition? RightDragColumn; |
| 30 | + ColumnDefinition? RightPaddingColumn; |
| 31 | + |
| 32 | + private void SetWASDKTitleBar() |
| 33 | + { |
| 34 | + appWindow = GetAppWindowForCurrentWindow(); |
| 35 | + if (ConfigureTitleBar) |
| 36 | + { |
| 37 | + appWindow.TitleBar.ExtendsContentIntoTitleBar = true; |
| 38 | + this.Loaded -= AppTitleBar_Loaded; |
| 39 | + this.SizeChanged -= AppTitleBar_SizeChanged; |
| 40 | + |
| 41 | + this.Loaded += AppTitleBar_Loaded; |
| 42 | + this.SizeChanged += AppTitleBar_SizeChanged; |
| 43 | + |
| 44 | + Window.Activated -= Window_Activated; |
| 45 | + Window.Activated += Window_Activated; |
| 46 | + |
| 47 | + appWindow.TitleBar.ButtonBackgroundColor = Colors.Transparent; |
| 48 | + appWindow.TitleBar.ButtonInactiveBackgroundColor = Colors.Transparent; |
| 49 | + |
| 50 | + // Set the width of padding columns in the UI. |
| 51 | + LeftPaddingColumn = (ColumnDefinition)_titleBar!.GetTemplateChild(PartLeftPaddingColumn); |
| 52 | + |
| 53 | + BackButtonColumn = (ColumnDefinition)_titleBar!.GetTemplateChild("BackButtonColumn"); |
| 54 | + MenuButtonColumn = (ColumnDefinition)_titleBar!.GetTemplateChild("MenuButtonColumn"); |
| 55 | + IconColumn = (ColumnDefinition)_titleBar!.GetTemplateChild("IconColumn"); |
| 56 | + TitleColumn = (ColumnDefinition)_titleBar!.GetTemplateChild("TitleColumn"); |
| 57 | + SubtitleColumn = (ColumnDefinition)_titleBar!.GetTemplateChild("SubtitleColumn"); |
| 58 | + LeftDragColumn = (ColumnDefinition)_titleBar!.GetTemplateChild("LeftDragColumn"); |
| 59 | + ContentColumn = (ColumnDefinition)_titleBar!.GetTemplateChild("ContentColumn"); |
| 60 | + FooterColumn = (ColumnDefinition)_titleBar!.GetTemplateChild("FooterColumn"); |
| 61 | + RightDragColumn = (ColumnDefinition)_titleBar!.GetTemplateChild("RightDragColumn"); |
| 62 | + RightPaddingColumn = (ColumnDefinition)_titleBar!.GetTemplateChild(PartRightPaddingColumn); |
| 63 | + |
| 64 | + // Get caption button occlusion information. |
| 65 | + int CaptionButtonOcclusionWidthRight = appWindow.TitleBar.RightInset; |
| 66 | + int CaptionButtonOcclusionWidthLeft = appWindow.TitleBar.LeftInset; |
| 67 | + LeftPaddingColumn.Width = new GridLength(CaptionButtonOcclusionWidthLeft); |
| 68 | + RightPaddingColumn.Width = new GridLength(CaptionButtonOcclusionWidthRight); |
| 69 | + |
| 70 | + // A taller title bar is only supported when drawing a fully custom title bar |
| 71 | + if (AppWindowTitleBar.IsCustomizationSupported() && appWindow.TitleBar.ExtendsContentIntoTitleBar) |
| 72 | + { |
| 73 | + if (DisplayMode == DisplayMode.Tall) |
| 74 | + { |
| 75 | + // Choose a tall title bar to provide more room for interactive elements |
| 76 | + // like search box or person picture controls. |
| 77 | + appWindow.TitleBar.PreferredHeightOption = TitleBarHeightOption.Tall; |
| 78 | + } |
| 79 | + else |
| 80 | + { |
| 81 | + appWindow.TitleBar.PreferredHeightOption = TitleBarHeightOption.Standard; |
| 82 | + } |
| 83 | + // Recalculate the drag region for the custom title bar |
| 84 | + // if you explicitly defined new draggable areas. |
| 85 | + SetDragRegionForCustomTitleBar(appWindow); |
| 86 | + } |
| 87 | + } |
| 88 | + else |
| 89 | + { |
| 90 | + appWindow.TitleBar.ResetToDefault(); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + private void AppTitleBar_Loaded(object sender, RoutedEventArgs e) |
| 95 | + { |
| 96 | + // Check to see if customization is supported. |
| 97 | + // The method returns true on Windows 10 since Windows App SDK 1.2, and on all versions of |
| 98 | + // Windows App SDK on Windows 11. |
| 99 | + if (AppWindowTitleBar.IsCustomizationSupported()) |
| 100 | + { |
| 101 | + SetDragRegionForCustomTitleBar(appWindow!); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + private void AppTitleBar_SizeChanged(object sender, SizeChangedEventArgs e) |
| 106 | + { |
| 107 | + // Check to see if customization is supported. |
| 108 | + // The method returns true on Windows 10 since Windows App SDK 1.2, and on all versions of |
| 109 | + // Windows App SDK on Windows 11. |
| 110 | + if (AppWindowTitleBar.IsCustomizationSupported() && appWindow!.TitleBar.ExtendsContentIntoTitleBar) |
| 111 | + { |
| 112 | + // Update drag region if the size of the title bar changes. |
| 113 | + SetDragRegionForCustomTitleBar(appWindow); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + private void Window_Activated(object sender, WindowActivatedEventArgs args) |
| 118 | + { |
| 119 | + if (args.WindowActivationState == WindowActivationState.Deactivated) |
| 120 | + { |
| 121 | + VisualStateManager.GoToState(this, WindowDeactivatedState, true); |
| 122 | + } |
| 123 | + else |
| 124 | + { |
| 125 | + VisualStateManager.GoToState(this, WindowActivatedState, true); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + private void SetDragRegionForCustomTitleBar(AppWindow appWindow) |
| 130 | + { |
| 131 | + // Check to see if customization is supported. |
| 132 | + // The method returns true on Windows 10 since Windows App SDK 1.2, and on all versions of |
| 133 | + // Windows App SDK on Windows 11. |
| 134 | + if (AppWindowTitleBar.IsCustomizationSupported() && appWindow.TitleBar.ExtendsContentIntoTitleBar) |
| 135 | + { |
| 136 | + double scaleAdjustment = GetScaleAdjustment(); |
| 137 | + |
| 138 | + RightPaddingColumn!.Width = new GridLength(appWindow.TitleBar.RightInset / scaleAdjustment); |
| 139 | + LeftPaddingColumn!.Width = new GridLength(appWindow.TitleBar.LeftInset / scaleAdjustment); |
| 140 | + |
| 141 | + List<Windows.Graphics.RectInt32> dragRectsList = new(); |
| 142 | + |
| 143 | + Windows.Graphics.RectInt32 dragRectL; |
| 144 | + dragRectL.X = (int)((LeftPaddingColumn.ActualWidth + BackButtonColumn!.ActualWidth + MenuButtonColumn!.ActualWidth) * scaleAdjustment); |
| 145 | + dragRectL.Y = 0; |
| 146 | + dragRectL.Height = (int)(this.ActualHeight * scaleAdjustment); |
| 147 | + dragRectL.Width = (int)((IconColumn!.ActualWidth |
| 148 | + + TitleColumn!.ActualWidth |
| 149 | + + SubtitleColumn!.ActualWidth |
| 150 | + + LeftDragColumn!.ActualWidth) * scaleAdjustment); |
| 151 | + dragRectsList.Add(dragRectL); |
| 152 | + |
| 153 | + Windows.Graphics.RectInt32 dragRectR; |
| 154 | + dragRectR.X = (int)((LeftPaddingColumn.ActualWidth + IconColumn.ActualWidth + BackButtonColumn!.ActualWidth + MenuButtonColumn!.ActualWidth |
| 155 | + + ((TextBlock)_titleBar!.GetTemplateChild("PART_TitleText")).ActualWidth |
| 156 | + + ((TextBlock)_titleBar!.GetTemplateChild("PART_SubTitleText")).ActualWidth |
| 157 | + + LeftDragColumn.ActualWidth |
| 158 | + + ContentColumn!.ActualWidth) * scaleAdjustment); |
| 159 | + dragRectR.Y = 0; |
| 160 | + dragRectR.Height = (int)(this.ActualHeight * scaleAdjustment); |
| 161 | + dragRectR.Width = (int)(RightDragColumn!.ActualWidth * scaleAdjustment); |
| 162 | + dragRectsList.Add(dragRectR); |
| 163 | + |
| 164 | + Windows.Graphics.RectInt32[] dragRects = dragRectsList.ToArray(); |
| 165 | + |
| 166 | + appWindow.TitleBar.SetDragRectangles(dragRects); |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + |
| 171 | + private AppWindow GetAppWindowForCurrentWindow() |
| 172 | + { |
| 173 | + IntPtr hWnd = WindowNative.GetWindowHandle(Window); |
| 174 | + WindowId wndId = Microsoft.UI.Win32Interop.GetWindowIdFromWindow(hWnd); |
| 175 | + return AppWindow.GetFromWindowId(wndId); |
| 176 | + } |
| 177 | + |
| 178 | + [DllImport("Shcore.dll", SetLastError = true)] |
| 179 | + internal static extern int GetDpiForMonitor(IntPtr hmonitor, Monitor_DPI_Type dpiType, out uint dpiX, out uint dpiY); |
| 180 | + |
| 181 | + internal enum Monitor_DPI_Type : int |
| 182 | + { |
| 183 | + MDT_Effective_DPI = 0, |
| 184 | + MDT_Angular_DPI = 1, |
| 185 | + MDT_Raw_DPI = 2, |
| 186 | + MDT_Default = MDT_Effective_DPI |
| 187 | + } |
| 188 | + |
| 189 | + private double GetScaleAdjustment() |
| 190 | + { |
| 191 | + IntPtr hWnd = WindowNative.GetWindowHandle(this.Window); |
| 192 | + WindowId wndId = Win32Interop.GetWindowIdFromWindow(hWnd); |
| 193 | + DisplayArea displayArea = DisplayArea.GetFromWindowId(wndId, DisplayAreaFallback.Primary); |
| 194 | + IntPtr hMonitor = Win32Interop.GetMonitorFromDisplayId(displayArea.DisplayId); |
| 195 | + |
| 196 | + // Get DPI. |
| 197 | + int result = GetDpiForMonitor(hMonitor, Monitor_DPI_Type.MDT_Default, out uint dpiX, out uint _); |
| 198 | + if (result != 0) |
| 199 | + { |
| 200 | + throw new Exception("Could not get DPI for monitor."); |
| 201 | + } |
| 202 | + |
| 203 | + uint scaleFactorPercent = (uint)(((long)dpiX * 100 + (96 >> 1)) / 96); |
| 204 | + return scaleFactorPercent / 100.0; |
| 205 | + } |
| 206 | +} |
| 207 | +#endif |
0 commit comments