Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try fix Korean IME on iOS #17680

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions src/iOS/Avalonia.iOS/TextInputResponder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ public TextInputResponder(AvaloniaView view, TextInputMethodClient client)
private int _inSurroundingTextUpdateEvent;
private readonly UITextPosition _beginningOfDocument = new AvaloniaTextPosition(0);
private readonly UITextInputStringTokenizer _tokenizer;
private bool _isInUpdate;

public TextInputMethodClient? Client => _client;

Expand Down Expand Up @@ -142,15 +143,15 @@ public override UITextInputMode TextInputMode
private void SurroundingTextChanged(object? sender, EventArgs e)
{
Logger.TryGet(LogEventLevel.Debug, ImeLog)?.Log(null, "SurroundingTextChanged");
if (WeakInputDelegate == null)
if (WeakInputDelegate == null || _isInUpdate)
return;
_inSurroundingTextUpdateEvent++;
try
{
objc_msgSend(WeakInputDelegate.Handle.Handle, TextWillChange, Handle.Handle);
objc_msgSend(WeakInputDelegate.Handle.Handle, TextDidChange, Handle.Handle);
objc_msgSend(WeakInputDelegate.Handle.Handle, SelectionWillChange, this.Handle.Handle);
objc_msgSend(WeakInputDelegate.Handle.Handle, SelectionDidChange, this.Handle.Handle);
objc_msgSend(WeakInputDelegate.Handle.Handle, SelectionWillChange, Handle.Handle);
objc_msgSend(WeakInputDelegate.Handle.Handle, SelectionDidChange, Handle.Handle);
}
finally
{
Expand All @@ -160,6 +161,7 @@ private void SurroundingTextChanged(object? sender, EventArgs e)

private void KeyPress(Key key, PhysicalKey physicalKey, string? keySymbol)
{
_isInUpdate = true;
Logger.TryGet(LogEventLevel.Debug, ImeLog)?.Log(null, "Triggering key press {key}", key);

if (_view._topLevelImpl.Input is { } input)
Expand All @@ -170,12 +172,15 @@ private void KeyPress(Key key, PhysicalKey physicalKey, string? keySymbol)
input.Invoke(new RawKeyEventArgs(KeyboardDevice.Instance!, 0, _view.InputRoot,
RawKeyEventType.KeyUp, key, RawInputModifiers.None, physicalKey, keySymbol));
}
_isInUpdate = false;
}

private void TextInput(string text)
{
_isInUpdate = true;
Logger.TryGet(LogEventLevel.Debug, ImeLog)?.Log(null, "Triggering text input {text}", text);
_view._topLevelImpl.Input?.Invoke(new RawTextInputEventArgs(KeyboardDevice.Instance!, 0, _view.InputRoot, text));
_isInUpdate = false;
}

void IUIKeyInput.InsertText(string text)
Expand Down Expand Up @@ -214,23 +219,22 @@ string IUITextInput.TextInRange(UITextRange range)
var r = (AvaloniaTextRange)range;
var surroundingText = _client.SurroundingText;

var currentSelection = _client.Selection;

Logger.TryGet(LogEventLevel.Debug, ImeLog)?.Log(null, "IUIKeyInput.TextInRange {start} {end}", r.StartIndex, r.EndIndex);

string result = "";
if (string.IsNullOrEmpty(_markedText))
{
if(surroundingText != null && r.EndIndex < surroundingText.Length)
if (surroundingText != null && r.EndIndex <= surroundingText.Length)
{
result = surroundingText[r.StartIndex..r.EndIndex];
}
}
else
{
var span = new CombinedSpan3<char>(surroundingText.AsSpan().Slice(0, currentSelection.Start),
var currentSelection = _client.Selection;
var span = new CombinedSpan3<char>(surroundingText.AsSpan(0, currentSelection.Start),
_markedText,
surroundingText.AsSpan().Slice(currentSelection.Start, currentSelection.End - currentSelection.Start));
surroundingText.AsSpan(currentSelection.Start, currentSelection.End - currentSelection.Start));
var buf = new char[r.EndIndex - r.StartIndex];
span.CopyTo(buf, r.StartIndex);
result = new string(buf);
Expand Down
Loading