Skip to content

Commit

Permalink
Fixes for caret position (#128)
Browse files Browse the repository at this point in the history
Co-authored-by: jose.pereda <jose.pereda@gluonhq.com>
  • Loading branch information
jperedadnr and jperedadnr authored Mar 24, 2022
1 parent a1be8e4 commit 23baae4
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 26 deletions.
24 changes: 12 additions & 12 deletions src/main/java/com/gluonhq/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,20 +70,20 @@ public class Main extends Application {
}

private final List<DecorationModel> decorations = List.of(
new DecorationModel(0, 20, TextDecoration.builder().presets().fontWeight(FontWeight.BOLD).fontSize(14).build()),
new DecorationModel(20, 576, TextDecoration.builder().presets().build()),
new DecorationModel(596, 17, TextDecoration.builder().presets().fontWeight(FontWeight.BOLD).fontSize(14).build()),
new DecorationModel(613, 615, TextDecoration.builder().presets().build()),
new DecorationModel(1228, 24, TextDecoration.builder().presets().fontWeight(FontWeight.BOLD).fontSize(14).build()),
new DecorationModel(1252, 1060, TextDecoration.builder().presets().build())
new DecorationModel(0, 21, TextDecoration.builder().presets().fontWeight(FontWeight.BOLD).fontSize(14).build()),
new DecorationModel(21, 575, TextDecoration.builder().presets().build()),
new DecorationModel(596, 18, TextDecoration.builder().presets().fontWeight(FontWeight.BOLD).fontSize(14).build()),
new DecorationModel(614, 614, TextDecoration.builder().presets().build()),
new DecorationModel(1228, 25, TextDecoration.builder().presets().fontWeight(FontWeight.BOLD).fontSize(14).build()),
new DecorationModel(1253, 1059, TextDecoration.builder().presets().build())
);

private final Document document = new Document("What is Lorem Ipsum?" +
"\nLorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.\n" +
"Why do we use it?" +
"\nIt is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).\n" +
"Where does it come from?" +
"\nContrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of \"de Finibus Bonorum et Malorum\" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, \"Lorem ipsum dolor sit amet..\", comes from a line in section 1.10.32.\nThe standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from \"de Finibus Bonorum et Malorum\" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham.\n",
private final Document document = new Document("What is Lorem Ipsum?\n" +
"Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.\n" +
"Why do we use it?\n" +
"It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).\n" +
"Where does it come from?\n" +
"Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of \"de Finibus Bonorum et Malorum\" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, \"Lorem ipsum dolor sit amet..\", comes from a line in section 1.10.32.\nThe standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from \"de Finibus Bonorum et Malorum\" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham.\n",
decorations, 2312);

// private final List<DecorationModel> decorations = List.of(
Expand Down
18 changes: 16 additions & 2 deletions src/main/java/com/gluonhq/richtext/ParagraphTile.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ protected double computePrefHeight(double width) {
private final RichTextAreaViewModel viewModel;

private Paragraph paragraph;
private final double textFlowLayoutX = 1d, textFlowLayoutY = 1d;
private final double textFlowLayoutX, textFlowLayoutY;
private final ChangeListener<Number> caretPositionListener = (o, ocp, p) -> updateCaretPosition(p.intValue());
private final ChangeListener<Selection> selectionListener = (o, os, selection) -> updateSelection(selection);

Expand Down Expand Up @@ -95,11 +95,16 @@ public ParagraphTile(RichTextAreaSkin richTextAreaSkin) {
getChildren().add(root);
// TODO: expose padding as paragraph indentation
setPadding(new Insets(0, 0, 10, 0));
textFlowLayoutX = 1d + textFlow.getPadding().getLeft();
textFlowLayoutY = 1d + textFlow.getPadding().getTop();
}

void setParagraph(Paragraph paragraph) {
viewModel.caretPositionProperty().removeListener(caretPositionListener);
viewModel.selectionProperty().removeListener(selectionListener);
if (paragraph == null) {
return;
}
this.paragraph = paragraph;
viewModel.caretPositionProperty().addListener(caretPositionListener);
viewModel.selectionProperty().addListener(selectionListener);
Expand All @@ -109,6 +114,14 @@ TextFlow getTextFlow() {
return textFlow;
}

double getTextFlowLayoutX() {
return textFlowLayoutX;
}

double getTextFlowLayoutY() {
return textFlowLayoutY;
}

ObservableSet<Path> getTextBackgroundColorPaths() {
return textBackgroundColorPaths;
}
Expand All @@ -123,7 +136,8 @@ private void mousePressedListener(MouseEvent e) {
int prevCaretPosition = viewModel.getCaretPosition();
int insertionIndex = hitInfo.getInsertionIndex();
if (insertionIndex >= 0) {
int globalInsertionIndex = paragraph.getStart() + insertionIndex;
// get global insertion point, preventing insertionIndex after linefeed
int globalInsertionIndex = Math.min(paragraph.getStart() + insertionIndex, paragraph.getEnd() - 1);
if (!(e.isControlDown() || e.isAltDown() || e.isShiftDown() || e.isMetaDown() || e.isShortcutDown())) {
viewModel.setCaretPosition(globalInsertionIndex);
if (e.getClickCount() == 2) {
Expand Down
10 changes: 6 additions & 4 deletions src/main/java/com/gluonhq/richtext/RichListCell.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class RichListCell extends ListCell<Paragraph> {
setFont(MIN_LF_FONT);

paragraphTile = new ParagraphTile(richTextAreaSkin);
paragraphTile.getTextFlow().setPrefWidth(richTextAreaSkin.textFlowPrefWidthProperty.get()); //.subtract(textTile.getPadding().getLeft() + textTile.getPadding().getRight()));
paragraphTile.getTextFlow().setPrefWidth(richTextAreaSkin.textFlowPrefWidthProperty.get());
setText(null);

addEventHandler(MouseEvent.DRAG_DETECTED, event -> {
Expand Down Expand Up @@ -92,13 +92,15 @@ protected void updateItem(Paragraph item, boolean empty) {
}
}, item.getStart(), item.getEnd());
paragraphTile.getTextFlow().getChildren().setAll(fragments);
paragraphTile.getTextFlow().setPrefWidth(richTextAreaSkin.textFlowPrefWidthProperty.get()); //.subtract(textTile.getPadding().getLeft() + textTile.getPadding().getRight()));
paragraphTile.getTextFlow().setPrefWidth(richTextAreaSkin.textFlowPrefWidthProperty.get());
paragraphTile.setParagraph(item);
addBackgroundPathsToLayers(paragraphTile.getTextFlow(), paragraphTile.getTextBackgroundColorPaths(), backgroundIndexRanges);
setGraphic(paragraphTile);
// required: update caret and selection
paragraphTile.updateLayout();
} else {
// clean up listeners
paragraphTile.setParagraph(null);
setGraphic(null);

}
Expand All @@ -110,8 +112,8 @@ private void addBackgroundPathsToLayers(TextFlow textFlow, ObservableSet<Path> t
final Path path = new BackgroundColorPath(textFlow.rangeShape(indexRangeBackground.getStart(), indexRangeBackground.getEnd()));
path.setStrokeWidth(0);
path.setFill(indexRangeBackground.getColor());
// path.setLayoutX(textFlowLayoutX);
// path.setLayoutY(textFlowLayoutY);
path.setLayoutX(paragraphTile.getTextFlowLayoutX());
path.setLayoutY(paragraphTile.getTextFlowLayoutY());
return path;
})
.collect(Collectors.toMap(Path::getFill, Function.identity(), (p1, p2) -> {
Expand Down
16 changes: 8 additions & 8 deletions src/main/java/com/gluonhq/richtext/RichTextAreaSkin.java
Original file line number Diff line number Diff line change
Expand Up @@ -377,15 +377,15 @@ private void requestLayout() {
private int getNextRowPosition(double x, boolean down) {
ObservableList<Paragraph> items = paragraphListView.getItems();
int caretPosition = viewModel.getCaretPosition();
int paragraphWithCaretIndex = items.stream()
.filter(p -> p.getStart() <= caretPosition && caretPosition < p.getEnd())
.mapToInt(items::indexOf)
.findFirst()
.orElse(-1);
int nextRowPosition = paragraphListView.getNextRowPosition(x, down);
// when the caret is at the top or bottom of the paragraph, the
// next row position is the same
if (nextRowPosition == viewModel.getCaretPosition()) {
// if the caret is at the top or bottom of the paragraph:
if ((down && nextRowPosition <= viewModel.getCaretPosition()) ||
(!down && nextRowPosition >= viewModel.getCaretPosition())) {
int paragraphWithCaretIndex = items.stream()
.filter(p -> p.getStart() <= caretPosition && caretPosition < p.getEnd())
.mapToInt(items::indexOf)
.findFirst()
.orElse(-1);
if (down) {
// move to next paragraph
int nextIndex = Math.min(items.size() - 1, paragraphWithCaretIndex + 1);
Expand Down

0 comments on commit 23baae4

Please sign in to comment.