forked from unoplatform/uno
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathViewExtensions.Android.cs
More file actions
562 lines (492 loc) · 16 KB
/
Copy pathViewExtensions.Android.cs
File metadata and controls
562 lines (492 loc) · 16 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
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
557
558
559
560
561
562
using System;
using System.Collections.Generic;
using System.Linq;
using Uno.Disposables;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Uno.Extensions;
using Uno.Logging;
using Windows.UI.Xaml;
using Uno.UI.Extensions;
using System.Drawing;
using Windows.UI.Core;
using System.Threading.Tasks;
using Android.Views.Animations;
namespace Uno.UI
{
public static class ViewExtensions
{
public static void Measure(this View view, Size size)
{
view.Measure(
ViewHelper.MakeMeasureSpec(size.Width, MeasureSpecMode.AtMost),
ViewHelper.MakeMeasureSpec(size.Height, MeasureSpecMode.AtMost)
);
}
public static bool HasParent(this View view)
{
var provider = view as DependencyObject;
if (provider != null)
{
// This value is set in OnLoaded, which avoids
// interacting with JNI, for performance.
return provider.GetParent() != null;
}
return view.Parent != null;
}
/// <summary>
/// Return First parent of the view of specified T type.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="view"></param>
/// <returns>First parent of the view of specified T type.</returns>
public static T FindFirstParent<T>(this IViewParent view)
where T : class
{
view = view?.Parent;
while (view != null)
{
if (view is T)
{
return (T)view;
}
view = view.Parent;
}
return null;
}
/// <summary>
/// Return First parent of the view of specified T type.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="view"></param>
/// <param name="predicate">Predicate that will be proved against the parents of type T</param>
/// <returns>First parent of the view of specified T type that also holds true with the predicate</returns>
public static T FindFirstParent<T>(this IViewParent view, Func<T, bool> predicate)
where T : class
{
view = view?.Parent;
while (view != null)
{
var typed = view as T;
if (typed != null && predicate(typed))
{
return typed;
}
view = view.Parent;
}
return null;
}
/// <summary>
/// Enumerates all the parents for a view.
/// </summary>
public static IEnumerable<View> GetParents(this View view)
{
var parent = view.Parent;
while (parent != null)
{
yield return parent as View;
if (parent == view.Parent)
{
break;
}
parent = view.Parent;
}
}
/// <summary>
/// Gets an enumerator containing all the children of a View group
/// </summary>
/// <param name="group"></param>
/// <returns></returns>
public static IEnumerable<View> GetChildren(this ViewGroup group)
{
var shadowProvider = group as Controls.IShadowChildrenProvider;
if (shadowProvider != null)
{
// To avoid calling ChildCount/GetChildAt too much during enumeration, use
// a fast path that relies on a shadowed list of the children in BindableView.
return shadowProvider.ChildrenShadow;
}
else
{
return GetChildrenSlow(group);
}
}
/// <summary>
/// Finds the first child <see cref="View"/> of the provided <see cref="ViewGroup"/>.
/// </summary>
/// <param name="group">The <see cref="ViewGroup"/> to search into.</param>
/// <returns>A <see cref="View"/> if any, otherwise null.</returns>
public static View FindFirstChild(this ViewGroup group)
{
var shadowProvider = group as Controls.IShadowChildrenProvider;
if (shadowProvider != null)
{
// To avoid calling ChildCount/GetChildAt too much during enumeration, use
// a fast path that relies on a shadowed list of the children in BindableView.
if (shadowProvider.ChildrenShadow.Count != 0)
{
return shadowProvider.ChildrenShadow[0];
}
}
else
{
if (group.ChildCount != 0)
{
return group.GetChildAt(0);
}
}
return null;
}
/// <summary>
/// A enumerator for ViewGroup children that uses interop calls.
/// </summary>
private static IEnumerable<View> GetChildrenSlow(ViewGroup group)
{
var count = group.ChildCount;
for (int i = 0; i < count; i++)
{
yield return group.GetChildAt(i);
}
}
/// <summary>
/// Gets a filtered enumerator containing children of the view group
/// </summary>
/// <param name="group">The group</param>
/// <param name="filter">The filter</param>
/// <returns>An enumerator of the children</returns>
public static IEnumerable<View> GetChildren(this ViewGroup group, Func<View, bool> filter)
{
foreach (var child in GetChildren(group))
{
if (filter(child))
{
yield return child;
}
}
}
/// <summary>
/// Enumerates all the children for a specified view group.
/// </summary>
/// <param name="view">The view group to get the children from</param>
/// <param name="selector">The selector function</param>
/// <param name="maxDepth">The depth to stop looking for children.</param>
/// <returns>A lazy enumerable of views</returns>
public static IEnumerable<View> EnumerateAllChildren(this ViewGroup view, Func<View, bool> selector, int maxDepth = 20)
{
foreach (var sub in view.GetChildren())
{
if (selector(sub))
{
yield return sub;
}
else if (maxDepth > 0)
{
var childGroup = sub as ViewGroup;
if (childGroup != null)
{
foreach (var subResult in childGroup.EnumerateAllChildren(selector, maxDepth - 1))
{
yield return subResult;
}
}
}
}
}
/// <summary>
/// Enumerates all the children for a specified view group.
/// </summary>
/// <param name="view">The view group to get the children from</param>
/// <param name="maxDepth">The depth to stop looking for children.</param>
/// <returns>A lazy enumerable of views</returns>
public static IEnumerable<View> EnumerateAllChildren(this ViewGroup view, int maxDepth = 20)
{
foreach (var sub in view.GetChildren())
{
yield return sub;
if (maxDepth > 0)
{
var childGroup = sub as ViewGroup;
if (childGroup != null)
{
foreach (var subResult in childGroup.EnumerateAllChildren(maxDepth - 1))
{
yield return subResult;
}
}
}
}
}
/// <summary>
/// Find the first child of a specific type.
/// </summary>
/// <typeparam name="T">Expected type of the searched child</typeparam>
/// <param name="view"></param>
/// <param name="childLevelLimit">Defines the max depth, null if not limit (Should never be used)</param>
/// <param name="includeCurrent">Indicates if the current view should also be tested or not.</param>
/// <returns></returns>
public static T FindFirstChild<T>(this ViewGroup view, int? childLevelLimit = null, bool includeCurrent = true)
where T : View
{
return view.FindFirstChild<T>(null, childLevelLimit, includeCurrent);
}
/// <summary>
/// Find the first child of a specific type.
/// </summary>
/// <typeparam name="T">Expected type of the searched child</typeparam>
/// <param name="view"></param>
/// <param name="selector">Aditional selector for the child</param>
/// <param name="childLevelLimit">Defines the max depth, null if not limit (Should never be used)</param>
/// <param name="includeCurrent">Indicates if the current view should also be tested or not.</param>
/// <returns></returns>
public static T FindFirstChild<T>(this ViewGroup view, Func<T, bool> selector, int? childLevelLimit = null, bool includeCurrent = true)
where T : View
{
Func<View, bool> childSelector;
if (selector == null)
{
childSelector = child => child is T;
}
else
{
childSelector = child =>
{
var t = child as T;
return t != null && selector(t);
};
}
if (includeCurrent
&& childSelector(view))
{
return view as T;
}
var maxDepth = childLevelLimit.HasValue
? childLevelLimit.Value
: Int32.MaxValue;
return (T)view.EnumerateAllChildren(childSelector, maxDepth).FirstOrDefault();
}
/// <summary>
/// Removes a child view from the specified view, and disposes it if the specified view is the owner.
/// </summary>
/// <param name="view">The ViewGroup to remove the child from.</param>
/// <param name="child">The child view to remove</param>
public static void RemoveViewAndDispose(this ViewGroup view, View child)
{
var bindableView = view as Controls.BindableView;
if (bindableView != null)
{
// Use the C# implementation of RemoveView so that it is
// executed faster. See UnoViewGroup for details.
bindableView.RemoveView(child);
}
else
{
view.RemoveView(child);
}
}
/// <summary>
/// Invalidates the layout of the selected view. For android, calls the RequestLayout method.
/// </summary>
/// <param name="view">The view to invalidate.</param>
public static void InvalidateMeasure(this View view)
{
var bindableView = view as Controls.BindableView;
if (bindableView != null)
{
// Use the C# implementation of RequestLayout so that it is
// executed faster. See UnoViewGroup for details.
bindableView.RequestLayout();
}
else
{
view.RequestLayout();
}
}
/// <summary>
/// Invalidates the layout of the selected view. For android, calls the RequestLayout method.
/// </summary>
/// <param name="view">The view to invalidate.</param>
public static void InvalidateArrange(this View view)
{
InvalidateMeasure(view);
}
/// <summary>
/// Registers safely on the ViewAttachedToWindow on ViewDetachedFromWindow.
/// </summary>
/// <param name="view">The view to observe</param>
/// <param name="attachedHandler">The method to be called when the view is attached.</param>
/// <param name="detachedHandler">The method to be called when the view is detached.</param>
/// <returns></returns>
/// <remarks>
/// This method wraps the AttachStateChangeListener for
/// proper lifecycle management, particularly when views are disposed.
/// </remarks>
public static IDisposable RegisterViewAttachedStateChanged(
this View view,
Action<View> attachedHandler,
Action<View> detachedHandler
)
{
var listener = ViewAttachedStateChangedHelper.CreateChangedListener(attachedHandler, detachedHandler);
view.AddOnAttachStateChangeListener(listener);
return Disposable.Create(() =>
{
view.RunIfNativeInstanceAvailable(v => v.RemoveOnAttachStateChangeListener(listener));
ViewAttachedStateChangedHelper.ReleaseChangedListener(listener);
});
}
public static IDisposable RegisterTouch(this View view, EventHandler<View.TouchEventArgs> handler)
{
view.Touch += handler;
return Disposable.Create(() => view.RunIfNativeInstanceAvailable(v => v.Touch -= handler));
}
public static IDisposable RegisterSystemUiVisibilityChange(this View view, EventHandler<View.SystemUiVisibilityChangeEventArgs> handler)
{
view.SystemUiVisibilityChange += handler;
return Disposable.Create(() => view.RunIfNativeInstanceAvailable(v => v.SystemUiVisibilityChange -= handler));
}
public static IDisposable RegisterLongClick(this View view, EventHandler<View.LongClickEventArgs> handler)
{
view.LongClick += handler;
return Disposable.Create(() => view.RunIfNativeInstanceAvailable(v => v.LongClick -= handler));
}
public static IDisposable RegisterLayoutChange(this View view, EventHandler<View.LayoutChangeEventArgs> handler)
{
view.LayoutChange += handler;
return Disposable.Create(() => view.RunIfNativeInstanceAvailable(v => v.LayoutChange -= handler));
}
public static IDisposable RegisterKeyPress(this View view, EventHandler<View.KeyEventArgs> handler)
{
view.KeyPress += handler;
return Disposable.Create(() => view.RunIfNativeInstanceAvailable(v => v.KeyPress -= handler));
}
public static IDisposable RegisterHover(this View view, EventHandler<View.HoverEventArgs> handler)
{
view.Hover += handler;
return Disposable.Create(() => view.RunIfNativeInstanceAvailable(v => v.Hover -= handler));
}
public static IDisposable RegisterGenericMotion(this View view, EventHandler<View.GenericMotionEventArgs> handler)
{
view.GenericMotion += handler;
return Disposable.Create(() => view.RunIfNativeInstanceAvailable(v => v.GenericMotion -= handler));
}
public static IDisposable RegisterFocusChange(this View view, EventHandler<View.FocusChangeEventArgs> handler)
{
view.FocusChange += handler;
return Disposable.Create(() => view.RunIfNativeInstanceAvailable(v => v.FocusChange -= handler));
}
public static IDisposable RegisterDrag(this View view, EventHandler<View.DragEventArgs> handler)
{
view.Drag += handler;
return Disposable.Create(() => view.RunIfNativeInstanceAvailable(v => v.Drag -= handler));
}
public static IDisposable RegisterContextMenuCreated(this View view, EventHandler<View.CreateContextMenuEventArgs> handler)
{
view.ContextMenuCreated += handler;
return Disposable.Create(() => view.RunIfNativeInstanceAvailable(v => v.ContextMenuCreated -= handler));
}
public static IDisposable RegisterClick(this View view, EventHandler handler)
{
view.Click += handler;
return Disposable.Create(() => view.RunIfNativeInstanceAvailable(v => v.Click -= handler));
}
/// <summary>
/// Gets an identifier that can be used for logging
/// </summary>
public static string GetDebugIdentifier(this View element)
{
if (element == null)
{
return "--NULL--";
}
var name = (element as IFrameworkElement)?.Name;
return name.HasValue()
? element.GetType().Name + "_" + name + "_" + element.GetHashCode()
: element.GetType().Name + "_" + element.GetHashCode();
}
public static Task AnimateAsync(this View view, Animation animation)
{
var tcs = new TaskCompletionSource<object>();
EventHandler<Animation.AnimationEndEventArgs> onAnimationEnd = null;
onAnimationEnd = (s, e) =>
{
animation.AnimationEnd -= onAnimationEnd;
tcs.SetResult(null);
};
animation.AnimationEnd += onAnimationEnd;
view.StartAnimation(animation);
return tcs.Task;
}
/// <summary>
/// Returns the root of the view's local visual tree.
/// </summary>
public static ViewGroup GetTopLevelParent(this View view)
{
var current = view as ViewGroup;
while (current != null)
{
var visualParent = current.Parent as ViewGroup;
if (visualParent == null)
{
return current;
}
current = visualParent;
}
return null;
}
/// <summary>
/// Displays all the visual descendants of <paramref name="viewGroup"/> for diagnostic purposes.
/// </summary>
public static string ShowDescendants(this ViewGroup viewGroup, StringBuilder sb = null, string spacing = "", ViewGroup viewOfInterest = null)
{
sb = sb ?? new StringBuilder();
AppendView(viewGroup);
spacing += " ";
for (int i = 0; i < viewGroup.ChildCount; i++)
{
var child = viewGroup.GetChildAt(i);
if (child is ViewGroup childViewGroup)
{
ShowDescendants(childViewGroup, sb, spacing, viewOfInterest);
}
else
{
AppendView(child);
}
}
return sb.ToString();
void AppendView(View view)
{
var name = (view as IFrameworkElement)?.Name;
var namePart = !name.IsNullOrEmpty() ? $"-'{name}'" : "";
sb.AppendLine($"{spacing}{(view == viewOfInterest ? "*" : "")}>{view.ToString()}{namePart}-({ViewHelper.PhysicalToLogicalPixels(view.Width)}x{ViewHelper.PhysicalToLogicalPixels(view.Height)})");
}
}
/// <summary>
/// Displays the visual tree in the vicinity of <paramref name="viewGroup"/> for diagnostic purposes.
/// </summary>
/// <param name="viewGroup">The view to display tree for.</param>
/// <param name="fromHeight">How many levels above <paramref name="viewGroup"/> should be included in the displayed subtree.</param>
/// <returns>A formatted string representing the visual tree around <paramref name="viewGroup"/>.</returns>
public static string ShowLocalVisualTree(this ViewGroup viewGroup, int fromHeight = 0)
{
var root = viewGroup;
for (int i = 0; i < fromHeight; i++)
{
if (root.Parent is ViewGroup)
{
root = root.Parent as ViewGroup;
}
else
{
break;
}
}
return ShowDescendants(root, viewOfInterest: viewGroup);
}
}
}