Keyboard: scroll up/down with vim bindings

This commit is contained in:
James Magahern
2021-02-17 19:02:18 -08:00
parent 18d74efa89
commit 15aaa12797
2 changed files with 53 additions and 0 deletions

View File

@@ -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<UIPress>) {

View File

@@ -8,3 +8,11 @@
#import <UIKit/UITextField_Private.h>
#import <WebKit/WKWebViewPrivate.h>
#import <WebKit/_WKFindDelegate.h>
@interface WKContentView : UIView
@property (nonatomic, readonly) BOOL isFocusingElement;
@end
@interface WKWebView (Internal)
- (WKContentView *)_currentContentView;
@end