Skip to content

Commit

Permalink
Merge pull request #11848 from Gillibald/textInputMethodClientRework
Browse files Browse the repository at this point in the history
Rework ITextInputMethodClient
  • Loading branch information
Dan Walmsley committed Jun 30, 2023
2 parents 8890c4e + 792143d commit fddb07e
Show file tree
Hide file tree
Showing 21 changed files with 505 additions and 566 deletions.
5 changes: 4 additions & 1 deletion samples/MobileSandbox.Android/MainActivity.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using Android.App;
using System;
using Android.App;
using Android.Content.PM;
using Android.OS;
using Avalonia;
using Avalonia.Android;

namespace MobileSandbox.Android
Expand Down
70 changes: 46 additions & 24 deletions src/Android/Avalonia.Android/AndroidInputMethod.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using System;
using Android.Content;
using Android.Runtime;
using Android.Text;
using Android.Views;
using Android.Views.InputMethods;
using Avalonia.Android.Platform.SkiaPlatform;
using Avalonia.Controls.Presenters;
using Avalonia.Input.TextInput;

namespace Avalonia.Android
Expand All @@ -13,7 +13,7 @@ internal interface IAndroidInputMethod
{
public View View { get; }

public ITextInputMethodClient Client { get; }
public TextInputMethodClient Client { get; }

public bool IsActive { get; }

Expand All @@ -36,7 +36,7 @@ internal class AndroidInputMethod<TView> : ITextInputMethodImpl, IAndroidInputMe
{
private readonly TView _host;
private readonly InputMethodManager _imm;
private ITextInputMethodClient _client;
private TextInputMethodClient _client;
private AvaloniaInputConnection _inputConnection;

public AndroidInputMethod(TView host)
Expand All @@ -56,7 +56,7 @@ public AndroidInputMethod(TView host)

public bool IsActive => Client != null;

public ITextInputMethodClient Client => _client;
public TextInputMethodClient Client => _client;

public InputMethodManager IMM => _imm;

Expand All @@ -65,7 +65,7 @@ public void Reset()

}

public void SetClient(ITextInputMethodClient client)
public void SetClient(TextInputMethodClient client)
{
_client = client;

Expand All @@ -77,16 +77,55 @@ public void SetClient(ITextInputMethodClient client)

_imm.ShowSoftInput(_host, ShowFlags.Implicit);

var surroundingText = Client.SurroundingText;
var selection = Client.Selection;

_imm.UpdateSelection(_host, surroundingText.AnchorOffset, surroundingText.CursorOffset, surroundingText.AnchorOffset, surroundingText.CursorOffset);
_imm.UpdateSelection(_host, selection.Start, selection.End, selection.Start, selection.End);

var surroundingText = _client.SurroundingText ?? "";

var extractedText = new ExtractedText
{
Text = new Java.Lang.String(surroundingText),
SelectionStart = selection.Start,
SelectionEnd = selection.End,
PartialEndOffset = surroundingText.Length
};

_imm.UpdateExtractedText(_host, _inputConnection?.ExtractedTextToken ?? 0, extractedText);

_client.SurroundingTextChanged += _client_SurroundingTextChanged;
_client.SelectionChanged += _client_SelectionChanged;
}
else
{
_imm.HideSoftInputFromWindow(_host.WindowToken, HideSoftInputFlags.ImplicitOnly);
}
}

private void _client_SelectionChanged(object sender, EventArgs e)
{
var selection = Client.Selection;

_imm.UpdateSelection(_host, selection.Start, selection.End, selection.Start, selection.End);

_inputConnection.SetSelection(selection.Start, selection.End);
}

private void _client_SurroundingTextChanged(object sender, EventArgs e)
{
var surroundingText = _client.SurroundingText ?? "";

_inputConnection.EditableWrapper.IgnoreChange = true;

_inputConnection.Editable.Replace(0, _inputConnection.Editable.Length(), surroundingText);

_inputConnection.EditableWrapper.IgnoreChange = false;

var selection = Client.Selection;

_imm.UpdateSelection(_host, selection.Start, selection.End, selection.Start, selection.End);
}

public void SetCursorRect(Rect rect)
{

Expand Down Expand Up @@ -134,25 +173,8 @@ public void SetOptions(TextInputOptions options)
outAttrs.ImeOptions |= ImeFlags.NoFullscreen | ImeFlags.NoExtractUi;
_client.TextEditable = _inputConnection.InputEditable;
return _inputConnection;
});
}
}

internal readonly record struct ComposingRegion
{
private readonly int _start = -1;
private readonly int _end = -1;

public ComposingRegion(int start, int end)
{
_start = start;
_end = end;
}

public int Start => _start;
public int End => _end;
}
}
127 changes: 0 additions & 127 deletions src/Android/Avalonia.Android/InputEditable.cs

This file was deleted.

Loading

0 comments on commit fddb07e

Please sign in to comment.