Repository navigation
Fix caret position with centered text - #22340
Conversation
Gillibald
left a comment
There was a problem hiding this comment.
LGTM with some small nit mentioned in the comments
- Arrange already recreates the layout when the constraint changes, so the explicit getter call added nothing
NuGet packagesThe packages for this pull request have been published (workflow run):
|
|
Thanks for this. The same regression from AvaloniaUI/Avalonia#22176 shows up with left-aligned, wrapped text too (TextWrapping="Wrap"), not only centered text. When a line's last word fits in Math.Ceiling(finalWidth) but not in the measure width, the drawn layout and the caret's layout wrap differently. Every caret on the following lines is then drawn a word to the right, and Up/Down move from the wrong spot. Seen on 12.1.3 on Windows, with a TextBox inside a LayoutTransformControl at 0.85 scale and 225% DPI: a caret at the start of line 2 was drawn after the first word. Re-placing the caret on the drawn layout after layout (what this PR does in ArrangeOverride) fixes it in our app. We'd welcome the 12.1.x backport. |
|
* Update caret bounds after TextLayout recreation * Drop redundant TextLayout access from the centered caret test - Arrange already recreates the layout when the constraint changes, so the explicit getter call added nothing --------- Co-authored-by: Benedikt Stebner <Gillibald@users.noreply.github.com>
|
@cla-avalonia agree |
* Update caret bounds after TextLayout recreation * Drop redundant TextLayout access from the centered caret test - Arrange already recreates the layout when the constraint changes, so the explicit getter call added nothing --------- Co-authored-by: Benedikt Stebner <Gillibald@users.noreply.github.com>
What does the pull request do?
Fixes incorrect caret positioning in
TextBoxwhenTextAlignmentis set toCenterand the final arranged width differs from the width used during measure.Adds a regression test covering the case where the
TextLayoutis recreated during arrange.What is the current behavior?
TextBoxCaretRepro.2026-09-29.11-00-59.mp4
What is the updated/expected behavior with this PR?
The caret position is recalculated against the same
TextLayoutthat is used to render the text.As a result, the caret remains correctly positioned when the text is centered, including after the layout is recreated with a different arranged width.
How was the solution implemented (if it's not obvious)?
TextPresenteralready marks the caret bounds as dirty whenever a newTextLayoutis created.Previously, when
ArrangeOverrideinvalidated and recreated the layout due to a changed final width, the dirty caret bounds were not recalculated before the caret was rendered.The fix ensures that the caret bounds are recalculated against the newly created layout after the arranged width changes.
Checklist
Breaking changes
None.
Obsoletions / Deprecations
None.
Fixed issues
Regression introduced by PR #22176.