This repository has been archived by the owner on May 1, 2024. It is now read-only.
/
PageRenderer.cs
451 lines (363 loc) · 11.1 KB
/
PageRenderer.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
using System;
using System.Collections.Generic;
using System.ComponentModel;
using Foundation;
using UIKit;
using Xamarin.Forms.PlatformConfiguration.iOSSpecific;
using PageUIStatusBarAnimation = Xamarin.Forms.PlatformConfiguration.iOSSpecific.UIStatusBarAnimation;
namespace Xamarin.Forms.Platform.iOS
{
public class PageRenderer : UIViewController, IVisualElementRenderer, IEffectControlProvider, IAccessibilityElementsController
{
bool _appeared;
bool _disposed;
EventTracker _events;
VisualElementPackager _packager;
VisualElementTracker _tracker;
internal PageContainer Container => NativeView as PageContainer;
Page Page => Element as Page;
IAccessibilityElementsController AccessibilityElementsController => this;
bool UsingSafeArea => (Forms.IsiOS11OrNewer) ? Page.On<PlatformConfiguration.iOS>().UsingSafeArea() : false;
Thickness SafeAreaInsets => Page.On<PlatformConfiguration.iOS>().SafeAreaInsets();
public PageRenderer()
{
}
void IEffectControlProvider.RegisterEffect(Effect effect)
{
VisualElementRenderer<VisualElement>.RegisterEffect(effect, NativeView);
}
public VisualElement Element { get; private set; }
public event EventHandler<VisualElementChangedEventArgs> ElementChanged;
public List<NSObject> GetAccessibilityElements()
{
if (Container == null || Element == null)
return null;
var children = Element.Descendants();
SortedDictionary<int, List<VisualElement>> tabIndexes = null;
List<NSObject> views = new List<NSObject>();
foreach (var child in children)
{
if (!(child is VisualElement ve))
continue;
tabIndexes = ve.GetSortedTabIndexesOnParentPage(out _);
break;
}
if (tabIndexes == null)
return null;
foreach (var idx in tabIndexes?.Keys)
{
var tabGroup = tabIndexes[idx];
foreach (var child in tabGroup)
{
if (child is Layout ||
!(
child is VisualElement ve && ve.IsTabStop
&& AutomationProperties.GetIsInAccessibleTree(ve) != false // accessible == true
&& ve.GetRenderer().NativeView is ITabStop tabStop)
)
continue;
var thisControl = tabStop.TabStop;
if (thisControl == null)
continue;
if (views.Contains(thisControl))
break; // we've looped to the beginning
views.Add(thisControl);
}
}
return views;
}
public SizeRequest GetDesiredSize(double widthConstraint, double heightConstraint)
{
return NativeView.GetSizeRequest(widthConstraint, heightConstraint);
}
public UIView NativeView
{
get { return _disposed ? null : View; }
}
public void SetElement(VisualElement element)
{
VisualElement oldElement = Element;
Element = element;
UpdateTitle();
OnElementChanged(new VisualElementChangedEventArgs(oldElement, element));
if (element != null)
{
if (!string.IsNullOrEmpty(element.AutomationId))
SetAutomationId(element.AutomationId);
element.SendViewInitialized(NativeView);
}
EffectUtilities.RegisterEffectControlProvider(this, oldElement, element);
}
public void SetElementSize(Size size)
{
Element.Layout(new Rectangle(Element.X, Element.Y, size.Width, size.Height));
}
public override void LoadView()
{
View = new PageContainer(this);
}
public override void ViewWillLayoutSubviews()
{
base.ViewWillLayoutSubviews();
AccessibilityElementsController.ResetAccessibilityElements();
}
public override void ViewDidLayoutSubviews()
{
base.ViewDidLayoutSubviews();
if (_disposed)
return;
if (Element.Parent is BaseShellItem)
Element.Layout(View.Bounds.ToRectangle());
UpdateShellInsetPadding();
}
public override void ViewSafeAreaInsetsDidChange()
{
UpdateShellInsetPadding();
if (Page != null && Forms.IsiOS11OrNewer)
{
var insets = NativeView.SafeAreaInsets;
if (Page.Parent is TabbedPage)
{
insets.Bottom = 0;
}
Page.On<PlatformConfiguration.iOS>().SetSafeAreaInsets(new Thickness(insets.Left, insets.Top, insets.Right, insets.Bottom));
}
base.ViewSafeAreaInsetsDidChange();
}
public UIViewController ViewController => _disposed ? null : this;
public override void ViewDidAppear(bool animated)
{
base.ViewDidAppear(animated);
if (_appeared || _disposed)
return;
_appeared = true;
UpdateStatusBarPrefersHidden();
if(Forms.IsiOS11OrNewer)
SetNeedsUpdateOfHomeIndicatorAutoHidden();
if (Element.Parent is CarouselPage)
return;
Page.SendAppearing();
}
public override void ViewDidDisappear(bool animated)
{
base.ViewDidDisappear(animated);
if (!_appeared || _disposed)
return;
_appeared = false;
if (Element.Parent is CarouselPage)
return;
Page.SendDisappearing();
}
public override void ViewDidLoad()
{
base.ViewDidLoad();
if (NativeView == null)
return;
var uiTapGestureRecognizer = new UITapGestureRecognizer(a => NativeView.EndEditing(true));
uiTapGestureRecognizer.ShouldRecognizeSimultaneously = (recognizer, gestureRecognizer) => true;
uiTapGestureRecognizer.ShouldReceiveTouch = OnShouldReceiveTouch;
uiTapGestureRecognizer.DelaysTouchesBegan =
uiTapGestureRecognizer.DelaysTouchesEnded = uiTapGestureRecognizer.CancelsTouchesInView = false;
NativeView.AddGestureRecognizer(uiTapGestureRecognizer);
UpdateBackground();
_packager = new VisualElementPackager(this);
_packager.Load();
Element.PropertyChanged += OnHandlePropertyChanged;
_tracker = new VisualElementTracker(this, !(Element.Parent is BaseShellItem));
_events = new EventTracker(this);
_events.LoadEvents(NativeView);
Element.SendViewInitialized(NativeView);
}
public override void ViewWillDisappear(bool animated)
{
base.ViewWillDisappear(animated);
NativeView?.Window?.EndEditing(true);
}
protected override void Dispose(bool disposing)
{
if (disposing && !_disposed)
{
Element.PropertyChanged -= OnHandlePropertyChanged;
Platform.SetRenderer(Element, null);
if (_appeared)
Page.SendDisappearing();
_appeared = false;
if (_events != null)
{
_events.Dispose();
_events = null;
}
if (_packager != null)
{
_packager.Dispose();
_packager = null;
}
if (_tracker != null)
{
_tracker.Dispose();
_tracker = null;
}
Element = null;
Container?.Dispose();
_disposed = true;
}
base.Dispose(disposing);
}
protected virtual void OnElementChanged(VisualElementChangedEventArgs e)
{
ElementChanged?.Invoke(this, e);
}
protected virtual void SetAutomationId(string id)
{
if (NativeView != null)
NativeView.AccessibilityIdentifier = id;
}
void OnHandlePropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == VisualElement.BackgroundColorProperty.PropertyName)
UpdateBackground();
else if (e.PropertyName == Page.BackgroundImageProperty.PropertyName)
UpdateBackground();
else if (e.PropertyName == Page.TitleProperty.PropertyName)
UpdateTitle();
else if (e.PropertyName == PlatformConfiguration.iOSSpecific.Page.PrefersStatusBarHiddenProperty.PropertyName)
UpdateStatusBarPrefersHidden();
else if (Forms.IsiOS11OrNewer && e.PropertyName == PlatformConfiguration.iOSSpecific.Page.UseSafeAreaProperty.PropertyName)
UpdateUseSafeArea();
else if (Forms.IsiOS11OrNewer && e.PropertyName == PlatformConfiguration.iOSSpecific.Page.SafeAreaInsetsProperty.PropertyName)
UpdateUseSafeArea();
else if (e.PropertyName == PlatformConfiguration.iOSSpecific.Page.PrefersHomeIndicatorAutoHiddenProperty.PropertyName)
UpdateHomeIndicatorAutoHidden();
}
public override UIKit.UIStatusBarAnimation PreferredStatusBarUpdateAnimation
{
get
{
var animation = ((Page)Element).OnThisPlatform().PreferredStatusBarUpdateAnimation();
switch (animation)
{
case (PageUIStatusBarAnimation.Fade):
return UIKit.UIStatusBarAnimation.Fade;
case (PageUIStatusBarAnimation.Slide):
return UIKit.UIStatusBarAnimation.Slide;
case (PageUIStatusBarAnimation.None):
default:
return UIKit.UIStatusBarAnimation.None;
}
}
}
void IAccessibilityElementsController.ResetAccessibilityElements()
{
Container?.ClearAccessibilityElements();
}
void UpdateUseSafeArea()
{
if (!Forms.IsiOS11OrNewer)
return;
if (!UsingSafeArea)
{
var safeAreaInsets = SafeAreaInsets;
if (safeAreaInsets == Page.Padding)
Page.Padding = default(Thickness);
}
else
{
Page.Padding = SafeAreaInsets;
}
}
void UpdateShellInsetPadding()
{
if (Element == null)
return;
var setInsets = Shell.GetSetPaddingInsets(Element);
if (!setInsets && Element.Parent != null)
setInsets = Shell.GetSetPaddingInsets(Element.Parent);
if (setInsets)
{
nfloat topPadding = 0;
nfloat bottomPadding = 0;
if (Forms.IsiOS11OrNewer)
{
topPadding = View.SafeAreaInsets.Top;
bottomPadding = View.SafeAreaInsets.Bottom;
}
else
{
topPadding = TopLayoutGuide.Length;
bottomPadding = BottomLayoutGuide.Length;
}
(Element as Page).Padding = new Thickness(0, topPadding, 0, bottomPadding);
}
}
void UpdateStatusBarPrefersHidden()
{
if (Element == null)
return;
var animation = ((Page)Element).OnThisPlatform().PreferredStatusBarUpdateAnimation();
if (animation == PageUIStatusBarAnimation.Fade || animation == PageUIStatusBarAnimation.Slide)
UIView.Animate(0.25, () => SetNeedsStatusBarAppearanceUpdate());
else
SetNeedsStatusBarAppearanceUpdate();
NativeView?.SetNeedsLayout();
}
bool OnShouldReceiveTouch(UIGestureRecognizer recognizer, UITouch touch)
{
foreach (UIView v in ViewAndSuperviewsOfView(touch.View))
{
if (v is UITableView || v is UITableViewCell || v.CanBecomeFirstResponder)
return false;
}
return true;
}
public override bool PrefersStatusBarHidden()
{
var mode = ((Page)Element).OnThisPlatform().PrefersStatusBarHidden();
switch (mode)
{
case (StatusBarHiddenMode.True):
return true;
case (StatusBarHiddenMode.False):
return false;
case (StatusBarHiddenMode.Default):
default:
return base.PrefersStatusBarHidden();
}
}
void UpdateBackground()
{
if (NativeView == null)
return;
string bgImage = ((Page)Element).BackgroundImage;
if (!string.IsNullOrEmpty(bgImage))
{
NativeView.BackgroundColor = ColorExtensions.FromPatternImageFromBundle(bgImage);
return;
}
Color bgColor = Element.BackgroundColor;
if (bgColor.IsDefault)
NativeView.BackgroundColor = UIColor.White;
else
NativeView.BackgroundColor = bgColor.ToUIColor();
}
void UpdateTitle()
{
if (!string.IsNullOrWhiteSpace(((Page)Element).Title))
NavigationItem.Title = ((Page)Element).Title;
}
IEnumerable<UIView> ViewAndSuperviewsOfView(UIView view)
{
while (view != null)
{
yield return view;
view = view.Superview;
}
}
void UpdateHomeIndicatorAutoHidden()
{
if (Element == null || !Forms.IsiOS11OrNewer)
return;
SetNeedsUpdateOfHomeIndicatorAutoHidden();
}
public override bool PrefersHomeIndicatorAutoHidden => ((Page)Element).OnThisPlatform().PrefersHomeIndicatorAutoHidden();
}
}