2021-02-17 18:28:18 -08:00
|
|
|
//
|
|
|
|
|
// BrowserViewController+Keyboard.swift
|
|
|
|
|
// App
|
|
|
|
|
//
|
|
|
|
|
// Created by James Magahern on 2/17/21.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
|
|
2021-02-17 19:02:18 -08:00
|
|
|
@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) {
|
2021-02-18 19:34:41 -08:00
|
|
|
return webView._contentViewIsFirstResponder && webView._currentContentView().isFocusingElement == false
|
2021-02-17 19:02:18 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-17 18:28:18 -08:00
|
|
|
extension BrowserViewController: ShortcutResponder
|
|
|
|
|
{
|
|
|
|
|
internal func updateCommandKeyState(forPresses presses: Set<UIPress>) {
|
|
|
|
|
guard let press = presses.first else { return }
|
|
|
|
|
|
|
|
|
|
if let key = press.key {
|
|
|
|
|
if key.modifierFlags == [.command] {
|
|
|
|
|
let isDown = press.phase == .began || press.phase == .changed || press.phase == .stationary
|
|
|
|
|
self.commandKeyHeld = isDown
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override func pressesBegan(_ presses: Set<UIPress>, with event: UIPressesEvent?) {
|
|
|
|
|
super.pressesBegan(presses, with: event)
|
|
|
|
|
updateCommandKeyState(forPresses: presses)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override func pressesCancelled(_ presses: Set<UIPress>, with event: UIPressesEvent?) {
|
|
|
|
|
super.pressesCancelled(presses, with: event)
|
|
|
|
|
updateCommandKeyState(forPresses: presses)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override func pressesEnded(_ presses: Set<UIPress>, with event: UIPressesEvent?) {
|
|
|
|
|
super.pressesEnded(presses, with: event)
|
|
|
|
|
updateCommandKeyState(forPresses: presses)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MARK: Keyboard Shortcuts
|
|
|
|
|
|
|
|
|
|
func focusURLBar(_ sender: Any?) {
|
|
|
|
|
toolbarController.urlBar.textField.becomeFirstResponder()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func goBack(_ sender: Any?) {
|
|
|
|
|
tab.webView.goBack()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func goForward(_ sender: Any?) {
|
|
|
|
|
tab.webView.goForward()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func createTab(_ sender: Any?) {
|
|
|
|
|
createNewTab(withURL: nil)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func previousTab(_ sender: Any?) {
|
|
|
|
|
if let tabIndex = tabController.tabs.firstIndex(of: self.tab) {
|
|
|
|
|
if tabIndex - 1 >= 0 {
|
|
|
|
|
self.tab = tabController.tabs[tabIndex - 1]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func nextTab(_ sender: Any?) {
|
|
|
|
|
if let tabIndex = tabController.tabs.firstIndex(of: self.tab) {
|
|
|
|
|
if tabIndex + 1 < tabController.tabs.count {
|
|
|
|
|
self.tab = tabController.tabs[tabIndex + 1]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func closeTab(_ sender: Any?) {
|
|
|
|
|
if tabController.tabs.count > 1 {
|
|
|
|
|
tabController.closeTab(self.tab)
|
|
|
|
|
} else {
|
|
|
|
|
#if targetEnvironment(macCatalyst)
|
|
|
|
|
if let originWindowScene = self.view.window?.windowScene {
|
|
|
|
|
UIApplication.shared.requestSceneSessionDestruction(originWindowScene.session, options: nil) { error in
|
|
|
|
|
print("Error when requesting scene destruction: " + error.localizedDescription)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func findOnPage(_ sender: Any?) {
|
|
|
|
|
browserView.setFindOnPageVisible(true, animated: true)
|
|
|
|
|
findOnPageController.findOnPageView.textField.becomeFirstResponder()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func refresh(_ sender: Any?) {
|
|
|
|
|
webView.reload()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override func increaseSize(_ sender: Any?) {
|
|
|
|
|
tab.webView._viewScale += 0.10
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override func decreaseSize(_ sender: Any?) {
|
|
|
|
|
tab.webView._viewScale -= 0.10
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|