-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathNavigationPageToolbar.cs
More file actions
346 lines (292 loc) · 10.4 KB
/
Copy pathNavigationPageToolbar.cs
File metadata and controls
346 lines (292 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
#nullable disable
using System;
using System.Collections.Generic;
using Microsoft.Maui.Graphics;
namespace Microsoft.Maui.Controls
{
internal class NavigationPageToolbar : Toolbar, IToolbar
{
ToolbarTracker _toolbarTracker = new ToolbarTracker();
NavigationPage _currentNavigationPage;
Page _currentPage;
bool _hasAppeared;
Color _barTextColor;
Color _iconColor;
string _title;
VisualElement _titleView;
bool _drawerToggleVisible;
bool _backButtonVisible;
bool _userChanged;
internal Page RootPage { get; private set; }
List<NavigationPage> _navigationPagesStack = new List<NavigationPage>();
internal NavigationPage CurrentNavigationPage => _currentNavigationPage;
internal ToolbarTracker ToolbarTracker => _toolbarTracker;
public override Color BarTextColor { get => GetBarTextColor(); set => SetProperty(ref _barTextColor, value); }
public override Color IconColor { get => GetIconColor(); set => SetProperty(ref _iconColor, value); }
public override string Title { get => GetTitle(); set => SetProperty(ref _title, value); }
public override VisualElement TitleView { get => GetTitleView(); set => SetProperty(ref _titleView, value); }
public override bool DrawerToggleVisible { get => _drawerToggleVisible; set => SetProperty(ref _drawerToggleVisible, value); }
public NavigationPageToolbar(Maui.IElement parent, Page rootPage) : base(parent)
{
_toolbarTracker.CollectionChanged += OnToolbarItemsChanged;
RootPage = rootPage;
_toolbarTracker.PageAppearing += OnPageAppearing;
_toolbarTracker.Target = RootPage;
}
void OnToolbarItemsChanged(object sender, EventArgs e)
{
ToolbarItems = _toolbarTracker.ToolbarItems;
}
void OnPagePropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
if (e.IsOneOf(NavigationPage.HasNavigationBarProperty,
NavigationPage.HasBackButtonProperty,
NavigationPage.TitleIconImageSourceProperty,
NavigationPage.TitleViewProperty,
NavigationPage.IconColorProperty) ||
e.IsOneOf(Page.TitleProperty,
PlatformConfiguration.AndroidSpecific.AppCompat.NavigationPage.BarHeightProperty,
NavigationPage.BarBackgroundColorProperty,
NavigationPage.BarBackgroundProperty,
NavigationPage.BarTextColorProperty) ||
e.IsOneOf(
PlatformConfiguration.WindowsSpecific.Page.ToolbarDynamicOverflowEnabledProperty,
PlatformConfiguration.WindowsSpecific.Page.ToolbarPlacementProperty) ||
e.Is(FlyoutPage.FlyoutLayoutBehaviorProperty) ||
e.Is(NavigationPage.BackButtonAccessibilityLabelProperty))
{
ApplyChanges(_currentNavigationPage);
}
else if (_currentPage != sender && sender == _currentNavigationPage && e.Is(NavigationPage.CurrentPageProperty))
{
ApplyChanges(_currentNavigationPage);
}
}
void OnPageAppearing(object sender, EventArgs e)
{
if (sender is not ContentPage cp)
return;
var pages = cp.GetParentPages();
Page previous = null;
foreach (var page in pages)
{
if (page is FlyoutPage fp)
{
if (fp.Flyout == cp || previous == fp.Flyout)
return;
}
previous = page;
}
_toolbarTracker.PagePropertyChanged -= OnPagePropertyChanged;
_currentPage = cp;
_currentNavigationPage = _currentPage.FindParentOfType<NavigationPage>();
foreach (var navPage in _navigationPagesStack)
{
navPage.ChildAdded -= NavigationPageChildrenChanged;
navPage.ChildRemoved -= NavigationPageChildrenChanged;
}
_navigationPagesStack.Clear();
if (_currentNavigationPage == null)
{
IsVisible = false;
return;
}
_navigationPagesStack.Add(_currentNavigationPage);
// we collect all the parent navigation pages because we need to know what
// all the nav stacks look like for things like BackButton Visibility
var parentNavigationPage = _currentNavigationPage.FindParentOfType<NavigationPage>();
if (parentNavigationPage != null)
_navigationPagesStack.Insert(0, parentNavigationPage);
while (parentNavigationPage != null)
{
parentNavigationPage = parentNavigationPage.FindParentOfType<NavigationPage>();
if (parentNavigationPage != null)
_navigationPagesStack.Insert(0, parentNavigationPage);
}
foreach (var navPage in _navigationPagesStack)
{
navPage.ChildAdded += NavigationPageChildrenChanged;
navPage.ChildRemoved += NavigationPageChildrenChanged;
}
_hasAppeared = true;
ApplyChanges(_currentNavigationPage);
_toolbarTracker.PagePropertyChanged += OnPagePropertyChanged;
}
// This is to catch scenarios where the user
// inserts or removes the root page.
// Which will cause the back button visibility to change.
void NavigationPageChildrenChanged(object s, ElementEventArgs a)
{
ApplyChanges(_currentNavigationPage);
}
bool GetBackButtonVisible()
{
if (_currentPage == null)
return false;
return NavigationPage.GetHasBackButton(_currentPage) && GetBackButtonVisibleCalculated(false).Value;
}
public override bool BackButtonVisible
{
get => GetBackButtonVisible();
set => _backButtonVisible = value;
}
bool? GetBackButtonVisibleCalculated(bool? defaultValue = null)
{
if (_currentPage == null || _currentNavigationPage == null)
return defaultValue;
foreach (var navPage in _navigationPagesStack)
{
if (navPage.Navigation.NavigationStack.Count == 0)
return defaultValue;
if (navPage.Navigation.NavigationStack.Count > 1)
{
return true;
}
}
return false;
}
void UpdateBackButton()
{
if (_currentPage == null || _currentNavigationPage == null)
return;
var anyPagesPushed = GetBackButtonVisibleCalculated();
if (anyPagesPushed == null)
return;
// Set this before BackButtonVisible triggers an update to the handler
// This way all useful information is present
var drawerToggleVisible = Parent is FlyoutPage flyout && flyout.ShouldShowToolbarButton()
#if !WINDOWS // TODO NET 10 : Move this logic to ShouldShowToolbarButton
&& !anyPagesPushed.Value
#endif
;
var drawerToggleVisibleChanged = _drawerToggleVisible != drawerToggleVisible;
_drawerToggleVisible = drawerToggleVisible;
// Once we have better logic inside core to handle backbutton visiblity this
// code should all go away.
// Windows currently doesn't have logic in core to handle back button visibility
// Android just handles it as part of core which means you get cool animations
// that we don't want to interrupt here.
// Once it's all built into core we can remove this code and simplify visibility logic
if (_currentPage.IsSet(NavigationPage.HasBackButtonProperty))
{
SetProperty(ref _backButtonVisible, GetBackButtonVisible(), nameof(BackButtonVisible));
_userChanged = true;
}
else
{
if (_userChanged)
{
SetProperty(ref _backButtonVisible, GetBackButtonVisible(), nameof(BackButtonVisible));
}
else
{
#if ANDROID
_backButtonVisible = GetBackButtonVisible();
#else
SetProperty(ref _backButtonVisible, GetBackButtonVisible(), nameof(BackButtonVisible));
#endif
}
_userChanged = false;
}
// Notified last so that BackButtonVisible (which takes precedence in the navigation slot)
// is already up to date when platform backends react to the drawer toggle change.
if (drawerToggleVisibleChanged)
NotifyPropertyChanged(nameof(DrawerToggleVisible));
}
void ApplyChanges(NavigationPage navigationPage)
{
if (_currentPage == null)
return;
var stack = navigationPage.Navigation.NavigationStack;
if (stack.Count == 0)
return;
var currentPage = _currentPage;
Page previousPage = null;
if (stack.Count > 1)
previousPage = stack[stack.Count - 1];
ToolbarItems = _toolbarTracker.ToolbarItems;
IsVisible = NavigationPage.GetHasNavigationBar(currentPage) && _hasAppeared;
UpdateBackButton();
if (navigationPage.IsSet(PlatformConfiguration.AndroidSpecific.AppCompat.NavigationPage.BarHeightProperty))
BarHeight = PlatformConfiguration.AndroidSpecific.AppCompat.NavigationPage.GetBarHeight(navigationPage);
else
BarHeight = null;
if (previousPage is not null)
{
BackButtonTitle = NavigationPage.GetBackButtonTitle(previousPage);
BackButtonAccessibilityLabel = NavigationPage.GetBackButtonAccessibilityLabel(previousPage);
}
else
{
BackButtonTitle = null;
BackButtonAccessibilityLabel = null;
}
TitleIcon = NavigationPage.GetTitleIconImageSource(currentPage);
BarBackground = navigationPage.BarBackground;
if (Brush.IsNullOrEmpty(BarBackground) &&
navigationPage.BarBackgroundColor != null)
{
BarBackground = new SolidColorBrush(navigationPage.BarBackgroundColor);
}
#if WINDOWS
if (Brush.IsNullOrEmpty(BarBackground))
{
var backgroundColor = navigationPage.CurrentPage.BackgroundColor ??
navigationPage.BackgroundColor;
BarBackground = navigationPage.CurrentPage.Background ??
navigationPage.Background;
if (Brush.IsNullOrEmpty(BarBackground) && backgroundColor != null)
{
BarBackground = new SolidColorBrush(backgroundColor);
}
}
#endif
BarTextColor = GetBarTextColor();
IconColor = GetIconColor();
Title = GetTitle();
TitleView = GetTitleView();
DynamicOverflowEnabled = PlatformConfiguration.WindowsSpecific.Page.GetToolbarDynamicOverflowEnabled(_currentPage);
}
Color GetBarTextColor() => _currentNavigationPage?.BarTextColor;
Color GetIconColor() => NavigationPage.GetIconColor(_currentPage) ?? NavigationPage.GetIconColor(_currentNavigationPage);
string GetTitle()
{
if (GetTitleView() != null)
{
return string.Empty;
}
return _currentNavigationPage?.CurrentPage?.Title ?? _currentNavigationPage?.Title;
}
VisualElement GetTitleView()
{
if (_currentNavigationPage == null)
{
return null;
}
Page target = _currentNavigationPage;
if (_currentNavigationPage.CurrentPage is Page currentPage)
{
target = currentPage;
}
return NavigationPage.GetTitleView(target);
}
internal void Disconnect()
{
if (_toolbarTracker is not null)
{
_toolbarTracker.PageAppearing -= OnPageAppearing;
_toolbarTracker.PagePropertyChanged -= OnPagePropertyChanged;
_toolbarTracker.CollectionChanged -= OnToolbarItemsChanged;
_toolbarTracker.Target = null;
}
if (_navigationPagesStack is not null)
{
foreach (var navPage in _navigationPagesStack)
{
navPage.ChildAdded -= NavigationPageChildrenChanged;
navPage.ChildRemoved -= NavigationPageChildrenChanged;
}
}
}
}
}