Skip to content

Commit

Permalink
Merge pull request #221 from JordanMartinez/characterHitAcess
Browse files Browse the repository at this point in the history
Make StyledTextArea.hit(x, y) public.
  • Loading branch information
TomasMikula committed Dec 4, 2015
2 parents 3b60b83 + 86fcbdc commit 9032e63
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import java.util.OptionalInt;

class CharacterHit {
public class CharacterHit {

public static CharacterHit insertionAt(int insertionIndex) {
return new CharacterHit(OptionalInt.empty(), insertionIndex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import org.reactfx.value.Val;
import org.reactfx.value.Var;

public class ParagraphBox<S, PS> extends Region {
class ParagraphBox<S, PS> extends Region {

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

public ParagraphBox(Paragraph<S, PS> par, BiConsumer<? super TextExt, S> applyStyle, BiConsumer<TextFlow, PS> applyParagraphStyle) {
ParagraphBox(Paragraph<S, PS> par, BiConsumer<? super TextExt, S> applyStyle, BiConsumer<TextFlow, PS> applyParagraphStyle) {
this.getStyleClass().add("paragraph-box");
this.text = new ParagraphText<>(par, applyStyle);
applyParagraphStyle.accept(this.text, par.getParagraphStyle());
Expand Down
18 changes: 17 additions & 1 deletion richtextfx/src/main/java/org/fxmisc/richtext/StyledTextArea.java
Original file line number Diff line number Diff line change
Expand Up @@ -677,7 +677,23 @@ CharacterHit hit(ParagraphBox.CaretOffsetX x, double y) {
}
}

CharacterHit hit(double x, double y) {
/**
* Helpful for determining which letter is at point x, y:
* <pre>
* {@code
* StyledTextArea area = // creation code
* area.addEventHandler(MouseEvent.MOUSE_PRESSED, (MouseEvent e) -> {
* CharacterHit hit = area.hit(e.getX(), e.getY());
* int characterPosition = hit.getInsertionIndex();
*
* // move the caret to that character's position
* area.moveTo(characterPosition, SelectionPolicy.CLEAR);
* }}
* </pre>
* @param x
* @param y
*/
public CharacterHit hit(double x, double y) {
VirtualFlowHit<Cell<Paragraph<S, PS>, ParagraphBox<S, PS>>> hit = virtualFlow.hit(x, y);
if(hit.isBeforeCells()) {
return CharacterHit.insertionAt(0);
Expand Down

0 comments on commit 9032e63

Please sign in to comment.