/
ContentViewGroup.cs
170 lines (140 loc) · 4.98 KB
/
ContentViewGroup.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
using System;
using Android.Content;
using Android.Graphics;
using Android.Runtime;
using Android.Util;
using Android.Views;
using Microsoft.Maui.Graphics;
using Microsoft.Maui.Graphics.Platform;
namespace Microsoft.Maui.Platform
{
public class ContentViewGroup : PlatformContentViewGroup, ICrossPlatformLayoutBacking, IVisualTreeElementProvidable
{
IBorderStroke? _clip;
readonly Context _context;
public ContentViewGroup(Context context) : base(context)
{
_context = context;
}
public ContentViewGroup(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer)
{
var context = Context;
ArgumentNullException.ThrowIfNull(context);
_context = context;
}
public ContentViewGroup(Context context, IAttributeSet attrs) : base(context, attrs)
{
_context = context;
}
public ContentViewGroup(Context context, IAttributeSet attrs, int defStyleAttr) : base(context, attrs, defStyleAttr)
{
_context = context;
}
public ContentViewGroup(Context context, IAttributeSet attrs, int defStyleAttr, int defStyleRes) : base(context, attrs, defStyleAttr, defStyleRes)
{
_context = context;
}
public ICrossPlatformLayout? CrossPlatformLayout
{
get; set;
}
Graphics.Size CrossPlatformMeasure(double widthConstraint, double heightConstraint)
{
return CrossPlatformLayout?.CrossPlatformMeasure(widthConstraint, heightConstraint) ?? Graphics.Size.Zero;
}
Graphics.Size CrossPlatformArrange(Graphics.Rect bounds)
{
return CrossPlatformLayout?.CrossPlatformArrange(bounds) ?? Graphics.Size.Zero;
}
protected override void OnMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
{
if (CrossPlatformLayout is IViewHandler handler &&
handler.VirtualView is IScrollView scrollView &&
scrollView.Orientation == ScrollOrientation.Horizontal)
{
System.Diagnostics.Debug.WriteLine($"");
System.Diagnostics.Debug.WriteLine($"OnMeasure Start {scrollView} {widthMeasureSpec} {heightMeasureSpec}");
}
}
if (CrossPlatformMeasure == null)
{
base.OnMeasure(widthMeasureSpec, heightMeasureSpec);
return;
}
var deviceIndependentWidth = widthMeasureSpec.ToDouble(_context);
var deviceIndependentHeight = heightMeasureSpec.ToDouble(_context);
var widthMode = MeasureSpec.GetMode(widthMeasureSpec);
var heightMode = MeasureSpec.GetMode(heightMeasureSpec);
var measure = CrossPlatformMeasure(deviceIndependentWidth, deviceIndependentHeight);
// If the measure spec was exact, we should return the explicit size value, even if the content
// measure came out to a different size
var width = widthMode == MeasureSpecMode.Exactly ? deviceIndependentWidth : measure.Width;
var height = heightMode == MeasureSpecMode.Exactly ? deviceIndependentHeight : measure.Height;
var platformWidth = _context.ToPixels(width);
var platformHeight = _context.ToPixels(height);
// Minimum values win over everything
platformWidth = Math.Max(MinimumWidth, platformWidth);
platformHeight = Math.Max(MinimumHeight, platformHeight);
SetMeasuredDimension((int)platformWidth, (int)platformHeight);
{
if (CrossPlatformLayout is IViewHandler handler &&
handler.VirtualView is IScrollView scrollView &&
scrollView.Orientation == ScrollOrientation.Horizontal)
{
System.Diagnostics.Debug.WriteLine($"OnMeasure End {scrollView} {platformWidth} {platformHeight}");
}
}
}
protected override void OnLayout(bool changed, int left, int top, int right, int bottom)
{
if (CrossPlatformArrange == null)
{
return;
}
var destination = _context.ToCrossPlatformRectInReferenceFrame(left, top, right, bottom);
CrossPlatformArrange(destination);
if (CrossPlatformLayout is IViewHandler handler &&
handler.VirtualView is IScrollView scrollView &&
scrollView.Orientation == ScrollOrientation.Horizontal)
{
System.Diagnostics.Debug.WriteLine($"OnLayout {scrollView} {destination} {left} {top} {right} {bottom}");
System.Diagnostics.Debug.WriteLine($"");
}
}
internal IBorderStroke? Clip
{
get => _clip;
set
{
_clip = value;
// NOTE: calls PostInvalidate()
SetHasClip(_clip is not null);
}
}
protected override Path? GetClipPath(int width, int height)
{
if (Clip is null || Clip?.Shape is null)
return null;
float density = _context.GetDisplayDensity();
float strokeThickness = (float)Clip.StrokeThickness;
float w = (width / density) - strokeThickness;
float h = (height / density) - strokeThickness;
float x = strokeThickness / 2;
float y = strokeThickness / 2;
IShape clipShape = Clip.Shape;
var bounds = new Graphics.RectF(x, y, w, h);
Path? platformPath = clipShape.ToPlatform(bounds, strokeThickness, density, true);
return platformPath;
}
IVisualTreeElement? IVisualTreeElementProvidable.GetElement()
{
if (CrossPlatformLayout is IVisualTreeElement layoutElement &&
layoutElement.IsThisMyPlatformView(this))
{
return layoutElement;
}
return null;
}
}
}