Skip to content

Commit 83207c7

Browse files
committed
Refactoring
1 parent da54c4e commit 83207c7

File tree

7 files changed

+372
-228
lines changed

7 files changed

+372
-228
lines changed

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

Lines changed: 111 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,83 +6,168 @@ namespace CommunityToolkit.App.Shared.Controls;
66

77
public partial class TitleBar : Control
88
{
9-
public static readonly DependencyProperty IconProperty = DependencyProperty.Register(nameof(Icon), typeof(ImageSource), typeof(TitleBar), new PropertyMetadata(default(ImageSource)));
10-
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>
1117
public static readonly DependencyProperty TitleProperty = DependencyProperty.Register(nameof(Title), typeof(string), typeof(TitleBar), new PropertyMetadata(default(string)));
1218

19+
/// <summary>
20+
/// The backing <see cref="DependencyProperty"/> for the <see cref="Subtitle"/> property.
21+
/// </summary>
1322
public static readonly DependencyProperty SubtitleProperty = DependencyProperty.Register(nameof(Subtitle), typeof(string), typeof(TitleBar), new PropertyMetadata(default(string)));
1423

24+
/// <summary>
25+
/// The backing <see cref="DependencyProperty"/> for the <see cref="Content"/> property.
26+
/// </summary>
1527
public static readonly DependencyProperty ContentProperty = DependencyProperty.Register(nameof(Content), typeof(object), typeof(TitleBar), new PropertyMetadata(null));
1628

29+
/// <summary>
30+
/// The backing <see cref="DependencyProperty"/> for the <see cref="Footer"/> property.
31+
/// </summary>
1732
public static readonly DependencyProperty FooterProperty = DependencyProperty.Register(nameof(Footer), typeof(object), typeof(TitleBar), new PropertyMetadata(null));
1833

34+
/// <summary>
35+
/// The backing <see cref="DependencyProperty"/> for the <see cref="IsBackButtonVisible"/> property.
36+
/// </summary>
1937
public static readonly DependencyProperty IsBackButtonVisibleProperty = DependencyProperty.Register(nameof(IsBackButtonVisible), typeof(bool), typeof(TitleBar), new PropertyMetadata(false, IsBackButtonVisibleChanged));
2038

39+
/// <summary>
40+
/// The backing <see cref="DependencyProperty"/> for the <see cref="IsPaneButtonVisible"/> property.
41+
/// </summary>
2142
public static readonly DependencyProperty IsPaneButtonVisibleProperty = DependencyProperty.Register(nameof(IsPaneButtonVisible), typeof(bool), typeof(TitleBar), new PropertyMetadata(false, IsPaneButtonVisibleChanged));
2243

23-
public static readonly DependencyProperty ConfigureTitleBarProperty = DependencyProperty.Register(nameof(ConfigureTitleBar), typeof(bool), typeof(TitleBar), new PropertyMetadata(true, ConfigureTitleBarChanged));
24-
44+
/// <summary>
45+
/// The backing <see cref="DependencyProperty"/> for the <see cref="Display"/> property.
46+
/// </summary>
2547
public static readonly DependencyProperty DisplayModeProperty = DependencyProperty.Register(nameof(DisplayMode), typeof(DisplayMode), typeof(TitleBar), new PropertyMetadata(DisplayMode.Standard, DisplayModeChanged));
2648

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+
2760
#if WINAPPSDK
28-
public static readonly DependencyProperty WindowProperty = DependencyProperty.Register(nameof(Window), typeof(Window), typeof(TitleBar), new PropertyMetadata(null));
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));
2965
#endif
3066

31-
public ImageSource Icon
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
3281
{
33-
get => (ImageSource)GetValue(IconProperty);
82+
get => (IconElement)GetValue(IconProperty);
3483
set => SetValue(IconProperty, value);
3584
}
3685

86+
/// <summary>
87+
/// Gets or sets the Title
88+
/// </summary>
3789
public string Title
3890
{
3991
get => (string)GetValue(TitleProperty);
4092
set => SetValue(TitleProperty, value);
4193
}
4294

95+
/// <summary>
96+
/// Gets or sets the Subtitle
97+
/// </summary>
4398
public string Subtitle
4499
{
45100
get => (string)GetValue(SubtitleProperty);
46101
set => SetValue(SubtitleProperty, value);
47102
}
48103

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>
49107
public object Content
50108
{
51109
get => (object)GetValue(ContentProperty);
52110
set => SetValue(ContentProperty, value);
53111
}
54112

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>
55116
public object Footer
56117
{
57118
get => (object)GetValue(FooterProperty);
58119
set => SetValue(FooterProperty, value);
59120
}
60121

122+
/// <summary>
123+
/// Gets or sets DisplayMode. Compact is default (32px), Tall is recommended when setting the Content or Footer.
124+
/// </summary>
61125
public DisplayMode DisplayMode
62126
{
63127
get => (DisplayMode)GetValue(DisplayModeProperty);
64128
set => SetValue(DisplayModeProperty, value);
65129
}
66130

131+
/// <summary>
132+
/// Gets or sets the visibility of the back button.
133+
/// </summary>
67134
public bool IsBackButtonVisible
68135
{
69136
get => (bool)GetValue(IsBackButtonVisibleProperty);
70137
set => SetValue(IsBackButtonVisibleProperty, value);
71138
}
72139

140+
/// <summary>
141+
/// Gets or sets the visibility of the pane toggle button.
142+
/// </summary>
73143
public bool IsPaneButtonVisible
74144
{
75145
get => (bool)GetValue(IsPaneButtonVisibleProperty);
76146
set => SetValue(IsPaneButtonVisibleProperty, value);
77147
}
78148

