forked from CommunityToolkit/WindowsCommunityToolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathListDetailsView.cs
More file actions
478 lines (421 loc) · 17 KB
/
Copy pathListDetailsView.cs
File metadata and controls
478 lines (421 loc) · 17 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
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Collections.Generic;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Input;
using Windows.ApplicationModel;
using Windows.UI.Core;
using NavigationView = Microsoft.UI.Xaml.Controls;
namespace CommunityToolkit.WinUI.UI.Controls
{
/// <summary>
/// Panel that allows for a List/Details pattern.
/// </summary>
[TemplatePart(Name = PartDetailsPresenter, Type = typeof(ContentPresenter))]
[TemplatePart(Name = PartDetailsPanel, Type = typeof(FrameworkElement))]
[TemplateVisualState(Name = NoSelectionNarrowState, GroupName = SelectionStates)]
[TemplateVisualState(Name = NoSelectionWideState, GroupName = SelectionStates)]
[TemplateVisualState(Name = HasSelectionWideState, GroupName = SelectionStates)]
[TemplateVisualState(Name = HasSelectionNarrowState, GroupName = SelectionStates)]
public partial class ListDetailsView : ItemsControl
{
// All view states:
private const string SelectionStates = "SelectionStates";
private const string NoSelectionWideState = "NoSelectionWide";
private const string HasSelectionWideState = "HasSelectionWide";
private const string NoSelectionNarrowState = "NoSelectionNarrow";
private const string HasSelectionNarrowState = "HasSelectionNarrow";
private const string HasItemsStates = "HasItemsStates";
private const string HasItemsState = "HasItemsState";
private const string HasNoItemsState = "HasNoItemsState";
// Control names:
private const string PartRootPanel = "RootPanel";
private const string PartDetailsPresenter = "DetailsPresenter";
private const string PartDetailsPanel = "DetailsPanel";
private const string PartMainList = "MainList";
private const string PartBackButton = "ListDetailsBackButton";
private const string PartHeaderContentPresenter = "HeaderContentPresenter";
private const string PartListPaneCommandBarPanel = "ListPaneCommandBarPanel";
private const string PartDetailsPaneCommandBarPanel = "DetailsPaneCommandBarPanel";
private ContentPresenter _detailsPresenter;
private NavigationView.TwoPaneView _twoPaneView;
private VisualStateGroup _selectionStateGroup;
/// <summary>
/// Initializes a new instance of the <see cref="ListDetailsView"/> class.
/// </summary>
public ListDetailsView()
{
DefaultStyleKey = typeof(ListDetailsView);
Loaded += OnLoaded;
Unloaded += OnUnloaded;
}
/// <summary>
/// Clears the <see cref="SelectedItem"/> and prevent flickering of the UI if only the order of the items changed.
/// </summary>
public void ClearSelectedItem()
{
SelectedItem = null;
}
/// <summary>
/// Invoked whenever application code or internal processes (such as a rebuilding layout pass) call
/// ApplyTemplate. In simplest terms, this means the method is called just before a UI element displays
/// in your app. Override this method to influence the default post-template logic of a class.
/// </summary>
protected override void OnApplyTemplate()
{
base.OnApplyTemplate();
if (_inlineBackButton != null)
{
_inlineBackButton.Click -= OnInlineBackButtonClicked;
}
_inlineBackButton = (Button)GetTemplateChild(PartBackButton);
if (_inlineBackButton != null)
{
_inlineBackButton.Click += OnInlineBackButtonClicked;
}
_selectionStateGroup = (VisualStateGroup)GetTemplateChild(SelectionStates);
if (_selectionStateGroup != null)
{
_selectionStateGroup.CurrentStateChanged += OnSelectionStateChanged;
}
_twoPaneView = (NavigationView.TwoPaneView)GetTemplateChild(PartRootPanel);
if (_twoPaneView != null)
{
_twoPaneView.ModeChanged += OnModeChanged;
}
_detailsPresenter = (ContentPresenter)GetTemplateChild(PartDetailsPresenter);
SetDetailsContent();
SetListHeaderVisibility();
OnDetailsCommandBarChanged();
OnListCommandBarChanged();
OnListPaneWidthChanged();
UpdateView(true);
}
/// <summary>
/// Fired when the SelectedIndex changes.
/// </summary>
/// <param name="e">The event args.</param>
/// <remarks>
/// Sets up animations for the DetailsPresenter for animating in/out.
/// </remarks>
private void OnSelectedIndexChanged(DependencyPropertyChangedEventArgs e)
{
if (e.NewValue is int newIndex)
{
object newItem = newIndex >= 0 && Items.Count > newIndex ? Items[newIndex] : null;
object oldItem = e.OldValue is int oldIndex && oldIndex >= 0 && Items.Count > oldIndex ? Items[oldIndex] : null;
if (SelectedItem != newItem)
{
if (newItem is null)
{
ClearSelectedItem();
}
else
{
SetValue(SelectedItemProperty, newItem);
UpdateSelection(oldItem, newItem);
}
}
}
}
/// <summary>
/// Fired when the SelectedItem changes.
/// </summary>
/// <param name="e">The event args.</param>
/// <remarks>
/// Sets up animations for the DetailsPresenter for animating in/out.
/// </remarks>
private void OnSelectedItemChanged(DependencyPropertyChangedEventArgs e)
{
int index = SelectedItem is null ? -1 : Items.IndexOf(SelectedItem);
// If there is no selection, do not remove the DetailsPresenter content but let it animate out.
if (index >= 0)
{
SetDetailsContent();
}
if (SelectedIndex != index)
{
SetValue(SelectedIndexProperty, index);
UpdateSelection(e.OldValue, e.NewValue);
}
OnSelectionChanged(new SelectionChangedEventArgs(new List<object> { e.OldValue }, new List<object> { e.NewValue }));
UpdateView(true);
SetFocus(ViewState);
}
private void OnLoaded(object sender, RoutedEventArgs e)
{
if (!DesignMode.DesignModeEnabled)
{
if (Window.Current != null)
{
SystemNavigationManager.GetForCurrentView().BackRequested += OnBackRequested;
}
if (_frame != null)
{
_frame.Navigating -= OnFrameNavigating;
}
_navigationView = this.FindAscendant<NavigationView.NavigationView>();
_frame = this.FindAscendant<Frame>();
if (_frame != null)
{
_frame.Navigating += OnFrameNavigating;
}
}
}
private void OnUnloaded(object sender, RoutedEventArgs e)
{
if (DesignMode.DesignModeEnabled == false)
{
if (Window.Current != null)
{
SystemNavigationManager.GetForCurrentView().BackRequested -= OnBackRequested;
}
if (_frame != null)
{
_frame.Navigating -= OnFrameNavigating;
}
_selectionStateGroup = (VisualStateGroup)GetTemplateChild(SelectionStates);
if (_selectionStateGroup != null)
{
_selectionStateGroup.CurrentStateChanged -= OnSelectionStateChanged;
_selectionStateGroup = null;
}
}
}
/// <summary>
/// Raises SelectionChanged event and updates view.
/// </summary>
/// <param name="oldSelection">Old selection.</param>
/// <param name="newSelection">New selection.</param>
private void UpdateSelection(object oldSelection, object newSelection)
{
OnSelectionChanged(new SelectionChangedEventArgs(new List<object> { oldSelection }, new List<object> { newSelection }));
UpdateView(true);
// If there is no selection, do not remove the DetailsPresenter content but let it animate out.
if (SelectedItem != null)
{
SetDetailsContent();
}
}
private void SetListHeaderVisibility()
{
if (GetTemplateChild(PartHeaderContentPresenter) is FrameworkElement headerPresenter)
{
headerPresenter.Visibility = ListHeader != null
? Visibility.Visible
: Visibility.Collapsed;
}
}
private void UpdateView(bool animate)
{
UpdateViewState();
SetVisualState(animate);
}
private void UpdateViewState()
{
ListDetailsViewState previousState = ViewState;
if (_twoPaneView == null)
{
ViewState = ListDetailsViewState.Both;
}
// Single pane:
else if (_twoPaneView.Mode == NavigationView.TwoPaneViewMode.SinglePane)
{
ViewState = SelectedItem == null ? ListDetailsViewState.List : ListDetailsViewState.Details;
_twoPaneView.PanePriority = SelectedItem == null ? NavigationView.TwoPaneViewPriority.Pane1 : NavigationView.TwoPaneViewPriority.Pane2;
}
// Dual pane:
else
{
ViewState = ListDetailsViewState.Both;
}
if (previousState != ViewState)
{
ViewStateChanged?.Invoke(this, ViewState);
SetBackButtonVisibility(previousState);
}
}
private void SetVisualState(bool animate)
{
string noSelectionState;
string hasSelectionState;
if (ViewState == ListDetailsViewState.Both)
{
noSelectionState = NoSelectionWideState;
hasSelectionState = HasSelectionWideState;
}
else
{
noSelectionState = NoSelectionNarrowState;
hasSelectionState = HasSelectionNarrowState;
}
VisualStateManager.GoToState(this, SelectedItem == null ? noSelectionState : hasSelectionState, animate);
VisualStateManager.GoToState(this, Items.Count > 0 ? HasItemsState : HasNoItemsState, animate);
}
/// <summary>
/// Sets the content of the <see cref="SelectedItem"/> based on current <see cref="MapDetails"/> function.
/// </summary>
private void SetDetailsContent()
{
if (_detailsPresenter != null)
{
// Update the content template:
if (_detailsPresenter.ContentTemplateSelector != null)
{
_detailsPresenter.ContentTemplate = _detailsPresenter.ContentTemplateSelector.SelectTemplate(SelectedItem, _detailsPresenter);
}
// Update the content:
_detailsPresenter.Content = MapDetails == null
? SelectedItem
: SelectedItem != null ? MapDetails(SelectedItem) : null;
}
}
private void OnListCommandBarChanged()
{
OnCommandBarChanged("ListCommandBar", ListCommandBar);
}
private void OnDetailsCommandBarChanged()
{
OnCommandBarChanged("DetailsCommandBar", DetailsCommandBar);
}
private void OnCommandBarChanged(string panelName, CommandBar commandbar)
{
var panel = GetTemplateChild(panelName) as Panel;
if (panel == null)
{
return;
}
panel.Children.Clear();
if (commandbar != null)
{
panel.Children.Add(commandbar);
}
}
/// <summary>
/// Sets whether the selected item should change when focused with the keyboard based on the view state
/// </summary>
/// <param name="viewState">the view state</param>
private void SetListSelectionWithKeyboardFocusOnVisualStateChanged(ListDetailsViewState viewState)
{
if (viewState == ListDetailsViewState.Both)
{
SetListSelectionWithKeyboardFocus(true);
}
else
{
SetListSelectionWithKeyboardFocus(false);
}
}
/// <summary>
/// Sets whether the selected item should change when focused with the keyboard
/// </summary>
private void SetListSelectionWithKeyboardFocus(bool singleSelectionFollowsFocus)
{
if (GetTemplateChild("List") is ListViewBase list)
{
list.SingleSelectionFollowsFocus = singleSelectionFollowsFocus;
}
}
/// <summary>
/// Fires when the selection state of the control changes
/// </summary>
/// <param name="sender">the sender</param>
/// <param name="e">the event args</param>
/// <remarks>
/// Sets focus to the item list when the viewState is not Details.
/// Sets whether the selected item should change when focused with the keyboard.
/// </remarks>
private void OnSelectionStateChanged(object sender, VisualStateChangedEventArgs e)
{
SetFocus(ViewState);
SetListSelectionWithKeyboardFocusOnVisualStateChanged(ViewState);
}
/// <summary>
/// Sets focus to the relevant control based on the viewState.
/// </summary>
/// <param name="viewState">the view state</param>
private void SetFocus(ListDetailsViewState viewState)
{
if (viewState != ListDetailsViewState.Details)
{
FocusItemList();
}
else
{
FocusFirstFocusableElementInDetails();
}
}
/// <summary>
/// Sets focus to the first focusable element in the details template
/// </summary>
private void FocusFirstFocusableElementInDetails()
{
if (GetTemplateChild(PartDetailsPanel) is DependencyObject details)
{
var focusableElement = FocusManager.FindFirstFocusableElement(details);
(focusableElement as Control)?.Focus(FocusState.Programmatic);
}
}
/// <summary>
/// Sets focus to the item list
/// </summary>
private void FocusItemList()
{
if (GetTemplateChild(PartMainList) is Control list)
{
list.Focus(FocusState.Programmatic);
}
}
/// <summary>
/// Fired when the <see cref="TwoPaneView"/> changed its view mode.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="args">The event args.</param>
private void OnModeChanged(NavigationView.TwoPaneView sender, object args)
{
UpdateView(true);
SetListSelectionWithKeyboardFocusOnVisualStateChanged(ViewState);
}
/// <summary>
/// Invoked once the items changed and ensures the visual state is constant.
/// </summary>
protected override void OnItemsChanged(object e)
{
base.OnItemsChanged(e);
UpdateView(true);
if (SelectedIndex < 0)
{
return;
}
// Ensure we still have the correct index and selected item for the new collection.
// This prevents flickering when the order of the collection changes.
int index = -1;
if (!(Items is null))
{
index = Items.IndexOf(SelectedItem);
}
if (index < 0)
{
ClearSelectedItem();
}
else if (SelectedIndex != index)
{
SetValue(SelectedIndexProperty, index);
}
}
/// <summary>
/// Updates the <see cref="TwoPaneView.Pane1Length"/> since it is of type 'GridLength',
/// but <see cref="ListPaneWidth"/> is of type <see cref="double"/>.
/// This should be changed in a further release.
/// </summary>
private void OnListPaneWidthChanged()
{
if (_twoPaneView != null)
{
_twoPaneView.Pane1Length = new GridLength(ListPaneWidth);
}
}
}
}