forked from xamarin/Xamarin.Forms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathListViewRenderer.cs
556 lines (466 loc) · 15.4 KB
/
ListViewRenderer.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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Automation.Peers;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using WListView = Windows.UI.Xaml.Controls.ListView;
using WBinding = Windows.UI.Xaml.Data.Binding;
using WApp = Windows.UI.Xaml.Application;
using Xamarin.Forms.Internals;
using Xamarin.Forms.PlatformConfiguration.WindowsSpecific;
using Specifics = Xamarin.Forms.PlatformConfiguration.WindowsSpecific.ListView;
namespace Xamarin.Forms.Platform.UWP
{
public class ListViewRenderer : ViewRenderer<ListView, FrameworkElement>
{
ITemplatedItemsView<Cell> TemplatedItemsView => Element;
bool _itemWasClicked;
bool _subscribedToItemClick;
bool _subscribedToTapped;
bool _disposed;
protected WListView List { get; private set; }
protected override void OnElementChanged(ElementChangedEventArgs<ListView> e)
{
base.OnElementChanged(e);
if (e.OldElement != null)
{
e.OldElement.ItemSelected -= OnElementItemSelected;
e.OldElement.ScrollToRequested -= OnElementScrollToRequested;
((ITemplatedItemsView<Cell>)e.OldElement).TemplatedItems.CollectionChanged -= OnCollectionChanged;
}
if (e.NewElement != null)
{
e.NewElement.ItemSelected += OnElementItemSelected;
e.NewElement.ScrollToRequested += OnElementScrollToRequested;
((ITemplatedItemsView<Cell>)e.NewElement).TemplatedItems.CollectionChanged += OnCollectionChanged;
if (List == null)
{
List = new WListView
{
IsSynchronizedWithCurrentItem = false,
ItemTemplate = (Windows.UI.Xaml.DataTemplate)WApp.Current.Resources["CellTemplate"],
HeaderTemplate = (Windows.UI.Xaml.DataTemplate)WApp.Current.Resources["View"],
FooterTemplate = (Windows.UI.Xaml.DataTemplate)WApp.Current.Resources["View"],
ItemContainerStyle = (Windows.UI.Xaml.Style)WApp.Current.Resources["FormsListViewItem"],
GroupStyleSelector = (GroupStyleSelector)WApp.Current.Resources["ListViewGroupSelector"]
};
List.SelectionChanged += OnControlSelectionChanged;
List.SetBinding(ItemsControl.ItemsSourceProperty, "");
}
// WinRT throws an exception if you set ItemsSource directly to a CVS, so bind it.
List.DataContext = new CollectionViewSource { Source = Element.ItemsSource, IsSourceGrouped = Element.IsGroupingEnabled };
if (Element.SelectedItem != null)
OnElementItemSelected(null, new SelectedItemChangedEventArgs(Element.SelectedItem));
UpdateGrouping();
UpdateHeader();
UpdateFooter();
UpdateSelectionMode();
ClearSizeEstimate();
}
}
void OnCollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
List.DataContext = new CollectionViewSource { Source = Element.ItemsSource, IsSourceGrouped = Element.IsGroupingEnabled };
}
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
if (e.PropertyName == ListView.IsGroupingEnabledProperty.PropertyName)
{
UpdateGrouping();
}
else if (e.PropertyName == ListView.HeaderProperty.PropertyName || e.PropertyName == "HeaderElement")
{
UpdateHeader();
}
else if (e.PropertyName == ListView.FooterProperty.PropertyName || e.PropertyName == "FooterElement")
{
UpdateFooter();
}
else if (e.PropertyName == ListView.RowHeightProperty.PropertyName)
{
ClearSizeEstimate();
}
else if (e.PropertyName == ListView.HasUnevenRowsProperty.PropertyName)
{
ClearSizeEstimate();
}
else if (e.PropertyName == ListView.ItemTemplateProperty.PropertyName)
{
ClearSizeEstimate();
}
else if (e.PropertyName == ListView.ItemsSourceProperty.PropertyName)
{
ClearSizeEstimate();
((CollectionViewSource)List.DataContext).Source = Element.ItemsSource;
}
else if (e.PropertyName == Specifics.SelectionModeProperty.PropertyName)
{
UpdateSelectionMode();
}
}
protected override void Dispose(bool disposing)
{
if (_disposed)
{
return;
}
_disposed = true;
if (disposing)
{
if (List != null)
{
if (_subscribedToTapped)
{
_subscribedToTapped = false;
List.Tapped -= ListOnTapped;
}
if (_subscribedToItemClick)
{
_subscribedToItemClick = false;
List.ItemClick -= OnListItemClicked;
}
List.SelectionChanged -= OnControlSelectionChanged;
List.DataContext = null;
List = null;
}
if (_zoom != null)
{
_zoom.ViewChangeCompleted -= OnViewChangeCompleted;
_zoom = null;
}
}
base.Dispose(disposing);
}
static IEnumerable<T> FindDescendants<T>(DependencyObject dobj) where T : DependencyObject
{
int count = VisualTreeHelper.GetChildrenCount(dobj);
for (var i = 0; i < count; i++)
{
DependencyObject element = VisualTreeHelper.GetChild(dobj, i);
if (element is T)
yield return (T)element;
foreach (T descendant in FindDescendants<T>(element))
yield return descendant;
}
}
sealed class BrushedElement
{
public BrushedElement(FrameworkElement element, WBinding brushBinding = null, Brush brush = null)
{
Element = element;
BrushBinding = brushBinding;
Brush = brush;
}
public Brush Brush { get; }
public WBinding BrushBinding { get; }
public FrameworkElement Element { get; }
public bool IsBound
{
get { return BrushBinding != null; }
}
}
SemanticZoom _zoom;
ScrollViewer _scrollViewer;
ContentControl _headerControl;
readonly List<BrushedElement> _highlightedElements = new List<BrushedElement>();
void ClearSizeEstimate()
{
Element.ClearValue(CellControl.MeasuredEstimateProperty);
}
void UpdateFooter()
{
List.Footer = Element.FooterElement;
}
void UpdateHeader()
{
List.Header = Element.HeaderElement;
}
void UpdateGrouping()
{
bool grouping = Element.IsGroupingEnabled;
((CollectionViewSource)List.DataContext).IsSourceGrouped = grouping;
var templatedItems = TemplatedItemsView.TemplatedItems;
if (grouping && templatedItems.ShortNames != null)
{
if (_zoom == null)
{
ScrollViewer.SetIsVerticalScrollChainingEnabled(List, false);
var grid = new GridView { ItemsSource = templatedItems.ShortNames, Style = (Windows.UI.Xaml.Style)WApp.Current.Resources["JumpListGrid"] };
ScrollViewer.SetIsHorizontalScrollChainingEnabled(grid, false);
_zoom = new SemanticZoom { IsZoomOutButtonEnabled = false, ZoomedOutView = grid };
// Since we reuse our ScrollTo, we have to wait until the change completes or ChangeView has odd behavior.
_zoom.ViewChangeCompleted += OnViewChangeCompleted;
// Specific order to let SNC unparent the ListView for us
SetNativeControl(_zoom);
_zoom.ZoomedInView = List;
}
else
{
_zoom.CanChangeViews = true;
}
}
else
{
if (_zoom != null)
_zoom.CanChangeViews = false;
else if (List != Control)
SetNativeControl(List);
}
}
void UpdateSelectionMode()
{
if (Element.OnThisPlatform().GetSelectionMode() == PlatformConfiguration.WindowsSpecific.ListViewSelectionMode.Accessible)
{
// Using Tapped will disable the ability to use the Enter key
List.IsItemClickEnabled = true;
if (!_subscribedToItemClick)
{
_subscribedToItemClick = true;
List.ItemClick += OnListItemClicked;
}
if (_subscribedToTapped)
{
_subscribedToTapped = false;
List.Tapped -= ListOnTapped;
}
}
else
{
// In order to support tapping on elements within a list item, we handle
// ListView.Tapped (which can be handled by child elements in the list items
// and prevented from bubbling up) rather than ListView.ItemClick
if (!_subscribedToTapped)
{
_subscribedToTapped = true;
List.Tapped += ListOnTapped;
}
List.IsItemClickEnabled = false;
if (_subscribedToItemClick)
{
_subscribedToItemClick = false;
List.ItemClick -= OnListItemClicked;
}
}
}
async void OnViewChangeCompleted(object sender, SemanticZoomViewChangedEventArgs e)
{
if (e.IsSourceZoomedInView)
return;
// HACK: Technically more than one short name could be the same, this will potentially find the wrong one in that case
var item = (string)e.SourceItem.Item;
var templatedItems = TemplatedItemsView.TemplatedItems;
int index = templatedItems.ShortNames.IndexOf(item);
if (index == -1)
return;
var til = templatedItems.GetGroup(index);
if (til.Count == 0)
return; // FIXME
// Delay until after the SemanticZoom change _actually_ finishes, fixes tons of odd issues on Phone w/ virtualization.
if (Device.Idiom == TargetIdiom.Phone)
await Task.Delay(1);
IListProxy listProxy = til.ListProxy;
ScrollTo(listProxy.ProxiedEnumerable, listProxy[0], ScrollToPosition.Start, true, true);
}
#pragma warning disable 1998 // considered for removal
async void ScrollTo(object group, object item, ScrollToPosition toPosition, bool shouldAnimate, bool includeGroup = false, bool previouslyFailed = false)
#pragma warning restore 1998
{
ScrollViewer viewer = GetScrollViewer();
if (viewer == null)
{
RoutedEventHandler loadedHandler = null;
loadedHandler = async (o, e) =>
{
List.Loaded -= loadedHandler;
// Here we try to avoid an exception, see explanation at bottom
await Dispatcher.RunIdleAsync(args => { ScrollTo(group, item, toPosition, shouldAnimate, includeGroup); });
};
List.Loaded += loadedHandler;
return;
}
var templatedItems = TemplatedItemsView.TemplatedItems;
Tuple<int, int> location = templatedItems.GetGroupAndIndexOfItem(group, item);
if (location.Item1 == -1 || location.Item2 == -1)
return;
object[] t = templatedItems.GetGroup(location.Item1).ItemsSource.Cast<object>().ToArray();
object c = t[location.Item2];
double viewportHeight = viewer.ViewportHeight;
var semanticLocation = new SemanticZoomLocation { Item = c };
switch (toPosition)
{
case ScrollToPosition.Start:
{
List.ScrollIntoView(c, ScrollIntoViewAlignment.Leading);
return;
}
case ScrollToPosition.MakeVisible:
{
List.ScrollIntoView(c, ScrollIntoViewAlignment.Default);
return;
}
case ScrollToPosition.End:
case ScrollToPosition.Center:
{
var content = (FrameworkElement)List.ItemTemplate.LoadContent();
content.DataContext = c;
content.Measure(new Windows.Foundation.Size(viewer.ActualWidth, double.PositiveInfinity));
double tHeight = content.DesiredSize.Height;
if (toPosition == ScrollToPosition.Center)
semanticLocation.Bounds = new Rect(0, viewportHeight / 2 - tHeight / 2, 0, 0);
else
semanticLocation.Bounds = new Rect(0, viewportHeight - tHeight, 0, 0);
break;
}
}
// Waiting for loaded doesn't seem to be enough anymore; the ScrollViewer does not appear until after Loaded.
// Even if the ScrollViewer is present, an invoke at low priority fails (E_FAIL) presumably because the items are
// still loading. An invoke at idle sometimes work, but isn't reliable enough, so we'll just have to commit
// treason and use a blanket catch for the E_FAIL and try again.
try
{
List.MakeVisible(semanticLocation);
}
catch (Exception)
{
if (previouslyFailed)
return;
Task.Delay(1).ContinueWith(ct => { ScrollTo(group, item, toPosition, shouldAnimate, includeGroup, true); }, TaskScheduler.FromCurrentSynchronizationContext()).WatchForError();
}
}
void OnElementScrollToRequested(object sender, ScrollToRequestedEventArgs e)
{
var scrollArgs = (ITemplatedItemsListScrollToRequestedEventArgs)e;
ScrollTo(scrollArgs.Group, scrollArgs.Item, e.Position, e.ShouldAnimate);
}
T GetFirstDescendant<T>(DependencyObject element) where T : FrameworkElement
{
int count = VisualTreeHelper.GetChildrenCount(element);
for (var i = 0; i < count; i++)
{
DependencyObject child = VisualTreeHelper.GetChild(element, i);
T target = child as T ?? GetFirstDescendant<T>(child);
if (target != null)
return target;
}
return null;
}
ContentControl GetHeaderControl()
{
if (_headerControl == null)
{
ScrollViewer viewer = GetScrollViewer();
if (viewer == null)
return null;
var presenter = GetFirstDescendant<ItemsPresenter>(viewer);
if (presenter == null)
return null;
_headerControl = GetFirstDescendant<ContentControl>(presenter);
}
return _headerControl;
}
ScrollViewer GetScrollViewer()
{
if (_scrollViewer == null)
{
_scrollViewer = List.GetFirstDescendant<ScrollViewer>();
}
return _scrollViewer;
}
void ListOnTapped(object sender, TappedRoutedEventArgs args)
{
var orig = args.OriginalSource as DependencyObject;
int index = -1;
// Work our way up the tree until we find the actual list item
// the user tapped on
while (orig != null && orig != List)
{
var lv = orig as ListViewItem;
if (lv != null)
{
index = TemplatedItemsView.TemplatedItems.GetGlobalIndexOfItem(lv.Content);
break;
}
orig = VisualTreeHelper.GetParent(orig);
}
if (index > -1)
{
OnListItemClicked(index);
}
}
void OnElementItemSelected(object sender, SelectedItemChangedEventArgs e)
{
if (Element == null)
return;
if (_deferSelection)
{
// If we get more than one of these, that's okay; we only want the latest one
_deferredSelectedItemChangedEvent = new Tuple<object, SelectedItemChangedEventArgs>(sender, e);
return;
}
if (e.SelectedItem == null)
{
List.SelectedIndex = -1;
return;
}
var templatedItems = TemplatedItemsView.TemplatedItems;
var index = 0;
if (Element.IsGroupingEnabled)
{
int selectedItemIndex = templatedItems.GetGlobalIndexOfItem(e.SelectedItem);
var leftOver = 0;
int groupIndex = templatedItems.GetGroupIndexFromGlobal(selectedItemIndex, out leftOver);
index = selectedItemIndex - (groupIndex + 1);
}
else
{
index = templatedItems.GetGlobalIndexOfItem(e.SelectedItem);
}
List.SelectedIndex = index;
}
void OnListItemClicked(int index)
{
Element.NotifyRowTapped(index, cell: null);
_itemWasClicked = true;
}
void OnListItemClicked(object sender, ItemClickEventArgs e)
{
if (e.ClickedItem != null && TemplatedItemsView?.TemplatedItems != null)
{
var templatedItems = TemplatedItemsView.TemplatedItems;
var selectedItemIndex = templatedItems.GetGlobalIndexOfItem(e.ClickedItem);
OnListItemClicked(selectedItemIndex);
}
}
void OnControlSelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (Element.SelectedItem != List.SelectedItem && !_itemWasClicked)
((IElementController)Element).SetValueFromRenderer(ListView.SelectedItemProperty, List.SelectedItem);
_itemWasClicked = false;
}
FrameworkElement FindElement(object cell)
{
foreach (CellControl selector in FindDescendants<CellControl>(List))
{
if (ReferenceEquals(cell, selector.DataContext))
return selector;
}
return null;
}
bool _deferSelection = false;
Tuple<object, SelectedItemChangedEventArgs> _deferredSelectedItemChangedEvent;
protected override AutomationPeer OnCreateAutomationPeer()
{
return List == null
? new FrameworkElementAutomationPeer(this)
: new ListViewAutomationPeer(List);
}
}
}