TopLevelImpl.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Reactive.Disposables;
  6. using System.Text;
  7. using CoreAnimation;
  8. using CoreGraphics;
  9. using Foundation;
  10. using Avalonia.Controls.Platform;
  11. using Avalonia.Input;
  12. using Avalonia.Input.Raw;
  13. using Avalonia.Media;
  14. using Avalonia.Platform;
  15. using Avalonia.Skia.iOS;
  16. using UIKit;
  17. using Avalonia.iOS.Specific;
  18. using ObjCRuntime;
  19. using Avalonia.Controls;
  20. using Avalonia.Controls.Platform.Surfaces;
  21. namespace Avalonia.iOS
  22. {
  23. [Adopts("UIKeyInput")]
  24. class TopLevelImpl : UIView, ITopLevelImpl, IFramebufferPlatformSurface
  25. {
  26. private IInputRoot _inputRoot;
  27. private readonly KeyboardEventsHelper<TopLevelImpl> _keyboardHelper;
  28. private Point _position;
  29. public TopLevelImpl()
  30. {
  31. _keyboardHelper = new KeyboardEventsHelper<TopLevelImpl>(this);
  32. AutoresizingMask = UIViewAutoresizing.All;
  33. _keyboardHelper.ActivateAutoShowKeybord();
  34. }
  35. [Export("hasText")]
  36. public bool HasText => _keyboardHelper.HasText();
  37. [Export("insertText:")]
  38. public void InsertText(string text) => _keyboardHelper.InsertText(text);
  39. [Export("deleteBackward")]
  40. public void DeleteBackward() => _keyboardHelper.DeleteBackward();
  41. public override bool CanBecomeFirstResponder => _keyboardHelper.CanBecomeFirstResponder();
  42. public Action Closed { get; set; }
  43. public Action<RawInputEventArgs> Input { get; set; }
  44. public Action<Rect> Paint { get; set; }
  45. public Action<Size> Resized { get; set; }
  46. public Action<double> ScalingChanged { get; set; }
  47. public IPlatformHandle Handle => null;
  48. public double Scaling => UIScreen.MainScreen.Scale;
  49. public override void LayoutSubviews() => Resized?.Invoke(ClientSize);
  50. public Size ClientSize => Bounds.Size.ToAvalonia();
  51. public IMouseDevice MouseDevice => iOSPlatform.MouseDevice;
  52. public override void Draw(CGRect rect)
  53. {
  54. Paint?.Invoke(new Rect(rect.X, rect.Y, rect.Width, rect.Height));
  55. }
  56. public void Invalidate(Rect rect) => SetNeedsDisplay();
  57. public void SetInputRoot(IInputRoot inputRoot) => _inputRoot = inputRoot;
  58. public Point PointToClient(Point point) => point;
  59. public Point PointToScreen(Point point) => point;
  60. public void SetCursor(IPlatformHandle cursor)
  61. {
  62. //Not supported
  63. }
  64. public IEnumerable<object> Surfaces => new object[] { this };
  65. public override void TouchesEnded(NSSet touches, UIEvent evt)
  66. {
  67. var touch = touches.AnyObject as UITouch;
  68. if (touch != null)
  69. {
  70. var location = touch.LocationInView(this).ToAvalonia();
  71. Input?.Invoke(new RawMouseEventArgs(
  72. iOSPlatform.MouseDevice,
  73. (uint)touch.Timestamp,
  74. _inputRoot,
  75. RawMouseEventType.LeftButtonUp,
  76. location,
  77. InputModifiers.None));
  78. }
  79. }
  80. Point _touchLastPoint;
  81. public override void TouchesBegan(NSSet touches, UIEvent evt)
  82. {
  83. var touch = touches.AnyObject as UITouch;
  84. if (touch != null)
  85. {
  86. var location = touch.LocationInView(this).ToAvalonia();
  87. _touchLastPoint = location;
  88. Input?.Invoke(new RawMouseEventArgs(iOSPlatform.MouseDevice, (uint)touch.Timestamp, _inputRoot,
  89. RawMouseEventType.Move, location, InputModifiers.None));
  90. Input?.Invoke(new RawMouseEventArgs(iOSPlatform.MouseDevice, (uint)touch.Timestamp, _inputRoot,
  91. RawMouseEventType.LeftButtonDown, location, InputModifiers.None));
  92. }
  93. }
  94. public override void TouchesMoved(NSSet touches, UIEvent evt)
  95. {
  96. var touch = touches.AnyObject as UITouch;
  97. if (touch != null)
  98. {
  99. var location = touch.LocationInView(this).ToAvalonia();
  100. if (iOSPlatform.MouseDevice.Captured != null)
  101. Input?.Invoke(new RawMouseEventArgs(iOSPlatform.MouseDevice, (uint)touch.Timestamp, _inputRoot,
  102. RawMouseEventType.Move, location, InputModifiers.LeftMouseButton));
  103. else
  104. {
  105. //magic number based on test - correction of 0.02 is working perfect
  106. double correction = 0.02;
  107. Input?.Invoke(new RawMouseWheelEventArgs(iOSPlatform.MouseDevice, (uint)touch.Timestamp,
  108. _inputRoot, location, (location - _touchLastPoint) * correction, InputModifiers.LeftMouseButton));
  109. }
  110. _touchLastPoint = location;
  111. }
  112. }
  113. public ILockedFramebuffer Lock() => new EmulatedFramebuffer(this);
  114. }
  115. }