Keyboard: scroll up/down with vim bindings
This commit is contained in:
@@ -7,6 +7,51 @@
|
|||||||
|
|
||||||
import Foundation
|
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
|
extension BrowserViewController: ShortcutResponder
|
||||||
{
|
{
|
||||||
internal func updateCommandKeyState(forPresses presses: Set<UIPress>) {
|
internal func updateCommandKeyState(forPresses presses: Set<UIPress>) {
|
||||||
|
|||||||
@@ -8,3 +8,11 @@
|
|||||||
#import <UIKit/UITextField_Private.h>
|
#import <UIKit/UITextField_Private.h>
|
||||||
#import <WebKit/WKWebViewPrivate.h>
|
#import <WebKit/WKWebViewPrivate.h>
|
||||||
#import <WebKit/_WKFindDelegate.h>
|
#import <WebKit/_WKFindDelegate.h>
|
||||||
|
|
||||||
|
@interface WKContentView : UIView
|
||||||
|
@property (nonatomic, readonly) BOOL isFocusingElement;
|
||||||
|
@end
|
||||||
|
|
||||||
|
@interface WKWebView (Internal)
|
||||||
|
- (WKContentView *)_currentContentView;
|
||||||
|
@end
|
||||||
|
|||||||
Reference in New Issue
Block a user