-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathBrowserViewController+KeyCommands.swift
146 lines (123 loc) · 7.63 KB
/
BrowserViewController+KeyCommands.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
import Shared
// Naming functions: use the suffix 'KeyCommand' for an additional level of namespacing (bug 1415830)
extension BrowserViewController {
@objc private func reloadTabKeyCommand() {
UnifiedTelemetry.recordEvent(category: .action, method: .press, object: .keyCommand, extras: ["action": "reload"])
if let tab = tabManager.selectedTab, homePanelController == nil {
tab.reload()
}
}
@objc private func goBackKeyCommand() {
UnifiedTelemetry.recordEvent(category: .action, method: .press, object: .keyCommand, extras: ["action": "go-back"])
if let tab = tabManager.selectedTab, tab.canGoBack, homePanelController == nil {
tab.goBack()
}
}
@objc private func goForwardKeyCommand() {
UnifiedTelemetry.recordEvent(category: .action, method: .press, object: .keyCommand, extras: ["action": "go-forward"])
if let tab = tabManager.selectedTab, tab.canGoForward {
tab.goForward()
}
}
@objc private func findInPageKeyCommand() {
UnifiedTelemetry.recordEvent(category: .action, method: .press, object: .keyCommand, extras: ["action": "find-in-page"])
if let tab = tabManager.selectedTab, homePanelController == nil {
self.tab(tab, didSelectFindInPageForSelection: "")
}
}
@objc private func selectLocationBarKeyCommand() {
UnifiedTelemetry.recordEvent(category: .action, method: .press, object: .keyCommand, extras: ["action": "select-location-bar"])
scrollController.showToolbars(animated: true)
urlBar.tabLocationViewDidTapLocation(urlBar.locationView)
}
@objc private func newTabKeyCommand() {
UnifiedTelemetry.recordEvent(category: .action, method: .press, object: .keyCommand, extras: ["action": "new-tab"])
openBlankNewTab(focusLocationField: true, isPrivate: false)
}
@objc private func newPrivateTabKeyCommand() {
// NOTE: We cannot and should not distinguish between "new-tab" and "new-private-tab"
// when recording telemetry for key commands.
UnifiedTelemetry.recordEvent(category: .action, method: .press, object: .keyCommand, extras: ["action": "new-tab"])
openBlankNewTab(focusLocationField: true, isPrivate: true)
}
@objc private func closeTabKeyCommand() {
UnifiedTelemetry.recordEvent(category: .action, method: .press, object: .keyCommand, extras: ["action": "close-tab"])
guard let currentTab = tabManager.selectedTab else {
return
}
tabManager.removeTab(currentTab)
}
@objc private func nextTabKeyCommand() {
UnifiedTelemetry.recordEvent(category: .action, method: .press, object: .keyCommand, extras: ["action": "next-tab"])
guard let currentTab = tabManager.selectedTab else {
return
}
let tabs = currentTab.isPrivate ? tabManager.privateTabs : tabManager.normalTabs
if let index = tabs.index(of: currentTab), index + 1 < tabs.count {
tabManager.selectTab(tabs[index + 1])
} else if let firstTab = tabs.first {
tabManager.selectTab(firstTab)
}
}
@objc private func previousTabKeyCommand() {
UnifiedTelemetry.recordEvent(category: .action, method: .press, object: .keyCommand, extras: ["action": "previous-tab"])
guard let currentTab = tabManager.selectedTab else {
return
}
let tabs = currentTab.isPrivate ? tabManager.privateTabs : tabManager.normalTabs
if let index = tabs.index(of: currentTab), index - 1 < tabs.count && index != 0 {
tabManager.selectTab(tabs[index - 1])
} else if let lastTab = tabs.last {
tabManager.selectTab(lastTab)
}
}
@objc private func showTabTrayKeyCommand() {
UnifiedTelemetry.recordEvent(category: .action, method: .press, object: .keyCommand, extras: ["action": "show-tab-tray"])
showTabTray()
}
@objc private func moveURLCompletionKeyCommand(sender: UIKeyCommand) {
guard let searchController = self.searchController else {
return
}
searchController.handleKeyCommands(sender: sender)
}
override var keyCommands: [UIKeyCommand]? {
let searchLocationCommands = [
UIKeyCommand(input: UIKeyInputDownArrow, modifierFlags: [], action: #selector(moveURLCompletionKeyCommand(sender:))),
UIKeyCommand(input: UIKeyInputUpArrow, modifierFlags: [], action: #selector(moveURLCompletionKeyCommand(sender:))),
]
let overidesTextEditing = [
UIKeyCommand(input: UIKeyInputRightArrow, modifierFlags: [.command, .shift], action: #selector(nextTabKeyCommand)),
UIKeyCommand(input: UIKeyInputLeftArrow, modifierFlags: [.command, .shift], action: #selector(previousTabKeyCommand)),
UIKeyCommand(input: UIKeyInputLeftArrow, modifierFlags: .command, action: #selector(goBackKeyCommand)),
UIKeyCommand(input: UIKeyInputRightArrow, modifierFlags: .command, action: #selector(goForwardKeyCommand)),
]
let tabNavigation = [
UIKeyCommand(input: "r", modifierFlags: .command, action: #selector(reloadTabKeyCommand), discoverabilityTitle: Strings.ReloadPageTitle),
UIKeyCommand(input: "[", modifierFlags: .command, action: #selector(goBackKeyCommand), discoverabilityTitle: Strings.BackTitle),
UIKeyCommand(input: "]", modifierFlags: .command, action: #selector(goForwardKeyCommand), discoverabilityTitle: Strings.ForwardTitle),
UIKeyCommand(input: "f", modifierFlags: .command, action: #selector(findInPageKeyCommand), discoverabilityTitle: Strings.FindTitle),
UIKeyCommand(input: "l", modifierFlags: .command, action: #selector(selectLocationBarKeyCommand), discoverabilityTitle: Strings.SelectLocationBarTitle),
UIKeyCommand(input: "t", modifierFlags: .command, action: #selector(newTabKeyCommand), discoverabilityTitle: Strings.NewTabTitle),
UIKeyCommand(input: "p", modifierFlags: [.command, .shift], action: #selector(newPrivateTabKeyCommand), discoverabilityTitle: Strings.NewPrivateTabTitle),
UIKeyCommand(input: "w", modifierFlags: .command, action: #selector(closeTabKeyCommand), discoverabilityTitle: Strings.CloseTabTitle),
UIKeyCommand(input: "\t", modifierFlags: .control, action: #selector(nextTabKeyCommand), discoverabilityTitle: Strings.ShowNextTabTitle),
UIKeyCommand(input: "\t", modifierFlags: [.control, .shift], action: #selector(previousTabKeyCommand), discoverabilityTitle: Strings.ShowPreviousTabTitle),
// Switch tab to match Safari on iOS.
UIKeyCommand(input: "]", modifierFlags: [.command, .shift], action: #selector(nextTabKeyCommand)),
UIKeyCommand(input: "[", modifierFlags: [.command, .shift], action: #selector(previousTabKeyCommand)),
UIKeyCommand(input: "\\", modifierFlags: [.command, .shift], action: #selector(showTabTrayKeyCommand)), // Safari on macOS
UIKeyCommand(input: "\t", modifierFlags: [.command, .alternate], action: #selector(showTabTrayKeyCommand), discoverabilityTitle: Strings.ShowTabTrayFromTabKeyCodeTitle)
]
let isEditingText = tabManager.selectedTab?.isEditing ?? false
if urlBar.inOverlayMode {
return tabNavigation + searchLocationCommands
} else if !isEditingText {
return tabNavigation + overidesTextEditing
}
return tabNavigation
}
}