Skip to content

Commit

Permalink
android - fix selection bounds in TextEditBuffer (#16970)
Browse files Browse the repository at this point in the history
  • Loading branch information
emmauss authored Sep 12, 2024
1 parent a31ff27 commit 94e81ba
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions src/Android/Avalonia.Android/Platform/Input/TextEditBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,13 @@ public TextEditBuffer(IAndroidInputMethod textInputMethod, TopLevelImpl topLevel

public TextSelection Selection
{
get => _textInputMethod.Client?.Selection ?? default; set
get
{
var selection = _textInputMethod.Client?.Selection ?? default;
return new TextSelection(Math.Min(selection.Start, selection.End), Math.Max(selection.Start, selection.End));
}

set
{
if (_textInputMethod.Client is { } client)
client.Selection = value;
Expand All @@ -39,7 +45,7 @@ public TextSelection? Composition
var text = Text;
var start = Math.Clamp(v.Start, 0, text.Length);
var end = Math.Clamp(v.End, 0, text.Length);
_composition = new TextSelection(start, end);
_composition = new TextSelection(Math.Min(start, end), Math.Max(start, end));
}
else
_composition = null;
Expand All @@ -55,7 +61,9 @@ public string? SelectedText
return "";
}

return client.SurroundingText.Substring(Selection.Start, Selection.End - Selection.Start);
var selection = Selection;

return client.SurroundingText.Substring(selection.Start, selection.End - selection.Start);
}
}

Expand All @@ -71,9 +79,9 @@ public string? ComposingText
}
else
{
var start = Selection.Start;
Replace(start, Selection.End, value ?? "");
Composition = new TextSelection(start, start + (value?.Length ?? 0));
var selection = Selection;
Replace(selection.Start, selection.End, value ?? "");
Composition = new TextSelection(selection.Start, selection.Start + (value?.Length ?? 0));
}
}
}
Expand Down

0 comments on commit 94e81ba

Please sign in to comment.