Skip to content

Commit

Permalink
Expose row and column position of caret (#376)
Browse files Browse the repository at this point in the history
  • Loading branch information
jperedadnr authored Jan 14, 2025
1 parent fab017f commit eee9090
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
15 changes: 15 additions & 0 deletions rta/src/main/java/com/gluonhq/richtextarea/RichTextArea.java
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,21 @@ public final Point2D getCaretOrigin() {
return caretOriginProperty.get();
}

/**
* A Point2D, with x for column and y for row, with integer values, for a given position of the caret.
* Column is defined by the number of characters from the beginning of the paragraph to the current position
* of the caret, starting in 0 (or -1 if the control has no text).
* Row is defined by the number of paragraph the caret is placed in, starting in 0 (or -1 if the control
* has no text).
*/
final ReadOnlyObjectWrapper<Point2D> caretRowColumnProperty = new ReadOnlyObjectWrapper<>(this, "caretRowColumn", Point2D.ZERO);
public final ReadOnlyObjectProperty<Point2D> caretRowColumnProperty() {
return caretRowColumnProperty.getReadOnlyProperty();
}
public final Point2D getCaretRowColumn() {
return caretRowColumnProperty.get();
}

/**
* The current decoration at the caret.
*/
Expand Down
14 changes: 13 additions & 1 deletion rta/src/main/java/com/gluonhq/richtextarea/RichTextAreaSkin.java
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,7 @@ protected void invalidated() {
getSkinnable().decorationAtParagraph.bind(viewModel.decorationAtParagraphProperty());
caretPositionProperty.bind(viewModel.caretPositionProperty());
getSkinnable().caretOriginProperty.bind(caretOriginProperty);
getSkinnable().caretRowColumnProperty.bind(caretRowColumnProperty);
promptNode.visibleProperty().bind(promptVisibleBinding);
promptNode.fontProperty().bind(promptFontBinding);
} else {
Expand All @@ -404,6 +405,7 @@ protected void invalidated() {
getSkinnable().decorationAtParagraph.unbind();
caretPositionProperty.unbind();
getSkinnable().caretOriginProperty.unbind();
getSkinnable().caretRowColumnProperty.unbind();
promptNode.visibleProperty().unbind();
promptNode.fontProperty().unbind();
}
Expand All @@ -425,7 +427,17 @@ protected void invalidated() {
}
};

final ObjectProperty<Point2D> caretOriginProperty = new SimpleObjectProperty<>(this, "caretOrigin", DEFAULT_POINT_2D);
final ObjectProperty<Point2D> caretOriginProperty = new SimpleObjectProperty<>(this, "caretOrigin", DEFAULT_POINT_2D) {
@Override
protected void invalidated() {
viewModel.getParagraphWithCaret().ifPresentOrElse(p -> {
int row = viewModel.getParagraphList().indexOf(p);
int col = caretPositionProperty.get() - p.getStart();
caretRowColumnProperty.set(new Point2D(col, row));
}, () -> caretRowColumnProperty.set(DEFAULT_POINT_2D));
}
};
private final ObjectProperty<Point2D> caretRowColumnProperty = new SimpleObjectProperty<>(this, "caretRowColumn", DEFAULT_POINT_2D);

private final ObjectBinding<Font> promptFontBinding = Bindings.createObjectBinding(this::getPromptNodeFont,
viewModel.decorationAtCaretProperty(), viewModel.decorationAtParagraphProperty());
Expand Down

0 comments on commit eee9090

Please sign in to comment.