iOS: Make shift-space scroll up even when the detail view doesn't have focus

This commit is contained in:
Nate Weaver 2020-07-10 13:51:41 -05:00
parent 5acce87995
commit 82f7f99827
4 changed files with 47 additions and 10 deletions

View File

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

View File

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

View File

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

View File

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