This repository has been archived by the owner on May 10, 2024. It is now read-only.
/
TabTrayController+KeyCommands.swift
78 lines (68 loc) · 3.51 KB
/
TabTrayController+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
/* 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
import UIKit
extension TabTrayController {
override var keyCommands: [UIKeyCommand]? {
let toggleText = privateMode ? Strings.SwitchToNonPBMKeyCodeTitle: Strings.SwitchToPBMKeyCodeTitle
return [
UIKeyCommand(input: "`", modifierFlags: .command, action: #selector(didTogglePrivateModeKeyCommand), discoverabilityTitle: toggleText),
UIKeyCommand(input: "w", modifierFlags: .command, action: #selector(didCloseTabKeyCommand)),
UIKeyCommand(input: "\u{8}", modifierFlags: [], action: #selector(didCloseTabKeyCommand), discoverabilityTitle: Strings.CloseTabFromTabTrayKeyCodeTitle),
UIKeyCommand(input: "w", modifierFlags: [.command, .shift], action: #selector(didCloseAllTabsKeyCommand), discoverabilityTitle: Strings.CloseAllTabsFromTabTrayKeyCodeTitle),
UIKeyCommand(input: "\r", modifierFlags: [], action: #selector(didEnterTabKeyCommand), discoverabilityTitle: Strings.OpenSelectedTabFromTabTrayKeyCodeTitle),
UIKeyCommand(input: "\\", modifierFlags: [.command, .shift], action: #selector(didEnterTabKeyCommand)),
UIKeyCommand(input: "\t", modifierFlags: [.command, .alternate], action: #selector(didEnterTabKeyCommand)),
UIKeyCommand(input: "t", modifierFlags: .command, action: #selector(didOpenNewTabKeyCommand), discoverabilityTitle: Strings.OpenNewTabFromTabTrayKeyCodeTitle),
UIKeyCommand(input: UIKeyInputLeftArrow, modifierFlags: [], action: #selector(didChangeSelectedTabKeyCommand(sender:))),
UIKeyCommand(input: UIKeyInputRightArrow, modifierFlags: [], action: #selector(didChangeSelectedTabKeyCommand(sender:))),
UIKeyCommand(input: UIKeyInputDownArrow, modifierFlags: [], action: #selector(didChangeSelectedTabKeyCommand(sender:))),
UIKeyCommand(input: UIKeyInputUpArrow, modifierFlags: [], action: #selector(didChangeSelectedTabKeyCommand(sender:))),
]
}
@objc func didTogglePrivateModeKeyCommand() {
// NOTE: We cannot and should not capture telemetry here.
didTogglePrivateMode()
}
@objc func didCloseTabKeyCommand() {
if let tab = tabManager.selectedTab {
tabManager.removeTab(tab)
}
}
@objc func didCloseAllTabsKeyCommand() {
closeTabsForCurrentTray()
}
@objc func didEnterTabKeyCommand() {
_ = self.navigationController?.popViewController(animated: true)
}
@objc func didOpenNewTabKeyCommand() {
openNewTab()
}
@objc func didChangeSelectedTabKeyCommand(sender: UIKeyCommand) {
let step: Int
guard let input = sender.input else { return }
switch input {
case UIKeyInputLeftArrow:
step = -1
case UIKeyInputRightArrow:
step = 1
case UIKeyInputUpArrow:
step = -numberOfColumns
case UIKeyInputDownArrow:
step = numberOfColumns
default:
step = 0
}
let tabs = self.tabs
let currentIndex: Int
if let selected = tabManager.selectedTab {
currentIndex = tabs.index(of: selected) ?? 0
} else {
currentIndex = 0
}
let nextIndex = max(0, min(currentIndex + step, tabs.count - 1))
let nextTab = tabs[nextIndex]
tabManager.selectTab(nextTab)
}
}