79-
public bool ConfigureTitleBar
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
80162
{
81-
get => (bool)GetValue(ConfigureTitleBarProperty);
82-
set => SetValue(ConfigureTitleBarProperty, value);
163+
get => (bool)GetValue(AutoConfigureCustomTitleBarProperty);
164+
set => SetValue(AutoConfigureCustomTitleBarProperty, value);
83165
}
84166

85167
#if WINAPPSDK
168+
/// <summary>
169+
/// Gets or sets the window the TitleBar should configure (WASDK only).
170+
/// </summary>
86171
public Window Window
87172
{
88173
get => (Window)GetValue(WindowProperty);
@@ -100,14 +185,26 @@ private static void IsPaneButtonVisibleChanged(DependencyObject d, DependencyPro
100185
((TitleBar)d).Update();
101186
}
102187

103-
private static void ConfigureTitleBarChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
188+
private static void DisplayModeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
104189
{
105-
((TitleBar)d).SetTitleBar();
190+
((TitleBar)d).Update();
106191
}
107192

108-
private static void DisplayModeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
193+
private static void IconChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
194+
{
195+
((TitleBar)d).Update();
196+
}
197+
198+
private static void AutoConfigureCustomTitleBarChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
109199
{
110-
((TitleBar)d).SetTitleBar();
200+
if (((TitleBar)d).AutoConfigureCustomTitleBar)
201+
{
202+
((TitleBar)d).Configure();
203+
}
204+
else
205+
{
206+
((TitleBar)d).Reset();
207+
}
111208
}
112209
}
113210

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

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,41 @@
99

1010
namespace CommunityToolkit.App.Shared.Controls;
1111

12+
[TemplatePart(Name = nameof(PART_DragRegion), Type = typeof(Grid))]
13+
1214
public partial class TitleBar : Control
1315
{
16+
Grid? PART_DragRegion;
17+
1418
private void SetUWPTitleBar()
1519
{
16-
if (ConfigureTitleBar)
20+
if (AutoConfigureCustomTitleBar)
1721
{
1822
CoreApplication.GetCurrentView().TitleBar.ExtendViewIntoTitleBar = true;
1923
CoreApplication.GetCurrentView().TitleBar.LayoutMetricsChanged -= this.TitleBar_LayoutMetricsChanged;
2024
CoreApplication.GetCurrentView().TitleBar.LayoutMetricsChanged += this.TitleBar_LayoutMetricsChanged;
2125
Window.Current.Activated -= this.Current_Activated;
2226
Window.Current.Activated += this.Current_Activated;
23-
Window.Current.SetTitleBar(_dragRegion);
27+
2428
ApplicationView.GetForCurrentView().TitleBar.ButtonBackgroundColor = Colors.Transparent;
2529
ApplicationView.GetForCurrentView().TitleBar.ButtonInactiveBackgroundColor = Colors.Transparent;
30+
Window.Current.SetTitleBar(PART_DragRegion);
31+
PART_DragRegion = GetTemplateChild(nameof(PART_DragRegion)) as Grid;
2632
}
2733
else
2834
{
29-
CoreApplication.GetCurrentView().TitleBar.ExtendViewIntoTitleBar = false;
30-
Window.Current.Activated -= this.Current_Activated;
31-
CoreApplication.GetCurrentView().TitleBar.LayoutMetricsChanged -= this.TitleBar_LayoutMetricsChanged;
32-
Window.Current.SetTitleBar(null);
35+
ResetUWPTitleBar();
3336
}
3437
}
3538

39+
private void ResetUWPTitleBar()
40+
{
41+
CoreApplication.GetCurrentView().TitleBar.ExtendViewIntoTitleBar = false;
42+
Window.Current.Activated -= this.Current_Activated;
43+
CoreApplication.GetCurrentView().TitleBar.LayoutMetricsChanged -= this.TitleBar_LayoutMetricsChanged;
44+
Window.Current.SetTitleBar(null);
45+
}
46+
3647
private void Current_Activated(object sender, Windows.UI.Core.WindowActivatedEventArgs e)
3748
{
3849
if (e.WindowActivationState == Windows.UI.Core.CoreWindowActivationState.Deactivated)
@@ -47,13 +58,8 @@ private void Current_Activated(object sender, Windows.UI.Core.WindowActivatedEve
4758

4859
private void TitleBar_LayoutMetricsChanged(CoreApplicationViewTitleBar sender, object args)
4960
{
50-
if (_titleBar != null)
51-
{
52-
ColumnDefinition Left = (ColumnDefinition)_titleBar.GetTemplateChild(PartLeftPaddingColumn);
53-
ColumnDefinition Right = (ColumnDefinition)_titleBar.GetTemplateChild(PartRightPaddingColumn);
54-
Left.Width = new GridLength(CoreApplication.GetCurrentView().TitleBar.SystemOverlayLeftInset);
55-
Right.Width = new GridLength(CoreApplication.GetCurrentView().TitleBar.SystemOverlayRightInset);
56-
}
61+
PART_LeftPaddingColumn!.Width = new GridLength(CoreApplication.GetCurrentView().TitleBar.SystemOverlayLeftInset);
62+
PART_RightPaddingColumn!.Width = new GridLength(CoreApplication.GetCurrentView().TitleBar.SystemOverlayRightInset);
5763
}
5864
}
5965
#endif

0 commit comments

Comments
 (0)