TopLevelImpl.cs 5.1 KB

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