-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathTabBar.cs
496 lines (431 loc) · 12.3 KB
/
TabBar.cs
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Numerics;
using Uno.Disposables;
using Uno.Extensions.Specialized;
using Windows.Foundation;
using Windows.Foundation.Collections;
#if IS_WINUI
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
using Microsoft.UI.Xaml.Data;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Navigation;
#else
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
#endif
namespace Uno.Toolkit.UI
{
/// <summary>
/// A control to display a set of <see cref="TabBarItem"/>s horizontally with the ability to display a custom view to denote selected state
/// </summary>
[TemplatePart(Name = TabBarGridName, Type = typeof(Grid))]
public partial class TabBar : ItemsControl
{
private const string TabBarGridName = "TabBarGrid";
internal bool IsUsingOwnContainerAsTemplateRoot { get; private set; }
private bool _isSynchronizingSelection;
private object? _previouslySelectedItem;
private bool _isLoaded;
private bool _isUpdatingSelectedItem;
public TabBar()
{
DefaultStyleKey = typeof(TabBar);
RegisterPropertyChangedCallback(ItemsSourceProperty, (s, e) => (s as TabBar)?.OnItemsSourceChanged());
Loaded += OnLoaded;
TemplateSettings = new TabBarTemplateSettings();
}
protected override void OnApplyTemplate()
{
base.OnApplyTemplate();
UpdateOrientation();
UpdateIndicatorPlacement();
}
protected override bool IsItemItsOwnContainerOverride(object? item) => item is TabBarItem;
protected override DependencyObject GetContainerForItemOverride()
{
if (IsUsingOwnContainerAsTemplateRoot)
{
return new ContentPresenter();
}
return new TabBarItem();
}
protected override void PrepareContainerForItemOverride(DependencyObject element, object item)
{
if (IsUsingOwnContainerAsTemplateRoot && element is ContentPresenter cp)
{
// ItemsControl::PrepareContainerForItemOverride will apply the ItemContainerStyle to the element which is not something we want here,
// since it can throw: The DP [WrongDP] is owned by [Control] and cannot be used on [ContentPresenter].
// While this doesnt break the control or the visual, it can cause a scaling performance degradation.
cp.ContentTemplate = ItemTemplate;
cp.ContentTemplateSelector = ItemTemplateSelector;
cp.DataContext = item;
SetContent(cp, item);
#if !HAS_UNO
// force template materialization
cp.Measure(Size.Empty);
#endif
if (cp.GetFirstChild() is TabBarItem tbi)
{
ApplyContainerStyle(tbi);
SetupTabBarItem(tbi);
}
}
else
{
base.PrepareContainerForItemOverride(element, item);
if (element is TabBarItem tbi)
{
SetupTabBarItem(tbi);
}
}
void SetContent(ContentPresenter cp, object item)
{
if (string.IsNullOrEmpty(DisplayMemberPath))
{
cp.Content = item;
}
else
{
cp.SetBinding(ContentPresenter.ContentProperty, new Binding
{
Source = item,
Path = new(DisplayMemberPath),
});
}
}
void ApplyContainerStyle(TabBarItem tbi)
{
var localStyleValue = tbi.ReadLocalValue(FrameworkElement.StyleProperty);
var isStyleSetFromTabBar = tbi.IsStyleSetFromTabBar;
if (localStyleValue == DependencyProperty.UnsetValue || isStyleSetFromTabBar)
{
var style = ItemContainerStyle ?? ItemContainerStyleSelector?.SelectStyle(item, tbi);
if (style is { })
{
tbi.Style = style;
tbi.IsStyleSetFromTabBar = true;
}
else
{
tbi.ClearValue(FrameworkElement.StyleProperty);
tbi.IsStyleSetFromTabBar = false;
}
}
}
void SetupTabBarItem(TabBarItem tbi)
{
tbi.IsSelected = IsSelected(IndexFromContainer(element));
tbi.Click += OnTabBarItemClick;
tbi.IsSelectedChanged += OnTabBarIsSelectedChanged;
}
}
internal virtual bool IsSelected(int index)
{
return SelectedIndex == index;
}
protected override void ClearContainerForItemOverride(DependencyObject element, object item)
{
if (IsUsingOwnContainerAsTemplateRoot && element is ContentPresenter cp)
{
if (cp.GetFirstChild() is TabBarItem tbi)
{
TearDownTabBarItem(tbi);
}
}
else
{
base.ClearContainerForItemOverride(element, item);
if (element is TabBarItem tbi)
{
TearDownTabBarItem(tbi);
}
}
void TearDownTabBarItem(TabBarItem item)
{
item.Click -= OnTabBarItemClick;
item.IsSelectedChanged -= OnTabBarIsSelectedChanged;
}
}
protected override void OnItemsChanged(object e)
{
if (
// When ItemsSource is set, we get collection changes from it directly (and it's not possible to directly modify Items)
ItemsSource == null &&
e is IVectorChangedEventArgs iVCE
)
{
if (iVCE.CollectionChange == CollectionChange.ItemChanged
|| (iVCE.CollectionChange == CollectionChange.ItemInserted && iVCE.Index < Items.Count))
{
var item = Items[(int)iVCE.Index];
if (GetInnerContainer(item as DependencyObject) is { IsSelected: true } selected) // see comment on GetInnerContainer
{
SelectedItem = selected;
}
}
else if (iVCE.CollectionChange == CollectionChange.ItemRemoved)
{
// If the removed item is the currently selected one, Set SelectedIndex to -1
if ((int)iVCE.Index == SelectedIndex)
{
SelectedIndex = -1;
}
// But if it's before the currently selected one, decrement SelectedIndex
else if ((int)iVCE.Index < SelectedIndex)
{
SelectedIndex--;
}
}
}
SynchronizeInitialSelection();
}
private void SynchronizeInitialSelection()
{
if (!IsReady)
{
return;
}
if (SelectedItem != null)
{
OnSelectedItemChanged(null);
}
else if (SelectedIndex >= 0)
{
OnSelectedIndexChanged(null);
}
}
private void OnLoaded(object sender, RoutedEventArgs e)
{
_isLoaded = true;
SynchronizeInitialSelection();
UpdateOrientation();
}
internal void OnItemsPanelConnected(TabBarListPanel panel)
{
System.Diagnostics.Debug.Assert(ItemsPanelRoot != null, "ItemsPanelRoot is expected to be already set in here.");
SynchronizeInitialSelection();
UpdateOrientation();
}
private void OnTabBarItemClick(object sender, RoutedEventArgs e)
{
if (_isSynchronizingSelection)
{
return;
}
if (sender is TabBarItem container && container.IsSelectable)
{
container.IsSelected = true;
}
}
private void OnTabBarIsSelectedChanged(object sender, RoutedEventArgs e)
{
if (_isSynchronizingSelection || _isUpdatingSelectedItem)
{
return;
}
if (sender is TabBarItem container)
{
_previouslySelectedItem = SelectedItem;
SynchronizeSelection(container);
}
}
private void OnPropertyChanged(DependencyPropertyChangedEventArgs args)
{
DependencyProperty property = args.Property;
if (property == SelectedItemProperty)
{
OnSelectedItemChanged(args);
}
else if (property == SelectedIndexProperty)
{
OnSelectedIndexChanged(args);
}
else if (property == OrientationProperty)
{
UpdateOrientation();
}
else if (property == SelectionIndicatorPlacementProperty)
{
UpdateIndicatorPlacement();
}
}
private void UpdateIndicatorPlacement()
{
var state = SelectionIndicatorPlacement switch
{
IndicatorPlacement.Above => "Above",
IndicatorPlacement.Below => "Below",
_ => throw new ArgumentOutOfRangeException(nameof(SelectionIndicatorPlacement))
};
VisualStateManager.GoToState(this, state, useTransitions: false);
}
private void UpdateOrientation()
{
var orientation = Orientation;
if (ItemsPanelRoot is TabBarListPanel panel)
{
panel.Orientation = orientation;
}
VisualStateManager.GoToState(this, orientation == Orientation.Horizontal ? "Horizontal" : "Vertical", useTransitions: false);
}
private void OnItemsSourceChanged()
{
SynchronizeInitialSelection();
}
protected override void OnItemTemplateChanged(DataTemplate oldItemTemplate, DataTemplate newItemTemplate)
{
IsUsingOwnContainerAsTemplateRoot = IsItemItsOwnContainerOverride(newItemTemplate?.LoadContent());
base.OnItemTemplateChanged(oldItemTemplate, newItemTemplate);
}
private void OnSelectedItemChanged(DependencyPropertyChangedEventArgs? args)
{
if (_isSynchronizingSelection)
{
return;
}
var newlySelectedItem = this.FindContainer<TabBarItem>(SelectedItem);
if (!IsReady && newlySelectedItem != null)
{
return;
}
if (TryUpdateTabBarItemSelectedState(args?.OldValue, newlySelectedItem))
{
SynchronizeSelection(newlySelectedItem);
}
}
private void OnSelectedIndexChanged(DependencyPropertyChangedEventArgs? args)
{
if (_isSynchronizingSelection)
{
return;
}
TabBarItem? oldItem = null;
if (args?.OldValue is int oldIndex && oldIndex != -1)
{
oldItem = this.InnerContainerFromIndexSafe(oldIndex);
}
var newItem = this.InnerContainerFromIndexSafe(SelectedIndex);
if (TryUpdateTabBarItemSelectedState(oldItem, newItem))
{
SynchronizeSelection(newItem);
}
}
//Update the IsSelected state for the TabBarItem
private bool TryUpdateTabBarItemSelectedState(object? oldItem, object? newItem)
{
try
{
_isUpdatingSelectedItem = true;
_previouslySelectedItem = oldItem;
var container = this.FindContainer<TabBarItem>(newItem);
if (container?.IsSelectable ?? false)
{
container.IsSelected = true;
return true;
}
return false;
}
finally
{
_isUpdatingSelectedItem = false;
}
}
//Sync the SelectedIndex and the SelectedItem properties for TabBar as well as the IsSelected states of the TabBarItems
private void SynchronizeSelection(TabBarItem? item)
{
if (!IsReady || _isSynchronizingSelection)
{
return;
}
try
{
_isSynchronizingSelection = true;
var containers = this.GetItemContainers<UIElement>();
foreach (var container in containers)
{
var tbi = GetInnerContainer(container); // see comment on GetInnerContainer
if (tbi is not { }) continue;
if (!tbi.IsSelected)
{
continue;
}
if (tbi != item)
{
tbi.IsSelected = false;
}
else
{
var oldSelectedItem = SelectedItem;
var newSelectedItem = ItemFromContainer(container);
if (oldSelectedItem != newSelectedItem)
{
SelectedItem = newSelectedItem;
}
var oldSelectedIndex = SelectedIndex;
var newSelectedIndex = IndexFromContainer(container);
if (oldSelectedIndex != newSelectedIndex)
{
SelectedIndex = newSelectedIndex;
}
if (!object.ReferenceEquals(_previouslySelectedItem, item))
{
RaiseSelectionChangedEvent(_previouslySelectedItem, item);
}
}
}
}
finally
{
_isSynchronizingSelection = false;
}
}
private void RaiseSelectionChangedEvent(object? prevItem, object? nextItem)
{
var eventArgs = new TabBarSelectionChangedEventArgs
{
OldItem = prevItem,
NewItem = nextItem
};
SelectionChanged?.Invoke(this, eventArgs);
}
// When using an `ItemTemplate` with a `TabBarItem`, the container will be a `ContentPresenter` that wraps the `TabBarItem`.
// In that case, to access the `ContentPresenter` from a `TabBarItem`, you must first call `ContainerFromItem`.
// Afterward, pass the resulting `ContentPresenter` as a parameter to this method.
internal TabBarItem? GetInnerContainer(DependencyObject? container)
{
if (IsUsingOwnContainerAsTemplateRoot)
{
return (container as ContentPresenter)?.GetFirstChild() as TabBarItem;
}
return container as TabBarItem;
}
internal DependencyObject? InnerContainerFromIndex(int index)
{
var container = ContainerFromIndex(index);
var inner = GetInnerContainer(container);
return inner;
}
private TabBarItem? InnerContainerFromIndexSafe(int index)
{
if (index >= 0 && index < Items.Count)
{
return InnerContainerFromIndex(index) as TabBarItem;
}
return null;
}
private bool IsReady => _isLoaded && HasItems && ItemsPanelRoot is { };
private bool HasItems => this.GetItems().Any();
}
}