NativeControlHostImpl.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. using System;
  2. using Avalonia.Controls.Platform;
  3. using Avalonia.MicroCom;
  4. using Avalonia.Native.Interop;
  5. using Avalonia.Platform;
  6. using Avalonia.VisualTree;
  7. namespace Avalonia.Native
  8. {
  9. class NativeControlHostImpl : IDisposable, INativeControlHostImpl
  10. {
  11. private IAvnNativeControlHost _host;
  12. public NativeControlHostImpl(IAvnNativeControlHost host)
  13. {
  14. _host = host;
  15. }
  16. public void Dispose()
  17. {
  18. _host?.Dispose();
  19. _host = null;
  20. }
  21. class DestroyableNSView : INativeControlHostDestroyableControlHandle
  22. {
  23. private IAvnNativeControlHost _impl;
  24. private IntPtr _nsView;
  25. public DestroyableNSView(IAvnNativeControlHost impl)
  26. {
  27. _impl = MicroComRuntime.CloneReference(impl);
  28. _nsView = _impl.CreateDefaultChild(IntPtr.Zero);
  29. }
  30. public IntPtr Handle => _nsView;
  31. public string HandleDescriptor => "NSView";
  32. public void Destroy()
  33. {
  34. if (_impl != null)
  35. {
  36. _impl.DestroyDefaultChild(_nsView);
  37. _impl.Dispose();
  38. _impl = null;
  39. _nsView = IntPtr.Zero;
  40. }
  41. }
  42. }
  43. public INativeControlHostDestroyableControlHandle CreateDefaultChild(IPlatformHandle parent)
  44. => new DestroyableNSView(_host);
  45. public INativeControlHostControlTopLevelAttachment CreateNewAttachment(
  46. Func<IPlatformHandle, IPlatformHandle> create)
  47. {
  48. var a = new Attachment(_host.CreateAttachment());
  49. try
  50. {
  51. var child = create(a.GetParentHandle());
  52. a.InitWithChild(child);
  53. a.AttachedTo = this;
  54. return a;
  55. }
  56. catch
  57. {
  58. a.Dispose();
  59. throw;
  60. }
  61. }
  62. public INativeControlHostControlTopLevelAttachment CreateNewAttachment(IPlatformHandle handle)
  63. {
  64. var a = new Attachment(_host.CreateAttachment());
  65. a.InitWithChild(handle);
  66. a.AttachedTo = this;
  67. return a;
  68. }
  69. public bool IsCompatibleWith(IPlatformHandle handle) => handle.HandleDescriptor == "NSView";
  70. class Attachment : INativeControlHostControlTopLevelAttachment
  71. {
  72. private IAvnNativeControlHostTopLevelAttachment _native;
  73. private NativeControlHostImpl _attachedTo;
  74. public IPlatformHandle GetParentHandle() => new PlatformHandle(_native.ParentHandle, "NSView");
  75. public Attachment(IAvnNativeControlHostTopLevelAttachment native)
  76. {
  77. _native = native;
  78. }
  79. public void Dispose()
  80. {
  81. if (_native != null)
  82. {
  83. _native.ReleaseChild();
  84. _native.Dispose();
  85. _native = null;
  86. }
  87. }
  88. public INativeControlHostImpl AttachedTo
  89. {
  90. get => _attachedTo;
  91. set
  92. {
  93. var host = (NativeControlHostImpl)value;
  94. if(host == null)
  95. _native.AttachTo(null);
  96. else
  97. _native.AttachTo(host._host);
  98. _attachedTo = host;
  99. }
  100. }
  101. public bool IsCompatibleWith(INativeControlHostImpl host) => host is NativeControlHostImpl;
  102. public void HideWithSize(Size size)
  103. {
  104. _native.HideWithSize(Math.Max(1, (float)size.Width), Math.Max(1, (float)size.Height));
  105. }
  106. public void ShowInBounds(Rect bounds)
  107. {
  108. if (_attachedTo == null)
  109. throw new InvalidOperationException("Native control isn't attached to a toplevel");
  110. bounds = new Rect(bounds.X, bounds.Y, Math.Max(1, bounds.Width),
  111. Math.Max(1, bounds.Height));
  112. _native.ShowInBounds((float) bounds.X, (float) bounds.Y, (float) bounds.Width, (float) bounds.Height);
  113. }
  114. public void InitWithChild(IPlatformHandle handle)
  115. => _native.InitializeWithChildHandle(handle.Handle);
  116. }
  117. }
  118. }