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 ].compactMap { $0 }
56 if isPad {
57 commands += (1...9).compactMap { index in
58 enabledCommand(
59 "thread.jump.\(index)",
60 input: String(index),
61 modifiers: .command,
62 action: #selector(jumpToThread(_:)),
63 title: "Go to Thread \(index)"
64 )
65 }
66 }
67 return commands
68 }
69
70 func setEnabledCommands(_ commands: [String]) {
71 enabledCommands = Set(commands)
72 if isFirstResponder {
73 resignFirstResponder()
74 }
75 reclaimFirstResponderIfAvailable()
76 }
77
78 private func enabledCommand(
79 _ identifier: String,
80 input: String,
81 modifiers: UIKeyModifierFlags,
82 action: Selector,
83 title: String
84 ) -> UIKeyCommand? {
85 guard enabledCommands.contains(identifier) else { return nil }
86 return command(input, modifiers: modifiers, action: action, title: title)
87 }
88
89 public required init(appContext: AppContext? = nil) {
90 super.init(appContext: appContext)
91 NotificationCenter.default.addObserver(
92 self,
93 selector: #selector(reclaimFirstResponderIfAvailable),
94 name: UITextView.textDidEndEditingNotification,
95 object: nil
96 )
97 NotificationCenter.default.addObserver(
98 self,
99 selector: #selector(reclaimFirstResponderIfAvailable),
100 name: UITextField.textDidEndEditingNotification,
101 object: nil
102 )
103 NotificationCenter.default.addObserver(
104 self,
105 selector: #selector(reclaimFirstResponderIfAvailable),
106 name: UIApplication.didBecomeActiveNotification,
107 object: nil
108 )
109 }
110
111 deinit {
112 NotificationCenter.default.removeObserver(self)
113 }
114
115 public override func didMoveToWindow() {
116 super.didMoveToWindow()
117 reclaimFirstResponderIfAvailable()
118 }
119
120 public override func layoutSubviews() {
121 super.layoutSubviews()
122 reclaimFirstResponderIfAvailable()
123 }
124
125 private func command(
126 _ input: String,
127 modifiers: UIKeyModifierFlags,
128 action: Selector,
129 title: String
130 ) -> UIKeyCommand {
131 let command = UIKeyCommand(input: input, modifierFlags: modifiers, action: action)
132 command.discoverabilityTitle = title
133 command.wantsPriorityOverSystemBehavior = true
134 return command
135 }
136
137 @objc private func newTask() { emit("newTask") }
138 @objc private func openCommandPalette() { emit("commandPalette") }
139 @objc private func paletteNext() { emit("paletteNext") }
140 @objc private func palettePrevious() { emit("palettePrevious") }
141 @objc private func paletteDismiss() { emit("paletteDismiss") }
142 @objc private func jumpToThread(_ sender: UIKeyCommand) {
143 guard let input = sender.input else { return }
144 emit("thread.jump.\(input)")
145 }
146 @objc private func focusSearch() { emit("focusSearch") }
147 @objc private func goBack() { emit("back") }
148 @objc private func openFiles() { emit("files") }
149 @objc private func openTerminal() { emit("terminal") }
150 @objc private func openReview() { emit("review") }
151 @objc private func copyThreadReference() { emit("copyThreadReference") }
152 @objc private func handleToggleSidebar() { emit("toggleSidebar") }
153
154 private func emit(_ command: String) {
155 onCommand(["command": command])
156 }
157
158 @objc private func reclaimFirstResponderIfAvailable() {
159 DispatchQueue.main.async { [weak self] in
160 guard let self, self.window?.t3FirstResponder == nil else { return }
161 self.becomeFirstResponder()
162 }
163 }
164}
165
166private extension UIView {
167 var t3FirstResponder: UIResponder? {
168 if isFirstResponder { return self }
169 for subview in subviews {
170 if let responder = subview.t3FirstResponder { return responder }
171 }
172 return nil
173 }
174}