Skip to content

Commit

Permalink
Support scrolling with page up and page down.
Browse files Browse the repository at this point in the history
  • Loading branch information
Lukas-Stuehrk committed Feb 9, 2023
1 parent f507bf9 commit ea047f3
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions Sources/Runestone/TextView/Core/Mac/TextView_Mac.swift
Original file line number Diff line number Diff line change
Expand Up @@ -770,6 +770,27 @@ public extension TextView {
override func selectAll(_ sender: Any?) {
textViewController.selectedRange = NSRange(location: 0, length: textViewController.stringView.string.length)
}

override func scrollPageUp(_ sender: Any?) {
var newOffset = scrollView.contentOffset.applying(.init(translationX: 0, y: -frame.size.height))
if newOffset.y < 0 {
newOffset.y = 0
}
textViewController.viewport = CGRect(origin: newOffset, size: frame.size)
textViewController.layoutIfNeeded()
scrollView.contentOffset = newOffset
}

override func scrollPageDown(_ sender: Any?) {
let newOffset = scrollView.contentOffset.applying(.init(translationX: 0, y: frame.size.height))
var newViewPort = CGRect(origin: newOffset, size: frame.size)
if newViewPort.maxY > scrollView.contentSize.height {
newViewPort = CGRect(origin: CGPoint(x: newOffset.x, y: scrollView.contentSize.height - frame.size.height), size: frame.size)
}
textViewController.viewport = newViewPort
textViewController.layoutIfNeeded()
scrollView.contentOffset = newOffset
}
}

// MARK: - Window
Expand Down

0 comments on commit ea047f3

Please sign in to comment.