2using Microsoft.Maui.Controls;
3using Microsoft.Maui.Controls.Shapes;
6using ControlsVisualElement =
Microsoft.Maui.Controls.VisualElement;
15 private Thickness originalPadding;
17 private ControlsVisualElement? associatedView;
50 protected override void OnAttachedTo(ControlsVisualElement bindable)
52 base.OnAttachedTo(bindable);
53 this.associatedView = bindable;
54 this.originalPadding = GetPadding(bindable);
56 this.keyboardInsetsService.KeyboardInsetChanged += this.OnKeyboardInsetChanged;
57 this.ApplyPadding(bindable, this.keyboardInsetsService.
KeyboardHeight,
false);
63 base.OnDetachingFrom(bindable);
64 if (this.keyboardInsetsService is not
null)
65 this.keyboardInsetsService.KeyboardInsetChanged -= this.OnKeyboardInsetChanged;
66 SetPadding(bindable, this.originalPadding);
67 this.keyboardInsetsService =
null;
68 this.associatedView =
null;
73 if (this.associatedView is not ControlsVisualElement view)
79 private void ApplyPadding(ControlsVisualElement view,
double keyboardHeight,
bool animate)
82 Thickness targetPadding =
new Thickness(
83 this.originalPadding.Left,
84 this.originalPadding.Top,
85 this.originalPadding.Right,
90 SetPadding(view, targetPadding);
94 Thickness currentPadding = GetPadding(view);
95 if (this.AreThicknessClose(currentPadding, targetPadding))
97 SetPadding(view, targetPadding);
101 const uint duration = 180;
102 Microsoft.Maui.Controls.ViewExtensions.CancelAnimations(view);
104 "KeyboardInsetsPadding",
105 progress => SetPadding(view, this.InterpolateThickness(currentPadding, targetPadding, progress)),
108 easing: Easing.CubicInOut,
109 finished: (v, cancelled) => SetPadding(view, targetPadding));
112 private static void SetPadding(ControlsVisualElement element, Thickness padding)
117 layout.Padding = padding;
119 case ContentView contentView:
120 contentView.Padding = padding;
122 case TemplatedView templatedView:
123 templatedView.Padding = padding;
126 border.Padding = padding;
131 private static Thickness GetPadding(ControlsVisualElement element)
133 return element
switch
135 Layout layout => layout.Padding,
136 ContentView contentView => contentView.Padding,
137 TemplatedView templatedView => templatedView.Padding,
138 Border border => border.Padding,
139 _ =>
new Thickness(0)
143 private bool AreThicknessClose(Thickness a, Thickness b)
145 return Math.Abs(a.Left - b.Left) < 0.5 &&
146 Math.Abs(a.Top - b.Top) < 0.5 &&
147 Math.Abs(a.Right - b.Right) < 0.5 &&
148 Math.Abs(a.Bottom - b.Bottom) < 0.5;
151 private Thickness InterpolateThickness(Thickness from, Thickness to,
double progress)
153 return new Thickness(
154 this.InterpolateDouble(from.Left, to.Left, progress),
155 this.InterpolateDouble(from.Top, to.Top, progress),
156 this.InterpolateDouble(from.Right, to.Right, progress),
157 this.InterpolateDouble(from.Bottom, to.Bottom, progress));
160 private double InterpolateDouble(
double from,
double to,
double progress)
162 return from + ((to - from) * progress);
Base class that references services in the app.
static IKeyboardInsetsService KeyboardInsetsService
Provides the current keyboard inset state.
Event data describing an update to the keyboard inset height.
double KeyboardHeight
Gets the keyboard height measured in device-independent units.
Applies the current keyboard inset to the padding of the associated view.
static readonly BindableProperty AnimateProperty
Identifies the Animate bindable property.
bool Animate
Gets or sets a value indicating whether padding changes should be animated.
override void OnDetachingFrom(ControlsVisualElement bindable)
override void OnAttachedTo(ControlsVisualElement bindable)
double AdditionalBottomPadding
Gets or sets an additional bottom padding offset that is always added to the keyboard height.
static readonly BindableProperty AdditionalBottomPaddingProperty
Identifies the AdditionalBottomPadding bindable property.
Provides normalized keyboard inset information for UI components.
double KeyboardHeight
Gets the current keyboard height in device-independent units.