This repository has been 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
/
Platform.cs
280 lines (217 loc) · 7.36 KB
/
Platform.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
using Gtk;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Xamarin.Forms.Internals;
using Xamarin.Forms.Platform.GTK.Helpers;
using Xamarin.Forms.Platform.GTK.Renderers;
namespace Xamarin.Forms.Platform.GTK
{
public class Platform : BindableObject, INavigation, IDisposable
#pragma warning disable CS0618
, IPlatform
#pragma warning restore
{
private bool _disposed;
readonly List<Page> _modals;
private readonly PlatformRenderer _renderer;
internal static readonly BindableProperty RendererProperty =
BindableProperty.CreateAttached("Renderer", typeof(IVisualElementRenderer),
typeof(Platform), default(IVisualElementRenderer),
propertyChanged: (bindable, oldvalue, newvalue) =>
{
var view = bindable as VisualElement;
if (view != null)
view.IsPlatformEnabled = newvalue != null;
});
internal PlatformRenderer PlatformRenderer => _renderer;
Page Page { get; set; }
IReadOnlyList<Page> INavigation.ModalStack
{
get { return _modals; }
}
IReadOnlyList<Page> INavigation.NavigationStack
{
get { return new List<Page>(); }
}
internal Platform()
{
_renderer = new PlatformRenderer(this);
_modals = new List<Page>();
Application.Current.NavigationProxy.Inner = this;
MessagingCenter.Subscribe(this, Page.AlertSignalName, (Page sender, AlertArguments arguments) => DialogHelper.ShowAlert(PlatformRenderer, arguments));
MessagingCenter.Subscribe(this, Page.ActionSheetSignalName, (Page sender, ActionSheetArguments arguments) => DialogHelper.ShowActionSheet(PlatformRenderer, arguments));
}
internal static void DisposeModelAndChildrenRenderers(Element view)
{
IVisualElementRenderer renderer;
foreach (VisualElement child in view.Descendants())
DisposeModelAndChildrenRenderers(child);
renderer = GetRenderer((VisualElement)view);
(renderer as Widget)?.Destroy();
view.ClearValue(RendererProperty);
}
public static SizeRequest GetNativeSize(VisualElement view, double widthConstraint, double heightConstraint)
{
var renderView = GetRenderer(view);
if (renderView == null || renderView.Container == null)
return new SizeRequest(Size.Zero);
return renderView.GetDesiredSize(widthConstraint, heightConstraint);
}
public static IVisualElementRenderer GetRenderer(VisualElement element)
{
return (IVisualElementRenderer)element.GetValue(RendererProperty);
}
public static void SetRenderer(VisualElement element, IVisualElementRenderer value)
{
if (element != null)
{
element.SetValue(RendererProperty, value);
element.IsPlatformEnabled = value != null;
}
}
public static IVisualElementRenderer CreateRenderer(VisualElement element)
{
var renderer = Registrar.Registered.GetHandlerForObject<IVisualElementRenderer>(element) ?? new DefaultRenderer();
renderer.SetElement(element);
return renderer;
}
void IDisposable.Dispose()
{
if (_disposed)
return;
_disposed = true;
MessagingCenter.Unsubscribe<Page, ActionSheetArguments>(this, Page.ActionSheetSignalName);
MessagingCenter.Unsubscribe<Page, AlertArguments>(this, Page.AlertSignalName);
MessagingCenter.Unsubscribe<Page, bool>(this, Page.BusySetSignalName);
foreach (var modal in _modals)
DisposeModelAndChildrenRenderers(modal);
DisposeModelAndChildrenRenderers(Page);
PlatformRenderer.Destroy();
}
internal void SetPage(Page newRoot)
{
if (newRoot == null)
return;
if (Page != null)
throw new NotImplementedException();
Page = newRoot;
#pragma warning disable CS0618 // Type or member is obsolete
// The Platform property is no longer necessary, but we have to set it because some third-party
// library might still be retrieving it and using it
Page.Platform = this;
#pragma warning restore CS0618 // Type or member is obsolete
AddChild(Page);
Application.Current.NavigationProxy.Inner = this;
}
private void AddChild(Page mainPage)
{
var viewRenderer = GetRenderer(mainPage);
if (viewRenderer == null)
{
viewRenderer = CreateRenderer(mainPage);
SetRenderer(mainPage, viewRenderer);
PlatformRenderer.Add(viewRenderer.Container);
PlatformRenderer.ShowAll();
}
}
private void HandleChildRemoved(object sender, ElementEventArgs e)
{
var view = e.Element;
DisposeModelAndChildrenRenderers(view);
}
void INavigation.InsertPageBefore(Page page, Page before)
{
throw new InvalidOperationException("InsertPageBefore is not supported globally on GTK, 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 GTK, please use a NavigationPage.");
}
Task<Page> INavigation.PopModalAsync()
{
return ((INavigation)this).PopModalAsync(true);
}
Task<Page> INavigation.PopModalAsync(bool animated)
{
var modal = _modals.Last();
_modals.Remove(modal);
modal.DescendantRemoved -= HandleChildRemoved;
var modalPage = GetRenderer(modal) as Container;
var pageControl = PlatformRenderer.Child as IPageControl;
Device.BeginInvokeOnMainThread(() =>
{
pageControl?.Control?.PopModal(modalPage);
DisposeModelAndChildrenRenderers(modal);
});
return Task.FromResult<Page>(modal);
}
Task INavigation.PopToRootAsync()
{
return ((INavigation)this).PopToRootAsync(true);
}
Task INavigation.PopToRootAsync(bool animated)
{
throw new InvalidOperationException("PopToRootAsync is not supported globally on GTK, 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 GTK, please use a NavigationPage.");
}
Task INavigation.PushModalAsync(Page modal)
{
return ((INavigation)this).PushModalAsync(modal, true);
}
Task INavigation.PushModalAsync(Page modal, bool animated)
{
_modals.Add(modal);
#pragma warning disable CS0618 // Type or member is obsolete
// The Platform property is no longer necessary, but we have to set it because some third-party
// library might still be retrieving it and using it
modal.Platform = this;
#pragma warning restore CS0618 // Type or member is obsolete
modal.DescendantRemoved += HandleChildRemoved;
var modalRenderer = GetRenderer(modal);
if (modalRenderer == null)
{
modalRenderer = CreateRenderer(modal);
SetRenderer(modal, modalRenderer);
}
var pageControl = PlatformRenderer.Child as IPageControl;
Device.BeginInvokeOnMainThread(() =>
{
if (pageControl != null)
{
var page = pageControl.Control;
if (page != null)
{
page.PushModal(modalRenderer.Container);
}
}
});
return Task.FromResult<object>(null);
}
void INavigation.RemovePage(Page page)
{
throw new InvalidOperationException("RemovePage is not supported globally on GTK, please use a NavigationPage.");
}
internal class DefaultRenderer : VisualElementRenderer<VisualElement, Widget>
{
}
#region Obsolete
SizeRequest IPlatform.GetNativeSize(VisualElement view, double widthConstraint, double heightConstraint)
{
return GetNativeSize(view, widthConstraint, heightConstraint);
}
#endregion
}
}