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 var keyCommands: [UIKeyCommand]? {
24 [
25 enabledCommand("newTask", input: "n", modifiers: .command, action: #selector(newTask), title: "New Task"),
26 enabledCommand("focusSearch", input: "f", modifiers: .command, action: #selector(focusSearch), title: "Find"),
27 enabledCommand("focusSearch", input: "k", modifiers: .command, action: #selector(focusSearch), title: "Focus Search"),
28 enabledCommand("back", input: "[", modifiers: .command, action: #selector(goBack), title: "Back"),
29 enabledCommand("files", input: "f", modifiers: [.command, .shift], action: #selector(openFiles), title: "Open Files"),
30 enabledCommand("terminal", input: "t", modifiers: [.command, .shift], action: #selector(openTerminal), title: "Open Terminal"),
31 enabledCommand("review", input: "r", modifiers: [.command, .shift], action: #selector(openReview), title: "Open Review"),
32 enabledCommand("toggleSidebar", input: "\\", modifiers: .command, action: #selector(handleToggleSidebar), title: "Toggle Sidebar"),
33 ].compactMap { $0 }
34 }
35
36 func setEnabledCommands(_ commands: [String]) {
37 enabledCommands = Set(commands)
38 if isFirstResponder {
39 resignFirstResponder()
40 }
41 reclaimFirstResponderIfAvailable()
42 }
43
44 private func enabledCommand(
45 _ identifier: String,
46 input: String,
47 modifiers: UIKeyModifierFlags,
48 action: Selector,
49 title: String
50 ) -> UIKeyCommand? {
51 guard enabledCommands.contains(identifier) else { return nil }
52 return command(input, modifiers: modifiers, action: action, title: title)
53 }
54
55 public required init(appContext: AppContext? = nil) {
56 super.init(appContext: appContext)
57 NotificationCenter.default.addObserver(
58 self,
59 selector: #selector(reclaimFirstResponderIfAvailable),
60 name: UITextView.textDidEndEditingNotification,
61 object: nil
62 )
63 NotificationCenter.default.addObserver(
64 self,
65 selector: #selector(reclaimFirstResponderIfAvailable),
66 name: UITextField.textDidEndEditingNotification,
67 object: nil
68 )
69 NotificationCenter.default.addObserver(
70 self,
71 selector: #selector(reclaimFirstResponderIfAvailable),
72 name: UIApplication.didBecomeActiveNotification,
73 object: nil
74 )
75 }
76
77 deinit {
78 NotificationCenter.default.removeObserver(self)
79 }
80
81 public override func didMoveToWindow() {
82 super.didMoveToWindow()
83 reclaimFirstResponderIfAvailable()
84 }
85
86 public override func layoutSubviews() {
87 super.layoutSubviews()
88 reclaimFirstResponderIfAvailable()
89 }
90
91 private func command(
92 _ input: String,
93 modifiers: UIKeyModifierFlags,
94 action: Selector,
95 title: String
96 ) -> UIKeyCommand {
97 let command = UIKeyCommand(input: input, modifierFlags: modifiers, action: action)
98 command.discoverabilityTitle = title
99 command.wantsPriorityOverSystemBehavior = true
100 return command
101 }
102
103 @objc private func newTask() { emit("newTask") }
104 @objc private func focusSearch() { emit("focusSearch") }
105 @objc private func goBack() { emit("back") }
106 @objc private func openFiles() { emit("files") }
107 @objc private func openTerminal() { emit("terminal") }
108 @objc private func openReview() { emit("review") }
109 @objc private func handleToggleSidebar() { emit("toggleSidebar") }
110
111 private func emit(_ command: String) {
112 onCommand(["command": command])
113 }
114
115 @objc private func reclaimFirstResponderIfAvailable() {
116 DispatchQueue.main.async { [weak self] in
117 guard let self, self.window?.t3FirstResponder == nil else { return }
118 self.becomeFirstResponder()
119 }
120 }
121}
122
123private extension UIView {
124 var t3FirstResponder: UIResponder? {
125 if isFirstResponder { return self }
126 for subview in subviews {
127 if let responder = subview.t3FirstResponder { return responder }
128 }
129 return nil
130 }
131}