Skip to content

Commit 12ebd7d

Browse files
authored
Merge pull request #376 from JordanMartinez/pageUpDown
Expose API for page up/down
2 parents 708ea4c + 7d7565d commit 12ebd7d

2 files changed

Lines changed: 44 additions & 33 deletions

File tree

richtextfx/src/main/java/org/fxmisc/richtext/StyledTextArea.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,9 @@ public Optional<Tuple2<Codec<PS>, Codec<S>>> getStyleCodecs() {
448448

449449
private Subscription subscriptions = () -> {};
450450

451+
// Remembers horizontal position when traversing up / down.
452+
private Optional<ParagraphBox.CaretOffsetX> targetCaretOffset = Optional.empty();
453+
451454
private final Binding<Boolean> caretVisible;
452455

453456
private final Val<UnaryOperator<Point2D>> _popupAnchorAdjustment;
@@ -967,6 +970,30 @@ private void followCaret() {
967970
virtualFlow.show(parIdx, region);
968971
}
969972

973+
/**
974+
* Moves caret to the previous page (i.e. page up)
975+
* @param selectionPolicy use {@link SelectionPolicy#CLEAR} when no selection is desired and
976+
* {@link SelectionPolicy#ADJUST} when a selection from starting point
977+
* to the place to where the caret is moved is desired.
978+
*/
979+
public void prevPage(SelectionPolicy selectionPolicy) {
980+
showCaretAtBottom();
981+
CharacterHit hit = hit(getTargetCaretOffset(), 1.0);
982+
model.moveTo(hit.getInsertionIndex(), selectionPolicy);
983+
}
984+
985+
/**
986+
* Moves caret to the next page (i.e. page down)
987+
* @param selectionPolicy use {@link SelectionPolicy#CLEAR} when no selection is desired and
988+
* {@link SelectionPolicy#ADJUST} when a selection from starting point
989+
* to the place to where the caret is moved is desired.
990+
*/
991+
public void nextPage(SelectionPolicy selectionPolicy) {
992+
showCaretAtTop();
993+
CharacterHit hit = hit(getTargetCaretOffset(), getViewportHeight() - 1.0);
994+
model.moveTo(hit.getInsertionIndex(), selectionPolicy);
995+
}
996+
970997
/**
971998
* Sets style for the given character range.
972999
*/
@@ -1289,6 +1316,16 @@ private static Bounds extendLeft(Bounds b, double w) {
12891316
}
12901317
}
12911318

