AndroidInsetsManager.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. using System;
  2. using System.Collections.Generic;
  3. using Android.OS;
  4. using Android.Views;
  5. using AndroidX.Core.View;
  6. using Avalonia.Android.Platform.SkiaPlatform;
  7. using Avalonia.Controls.Platform;
  8. using static Avalonia.Controls.Platform.IInsetsManager;
  9. namespace Avalonia.Android.Platform
  10. {
  11. internal class AndroidInsetsManager : Java.Lang.Object, IInsetsManager, IOnApplyWindowInsetsListener, ViewTreeObserver.IOnGlobalLayoutListener
  12. {
  13. private readonly AvaloniaMainActivity _activity;
  14. private readonly TopLevelImpl _topLevel;
  15. private readonly InsetsAnimationCallback _callback;
  16. private bool _displayEdgeToEdge;
  17. private bool _usesLegacyLayouts;
  18. private bool? _systemUiVisibility;
  19. private SystemBarTheme? _statusBarTheme;
  20. private bool? _isDefaultSystemBarLightTheme;
  21. public event EventHandler<SafeAreaChangedArgs> SafeAreaChanged;
  22. public bool DisplayEdgeToEdge
  23. {
  24. get => _displayEdgeToEdge;
  25. set
  26. {
  27. _displayEdgeToEdge = value;
  28. if(Build.VERSION.SdkInt >= BuildVersionCodes.P)
  29. {
  30. _activity.Window.Attributes.LayoutInDisplayCutoutMode = value ? LayoutInDisplayCutoutMode.ShortEdges : LayoutInDisplayCutoutMode.Default;
  31. }
  32. WindowCompat.SetDecorFitsSystemWindows(_activity.Window, !value);
  33. }
  34. }
  35. public AndroidInsetsManager(AvaloniaMainActivity activity, TopLevelImpl topLevel)
  36. {
  37. _activity = activity;
  38. _topLevel = topLevel;
  39. _callback = new InsetsAnimationCallback(WindowInsetsAnimationCompat.Callback.DispatchModeStop);
  40. _callback.InsetsManager = this;
  41. ViewCompat.SetOnApplyWindowInsetsListener(_activity.Window.DecorView, this);
  42. ViewCompat.SetWindowInsetsAnimationCallback(_activity.Window.DecorView, _callback);
  43. if(Build.VERSION.SdkInt < BuildVersionCodes.R)
  44. {
  45. _usesLegacyLayouts = true;
  46. _activity.Window.DecorView.ViewTreeObserver.AddOnGlobalLayoutListener(this);
  47. }
  48. }
  49. public Thickness GetSafeAreaPadding()
  50. {
  51. var insets = ViewCompat.GetRootWindowInsets(_activity.Window.DecorView);
  52. if (insets != null)
  53. {
  54. var renderScaling = _topLevel.RenderScaling;
  55. var inset = insets.GetInsets((DisplayEdgeToEdge ? WindowInsetsCompat.Type.SystemBars() | WindowInsetsCompat.Type.DisplayCutout() : 0 ) | WindowInsetsCompat.Type.Ime());
  56. var navBarInset = insets.GetInsets(WindowInsetsCompat.Type.NavigationBars());
  57. var imeInset = insets.GetInsets(WindowInsetsCompat.Type.Ime());
  58. return new Thickness(inset.Left / renderScaling,
  59. inset.Top / renderScaling,
  60. inset.Right / renderScaling,
  61. (imeInset.Bottom > 0 && ((_usesLegacyLayouts && !DisplayEdgeToEdge) || !_usesLegacyLayouts) ? imeInset.Bottom - navBarInset.Bottom : inset.Bottom) / renderScaling);
  62. }
  63. return default;
  64. }
  65. public WindowInsetsCompat OnApplyWindowInsets(View v, WindowInsetsCompat insets)
  66. {
  67. NotifySafeAreaChanged(GetSafeAreaPadding());
  68. return insets;
  69. }
  70. private void NotifySafeAreaChanged(Thickness safeAreaPadding)
  71. {
  72. SafeAreaChanged?.Invoke(this, new SafeAreaChangedArgs(safeAreaPadding));
  73. }
  74. public void OnGlobalLayout()
  75. {
  76. NotifySafeAreaChanged(GetSafeAreaPadding());
  77. }
  78. public SystemBarTheme? SystemBarTheme
  79. {
  80. get
  81. {
  82. try
  83. {
  84. var compat = new WindowInsetsControllerCompat(_activity.Window, _topLevel.View);
  85. return compat.AppearanceLightStatusBars ? Controls.Platform.SystemBarTheme.Light : Controls.Platform.SystemBarTheme.Dark;
  86. }
  87. catch (Exception _)
  88. {
  89. return Controls.Platform.SystemBarTheme.Light;
  90. }
  91. }
  92. set
  93. {
  94. _statusBarTheme = value;
  95. if (!_topLevel.View.IsShown)
  96. {
  97. return;
  98. }
  99. var compat = new WindowInsetsControllerCompat(_activity.Window, _topLevel.View);
  100. if (_isDefaultSystemBarLightTheme == null)
  101. {
  102. _isDefaultSystemBarLightTheme = compat.AppearanceLightStatusBars;
  103. }
  104. if (value == null && _isDefaultSystemBarLightTheme != null)
  105. {
  106. value = (bool)_isDefaultSystemBarLightTheme ? Controls.Platform.SystemBarTheme.Light : Controls.Platform.SystemBarTheme.Dark;
  107. }
  108. compat.AppearanceLightStatusBars = value == Controls.Platform.SystemBarTheme.Light;
  109. compat.AppearanceLightNavigationBars = value == Controls.Platform.SystemBarTheme.Light;
  110. }
  111. }
  112. public bool? IsSystemBarVisible
  113. {
  114. get
  115. {
  116. var compat = ViewCompat.GetRootWindowInsets(_topLevel.View);
  117. return compat?.IsVisible(WindowInsetsCompat.Type.SystemBars());
  118. }
  119. set
  120. {
  121. _systemUiVisibility = value;
  122. if (!_topLevel.View.IsShown)
  123. {
  124. return;
  125. }
  126. var compat = WindowCompat.GetInsetsController(_activity.Window, _topLevel.View);
  127. if (value == null || value.Value)
  128. {
  129. compat?.Show(WindowInsetsCompat.Type.SystemBars());
  130. }
  131. else
  132. {
  133. compat?.Hide(WindowInsetsCompat.Type.SystemBars());
  134. if (compat != null)
  135. {
  136. compat.SystemBarsBehavior = WindowInsetsControllerCompat.BehaviorShowTransientBarsBySwipe;
  137. }
  138. }
  139. }
  140. }
  141. internal void ApplyStatusBarState()
  142. {
  143. IsSystemBarVisible = _systemUiVisibility;
  144. SystemBarTheme = _statusBarTheme;
  145. }
  146. private class InsetsAnimationCallback : WindowInsetsAnimationCompat.Callback
  147. {
  148. public InsetsAnimationCallback(int dispatchMode) : base(dispatchMode)
  149. {
  150. }
  151. public AndroidInsetsManager InsetsManager { get; set; }
  152. public override WindowInsetsCompat OnProgress(WindowInsetsCompat insets, IList<WindowInsetsAnimationCompat> runningAnimations)
  153. {
  154. foreach (var anim in runningAnimations)
  155. {
  156. if ((anim.TypeMask & WindowInsetsCompat.Type.Ime()) != 0)
  157. {
  158. var renderScaling = InsetsManager._topLevel.RenderScaling;
  159. var inset = insets.GetInsets((InsetsManager.DisplayEdgeToEdge ? WindowInsetsCompat.Type.SystemBars() | WindowInsetsCompat.Type.DisplayCutout() : 0) | WindowInsetsCompat.Type.Ime());
  160. var navBarInset = insets.GetInsets(WindowInsetsCompat.Type.NavigationBars());
  161. var imeInset = insets.GetInsets(WindowInsetsCompat.Type.Ime());
  162. var bottomPadding = (imeInset.Bottom > 0 && !InsetsManager.DisplayEdgeToEdge ? imeInset.Bottom - navBarInset.Bottom : inset.Bottom);
  163. bottomPadding = (int)(bottomPadding * anim.InterpolatedFraction);
  164. var padding = new Thickness(inset.Left / renderScaling,
  165. inset.Top / renderScaling,
  166. inset.Right / renderScaling,
  167. bottomPadding / renderScaling);
  168. InsetsManager?.NotifySafeAreaChanged(padding);
  169. break;
  170. }
  171. }
  172. return insets;
  173. }
  174. }
  175. }
  176. }