Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose row and column position of caret #376

Merged
merged 1 commit into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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