Skip to content

Commit 9032e63

Browse files
committed
Merge pull request #221 from JordanMartinez/characterHitAcess
Make StyledTextArea.hit(x, y) public.
2 parents 3b60b83 + 86fcbdc commit 9032e63

3 files changed

Lines changed: 20 additions & 4 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import java.util.OptionalInt;
44

5-
class CharacterHit {
5+
public class CharacterHit {
66

77
public static CharacterHit insertionAt(int insertionIndex) {
88
return new CharacterHit(OptionalInt.empty(), insertionIndex);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
import org.reactfx.value.Val;
3232
import org.reactfx.value.Var;
3333

34-
public class ParagraphBox<S, PS> extends Region {
34+
class ParagraphBox<S, PS> extends Region {
3535

3636
/**
3737
* An opaque class representing horizontal caret offset.
@@ -69,7 +69,7 @@ public ObjectProperty<IntFunction<? extends Node>> graphicFactoryProperty() {
6969
public void setIndex(int index) { this.index.setValue(index); }
7070
public int getIndex() { return index.getValue(); }
7171

72-
public ParagraphBox(Paragraph<S, PS> par, BiConsumer<? super TextExt, S> applyStyle, BiConsumer<TextFlow, PS> applyParagraphStyle) {
72+
ParagraphBox(Paragraph<S, PS> par, BiConsumer<? super TextExt, S> applyStyle, BiConsumer<TextFlow, PS> applyParagraphStyle) {
7373
this.getStyleClass().add("paragraph-box");
7474
this.text = new ParagraphText<>(par, applyStyle);
7575
applyParagraphStyle.accept(this.text, par.getParagraphStyle());

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -677,7 +677,23 @@ CharacterHit hit(ParagraphBox.CaretOffsetX x, double y) {
677677
}
678678
}
679679

680-
CharacterHit hit(double x, double y) {
680+
/**
681+
* Helpful for determining which letter is at point x, y:
682+
* <pre>
683+
* {@code
684+
* StyledTextArea area = // creation code
685+
* area.addEventHandler(MouseEvent.MOUSE_PRESSED, (MouseEvent e) -> {
686+
* CharacterHit hit = area.hit(e.getX(), e.getY());
687+
* int characterPosition = hit.getInsertionIndex();
688+
*
689+
* // move the caret to that character's position
690+
* area.moveTo(characterPosition, SelectionPolicy.CLEAR);
691+
* }}
692+
* </pre>
693+
* @param x
694+
* @param y
695+
*/
696+
public CharacterHit hit(double x, double y) {
681697
VirtualFlowHit<Cell<Paragraph<S, PS>, ParagraphBox<S, PS>>> hit = virtualFlow.hit(x, y);
682698
if(hit.isBeforeCells()) {
683699
return CharacterHit.insertionAt(0);

0 commit comments

Comments
 (0)