diff --git a/src/iOS/Avalonia.iOS/TextInputResponder.cs b/src/iOS/Avalonia.iOS/TextInputResponder.cs index b1575645c8a..5a83f482135 100644 --- a/src/iOS/Avalonia.iOS/TextInputResponder.cs +++ b/src/iOS/Avalonia.iOS/TextInputResponder.cs @@ -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; @@ -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 { @@ -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) @@ -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) @@ -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(surroundingText.AsSpan().Slice(0, currentSelection.Start), + var currentSelection = _client.Selection; + var span = new CombinedSpan3(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);