Skip to content

Commit a9dec62

Browse files
committed
Fix some failing tests
1 parent 2cc9524 commit a9dec62

File tree

5 files changed

+32
-19
lines changed

5 files changed

+32
-19
lines changed

src/Avalonia.Controls/Presenters/TextPresenter.cs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,6 @@ public int CaretIndex
215215
set
216216
{
217217
value = CoerceCaretIndex(value);
218-
_caretBounds = TextLayout.GetRectFromCharacterIndex(value, true);
219218
SetAndRaise(CaretIndexProperty, ref _caretIndex, value);
220219
}
221220
}
@@ -281,7 +280,7 @@ public int SelectionEnd
281280
public int GetCaretIndex(Point point)
282281
{
283282
var hit = TextLayout.HitTestPoint(point);
284-
_caretBounds = hit.CaretBounds;
283+
_caretBounds = TextLayout.GetRectFromCharacterIndex(hit.TextPosition, hit.IsTrailing);
285284
_navigationPosition = _caretBounds.Position;
286285
return hit.TextPosition;
287286
}
@@ -485,15 +484,15 @@ protected virtual TextLayout CreateTextLayout()
485484

486485
IReadOnlyList<ValueSpan<TextRunProperties>> textStyleOverrides = null;
487486

488-
/*if (length > 0)
487+
if (length > 0)
489488
{
490489
textStyleOverrides = new[]
491490
{
492491
new ValueSpan<TextRunProperties>(start, length,
493492
new GenericTextRunProperties(typeface, FontSize,
494493
foregroundBrush: SelectionForegroundBrush ?? Brushes.White))
495494
};
496-
}*/
495+
}
497496

