Skip to content

Commit 58eead7

Browse files
committed
Improvements
1 parent 4ae36eb commit 58eead7

File tree

13 files changed

+222
-139
lines changed

13 files changed

+222
-139
lines changed

CommunityToolkit.App.Shared/App.xaml.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
using Windows.UI;
6+
57
namespace CommunityToolkit.App.Shared;
68

79
/// <summary>
@@ -35,6 +37,12 @@ protected override void OnLaunched(LaunchActivatedEventArgs e)
3537
{
3638
#if WINAPPSDK
3739
currentWindow = new Window();
40+
currentWindow.Title = "Toolkit Labs Gallery";
41+
currentWindow.AppWindow.SetIcon("Assets/Icon.ico");
42+
#if ALL_SAMPLES
43+
currentWindow.AppWindow.TitleBar.ExtendsContentIntoTitleBar = true;
44+
currentWindow.AppWindow.TitleBar.ButtonBackgroundColor = Microsoft.UI.Colors.Transparent;
45+
#endif
3846
#endif
3947

4048
// Do not repeat app initialization when the Window already has content,
160 KB
Binary file not shown.

CommunityToolkit.App.Shared/CommunityToolkit.App.Shared.projitems

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
</Compile>
2424
<Compile Include="$(MSBuildThisFileDirectory)Attributes\PackageProjectUrlAttribute.cs" />
2525
<Compile Include="$(MSBuildThisFileDirectory)Behaviors\NavigateToUriAction.cs" />
26+
<Compile Include="$(MSBuildThisFileDirectory)Controls\TitleBar\NativeMethods.cs" />
2627
<Compile Include="$(MSBuildThisFileDirectory)Controls\TitleBar\TitleBar.cs" />
2728
<Compile Include="$(MSBuildThisFileDirectory)Controls\TitleBar\TitleBar.UWP.cs" />
2829
<Compile Include="$(MSBuildThisFileDirectory)Controls\TitleBar\TitleBar.WASDK.cs" />
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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 System.Runtime.InteropServices;
7+
using WinRT.Interop;
8+
using Microsoft.UI;
9+
using Microsoft.UI.Windowing;
10+
11+
namespace CommunityToolkit.App.Shared.Controls;
12+
13+
public partial class TitleBar : Control
14+
{
15+
[DllImport("Shcore.dll", SetLastError = true)]
16+
internal static extern int GetDpiForMonitor(IntPtr hmonitor, Monitor_DPI_Type dpiType, out uint dpiX, out uint dpiY);
17+
18+
internal enum Monitor_DPI_Type : int
19+
{
20+
MDT_Effective_DPI = 0,
21+
MDT_Angular_DPI = 1,
22+
MDT_Raw_DPI = 2,
23+
MDT_Default = MDT_Effective_DPI
24+
}
25+
26+
private double GetScaleAdjustment()
27+
{
28+
IntPtr hWnd = WindowNative.GetWindowHandle(this.Window);
29+
WindowId wndId = Win32Interop.GetWindowIdFromWindow(hWnd);
30+
DisplayArea displayArea = DisplayArea.GetFromWindowId(wndId, DisplayAreaFallback.Primary);
31+
IntPtr hMonitor = Win32Interop.GetMonitorFromDisplayId(displayArea.DisplayId);
32+
33+
// Get DPI.
34+
int result = GetDpiForMonitor(hMonitor, Monitor_DPI_Type.MDT_Default, out uint dpiX, out uint _);
35+
if (result != 0)
36+
{
37+
throw new Exception("Could not get DPI for monitor.");
38+
}
39+
40+
uint scaleFactorPercent = (uint)(((long)dpiX * 100 + (96 >> 1)) / 96);
41+
return scaleFactorPercent / 100.0;
42+
}
43+
}
44+
#endif

CommunityToolkit.App.Shared/Controls/TitleBar/TitleBar.Properties.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ public partial class TitleBar : Control
2424
/// <summary>
2525
/// The backing <see cref="DependencyProperty"/> for the <see cref="Content"/> property.
2626
/// </summary>
27-
public static readonly DependencyProperty ContentProperty = DependencyProperty.Register(nameof(Content), typeof(object), typeof(TitleBar), new PropertyMetadata(null));
27+
public static readonly DependencyProperty ContentProperty = DependencyProperty.Register(nameof(Content), typeof(object), typeof(TitleBar), new PropertyMetadata(null, ContentChanged));
2828

2929
/// <summary>
3030
/// The backing <see cref="DependencyProperty"/> for the <see cref="Footer"/> property.
3131
/// </summary>
32-
public static readonly DependencyProperty FooterProperty = DependencyProperty.Register(nameof(Footer), typeof(object), typeof(TitleBar), new PropertyMetadata(null));
32+
public static readonly DependencyProperty FooterProperty = DependencyProperty.Register(nameof(Footer), typeof(object), typeof(TitleBar), new PropertyMetadata(null, FooterChanged));
3333

3434
/// <summary>
3535
/// The backing <see cref="DependencyProperty"/> for the <see cref="IsBackButtonVisible"/> property.
@@ -190,6 +190,16 @@ private static void DisplayModeChanged(DependencyObject d, DependencyPropertyCha
190190
((TitleBar)d).Update();
191191
}
192192

193+
private static void ContentChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
194+
{
195+
((TitleBar)d).Update();
196+
}
197+
198+
private static void FooterChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
199+
{
200+
((TitleBar)d).Update();
201+
}
202+
193203
private static void IconChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
194204
{
195205
((TitleBar)d).Update();

CommunityToolkit.App.Shared/Controls/TitleBar/TitleBar.UWP.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,20 @@ private void SetUWPTitleBar()
2424
CoreApplication.GetCurrentView().TitleBar.LayoutMetricsChanged += this.TitleBar_LayoutMetricsChanged;
2525
Window.Current.Activated -= this.Current_Activated;
2626
Window.Current.Activated += this.Current_Activated;
27-
27+
2828
ApplicationView.GetForCurrentView().TitleBar.ButtonBackgroundColor = Colors.Transparent;
2929
ApplicationView.GetForCurrentView().TitleBar.ButtonInactiveBackgroundColor = Colors.Transparent;
30-
Window.Current.SetTitleBar(PART_DragRegion);
30+
3131
PART_DragRegion = GetTemplateChild(nameof(PART_DragRegion)) as Grid;
32-
}
33-
else
34-
{
35-
ResetUWPTitleBar();
32+
Window.Current.SetTitleBar(PART_DragRegion);
3633
}
3734
}
3835

3936
private void ResetUWPTitleBar()
4037
{
4138
CoreApplication.GetCurrentView().TitleBar.ExtendViewIntoTitleBar = false;
4239
Window.Current.Activated -= this.Current_Activated;
40+
SizeChanged -= this.TitleBar_SizeChanged;
4341
CoreApplication.GetCurrentView().TitleBar.LayoutMetricsChanged -= this.TitleBar_LayoutMetricsChanged;
4442
Window.Current.SetTitleBar(null);
4543
}

CommunityToolkit.App.Shared/Controls/TitleBar/TitleBar.WASDK.cs

Lines changed: 52 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,6 @@
55
#if WINAPPSDK
66
using Microsoft.UI;
77
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;
148

159
namespace CommunityToolkit.App.Shared.Controls;
1610

@@ -25,8 +19,7 @@ namespace CommunityToolkit.App.Shared.Controls;
2519

2620
public 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

Comments
 (0)