123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172 |
- using System;
- using Avalonia;
- using Avalonia.Animation.Easings;
- using Avalonia.Controls;
- using Avalonia.Controls.Platform;
- using MiniMvvm;
- namespace SafeAreaDemo.ViewModels
- {
- public class MainViewModel : ViewModelBase
- {
- private bool _useSafeArea = true;
- private bool _displayEdgeToEdge;
- private IInsetsManager? _insetsManager;
- private bool _hideSystemBars;
- private bool _autoSafeAreaPadding;
- private IInputPane? _inputPane;
- public InputPaneState InputPaneState
- {
- get
- {
- return _inputPane?.State ?? InputPaneState.Closed;
- }
- }
- public IEasing? InputPaneEasing { get; private set; }
- public TimeSpan? InputPaneDuration { get; private set; }
- public Thickness InputPaneMarkerMargin => InputPaneState == InputPaneState.Open
- ? new Thickness(0, 0, 0, Math.Max(0, CanvasSize.Height - InputPaneRect.Top))
- : default;
- public Rect InputPaneRect => _inputPane?.OccludedRect ?? default;
- public Rect CanvasSize { get; set; }
-
- public Thickness SafeAreaPadding
- {
- get
- {
- return !_autoSafeAreaPadding ? _insetsManager?.SafeAreaPadding ?? default : default;
- }
- }
- public Thickness ViewPadding
- {
- get
- {
- return _useSafeArea ? SafeAreaPadding : default;
- }
- }
- public bool UseSafeArea
- {
- get => _useSafeArea;
- set
- {
- _useSafeArea = value;
- this.RaisePropertyChanged();
- RaiseSafeAreaChanged();
- }
- }
- public bool DisplayEdgeToEdge
- {
- get => _displayEdgeToEdge;
- set
- {
- _displayEdgeToEdge = value;
- if (_insetsManager != null)
- {
- _insetsManager.DisplayEdgeToEdgePreference = value;
- }
- this.RaisePropertyChanged();
- RaiseSafeAreaChanged();
- }
- }
- public bool HideSystemBars
- {
- get => _hideSystemBars;
- set
- {
- _hideSystemBars = value;
- if (_insetsManager != null)
- {
- _insetsManager.IsSystemBarVisible = !value;
- }
- this.RaisePropertyChanged();
- RaiseSafeAreaChanged();
- }
- }
- public bool AutoSafeAreaPadding
- {
- get => _autoSafeAreaPadding;
- set
- {
- _autoSafeAreaPadding = value;
-
- RaisePropertyChanged();
- RaiseSafeAreaChanged();
- }
- }
- internal void Initialize(Control mainView, IInsetsManager? InsetsManager, IInputPane? inputPane)
- {
- if (_insetsManager != null)
- {
- _insetsManager.SafeAreaChanged -= InsetsManager_SafeAreaChanged;
- }
- if (_inputPane != null)
- {
- _inputPane.StateChanged -= InputPaneOnStateChanged;
- }
- _autoSafeAreaPadding = mainView.GetValue(TopLevel.AutoSafeAreaPaddingProperty);
- _insetsManager = InsetsManager;
- if (_insetsManager != null)
- {
- _insetsManager.SafeAreaChanged += InsetsManager_SafeAreaChanged;
- _displayEdgeToEdge = _insetsManager.DisplayEdgeToEdgePreference;
- _hideSystemBars = !(_insetsManager.IsSystemBarVisible ?? false);
- }
- _inputPane = inputPane;
- if (_inputPane != null)
- {
- _inputPane.StateChanged += InputPaneOnStateChanged;
- }
- RaiseKeyboardChanged();
- }
- private void InputPaneOnStateChanged(object? sender, InputPaneStateEventArgs e)
- {
- InputPaneDuration = e.AnimationDuration;
- InputPaneEasing = e.Easing ?? new LinearEasing();
- RaiseKeyboardChanged();
- }
- private void InsetsManager_SafeAreaChanged(object? sender, SafeAreaChangedArgs e)
- {
- RaiseSafeAreaChanged();
- }
- private void RaiseSafeAreaChanged()
- {
- this.RaisePropertyChanged(nameof(SafeAreaPadding));
- this.RaisePropertyChanged(nameof(ViewPadding));
- this.RaisePropertyChanged(nameof(InputPaneMarkerMargin));
- }
-
- private void RaiseKeyboardChanged()
- {
- this.RaisePropertyChanged(nameof(InputPaneState));
- this.RaisePropertyChanged(nameof(InputPaneRect));
- this.RaisePropertyChanged(nameof(InputPaneEasing));
- this.RaisePropertyChanged(nameof(InputPaneDuration));
- this.RaisePropertyChanged(nameof(InputPaneMarkerMargin));
- }
- }
- }
|