/
ViewExtensions.cs
217 lines (183 loc) · 4.75 KB
/
ViewExtensions.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
using Android.Content;
using Android.Graphics.Drawables;
using Android.OS;
#if __ANDROID_29__
using AndroidX.Core.Content;
#else
using Android.Support.V4.Content;
#endif
using Android.Util;
using Android.Views;
using AView = Android.Views.View;
using AColor = Android.Graphics.Color;
using Android.Graphics;
using System.Collections.Generic;
using System.Linq;
namespace Xamarin.Forms.Platform.Android
{
public static class ViewExtensions
{
static readonly int s_apiLevel;
static ViewExtensions()
{
s_apiLevel = (int)Forms.SdkInt;
}
public static void RemoveFromParent(this AView view)
{
if (view == null)
return;
if (view.Parent == null)
return;
((ViewGroup)view.Parent).RemoveView(view);
}
public static void SetBackground(this AView view, Drawable drawable)
{
if (s_apiLevel < 16)
{
#pragma warning disable 618 // Using older method for compatibility with API 15
view.SetBackgroundDrawable(drawable);
#pragma warning restore 618
}
else
{
view.Background = drawable;
}
}
public static void SetWindowBackground(this AView view)
{
Context context = view.Context;
using (var background = new TypedValue())
{
if (context.Theme.ResolveAttribute(global::Android.Resource.Attribute.WindowBackground, background, true))
{
string type = context.Resources.GetResourceTypeName(background.ResourceId).ToLower();
switch (type)
{
case "color":
var color = new AColor(ContextCompat.GetColor(context, background.ResourceId));
view.SetBackgroundColor(color);
break;
case "drawable":
using (Drawable drawable = ContextCompat.GetDrawable(context, background.ResourceId))
view.SetBackground(drawable);
break;
}
}
}
}
public static void EnsureId(this AView view)
{
if (view.IsDisposed())
{
return;
}
if (view.Id == AView.NoId)
{
view.Id = Platform.GenerateViewId();
}
}
public static bool GetClipToOutline(this AView view)
{
if (view.IsDisposed() || !Forms.IsLollipopOrNewer)
return false;
return view.ClipToOutline;
}
public static void SetClipToOutline(this AView view, bool value)
{
if (view.IsDisposed() || !Forms.IsLollipopOrNewer)
return;
view.ClipToOutline = value;
}
public static void SetClipToOutline(this AView view, bool value, VisualElement element)
{
if (view.IsDisposed())
return;
var shouldClip = value;
if (element is Frame frame)
{
shouldClip = frame.IsSet(Layout.IsClippedToBoundsProperty)
? frame.IsClippedToBounds : frame.CornerRadius > 0f;
}
if (view is FastRenderers.FrameRenderer && Forms.IsLollipopOrNewer)
{
view.SetClipToOutline(shouldClip);
return;
}
// setClipBounds is only available in API 18 +
if ((int)Build.VERSION.SdkInt >= 18)
{
if (!(view is ViewGroup viewGroup))
{
return;
}
// Forms layouts should not impose clipping on their children
viewGroup.SetClipChildren(false);
// But if IsClippedToBounds is true, they _should_ enforce clipping at their own edges
viewGroup.ClipBounds = shouldClip ? new Rect(0, 0, viewGroup.Width, viewGroup.Height) : null;
}
else
{
// For everything in 17 and below, use the setClipChildren method
if (!(view.Parent is ViewGroup parent))
return;
if ((int)Build.VERSION.SdkInt >= 18 && parent.ClipChildren == shouldClip)
return;
parent.SetClipChildren(shouldClip);
parent.Invalidate();
}
}
public static bool SetElevation(this AView view, float value)
{
if (view.IsDisposed() || !Forms.IsLollipopOrNewer)
return false;
view.Elevation = value;
return true;
}
internal static void MaybeRequestLayout(this AView view)
{
var isInLayout = false;
if ((int)Build.VERSION.SdkInt >= 18)
isInLayout = view.IsInLayout;
if (!isInLayout && !view.IsLayoutRequested)
view.RequestLayout();
}
internal static T GetParentOfType<T>(this IViewParent view)
where T : class
{
if (view is T t)
return t;
while (view != null)
{
T parent = view.Parent as T;
if (parent != null)
return parent;
view = view.Parent;
}
return default(T);
}
internal static T GetParentOfType<T>(this AView view)
where T : class
{
T t = view as T;
if (view != null)
return t;
return view.Parent.GetParentOfType<T>();
}
internal static T FindParentOfType<T>(this VisualElement element)
{
var navPage = element.GetParentsPath()
.OfType<T>()
.FirstOrDefault();
return navPage;
}
internal static IEnumerable<Element> GetParentsPath(this VisualElement self)
{
Element current = self;
while (!Application.IsApplicationOrNull(current.RealParent))
{
current = current.RealParent;
yield return current;
}
}
}
}