From 19c448072094efd8061580dc650cc5afee9c1ce8 Mon Sep 17 00:00:00 2001 From: JordanMartinez Date: Thu, 6 Oct 2016 17:18:16 -0700 Subject: [PATCH] Request follow caret (#378) Only follow caret on edit done through the UI, not programmatic edits. --- .../java/org/fxmisc/richtext/StyledTextArea.java | 15 +++++++-------- .../fxmisc/richtext/StyledTextAreaBehavior.java | 5 ++++- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/richtextfx/src/main/java/org/fxmisc/richtext/StyledTextArea.java b/richtextfx/src/main/java/org/fxmisc/richtext/StyledTextArea.java index ae50ec232..73bdbb0ab 100644 --- a/richtextfx/src/main/java/org/fxmisc/richtext/StyledTextArea.java +++ b/richtextfx/src/main/java/org/fxmisc/richtext/StyledTextArea.java @@ -600,14 +600,6 @@ public StyledTextArea(PS initialParagraphStyle, BiConsumer applyPa IntUnaryOperator cellLength = i -> virtualFlow.getCell(i).getNode().getLineCount(); navigator = new TwoLevelNavigator(cellCount, cellLength); - // follow the caret every time the caret position or paragraphs change - EventStream caretPosDirty = invalidationsOf(caretPositionProperty()); - EventStream paragraphsDirty = invalidationsOf(getParagraphs()); - EventStream selectionDirty = invalidationsOf(selectionProperty()); - // need to reposition popup even when caret hasn't moved, but selection has changed (been deselected) - EventStream caretDirty = merge(caretPosDirty, paragraphsDirty, selectionDirty); - subscribeTo(caretDirty, x -> requestFollowCaret()); - // relayout the popup when any of its settings values change (besides the caret being dirty) EventStream popupAlignmentDirty = invalidationsOf(popupAlignmentProperty()); EventStream popupAnchorAdjustmentDirty = invalidationsOf(popupAnchorAdjustmentProperty()); @@ -615,6 +607,13 @@ public StyledTextArea(PS initialParagraphStyle, BiConsumer applyPa EventStream popupDirty = merge(popupAlignmentDirty, popupAnchorAdjustmentDirty, popupAnchorOffsetDirty); subscribeTo(popupDirty, x -> layoutPopup()); + // follow the caret every time the caret position or paragraphs change + EventStream caretPosDirty = invalidationsOf(caretPositionProperty()); + EventStream paragraphsDirty = invalidationsOf(getParagraphs()); + EventStream selectionDirty = invalidationsOf(selectionProperty()); + // need to reposition popup even when caret hasn't moved, but selection has changed (been deselected) + EventStream caretDirty = merge(caretPosDirty, paragraphsDirty, selectionDirty); + // whether or not to display the caret EventStream blinkCaret = EventStreams.valuesOf(showCaretProperty()) .flatMap(mode -> { diff --git a/richtextfx/src/main/java/org/fxmisc/richtext/StyledTextAreaBehavior.java b/richtextfx/src/main/java/org/fxmisc/richtext/StyledTextAreaBehavior.java index 84543f990..1144a6a92 100644 --- a/richtextfx/src/main/java/org/fxmisc/richtext/StyledTextAreaBehavior.java +++ b/richtextfx/src/main/java/org/fxmisc/richtext/StyledTextAreaBehavior.java @@ -169,13 +169,16 @@ class StyledTextAreaBehavior { .orElse(otherNavigation).ifConsumed((b, e) -> b.view.clearTargetCaretOffset()) .orElse(verticalNavigation) .orElse(copyAction) + .ifConsumed((b, e) -> b.view.requestFollowCaret()) + // no need to add 'ifConsumed' after charPress since + // requestFollowCaret is called in keyTypedTemplate .orElse(charPressConsumer); InputMapTemplate keyTypedBase = consume( // character input EventPattern.keyTyped().onlyIf(noControlKeys.and(e -> isLegal(e.getCharacter()))), StyledTextAreaBehavior::keyTyped - ); + ).ifConsumed((b, e) -> b.view.requestFollowCaret()); InputMapTemplate keyTypedTemplate = when(b -> b.view.isEditable(), keyTypedBase); InputMapTemplate mouseEventTemplate = sequence(