MainViewModel.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. using System;
  2. using Avalonia;
  3. using Avalonia.Animation.Easings;
  4. using Avalonia.Controls;
  5. using Avalonia.Controls.Platform;
  6. using MiniMvvm;
  7. namespace SafeAreaDemo.ViewModels
  8. {
  9. public class MainViewModel : ViewModelBase
  10. {
  11. private bool _useSafeArea = true;
  12. private bool _displayEdgeToEdge;
  13. private IInsetsManager? _insetsManager;
  14. private bool _hideSystemBars;
  15. private bool _autoSafeAreaPadding;
  16. private IInputPane? _inputPane;
  17. public InputPaneState InputPaneState
  18. {
  19. get
  20. {
  21. return _inputPane?.State ?? InputPaneState.Closed;
  22. }
  23. }
  24. public IEasing? InputPaneEasing { get; private set; }
  25. public TimeSpan? InputPaneDuration { get; private set; }
  26. public Thickness InputPaneMarkerMargin => InputPaneState == InputPaneState.Open
  27. ? new Thickness(0, 0, 0, Math.Max(0, CanvasSize.Height - InputPaneRect.Top))
  28. : default;
  29. public Rect InputPaneRect => _inputPane?.OccludedRect ?? default;
  30. public Rect CanvasSize { get; set; }
  31. public Thickness SafeAreaPadding
  32. {
  33. get
  34. {
  35. return !_autoSafeAreaPadding ? _insetsManager?.SafeAreaPadding ?? default : default;
  36. }
  37. }
  38. public Thickness ViewPadding
  39. {
  40. get
  41. {
  42. return _useSafeArea ? SafeAreaPadding : default;
  43. }
  44. }
  45. public bool UseSafeArea
  46. {
  47. get => _useSafeArea;
  48. set
  49. {
  50. _useSafeArea = value;
  51. this.RaisePropertyChanged();
  52. RaiseSafeAreaChanged();
  53. }
  54. }
  55. public bool DisplayEdgeToEdge
  56. {
  57. get => _displayEdgeToEdge;
  58. set
  59. {
  60. _displayEdgeToEdge = value;
  61. if (_insetsManager != null)
  62. {
  63. _insetsManager.DisplayEdgeToEdgePreference = value;
  64. }
  65. this.RaisePropertyChanged();
  66. RaiseSafeAreaChanged();
  67. }
  68. }
  69. public bool HideSystemBars
  70. {
  71. get => _hideSystemBars;
  72. set
  73. {
  74. _hideSystemBars = value;
  75. if (_insetsManager != null)
  76. {
  77. _insetsManager.IsSystemBarVisible = !value;
  78. }
  79. this.RaisePropertyChanged();
  80. RaiseSafeAreaChanged();
  81. }
  82. }
  83. public bool AutoSafeAreaPadding
  84. {
  85. get => _autoSafeAreaPadding;
  86. set
  87. {
  88. _autoSafeAreaPadding = value;
  89. RaisePropertyChanged();
  90. RaiseSafeAreaChanged();
  91. }
  92. }
  93. internal void Initialize(Control mainView, IInsetsManager? InsetsManager, IInputPane? inputPane)
  94. {
  95. if (_insetsManager != null)
  96. {
  97. _insetsManager.SafeAreaChanged -= InsetsManager_SafeAreaChanged;
  98. }
  99. if (_inputPane != null)
  100. {
  101. _inputPane.StateChanged -= InputPaneOnStateChanged;
  102. }
  103. _autoSafeAreaPadding = mainView.GetValue(TopLevel.AutoSafeAreaPaddingProperty);
  104. _insetsManager = InsetsManager;
  105. if (_insetsManager != null)
  106. {
  107. _insetsManager.SafeAreaChanged += InsetsManager_SafeAreaChanged;
  108. _displayEdgeToEdge = _insetsManager.DisplayEdgeToEdgePreference;
  109. _hideSystemBars = !(_insetsManager.IsSystemBarVisible ?? false);
  110. }
  111. _inputPane = inputPane;
  112. if (_inputPane != null)
  113. {
  114. _inputPane.StateChanged += InputPaneOnStateChanged;
  115. }
  116. RaiseKeyboardChanged();
  117. }
  118. private void InputPaneOnStateChanged(object? sender, InputPaneStateEventArgs e)
  119. {
  120. InputPaneDuration = e.AnimationDuration;
  121. InputPaneEasing = e.Easing ?? new LinearEasing();
  122. RaiseKeyboardChanged();
  123. }
  124. private void InsetsManager_SafeAreaChanged(object? sender, SafeAreaChangedArgs e)
  125. {
  126. RaiseSafeAreaChanged();
  127. }
  128. private void RaiseSafeAreaChanged()
  129. {
  130. this.RaisePropertyChanged(nameof(SafeAreaPadding));
  131. this.RaisePropertyChanged(nameof(ViewPadding));
  132. this.RaisePropertyChanged(nameof(InputPaneMarkerMargin));
  133. }
  134. private void RaiseKeyboardChanged()
  135. {
  136. this.RaisePropertyChanged(nameof(InputPaneState));
  137. this.RaisePropertyChanged(nameof(InputPaneRect));
  138. this.RaisePropertyChanged(nameof(InputPaneEasing));
  139. this.RaisePropertyChanged(nameof(InputPaneDuration));
  140. this.RaisePropertyChanged(nameof(InputPaneMarkerMargin));
  141. }
  142. }
  143. }