/
MaterialFrameRenderer.cs
265 lines (217 loc) · 6.76 KB
/
MaterialFrameRenderer.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
using System;
using System.ComponentModel;
using System.Linq;
using CoreAnimation;
using CoreGraphics;
using MaterialComponents;
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
using MCard = MaterialComponents.Card;
namespace Xamarin.Forms.Material.iOS
{
public class MaterialFrameRenderer : MCard,
IVisualElementRenderer
{
CardScheme _defaultCardScheme;
CardScheme _cardScheme;
float _defaultCornerRadius = -1;
VisualElementPackager _packager;
VisualElementTracker _tracker;
EventTracker _events;
bool _disposed = false;
public event EventHandler<VisualElementChangedEventArgs> ElementChanged;
public Frame Element { get; private set; }
public override void WillRemoveSubview(UIView uiview)
{
var content = Element?.Content;
if (content != null && uiview == Platform.iOS.Platform.GetRenderer(content))
{
uiview.Layer.Mask = null;
}
base.WillRemoveSubview(uiview);
}
public override void LayoutSubviews()
{
base.LayoutSubviews();
// if the card's shadow has a path/shape, then use that to clip the contents
var content = Element?.Content;
if (content != null && Layer is ShapedShadowLayer shadowLayer && shadowLayer.ShapeLayer.Path is CGPath shapePath)
{
var renderer = Platform.iOS.Platform.GetRenderer(content);
if (renderer is UIView uiview)
{
var padding = Element.Padding;
var offset = CGAffineTransform.MakeTranslation((nfloat)(-padding.Left), (nfloat)(-padding.Top));
uiview.Layer.Mask = new CAShapeLayer
{
Path = new CGPath(shapePath, offset)
};
}
}
}
public void SetElement(VisualElement element)
{
_cardScheme?.Dispose();
_cardScheme = CreateCardScheme();
var oldElement = Element;
if (oldElement != null)
{
oldElement.PropertyChanged -= OnElementPropertyChanged;
}
if (element is null)
Element = null;
else
Element = element as Frame ?? throw new ArgumentException("Element must be of type Frame.");
if (Element != null)
{
if (_packager == null)
{
_defaultCardScheme = CreateCardScheme();
_packager = new VisualElementPackager(this);
_packager.Load();
_tracker = new VisualElementTracker(this);
_events = new EventTracker(this);
_events.LoadEvents(this);
}
Element.PropertyChanged += OnElementPropertyChanged;
ApplyTheme();
}
OnElementChanged(new VisualElementChangedEventArgs(oldElement, element));
if (element != null)
element.SendViewInitialized(this);
if (!string.IsNullOrEmpty(element?.AutomationId))
AccessibilityIdentifier = element.AutomationId;
}
protected override void Dispose(bool disposing)
{
if (disposing && !_disposed)
{
_disposed = true;
if (_packager == null)
return;
_packager.Dispose();
_packager = null;
_tracker.Dispose();
_tracker = null;
_events.Dispose();
_events = null;
if (Element != null)
{
Element.ClearValue(Platform.iOS.Platform.RendererProperty);
SetElement(null);
}
}
base.Dispose(disposing);
}
protected virtual CardScheme CreateCardScheme()
{
return new CardScheme
{
ColorScheme = MaterialColors.Light.CreateColorScheme()
};
}
protected virtual void ApplyTheme()
{
UpdateCornerRadius();
UpdateBorderColor();
UpdateBackgroundColor();
if (Element.BorderColor.IsDefault)
CardThemer.ApplyScheme(_cardScheme, this);
else
CardThemer.ApplyOutlinedVariant(_cardScheme, this);
// a special case for no shadow
if (!Element.HasShadow)
SetShadowElevation(0, UIControlState.Normal);
if (Element.GestureRecognizers != null && Element.GestureRecognizers.Any())
{
Interactable = true;
// disable ink (ripple) and elevation effect while tapped
InkView.Hidden = true;
if (Element.HasShadow)
SetShadowElevation(1f, UIControlState.Highlighted);
}
else
{
// this is set in the theme, so we must always disable it
Interactable = false;
}
}
protected virtual void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
var updatedTheme = false;
if (e.PropertyName == Xamarin.Forms.Frame.HasShadowProperty.PropertyName)
{
// this is handled in ApplyTheme
updatedTheme = true;
}
else if (e.PropertyName == Xamarin.Forms.Frame.CornerRadiusProperty.PropertyName)
{
updatedTheme = true;
}
else if (e.PropertyName == Xamarin.Forms.Frame.BorderColorProperty.PropertyName)
{
updatedTheme = true;
}
else if (e.PropertyName == VisualElement.BackgroundColorProperty.PropertyName)
{
updatedTheme = true;
}
if (updatedTheme)
ApplyTheme();
}
protected virtual void OnElementChanged(VisualElementChangedEventArgs e) => ElementChanged?.Invoke(this, e);
void UpdateCornerRadius()
{
// set the default radius on the first time
if (_defaultCornerRadius < 0)
_defaultCornerRadius = (float)MaterialColors.kFrameCornerRadiusDefault;
var cornerRadius = Element.CornerRadius;
if (cornerRadius < 0)
cornerRadius = _defaultCornerRadius;
if (_cardScheme != null)
{
var shapeScheme = new ShapeScheme();
var shapeCategory = new ShapeCategory(ShapeCornerFamily.Rounded, cornerRadius);
shapeScheme.SmallComponentShape = shapeCategory;
shapeScheme.MediumComponentShape = shapeCategory;
shapeScheme.LargeComponentShape = shapeCategory;
_cardScheme.ShapeScheme = shapeScheme;
}
CornerRadius = cornerRadius;
}
void UpdateBorderColor()
{
if (_cardScheme.ColorScheme is SemanticColorScheme colorScheme)
{
var borderColor = Element.BorderColor;
if (borderColor.IsDefault)
colorScheme.OnSurfaceColor = _defaultCardScheme.ColorScheme.OnSurfaceColor;
else
colorScheme.OnSurfaceColor = borderColor.ToUIColor();
SetBorderWidth(borderColor.IsDefault ? 0f : 1f, UIControlState.Normal);
}
}
void UpdateBackgroundColor()
{
if (_cardScheme.ColorScheme is SemanticColorScheme colorScheme)
{
var bgColor = Element.BackgroundColor;
if (bgColor.IsDefault)
colorScheme.SurfaceColor = _defaultCardScheme.ColorScheme.SurfaceColor;
else
colorScheme.SurfaceColor = bgColor.ToUIColor();
}
}
// IVisualElementRenderer
VisualElement IVisualElementRenderer.Element => Element;
UIView IVisualElementRenderer.NativeView => this;
UIViewController IVisualElementRenderer.ViewController => null;
SizeRequest IVisualElementRenderer.GetDesiredSize(double widthConstraint, double heightConstraint) =>
this.GetSizeRequest(widthConstraint, heightConstraint, 44, 44);
void IVisualElementRenderer.SetElement(VisualElement element) =>
SetElement(element);
void IVisualElementRenderer.SetElementSize(Size size) =>
Layout.LayoutChildIntoBoundingRegion(Element, new Rectangle(Element.X, Element.Y, size.Width, size.Height));
}
}