Browse Source

Move keyboard logic back to the helper

pull/5735/head
ili 5 years ago
parent
commit
2ca903631e
  1. 12
      src/Android/Avalonia.Android/IInitEditorInfo.cs
  2. 48
      src/Android/Avalonia.Android/Platform/SkiaPlatform/InvalidationAwareSurfaceView.cs
  3. 14
      src/Android/Avalonia.Android/Platform/SkiaPlatform/TopLevelImpl.cs
  4. 27
      src/Android/Avalonia.Android/Platform/Specific/Helpers/AndroidKeyboardEventsHelper.cs
  5. 7
      src/Android/Avalonia.Android/Platform/Specific/IAndroidView.cs

12
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<EditorInfo> init);
}
}

48
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<EditorInfo> _initEditorInfo;
public void InitEditorInfo(Action<EditorInfo> 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<InputMethodManager>();
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<InputMethodManager>();
input.HideSoftInputFromWindow(WindowToken, HideSoftInputFlags.None);
}
}
}

14
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<EditorInfo> init)
{
_view.HideSoftInput();
_view.InitEditorInfo(init);
}
}
}

27
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<TView> : IDisposable where TView : TopLevelImpl, IAndroidView, IAndroidSoftInput
internal class AndroidKeyboardEventsHelper<TView> : 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<InputMethodManager>();
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);
}
}

7
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();
}
}

Loading…
Cancel
Save