Skip to content

Commit 27ced36

Browse files
committed
fixed caret positioning in wrapped lines (issue FXMisc#423)
1 parent cc21b32 commit 27ced36

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

richtextfx/src/main/java/org/fxmisc/richtext/TextFlowExt.java

+16-3
Original file line numberDiff line numberDiff line change
@@ -141,23 +141,36 @@ CharacterHit hitLine(double x, int lineIndex) {
141141
CharacterHit hit(double x, double y) {
142142
HitInfo hit = textLayout().getHitInfo((float) x, (float) y);
143143
int charIdx = hit.getCharIndex();
144+
boolean leading = hit.isLeading();
144145

145146
int lineIdx = getLineIndex((float) y);
146147
if(lineIdx >= getLineCount()) {
147148
return CharacterHit.insertionAt(getCharCount());
148149
}
149150

150-
TextLine line = getLines()[lineIdx];
151+
TextLine[] lines = getLines();
152+
TextLine line = lines[lineIdx];
151153
RectBounds lineBounds = line.getBounds();
152154

155+
// If this is a wrapped paragraph and hit character is at end of hit line,
156+
// make sure that the "character hit" stays at the end of the hit line
157+
// (and not at the beginning of the next line).
158+
if(lines.length > 1 &&
159+
lineIdx < lines.length - 1 &&
160+
charIdx + 1 >= line.getStart() + line.getLength() &&
161+
!leading)
162+
{
163+
leading = true;
164+
}
165+
153166
if(x < lineBounds.getMinX() || x > lineBounds.getMaxX()) {
154-
if(hit.isLeading()) {
167+
if(leading) {
155168
return CharacterHit.insertionAt(charIdx);
156169
} else {
157170
return CharacterHit.insertionAt(charIdx + 1);
158171
}
159172
} else {
160-
if(hit.isLeading()) {
173+
if(leading) {
161174
return CharacterHit.leadingHalfOf(charIdx);
162175
} else {
163176
return CharacterHit.trailingHalfOf(charIdx);

0 commit comments

Comments
 (0)