498497
if (PasswordChar != default(char) && !RevealPassword)
499498
{
@@ -599,12 +598,16 @@ public void MoveCaretVertical(LogicalDirection direction = LogicalDirection.Forw
599598

600599
CaretIndex = caretIndex;
601600

601+
_caretBounds = TextLayout.GetRectFromCharacterIndex(caretIndex, false);
602+
602603
_navigationPosition = _navigationPosition.WithY(_caretBounds.Y);
603604
}
604605

605606
public void MoveCaretHorizontal(LogicalDirection direction = LogicalDirection.Forward)
606607
{
607608
var caretIndex = CaretIndex;
609+
610+
CharacterHit characterHit = default;
608611

609612
var lineIndex = TextLayout.GetLineIndexFromCharacterIndex(caretIndex);
610613

@@ -619,10 +622,17 @@ public void MoveCaretHorizontal(LogicalDirection direction = LogicalDirection.Fo
619622
{
620623
var textLine = TextLayout.TextLines[lineIndex];
621624

622-
var characterHit = textLine.GetNextCaretCharacterHit(new CharacterHit(caretIndex));
625+
characterHit = textLine.GetNextCaretCharacterHit(new CharacterHit(caretIndex));
623626

624627
caretIndex = characterHit.FirstCharacterIndex + characterHit.TrailingLength;
625628

629+
if (caretIndex - textLine.TrailingWhitespaceLength == textLine.TextRange.End)
630+
{
631+
characterHit = new CharacterHit(caretIndex);
632+
633+
break;
634+
}
635+
626636
if (caretIndex <= CaretIndex)
627637
{
628638
lineIndex++;
@@ -639,7 +649,7 @@ public void MoveCaretHorizontal(LogicalDirection direction = LogicalDirection.Fo
639649
{
640650
var textLine = TextLayout.TextLines[lineIndex];
641651

642-
var characterHit = textLine.GetPreviousCaretCharacterHit(new CharacterHit(caretIndex));
652+
characterHit = textLine.GetPreviousCaretCharacterHit(new CharacterHit(caretIndex));
643653

644654
caretIndex = characterHit.FirstCharacterIndex + characterHit.TrailingLength;
645655

@@ -656,6 +666,8 @@ public void MoveCaretHorizontal(LogicalDirection direction = LogicalDirection.Fo
656666

657667
CaretIndex = caretIndex;
658668

669+
_caretBounds = TextLayout.GetRectFromCharacterIndex(caretIndex, characterHit.TrailingLength > 0);
670+
659671
_navigationPosition = _caretBounds.Position;
660672
}
661673

src/Avalonia.Visuals/Media/TextFormatting/TextFormatterImpl.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ internal static SplitTextRunsResult SplitTextRuns(List<ShapedTextCharacters> tex
219219

220220
if (second != null)
221221
{
222-
var offset = currentRun.GlyphRun.Characters.Length > 1 ? 1 : 0;
222+
var offset = currentRun.GlyphRun.Characters.Length >= 1 ? 1 : 0;
223223

224224
for (var j = 0; j < secondCount; j++)
225225
{
@@ -514,7 +514,10 @@ private static TextLine PerformTextWrapping(List<ShapedTextCharacters> textRuns,
514514
break;
515515
}
516516

517-
lastWrapPosition = currentLength + lineBreaker.Current.PositionWrap;
517+
if (lineBreaker.Current.PositionMeasure != lineBreaker.Current.PositionWrap)
518+
{
519+
lastWrapPosition = currentLength + lineBreaker.Current.PositionWrap;
520+
}
518521
}
519522

520523
if (!breakFound)

src/Avalonia.Visuals/Media/TextFormatting/TextLayout.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ private TextHitTestResult GetHitTestResult(TextLine textLine, CharacterHit chara
262262
var lastTrailingIndex = textLine.TextRange.Start + textLine.TextRange.Length;
263263

264264
var isInside = x >= 0 && x <= textLine.Width && y >= 0 && y <= textLine.Height;
265-
265+
266266
if (x >= textLine.Width && textLine.TextRange.Length > 0 && textLine.NewLineLength > 0)
267267
{
268268
lastTrailingIndex -= textLine.NewLineLength;
@@ -272,7 +272,7 @@ private TextHitTestResult GetHitTestResult(TextLine textLine, CharacterHit chara
272272

273273
var isTrailing = lastTrailingIndex == textPosition && characterHit.TrailingLength > 0 ||
274274
y > Size.Height;
275-
275+
276276
if (textPosition == textLine.TextRange.Start + textLine.TextRange.Length)
277277
{
278278
textPosition -= textLine.NewLineLength;
@@ -528,7 +528,8 @@ public Rect GetRectFromCharacterIndex(int characterIndex, bool trailingEdge)
528528

529529
foreach (var textLine in TextLines)
530530
{
531-
if (currentIndex + textLine.TextRange.Length < characterIndex)
531+
if (currentIndex + textLine.TextRange.Length <= characterIndex && !trailingEdge ||
532+
currentIndex + textLine.TextRange.Length < characterIndex)
532533
{
533534
distanceY += textLine.Height;
534535

src/Avalonia.Visuals/Media/TextHitTestResult.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,6 @@ public class TextHitTestResult
1414
/// Gets the index of the hit character in the text.
1515
/// </summary>
1616
public int TextPosition { get; set; }
17-
18-
/// <summary>
19-
/// Gets the caret position.
20-
/// </summary>
21-
public Rect CaretBounds { get; set; }
2217

2318
/// <summary>
2419
/// Gets a value indicating whether the hit is on the trailing edge of the character.

tests/Avalonia.Skia.UnitTests/Media/TextFormatting/TextLayoutTests.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,10 @@ public void Should_Apply_TextStyleSpan_To_Text_In_Between()
6767
}
6868
}
6969

70-
[Fact]
71-
public void Should_Wrap_And_Apply_Style()
70+
[InlineData(27)]
71+
[InlineData(22)]
72+
[Theory]
73+
public void Should_Wrap_And_Apply_Style(int length)
7274
{
7375
using (Start())
7476
{
@@ -89,7 +91,7 @@ public void Should_Wrap_And_Apply_Style()
8991

9092
var spans = new[]
9193
{
92-
new ValueSpan<TextRunProperties>(0, 27,
94+
new ValueSpan<TextRunProperties>(0, length,
9395
new GenericTextRunProperties(Typeface.Default, 12, foregroundBrush: foreground))
9496
};
9597

0 commit comments

Comments
 (0)