diff --git a/App/Browser View/BrowserViewController+Keyboard.swift b/App/Browser View/BrowserViewController+Keyboard.swift index 6abaee7..38f9c6b 100644 --- a/App/Browser View/BrowserViewController+Keyboard.swift +++ b/App/Browser View/BrowserViewController+Keyboard.swift @@ -7,6 +7,51 @@ import Foundation +@objc +protocol VIMBindings +{ + func scrollDownPressed(_ sender: Any?) + func scrollUpPressed(_ sender: Any?) +} + +extension BrowserViewController: VIMBindings +{ + static let keyboardScrollAmount: CGFloat = 33.0 + + override var keyCommands: [UIKeyCommand]? { + get { + [ + UIKeyCommand(input: "j", modifierFlags: [], action: #selector(VIMBindings.scrollDownPressed)), + UIKeyCommand(input: "k", modifierFlags: [], action: #selector(VIMBindings.scrollUpPressed)), + ] + } + } + + override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool { + if action == #selector(VIMBindings.scrollDownPressed) || action == #selector(VIMBindings.scrollUpPressed) { + return webView._currentContentView().isFocusingElement == false + } + + return super.canPerformAction(action, withSender: sender) + } + + func scrollDownPressed(_ sender: Any?) { + var offset = webView.scrollView.contentOffset + offset.y += Self.keyboardScrollAmount + offset.y = min(offset.y, webView.scrollView.contentSize.height - webView.scrollView.bounds.height) + + webView.scrollView.setContentOffset(offset, animated: false) + } + + func scrollUpPressed(_ sender: Any?) { + var offset = webView.scrollView.contentOffset + offset.y -= Self.keyboardScrollAmount + offset.y = max(offset.y, webView.scrollView.contentInset.top) + + webView.scrollView.setContentOffset(offset, animated: false) + } +} + extension BrowserViewController: ShortcutResponder { internal func updateCommandKeyState(forPresses presses: Set) { diff --git a/App/Supporting Files/SBrowser-Bridging-Header.h b/App/Supporting Files/SBrowser-Bridging-Header.h index dbf6b61..2e372a4 100644 --- a/App/Supporting Files/SBrowser-Bridging-Header.h +++ b/App/Supporting Files/SBrowser-Bridging-Header.h @@ -8,3 +8,11 @@ #import #import #import + +@interface WKContentView : UIView +@property (nonatomic, readonly) BOOL isFocusingElement; +@end + +@interface WKWebView (Internal) +- (WKContentView *)_currentContentView; +@end