When hitting a Text node, there is no way to distinguish between a hit to the trailing edge of the last character and a hit beyond the end of text. Both return a HitInfo
charIndex: n-1, isLeading: false
where n is the length of the text. The code to showcase the hit beyond the end of text is below.
For a hit beyond the end of text, I would suggest to return a HitInfo
charIndex: n, isLeading: true
instead. This resolves to the same insertion index, but provides a way to tell apart the two cases.
Note that making this change also resolvesRT-37801.
I realize that impl_* methods are not public API, but I'm using them in RichTextFX [1] anyway because there's no public API for this functionality. If you are planning/willing to expose more public API on Text/TextFlow, I will be happy to summarize what private API is currently used in RichTextFX.
import javafx.geometry.Point2D;
import javafx.scene.text.Text;
import com.sun.javafx.scene.text.HitInfo;
public class HitEndOfText {
public static void main(String[] args) {
Text text = new Text("foo");
HitInfo hit = text.impl_hitTestChar(new Point2D(9999, 1));
System.out.println(hit);
// prints: charIndex: 2, isLeading: false
// expected: charIndex: 3, isLeading: true
}
}
[1] https://github.com/TomasMikula/RichTextFX
charIndex: n-1, isLeading: false
where n is the length of the text. The code to showcase the hit beyond the end of text is below.
For a hit beyond the end of text, I would suggest to return a HitInfo
charIndex: n, isLeading: true
instead. This resolves to the same insertion index, but provides a way to tell apart the two cases.
Note that making this change also resolves
I realize that impl_* methods are not public API, but I'm using them in RichTextFX [1] anyway because there's no public API for this functionality. If you are planning/willing to expose more public API on Text/TextFlow, I will be happy to summarize what private API is currently used in RichTextFX.
import javafx.geometry.Point2D;
import javafx.scene.text.Text;
import com.sun.javafx.scene.text.HitInfo;
public class HitEndOfText {
public static void main(String[] args) {
Text text = new Text("foo");
HitInfo hit = text.impl_hitTestChar(new Point2D(9999, 1));
System.out.println(hit);
// prints: charIndex: 2, isLeading: false
// expected: charIndex: 3, isLeading: true
}
}
[1] https://github.com/TomasMikula/RichTextFX
- relates to
-
JDK-8136350 [TextFlow] Hit test API for TextFlow
- Resolved
-
JDK-8300569 ☂ Missing APIs related to rich text control
- In Progress
-
JDK-8090357 [Text] Make public API's for hit_test/selection
- Resolved
-
JDK-8136971 Review the new TextPosInfo API
- Resolved
-
JDK-8341670 [Text,TextFlow] Public API for Text Layout Info
- In Progress
-
JDK-8098258 [Text] Hitting an empty Text returns insertion index out of text bounds
- Resolved
(1 relates to)