From 7e3c97c7dd7927b7cbfb7d3f9321020706654426 Mon Sep 17 00:00:00 2001 From: Jordan Martinez Date: Mon, 26 Jun 2017 15:00:09 -0700 Subject: [PATCH] Account for padding in ParagraphBox's layout call --- .../org/fxmisc/richtext/api/HitTests.java | 57 +++++++++++++++++++ .../richtext/api/padded-paragraph-box.css | 3 + .../org/fxmisc/richtext/ParagraphBox.java | 10 ++-- 3 files changed, 65 insertions(+), 5 deletions(-) create mode 100644 richtextfx/src/integrationTest/resources/org/fxmisc/richtext/api/padded-paragraph-box.css diff --git a/richtextfx/src/integrationTest/java/org/fxmisc/richtext/api/HitTests.java b/richtextfx/src/integrationTest/java/org/fxmisc/richtext/api/HitTests.java index 2ac02eb9b..c31757790 100644 --- a/richtextfx/src/integrationTest/java/org/fxmisc/richtext/api/HitTests.java +++ b/richtextfx/src/integrationTest/java/org/fxmisc/richtext/api/HitTests.java @@ -165,4 +165,61 @@ public void nextPageMovesCaretToBottomOfPage() { } + public class WhenParagraphBoxIsPadded { + + double paddingAmount = 20; + + String text = "abcdefghijklmnopqrstuvwxyz"; + String fullText; + + { + int totalPars = 50; + int indexLimit = totalPars - 1; + StringBuilder sb = new StringBuilder(); + Consumer appendParagraph = i -> sb.append("Par #").append(i).append(" ").append(text); + for (int i = 0; i < indexLimit; i++) { + appendParagraph.accept(i); + sb.append("\n"); + } + appendParagraph.accept(indexLimit); + fullText = sb.toString(); + } + + @Before + public void setup() { + interact(() -> { + area.replaceText(fullText); + area.setStyle("-fx-font-family: monospace; -fx-font-size: 12pt;"); + scene.getStylesheets().add(HitTests.class.getResource("padded-paragraph-box.css").toExternalForm()); + }); + } + + private void runTest() { + int start = area.getAbsolutePosition(3, 8); + Bounds b = area.getCharacterBoundsOnScreen(start, start + 1).get(); + moveTo(b).clickOn(PRIMARY); + assertEquals(start, area.getCaretPosition()); + } + + public class AndAreaIsPadded { + + @Test + public void clickingCharacterShouldMoveCaretToThatPosition() { + interact(() -> area.setPadding(new Insets(paddingAmount))); + + runTest(); + } + } + + public class AndAreaIsNotPadded { + + @Test + public void clickingCharacterShouldMoveCaretToThatPosition() { + runTest(); + } + + } + + } + } diff --git a/richtextfx/src/integrationTest/resources/org/fxmisc/richtext/api/padded-paragraph-box.css b/richtextfx/src/integrationTest/resources/org/fxmisc/richtext/api/padded-paragraph-box.css new file mode 100644 index 000000000..cf45c0616 --- /dev/null +++ b/richtextfx/src/integrationTest/resources/org/fxmisc/richtext/api/padded-paragraph-box.css @@ -0,0 +1,3 @@ +.paragraph-box { + -fx-padding: 20px; +} \ No newline at end of file diff --git a/richtextfx/src/main/java/org/fxmisc/richtext/ParagraphBox.java b/richtextfx/src/main/java/org/fxmisc/richtext/ParagraphBox.java index bfcdfd852..412f7eccc 100644 --- a/richtextfx/src/main/java/org/fxmisc/richtext/ParagraphBox.java +++ b/richtextfx/src/main/java/org/fxmisc/richtext/ParagraphBox.java @@ -218,14 +218,14 @@ protected double computePrefHeight(double width) { @Override protected void layoutChildren() { - Bounds bounds = getLayoutBounds(); - double w = bounds.getWidth(); - double h = bounds.getHeight(); + Insets ins = getInsets(); + double w = getWidth() - ins.getLeft() - ins.getRight(); + double h = getHeight() - ins.getTop() - ins.getBottom(); double graphicWidth = getGraphicPrefWidth(); - text.resizeRelocate(graphicWidth, 0, w - graphicWidth, h); + text.resizeRelocate(graphicWidth + ins.getLeft(), ins.getTop(), w - graphicWidth, h); - graphic.ifPresent(g -> g.resizeRelocate(graphicOffset.get(), 0, graphicWidth, h)); + graphic.ifPresent(g -> g.resizeRelocate(graphicOffset.get() + ins.getLeft(), ins.getTop(), graphicWidth, h)); } double getGraphicPrefWidth() {