5
5
#if WINAPPSDK
6
6
using Microsoft . UI ;
7
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
8
15
9
namespace CommunityToolkit . App . Shared . Controls ;
16
10
@@ -25,8 +19,7 @@ namespace CommunityToolkit.App.Shared.Controls;
25
19
26
20
public partial class TitleBar : Control
27
21
{
28
- private AppWindow ? appWindow ;
29
- ColumnDefinition ? PART_ButtonsHolderColumn ;
22
+ ColumnDefinition ? PART_ButtonsHolderColumn ;
30
23
ColumnDefinition ? PART_IconColumn ;
31
24
ColumnDefinition ? PART_TitleColumn ;
32
25
ColumnDefinition ? PART_LeftDragColumn ;
@@ -44,14 +37,19 @@ private void SetWASDKTitleBar()
44
37
}
45
38
if ( AutoConfigureCustomTitleBar )
46
39
{
47
- appWindow = GetAppWindow ( ) ;
48
- appWindow . TitleBar . ExtendsContentIntoTitleBar = true ;
40
+ Window . AppWindow . TitleBar . ExtendsContentIntoTitleBar = true ;
49
41
50
42
this . Window . Activated -= Window_Activated ;
51
43
this . Window . Activated += Window_Activated ;
52
44
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
+ }
55
53
56
54
// Set the width of padding columns in the UI.
57
55
PART_ButtonsHolderColumn = GetTemplateChild ( nameof ( PART_ButtonsHolderColumn ) ) as ColumnDefinition ;
@@ -64,30 +62,41 @@ private void SetWASDKTitleBar()
64
62
PART_TitleHolder = GetTemplateChild ( nameof ( PART_TitleHolder ) ) as StackPanel ;
65
63
66
64
// 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 ;
69
67
PART_LeftPaddingColumn ! . Width = new GridLength ( CaptionButtonOcclusionWidthLeft ) ;
70
68
PART_RightPaddingColumn ! . Width = new GridLength ( CaptionButtonOcclusionWidthRight ) ;
71
69
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
+ }
72
85
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
+ }
91
100
}
92
101
93
102
private void ResetWASDKTitleBar ( )
@@ -98,14 +107,10 @@ private void ResetWASDKTitleBar()
98
107
// TO DO: Throw exception that window has not been set?
99
108
}
100
109
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 ( ) ;
109
114
}
110
115
111
116
private void Window_Activated ( object sender , WindowActivatedEventArgs args )
@@ -120,13 +125,14 @@ private void Window_Activated(object sender, WindowActivatedEventArgs args)
120
125
}
121
126
}
122
127
123
- private void SetDragRegionForCustomTitleBar ( AppWindow appWindow )
128
+ private void SetDragRegionForCustomTitleBar ( )
124
129
{
125
- if ( appWindow != null )
126
- { double scaleAdjustment = GetScaleAdjustment ( ) ;
130
+ if ( AutoConfigureCustomTitleBar && Window != null && PART_RightPaddingColumn != null && PART_LeftPaddingColumn != null )
131
+ {
132
+ double scaleAdjustment = GetScaleAdjustment ( ) ;
127
133
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 ) ;
130
136
131
137
List < Windows . Graphics . RectInt32 > dragRectsList = new ( ) ;
132
138
@@ -157,45 +163,8 @@ private void SetDragRegionForCustomTitleBar(AppWindow appWindow)
157
163
158
164
Windows . Graphics . RectInt32 [ ] dragRects = dragRectsList . ToArray ( ) ;
159
165
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 ) ;
195
167
}
196
-
197
- uint scaleFactorPercent = ( uint ) ( ( ( long ) dpiX * 100 + ( 96 >> 1 ) ) / 96 ) ;
198
- return scaleFactorPercent / 100.0 ;
199
168
}
200
169
}
201
170
#endif
0 commit comments