Skip to content

Commit

Permalink
Added scroll line method
Browse files Browse the repository at this point in the history
  • Loading branch information
Jugen committed Feb 7, 2022
1 parent fbdcd28 commit 961fe88
Showing 1 changed file with 20 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -1393,7 +1393,7 @@ public void nextPage(SelectionPolicy selectionPolicy) {
* @param pgCount the number of pages to page up/down.
* <br>Negative numbers for paging up and positive for down.
*/
private void page(int pgCount, SelectionPolicy selectionPolicy)
private void page(double pgCount, SelectionPolicy selectionPolicy)
{
// Use underlying caret to get the same behaviour as navigating up/down a line where the x position is sticky
Optional<Bounds> cb = caretSelectionBind.getUnderlyingCaret().getCaretBounds();
Expand All @@ -1410,6 +1410,25 @@ private void page(int pgCount, SelectionPolicy selectionPolicy)
.filter( delta -> delta != 0.0 ).ifPresent( delta -> scrollYBy( delta ) ) );
}

/**
* Scrolls nLines down (for positive numbers) or up (for negative numbers),
* while maintaining the caret's current position on screen.
* @param selectionPolicy
* @param nLines
*/
public void scrollLines(SelectionPolicy selectionPolicy, int nLines) {
if (nLines == 0) return;

int direction = nLines < 0 ? -1 : +1;
double viewHeight = getViewportHeight() * direction;

for (int lines = nLines; lines != 0; lines -= direction)
{
getCaretBounds().map( caret -> (caret.getHeight()-2) / viewHeight )
.ifPresent(lineHeight -> page(lineHeight, selectionPolicy));
}
}

@Override
public void displaceCaret(int pos) {
caretSelectionBind.displaceCaret(pos);
Expand Down

0 comments on commit 961fe88

Please sign in to comment.