Browse Source

[Android] [TextBox] fix a bug which cause cannot call out the soft keyboard again after closing it (#15603)

* [Android] fix a bug which cause closing soft keyboard does not clear focus on TextBox, which makes clicking on the TextBox cannot call out the soft keyboard again

* change way to show soft keyboard

* add new api

---------

Co-authored-by: Max Katz <maxkatz6@outlook.com>
release/11.1.3
ijklam 2 years ago
committed by Max Katz
parent
commit
b2083a389e
  1. 2
      src/Avalonia.Base/Input/TextInput/TextInputMethodClient.cs
  2. 2
      src/Avalonia.Controls/TextBox.cs
  3. 23
      src/Avalonia.Controls/TextBoxTextInputMethodClient.cs

2
src/Avalonia.Base/Input/TextInput/TextInputMethodClient.cs

@ -71,6 +71,8 @@ namespace Avalonia.Input.TextInput
{ {
SetPreeditText(preeditText); SetPreeditText(preeditText);
} }
public virtual void ShowInputPanel() { }
protected virtual void RaiseTextViewVisualChanged() protected virtual void RaiseTextViewVisualChanged()
{ {

2
src/Avalonia.Controls/TextBox.cs

@ -1724,6 +1724,8 @@ namespace Avalonia.Controls
if (e.Pointer.Type != PointerType.Mouse && !_isDoubleTapped) if (e.Pointer.Type != PointerType.Mouse && !_isDoubleTapped)
{ {
_imClient.ShowInputPanel();
var text = Text; var text = Text;
var clickInfo = e.GetCurrentPoint(this); var clickInfo = e.GetCurrentPoint(this);
if (text != null && !(clickInfo.Pointer?.Captured is Border)) if (text != null && !(clickInfo.Pointer?.Captured is Border))

23
src/Avalonia.Controls/TextBoxTextInputMethodClient.cs

@ -13,6 +13,7 @@ namespace Avalonia.Controls
private TextPresenter? _presenter; private TextPresenter? _presenter;
private bool _selectionChanged; private bool _selectionChanged;
private bool _isInChange; private bool _isInChange;
private ITextInputMethodImpl? _im;
public override Visual TextViewVisual => _presenter!; public override Visual TextViewVisual => _presenter!;
@ -24,7 +25,7 @@ namespace Avalonia.Controls
{ {
return ""; return "";
} }
if (_parent.CaretIndex != _presenter.CaretIndex) if (_parent.CaretIndex != _presenter.CaretIndex)
{ {
_presenter.SetCurrentValue(TextPresenter.CaretIndexProperty, _parent.CaretIndex); _presenter.SetCurrentValue(TextPresenter.CaretIndexProperty, _parent.CaretIndex);
@ -34,7 +35,7 @@ namespace Avalonia.Controls
{ {
_presenter.SetCurrentValue(TextPresenter.TextProperty, _parent.Text); _presenter.SetCurrentValue(TextPresenter.TextProperty, _parent.Text);
} }
var lineIndex = _presenter.TextLayout.GetLineIndexFromCharacterIndex(_presenter.CaretIndex, false); var lineIndex = _presenter.TextLayout.GetLineIndexFromCharacterIndex(_presenter.CaretIndex, false);
var textLine = _presenter.TextLayout.TextLines[lineIndex]; var textLine = _presenter.TextLayout.TextLines[lineIndex];
@ -125,6 +126,11 @@ namespace Avalonia.Controls
if (_parent != null) if (_parent != null)
{ {
_parent.PropertyChanged += OnParentPropertyChanged; _parent.PropertyChanged += OnParentPropertyChanged;
_im = (_parent.VisualRoot as ITextInputMethodRoot)?.InputMethod;
}
else
{
_im = null;
} }
var oldPresenter = _presenter; var oldPresenter = _presenter;
@ -133,7 +139,7 @@ namespace Avalonia.Controls
{ {
oldPresenter.ClearValue(TextPresenter.PreeditTextProperty); oldPresenter.ClearValue(TextPresenter.PreeditTextProperty);
oldPresenter.CaretBoundsChanged -= (s,e) => RaiseCursorRectangleChanged(); oldPresenter.CaretBoundsChanged -= (s, e) => RaiseCursorRectangleChanged();
} }
_presenter = presenter; _presenter = presenter;
@ -161,6 +167,17 @@ namespace Avalonia.Controls
_presenter.SetCurrentValue(TextPresenter.PreeditTextCursorPositionProperty, cursorPos); _presenter.SetCurrentValue(TextPresenter.PreeditTextCursorPositionProperty, cursorPos);
} }
public override void ShowInputPanel()
{
base.ShowInputPanel();
if (_parent is { } && _im is { })
{
_im.SetOptions(TextInputOptions.FromStyledElement(_parent));
_im.SetClient(this);
}
}
private static string GetTextLineText(TextLine textLine) private static string GetTextLineText(TextLine textLine)
{ {
var builder = StringBuilderCache.Acquire(textLine.Length); var builder = StringBuilderCache.Acquire(textLine.Length);

Loading…
Cancel
Save