TopLevelImpl.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. using System;
  2. using System.Collections.Generic;
  3. using Android.Content;
  4. using Android.Graphics;
  5. using Android.Views;
  6. using Avalonia.Android.Platform.Input;
  7. using Avalonia.Android.Platform.Specific;
  8. using Avalonia.Android.Platform.Specific.Helpers;
  9. using Avalonia.Controls.Platform.Surfaces;
  10. using Avalonia.Input;
  11. using Avalonia.Input.Raw;
  12. using Avalonia.Platform;
  13. using Avalonia.Rendering;
  14. namespace Avalonia.Android.Platform.SkiaPlatform
  15. {
  16. class TopLevelImpl : IAndroidView, ITopLevelImpl, IFramebufferPlatformSurface
  17. {
  18. private readonly AndroidKeyboardEventsHelper<TopLevelImpl> _keyboardHelper;
  19. private readonly AndroidTouchEventsHelper<TopLevelImpl> _touchHelper;
  20. private ViewImpl _view;
  21. public TopLevelImpl(Context context, bool placeOnTop = false)
  22. {
  23. _view = new ViewImpl(context, this, placeOnTop);
  24. _keyboardHelper = new AndroidKeyboardEventsHelper<TopLevelImpl>(this);
  25. _touchHelper = new AndroidTouchEventsHelper<TopLevelImpl>(this, () => InputRoot,
  26. p => GetAvaloniaPointFromEvent(p));
  27. MaxClientSize = new Size(_view.Resources.DisplayMetrics.WidthPixels,
  28. _view.Resources.DisplayMetrics.HeightPixels);
  29. }
  30. private bool _handleEvents;
  31. public bool HandleEvents
  32. {
  33. get { return _handleEvents; }
  34. set
  35. {
  36. _handleEvents = value;
  37. _keyboardHelper.HandleEvents = _handleEvents;
  38. }
  39. }
  40. public virtual Point GetAvaloniaPointFromEvent(MotionEvent e) => new Point(e.GetX(), e.GetY());
  41. public IInputRoot InputRoot { get; private set; }
  42. public virtual Size ClientSize
  43. {
  44. get
  45. {
  46. if (_view == null)
  47. return new Size(0, 0);
  48. return new Size(_view.Width, _view.Height);
  49. }
  50. set
  51. {
  52. }
  53. }
  54. public IMouseDevice MouseDevice => AndroidMouseDevice.Instance;
  55. public Action Closed { get; set; }
  56. public Action<RawInputEventArgs> Input { get; set; }
  57. public Size MaxClientSize { get; protected set; }
  58. public Action<Rect> Paint { get; set; }
  59. public Action<Size> Resized { get; set; }
  60. public Action<double> ScalingChanged { get; set; }
  61. public View View => _view;
  62. public IPlatformHandle Handle => _view;
  63. public IEnumerable<object> Surfaces => new object[] {this};
  64. public IRenderer CreateRenderer(IRenderRoot root)
  65. {
  66. return new ImmediateRenderer(root);
  67. }
  68. public virtual void Hide()
  69. {
  70. _view.Visibility = ViewStates.Invisible;
  71. }
  72. public void Invalidate(Rect rect)
  73. {
  74. if (_view.Holder?.Surface?.IsValid == true) _view.Invalidate();
  75. }
  76. public Point PointToClient(Point point)
  77. {
  78. return point;
  79. }
  80. public Point PointToScreen(Point point)
  81. {
  82. return point;
  83. }
  84. public void SetCursor(IPlatformHandle cursor)
  85. {
  86. //still not implemented
  87. }
  88. public void SetInputRoot(IInputRoot inputRoot)
  89. {
  90. InputRoot = inputRoot;
  91. }
  92. public virtual void Show()
  93. {
  94. _view.Visibility = ViewStates.Visible;
  95. }
  96. public double Scaling => 1;
  97. void Draw()
  98. {
  99. Paint?.Invoke(new Rect(new Point(0, 0), ClientSize));
  100. }
  101. public virtual void Dispose()
  102. {
  103. _view.Dispose();
  104. _view = null;
  105. }
  106. protected virtual void OnResized(Size size)
  107. {
  108. Resized?.Invoke(size);
  109. }
  110. class ViewImpl : InvalidationAwareSurfaceView, ISurfaceHolderCallback
  111. {
  112. private readonly TopLevelImpl _tl;
  113. private Size _oldSize;
  114. public ViewImpl(Context context, TopLevelImpl tl, bool placeOnTop) : base(context)
  115. {
  116. _tl = tl;
  117. if (placeOnTop)
  118. SetZOrderOnTop(true);
  119. }
  120. protected override void Draw()
  121. {
  122. _tl.Draw();
  123. }
  124. public override bool DispatchTouchEvent(MotionEvent e)
  125. {
  126. bool callBase;
  127. bool? result = _tl._touchHelper.DispatchTouchEvent(e, out callBase);
  128. bool baseResult = callBase ? base.DispatchTouchEvent(e) : false;
  129. return result != null ? result.Value : baseResult;
  130. }
  131. public override bool DispatchKeyEvent(KeyEvent e)
  132. {
  133. bool callBase;
  134. bool? res = _tl._keyboardHelper.DispatchKeyEvent(e, out callBase);
  135. bool baseResult = callBase ? base.DispatchKeyEvent(e) : false;
  136. return res != null ? res.Value : baseResult;
  137. }
  138. void ISurfaceHolderCallback.SurfaceChanged(ISurfaceHolder holder, Format format, int width, int height)
  139. {
  140. var newSize = new Size(width, height);
  141. if (newSize != _oldSize)
  142. {
  143. _oldSize = newSize;
  144. _tl.OnResized(newSize);
  145. }
  146. base.SurfaceChanged(holder, format, width, height);
  147. }
  148. }
  149. ILockedFramebuffer IFramebufferPlatformSurface.Lock()=>new AndroidFramebuffer(_view.Holder.Surface);
  150. }
  151. }