2017-11-17 20:28:22 +00:00
|
|
|
|
using System;
|
2017-11-09 07:57:04 +00:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Ooui.Forms.Renderers;
|
2017-11-09 06:21:05 +00:00
|
|
|
|
using Xamarin.Forms;
|
|
|
|
|
using Xamarin.Forms.Internals;
|
|
|
|
|
|
2017-11-17 20:28:22 +00:00
|
|
|
|
namespace Ooui.Forms
|
2017-11-09 06:21:05 +00:00
|
|
|
|
{
|
2017-11-09 21:03:56 +00:00
|
|
|
|
public class Platform : BindableObject, IPlatform, INavigation, IDisposable
|
|
|
|
|
{
|
|
|
|
|
bool _disposed;
|
|
|
|
|
|
|
|
|
|
readonly PlatformRenderer _renderer;
|
|
|
|
|
|
|
|
|
|
public Ooui.Element Element => _renderer;
|
|
|
|
|
|
|
|
|
|
public Page Page { get; private set; }
|
|
|
|
|
|
|
|
|
|
IReadOnlyList<Page> INavigation.ModalStack => throw new NotImplementedException ();
|
|
|
|
|
|
|
|
|
|
IReadOnlyList<Page> INavigation.NavigationStack => throw new NotImplementedException ();
|
|
|
|
|
|
|
|
|
|
public 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;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
public Platform ()
|
|
|
|
|
{
|
|
|
|
|
_renderer = new PlatformRenderer (this);
|
2017-11-17 20:28:22 +00:00
|
|
|
|
|
2017-11-19 20:19:44 +00:00
|
|
|
|
_renderer.Style.PropertyChanged += HandleRendererStyle_PropertyChanged;
|
|
|
|
|
|
|
|
|
|
MessagingCenter.Subscribe (this, Page.AlertSignalName, (Page sender, AlertArguments arguments) => {
|
|
|
|
|
var alert = new DisplayAlert (arguments);
|
2017-11-17 22:25:55 +00:00
|
|
|
|
alert.Clicked += CloseAlert;
|
2017-11-17 20:31:56 +00:00
|
|
|
|
|
2017-11-19 20:19:44 +00:00
|
|
|
|
_renderer.AppendChild (alert.Element);
|
2017-11-17 20:31:56 +00:00
|
|
|
|
|
2017-11-19 20:19:44 +00:00
|
|
|
|
void CloseAlert (object s, EventArgs e)
|
2017-11-17 20:31:56 +00:00
|
|
|
|
{
|
2017-11-19 20:19:44 +00:00
|
|
|
|
_renderer.RemoveChild (alert.Element);
|
2017-11-17 20:31:56 +00:00
|
|
|
|
}
|
2017-11-17 20:28:22 +00:00
|
|
|
|
});
|
2018-11-07 17:20:39 +00:00
|
|
|
|
|
|
|
|
|
MessagingCenter.Subscribe(this, Page.ActionSheetSignalName, (Page sender, ActionSheetArguments arguments) =>
|
|
|
|
|
{
|
|
|
|
|
var sheet = new ActionSheet(arguments);
|
|
|
|
|
sheet.Clicked += CloseSheet;
|
|
|
|
|
|
|
|
|
|
_renderer.AppendChild(sheet.Element);
|
|
|
|
|
|
|
|
|
|
void CloseSheet (object s, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
_renderer.RemoveChild(sheet.Element);
|
|
|
|
|
}
|
|
|
|
|
});
|
2017-11-09 21:03:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
2017-12-10 04:39:10 +00:00
|
|
|
|
|
|
|
|
|
DisposeModelAndChildrenRenderers (Page);
|
|
|
|
|
//foreach (var modal in _modals)
|
|
|
|
|
//DisposeModelAndChildrenRenderers (modal);
|
2017-11-09 21:03:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static IVisualElementRenderer CreateRenderer (VisualElement element)
|
|
|
|
|
{
|
|
|
|
|
var renderer = Registrar.Registered.GetHandler<IVisualElementRenderer> (element.GetType ()) ?? new DefaultRenderer ();
|
|
|
|
|
renderer.SetElement (element);
|
|
|
|
|
return renderer;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static IVisualElementRenderer GetRenderer (VisualElement bindable)
|
|
|
|
|
{
|
|
|
|
|
return (IVisualElementRenderer)bindable.GetValue (RendererProperty);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void SetRenderer (VisualElement bindable, IVisualElementRenderer value)
|
|
|
|
|
{
|
|
|
|
|
bindable.SetValue (RendererProperty, value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void OnBindingContextChanged ()
|
|
|
|
|
{
|
|
|
|
|
SetInheritedBindingContext (Page, BindingContext);
|
|
|
|
|
|
|
|
|
|
base.OnBindingContextChanged ();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public SizeRequest GetNativeSize (VisualElement view, double widthConstraint, double heightConstraint)
|
|
|
|
|
{
|
2017-11-17 20:28:22 +00:00
|
|
|
|
var renderView = GetRenderer (view);
|
|
|
|
|
if (renderView == null || renderView.NativeView == null)
|
|
|
|
|
return new SizeRequest (Size.Zero);
|
|
|
|
|
|
2017-11-10 01:23:41 +00:00
|
|
|
|
return renderView.GetDesiredSize (widthConstraint, heightConstraint);
|
2017-11-09 21:03:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SetPage (Page newRoot)
|
|
|
|
|
{
|
|
|
|
|
if (newRoot == null)
|
|
|
|
|
return;
|
|
|
|
|
if (Page != null)
|
|
|
|
|
throw new NotImplementedException ();
|
|
|
|
|
Page = newRoot;
|
|
|
|
|
|
|
|
|
|
Page.Platform = this;
|
|
|
|
|
AddChild (Page);
|
|
|
|
|
|
|
|
|
|
Page.DescendantRemoved += HandleChildRemoved;
|
|
|
|
|
|
|
|
|
|
Application.Current.NavigationProxy.Inner = this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HandleChildRemoved (object sender, ElementEventArgs e)
|
|
|
|
|
{
|
2017-12-10 04:39:10 +00:00
|
|
|
|
var view = e.Element;
|
|
|
|
|
DisposeModelAndChildrenRenderers (view);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DisposeModelAndChildrenRenderers (Xamarin.Forms.Element view)
|
|
|
|
|
{
|
|
|
|
|
IVisualElementRenderer renderer;
|
|
|
|
|
foreach (VisualElement child in view.Descendants ()) {
|
|
|
|
|
renderer = GetRenderer (child);
|
|
|
|
|
child.ClearValue (RendererProperty);
|
|
|
|
|
|
|
|
|
|
if (renderer != null) {
|
|
|
|
|
//renderer.NativeView.RemoveFromSuperview ();
|
|
|
|
|
renderer.Dispose ();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
renderer = GetRenderer ((VisualElement)view);
|
|
|
|
|
if (renderer != null) {
|
|
|
|
|
//renderer.NativeView.RemoveFromSuperview ();
|
|
|
|
|
renderer.Dispose ();
|
|
|
|
|
}
|
|
|
|
|
view.ClearValue (RendererProperty);
|
2017-11-09 21:03:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void AddChild (VisualElement view)
|
|
|
|
|
{
|
|
|
|
|
if (!Application.IsApplicationOrNull (view.RealParent))
|
2018-03-10 05:28:39 +00:00
|
|
|
|
System.Diagnostics.Debug.WriteLine ("Tried to add parented view to canvas directly");
|
2017-11-09 21:03:56 +00:00
|
|
|
|
|
|
|
|
|
if (GetRenderer (view) == null) {
|
|
|
|
|
var viewRenderer = CreateRenderer (view);
|
|
|
|
|
SetRenderer (view, viewRenderer);
|
|
|
|
|
|
|
|
|
|
_renderer.AppendChild (viewRenderer.NativeView);
|
|
|
|
|
viewRenderer.SetElementSize (new Size (640, 480));
|
|
|
|
|
}
|
|
|
|
|
else
|
2018-03-10 05:28:39 +00:00
|
|
|
|
System.Diagnostics.Debug.WriteLine ("Potential view double add");
|
2017-11-09 21:03:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-11-19 20:19:44 +00:00
|
|
|
|
void HandleRendererStyle_PropertyChanged (object sender, System.ComponentModel.PropertyChangedEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
var pageRenderer = GetRenderer (Page);
|
|
|
|
|
pageRenderer?.SetElementSize (Ooui.Forms.Extensions.ElementExtensions.GetSizeRequest (_renderer, double.PositiveInfinity, double.PositiveInfinity).Request);
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-09 21:03:56 +00:00
|
|
|
|
void INavigation.InsertPageBefore (Page page, Page before)
|
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException ();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Task<Page> INavigation.PopAsync ()
|
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException ();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Task<Page> INavigation.PopAsync (bool animated)
|
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException ();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Task<Page> INavigation.PopModalAsync ()
|
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException ();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Task<Page> INavigation.PopModalAsync (bool animated)
|
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException ();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Task INavigation.PopToRootAsync ()
|
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException ();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Task INavigation.PopToRootAsync (bool animated)
|
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException ();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Task INavigation.PushAsync (Page page)
|
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException ();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Task INavigation.PushAsync (Page page, bool animated)
|
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException ();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Task INavigation.PushModalAsync (Page page)
|
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException ();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Task INavigation.PushModalAsync (Page page, bool animated)
|
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException ();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void INavigation.RemovePage (Page page)
|
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException ();
|
|
|
|
|
}
|
2017-11-17 20:28:22 +00:00
|
|
|
|
}
|
|
|
|
|
}
|