Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

T3KeyboardCommandsModule.swift

139 lines4.6 KB

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