A cross-platform UI framework for .NET
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

58 lines
2.3 KiB

using System;
using System.Diagnostics;
using System.Runtime.Versioning;
using Avalonia.Animation.Easings;
using Avalonia.Controls.Platform;
using Foundation;
using UIKit;
namespace Avalonia.iOS;
[UnsupportedOSPlatform("tvos")]
[SupportedOSPlatform("maccatalyst")]
[SupportedOSPlatform("ios")]
internal sealed class UIKitInputPane : InputPaneBase
{
public static UIKitInputPane Instance { get; } = new();
public UIKitInputPane()
{
NSNotificationCenter
.DefaultCenter
.AddObserver(UIKeyboard.WillShowNotification, KeyboardUpNotification);
NSNotificationCenter
.DefaultCenter
.AddObserver(UIKeyboard.WillHideNotification, KeyboardDownNotification);
}
private void KeyboardDownNotification(NSNotification obj) => RaiseEventFromNotification(false, obj);
private void KeyboardUpNotification(NSNotification obj) => RaiseEventFromNotification(true, obj);
private void RaiseEventFromNotification(bool isUp, NSNotification notification)
{
State = isUp ? InputPaneState.Open : InputPaneState.Closed;
#if MACCATALYST
OccludedRect = default;
OnStateChanged(new InputPaneStateEventArgs(
State, null, OccludedRect));
#else
var startFrame = UIKeyboard.FrameBeginFromNotification(notification);
var endFrame = UIKeyboard.FrameEndFromNotification(notification);
var duration = UIKeyboard.AnimationDurationFromNotification(notification);
var curve = (UIViewAnimationOptions)UIKeyboard.AnimationCurveFromNotification(notification);
IEasing? easing =
curve.HasFlag(UIViewAnimationOptions.CurveLinear) ? new LinearEasing()
: curve.HasFlag(UIViewAnimationOptions.CurveEaseIn) ? new SineEaseIn()
: curve.HasFlag(UIViewAnimationOptions.CurveEaseOut) ? new SineEaseOut()
: curve.HasFlag(UIViewAnimationOptions.CurveEaseInOut) ? new SineEaseInOut()
: null;
var startRect = new Rect(startFrame.X, startFrame.Y, startFrame.Width, startFrame.Height);
OccludedRect = new Rect(endFrame.X, endFrame.Y, endFrame.Width, endFrame.Height);
OnStateChanged(new InputPaneStateEventArgs(
State, startRect, OccludedRect, TimeSpan.FromSeconds(duration), easing));
#endif
}
}