From 50ec050fdc7b2103dd0def47d4ba6fd8422f9122 Mon Sep 17 00:00:00 2001 From: Jurgen Date: Tue, 26 Nov 2019 10:10:47 +0200 Subject: [PATCH] Added test and anchor properties fix (#877) --- .../richtext/api/selection/PositionTests.java | 13 +++++++++++++ .../richtext/CaretSelectionBindImpl.java | 19 ++++++++++++------- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/richtextfx/src/integrationTest/java/org/fxmisc/richtext/api/selection/PositionTests.java b/richtextfx/src/integrationTest/java/org/fxmisc/richtext/api/selection/PositionTests.java index d250917b2..7048af6ff 100644 --- a/richtextfx/src/integrationTest/java/org/fxmisc/richtext/api/selection/PositionTests.java +++ b/richtextfx/src/integrationTest/java/org/fxmisc/richtext/api/selection/PositionTests.java @@ -159,6 +159,7 @@ public void end_position_is_correct_when_change_occurs_after_position() { }); } + @Test public void deletion_which_includes_selection_and_which_occurs_at_end_of_area_moves_selection_to_new_area_end() { interact(() -> { selection.selectRange(area.getLength(), area.getLength()); @@ -167,4 +168,16 @@ public void deletion_which_includes_selection_and_which_occurs_at_end_of_area_mo assertEquals(area.getLength(), selection.getEndPosition()); }); } + + @Test + public void anchor_updates_correctly_with_listener_attached() { + interact(() -> { + area.clear(); + area.anchorProperty().addListener( (ob,ov,nv) -> nv++ ); + area.appendText("asdf"); + area.selectRange(1,2); + assertEquals("s",area.getSelectedText()); + assertEquals(1,area.getAnchor()); + }); + } } diff --git a/richtextfx/src/main/java/org/fxmisc/richtext/CaretSelectionBindImpl.java b/richtextfx/src/main/java/org/fxmisc/richtext/CaretSelectionBindImpl.java index 525e5ec65..0be828b92 100644 --- a/richtextfx/src/main/java/org/fxmisc/richtext/CaretSelectionBindImpl.java +++ b/richtextfx/src/main/java/org/fxmisc/richtext/CaretSelectionBindImpl.java @@ -135,9 +135,15 @@ public void configureSelectionPath(SelectionPath path) { public final boolean isBeingUpdated() { return beingUpdated.get(); } public final ObservableValue beingUpdatedProperty() { return beingUpdated; } - private final Var internalStartedByAnchor = Var.newSimpleVar(true); - private final SuspendableVal startedByAnchor = internalStartedByAnchor.suspendable(); - private boolean anchorIsStart() { return startedByAnchor.getValue(); } + private final Var internalStartedByAnchor = Var.newSimpleVar( new BooleanEvent( true ) ); + private final SuspendableVal startedByAnchor = internalStartedByAnchor.suspendable(); + private boolean anchorIsStart() { return startedByAnchor.getValue().get(); } + + private class BooleanEvent { // See #874 + public BooleanEvent( boolean b ) { value = b; } + public boolean get() { return value; } + private final boolean value; + } private Subscription subscription = () -> {}; @@ -166,7 +172,7 @@ public void configureSelectionPath(SelectionPath path) { } Val> anchorPositions = startedByAnchor.flatMap(b -> - b + b.get() ? Val.constant(Tuples.t(getStartPosition(), getStartParagraphIndex(), getStartColumnPosition())) : Val.constant(Tuples.t(getEndPosition(), getEndParagraphIndex(), getEndColumnPosition())) ); @@ -431,7 +437,7 @@ public void displaceCaret(int position) { public void displaceSelection(int startPosition, int endPosition) { doUpdate(() -> { delegateSelection.selectRange(startPosition, endPosition); - internalStartedByAnchor.setValue(startPosition < endPosition); + internalStartedByAnchor.setValue( new BooleanEvent( startPosition < endPosition ) ); }); } @@ -449,8 +455,7 @@ public void dispose() { private void doSelect(int startPosition, int endPosition, boolean anchorIsStart) { doUpdate(() -> { delegateSelection.selectRange(startPosition, endPosition); - internalStartedByAnchor.setValue(anchorIsStart); - + internalStartedByAnchor.setValue( new BooleanEvent( anchorIsStart ) ); delegateCaret.moveTo(anchorIsStart ? endPosition : startPosition); }); }