/
LayoutHandler.Android.cs
160 lines (130 loc) · 5.17 KB
/
LayoutHandler.Android.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
using System;
using Android.Views;
using AView = Android.Views.View;
namespace Microsoft.Maui.Handlers
{
public partial class LayoutHandler : ViewHandler<ILayout, LayoutViewGroup>
{
protected override LayoutViewGroup CreatePlatformView()
{
if (VirtualView == null)
{
throw new InvalidOperationException($"{nameof(VirtualView)} must be set to create a LayoutViewGroup");
}
var viewGroup = new LayoutViewGroup(Context!)
{
CrossPlatformMeasure = VirtualView.CrossPlatformMeasure,
CrossPlatformArrange = VirtualView.CrossPlatformArrange
};
// .NET MAUI layouts should not impose clipping on their children
viewGroup.SetClipChildren(false);
return viewGroup;
}
public override void SetVirtualView(IView view)
{
base.SetVirtualView(view);
_ = PlatformView ?? throw new InvalidOperationException($"{nameof(PlatformView)} should have been set by base class.");
_ = VirtualView ?? throw new InvalidOperationException($"{nameof(VirtualView)} should have been set by base class.");
_ = MauiContext ?? throw new InvalidOperationException($"{nameof(MauiContext)} should have been set by base class.");
PlatformView.CrossPlatformMeasure = VirtualView.CrossPlatformMeasure;
PlatformView.CrossPlatformArrange = VirtualView.CrossPlatformArrange;
PlatformView.RemoveAllViews();
foreach (var child in VirtualView.OrderByZIndex())
{
PlatformView.SafelyAddView(child.ToPlatform(MauiContext));
}
}
public void Add(IView child)
{
_ = PlatformView ?? throw new InvalidOperationException($"{nameof(PlatformView)} should have been set by base class.");
_ = VirtualView ?? throw new InvalidOperationException($"{nameof(VirtualView)} should have been set by base class.");
_ = MauiContext ?? throw new InvalidOperationException($"{nameof(MauiContext)} should have been set by base class.");
var targetIndex = VirtualView.GetLayoutHandlerIndex(child);
PlatformView.AddView(child.ToPlatform(MauiContext), targetIndex);
}
public void Remove(IView child)
{
_ = PlatformView ?? throw new InvalidOperationException($"{nameof(PlatformView)} should have been set by base class.");
_ = VirtualView ?? throw new InvalidOperationException($"{nameof(VirtualView)} should have been set by base class.");
if (child?.ToPlatform() is View view)
{
PlatformView.RemoveView(view);
}
}
void Clear(LayoutViewGroup platformView)
{
platformView.RemoveAllViews();
}
public void Clear()
{
Clear(PlatformView);
}
public void Insert(int index, IView child)
{
_ = PlatformView ?? throw new InvalidOperationException($"{nameof(PlatformView)} should have been set by base class.");
_ = VirtualView ?? throw new InvalidOperationException($"{nameof(VirtualView)} should have been set by base class.");
_ = MauiContext ?? throw new InvalidOperationException($"{nameof(MauiContext)} should have been set by base class.");
var targetIndex = VirtualView.GetLayoutHandlerIndex(child);
PlatformView.AddView(child.ToPlatform(MauiContext), targetIndex);
}
public void Update(int index, IView child)
{
_ = PlatformView ?? throw new InvalidOperationException($"{nameof(PlatformView)} should have been set by base class.");
_ = VirtualView ?? throw new InvalidOperationException($"{nameof(VirtualView)} should have been set by base class.");
_ = MauiContext ?? throw new InvalidOperationException($"{nameof(MauiContext)} should have been set by base class.");
PlatformView.RemoveViewAt(index);
var targetIndex = VirtualView.GetLayoutHandlerIndex(child);
PlatformView.AddView(child.ToPlatform(MauiContext), targetIndex);
}
public void UpdateZIndex(IView child)
{
_ = PlatformView ?? throw new InvalidOperationException($"{nameof(PlatformView)} should have been set by base class.");
_ = VirtualView ?? throw new InvalidOperationException($"{nameof(VirtualView)} should have been set by base class.");
_ = MauiContext ?? throw new InvalidOperationException($"{nameof(MauiContext)} should have been set by base class.");
EnsureZIndexOrder(child);
}
protected override void DisconnectHandler(LayoutViewGroup platformView)
{
// If we're being disconnected from the xplat element, then we should no longer be managing its chidren
Clear(platformView);
base.DisconnectHandler(platformView);
}
void EnsureZIndexOrder(IView child)
{
if (PlatformView.ChildCount == 0)
{
return;
}
AView platformChildView = child.ToPlatform(MauiContext!);
var currentIndex = IndexOf(PlatformView, platformChildView);
if (currentIndex == -1)
{
return;
}
var targetIndex = VirtualView.GetLayoutHandlerIndex(child);
if (currentIndex != targetIndex)
{
PlatformView.RemoveViewAt(currentIndex);
PlatformView.AddView(platformChildView, targetIndex);
}
}
static int IndexOf(ViewGroup viewGroup, AView view)
{
for (int n = 0; n < viewGroup.ChildCount; n++)
{
if (viewGroup.GetChildAt(n) == view)
{
return n;
}
}
return -1;
}
static void MapInputTransparent(ILayoutHandler handler, ILayout layout)
{
if (handler.PlatformView is LayoutViewGroup layoutViewGroup)
{
layoutViewGroup.InputTransparent = layout.InputTransparent;
}
}
}
}