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
|
|
|
|
|
{
|
2021-02-26 17:43:53 -08:00
|
|
|
static let keyboardScrollingEnabled = false // this is tricky...
|
2021-02-17 19:02:18 -08:00
|
|
|
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-26 17:43:53 -08:00
|
|
|
return Self.keyboardScrollingEnabled && 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 {
|
2022-02-21 16:01:01 -08:00
|
|
|
let isDown = press.phase == .began || press.phase == .changed || press.phase == .stationary
|
|
|
|
|
if key.keyCode == .keyboardLeftGUI || key.keyCode == .keyboardRightGUI {
|
2021-02-17 18:28:18 -08:00
|
|
|
self.commandKeyHeld = isDown
|
2022-02-21 16:01:01 -08:00
|
|
|
} else if key.keyCode == .keyboardLeftShift || key.keyCode == .keyboardRightShift {
|
|
|
|
|
self.shiftKeyHeld = isDown
|
2021-02-17 18:28:18 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-04 16:07:30 -08:00
|
|
|
func focusWebView(_ sender: Any?) {
|
|
|
|
|
webView.becomeFirstResponder()
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-17 18:28:18 -08:00
|
|
|
func goBack(_ sender: Any?) {
|
2024-07-29 19:21:51 -07:00
|
|
|
currentTab.webView.goBack()
|
2021-02-17 18:28:18 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func goForward(_ sender: Any?) {
|
2024-07-29 19:21:51 -07:00
|
|
|
currentTab.webView.goForward()
|
2021-02-17 18:28:18 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func createTab(_ sender: Any?) {
|
|
|
|
|
createNewTab(withURL: nil)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func previousTab(_ sender: Any?) {
|
2024-07-29 19:21:51 -07:00
|
|
|
if let tabIndex = tabController.tabs.firstIndex(of: self.currentTab) {
|
2021-02-17 18:28:18 -08:00
|
|
|
if tabIndex - 1 >= 0 {
|
2024-07-29 19:21:51 -07:00
|
|
|
self.currentTab = tabController.tabs[tabIndex - 1]
|
2021-02-17 18:28:18 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func nextTab(_ sender: Any?) {
|
2024-07-29 19:21:51 -07:00
|
|
|
if let tabIndex = tabController.tabs.firstIndex(of: self.currentTab) {
|
2021-02-17 18:28:18 -08:00
|
|
|
if tabIndex + 1 < tabController.tabs.count {
|
2024-07-29 19:21:51 -07:00
|
|
|
self.currentTab = tabController.tabs[tabIndex + 1]
|
2021-02-17 18:28:18 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func closeTab(_ sender: Any?) {
|
|
|
|
|
if tabController.tabs.count > 1 {
|
2024-07-29 19:21:51 -07:00
|
|
|
tabController.closeTab(self.currentTab)
|
2021-02-17 18:28:18 -08:00
|
|
|
} 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()
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-17 13:38:41 -07:00
|
|
|
func stop(_ sender: Any?) {
|
|
|
|
|
webView.stopLoading()
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-17 18:28:18 -08:00
|
|
|
override func increaseSize(_ sender: Any?) {
|
2024-07-29 19:21:51 -07:00
|
|
|
currentTab.webView._viewScale += 0.10
|
2021-02-17 18:28:18 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override func decreaseSize(_ sender: Any?) {
|
2024-07-29 19:21:51 -07:00
|
|
|
currentTab.webView._viewScale -= 0.10
|
2021-02-17 18:28:18 -08:00
|
|
|
}
|
2024-10-17 13:38:41 -07:00
|
|
|
|
|
|
|
|
func zoomToActualSize(_ sender: Any?) {
|
|
|
|
|
currentTab.webView._viewScale = 1.0
|
|
|
|
|
}
|
2021-02-17 18:28:18 -08:00
|
|
|
|
2021-03-03 16:17:54 -08:00
|
|
|
func showPreferences(_ sender: Any?) {
|
|
|
|
|
showSettingsWindow()
|
|
|
|
|
}
|
2021-03-08 23:38:01 -08:00
|
|
|
|
2023-01-20 17:28:15 -08:00
|
|
|
func showHistory(_ sender: Any?) {
|
|
|
|
|
showHistoryWindow()
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-08 23:38:01 -08:00
|
|
|
func toggleDarkMode(_ sender: Any?) {
|
|
|
|
|
self.darkModeEnabled = !self.darkModeEnabled
|
|
|
|
|
}
|
2022-08-22 17:18:50 -07:00
|
|
|
|
|
|
|
|
func openInReaderMode(_ sender: Any?) {
|
|
|
|
|
showReaderWindow()
|
|
|
|
|
}
|
2023-01-20 17:28:15 -08:00
|
|
|
|
2023-01-27 13:46:23 -08:00
|
|
|
func handleOpenURL(_ sender: Any?, forEvent event: OpenURLEvent?) {
|
|
|
|
|
guard let event else { return }
|
2023-01-20 17:28:15 -08:00
|
|
|
|
2024-07-29 19:21:51 -07:00
|
|
|
if currentTab.url == nil {
|
|
|
|
|
currentTab.beginLoadingURL(event.url)
|
2023-01-20 17:28:15 -08:00
|
|
|
} else {
|
2023-01-27 13:46:23 -08:00
|
|
|
createNewTab(withURL: event.url)
|
2023-01-20 17:28:15 -08:00
|
|
|
}
|
|
|
|
|
}
|
2024-08-05 18:09:04 -07:00
|
|
|
|
|
|
|
|
func raiseScriptPolicyRestriction(_ sender: Any?) {
|
|
|
|
|
guard let hostOrigin = currentTab.webView.url?.securityOrigin else { return }
|
|
|
|
|
let currentPolicy = policyManager.scriptPolicy(forOrigin: hostOrigin)
|
|
|
|
|
policyManager.setScriptPolicyType(currentPolicy.policyType--, forOrigin: hostOrigin)
|
|
|
|
|
didChangeScriptPolicy()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func lowerScriptPolicyRestriction(_ sender: Any?) {
|
|
|
|
|
guard let hostOrigin = currentTab.webView.url?.securityOrigin else { return }
|
|
|
|
|
let currentPolicy = policyManager.scriptPolicy(forOrigin: hostOrigin)
|
|
|
|
|
policyManager.setScriptPolicyType(currentPolicy.policyType++, forOrigin: hostOrigin)
|
|
|
|
|
didChangeScriptPolicy()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extension ScriptPolicy.PolicyType {
|
|
|
|
|
static postfix func ++ (obj: ScriptPolicy.PolicyType) -> ScriptPolicy.PolicyType {
|
|
|
|
|
switch obj {
|
|
|
|
|
case .alpha: .bravo
|
|
|
|
|
case .bravo: .charlie
|
|
|
|
|
case .charlie: .delta
|
|
|
|
|
case .delta: .echo
|
|
|
|
|
default: obj
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static postfix func -- (obj: ScriptPolicy.PolicyType) -> ScriptPolicy.PolicyType {
|
|
|
|
|
switch obj {
|
|
|
|
|
case .bravo: .alpha
|
|
|
|
|
case .charlie: .bravo
|
|
|
|
|
case .delta: .charlie
|
|
|
|
|
case .echo: .delta
|
|
|
|
|
default: obj
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-02-17 18:28:18 -08:00
|
|
|
}
|