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
/
ContainerView.cs
118 lines (95 loc) · 2.64 KB
/
ContainerView.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
using Android.Content;
using Android.Runtime;
using Android.Util;
using Android.Views;
using System;
namespace Xamarin.Forms.Platform.Android
{
public class ContainerView : ViewGroup
{
Context _context;
IVisualElementRenderer _renderer;
View _view;
public ContainerView(Context context, View view) : base(context)
{
_context = context;
View = view;
}
public ContainerView(Context context, IAttributeSet attribs) : base(context, attribs)
{
}
public ContainerView(Context context, IAttributeSet attribs, int defStyleAttr) : base(context, attribs, defStyleAttr)
{
}
protected ContainerView(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer)
{
}
public bool MatchHeight { get; set; }
public bool MatchWidth { get; set; }
public View View
{
get { return _view; }
set
{
_view = value;
OnViewSet(value);
}
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
if (disposing)
{
_renderer?.Dispose();
_renderer = null;
_view = null;
_context = null;
}
}
protected override void OnLayout(bool changed, int l, int t, int r, int b)
{
if (_renderer == null)
return;
var width = _context.FromPixels(r - l);
var height = _context.FromPixels(b - t);
LayoutView(0, 0, width, height);
_renderer.UpdateLayout();
}
protected virtual void LayoutView(double x, double y, double width, double height)
{
View?.Layout(new Rectangle(x, y, width, height));
}
protected override void OnMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
if (View == null)
{
SetMeasuredDimension(0, 0);
return;
}
// chain on down
_renderer.View.Measure(widthMeasureSpec, heightMeasureSpec);
var width = MeasureSpecFactory.GetSize(widthMeasureSpec);
var height = MeasureSpecFactory.GetSize(heightMeasureSpec);
var measureWidth = width > 0 ? _context.FromPixels(width) : double.PositiveInfinity;
var measureHeight = height > 0 ? _context.FromPixels(height) : double.PositiveInfinity;
var sizeReq = View.Measure(measureWidth, measureHeight);
SetMeasuredDimension((MatchWidth && width != 0) ? width : (int)_context.ToPixels(sizeReq.Request.Width),
(MatchHeight && height != 0) ? height : (int)_context.ToPixels(sizeReq.Request.Height));
}
protected virtual void OnViewSet(View view)
{
if (_renderer != null)
{
_renderer.View.RemoveFromParent();
_renderer.Dispose();
_renderer = null;
}
if (view != null)
{
_renderer = Platform.CreateRenderer(view, Context);
Platform.SetRenderer(view, _renderer);
AddView(_renderer.View);
}
}
}
}