Keyboard: scroll up/down with vim bindings
This commit is contained in:
@@ -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>) {
|
||||
|
||||
Reference in New Issue
Block a user