Skip to content

Commit

Permalink
fixed behavior of PageUp and PageDown keys, which now always scro…
Browse files Browse the repository at this point in the history
…ll a whole page up or down
  • Loading branch information
DevCharly committed Dec 12, 2018
1 parent 53b9e1c commit 4f5c45b
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ Markdown Writer FX Change Log
- Project specific options: Select "Store in project" checkbox in Options dialog
to save options in per project (in file `<project-root>/.markdownwriterfx`).
- "Format only modified paragraphs on Save" checkbox added to Options dialog.
- Fixed behavior of `PageUp` and `PageDown` keys, which now always scroll a
whole page up or down.
- Fixed behavior of `Ctrl+RIGHT`, which now moves caret to beginning of next
word.
- Disabled "Save As" for read-only editors (binary or too-large files).
Expand Down
69 changes: 69 additions & 0 deletions src/main/java/org/markdownwriterfx/editor/MarkdownTextArea.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,73 @@ public void wordBreaksForwards(int n, SelectionPolicy selectionPolicy) {
if (newCaretPosition != caretPosition)
moveTo(newCaretPosition, selectionPolicy);
}

@Override
public void prevPage(SelectionPolicy selectionPolicy) {
disableFollowCaret = true;

// change behavior of PAGE_UP key:
// old behavior: move caret visible lines count up (scrolling depends on caret position)
// new behavior: scroll one page up and move caret visible lines count up
try {
int firstVisible = firstVisibleParToAllParIndex();
int lastVisible = lastVisibleParToAllParIndex();
int caretParagraph = getCurrentParagraph();
int caretColumn = getCaretColumn();

showParagraphAtBottom(firstVisible - 1);

// TODO improve handling of wrapped lines and tabs
int newCaretParagraph = Math.max(firstVisible - (lastVisible - caretParagraph), 0);
if (caretParagraph == lastVisible && newCaretParagraph > 0)
newCaretParagraph--;
int newCaretColumn = Math.min(caretColumn, getParagraphLength(newCaretParagraph));
moveTo(newCaretParagraph, newCaretColumn, selectionPolicy);
} catch (AssertionError e) {
// may be thrown in textArea.visibleParToAllParIndex()
// occurs if the last line is empty and and the text fits into
// the visible area (no vertical scroll bar shown)
// --> ignore
super.prevPage(selectionPolicy);
}
}

@Override
public void nextPage(SelectionPolicy selectionPolicy) {
disableFollowCaret = true;

// change behavior of PAGE_DOWN key:
// old behavior: move caret visible lines count down (scrolling depends on caret position)
// new behavior: scroll one page down and move caret visible lines count down
try {
int firstVisible = firstVisibleParToAllParIndex();
int lastVisible = lastVisibleParToAllParIndex();
int caretParagraph = getCurrentParagraph();
int caretColumn = getCaretColumn();

showParagraphAtTop(lastVisible);

// TODO improve handling of wrapped lines and tabs
int newCaretParagraph = Math.min(lastVisible + (caretParagraph - firstVisible), getParagraphs().size() - 1);
int newCaretColumn = Math.min(caretColumn, getParagraphLength(newCaretParagraph));
moveTo(newCaretParagraph, newCaretColumn, selectionPolicy);
} catch (AssertionError e) {
// may be thrown in textArea.visibleParToAllParIndex()
// occurs if the last line is empty and and the text fits into
// the visible area (no vertical scroll bar shown)
// --> ignore
super.nextPage(selectionPolicy);
}
}

private boolean disableFollowCaret;

@Override
public void requestFollowCaret() {
if (disableFollowCaret) {
disableFollowCaret = false;
requestLayout();
} else
super.requestFollowCaret();
}
}

0 comments on commit 4f5c45b

Please sign in to comment.