Merge pull request #2248 from Wevah/shift-space

Make shift-space scroll up in the detail view even when it doesn't have focus
This commit is contained in:
Maurice Parker 2020-07-10 14:20:47 -05:00 committed by GitHub
commit 6672013111
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 88 additions and 10 deletions

View File

@ -66,9 +66,17 @@ final class DetailViewController: NSViewController, WKUIDelegate {
currentWebViewController.canScrollDown(callback)
}
func canScrollUp(_ callback: @escaping (Bool) -> Void) {
currentWebViewController.canScrollUp(callback)
}
override func scrollPageDown(_ sender: Any?) {
currentWebViewController.scrollPageDown(sender)
}
override func scrollPageUp(_ sender: Any?) {
currentWebViewController.scrollPageUp(sender)
}
// MARK: - Navigation

View File

@ -167,9 +167,19 @@ final class DetailWebViewController: NSViewController, WKUIDelegate {
}
}
func canScrollUp(_ completion: @escaping (Bool) -> Void) {
fetchScrollInfo { (scrollInfo) in
completion(scrollInfo?.canScrollUp ?? false)
}
}
override func scrollPageDown(_ sender: Any?) {
webView.scrollPageDown(sender)
}
override func scrollPageUp(_ sender: Any?) {
webView.scrollPageUp(sender)
}
}
// MARK: - WKScriptMessageHandler

View File

@ -264,6 +264,19 @@ class MainWindowController : NSWindowController, NSUserInterfaceValidations {
}
}
@IBAction func scrollUp(_ sender: Any?) {
guard let detailViewController = detailViewController else {
return
}
detailViewController.canScrollUp { (canScroll) in
if (canScroll) {
NSCursor.setHiddenUntilMouseMoves(true)
detailViewController.scrollPageUp(sender)
}
}
}
@IBAction func openArticleInBrowser(_ sender: Any?) {
if let link = currentLink {
Browser.open(link, invertPreference: NSApp.currentEvent?.modifierFlags.contains(.shift) ?? false)

View File

@ -10,6 +10,16 @@
<key>action</key>
<string>scrollOrGoToNextUnread:</string>
</dict>
<dict>
<key>title</key>
<string>Scroll or Go to Next Unread</string>
<key>key</key>
<string>[space]</string>
<key>shiftModifier</key>
<true/>
<key>action</key>
<string>scrollUp:</string>
</dict>
<dict>
<key>title</key>
<string>Go to Previous Unread</string>

View File

@ -293,9 +293,17 @@ class ArticleViewController: UIViewController {
return currentWebViewController?.canScrollDown() ?? false
}
func canScrollUp() -> Bool {
return currentWebViewController?.canScrollUp() ?? false
}
func scrollPageDown() {
currentWebViewController?.scrollPageDown()
}
func scrollPageUp() {
currentWebViewController?.scrollPageUp()
}
func stopArticleExtractorIfProcessing() {
currentWebViewController?.stopArticleExtractorIfProcessing()

View File

@ -123,24 +123,38 @@ class WebViewController: UIViewController {
func canScrollDown() -> Bool {
guard let webView = webView else { return false }
return webView.scrollView.contentOffset.y < finalScrollPosition()
return webView.scrollView.contentOffset.y < finalScrollPosition(scrollingUp: false)
}
func scrollPageDown() {
func canScrollUp() -> Bool {
guard let webView = webView else { return false }
return webView.scrollView.contentOffset.y > finalScrollPosition(scrollingUp: true)
}
private func scrollPage(up scrollingUp: Bool) {
guard let webView = webView else { return }
let overlap = 2 * UIFont.systemFont(ofSize: UIFont.systemFontSize).lineHeight * UIScreen.main.scale
let scrollToY: CGFloat = {
let fullScroll = webView.scrollView.contentOffset.y + webView.scrollView.layoutMarginsGuide.layoutFrame.height - overlap
let final = finalScrollPosition()
return fullScroll < final ? fullScroll : final
let scrollDistance = webView.scrollView.layoutMarginsGuide.layoutFrame.height - overlap;
let fullScroll = webView.scrollView.contentOffset.y + (scrollingUp ? -scrollDistance : scrollDistance)
let final = finalScrollPosition(scrollingUp: scrollingUp)
return (scrollingUp ? fullScroll > final : fullScroll < final) ? fullScroll : final
}()
let convertedPoint = self.view.convert(CGPoint(x: 0, y: 0), to: webView.scrollView)
let scrollToPoint = CGPoint(x: convertedPoint.x, y: scrollToY)
webView.scrollView.setContentOffset(scrollToPoint, animated: true)
}
func scrollPageDown() {
scrollPage(up: false)
}
func scrollPageUp() {
scrollPage(up: true)
}
func hideClickedImage() {
webView?.evaluateJavaScript("hideClickedImage();")
}
@ -539,9 +553,14 @@ private extension WebViewController {
}
func finalScrollPosition() -> CGFloat {
func finalScrollPosition(scrollingUp: Bool) -> CGFloat {
guard let webView = webView else { return 0 }
return webView.scrollView.contentSize.height - webView.scrollView.bounds.height + webView.scrollView.safeAreaInsets.bottom
if scrollingUp {
return -webView.scrollView.safeAreaInsets.top
} else {
return webView.scrollView.contentSize.height - webView.scrollView.bounds.height + webView.scrollView.safeAreaInsets.bottom
}
}
func startArticleExtractor() {

View File

@ -35,6 +35,10 @@ class RootSplitViewController: UISplitViewController {
@objc func scrollOrGoToNextUnread(_ sender: Any?) {
coordinator.scrollOrGoToNextUnread()
}
@objc func scrollUp(_ sender: Any?) {
coordinator.scrollUp()
}
@objc func goToPreviousUnread(_ sender: Any?) {
coordinator.selectPrevUnread()

View File

@ -990,6 +990,12 @@ class SceneCoordinator: NSObject, UndoableCommandRunner, UnreadCountProvider {
selectNextUnread()
}
}
func scrollUp() {
if articleViewController?.canScrollUp() ?? false {
articleViewController?.scrollPageUp()
}
}
func markAllAsRead(_ articles: [Article]) {
markArticlesWithUndo(articles, statusKey: .read, flag: true)