From fa17d2819bf096ccb526e15fc3fc9f6839f2af83 Mon Sep 17 00:00:00 2001 From: Jurgen Date: Wed, 23 Dec 2020 09:27:02 +0200 Subject: [PATCH] Fix TextFlowExt hit (#992) Fix TextFlowExt hit and added click on padding test --- .../richtext/mouse/ClickAndDragTests.java | 19 +++++++++++++++++++ .../org/fxmisc/richtext/mouse/padtest.css | 4 ++++ .../org/fxmisc/richtext/TextFlowExt.java | 4 ++++ 3 files changed, 27 insertions(+) create mode 100644 richtextfx/src/integrationTest/resources/org/fxmisc/richtext/mouse/padtest.css diff --git a/richtextfx/src/integrationTest/java/org/fxmisc/richtext/mouse/ClickAndDragTests.java b/richtextfx/src/integrationTest/java/org/fxmisc/richtext/mouse/ClickAndDragTests.java index eaf37dddf..1f5d97faa 100644 --- a/richtextfx/src/integrationTest/java/org/fxmisc/richtext/mouse/ClickAndDragTests.java +++ b/richtextfx/src/integrationTest/java/org/fxmisc/richtext/mouse/ClickAndDragTests.java @@ -3,6 +3,7 @@ import com.nitorcreations.junit.runners.NestedRunner; import javafx.beans.property.SimpleIntegerProperty; import javafx.geometry.Bounds; +import javafx.geometry.Point2D; import javafx.stage.Stage; import org.fxmisc.richtext.InlineCssTextAreaAppTest; import org.junit.Test; @@ -117,6 +118,24 @@ public void single_clicking_area_moves_caret_to_that_position() assertEquals(firstWord.length(), area.getCaretPosition()); } + @Test + public void single_clicking_area_beyond_text_moves_caret_to_end_position() + throws InterruptedException, ExecutionException { + + int position = firstParagraph.length() + secondWord.length(); + interact( () -> area.getStylesheets ().add("org/fxmisc/richtext/mouse/padtest.css") ); + assertEquals(0, area.getCaretPosition()); + clickOn( area ); + + Bounds b = asyncFx( + () -> area.getCharacterBoundsOnScreen(position, position + 1).get()) + .get(); + + moveTo(new Point2D( b.getMaxX(), b.getMaxY()+25 )).clickOn(PRIMARY); + + assertEquals(area.getLength(), area.getCaretPosition()); + } + @Test public void double_clicking_text_in_area_selects_closest_word() { doubleClickOnFirstLine(); diff --git a/richtextfx/src/integrationTest/resources/org/fxmisc/richtext/mouse/padtest.css b/richtextfx/src/integrationTest/resources/org/fxmisc/richtext/mouse/padtest.css new file mode 100644 index 000000000..a00bf35d8 --- /dev/null +++ b/richtextfx/src/integrationTest/resources/org/fxmisc/richtext/mouse/padtest.css @@ -0,0 +1,4 @@ +.styled-text-area .paragraph-text +{ + -fx-padding: 0 0 50pt 0; +} diff --git a/richtextfx/src/main/java9/org/fxmisc/richtext/TextFlowExt.java b/richtextfx/src/main/java9/org/fxmisc/richtext/TextFlowExt.java index cb545eb8a..854cbda6b 100644 --- a/richtextfx/src/main/java9/org/fxmisc/richtext/TextFlowExt.java +++ b/richtextfx/src/main/java9/org/fxmisc/richtext/TextFlowExt.java @@ -109,6 +109,10 @@ CharacterHit hit(double x, double y) { int charIdx = hit.getCharIndex(); boolean leading = hit.isLeading(); + if (y >= span.getBounds().getMaxY()) { + return CharacterHit.insertionAt(charIdx); + } + if ( ! leading && getLineCount() > 1) { // If this is a wrapped paragraph and hit character is at end of hit line, make sure that the // "character hit" stays at the end of the hit line (and not at the beginning of the next line).