1319+
void clearTargetCaretOffset() {
1320+
targetCaretOffset = Optional.empty();
1321+
}
1322+
1323+
ParagraphBox.CaretOffsetX getTargetCaretOffset() {
1324+
if(!targetCaretOffset.isPresent())
1325+
targetCaretOffset = Optional.of(getCaretOffsetX());
1326+
return targetCaretOffset.get();
1327+
}
1328+
12921329
private static EventStream<Boolean> booleanPulse(javafx.util.Duration javafxDuration, EventStream<?> restartImpulse) {
12931330
Duration duration = Duration.ofMillis(Math.round(javafxDuration.toMillis()));
12941331
EventStream<?> ticks = EventStreams.restartableTicks(duration, restartImpulse);

richtextfx/src/main/java/org/fxmisc/richtext/StyledTextAreaBehavior.java

Lines changed: 7 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -84,17 +84,17 @@ class StyledTextAreaBehavior {
8484
consume(
8585
anyOf(keyPressed(DOWN), keyPressed(KP_DOWN)),
8686
(b, e) -> b.nextLine(SelectionPolicy.CLEAR)),
87-
consume(keyPressed(PAGE_UP), (b, e) -> b.prevPage(SelectionPolicy.CLEAR)),
88-
consume(keyPressed(PAGE_DOWN), (b, e) -> b.nextPage(SelectionPolicy.CLEAR)),
87+
consume(keyPressed(PAGE_UP), (b, e) -> b.view.prevPage(SelectionPolicy.CLEAR)),
88+
consume(keyPressed(PAGE_DOWN), (b, e) -> b.view.nextPage(SelectionPolicy.CLEAR)),
8989
// vertical selection
9090
consume(
9191
anyOf(keyPressed(UP, SHIFT_DOWN), keyPressed(KP_UP, SHIFT_DOWN)),
9292
(b, e) -> b.prevLine(SelectionPolicy.ADJUST)),
9393
consume(
9494
anyOf(keyPressed(DOWN, SHIFT_DOWN), keyPressed(KP_DOWN, SHIFT_DOWN)),
9595
(b, e) -> b.nextLine(SelectionPolicy.ADJUST)),
96-
consume(keyPressed(PAGE_UP, SHIFT_DOWN), (b, e) -> b.prevPage(SelectionPolicy.ADJUST)),
97-
consume(keyPressed(PAGE_DOWN, SHIFT_DOWN), (b, e) -> b.nextPage(SelectionPolicy.ADJUST))
96+
consume(keyPressed(PAGE_UP, SHIFT_DOWN), (b, e) -> b.view.prevPage(SelectionPolicy.ADJUST)),
97+
consume(keyPressed(PAGE_DOWN, SHIFT_DOWN), (b, e) -> b.view.nextPage(SelectionPolicy.ADJUST))
9898
);
9999

100100
InputMapTemplate<StyledTextAreaBehavior, KeyEvent> otherNavigation = sequence(
@@ -165,7 +165,7 @@ class StyledTextAreaBehavior {
165165
InputMapTemplate<StyledTextAreaBehavior, KeyEvent> charPressConsumer = consume(keyPressed().onlyIf(isChar.and(noControlKeys)));
166166

167167
InputMapTemplate<StyledTextAreaBehavior, ? super KeyEvent> keyPressedTemplate = edits
168-
.orElse(otherNavigation).ifConsumed((b, e) -> b.clearTargetCaretOffset())
168+
.orElse(otherNavigation).ifConsumed((b, e) -> b.view.clearTargetCaretOffset())
169169
.orElse(verticalNavigation)
170170
.orElse(copyAction)
171171
.orElse(charPressConsumer);
@@ -214,19 +214,6 @@ private enum DragState {
214214
*/
215215
private DragState dragSelection = DragState.NO_DRAG;
216216

217-
/**
218-
* Remembers horizontal position when traversing up / down.
219-
*/
220-
private Optional<CaretOffsetX> targetCaretOffset = Optional.empty();
221-
private void clearTargetCaretOffset() {
222-
targetCaretOffset = Optional.empty();
223-
}
224-
private CaretOffsetX getTargetCaretOffset() {
225-
if(!targetCaretOffset.isPresent())
226-
targetCaretOffset = Optional.of(view.getCaretOffsetX());
227-
return targetCaretOffset.get();
228-
}
229-
230217
private final Var<Point2D> autoscrollTo = Var.newSimpleVar(null);
231218

232219
/* ********************************************************************** *
@@ -359,7 +346,7 @@ private void downLines(SelectionPolicy selectionPolicy, int nLines) {
359346
Position targetLine = currentLine.offsetBy(nLines, Forward).clamp();
360347
if(!currentLine.sameAs(targetLine)) {
361348
// compute new caret position
362-
CharacterHit hit = view.hit(getTargetCaretOffset(), targetLine);
349+
CharacterHit hit = view.hit(view.getTargetCaretOffset(), targetLine);
363350

364351
// update model
365352
model.moveTo(hit.getInsertionIndex(), selectionPolicy);
@@ -374,19 +361,6 @@ private void nextLine(SelectionPolicy selectionPolicy) {
374361
downLines(selectionPolicy, 1);
375362
}
376363

377-
private void prevPage(SelectionPolicy selectionPolicy) {
378-
view.showCaretAtBottom();
379-
CharacterHit hit = view.hit(getTargetCaretOffset(), 1.0);
380-
model.moveTo(hit.getInsertionIndex(), selectionPolicy);
381-
}
382-
383-
private void nextPage(SelectionPolicy selectionPolicy) {
384-
view.showCaretAtTop();
385-
CharacterHit hit = view.hit(getTargetCaretOffset(), view.getViewportHeight() - 1.0);
386-
model.moveTo(hit.getInsertionIndex(), selectionPolicy);
387-
}
388-
389-
390364
/* ********************************************************************** *
391365
* Mouse handling implementation *
392366
* ********************************************************************** */
@@ -423,7 +397,7 @@ private void mousePressed(MouseEvent e) {
423397
}
424398

425399
private void firstLeftPress(CharacterHit hit) {
426-
clearTargetCaretOffset();
400+
view.clearTargetCaretOffset();
427401
IndexRange selection = model.getSelection();
428402
if(view.isEditable() &&
429403
selection.getLength() != 0 &&

0 commit comments

Comments
 (0)