Skip to content

Commit b543cfe

Browse files
authored
Compute pref width of ParagraphTile, including tests (#417)
* Compute pref width of ParagraphTile, including tests * Set pref width base on children * remove unneeded code
1 parent 848d199 commit b543cfe

File tree

3 files changed

+59
-6
lines changed

3 files changed

+59
-6
lines changed

rta/pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@
1414

1515
<properties>
1616
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
17-
<maven.compiler.release>11</maven.compiler.release>
17+
<maven.compiler.release>21</maven.compiler.release>
1818
<javafx.version>23.0.1</javafx.version>
1919
<gpg.plugin.version>3.2.7</gpg.plugin.version>
2020
<emoji.version>1.1.0</emoji.version>
2121
<test.headless/>
22-
<test.javafx.version>26-ea+15</test.javafx.version>
22+
<test.javafx.version>26-ea+18</test.javafx.version>
2323
</properties>
2424

2525
<dependencies>
@@ -228,7 +228,7 @@
228228
<profile>
229229
<id>test</id>
230230
<properties>
231-
<test.headless>-Dtestfx.headless=true -Dprism.order=sw</test.headless>
231+
<test.headless>-Dglass.platform=Headless -Dprism.order=sw</test.headless>
232232
</properties>
233233
<dependencies>
234234
<dependency>

rta/src/main/java/com/gluonhq/richtextarea/ParagraphTile.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,13 +140,15 @@ void setParagraph(Paragraph paragraph, List<Node> fragments, List<Integer> posit
140140
if (!fragments.isEmpty()) {
141141
HBox gridBox = createGridBox(fragments, positions, background, decoration);
142142
contentPane.getChildren().add(gridBox);
143+
contentPane.setPrefWidth(gridBox.getPrefWidth());
143144
contentPane.layout();
144145
}
145146
} else {
146147
Layer layer = new Layer(paragraph.getStart(), paragraph.getEnd(), false);
147148
layer.setContent(fragments, background, decoration);
148149
layers.add(layer);
149150
contentPane.getChildren().add(layer);
151+
contentPane.setPrefWidth(layer.getPrefWidth());
150152
updateGraphicBox(layer, control.getParagraphGraphicFactory());
151153
graphicBox.setPadding(new Insets(decoration.getTopInset(), 2, decoration.getBottomInset(), 0));
152154
contentPane.layout();

rta/src/test/java/com/gluonhq/richtextarea/ui/RTATest.java

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2024, Gluon
2+
* Copyright (c) 2024, 2025, Gluon
33
*
44
* This program is free software: you can redistribute it and/or modify
55
* it under the terms of the GNU General Public License as published by
@@ -41,7 +41,10 @@
4141
import com.gluonhq.richtextarea.model.TextDecoration;
4242
import javafx.application.Platform;
4343
import javafx.event.ActionEvent;
44+
import javafx.geometry.Orientation;
45+
import javafx.scene.Node;
4446
import javafx.scene.Scene;
47+
import javafx.scene.control.ScrollBar;
4548
import javafx.scene.control.ToggleButton;
4649
import javafx.scene.image.ImageView;
4750
import javafx.scene.input.Clipboard;
@@ -92,9 +95,9 @@
9295
import static org.junit.jupiter.api.Assertions.assertFalse;
9396
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
9497
import static org.junit.jupiter.api.Assertions.assertNotNull;
98+
import static org.junit.jupiter.api.Assertions.assertNull;
9599
import static org.junit.jupiter.api.Assertions.assertTrue;
96100
import static org.testfx.api.FxAssert.verifyThat;
97-
import static org.testfx.util.WaitForAsyncUtils.sleep;
98101
import static org.testfx.util.WaitForAsyncUtils.waitForFxEvents;
99102

100103
@ExtendWith(ApplicationExtension.class)
@@ -891,6 +894,7 @@ public void copyPaste3Test(FxRobot robot) {
891894
assertEquals(NORMAL, ((TextDecoration) dm4.getDecoration()).getFontWeight());
892895
assertEquals("black", ((TextDecoration) dm4.getDecoration()).getForeground());
893896
}
897+
894898
@Test
895899
public void multiLineDocumentTest(FxRobot robot) {
896900
run(() -> {
@@ -923,7 +927,54 @@ public void multiLineDocumentTest(FxRobot robot) {
923927
assertInstanceOf(Text.class, tf.getChildren().get(0));
924928
assertFalse(((Text) tf.getChildren().get(0)).getText().contains("\n"));
925929
}
926-
sleep(4, TimeUnit.SECONDS);
930+
}
931+
932+
@Test
933+
public void longLineWrapDocumentTest(FxRobot robot) {
934+
run(() -> {
935+
String text = "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.";
936+
TextDecoration textDecoration = TextDecoration.builder().presets().fontFamily("Arial").build();
937+
ParagraphDecoration paragraphDecoration = ParagraphDecoration.builder().presets().build();
938+
Document document = new Document(text,
939+
List.of(new DecorationModel(0, text.length(), textDecoration, paragraphDecoration)), text.length());
940+
richTextArea.getActionFactory().open(document).execute(new ActionEvent());
941+
});
942+
waitForFxEvents();
943+
944+
verifyThat(".rich-text-area", node -> node instanceof RichTextArea);
945+
assertEquals(2, robot.lookup(".scroll-bar").queryAll().size());
946+
ScrollBar scrollBar = (ScrollBar) robot.lookup(".scroll-bar").queryAll().stream().filter(Node::isVisible).findFirst().orElse(null);
947+
assertNull(scrollBar);
948+
assertEquals(1, robot.lookup(".text-flow").queryAll().size());
949+
assertInstanceOf(TextFlow.class, robot.lookup(".text-flow").query());
950+
TextFlow tf = robot.lookup(".text-flow").query();
951+
assertEquals(3, tf.getLayoutInfo().getTextLineCount());
952+
}
953+
954+
@Test
955+
public void longLineNoWrapDocumentTest(FxRobot robot) {
956+
run(() -> {
957+
String text = "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.";
958+
TextDecoration textDecoration = TextDecoration.builder().presets().fontFamily("Arial").build();
959+
ParagraphDecoration paragraphDecoration = ParagraphDecoration.builder().presets().build();
960+
Document document = new Document(text,
961+
List.of(new DecorationModel(0, text.length(), textDecoration, paragraphDecoration)), text.length());
962+
richTextArea.setContentAreaWidth(2000);
963+
richTextArea.getActionFactory().open(document).execute(new ActionEvent());
964+
});
965+
waitForFxEvents();
966+
967+
verifyThat(".rich-text-area", node -> node instanceof RichTextArea);
968+
assertEquals(2, robot.lookup(".scroll-bar").queryAll().size());
969+
ScrollBar scrollBar = (ScrollBar) robot.lookup(".scroll-bar").queryAll().stream().filter(Node::isVisible).findFirst().orElse(null);
970+
assertNotNull(scrollBar);
971+
assertEquals(Orientation.HORIZONTAL, scrollBar.getOrientation());
972+
assertEquals(0, scrollBar.getValue());
973+
assertEquals(1, robot.lookup(".text-flow").queryAll().size());
974+
assertInstanceOf(TextFlow.class, robot.lookup(".text-flow").query());
975+
TextFlow tf = robot.lookup(".text-flow").query();
976+
assertEquals(2000, tf.prefWidth(tf.getHeight()));
977+
assertEquals(1, tf.getLayoutInfo().getTextLineCount());
927978
}
928979

929980
@Test

0 commit comments

Comments
 (0)