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
/
ViewExtensions.cs
166 lines (141 loc) · 3.5 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
using System.Collections.Generic;
using System.Linq;
using Android.Content;
using Android.Graphics.Drawables;
using Android.OS;
using Android.Util;
using Android.Views;
using AndroidX.Core.Content;
using AColor = Android.Graphics.Color;
using ARect = Android.Graphics.Rect;
using AView = Android.Views.View;
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 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;
}
}
}
}