From dafa53a49c530ec933b08c8fde939fadb05168da Mon Sep 17 00:00:00 2001 From: kerams Date: Mon, 2 Mar 2026 13:03:21 +0100 Subject: [PATCH] Don't throw on unexpected iOS input (#20728) * Don't throw on unexpected iOS input * Improve normalization --- src/iOS/Avalonia.iOS/TextInputResponder.cs | 27 +++++++++------------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/src/iOS/Avalonia.iOS/TextInputResponder.cs b/src/iOS/Avalonia.iOS/TextInputResponder.cs index a16a381522..678fac8766 100644 --- a/src/iOS/Avalonia.iOS/TextInputResponder.cs +++ b/src/iOS/Avalonia.iOS/TextInputResponder.cs @@ -34,14 +34,10 @@ partial class AvaloniaView public AvaloniaTextRange(int startIndex, int endIndex) { - if (startIndex < 0) - throw new ArgumentOutOfRangeException(nameof(startIndex)); - - if (endIndex < startIndex) - throw new ArgumentOutOfRangeException(nameof(endIndex)); - - StartIndex = startIndex; - EndIndex = endIndex; + var a = Math.Max(0, startIndex); + var b = Math.Max(0, endIndex); + StartIndex = Math.Min(a, b); + EndIndex = Math.Max(a, b); } public override bool IsEmpty => StartIndex == EndIndex; @@ -58,9 +54,7 @@ partial class AvaloniaView { public AvaloniaTextPosition(int index) { - if (index < 0) - throw new ArgumentOutOfRangeException(nameof(index)); - Index = index; + Index = Math.Max(0, index); } public int Index { get; } @@ -509,14 +503,15 @@ partial class AvaloniaView { if (_inSurroundingTextUpdateEvent > 0) return; - if (value == null) - _client.Selection = default; - else - { - var r = (AvaloniaTextRange)value; + if (value is AvaloniaTextRange r) + { _client.Selection = new TextSelection(r.StartIndex, r.EndIndex); } + else + { + _client.Selection = default; + } } }