Skip to content

Commit 764ecb6

Browse files
authored
Merge pull request #88 from CommunityToolkit/niels9001/titlebar-improvements
Gallery design improvements
2 parents e54c631 + 93a54d7 commit 764ecb6

File tree

15 files changed

+991
-282
lines changed

15 files changed

+991
-282
lines changed

CommunityToolkit.App.Shared/App.xaml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,6 @@
2323

2424
<SolidColorBrush x:Key="BackgroundColorBrush"
2525
Color="{ThemeResource SolidBackgroundFillColorBase}" />
26-
<SolidColorBrush x:Key="WindowCaptionBackground"
27-
Color="Transparent" />
28-
<SolidColorBrush x:Key="WindowCaptionBackgroundDisabled"
29-
Color="Transparent" />
3026

3127
<!-- PathData for the GitHub icon -->
3228
<x:String x:Key="GithubIcon">M21.7999992370605,0L19.220495223999,0.26007080078125 16.81787109375,1.00595712661743 14.6436157226563,2.18616962432861 12.7492189407349,3.74921894073486 11.1861696243286,5.64361572265625 10.0059566497803,7.81787109375 9.26007080078125,10.2204961776733 9,12.8000001907349 9.65248012542725,16.8459720611572 11.4694375991821,20.3591785430908 14.2401514053345,23.1291217803955 17.7539005279541,24.9453010559082 18.4305686950684,24.8080005645752 18.6273498535156,24.3296756744385 18.6207065582275,23.4247951507568 18.609375,21.9468746185303 16.4340572357178,22.0373229980469 15.1187467575073,21.4822216033936 14.4708204269409,20.7821025848389 14.2976503372192,20.4375 13.8297338485718,19.5214366912842 13.3685493469238,18.947265625 12.8765497207642,18.5656261444092 12.3995819091797,18.1091804504395 12.4844465255737,17.87890625 12.7874250411987,17.7974605560303 12.9647998809814,17.7875003814697 13.8134965896606,18.0311241149902 14.4276065826416,18.4802703857422 14.8007507324219,18.9127178192139 14.926549911499,19.1062507629395 15.8880548477173,20.1437015533447 16.9443283081055,20.494140625 17.9229640960693,20.416259765625 18.6515502929688,20.1687507629395 18.9645938873291,19.1242198944092 19.4640502929688,18.4593753814697 17.3543262481689,18.0241260528564 15.4833002090454,17.014066696167 14.1450357437134,15.1450166702271 13.6336002349854,12.1328001022339 13.9853601455688,10.2268438339233 14.9500007629395,8.69764995574951 14.7027282714844,7.54188776016235 14.7441072463989,6.53565359115601 15.0765495300293,5.30859994888306 15.2825078964233,5.28076791763306 15.9191312789917,5.34375619888306 17.0145378112793,5.71729135513306 18.596851348877,6.62109994888306 21.799976348877,6.19062519073486 25.004674911499,6.62265014648438 26.5845413208008,5.71818733215332 27.6791000366211,5.34472513198853 28.315746307373,5.28210020065308 28.5218753814697,5.31015014648438 28.8556652069092,6.53784370422363 28.8976573944092,7.5438346862793 28.6499996185303,8.69764995574951 29.6154251098633,10.2268533706665 29.9656257629395,12.1328001022339 29.453296661377,15.1497011184692 28.1123065948486,17.0164012908936 26.2366523742676,18.020601272583 24.120325088501,18.4500007629395 24.7275562286377,19.3355484008789 24.9890747070313,20.8187503814697 24.9804744720459,23.0584030151367 24.9718742370605,24.3312511444092 25.1693305969238,24.8128852844238 25.8531246185303,24.9453010559082 29.3641395568848,23.1273632049561 32.1326217651367,20.3568344116211 33.948070526123,16.8442134857178 34.5999984741211,12.8000001907349 34.3399276733398,10.2204961776733 33.5940399169922,7.81787109375 32.4138298034668,5.64361572265625 30.8507804870605,3.74921894073486 28.9563827514648,2.18616962432861 26.7821273803711,1.00595712661743 24.3795032501221,0.26007080078125 21.7999992370605,0z</x:String>

