AvaloniaView.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. #nullable enable
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Runtime.Versioning;
  5. using Avalonia.Controls;
  6. using Avalonia.Controls.Embedding;
  7. using Avalonia.Controls.Platform;
  8. using Avalonia.Controls.Primitives;
  9. using Avalonia.Data;
  10. using Avalonia.Input;
  11. using Avalonia.Input.Platform;
  12. using Avalonia.Input.Raw;
  13. using Avalonia.Input.TextInput;
  14. using Avalonia.iOS.Storage;
  15. using Avalonia.Platform;
  16. using Avalonia.Platform.Storage;
  17. using Avalonia.Rendering.Composition;
  18. using CoreAnimation;
  19. using Foundation;
  20. using ObjCRuntime;
  21. using OpenGLES;
  22. using UIKit;
  23. using IInsetsManager = Avalonia.Controls.Platform.IInsetsManager;
  24. namespace Avalonia.iOS
  25. {
  26. public partial class AvaloniaView : UIView, ITextInputMethodImpl
  27. {
  28. internal IInputRoot InputRoot
  29. => _inputRoot ?? throw new InvalidOperationException($"{nameof(IWindowImpl.SetInputRoot)} must have been called");
  30. private readonly TopLevelImpl _topLevelImpl;
  31. private readonly EmbeddableControlRoot _topLevel;
  32. private readonly TouchHandler _touches;
  33. private TextInputMethodClient? _client;
  34. private IAvaloniaViewController? _controller;
  35. private IInputRoot? _inputRoot;
  36. public AvaloniaView()
  37. {
  38. _topLevelImpl = new TopLevelImpl(this);
  39. _touches = new TouchHandler(this, _topLevelImpl);
  40. _topLevel = new EmbeddableControlRoot(_topLevelImpl);
  41. _topLevel.Prepare();
  42. _topLevel.StartRendering();
  43. InitEagl();
  44. MultipleTouchEnabled = true;
  45. }
  46. [ObsoletedOSPlatform("ios12.0", "Use 'Metal' instead.")]
  47. [SupportedOSPlatform("ios")]
  48. [UnsupportedOSPlatform("maccatalyst")]
  49. private void InitEagl()
  50. {
  51. var l = (CAEAGLLayer)Layer;
  52. l.ContentsScale = UIScreen.MainScreen.Scale;
  53. l.Opaque = true;
  54. l.DrawableProperties = new NSDictionary(
  55. EAGLDrawableProperty.RetainedBacking, false,
  56. EAGLDrawableProperty.ColorFormat, EAGLColorFormat.RGBA8
  57. );
  58. _topLevelImpl.Surfaces = new[] { new EaglLayerSurface(l) };
  59. }
  60. /// <inheritdoc />
  61. public override bool CanBecomeFirstResponder => true;
  62. /// <inheritdoc />
  63. public override bool CanResignFirstResponder => true;
  64. /// <inheritdoc />
  65. public override void TraitCollectionDidChange(UITraitCollection? previousTraitCollection)
  66. {
  67. base.TraitCollectionDidChange(previousTraitCollection);
  68. var settings = AvaloniaLocator.Current.GetRequiredService<IPlatformSettings>() as PlatformSettings;
  69. settings?.TraitCollectionDidChange();
  70. }
  71. /// <inheritdoc />
  72. public override void TintColorDidChange()
  73. {
  74. base.TintColorDidChange();
  75. var settings = AvaloniaLocator.Current.GetRequiredService<IPlatformSettings>() as PlatformSettings;
  76. settings?.TraitCollectionDidChange();
  77. }
  78. public void InitWithController<TController>(TController controller)
  79. where TController : UIViewController, IAvaloniaViewController
  80. {
  81. _controller = controller;
  82. _topLevelImpl._insetsManager.InitWithController(controller);
  83. }
  84. internal class TopLevelImpl : ITopLevelImpl
  85. {
  86. private readonly AvaloniaView _view;
  87. private readonly INativeControlHostImpl _nativeControlHost;
  88. private readonly IStorageProvider _storageProvider;
  89. internal readonly InsetsManager _insetsManager;
  90. private readonly ClipboardImpl _clipboard;
  91. private IDisposable? _paddingInsets;
  92. public AvaloniaView View => _view;
  93. public TopLevelImpl(AvaloniaView view)
  94. {
  95. _view = view;
  96. _nativeControlHost = new NativeControlHostImpl(view);
  97. _storageProvider = new IOSStorageProvider(view);
  98. _insetsManager = new InsetsManager(view);
  99. _insetsManager.DisplayEdgeToEdgeChanged += (_, edgeToEdge) =>
  100. {
  101. // iOS doesn't add any paddings/margins to the application by itself.
  102. // Application is fully responsible for safe area paddings.
  103. // So, unlikely to android, we need to "fake" safe area insets when edge to edge is disabled.
  104. _paddingInsets?.Dispose();
  105. if (!edgeToEdge && view._controller is { } controller)
  106. {
  107. _paddingInsets = view._topLevel.SetValue(
  108. TemplatedControl.PaddingProperty,
  109. controller.SafeAreaPadding,
  110. BindingPriority.Style); // lower priority, so it can be redefined by user
  111. }
  112. };
  113. _clipboard = new ClipboardImpl();
  114. }
  115. public void Dispose()
  116. {
  117. // No-op
  118. }
  119. public Compositor Compositor => Platform.Compositor;
  120. public void Invalidate(Rect rect)
  121. {
  122. // No-op
  123. }
  124. public void SetInputRoot(IInputRoot inputRoot)
  125. {
  126. _view._inputRoot = inputRoot;
  127. }
  128. public Point PointToClient(PixelPoint point) => new Point(point.X, point.Y);
  129. public PixelPoint PointToScreen(Point point) => new PixelPoint((int)point.X, (int)point.Y);
  130. public void SetCursor(ICursorImpl? cursor)
  131. {
  132. // no-op
  133. }
  134. public IPopupImpl? CreatePopup()
  135. {
  136. // In-window popups
  137. return null;
  138. }
  139. public void SetTransparencyLevelHint(IReadOnlyList<WindowTransparencyLevel> transparencyLevel)
  140. {
  141. // No-op
  142. }
  143. public Size ClientSize => new Size(_view.Bounds.Width, _view.Bounds.Height);
  144. public Size? FrameSize => null;
  145. public double RenderScaling => _view.ContentScaleFactor;
  146. public IEnumerable<object> Surfaces { get; set; } = Array.Empty<object>();
  147. public Action<RawInputEventArgs>? Input { get; set; }
  148. public Action<Rect>? Paint { get; set; }
  149. public Action<Size, WindowResizeReason>? Resized { get; set; }
  150. public Action<double>? ScalingChanged { get; set; }
  151. public Action<WindowTransparencyLevel>? TransparencyLevelChanged { get; set; }
  152. public Action? Closed { get; set; }
  153. public Action? LostFocus { get; set; }
  154. public WindowTransparencyLevel TransparencyLevel => WindowTransparencyLevel.None;
  155. public void SetFrameThemeVariant(PlatformThemeVariant themeVariant)
  156. {
  157. // TODO adjust status bar depending on full screen mode.
  158. if (OperatingSystem.IsIOSVersionAtLeast(13) && _view._controller is not null)
  159. {
  160. _view._controller.PreferredStatusBarStyle = themeVariant switch
  161. {
  162. PlatformThemeVariant.Light => UIStatusBarStyle.DarkContent,
  163. PlatformThemeVariant.Dark => UIStatusBarStyle.LightContent,
  164. _ => UIStatusBarStyle.Default
  165. };
  166. }
  167. }
  168. public AcrylicPlatformCompensationLevels AcrylicCompensationLevels { get; } =
  169. new AcrylicPlatformCompensationLevels();
  170. public object? TryGetFeature(Type featureType)
  171. {
  172. if (featureType == typeof(IStorageProvider))
  173. {
  174. return _storageProvider;
  175. }
  176. if (featureType == typeof(ITextInputMethodImpl))
  177. {
  178. return _view;
  179. }
  180. if (featureType == typeof(INativeControlHostImpl))
  181. {
  182. return _nativeControlHost;
  183. }
  184. if (featureType == typeof(IInsetsManager))
  185. {
  186. return _insetsManager;
  187. }
  188. if (featureType == typeof(IClipboard))
  189. {
  190. return _clipboard;
  191. }
  192. if (featureType == typeof(IInputPane))
  193. {
  194. return UIKitInputPane.Instance;
  195. }
  196. return null;
  197. }
  198. }
  199. [Export("layerClass")]
  200. public static Class LayerClass()
  201. {
  202. return new Class(typeof(CAEAGLLayer));
  203. }
  204. public override void TouchesBegan(NSSet touches, UIEvent? evt) => _touches.Handle(touches, evt);
  205. public override void TouchesMoved(NSSet touches, UIEvent? evt) => _touches.Handle(touches, evt);
  206. public override void TouchesEnded(NSSet touches, UIEvent? evt) => _touches.Handle(touches, evt);
  207. public override void TouchesCancelled(NSSet touches, UIEvent? evt) => _touches.Handle(touches, evt);
  208. public override void LayoutSubviews()
  209. {
  210. _topLevelImpl.Resized?.Invoke(_topLevelImpl.ClientSize, WindowResizeReason.Layout);
  211. base.LayoutSubviews();
  212. }
  213. public Control? Content
  214. {
  215. get => (Control?)_topLevel.Content;
  216. set => _topLevel.Content = value;
  217. }
  218. }
  219. }