Keyboard shortcuts

This commit is contained in:
James Magahern
2020-09-21 18:35:39 -07:00
parent 14a28a0776
commit e934a4d3f5
5 changed files with 138 additions and 1 deletions

View File

@@ -21,5 +21,55 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
{
return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
}
override func buildMenu(with builder: UIMenuBuilder) {
let fileCommands = [
// Open Location...
UIKeyCommand(
modifiers: .command, input: "L",
title: "Open Location",
action: #selector(ShortcutResponder.focusURLBar)
),
// Go Back
UIKeyCommand(
modifiers: .command, input: "[",
title: "Go Back",
action: #selector(ShortcutResponder.goBack)
),
// Go Forward
UIKeyCommand(
modifiers: .command, input: "]",
title: "Go Forward",
action: #selector(ShortcutResponder.goForward)
),
// Create Tab
UIKeyCommand(
modifiers: .command, input: "T",
title: "New Tab",
action: #selector(ShortcutResponder.createTab)
),
// Previous Tab
UIKeyCommand(
modifiers: [.command, .shift], input: "[",
title: "Previous Tab",
action: #selector(ShortcutResponder.previousTab)
),
// Next Tab
UIKeyCommand(
modifiers: [.command, .shift], input: "]",
title: "Next Tab",
action: #selector(ShortcutResponder.nextTab)
),
]
builder.replaceChildren(ofMenu: .file) { children in
return fileCommands + children
}
}
}

View File

@@ -11,7 +11,7 @@ import UniformTypeIdentifiers
class BrowserViewController: UIViewController, WKNavigationDelegate, WKUIDelegate,
UITextFieldDelegate, ScriptPolicyViewControllerDelegate,
UIPopoverPresentationControllerDelegate, TabDelegate, TabPickerViewControllerDelegate,
AutocompleteViewControllerDelegate
AutocompleteViewControllerDelegate, ShortcutResponder
{
let browserView = BrowserView()
var tab: Tab { didSet { didChangeTab(tab) } }
@@ -428,4 +428,39 @@ class BrowserViewController: UIViewController, WKNavigationDelegate, WKUIDelegat
tab.beginLoadingURL(item.url)
autocompleteViewController.view.isHidden = true
}
// 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?) {
let newTab = tabController.createNewTab(url: nil)
self.tab = newTab
}
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]
}
}
}
}

View File

@@ -0,0 +1,29 @@
//
// KeyboardShortcuts.swift
// App
//
// Created by James Magahern on 9/21/20.
//
import UIKit
@objc
protocol ShortcutResponder: class {
@objc
optional func focusURLBar(_ sender: Any?)
@objc
optional func goBack(_ sender: Any?)
@objc
optional func goForward(_ sender: Any?)
@objc
optional func createTab(_ sender: Any?)
@objc
optional func previousTab(_ sender: Any?)
@objc
optional func nextTab(_ sender: Any?)
}

View File

@@ -0,0 +1,15 @@
//
// UIKeyCommand+ConvInit.swift
// App
//
// Created by James Magahern on 9/21/20.
//
import UIKit
extension UIKeyCommand {
convenience init(modifiers: UIKeyModifierFlags, input: String, title: String, action: Selector) {
self.init(input: input, modifierFlags: modifiers, action: action)
self.title = title
}
}