Browse Source

Respect actual AttachedTo in ios control host

pull/8110/head
Max Katz 4 years ago
parent
commit
7af062e8ba
  1. 27
      src/iOS/Avalonia.iOS/NativeControlHostImpl.cs

27
src/iOS/Avalonia.iOS/NativeControlHostImpl.cs

@ -21,19 +21,19 @@ namespace Avalonia.iOS
public INativeControlHostDestroyableControlHandle CreateDefaultChild(IPlatformHandle parent) public INativeControlHostDestroyableControlHandle CreateDefaultChild(IPlatformHandle parent)
{ {
return new UIViewControlHandle(new UIView()); return new UIViewControlHandle(new UIView(), true);
} }
public INativeControlHostControlTopLevelAttachment CreateNewAttachment(Func<IPlatformHandle, IPlatformHandle> create) public INativeControlHostControlTopLevelAttachment CreateNewAttachment(Func<IPlatformHandle, IPlatformHandle> create)
{ {
var holder = new UIViewControlHandle(_avaloniaView); var holder = new UIViewControlHandle(_avaloniaView, false);
NativeControlAttachment? attachment = null; NativeControlAttachment? attachment = null;
try try
{ {
var child = create(holder); var child = create(holder);
// It has to be assigned to the variable before property setter is called so we dispose it on exception // It has to be assigned to the variable before property setter is called so we dispose it on exception
#pragma warning disable IDE0017 // Simplify object initialization #pragma warning disable IDE0017 // Simplify object initialization
attachment = new NativeControlAttachment(holder, child); attachment = new NativeControlAttachment(child);
#pragma warning restore IDE0017 // Simplify object initialization #pragma warning restore IDE0017 // Simplify object initialization
attachment.AttachedTo = this; attachment.AttachedTo = this;
return attachment; return attachment;
@ -48,7 +48,7 @@ namespace Avalonia.iOS
public INativeControlHostControlTopLevelAttachment CreateNewAttachment(IPlatformHandle handle) public INativeControlHostControlTopLevelAttachment CreateNewAttachment(IPlatformHandle handle)
{ {
return new NativeControlAttachment(new UIViewControlHandle(_avaloniaView), handle) return new NativeControlAttachment(handle)
{ {
AttachedTo = this AttachedTo = this
}; };
@ -68,17 +68,14 @@ namespace Avalonia.iOS
{ {
// ReSharper disable once NotAccessedField.Local (keep GC reference) // ReSharper disable once NotAccessedField.Local (keep GC reference)
private IPlatformHandle? _child; private IPlatformHandle? _child;
private UIViewControlHandle? _holder;
private UIView? _view; private UIView? _view;
private NativeControlHostImpl? _attachedTo; private NativeControlHostImpl? _attachedTo;
public NativeControlAttachment(UIViewControlHandle holder, IPlatformHandle child) public NativeControlAttachment(IPlatformHandle child)
{ {
_holder = holder;
_child = child; _child = child;
_view = (child as UIViewControlHandle)?.View ?? new ViewHolder(child.Handle); _view = (child as UIViewControlHandle)?.View ?? new ViewHolder(child.Handle);
_holder.View.AddSubview(_view);
} }
[MemberNotNull(nameof(_view))] [MemberNotNull(nameof(_view))]
@ -91,7 +88,6 @@ namespace Avalonia.iOS
public void Dispose() public void Dispose()
{ {
_view?.RemoveFromSuperview(); _view?.RemoveFromSuperview();
_holder = null;
_child = null; _child = null;
_attachedTo = null; _attachedTo = null;
_view?.Dispose(); _view?.Dispose();
@ -112,7 +108,7 @@ namespace Avalonia.iOS
} }
else else
{ {
_holder!.View.AddSubview(_view); _attachedTo._avaloniaView.AddSubview(_view);
} }
} }
} }
@ -131,7 +127,6 @@ namespace Avalonia.iOS
if (_attachedTo == null) if (_attachedTo == null)
throw new InvalidOperationException("The control isn't currently attached to a toplevel"); throw new InvalidOperationException("The control isn't currently attached to a toplevel");
//bounds = _attachedTo._avaloniaView.ContentScaleFactor;
_view.Frame = new CGRect(bounds.X, bounds.Y, bounds.Width, bounds.Height); _view.Frame = new CGRect(bounds.X, bounds.Y, bounds.Width, bounds.Height);
_view.Hidden = false; _view.Hidden = false;
} }
@ -143,10 +138,12 @@ namespace Avalonia.iOS
internal const string UIViewDescriptor = "UIView"; internal const string UIViewDescriptor = "UIView";
private UIView? _view; private UIView? _view;
private bool _disposeView;
public UIViewControlHandle(UIView view) public UIViewControlHandle(UIView view, bool disposeView)
{ {
_view = view; _view = view;
_disposeView = disposeView;
} }
public UIView View => _view ?? throw new ObjectDisposedException(nameof(UIViewControlHandle)); public UIView View => _view ?? throw new ObjectDisposedException(nameof(UIViewControlHandle));
@ -172,7 +169,11 @@ namespace Avalonia.iOS
private void Dispose(bool disposing) private void Dispose(bool disposing)
{ {
_view?.Dispose(); if (_disposeView)
{
_view?.Dispose();
}
_view = null; _view = null;
if (disposing) if (disposing)
{ {

Loading…
Cancel
Save