Skip to content

Commit 1fef2c0

Browse files
Merge pull request #519 from JordanMartinez/paddingPBox
Account for padding in ParagraphBox's layout call
2 parents def41b9 + 7e3c97c commit 1fef2c0

3 files changed

Lines changed: 65 additions & 5 deletions

File tree

richtextfx/src/integrationTest/java/org/fxmisc/richtext/api/HitTests.java

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,4 +165,61 @@ public void nextPageMovesCaretToBottomOfPage() {
165165

166166
}
167167

168+
public class WhenParagraphBoxIsPadded {
169+
170+
double paddingAmount = 20;
171+
172+
String text = "abcdefghijklmnopqrstuvwxyz";
173+
String fullText;
174+
175+
{
176+
int totalPars = 50;
177+
int indexLimit = totalPars - 1;
178+
StringBuilder sb = new StringBuilder();
179+
Consumer<Integer> appendParagraph = i -> sb.append("Par #").append(i).append(" ").append(text);
180+
for (int i = 0; i < indexLimit; i++) {
181+
appendParagraph.accept(i);
182+
sb.append("\n");
183+
}
184+
appendParagraph.accept(indexLimit);
185+
fullText = sb.toString();
186+
}
187+
188+
@Before
189+
public void setup() {
190+
interact(() -> {
191+
area.replaceText(fullText);
192+
area.setStyle("-fx-font-family: monospace; -fx-font-size: 12pt;");
193+
scene.getStylesheets().add(HitTests.class.getResource("padded-paragraph-box.css").toExternalForm());
194+
});
195+
}
196+
197+
private void runTest() {
198+
int start = area.getAbsolutePosition(3, 8);
199+
Bounds b = area.getCharacterBoundsOnScreen(start, start + 1).get();
200+
moveTo(b).clickOn(PRIMARY);
201+
assertEquals(start, area.getCaretPosition());
202+
}
203+
204+
public class AndAreaIsPadded {
205+
206+
@Test
207+
public void clickingCharacterShouldMoveCaretToThatPosition() {
208+
interact(() -> area.setPadding(new Insets(paddingAmount)));
209+
210+
runTest();
211+
}
212+
}
213+
214+
public class AndAreaIsNotPadded {
215+
216+
@Test
217+
public void clickingCharacterShouldMoveCaretToThatPosition() {
218+
runTest();
219+
}
220+
221+
}
222+
223+
}
224+
168225
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.paragraph-box {
2+
-fx-padding: 20px;
3+
}

richtextfx/src/main/java/org/fxmisc/richtext/ParagraphBox.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -218,14 +218,14 @@ protected double computePrefHeight(double width) {
218218
@Override
219219
protected
220220
void layoutChildren() {
221-
Bounds bounds = getLayoutBounds();
222-
double w = bounds.getWidth();
223-
double h = bounds.getHeight();
221+
Insets ins = getInsets();
222+
double w = getWidth() - ins.getLeft() - ins.getRight();
223+
double h = getHeight() - ins.getTop() - ins.getBottom();
224224
double graphicWidth = getGraphicPrefWidth();
225225

226-
text.resizeRelocate(graphicWidth, 0, w - graphicWidth, h);
226+
text.resizeRelocate(graphicWidth + ins.getLeft(), ins.getTop(), w - graphicWidth, h);
227227

228-
graphic.ifPresent(g -> g.resizeRelocate(graphicOffset.get(), 0, graphicWidth, h));
228+
graphic.ifPresent(g -> g.resizeRelocate(graphicOffset.get() + ins.getLeft(), ins.getTop(), graphicWidth, h));
229229
}
230230

231231
double getGraphicPrefWidth() {

0 commit comments

Comments
 (0)