2
2
// The .NET Foundation licenses this file to you under the MIT license.
3
3
// See the LICENSE file in the project root for more information.
4
4
5
- using System ;
6
- using System . Collections . Generic ;
7
- using System . Linq ;
8
- using System . Runtime . InteropServices . WindowsRuntime ;
9
5
10
- // A control to show a Fluent titlebar
6
+ using Windows . ApplicationModel . Core ;
11
7
12
8
namespace CommunityToolkit . App . Shared . Controls ;
13
9
14
- [ TemplateVisualState ( Name = "Visible" , GroupName = "BackButtonStates" ) ]
15
- [ TemplateVisualState ( Name = "Collapsed" , GroupName = "BackButtonStates" ) ]
16
- [ TemplatePart ( Name = PartIconPresenter , Type = typeof ( Button ) ) ]
10
+ [ TemplateVisualState ( Name = BackButtonVisibleState , GroupName = BackButtonStates ) ]
11
+ [ TemplateVisualState ( Name = BackButtonCollapsedState , GroupName = BackButtonStates ) ]
12
+ [ TemplateVisualState ( Name = PaneButtonVisibleState , GroupName = PaneButtonStates ) ]
13
+ [ TemplateVisualState ( Name = PaneButtonCollapsedState , GroupName = PaneButtonStates ) ]
14
+ [ TemplatePart ( Name = PartBackButton , Type = typeof ( Button ) ) ]
15
+ [ TemplatePart ( Name = PartPaneButton , Type = typeof ( Button ) ) ]
17
16
[ TemplatePart ( Name = PartDragRegionPresenter , Type = typeof ( Grid ) ) ]
18
- public sealed partial class TitleBar : Control
17
+ public partial class TitleBar : Control
19
18
{
20
19
private const string PartDragRegionPresenter = "PART_DragRegion" ;
21
- private const string PartIconPresenter = "PART_BackButton" ;
22
- private Button ? _backButton ;
23
- private Grid ? _dragRegion ;
24
- private TitleBar ? _titleBar ;
25
-
26
- public string Title
27
- {
28
- get => ( string ) GetValue ( TitleProperty ) ;
29
- set => SetValue ( TitleProperty , value ) ;
30
- }
31
-
32
- public static readonly DependencyProperty TitleProperty = DependencyProperty . Register ( "Title" , typeof ( string ) , typeof ( TitleBar ) , new PropertyMetadata ( default ( string ) ) ) ;
20
+ private const string PartBackButton = "PART_BackButton" ;
21
+ private const string PartPaneButton = "PART_PaneButton" ;
33
22
34
- public ImageSource Icon
35
- {
36
- get => ( ImageSource ) GetValue ( IconProperty ) ;
37
- set => SetValue ( IconProperty , value ) ;
38
- }
23
+ private const string BackButtonVisibleState = "PaneButtonVisible" ;
24
+ private const string BackButtonCollapsedState = "PaneButtonCollapsed" ;
25
+ private const string BackButtonStates = "BackButtonStates" ;
39
26
40
- public static readonly DependencyProperty IconProperty = DependencyProperty . Register ( "Icon" , typeof ( ImageSource ) , typeof ( TitleBar ) , new PropertyMetadata ( default ( ImageSource ) ) ) ;
27
+ private const string PaneButtonVisibleState = "PaneButtonVisible" ;
28
+ private const string PaneButtonCollapsedState = "PaneButtonCollapsed" ;
29
+ private const string PaneButtonStates = "PaneButtonStates" ;
41
30
42
- public bool IsBackButtonVisible
43
- {
44
- get => ( bool ) GetValue ( IsBackButtonVisibleProperty ) ;
45
- set => SetValue ( IsBackButtonVisibleProperty , value ) ;
46
- }
47
31
48
- public static readonly DependencyProperty IsBackButtonVisibleProperty = DependencyProperty . Register ( "IsBackButtonVisible" , typeof ( bool ) , typeof ( TitleBar ) , new PropertyMetadata ( default ( bool ) , IsBackButtonVisibleChanged ) ) ;
32
+ private Grid ? _dragRegion ;
33
+ private TitleBar ? _titleBar ;
34
+
35
+
49
36
50
37
public event EventHandler < RoutedEventArgs > ? BackButtonClick ;
51
-
38
+ public event EventHandler < RoutedEventArgs > ? PaneButtonClick ;
52
39
53
40
public TitleBar ( )
54
41
{
@@ -59,32 +46,52 @@ protected override void OnApplyTemplate()
59
46
{
60
47
Update ( ) ;
61
48
_titleBar = ( TitleBar ) this ;
62
- _backButton = ( Button ) _titleBar . GetTemplateChild ( PartIconPresenter ) ;
49
+
50
+ if ( ( Button ) _titleBar . GetTemplateChild ( PartBackButton ) is Button backButton )
51
+ {
52
+ backButton . Click -= BackButton_Click ;
53
+ backButton . Click += BackButton_Click ;
54
+ }
55
+
56
+ if ( ( Button ) _titleBar . GetTemplateChild ( PartPaneButton ) is Button paneButton )
57
+ {
58
+ paneButton . Click -= PaneButton_Click ;
59
+ paneButton . Click += PaneButton_Click ;
60
+ }
63
61
_dragRegion = ( Grid ) _titleBar . GetTemplateChild ( PartDragRegionPresenter ) ;
64
- _backButton . Click += _backButton_Click ;
62
+
63
+
65
64
66
65
SetTitleBar ( ) ;
67
66
base . OnApplyTemplate ( ) ;
68
67
}
69
68
70
- private void _backButton_Click ( object sender , RoutedEventArgs e )
69
+ private void BackButton_Click ( object sender , RoutedEventArgs e )
71
70
{
72
71
OnBackButtonClicked ( ) ;
73
72
}
74
73
74
+ private void PaneButton_Click ( object sender , RoutedEventArgs e )
75
+ {
76
+ OnPaneButtonClicked ( ) ;
77
+ }
78
+
75
79
private void OnBackButtonClicked ( )
76
80
{
77
81
BackButtonClick ? . Invoke ( this , new RoutedEventArgs ( ) ) ;
78
82
}
79
83
80
- private static void IsBackButtonVisibleChanged ( DependencyObject d , DependencyPropertyChangedEventArgs e )
84
+ private void OnPaneButtonClicked ( )
81
85
{
82
- ( ( TitleBar ) d ) . Update ( ) ;
86
+ PaneButtonClick ? . Invoke ( this , new RoutedEventArgs ( ) ) ;
83
87
}
84
88
89
+
90
+
85
91
private void Update ( )
86
92
{
87
- VisualStateManager . GoToState ( this , IsBackButtonVisible ? "Visible" : "Collapsed" , true ) ;
93
+ VisualStateManager . GoToState ( this , IsBackButtonVisible ? BackButtonVisibleState : BackButtonCollapsedState , true ) ;
94
+ VisualStateManager . GoToState ( this , IsPaneButtonVisible ? PaneButtonVisibleState : PaneButtonCollapsedState , true ) ;
88
95
}
89
96
90
97
private void SetTitleBar ( )
@@ -97,8 +104,20 @@ private void SetTitleBar()
97
104
#endif
98
105
#if WINDOWS_UWP && ! HAS_UNO
99
106
Windows . ApplicationModel . Core . CoreApplication . GetCurrentView ( ) . TitleBar . ExtendViewIntoTitleBar = true ;
107
+ Windows . ApplicationModel . Core . CoreApplication . GetCurrentView ( ) . TitleBar . LayoutMetricsChanged += this . TitleBar_LayoutMetricsChanged ;
100
108
Window . Current . SetTitleBar ( _dragRegion ) ;
101
109
// NOT SUPPORTED IN UNO WASM
102
110
#endif
103
111
}
112
+
113
+ private void TitleBar_LayoutMetricsChanged ( CoreApplicationViewTitleBar sender , object args )
114
+ {
115
+ if ( _titleBar != null )
116
+ {
117
+ ColumnDefinition Left = ( ColumnDefinition ) _titleBar . GetTemplateChild ( "LeftPaddingColumn" ) ;
118
+ ColumnDefinition Right = ( ColumnDefinition ) _titleBar . GetTemplateChild ( "RightPaddingColumn" ) ;
119
+ Left . Width = new GridLength ( CoreApplication . GetCurrentView ( ) . TitleBar . SystemOverlayLeftInset ) ;
120
+ Right . Width = new GridLength ( CoreApplication . GetCurrentView ( ) . TitleBar . SystemOverlayRightInset ) ;
121
+ }
122
+ }
104
123
}
0 commit comments