diff --git a/src/Android/Avalonia.Android/IInitEditorInfo.cs b/src/Android/Avalonia.Android/IInitEditorInfo.cs new file mode 100644 index 0000000000..98fc4eafc1 --- /dev/null +++ b/src/Android/Avalonia.Android/IInitEditorInfo.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Text; +using Android.Views.InputMethods; + +namespace Avalonia.Android +{ + interface IInitEditorInfo + { + void InitEditorInfo(Action init); + } +} diff --git a/src/Android/Avalonia.Android/Platform/SkiaPlatform/InvalidationAwareSurfaceView.cs b/src/Android/Avalonia.Android/Platform/SkiaPlatform/InvalidationAwareSurfaceView.cs index a1c2b2d9fb..a43861d4e7 100644 --- a/src/Android/Avalonia.Android/Platform/SkiaPlatform/InvalidationAwareSurfaceView.cs +++ b/src/Android/Avalonia.Android/Platform/SkiaPlatform/InvalidationAwareSurfaceView.cs @@ -13,7 +13,7 @@ using Avalonia.Platform; namespace Avalonia.Android { - public abstract class InvalidationAwareSurfaceView : SurfaceView, ISurfaceHolderCallback, IPlatformHandle, IAndroidSoftInput + public abstract class InvalidationAwareSurfaceView : SurfaceView, ISurfaceHolderCallback, IPlatformHandle, IInitEditorInfo { bool _invalidateQueued; private ISoftInputElement _softInputElement; @@ -92,14 +92,19 @@ namespace Avalonia.Android protected abstract void Draw(); public string HandleDescriptor => "SurfaceView"; - public override IInputConnection OnCreateInputConnection(EditorInfo outAttrs) + private Action _initEditorInfo; + + public void InitEditorInfo(Action init) { - outAttrs.InputType = _softInputElement.InputType switch - { - InputType.Numeric => global::Android.Text.InputTypes.ClassNumber, - InputType.Phone => global::Android.Text.InputTypes.ClassPhone, - _ => global::Android.Text.InputTypes.Null - }; + _initEditorInfo = init; + } + + public sealed override IInputConnection OnCreateInputConnection(EditorInfo outAttrs) + { + if (_initEditorInfo == null) + throw new InvalidOperationException("Call IInitEditorInfo.InitEditorInfo first"); + + _initEditorInfo(outAttrs); return base.OnCreateInputConnection(outAttrs); } @@ -119,32 +124,5 @@ namespace Avalonia.Android { return true; } - - public void ShowSoftInput(ISoftInputElement softInputElement) - { - var input = Context.GetSystemService(Context.InputMethodService).JavaCast(); - var previousSoftInput = _softInputElement; - _softInputElement = softInputElement; - - if (_softInputElement.InputType == InputType.None) - HideSoftInput(); - else - { - RequestFocus(); - - if (!ReferenceEquals(_softInputElement, previousSoftInput)) - { - input.RestartInput(this); - } - - input.ToggleSoftInput(ShowFlags.Forced, HideSoftInputFlags.NotAlways); - } - } - - public void HideSoftInput() - { - var input = Context.GetSystemService(Context.InputMethodService).JavaCast(); - input.HideSoftInputFromWindow(WindowToken, HideSoftInputFlags.None); - } } } diff --git a/src/Android/Avalonia.Android/Platform/SkiaPlatform/TopLevelImpl.cs b/src/Android/Avalonia.Android/Platform/SkiaPlatform/TopLevelImpl.cs index 219dd4cf67..bd9a5868c0 100644 --- a/src/Android/Avalonia.Android/Platform/SkiaPlatform/TopLevelImpl.cs +++ b/src/Android/Avalonia.Android/Platform/SkiaPlatform/TopLevelImpl.cs @@ -4,7 +4,7 @@ using Android.Content; using Android.Graphics; using Android.Runtime; using Android.Views; - +using Android.Views.InputMethods; using Avalonia.Android.OpenGL; using Avalonia.Android.Platform.Specific; using Avalonia.Android.Platform.Specific.Helpers; @@ -19,7 +19,7 @@ using Avalonia.Rendering; namespace Avalonia.Android.Platform.SkiaPlatform { - class TopLevelImpl : IAndroidView, ITopLevelImpl, EglGlPlatformSurfaceBase.IEglWindowGlPlatformSurfaceInfo, IAndroidSoftInput + class TopLevelImpl : IAndroidView, ITopLevelImpl, EglGlPlatformSurfaceBase.IEglWindowGlPlatformSurfaceInfo, IInitEditorInfo { private readonly IGlPlatformSurface _gl; private readonly IFramebufferPlatformSurface _framebuffer; @@ -210,15 +210,9 @@ namespace Avalonia.Android.Platform.SkiaPlatform { throw new NotImplementedException(); } - - void IAndroidSoftInput.ShowSoftInput(ISoftInputElement softInputElement) - { - _view.ShowSoftInput(softInputElement); - } - - void IAndroidSoftInput.HideSoftInput() + public void InitEditorInfo(Action init) { - _view.HideSoftInput(); + _view.InitEditorInfo(init); } } } diff --git a/src/Android/Avalonia.Android/Platform/Specific/Helpers/AndroidKeyboardEventsHelper.cs b/src/Android/Avalonia.Android/Platform/Specific/Helpers/AndroidKeyboardEventsHelper.cs index 264c9f4cb5..e98a8f183e 100644 --- a/src/Android/Avalonia.Android/Platform/Specific/Helpers/AndroidKeyboardEventsHelper.cs +++ b/src/Android/Avalonia.Android/Platform/Specific/Helpers/AndroidKeyboardEventsHelper.cs @@ -14,7 +14,7 @@ using Avalonia.Input.Raw; namespace Avalonia.Android.Platform.Specific.Helpers { - internal class AndroidKeyboardEventsHelper : IDisposable where TView : TopLevelImpl, IAndroidView, IAndroidSoftInput + internal class AndroidKeyboardEventsHelper : IDisposable where TView : TopLevelImpl, IAndroidView, IInitEditorInfo { private TView _view; private IInputElement _lastFocusedElement; @@ -107,13 +107,32 @@ namespace Avalonia.Android.Platform.Specific.Helpers private void TryShowHideKeyboard(ISoftInputElement element, bool value) { - if (value) + _view.InitEditorInfo((outAttrs) => { - _view.ShowSoftInput(element); + outAttrs.InputType = element.InputType switch + { + InputType.Numeric => global::Android.Text.InputTypes.ClassNumber, + InputType.Phone => global::Android.Text.InputTypes.ClassPhone, + _ => global::Android.Text.InputTypes.Null + }; + }); + + var input = _view.View.Context.GetSystemService(Context.InputMethodService).JavaCast(); + + if (value && element != null && element.InputType != InputType.None) + { + _view.View.RequestFocus(); + + if (!ReferenceEquals(_lastFocusedElement, element)) + { + input.RestartInput(_view.View); + } + + input.ToggleSoftInput(ShowFlags.Forced, HideSoftInputFlags.NotAlways); } else { - _view.HideSoftInput(); + input.HideSoftInputFromWindow(_view.View.WindowToken, HideSoftInputFlags.None); } } diff --git a/src/Android/Avalonia.Android/Platform/Specific/IAndroidView.cs b/src/Android/Avalonia.Android/Platform/Specific/IAndroidView.cs index db65a1335b..1f99051fe6 100644 --- a/src/Android/Avalonia.Android/Platform/Specific/IAndroidView.cs +++ b/src/Android/Avalonia.Android/Platform/Specific/IAndroidView.cs @@ -9,11 +9,4 @@ namespace Avalonia.Android.Platform.Specific } - - public interface IAndroidSoftInput - { - void ShowSoftInput(ISoftInputElement softInputElement); - - void HideSoftInput(); - } }