AvaloniaView.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. using System;
  2. using System.Collections.Generic;
  3. using Avalonia.Controls;
  4. using Avalonia.Controls.Embedding;
  5. using Avalonia.Controls.Platform;
  6. using Avalonia.Input;
  7. using Avalonia.Input.Raw;
  8. using Avalonia.Input.TextInput;
  9. using Avalonia.iOS.Storage;
  10. using Avalonia.Platform;
  11. using Avalonia.Platform.Storage;
  12. using Avalonia.Rendering;
  13. using Avalonia.Rendering.Composition;
  14. using CoreAnimation;
  15. using Foundation;
  16. using ObjCRuntime;
  17. using OpenGLES;
  18. using UIKit;
  19. namespace Avalonia.iOS
  20. {
  21. public partial class AvaloniaView : UIView
  22. {
  23. internal IInputRoot InputRoot { get; private set; }
  24. private TopLevelImpl _topLevelImpl;
  25. private EmbeddableControlRoot _topLevel;
  26. private TouchHandler _touches;
  27. public AvaloniaView()
  28. {
  29. _topLevelImpl = new TopLevelImpl(this);
  30. _touches = new TouchHandler(this, _topLevelImpl);
  31. _topLevel = new EmbeddableControlRoot(_topLevelImpl);
  32. _topLevel.Prepare();
  33. _topLevel.Renderer.Start();
  34. var l = (CAEAGLLayer) Layer;
  35. l.ContentsScale = UIScreen.MainScreen.Scale;
  36. l.Opaque = true;
  37. l.DrawableProperties = new NSDictionary(
  38. EAGLDrawableProperty.RetainedBacking, false,
  39. EAGLDrawableProperty.ColorFormat, EAGLColorFormat.RGBA8
  40. );
  41. _topLevelImpl.Surfaces = new[] {new EaglLayerSurface(l)};
  42. MultipleTouchEnabled = true;
  43. AddSubviews(new UIView[] { new UIKit.UIButton(UIButtonType.InfoDark) });
  44. }
  45. internal class TopLevelImpl : ITopLevelImplWithTextInputMethod, ITopLevelImplWithNativeControlHost, ITopLevelImplWithStorageProvider
  46. {
  47. private readonly AvaloniaView _view;
  48. public AvaloniaView View => _view;
  49. public TopLevelImpl(AvaloniaView view)
  50. {
  51. _view = view;
  52. NativeControlHost = new NativeControlHostImpl(_view);
  53. StorageProvider = new IOSStorageProvider(view);
  54. }
  55. public void Dispose()
  56. {
  57. // No-op
  58. }
  59. public IRenderer CreateRenderer(IRenderRoot root) => new CompositingRenderer(root, Platform.Compositor);
  60. public void Invalidate(Rect rect)
  61. {
  62. // No-op
  63. }
  64. public void SetInputRoot(IInputRoot inputRoot)
  65. {
  66. _view.InputRoot = inputRoot;
  67. }
  68. public Point PointToClient(PixelPoint point) => new Point(point.X, point.Y);
  69. public PixelPoint PointToScreen(Point point) => new PixelPoint((int) point.X, (int) point.Y);
  70. public void SetCursor(ICursorImpl _)
  71. {
  72. // no-op
  73. }
  74. public IPopupImpl CreatePopup()
  75. {
  76. // In-window popups
  77. return null;
  78. }
  79. public void SetTransparencyLevelHint(WindowTransparencyLevel transparencyLevel)
  80. {
  81. // No-op
  82. }
  83. public Size ClientSize => new Size(_view.Bounds.Width, _view.Bounds.Height);
  84. public Size? FrameSize => null;
  85. public double RenderScaling => _view.ContentScaleFactor;
  86. public IEnumerable<object> Surfaces { get; set; }
  87. public Action<RawInputEventArgs> Input { get; set; }
  88. public Action<Rect> Paint { get; set; }
  89. public Action<Size, PlatformResizeReason> Resized { get; set; }
  90. public Action<double> ScalingChanged { get; set; }
  91. public Action<WindowTransparencyLevel> TransparencyLevelChanged { get; set; }
  92. public Action Closed { get; set; }
  93. public Action LostFocus { get; set; }
  94. // legacy no-op
  95. public IMouseDevice MouseDevice { get; } = new MouseDevice();
  96. public WindowTransparencyLevel TransparencyLevel { get; }
  97. public AcrylicPlatformCompensationLevels AcrylicCompensationLevels { get; } =
  98. new AcrylicPlatformCompensationLevels();
  99. public ITextInputMethodImpl? TextInputMethod => _view;
  100. public INativeControlHostImpl NativeControlHost { get; }
  101. public IStorageProvider StorageProvider { get; }
  102. }
  103. [Export("layerClass")]
  104. public static Class LayerClass()
  105. {
  106. return new Class(typeof(CAEAGLLayer));
  107. }
  108. public override void TouchesBegan(NSSet touches, UIEvent evt) => _touches.Handle(touches, evt);
  109. public override void TouchesMoved(NSSet touches, UIEvent evt) => _touches.Handle(touches, evt);
  110. public override void TouchesEnded(NSSet touches, UIEvent evt) => _touches.Handle(touches, evt);
  111. public override void TouchesCancelled(NSSet touches, UIEvent evt) => _touches.Handle(touches, evt);
  112. public override void LayoutSubviews()
  113. {
  114. _topLevelImpl.Resized?.Invoke(_topLevelImpl.ClientSize, PlatformResizeReason.Layout);
  115. base.LayoutSubviews();
  116. }
  117. public Control Content
  118. {
  119. get => (Control)_topLevel.Content;
  120. set => _topLevel.Content = value;
  121. }
  122. }
  123. }