/
VisualElementTracker.cs
345 lines (288 loc) · 9.11 KB
/
VisualElementTracker.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
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using Xamarin.Forms.Internals;
namespace Xamarin.Forms.Platform.WPF
{
public abstract class VisualElementTracker : IDisposable
{
public abstract void Dispose();
public event EventHandler Updated;
protected void OnUpdated()
{
Updated?.Invoke(this, EventArgs.Empty);
}
}
public class VisualElementTracker<TElement, TNativeElement> : VisualElementTracker where TElement : VisualElement where TNativeElement : FrameworkElement
{
bool _disposed;
TNativeElement _control;
TElement _element;
bool _invalidateArrangeNeeded;
bool _isPanning;
#pragma warning disable 0414 // The private field 'field' is assigned but its value is never used
bool _isPinching;
#pragma warning restore 0414
bool _touchFrameReportedEventSet;
int _touchPoints = 1;
public TNativeElement Control
{
get { return _control; }
set
{
if (_control == value)
return;
if (_control != null)
{
_control.MouseLeftButtonUp -= MouseLeftButtonUp;
_control.ManipulationDelta -= OnManipulationDelta;
_control.ManipulationCompleted -= OnManipulationCompleted;
}
_control = value;
if (_control != null)
{
_control.MouseLeftButtonUp += MouseLeftButtonUp;
_control.ManipulationDelta += OnManipulationDelta;
_control.ManipulationCompleted += OnManipulationCompleted;
}
UpdateNativeControl();
}
}
public TElement Element
{
get { return _element; }
set
{
if (_element == value)
return;
if (_element != null)
{
_element.BatchCommitted -= HandleRedrawNeeded;
_element.PropertyChanged -= HandlePropertyChanged;
}
_element = value;
if (_element != null)
{
_element.BatchCommitted += HandleRedrawNeeded;
_element.PropertyChanged += HandlePropertyChanged;
}
UpdateNativeControl();
}
}
private void MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
var fe = (sender as FrameworkElement);
var vr = (sender as DefaultViewRenderer)?.Element;
if ((fe != null && !fe.IsEnabled) || (vr != null && !vr.IsEnabled))
return;
e.Handled = ElementOnTap(e.ClickCount, e.GetPosition(fe));
}
protected virtual void HandlePropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (Element.Batched)
{
if (e.PropertyName == VisualElement.XProperty.PropertyName ||
e.PropertyName == VisualElement.YProperty.PropertyName ||
e.PropertyName == VisualElement.WidthProperty.PropertyName ||
e.PropertyName == VisualElement.HeightProperty.PropertyName)
_invalidateArrangeNeeded = true;
return;
}
if (e.PropertyName == VisualElement.XProperty.PropertyName ||
e.PropertyName == VisualElement.YProperty.PropertyName ||
e.PropertyName == VisualElement.WidthProperty.PropertyName ||
e.PropertyName == VisualElement.HeightProperty.PropertyName)
MaybeInvalidate();
else if (e.PropertyName == VisualElement.AnchorXProperty.PropertyName ||
e.PropertyName == VisualElement.AnchorYProperty.PropertyName ||
e.PropertyName == VisualElement.ScaleProperty.PropertyName ||
e.PropertyName == VisualElement.TranslationXProperty.PropertyName ||
e.PropertyName == VisualElement.TranslationYProperty.PropertyName ||
e.PropertyName == VisualElement.RotationProperty.PropertyName ||
e.PropertyName == VisualElement.RotationXProperty.PropertyName ||
e.PropertyName == VisualElement.RotationYProperty.PropertyName)
UpdateScaleAndTranslateAndRotation();
else if (e.PropertyName == VisualElement.IsVisibleProperty.PropertyName)
UpdateVisibility();
else if (e.PropertyName == VisualElement.OpacityProperty.PropertyName)
UpdateOpacity();
else if (e.PropertyName == VisualElement.InputTransparentProperty.PropertyName)
UpdateInputTransparent();
}
protected virtual void UpdateNativeControl()
{
if (Element == null || Control == null)
return;
UpdateOpacity();
UpdateScaleAndTranslateAndRotation();
UpdateInputTransparent();
UpdateVisibility();
if (_invalidateArrangeNeeded)
MaybeInvalidate();
_invalidateArrangeNeeded = false;
UpdateTouchFrameReportedEvent();
OnUpdated();
}
bool ElementOnTap(int numberOfTapsRequired, System.Windows.Point tapPosition)
{
var view = Element as View;
if (view == null)
return false;
var handled = false;
var children = (view as IGestureController)?.GetChildElements(new Point(tapPosition.X, tapPosition.Y));
if (children != null)
foreach (var recognizer in children.GetChildGesturesFor<TapGestureRecognizer>().Where(g => g.NumberOfTapsRequired == numberOfTapsRequired))
{
recognizer.SendTapped(view);
handled = true;
}
if (handled)
return handled;
foreach (var gestureRecognizer in
view.GestureRecognizers.OfType<TapGestureRecognizer>().Where(g => g.NumberOfTapsRequired == numberOfTapsRequired))
{
gestureRecognizer.SendTapped(view);
handled = true;
}
return handled;
}
void HandlePan(ManipulationDeltaEventArgs e, View view)
{
foreach (PanGestureRecognizer recognizer in
view.GestureRecognizers.GetGesturesFor<PanGestureRecognizer>().Where(g => g.TouchPoints == _touchPoints))
{
if (!_isPanning)
((IPanGestureController)recognizer).SendPanStarted(view, Application.Current.PanGestureId);
double totalX = 0;
double totalY = 0;
((IPanGestureController)recognizer).SendPan(view, totalX, totalY, Application.Current.PanGestureId);
_isPanning = true;
}
}
void HandleRedrawNeeded(object sender, EventArgs e)
{
UpdateNativeControl();
}
void OnManipulationCompleted(object sender, ManipulationCompletedEventArgs e)
{
var view = Element as View;
if (view == null)
return;
IEnumerable pinchGestures = view.GestureRecognizers.GetGesturesFor<PinchGestureRecognizer>();
foreach (var recognizer in pinchGestures)
((IPinchGestureController)recognizer).SendPinchEnded(view);
_isPinching = false;
IEnumerable<PanGestureRecognizer> panGestures = view.GestureRecognizers.GetGesturesFor<PanGestureRecognizer>().Where(g => g.TouchPoints == _touchPoints);
foreach (PanGestureRecognizer recognizer in panGestures)
((IPanGestureController)recognizer).SendPanCompleted(view, Application.Current.PanGestureId);
Application.Current.PanGestureId++;
_isPanning = false;
}
void OnManipulationDelta(object sender, ManipulationDeltaEventArgs e)
{
var view = Element as View;
if (view == null)
return;
HandlePan(e, view);
}
void Touch_FrameReported(object sender, TouchFrameEventArgs e)
{
_touchPoints = e.GetTouchPoints(Control).Count;
}
void MaybeInvalidate()
{
if (Element.IsInNativeLayout)
return;
var parent = (FrameworkElement)Control.Parent;
parent?.InvalidateMeasure();
Control.InvalidateMeasure();
}
void UpdateInputTransparent()
{
Control.IsHitTestVisible = !Element.InputTransparent;
}
void UpdateOpacity()
{
Control.Opacity = Element.Opacity;
}
void UpdateScaleAndTranslateAndRotation()
{
// TODO : Implement plane projection - Don't exist in WPF framework :(
double anchorX = Element.AnchorX;
double anchorY = Element.AnchorY;
double rotationX = Element.RotationX;
double rotationY = Element.RotationY;
double rotation = Element.Rotation;
double translationX = Element.TranslationX;
double translationY = Element.TranslationY;
double scale = Element.Scale;
double offsetX = scale == 0 ? 0 : translationX / scale;
double offsetY = scale == 0 ? 0 : translationY / scale;
Control.RenderTransformOrigin = new System.Windows.Point(anchorX, anchorY);
Control.RenderTransform = new TransformGroup()
{
Children = new TransformCollection()
{
new RotateTransform()
{
CenterX = anchorX,
CenterY = anchorY,
Angle = Element.Rotation
},
new TranslateTransform()
{
X = offsetX,
Y = offsetY
},
new ScaleTransform
{
ScaleX = scale,
ScaleY = scale
}
}
};
}
void UpdateTouchFrameReportedEvent()
{
if (_touchFrameReportedEventSet)
return;
Touch.FrameReported -= Touch_FrameReported;
_touchFrameReportedEventSet = false;
var view = Element as View;
if (view == null)
return;
if (!view.GestureRecognizers.GetGesturesFor<PanGestureRecognizer>().Any(g => g.TouchPoints > 1))
return;
Touch.FrameReported += Touch_FrameReported;
_touchFrameReportedEventSet = true;
}
void UpdateVisibility()
{
Control.Visibility = Element.IsVisible ? Visibility.Visible : Visibility.Collapsed;
}
public override void Dispose()
{
if (_disposed)
return;
_disposed = true;
if (_control != null)
{
_control.MouseLeftButtonUp -= MouseLeftButtonUp;
_control.ManipulationDelta -= OnManipulationDelta;
_control.ManipulationCompleted -= OnManipulationCompleted;
}
if (_element != null)
{
_element.BatchCommitted -= HandleRedrawNeeded;
_element.PropertyChanged -= HandlePropertyChanged;
}
Element = null;
Control = null;
}
}
}