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

Account for padding in ParagraphBox's layout call #519

Merged
merged 1 commit into from
Jun 26, 2017
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
Original file line number Diff line number Diff line change
Expand Up @@ -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<Integer> 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();
}

}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.paragraph-box {
-fx-padding: 20px;
}
10 changes: 5 additions & 5 deletions richtextfx/src/main/java/org/fxmisc/richtext/ParagraphBox.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down