55#if WINAPPSDK
66using Microsoft . UI ;
77using 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 ;
148
159namespace CommunityToolkit . App . Shared . Controls ;
1610
@@ -25,8 +19,7 @@ namespace CommunityToolkit.App.Shared.Controls;
2519
2620public partial class TitleBar : Control
2721{
28- private AppWindow ? appWindow ;
29- ColumnDefinition ? PART_ButtonsHolderColumn ;
22+ ColumnDefinition ? PART_ButtonsHolderColumn ;
3023 ColumnDefinition ? PART_IconColumn ;
3124 ColumnDefinition ? PART_TitleColumn ;
3225 ColumnDefinition ? PART_LeftDragColumn ;
@@ -44,14 +37,19 @@ private void SetWASDKTitleBar()
4437 }
4538 if ( AutoConfigureCustomTitleBar )
4639 {
47- appWindow = GetAppWindow ( ) ;
48- appWindow . TitleBar . ExtendsContentIntoTitleBar = true ;
40+ Window . AppWindow . TitleBar . ExtendsContentIntoTitleBar = true ;
4941
5042 this . Window . Activated -= Window_Activated ;
5143 this . Window . Activated += Window_Activated ;
5244
53- appWindow . TitleBar . ButtonBackgroundColor = Colors . Transparent ;
54- appWindow . TitleBar . ButtonInactiveBackgroundColor = Colors . Transparent ;
45+ if ( Window . Content is FrameworkElement rootElement )
46+ {
47+ UpdateCaptionButtons ( rootElement ) ;
48+ rootElement . ActualThemeChanged += ( s , e ) =>
49+ {
50+ UpdateCaptionButtons ( rootElement ) ;
51+ } ;
52+ }
5553
5654 // Set the width of padding columns in the UI.
5755 PART_ButtonsHolderColumn = GetTemplateChild ( nameof ( PART_ButtonsHolderColumn ) ) as ColumnDefinition ;
@@ -64,30 +62,41 @@ private void SetWASDKTitleBar()
6462 PART_TitleHolder = GetTemplateChild ( nameof ( PART_TitleHolder ) ) as StackPanel ;
6563
6664 // Get caption button occlusion information.
67- int CaptionButtonOcclusionWidthRight = appWindow . TitleBar . RightInset ;
68- int CaptionButtonOcclusionWidthLeft = appWindow . TitleBar . LeftInset ;
65+ int CaptionButtonOcclusionWidthRight = Window . AppWindow . TitleBar . RightInset ;
66+ int CaptionButtonOcclusionWidthLeft = Window . AppWindow . TitleBar . LeftInset ;
6967 PART_LeftPaddingColumn ! . Width = new GridLength ( CaptionButtonOcclusionWidthLeft ) ;
7068 PART_RightPaddingColumn ! . Width = new GridLength ( CaptionButtonOcclusionWidthRight ) ;
7169
70+ if ( DisplayMode == DisplayMode . Tall )
71+ {
72+ // Choose a tall title bar to provide more room for interactive elements
73+ // like search box or person picture controls.
74+ Window . AppWindow . TitleBar . PreferredHeightOption = TitleBarHeightOption . Tall ;
75+ }
76+ else
77+ {
78+ Window . AppWindow . TitleBar . PreferredHeightOption = TitleBarHeightOption . Standard ;
79+ }
80+ // Recalculate the drag region for the custom title bar
81+ // if you explicitly defined new draggable areas.
82+ SetDragRegionForCustomTitleBar ( ) ;
83+ }
84+ }
7285
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- else
88- {
89- ResetWASDKTitleBar ( ) ;
90- }
86+ private void UpdateCaptionButtons ( FrameworkElement rootElement )
87+ {
88+ Window . AppWindow . TitleBar . ButtonBackgroundColor = Colors . Transparent ;
89+ Window . AppWindow . TitleBar . ButtonInactiveBackgroundColor = Colors . Transparent ;
90+ if ( rootElement . ActualTheme == ElementTheme . Dark )
91+ {
92+ Window . AppWindow . TitleBar . ButtonForegroundColor = Colors . White ;
93+ Window . AppWindow . TitleBar . ButtonInactiveForegroundColor = Colors . DarkGray ;
94+ }
95+ else
96+ {
97+ Window . AppWindow . TitleBar . ButtonForegroundColor = Colors . Black ;
98+ Window . AppWindow . TitleBar . ButtonInactiveForegroundColor = Colors . DarkGray ;
99+ }
91100 }
92101
93102 private void ResetWASDKTitleBar ( )
@@ -98,14 +107,10 @@ private void ResetWASDKTitleBar()
98107 // TO DO: Throw exception that window has not been set?
99108 }
100109
101- appWindow = GetAppWindow ( ) ;
102- appWindow . TitleBar . ResetToDefault ( ) ;
103- }
104-
105- private void UpdateRegionToSize ( )
106- {
107- // Update drag region if the size of the title bar changes.
108- SetDragRegionForCustomTitleBar ( appWindow ! ) ;
110+ Window . AppWindow . TitleBar . ExtendsContentIntoTitleBar = false ;
111+ this . Window . Activated -= Window_Activated ;
112+ SizeChanged -= this . TitleBar_SizeChanged ;
113+ Window . AppWindow . TitleBar . ResetToDefault ( ) ;
109114 }
110115
111116 private void Window_Activated ( object sender , WindowActivatedEventArgs args )
@@ -120,13 +125,14 @@ private void Window_Activated(object sender, WindowActivatedEventArgs args)
120125 }
121126 }
122127
123- private void SetDragRegionForCustomTitleBar ( AppWindow appWindow )
128+ private void SetDragRegionForCustomTitleBar ( )
124129 {
125- if ( appWindow != null )
126- { double scaleAdjustment = GetScaleAdjustment ( ) ;
130+ if ( AutoConfigureCustomTitleBar && Window != null && PART_RightPaddingColumn != null && PART_LeftPaddingColumn != null )
131+ {
132+ double scaleAdjustment = GetScaleAdjustment ( ) ;
127133
128- PART_RightPaddingColumn ! . Width = new GridLength ( appWindow . TitleBar . RightInset / scaleAdjustment ) ;
129- PART_LeftPaddingColumn ! . Width = new GridLength ( appWindow . TitleBar . LeftInset / scaleAdjustment ) ;
134+ PART_RightPaddingColumn . Width = new GridLength ( Window . AppWindow . TitleBar . RightInset / scaleAdjustment ) ;
135+ PART_LeftPaddingColumn . Width = new GridLength ( Window . AppWindow . TitleBar . LeftInset / scaleAdjustment ) ;
130136
131137 List < Windows . Graphics . RectInt32 > dragRectsList = new ( ) ;
132138
@@ -157,45 +163,8 @@ private void SetDragRegionForCustomTitleBar(AppWindow appWindow)
157163
158164 Windows . Graphics . RectInt32 [ ] dragRects = dragRectsList . ToArray ( ) ;
159165
160- appWindow . TitleBar . SetDragRectangles ( dragRects ) ;
161- }
162- }
163-
164-
165- private AppWindow GetAppWindow ( )
166- {
167- IntPtr hWnd = WindowNative . GetWindowHandle ( this . Window ) ;
168- WindowId wndId = Microsoft . UI . Win32Interop . GetWindowIdFromWindow ( hWnd ) ;
169- return AppWindow . GetFromWindowId ( wndId ) ;
170- }
171-
172- [ DllImport ( "Shcore.dll" , SetLastError = true ) ]
173- internal static extern int GetDpiForMonitor ( IntPtr hmonitor , Monitor_DPI_Type dpiType , out uint dpiX , out uint dpiY ) ;
174-
175- internal enum Monitor_DPI_Type : int
176- {
177- MDT_Effective_DPI = 0 ,
178- MDT_Angular_DPI = 1 ,
179- MDT_Raw_DPI = 2 ,
180- MDT_Default = MDT_Effective_DPI
181- }
182-
183- private double GetScaleAdjustment ( )
184- {
185- IntPtr hWnd = WindowNative . GetWindowHandle ( this . Window ) ;
186- WindowId wndId = Win32Interop . GetWindowIdFromWindow ( hWnd ) ;
187- DisplayArea displayArea = DisplayArea . GetFromWindowId ( wndId , DisplayAreaFallback . Primary ) ;
188- IntPtr hMonitor = Win32Interop . GetMonitorFromDisplayId ( displayArea . DisplayId ) ;
189-
190- // Get DPI.
191- int result = GetDpiForMonitor ( hMonitor , Monitor_DPI_Type . MDT_Default , out uint dpiX , out uint _ ) ;
192- if ( result != 0 )
193- {
194- throw new Exception ( "Could not get DPI for monitor." ) ;
166+ Window . AppWindow . TitleBar . SetDragRectangles ( dragRects ) ;
195167 }
196-
197- uint scaleFactorPercent = ( uint ) ( ( ( long ) dpiX * 100 + ( 96 >> 1 ) ) / 96 ) ;
198- return scaleFactorPercent / 100.0 ;
199168 }
200169}
201170#endif
0 commit comments