This repository has been archived by the owner on May 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
GestureElement.cs
74 lines (65 loc) · 1.93 KB
/
GestureElement.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
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Linq;
using Xamarin.Forms.Internals;
namespace Xamarin.Forms
{
public class GestureElement : Element, ISpatialElement, IGestureRecognizers
{
readonly GestureRecognizerCollection _gestureRecognizers = new GestureRecognizerCollection();
internal event NotifyCollectionChangedEventHandler GestureRecognizersCollectionChanged;
public GestureElement()
{
_gestureRecognizers.CollectionChanged += (sender, args) =>
{
void AddItems()
{
foreach (IElement item in args.NewItems.OfType<IElement>())
{
ValidateGesture(item as IGestureRecognizer);
item.Parent = this;
}
}
void RemoveItems()
{
foreach (IElement item in args.OldItems.OfType<IElement>())
item.Parent = null;
}
switch (args.Action)
{
case NotifyCollectionChangedAction.Add:
AddItems();
break;
case NotifyCollectionChangedAction.Remove:
RemoveItems();
break;
case NotifyCollectionChangedAction.Replace:
AddItems();
RemoveItems();
break;
case NotifyCollectionChangedAction.Reset:
foreach (IElement item in _gestureRecognizers.OfType<IElement>())
item.Parent = this;
break;
}
GestureRecognizersCollectionChanged?.Invoke(sender, args);
};
}
Region ISpatialElement.Region { get; set; }
public IList<IGestureRecognizer> GestureRecognizers
{
get { return _gestureRecognizers; }
}
internal virtual void ValidateGesture(IGestureRecognizer gesture) { }
class GestureRecognizerCollection : ObservableCollection<IGestureRecognizer>
{
protected override void ClearItems()
{
List<IGestureRecognizer> removed = new List<IGestureRecognizer>(this);
base.ClearItems();
base.OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, removed));
}
}
}
}