-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
LightweightPlatform.cs
222 lines (181 loc) · 5.56 KB
/
LightweightPlatform.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using ElmSharp;
using Xamarin.Forms.Internals;
using EColor = ElmSharp.Color;
namespace Xamarin.Forms.Platform.Tizen
{
public class LightweightPlatform : ITizenPlatform, INavigation, IDisposable
{
NavigationModel _navModel = new NavigationModel();
Native.Canvas _viewStack;
readonly PopupManager _popupManager;
bool _hasAlpha;
readonly EColor _defaultPlatformColor;
public LightweightPlatform(EvasObject parent)
{
Forms.NativeParent = parent;
_defaultPlatformColor = Device.Idiom == TargetIdiom.Phone ? EColor.White : EColor.Transparent;
_viewStack = new Native.Canvas(parent)
{
BackgroundColor = _defaultPlatformColor,
};
_viewStack.SetAlignment(-1, -1);
_viewStack.SetWeight(1.0, 1.0);
_viewStack.LayoutUpdated += OnLayout;
_viewStack.Show();
if (Forms.UseMessagingCenter)
{
_popupManager = new PopupManager(this);
}
}
#pragma warning disable 0067
public event EventHandler<RootNativeViewChangedEventArgs> RootNativeViewChanged;
#pragma warning restore 0067
public bool HasAlpha
{
get => _hasAlpha;
set
{
_hasAlpha = value;
_viewStack.BackgroundColor = _hasAlpha ? EColor.Transparent : _defaultPlatformColor;
}
}
IPageController CurrentPageController => _navModel.CurrentPage as IPageController;
IReadOnlyList<Page> INavigation.ModalStack => _navModel.Modals.ToList();
IReadOnlyList<Page> INavigation.NavigationStack => new List<Page>();
public void SetPage(Page page)
{
ResetChildren();
_navModel = new NavigationModel();
if (page == null)
return;
_navModel.Push(page, null);
#pragma warning disable CS0618 // Type or member is obsolete
page.Platform = this;
#pragma warning restore CS0618 // Type or member is obsolete
((Application)page.RealParent).NavigationProxy.Inner = this;
var renderer = Platform.CreateRenderer(page);
renderer.NativeView.Geometry = _viewStack.Geometry;
_viewStack.Children.Add(renderer.NativeView);
CurrentPageController?.SendAppearing();
}
public bool SendBackButtonPressed()
{
return _navModel?.CurrentPage?.SendBackButtonPressed() ?? false;
}
public EvasObject GetRootNativeView()
{
return _viewStack;
}
public bool PageIsChildOfPlatform(Page page)
{
var parent = page.AncestorToRoot();
return _navModel.Modals.FirstOrDefault() == page || _navModel.Roots.Contains(parent);
}
public void Dispose()
{
Dispose(true);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
_popupManager?.Dispose();
_viewStack?.Unrealize();
}
}
Task<Page> INavigation.PopModalAsync()
{
return (this as INavigation).PopModalAsync(true);
}
Task<Page> INavigation.PopModalAsync(bool animated)
{
Page page = _navModel.PopModal();
(page as IPageController)?.SendDisappearing();
var renderer = Platform.GetRenderer(page);
_viewStack.Children.Remove(renderer.NativeView);
renderer.Dispose();
_viewStack.Children.LastOrDefault()?.Show();
CurrentPageController?.SendAppearing();
return Task.FromResult(page);
}
Task INavigation.PushModalAsync(Page modal)
{
return (this as INavigation).PushModalAsync(modal, true);
}
Task INavigation.PushModalAsync(Page page, bool animated)
{
var previousPage = CurrentPageController;
previousPage?.SendDisappearing();
_navModel.PushModal(page);
var lastTop = _viewStack.Children.LastOrDefault();
var renderer = Platform.GetOrCreateRenderer(page);
renderer.NativeView.Geometry = _viewStack.Geometry;
_viewStack.Children.Add(renderer.NativeView);
if (lastTop != null)
{
lastTop.Hide();
renderer.NativeView.StackAbove(lastTop);
}
// Verify that the modal is still on the stack
if (_navModel.CurrentPage == page)
CurrentPageController.SendAppearing();
return Task.CompletedTask;
}
void INavigation.InsertPageBefore(Page page, Page before)
{
throw new InvalidOperationException("InsertPageBefore is not supported globally on Tizen, please use a NavigationPage.");
}
Task<Page> INavigation.PopAsync()
{
return ((INavigation)this).PopAsync(true);
}
Task<Page> INavigation.PopAsync(bool animated)
{
throw new InvalidOperationException("PopAsync is not supported globally on Tizen, please use a NavigationPage.");
}
Task INavigation.PopToRootAsync()
{
return ((INavigation)this).PopToRootAsync(true);
}
Task INavigation.PopToRootAsync(bool animated)
{
throw new InvalidOperationException("PopToRootAsync is not supported globally on Tizen, please use a NavigationPage.");
}
Task INavigation.PushAsync(Page root)
{
return ((INavigation)this).PushAsync(root, true);
}
Task INavigation.PushAsync(Page root, bool animated)
{
throw new InvalidOperationException("PushAsync is not supported globally on Tizen, please use a NavigationPage.");
}
void INavigation.RemovePage(Page page)
{
throw new InvalidOperationException("RemovePage is not supported globally on Tizen, please use a NavigationPage.");
}
SizeRequest IPlatform.GetNativeSize(VisualElement view, double widthConstraint, double heightConstraint)
{
return Platform.GetNativeSize(view, widthConstraint, heightConstraint);
}
void OnLayout(object sender, Native.LayoutEventArgs e)
{
foreach (var child in _viewStack.Children)
{
child.Geometry = _viewStack.Geometry;
}
}
void ResetChildren()
{
var children = _viewStack.Children.ToList();
_viewStack.Children.Clear();
foreach (var child in children)
{
child.Unrealize();
}
}
}
}