CommunityToolkit.App.Shared/App.xaml.cs

Lines changed: 9 additions & 10 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,13 @@ 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+
currentWindow.SystemBackdrop = new MicaBackdrop();
43+
#if ALL_SAMPLES
44+
currentWindow.AppWindow.TitleBar.ExtendsContentIntoTitleBar = true;
45+
currentWindow.AppWindow.TitleBar.ButtonBackgroundColor = Microsoft.UI.Colors.Transparent;
46+
#endif
3847
#endif
3948

4049
// Do not repeat app initialization when the Window already has content,
@@ -52,7 +61,6 @@ protected override void OnLaunched(LaunchActivatedEventArgs e)
5261
#endif
5362
rootFrame.Navigate(typeof(AppLoadingView), e.Arguments);
5463

55-
SetTitleBar();
5664
// Ensure the current window is active
5765
currentWindow.Activate();
5866
}
@@ -66,13 +74,4 @@ void OnNavigationFailed(object sender, NavigationFailedEventArgs e)
6674
{
6775
throw new Exception("Failed to load Page " + e.SourcePageType.FullName);
6876
}
69-
70-
private void SetTitleBar()
71-
{
72-
#if WINDOWS_UWP
73-
var viewTitleBar = Windows.UI.ViewManagement.ApplicationView.GetForCurrentView().TitleBar;
74-
viewTitleBar.ButtonBackgroundColor = Windows.UI.Colors.Transparent;
75-
viewTitleBar.ButtonInactiveBackgroundColor = Windows.UI.Colors.Transparent;
76-
#endif
77-
}
7877
}
160 KB
Binary file not shown.

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,13 @@
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" />
28+
<Compile Include="$(MSBuildThisFileDirectory)Controls\TitleBar\TitleBar.UWP.cs" />
29+
<Compile Include="$(MSBuildThisFileDirectory)Controls\TitleBar\TitleBar.WASDK.cs" />
30+
<Compile Include="$(MSBuildThisFileDirectory)Controls\TitleBar\TitleBar.Properties.cs" />
2731
<Compile Include="$(MSBuildThisFileDirectory)Converters\StringToUriConverter.cs" />
2832
<Compile Include="$(MSBuildThisFileDirectory)DocOrSampleTemplateSelector.cs" />
29-
<Compile Include="$(MSBuildThisFileDirectory)Helpers\BackgroundHelper.cs" />
3033
<Compile Include="$(MSBuildThisFileDirectory)Helpers\IconHelper.cs" />
3134
<Compile Include="$(MSBuildThisFileDirectory)Helpers\NavigationViewHelper.cs" />
3235
<Compile Include="$(MSBuildThisFileDirectory)Pages\GettingStartedPage.xaml.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
Lines changed: 225 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,225 @@
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+
namespace CommunityToolkit.App.Shared.Controls;
6+
7+
public partial class TitleBar : Control
8+
{
9+
/// <summary>
10+
/// The backing <see cref="DependencyProperty"/> for the <see cref="Icon"/> property.
11+
/// </summary>
12+
public static readonly DependencyProperty IconProperty = DependencyProperty.Register(nameof(Icon), typeof(IconElement), typeof(TitleBar), new PropertyMetadata(null, IconChanged));
13+
14+
/// <summary>
15+
/// The backing <see cref="DependencyProperty"/> for the <see cref="Title"/> property.
16+
/// </summary>
17+
public static readonly DependencyProperty TitleProperty = DependencyProperty.Register(nameof(Title), typeof(string), typeof(TitleBar), new PropertyMetadata(default(string)));
18+
19+
/// <summary>
20+
/// The backing <see cref="DependencyProperty"/> for the <see cref="Subtitle"/> property.
21+
/// </summary>
22+
public static readonly DependencyProperty SubtitleProperty = DependencyProperty.Register(nameof(Subtitle), typeof(string), typeof(TitleBar), new PropertyMetadata(default(string)));
23+
24+
/// <summary>
25+
/// The backing <see cref="DependencyProperty"/> for the <see cref="Content"/> property.
26+
/// </summary>
27+
public static readonly DependencyProperty ContentProperty = DependencyProperty.Register(nameof(Content), typeof(object), typeof(TitleBar), new PropertyMetadata(null, ContentChanged));
28+
29+
/// <summary>
30+
/// The backing <see cref="DependencyProperty"/> for the <see cref="Footer"/> property.
31+
/// </summary>
32+
public static readonly DependencyProperty FooterProperty = DependencyProperty.Register(nameof(Footer), typeof(object), typeof(TitleBar), new PropertyMetadata(null, FooterChanged));
33+
34+
/// <summary>
35+
/// The backing <see cref="DependencyProperty"/> for the <see cref="IsBackButtonVisible"/> property.
36+
/// </summary>
37+
public static readonly DependencyProperty IsBackButtonVisibleProperty = DependencyProperty.Register(nameof(IsBackButtonVisible), typeof(bool), typeof(TitleBar), new PropertyMetadata(false, IsBackButtonVisibleChanged));
38+
39+
/// <summary>
40+
/// The backing <see cref="DependencyProperty"/> for the <see cref="IsPaneButtonVisible"/> property.
41+
/// </summary>
42+
public static readonly DependencyProperty IsPaneButtonVisibleProperty = DependencyProperty.Register(nameof(IsPaneButtonVisible), typeof(bool), typeof(TitleBar), new PropertyMetadata(false, IsPaneButtonVisibleChanged));
43+
44+
/// <summary>
45+
/// The backing <see cref="DependencyProperty"/> for the <see cref="Display"/> property.
46+
/// </summary>
47+
public static readonly DependencyProperty DisplayModeProperty = DependencyProperty.Register(nameof(DisplayMode), typeof(DisplayMode), typeof(TitleBar), new PropertyMetadata(DisplayMode.Standard, DisplayModeChanged));
48+
49+
/// <summary>
50+
/// The backing <see cref="DependencyProperty"/> for the <see cref="CompactStateBreakpoint
51+
/// "/> property.
52+
/// </summary>
53+
public static readonly DependencyProperty CompactStateBreakpointProperty = DependencyProperty.Register(nameof(CompactStateBreakpoint), typeof(int), typeof(TitleBar), new PropertyMetadata(850));
54+
55+
/// <summary>
56+
/// The backing <see cref="DependencyProperty"/> for the <see cref="AutoConfigureCustomTitleBar"/> property.
57+
/// </summary>
58+
public static readonly DependencyProperty AutoConfigureCustomTitleBarProperty = DependencyProperty.Register(nameof(AutoConfigureCustomTitleBar), typeof(bool), typeof(TitleBar), new PropertyMetadata(true, AutoConfigureCustomTitleBarChanged));
59+
60+
#if WINAPPSDK
61+
/// <summary>
62+
/// The backing <see cref="DependencyProperty"/> for the <see cref="Window"/> property.
63+
/// </summary>
64+
public static readonly DependencyProperty WindowProperty = DependencyProperty.Register(nameof(Window), typeof(Window), typeof(TitleBar), new PropertyMetadata(null));
65+
#endif
66+
67+
/// <summary>
68+
/// The event that gets fired when the back button is clicked
69+
/// </summary>
70+
public event EventHandler<RoutedEventArgs>? BackButtonClick;
71+
72+
/// <summary>
73+
/// The event that gets fired when the pane toggle button is clicked
74+
/// </summary>
75+
public event EventHandler<RoutedEventArgs>? PaneButtonClick;
76+
77+
/// <summary>
78+
/// Gets or sets the Icon
79+
/// </summary>
80+
public IconElement Icon
81+
{
82+
get => (IconElement)GetValue(IconProperty);
83+
set => SetValue(IconProperty, value);
84+
}
85+
86+
/// <summary>
87+
/// Gets or sets the Title
88+
/// </summary>
89+
public string Title
90+
{
91+
get => (string)GetValue(TitleProperty);
92+
set => SetValue(TitleProperty, value);
93+
}
94+
95+
/// <summary>
96+
/// Gets or sets the Subtitle
97+
/// </summary>
98+
public string Subtitle
99+
{
100+
get => (string)GetValue(SubtitleProperty);
101+
set => SetValue(SubtitleProperty, value);
102+
}
103+
104+
/// <summary>
105+
/// Gets or sets the content shown at the center of the TitleBar. When setting this, using DisplayMode=Tall is recommended.
106+
/// </summary>
107+
public object Content
108+
{
109+
get => (object)GetValue(ContentProperty);
110+
set => SetValue(ContentProperty, value);
111+
}
112+
113+
/// <summary>
114+
/// Gets or sets the content shown at the right of the TitleBar, next to the caption buttons. When setting this, using DisplayMode=Tall is recommended.
115+
/// </summary>
116+
public object Footer
117+
{
118+
get => (object)GetValue(FooterProperty);
119+
set => SetValue(FooterProperty, value);
120+
}
121+
122+
/// <summary>
123+
/// Gets or sets DisplayMode. Compact is default (32px), Tall is recommended when setting the Content or Footer.
124+
/// </summary>
125+
public DisplayMode DisplayMode
126+
{
127+
get => (DisplayMode)GetValue(DisplayModeProperty);
128+
set => SetValue(DisplayModeProperty, value);
129+
}
130+
131+
/// <summary>
132+
/// Gets or sets the visibility of the back button.
133+
/// </summary>
134+
public bool IsBackButtonVisible
135+
{
136+
get => (bool)GetValue(IsBackButtonVisibleProperty);
137+
set => SetValue(IsBackButtonVisibleProperty, value);
138+
}
139+
140+
/// <summary>
141+
/// Gets or sets the visibility of the pane toggle button.
142+
/// </summary>
143+
public bool IsPaneButtonVisible
144+
{
145+
get => (bool)GetValue(IsPaneButtonVisibleProperty);
146+
set => SetValue(IsPaneButtonVisibleProperty, value);
147+
}
148+
149+
/// <summary>
150+
/// Gets or sets the breakpoint of when the compact state is triggered.
151+
/// </summary>
152+
public int CompactStateBreakpoint
153+
{
154+
get => (int)GetValue(CompactStateBreakpointProperty);
155+
set => SetValue(CompactStateBreakpointProperty, value);
156+
}
157+
158+
/// <summary>
159+
/// Gets or sets if the TitleBar should auto configure ExtendContentIntoTitleBar and CaptionButtion background colors.
160+
/// </summary>
161+
public bool AutoConfigureCustomTitleBar
162+
{
163+
get => (bool)GetValue(AutoConfigureCustomTitleBarProperty);
164+
set => SetValue(AutoConfigureCustomTitleBarProperty, value);
165+
}
166+
167+
#if WINAPPSDK
168+
/// <summary>
169+
/// Gets or sets the window the TitleBar should configure (WASDK only).
170+
/// </summary>
171+
public Window Window
172+
{
173+
get => (Window)GetValue(WindowProperty);
174+
set => SetValue(WindowProperty, value);
175+
}
176+
#endif
177+
178+
private static void IsBackButtonVisibleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
179+
{
180+
((TitleBar)d).Update();
181+
}
182+
183+
private static void IsPaneButtonVisibleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
184+
{
185+
((TitleBar)d).Update();
186+
}
187+
188+
private static void DisplayModeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
189+
{
190+
((TitleBar)d).Update();
191+
}
192+
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+
203+
private static void IconChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
204+
{
205+
((TitleBar)d).Update();
206+
}
207+
208+
private static void AutoConfigureCustomTitleBarChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
209+
{
210+
if (((TitleBar)d).AutoConfigureCustomTitleBar)
211+
{
212+
((TitleBar)d).Configure();
213+
}
214+
else
215+
{
216+
((TitleBar)d).Reset();
217+
}
218+
}
219+
}
220+
221+
public enum DisplayMode
222+
{
223+
Standard,
224+
Tall
225+
}

0 commit comments

Comments
 (0)