Fix TextBox caret overlapping last glyph at end of line#21059
Open
jdmichel wants to merge 1 commit intoAvaloniaUI:masterfrom
Open
Fix TextBox caret overlapping last glyph at end of line#21059jdmichel wants to merge 1 commit intoAvaloniaUI:masterfrom
jdmichel wants to merge 1 commit intoAvaloniaUI:masterfrom
Conversation
GetCaretPoints() subtracted 1px from the caret X position when the caret was at or past the line width, to prevent clipping at the control boundary. This caused the caret to render on top of the last character instead of after it — visible at all font sizes and font families. Fix: replace "x -= 1" with "x = Math.Ceiling(X) + 0.5" so the caret snaps to the next pixel boundary past the glyph. Reserve 2px of extra width in MeasureOverride so the caret is not clipped. Fixes AvaloniaUI#12809
Contributor
|
A real fix to this issue is drawing the caret via an adorner layer |
Collaborator
|
|
You can test this PR using the following package version. |
Author
|
@cla-avalonia agree |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
GetCaretPoints()subtracts 1px from the caret X position when the caretis at or past the line width, to prevent clipping at the control boundary.
This causes the caret to render on top of the last character instead
of after it — visible at all font sizes and font families.
Changes
GetCaretPoints(): Replacex -= 1withx = Math.Ceiling(X) + 0.5so the caret snaps to the next pixelboundary past the glyph, guaranteeing visible separation.
MeasureOverride(): Reserve 2 extra pixels of width(
textWidth + 2) so the caret is not clipped by the control boundary.Diagnostic evidence
Minimal repro app traces
_lastCharacterHitand_caretBoundsviareflection. For text "rrrrrrr" at FontSize 23:
The CharacterHit logic resolves correctly. The only corruption is
x -= 1inGetCaretPoints():Repro
Any Avalonia app with a TextBox — type text and observe the caret at
the end of the string overlaps the last character. Tested on Windows
with Avalonia 11.3.12, 11.3.13, .NET 9, .NET 10, multiple fonts and
font sizes.
Fixes #12809