Open History to see the last commit
1import ExpoModulesCore
2import UIKit
3
4public final class T3KeyboardCommandsModule: Module {
5 public func definition() -> ModuleDefinition {
6 Name("T3KeyboardCommands")
7
8 View(T3KeyboardCommandsView.self) {
9 Prop("enabledCommands") { (view: T3KeyboardCommandsView, commands: [String]) in
10 view.setEnabledCommands(commands)
11 }
12 Events("onCommand")
13 }
14 }
15}
16
17public final class T3KeyboardCommandsView: ExpoView {
18 let onCommand = EventDispatcher()
19 private var enabledCommands = Set<String>()
20
21 public override var canBecomeFirstResponder: Bool { true }
22
23 public override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
24 if action == #selector(openCommandPalette) || action == #selector(paletteNext) || action == #selector(palettePrevious) || action == #selector(paletteDismiss),
25 let input = window?.t3FirstResponder as? UITextInput,
26 input.markedTextRange != nil {
27 return false
28 }
29 return super.canPerformAction(action, withSender: sender)
30 }
31
32 public override var keyCommands: [UIKeyCommand]? {
33 let isPad = UIDevice.current.userInterfaceIdiom == .pad
34 var commands = [
35 enabledCommand("newTask", input: "n", modifiers: .command, action: #selector(newTask), title: "New Task"),
36 enabledCommand("focusSearch", input: "f", modifiers: .command, action: #selector(focusSearch), title: "Find"),
37 isPad
38 ? enabledCommand("commandPalette", input: "k", modifiers: .command, action: #selector(openCommandPalette), title: "Command Palette")
39 : enabledCommand("focusSearch", input: "k", modifiers: .command, action: #selector(focusSearch), title: "Focus Search"),
40 enabledCommand("paletteNext", input: UIKeyCommand.inputDownArrow, modifiers: [], action: #selector(paletteNext), title: "Next Result"),
41 enabledCommand("palettePrevious", input: UIKeyCommand.inputUpArrow, modifiers: [], action: #selector(palettePrevious), title: "Previous Result"),
42 enabledCommand("paletteDismiss", input: UIKeyCommand.inputEscape, modifiers: [], action: #selector(paletteDismiss), title: "Close Command Palette"),
43 enabledCommand("back", input: "[", modifiers: .command, action: #selector(goBack), title: "Back"),
44 enabledCommand("files", input: "f", modifiers: [.command, .shift], action: #selector(openFiles), title: "Open Files"),
45 enabledCommand("terminal", input: "t", modifiers: [.command, .shift], action: #selector(openTerminal), title: "Open Terminal"),
46 enabledCommand("review", input: "r", modifiers: [.command, .shift], action: #selector(openReview), title: "Open Review"),
47 enabledCommand(
48 "copyThreadReference",
49 input: "c",
50 modifiers: [.command, .shift],
51 action: #selector(copyThreadReference),
52 title: "Copy PR Link or Thread ID"
53 ),
54 enabledCommand("toggleSidebar", input: "\\", modifiers: .command, action: #selector(handleToggleSidebar), title: "Toggle Sidebar"),
55 enabledCommand("cycleHost", input: "h", modifiers: [.command, .shift], action: #selector(cycleHost), title: "Next Machine"),
56 ].compactMap { $0 }
57 if isPad {
58 commands += (1...9).compactMap { index in
59 enabledCommand(
60 "thread.jump.\(index)",
61 input: String(index),
62 modifiers: .command,
63 action: #selector(jumpToThread(_:)),
64 title: "Go to Thread \(index)"
65 )
66 }
67 }
68 return commands
69 }
70
71 func setEnabledCommands(_ commands: [String]) {
72 enabledCommands = Set(commands)
73 if isFirstResponder {
74 resignFirstResponder()
75 }
76 reclaimFirstResponderIfAvailable()
77 }
78
79 private func enabledCommand(
80 _ identifier: String,
81 input: String,
82 modifiers: UIKeyModifierFlags,
83 action: Selector,
84 title: String
85 ) -> UIKeyCommand? {
86 guard enabledCommands.contains(identifier) else { return nil }
87 return command(input, modifiers: modifiers, action: action, title: title)
88 }
89
90 public required init(appContext: AppContext? = nil) {
91 super.init(appContext: appContext)
92 NotificationCenter.default.addObserver(
93 self,
94 selector: #selector(reclaimFirstResponderIfAvailable),
95 name: UITextView.textDidEndEditingNotification,
96 object: nil
97 )
98 NotificationCenter.default.addObserver(
99 self,
100 selector: #selector(reclaimFirstResponderIfAvailable),
101 name: UITextField.textDidEndEditingNotification,
102 object: nil
103 )
104 NotificationCenter.default.addObserver(
105 self,
106 selector: #selector(reclaimFirstResponderIfAvailable),
107 name: UIApplication.didBecomeActiveNotification,
108 object: nil
109 )
110 }
111
112 deinit {
113 NotificationCenter.default.removeObserver(self)
114 }
115
116 public override func didMoveToWindow() {
117 super.didMoveToWindow()
118 reclaimFirstResponderIfAvailable()
119 }
120
121 public override func layoutSubviews() {
122 super.layoutSubviews()
123 reclaimFirstResponderIfAvailable()
124 }
125
126 private func command(
127 _ input: String,
128 modifiers: UIKeyModifierFlags,
129 action: Selector,
130 title: String
131 ) -> UIKeyCommand {
132 let command = UIKeyCommand(input: input, modifierFlags: modifiers, action: action)
133 command.discoverabilityTitle = title
134 command.wantsPriorityOverSystemBehavior = true
135 return command
136 }
137
138 @objc private func newTask() { emit("newTask") }
139 @objc private func openCommandPalette() { emit("commandPalette") }
140 @objc private func paletteNext() { emit("paletteNext") }
141 @objc private func palettePrevious() { emit("palettePrevious") }
142 @objc private func paletteDismiss() { emit("paletteDismiss") }
143 @objc private func jumpToThread(_ sender: UIKeyCommand) {
144 guard let input = sender.input else { return }
145 emit("thread.jump.\(input)")
146 }
147 @objc private func focusSearch() { emit("focusSearch") }
148 @objc private func goBack() { emit("back") }
149 @objc private func openFiles() { emit("files") }
150 @objc private func openTerminal() { emit("terminal") }
151 @objc private func openReview() { emit("review") }
152 @objc private func copyThreadReference() { emit("copyThreadReference") }
153 @objc private func handleToggleSidebar() { emit("toggleSidebar") }
154 @objc private func cycleHost() { emit("cycleHost") }
155
156 private func emit(_ command: String) {
157 onCommand(["command": command])
158 }
159
160 @objc private func reclaimFirstResponderIfAvailable() {
161 DispatchQueue.main.async { [weak self] in
162 guard let self, self.window?.t3FirstResponder == nil else { return }
163 self.becomeFirstResponder()
164 }
165 }
166}
167
168private extension UIView {
169 var t3FirstResponder: UIResponder? {
170 if isFirstResponder { return self }
171 for subview in subviews {
172 if let responder = subview.t3FirstResponder { return responder }
173 }
174 return nil
175 }
176}