This repository was 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
/
Copy pathModalWrapper.cs
175 lines (143 loc) · 4.83 KB
/
ModalWrapper.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
using System;
using System.Linq;
using Xamarin.Forms.PlatformConfiguration.iOSSpecific;
using UIKit;
using Foundation;
using System.Threading.Tasks;
using System.ComponentModel;
namespace Xamarin.Forms.Platform.iOS
{
internal class ModalWrapper : UIViewController, IUIAdaptivePresentationControllerDelegate
{
IVisualElementRenderer _modal;
bool _isDisposed;
internal ModalWrapper(IVisualElementRenderer modal)
{
_modal = modal;
var elementConfiguration = modal.Element as IElementConfiguration<Page>;
if (elementConfiguration?.On<PlatformConfiguration.iOS>()?.ModalPresentationStyle() is PlatformConfiguration.iOSSpecific.UIModalPresentationStyle style)
{
var result = style.ToNativeModalPresentationStyle();
if (!Forms.IsiOS13OrNewer && result == UIKit.UIModalPresentationStyle.Automatic)
{
result = UIKit.UIModalPresentationStyle.FullScreen;
}
if (result == UIKit.UIModalPresentationStyle.FullScreen)
{
Color modalBkgndColor = ((Page)_modal.Element).BackgroundColor;
if (modalBkgndColor.A > 0)
result = UIKit.UIModalPresentationStyle.OverFullScreen;
}
ModalPresentationStyle = result;
}
UpdateBackgroundColor();
View.AddSubview(modal.ViewController.View);
TransitioningDelegate = modal.ViewController.TransitioningDelegate;
AddChildViewController(modal.ViewController);
modal.ViewController.DidMoveToParentViewController(this);
if (Forms.IsiOS13OrNewer)
PresentationController.Delegate = this;
((Page)modal.Element).PropertyChanged += OnModalPagePropertyChanged;
}
[Export("presentationControllerDidDismiss:")]
[Internals.Preserve(Conditional = true)]
public async void DidDismiss(UIPresentationController presentationController)
{
await Application.Current.NavigationProxy.PopModalAsync(false);
}
public override void DismissViewController(bool animated, Action completionHandler)
{
base.DismissViewController(animated, completionHandler);
}
public override UIInterfaceOrientationMask GetSupportedInterfaceOrientations()
{
if ((ChildViewControllers != null) && (ChildViewControllers.Length > 0))
{
return ChildViewControllers[0].GetSupportedInterfaceOrientations();
}
return base.GetSupportedInterfaceOrientations();
}
public override bool ModalPresentationCapturesStatusBarAppearance => true;
public override UIInterfaceOrientation PreferredInterfaceOrientationForPresentation()
{
if ((ChildViewControllers != null) && (ChildViewControllers.Length > 0))
{
return ChildViewControllers[0].PreferredInterfaceOrientationForPresentation();
}
return base.PreferredInterfaceOrientationForPresentation();
}
public override bool ShouldAutorotate()
{
if ((ChildViewControllers != null) && (ChildViewControllers.Length > 0))
{
return ChildViewControllers[0].ShouldAutorotate();
}
return base.ShouldAutorotate();
}
public override bool ShouldAutorotateToInterfaceOrientation(UIInterfaceOrientation toInterfaceOrientation)
{
if ((ChildViewControllers != null) && (ChildViewControllers.Length > 0))
{
return ChildViewControllers[0].ShouldAutorotateToInterfaceOrientation(toInterfaceOrientation);
}
return base.ShouldAutorotateToInterfaceOrientation(toInterfaceOrientation);
}
public override bool ShouldAutomaticallyForwardRotationMethods => true;
public override void ViewDidLayoutSubviews()
{
base.ViewDidLayoutSubviews();
_modal?.SetElementSize(new Size(View.Bounds.Width, View.Bounds.Height));
}
public override void ViewWillAppear(bool animated)
{
if (!_isDisposed)
UpdateBackgroundColor();
base.ViewWillAppear(animated);
}
protected override void Dispose(bool disposing)
{
if (_isDisposed)
return;
_isDisposed = true;
if (disposing)
{
if (_modal?.Element is Page modalPage)
{
modalPage.PropertyChanged -= OnModalPagePropertyChanged;
}
_modal = null;
}
base.Dispose(disposing);
}
public override void ViewDidLoad()
{
base.ViewDidLoad();
SetNeedsStatusBarAppearanceUpdate();
if (Forms.RespondsToSetNeedsUpdateOfHomeIndicatorAutoHidden)
SetNeedsUpdateOfHomeIndicatorAutoHidden();
}
public override UIViewController ChildViewControllerForStatusBarStyle()
{
return ChildViewControllers?.LastOrDefault();
}
void OnModalPagePropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == Page.BackgroundColorProperty.PropertyName)
UpdateBackgroundColor();
}
void UpdateBackgroundColor()
{
if (_isDisposed)
return;
if (ModalPresentationStyle == UIKit.UIModalPresentationStyle.FullScreen)
{
Color modalBkgndColor = ((Page)_modal.Element).BackgroundColor;
View.BackgroundColor = modalBkgndColor.IsDefault ? ColorExtensions.BackgroundColor : modalBkgndColor.ToUIColor();
}
else
{
View.BackgroundColor = UIColor.Clear;
}
}
}
}