|
| 1 | +package com.gluonhq.richtext; |
| 2 | + |
| 3 | +import com.gluonhq.richtext.model.Paragraph; |
| 4 | +import com.gluonhq.richtext.viewmodel.RichTextAreaViewModel; |
| 5 | +import javafx.animation.KeyFrame; |
| 6 | +import javafx.animation.Timeline; |
| 7 | +import javafx.beans.value.ChangeListener; |
| 8 | +import javafx.collections.FXCollections; |
| 9 | +import javafx.collections.ObservableSet; |
| 10 | +import javafx.collections.SetChangeListener; |
| 11 | +import javafx.geometry.Bounds; |
| 12 | +import javafx.geometry.Insets; |
| 13 | +import javafx.geometry.Point2D; |
| 14 | +import javafx.scene.Group; |
| 15 | +import javafx.scene.image.Image; |
| 16 | +import javafx.scene.image.ImageView; |
| 17 | +import javafx.scene.input.MouseButton; |
| 18 | +import javafx.scene.input.MouseEvent; |
| 19 | +import javafx.scene.layout.HBox; |
| 20 | +import javafx.scene.layout.Pane; |
| 21 | +import javafx.scene.shape.Path; |
| 22 | +import javafx.scene.shape.PathElement; |
| 23 | +import javafx.scene.text.Font; |
| 24 | +import javafx.scene.text.HitInfo; |
| 25 | +import javafx.scene.text.Text; |
| 26 | +import javafx.scene.text.TextAlignment; |
| 27 | +import javafx.scene.text.TextFlow; |
| 28 | +import javafx.util.Duration; |
| 29 | + |
| 30 | +import java.util.ArrayList; |
| 31 | +import java.util.List; |
| 32 | +import java.util.Objects; |
| 33 | +import java.util.Set; |
| 34 | +import java.util.stream.Collectors; |
| 35 | + |
| 36 | +public class ParagraphTile extends HBox { |
| 37 | + |
| 38 | + private final Timeline caretTimeline = new Timeline( |
| 39 | + new KeyFrame(Duration.ZERO , e -> setCaretVisibility(false)), |
| 40 | + new KeyFrame(Duration.seconds(0.5), e -> setCaretVisibility(true)), |
| 41 | + new KeyFrame(Duration.seconds(1.0)) |
| 42 | + ); |
| 43 | + |
| 44 | + private final Pane root; |
| 45 | + private final Group layers; |
| 46 | + private final ObservableSet<Path> textBackgroundColorPaths = FXCollections.observableSet(); |
| 47 | + private final Path caretShape = new Path(); |
| 48 | + private final Path selectionShape = new Path(); |
| 49 | + private final TextFlow textFlow = new TextFlow() { |
| 50 | + @Override |
| 51 | + protected double computePrefHeight(double width) { |
| 52 | + double prefHeight = super.computePrefHeight(width); |
| 53 | + // take into account caret |
| 54 | + root.setPrefHeight(prefHeight + 1); |
| 55 | + return prefHeight; |
| 56 | + } |
| 57 | + }; |
| 58 | + |
| 59 | + private final RichTextArea control; |
| 60 | + private final RichTextAreaSkin richTextAreaSkin; |
| 61 | + private final RichTextAreaViewModel viewModel; |
| 62 | + |
| 63 | + private Paragraph paragraph; |
| 64 | + private final double textFlowLayoutX = 1d, textFlowLayoutY = 1d; |
| 65 | + private final ChangeListener<Number> caretPositionListener = (o, ocp, p) -> updateCaretPosition(p.intValue()); |
| 66 | + private final ChangeListener<Selection> selectionListener = (o, os, selection) -> updateSelection(selection); |
| 67 | + |
| 68 | + public ParagraphTile(RichTextAreaSkin richTextAreaSkin) { |
| 69 | + this.richTextAreaSkin = richTextAreaSkin; |
| 70 | + this.control = richTextAreaSkin.getSkinnable(); |
| 71 | + this.viewModel = richTextAreaSkin.getViewModel(); |
| 72 | + getStyleClass().setAll("paragraph-tile"); |
| 73 | + caretTimeline.setCycleCount(Timeline.INDEFINITE); |
| 74 | + textFlow.setFocusTraversable(false); |
| 75 | + textFlow.getStyleClass().setAll("text-flow"); |
| 76 | + textFlow.setOnMousePressed(this::mousePressedListener); |
| 77 | + // TODO: Expose paragraph justification |
| 78 | + textFlow.setTextAlignment(TextAlignment.LEFT); |
| 79 | + // TODO: Expose paragraph spacing |
| 80 | + textFlow.setLineSpacing(0); |
| 81 | + |
| 82 | + caretShape.setFocusTraversable(false); |
| 83 | + caretShape.getStyleClass().add("caret"); |
| 84 | + |
| 85 | + selectionShape.getStyleClass().setAll("selection"); |
| 86 | + |
| 87 | + layers = new Group(selectionShape, caretShape, textFlow); |
| 88 | + layers.getStyleClass().add("layers"); |
| 89 | + root = new Pane(layers); |
| 90 | + root.setPadding(new Insets(1)); |
| 91 | + root.getStyleClass().setAll("content-area"); |
| 92 | + layers.getChildren().addAll(0, textBackgroundColorPaths); |
| 93 | + textBackgroundColorPaths.addListener(this::updateLayers); |
| 94 | + // TODO: add left label for list decoration, line number, indentation |
| 95 | + getChildren().add(root); |
| 96 | + // TODO: expose padding as paragraph indentation |
| 97 | + setPadding(new Insets(0, 0, 10, 0)); |
| 98 | + } |
| 99 | + |
| 100 | + void setParagraph(Paragraph paragraph) { |
| 101 | + viewModel.caretPositionProperty().removeListener(caretPositionListener); |
| 102 | + viewModel.selectionProperty().removeListener(selectionListener); |
| 103 | + this.paragraph = paragraph; |
| 104 | + viewModel.caretPositionProperty().addListener(caretPositionListener); |
| 105 | + viewModel.selectionProperty().addListener(selectionListener); |
| 106 | + } |
| 107 | + |
| 108 | + TextFlow getTextFlow() { |
| 109 | + return textFlow; |
| 110 | + } |
| 111 | + |
| 112 | + ObservableSet<Path> getTextBackgroundColorPaths() { |
| 113 | + return textBackgroundColorPaths; |
| 114 | + } |
| 115 | + |
| 116 | + private void mousePressedListener(MouseEvent e) { |
| 117 | + if (control.isDisabled()) { |
| 118 | + return; |
| 119 | + } |
| 120 | + if (e.getButton() == MouseButton.PRIMARY && !(e.isMiddleButtonDown() || e.isSecondaryButtonDown())) { |
| 121 | + HitInfo hitInfo = textFlow.hitTest(new Point2D(e.getX() - textFlowLayoutX, e.getY() - textFlowLayoutY)); |
| 122 | + Selection prevSelection = viewModel.getSelection(); |
| 123 | + int prevCaretPosition = viewModel.getCaretPosition(); |
| 124 | + int insertionIndex = hitInfo.getInsertionIndex(); |
| 125 | + if (insertionIndex >= 0) { |
| 126 | + int globalInsertionIndex = paragraph.getStart() + insertionIndex; |
| 127 | + if (!(e.isControlDown() || e.isAltDown() || e.isShiftDown() || e.isMetaDown() || e.isShortcutDown())) { |
| 128 | + viewModel.setCaretPosition(globalInsertionIndex); |
| 129 | + if (e.getClickCount() == 2) { |
| 130 | + viewModel.selectCurrentWord(); |
| 131 | + } else if (e.getClickCount() == 3) { |
| 132 | + viewModel.selectCurrentParagraph(); |
| 133 | + } else { |
| 134 | + richTextAreaSkin.dragStart = globalInsertionIndex; |
| 135 | + viewModel.clearSelection(); |
| 136 | + } |
| 137 | + } else if (e.isShiftDown() && e.getClickCount() == 1 && !(e.isControlDown() || e.isAltDown() || e.isMetaDown() || e.isShortcutDown())) { |
| 138 | + int pos = prevSelection.isDefined() ? |
| 139 | + globalInsertionIndex < prevSelection.getStart() ? prevSelection.getEnd() : prevSelection.getStart() : |
| 140 | + prevCaretPosition; |
| 141 | + viewModel.setSelection(new Selection(pos, globalInsertionIndex)); |
| 142 | + viewModel.setCaretPosition(globalInsertionIndex); |
| 143 | + } |
| 144 | + } |
| 145 | + control.requestFocus(); |
| 146 | + e.consume(); |
| 147 | + } |
| 148 | + if (richTextAreaSkin.contextMenu.isShowing()) { |
| 149 | + richTextAreaSkin.contextMenu.hide(); |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + void mouseDraggedListener(MouseEvent e) { |
| 154 | + HitInfo hitInfo = textFlow.hitTest(new Point2D(e.getX() - textFlowLayoutX, e.getY() - textFlowLayoutY)); |
| 155 | + if (hitInfo.getInsertionIndex() >= 0) { |
| 156 | + int dragEnd = paragraph.getStart() + hitInfo.getInsertionIndex(); |
| 157 | + viewModel.setSelection(new Selection(richTextAreaSkin.dragStart, dragEnd)); |
| 158 | + viewModel.setCaretPosition(dragEnd); |
| 159 | + } |
| 160 | + e.consume(); |
| 161 | + } |
| 162 | + |
| 163 | + private void updateCaretPosition(int caretPosition) { |
| 164 | + caretShape.getElements().clear(); |
| 165 | + if (paragraph == null || caretPosition < paragraph.getStart() || paragraph.getEnd() <= caretPosition) { |
| 166 | + caretTimeline.stop(); |
| 167 | + return; |
| 168 | + } |
| 169 | + if (caretPosition < 0 || !control.isEditable()) { |
| 170 | + caretTimeline.stop(); |
| 171 | + } else { |
| 172 | + var pathElements = textFlow.caretShape(caretPosition - paragraph.getStart(), true); |
| 173 | + if (pathElements.length > 0) { |
| 174 | + caretShape.getElements().addAll(pathElements); |
| 175 | + richTextAreaSkin.lastValidCaretPosition = caretPosition; |
| 176 | + caretTimeline.play(); |
| 177 | + } |
| 178 | + } |
| 179 | + caretShape.setLayoutX(textFlowLayoutX); |
| 180 | + caretShape.setLayoutY(textFlowLayoutY); |
| 181 | + } |
| 182 | + |
| 183 | + private void setCaretVisibility(boolean on) { |
| 184 | + if (caretShape.getElements().size() > 0) { |
| 185 | + // Opacity is used since we don't want the changing caret bounds to affect the layout |
| 186 | + // Otherwise text appears to be jumping |
| 187 | + caretShape.setOpacity(on ? 1 : 0); |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | + private void updateSelection(Selection selection) { |
| 192 | + selectionShape.getElements().clear(); |
| 193 | + if (selection != null && selection.isDefined()) { |
| 194 | + PathElement[] pathElements = textFlow.rangeShape( |
| 195 | + Math.max(paragraph.getStart(), selection.getStart()) - paragraph.getStart(), |
| 196 | + Math.min(paragraph.getEnd(), selection.getEnd()) - paragraph.getStart()); |
| 197 | + if (pathElements.length > 0) { |
| 198 | + selectionShape.getElements().setAll(pathElements); |
| 199 | + } |
| 200 | + } |
| 201 | + selectionShape.setLayoutX(textFlowLayoutX); |
| 202 | + selectionShape.setLayoutY(textFlowLayoutY); |
| 203 | + } |
| 204 | + |
| 205 | + private void updateLayers(SetChangeListener.Change<? extends Path> change) { |
| 206 | + if (change.wasAdded()) { |
| 207 | + layers.getChildren().add(0, change.getElementAdded()); |
| 208 | + } else if (change.wasRemoved()) { |
| 209 | + layers.getChildren().remove(change.getElementRemoved()); |
| 210 | + } |
| 211 | + } |
| 212 | + |
| 213 | + void evictUnusedObjects() { |
| 214 | + Set<Font> usedFonts = textFlow.getChildren() |
| 215 | + .stream() |
| 216 | + .filter(Text.class::isInstance) |
| 217 | + .map(t -> ((Text) t).getFont()) |
| 218 | + .filter(Objects::nonNull) |
| 219 | + .collect(Collectors.toSet()); |
| 220 | + List<Font> cachedFonts = new ArrayList<>(richTextAreaSkin.getFontCache().values()); |
| 221 | + cachedFonts.removeAll(usedFonts); |
| 222 | + richTextAreaSkin.getFontCache().values().removeAll(cachedFonts); |
| 223 | + |
| 224 | + Set<Image> usedImages = textFlow.getChildren() |
| 225 | + .stream() |
| 226 | + .filter(ImageView.class::isInstance) |
| 227 | + .map(t -> ((ImageView) t).getImage()) |
| 228 | + .filter(Objects::nonNull) |
| 229 | + .collect(Collectors.toSet()); |
| 230 | + List<Image> cachedImages = new ArrayList<>(richTextAreaSkin.getImageCache().values()); |
| 231 | + cachedImages.removeAll(usedImages); |
| 232 | + richTextAreaSkin.getImageCache().values().removeAll(cachedImages); |
| 233 | + } |
| 234 | + |
| 235 | + void updateLayout() { |
| 236 | + if (control == null || viewModel == null) { |
| 237 | + return; |
| 238 | + } |
| 239 | + updateSelection(viewModel.getSelection()); |
| 240 | + updateCaretPosition(viewModel.getCaretPosition()); |
| 241 | + } |
| 242 | + |
| 243 | + boolean hasCaret() { |
| 244 | + return !caretShape.getElements().isEmpty(); |
| 245 | + } |
| 246 | + |
| 247 | + int getNextRowPosition(double x, boolean down) { |
| 248 | + Bounds caretBounds = caretShape.getLayoutBounds(); |
| 249 | + double nextRowPos = x < 0d ? |
| 250 | + down ? |
| 251 | + caretBounds.getMaxY() + textFlow.getLineSpacing() : |
| 252 | + caretBounds.getMinY() - textFlow.getLineSpacing() : |
| 253 | + caretBounds.getCenterY(); |
| 254 | + double xPos = x < 0d ? caretBounds.getMaxX() : x; |
| 255 | + HitInfo hitInfo = textFlow.hitTest(new Point2D(xPos, nextRowPos)); |
| 256 | + return paragraph.getStart() + hitInfo.getInsertionIndex(); |
| 257 | + } |
| 258 | + |
| 259 | +} |
0 commit comments