using Xamarin.Essentials;
using Xamarin.Forms;
namespace Bit.App.Behaviors
{
///
/// This behavior prevents the Editor to be automatically scrolled to the bottom on focus.
/// This is needed due to this Xamarin Forms issue: https://github.com/xamarin/Xamarin.Forms/issues/2233
///
public class EditorPreventAutoBottomScrollingOnFocusedBehavior : Behavior
{
public static readonly BindableProperty ParentScrollViewProperty
= BindableProperty.Create(nameof(ParentScrollView), typeof(ScrollView), typeof(EditorPreventAutoBottomScrollingOnFocusedBehavior));
public ScrollView ParentScrollView
{
get => (ScrollView)GetValue(ParentScrollViewProperty);
set => SetValue(ParentScrollViewProperty, value);
}
protected override void OnAttachedTo(Editor bindable)
{
base.OnAttachedTo(bindable);
bindable.Focused += OnFocused;
}
private void OnFocused(object sender, FocusEventArgs e)
{
if (DeviceInfo.Platform.Equals(DevicePlatform.iOS) && ParentScrollView != null)
{
ParentScrollView.ScrollToAsync(ParentScrollView.ScrollX, ParentScrollView.ScrollY, true);
}
}
protected override void OnDetachingFrom(Editor bindable)
{
bindable.Focused -= OnFocused;
base.OnDetachingFrom(bindable);
}
}
}