Skip to content

Commit

Permalink
Request follow caret (#378)
Browse files Browse the repository at this point in the history
Only follow caret on edit done through the UI, not programmatic edits.
  • Loading branch information
JordanMartinez authored and TomasMikula committed Oct 7, 2016
1 parent b518c6a commit 19c4480
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
15 changes: 7 additions & 8 deletions richtextfx/src/main/java/org/fxmisc/richtext/StyledTextArea.java
Original file line number Diff line number Diff line change
Expand Up @@ -600,21 +600,20 @@ public StyledTextArea(PS initialParagraphStyle, BiConsumer<TextFlow, PS> 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());
EventStream<?> popupAnchorOffsetDirty = invalidationsOf(popupAnchorOffsetProperty());
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<Boolean> blinkCaret = EventStreams.valuesOf(showCaretProperty())
.flatMap(mode -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<StyledTextAreaBehavior, KeyEvent> keyTypedBase = consume(
// character input
EventPattern.keyTyped().onlyIf(noControlKeys.and(e -> isLegal(e.getCharacter()))),
StyledTextAreaBehavior::keyTyped
);
).ifConsumed((b, e) -> b.view.requestFollowCaret());
InputMapTemplate<StyledTextAreaBehavior, ? super KeyEvent> keyTypedTemplate = when(b -> b.view.isEditable(), keyTypedBase);

InputMapTemplate<StyledTextAreaBehavior, ? super MouseEvent> mouseEventTemplate = sequence(
Expand Down

0 comments on commit 19c4480

Please sign in to comment.