TopLevelImpl.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 UIKit;
  16. using Avalonia.iOS.Specific;
  17. using ObjCRuntime;
  18. using Avalonia.Controls;
  19. using Avalonia.Controls.Platform.Surfaces;
  20. using Avalonia.Rendering;
  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 IRenderer CreateRenderer(IRenderRoot root)
  53. {
  54. return new ImmediateRenderer(root);
  55. }
  56. public override void Draw(CGRect rect)
  57. {
  58. Paint?.Invoke(new Rect(rect.X, rect.Y, rect.Width, rect.Height));
  59. }
  60. public void Invalidate(Rect rect) => SetNeedsDisplay();
  61. public void SetInputRoot(IInputRoot inputRoot) => _inputRoot = inputRoot;
  62. public Point PointToClient(Point point) => point;
  63. public Point PointToScreen(Point point) => point;
  64. public void SetCursor(IPlatformHandle cursor)
  65. {
  66. //Not supported
  67. }
  68. public IEnumerable<object> Surfaces => new object[] { this };
  69. public override void TouchesEnded(NSSet touches, UIEvent evt)
  70. {
  71. var touch = touches.AnyObject as UITouch;
  72. if (touch != null)
  73. {
  74. var location = touch.LocationInView(this).ToAvalonia();
  75. Input?.Invoke(new RawMouseEventArgs(
  76. iOSPlatform.MouseDevice,
  77. (uint)touch.Timestamp,
  78. _inputRoot,
  79. RawMouseEventType.LeftButtonUp,
  80. location,
  81. InputModifiers.None));
  82. }
  83. }
  84. Point _touchLastPoint;
  85. public override void TouchesBegan(NSSet touches, UIEvent evt)
  86. {
  87. var touch = touches.AnyObject as UITouch;
  88. if (touch != null)
  89. {
  90. var location = touch.LocationInView(this).ToAvalonia();
  91. _touchLastPoint = location;
  92. Input?.Invoke(new RawMouseEventArgs(iOSPlatform.MouseDevice, (uint)touch.Timestamp, _inputRoot,
  93. RawMouseEventType.Move, location, InputModifiers.None));
  94. Input?.Invoke(new RawMouseEventArgs(iOSPlatform.MouseDevice, (uint)touch.Timestamp, _inputRoot,
  95. RawMouseEventType.LeftButtonDown, location, InputModifiers.None));
  96. }
  97. }
  98. public override void TouchesMoved(NSSet touches, UIEvent evt)
  99. {
  100. var touch = touches.AnyObject as UITouch;
  101. if (touch != null)
  102. {
  103. var location = touch.LocationInView(this).ToAvalonia();
  104. if (iOSPlatform.MouseDevice.Captured != null)
  105. Input?.Invoke(new RawMouseEventArgs(iOSPlatform.MouseDevice, (uint)touch.Timestamp, _inputRoot,
  106. RawMouseEventType.Move, location, InputModifiers.LeftMouseButton));
  107. else
  108. {
  109. //magic number based on test - correction of 0.02 is working perfect
  110. double correction = 0.02;
  111. Input?.Invoke(new RawMouseWheelEventArgs(iOSPlatform.MouseDevice, (uint)touch.Timestamp,
  112. _inputRoot, location, (location - _touchLastPoint) * correction, InputModifiers.LeftMouseButton));
  113. }
  114. _touchLastPoint = location;
  115. }
  116. }
  117. public ILockedFramebuffer Lock() => new EmulatedFramebuffer(this);
  118. }
  119. }