The best way to control Claude Code, Codex and more from any computer or phone

main
swift 390 lines 15.7 KB
Raw
1 import AVFoundation
2 import ExpoModulesCore
3 import Photos
4 import Security
5 import UIKit
6 import UniformTypeIdentifiers
7
8 @ExpoModule("T3NativeControls")
9 public final class T3NativeControlsModule: Module {
10 private let presentationSources = T3PresentationSources()
11 private var videoPresentation: T3NativeVideoPresentation?
12 private var filePresentation: T3NativeFilePresentation?
13 private var imageViewer: T3NativeImageViewer?
14 /// The last file preview that closed. Written on main, read synchronously from JS.
15 private let filePreviewActivity = NSLock()
16 private var finishedFilePreview: String?
17
18 public func definition() -> ModuleDefinition {
19 Events("videoPlaybackChange")
20 Constants {
21 if #available(iOS 26.0, *) {
22 return [
23 "supportsWorkspaceColumns": NSClassFromString("RNSSplitHostComponentView") != nil,
24 ]
25 }
26 return ["supportsWorkspaceColumns": false]
27 }
28 View(T3SegmentedControlView.self) {
29 ViewName("SegmentedControl")
30 Events("onSelectionChange")
31 Prop("labels") { (view: T3SegmentedControlView, value: [String]) in view.labels = value }
32 Prop("selectedIndex") { (view: T3SegmentedControlView, value: Int) in view.selectedIndex = value }
33 Prop("trackColor") { (view: T3SegmentedControlView, value: UIColor) in view.trackColor = value }
34 Prop("selectedColor") { (view: T3SegmentedControlView, value: UIColor) in view.selectedColor = value }
35 Prop("textColor") { (view: T3SegmentedControlView, value: UIColor) in view.textColor = value }
36 Prop("mutedColor") { (view: T3SegmentedControlView, value: UIColor) in view.mutedColor = value }
37 Prop("appearance") { (view: T3SegmentedControlView, value: String) in view.appearance = value }
38 }
39
40 View(T3SidebarTabBarView.self) {
41 ViewName("SidebarTabBar")
42 Events("onSelectionChange", "onCompose")
43 Prop("tabs") { (view: T3SidebarTabBarView, value: [String]) in view.tabs = value }
44 Prop("selectedTab") { (view: T3SidebarTabBarView, value: String) in view.selectedTab = value }
45 Prop("selectedColor") { (view: T3SidebarTabBarView, value: UIColor) in view.selectedColor = value }
46 Prop("showsCompose") { (view: T3SidebarTabBarView, value: Bool) in view.showsCompose = value }
47 Prop("compact") { (view: T3SidebarTabBarView, value: Bool) in view.compact = value }
48 Prop("appearance") { (view: T3SidebarTabBarView, value: String) in view.appearance = value }
49 }
50
51 View(T3LayoutMetricsView.self) {
52 ViewName("LayoutMetrics")
53 Events("onMetricsChange")
54 }
55
56 View(T3ScrollEdgeBleedView.self) {
57 ViewName("ScrollEdgeBleed")
58 }
59
60 AsyncFunction("generateDpopKey") { T3DpopKeys.generate() }
61
62 AsyncFunction("dpopKeyMatches") { (privateKey: Data, publicKey: Data) in
63 try T3DpopKeys.matches(privateKey: privateKey, publicKey: publicKey)
64 }
65
66 AsyncFunction("signDpop") { (privateKey: Data, signingInput: Data) in
67 try T3DpopKeys.sign(privateKey: privateKey, signingInput: signingInput)
68 }
69
70 AsyncFunction("presentVideo") { (url: URL, title: String, sourceIdentifier: String, identifier: String, promise: Promise) in
71 try self.presentVideo(
72 url: url,
73 title: title,
74 sourceIdentifier: sourceIdentifier,
75 identifier: identifier,
76 promise: promise
77 )
78 }.runOnQueue(.main)
79
80 AsyncFunction("presentFile") { (url: URL, title: String, sourceIdentifier: String, identifier: String, promise: Promise) in
81 try self.presentFile(url: url, title: title, sourceIdentifier: sourceIdentifier,
82 identifier: identifier, promise: promise)
83 }.runOnQueue(.main)
84
85 // A nil URL is still resolving in JS and arrives later through updateFile.
86 AsyncFunction("presentFiles") { (urls: [URL?], titles: [String], selectedIndex: Int, sourceIdentifiers: [String], identifier: String, promise: Promise) in
87 guard !urls.isEmpty, urls.count == titles.count, urls.count == sourceIdentifiers.count,
88 urls.indices.contains(selectedIndex)
89 else {
90 throw URLError(.badURL)
91 }
92 // A viewer still animating closed must not block the next one, as Quick Look's error alert did.
93 self.imageViewer?.finishImmediately()
94 guard self.filePresentation == nil, self.videoPresentation == nil,
95 let presenter = self.appContext?.utilities?.currentViewController()
96 else { throw URLError(.cannotLoadFromNetwork) }
97 let viewer = T3NativeImageViewer(
98 identifier: identifier, sources: self.presentationSources, sourceIdentifiers: sourceIdentifiers,
99 onFinish: { [weak self] in self?.setFinishedFilePreview(identifier) },
100 completion: { [weak self] error in
101 if self?.imageViewer?.identifier == identifier { self?.imageViewer = nil }
102 if let error { promise.reject(error) } else { promise.resolve(nil) }
103 }
104 )
105 self.imageViewer = viewer
106 viewer.present(urls: urls, titles: titles, selectedIndex: selectedIndex, from: presenter)
107 }.runOnQueue(.main)
108
109 // A tap can reach JS before the old viewer's close does; this tells it whether that viewer is gone.
110 // A presentation main has not started yet reads as still open.
111 Function("isFilePreviewFinished") { (identifier: String) -> Bool in
112 self.filePreviewActivity.withLock { self.finishedFilePreview == identifier }
113 }
114
115 // A nil URL marks the item as one that could not be resolved.
116 AsyncFunction("updateFile") { (identifier: String, index: Int, url: URL?) in
117 if self.imageViewer?.identifier == identifier { self.imageViewer?.update(index: index, url: url) }
118 }.runOnQueue(.main)
119
120 OnDestroy {
121 let presentation = self.videoPresentation
122 let file = self.filePresentation
123 let images = self.imageViewer
124 DispatchQueue.main.async {
125 presentation?.dismiss()
126 file?.dismiss()
127 images?.dismiss()
128 }
129 }
130
131 View(T3PresentationSourceView.self) {
132 ViewName("PresentationSource")
133 Prop("identifier") { (view: T3PresentationSourceView, identifier: String) in
134 view.sources = self.presentationSources
135 view.identifier = identifier
136 }
137 }
138
139 View(T3FrostedCutoutView.self) {
140 ViewName("FrostedCutout")
141 Prop("cutoutTop") { (view: T3FrostedCutoutView, value: Double) in view.cutoutTop = CGFloat(value) }
142 Prop("cutoutWidth") { (view: T3FrostedCutoutView, value: Double) in view.cutoutWidth = CGFloat(value) }
143 Prop("cutoutHeight") { (view: T3FrostedCutoutView, value: Double) in view.cutoutHeight = CGFloat(value) }
144 Prop("cutoutRadius") { (view: T3FrostedCutoutView, value: Double) in view.cutoutRadius = CGFloat(value) }
145 Prop("appearance") { (view: T3FrostedCutoutView, value: String) in view.appearance = value }
146 }
147
148 View(T3ContextSheetSizeView.self) {
149 ViewName("ContextSheetSize")
150 Prop("contentHeight") { (view: T3ContextSheetSizeView, height: Double) in
151 view.contentHeight = CGFloat(height)
152 }
153 }
154
155 AsyncFunction("shareFileFromSource") { (url: URL, title: String, identifier: String, promise: Promise) in
156 try self.shareFile(url: url, title: title, sourceIdentifier: identifier, promise: promise)
157 }.runOnQueue(.main)
158
159 // Photos names the asset itself, so iOS ignores the Android display name.
160 AsyncFunction("saveImageToLibrary") { (url: URL, mimeType: String, name: String, promise: Promise) in
161 Self.saveImageToLibrary(url: url, mimeType: mimeType, name: name, promise: promise)
162 }
163 }
164
165 // UIKit presenters are main-actor isolated rather than run on the JS thread
166 // the macro would otherwise pick.
167 @JS
168 @MainActor
169 func dismissVideo(identifier: String) async {
170 dismissVideoPresentation(identifier: identifier)
171 }
172
173 // expo-video activates the session for inline playback but never deactivates
174 // it. Any other category or mode means another feature (recording) owns it.
175 @JS
176 @MainActor
177 func releaseVideoAudioSession() async {
178 // The fullscreen presentation releases the session in its own teardown.
179 guard videoPresentation == nil else { return }
180 let session = AVAudioSession.sharedInstance()
181 guard session.category == .playback, session.mode == .moviePlayback else { return }
182 try? session.setActive(false, options: [.notifyOthersOnDeactivation])
183 }
184
185 @JS
186 @MainActor
187 func dismissFile(identifier: String) async {
188 dismissFilePresentation(identifier: identifier)
189 }
190
191 // Rotates the interface without Simulator menu UI scripting, which CI
192 // runners cannot perform (osascript is denied Accessibility access there).
193 @JS
194 @MainActor
195 func applyShowcaseOrientation(orientation: String) async {
196 let mask: UIInterfaceOrientationMask = orientation == "landscape" ? .landscapeRight : .portrait
197 for case let windowScene as UIWindowScene in UIApplication.shared.connectedScenes {
198 windowScene.requestGeometryUpdate(.iOS(interfaceOrientations: mask)) { error in
199 NSLog("T3NativeControls applyShowcaseOrientation(\(orientation)) failed: \(error)")
200 }
201 for window in windowScene.windows {
202 window.rootViewController?.setNeedsUpdateOfSupportedInterfaceOrientations()
203 }
204 }
205 }
206
207 // Geometry requests can fail before foreground activation; callers retry until bounds match.
208 // Use screen bounds because iPadOS floating-window orientation can differ from the captured framebuffer.
209 @JS
210 @MainActor
211 func getInterfaceOrientation() async -> String {
212 guard
213 let windowScene = UIApplication.shared.connectedScenes
214 .compactMap({ $0 as? UIWindowScene })
215 .first
216 else {
217 return "unknown"
218 }
219 let bounds = windowScene.screen.coordinateSpace.bounds
220 return bounds.width > bounds.height ? "landscape" : "portrait"
221 }
222
223 @JS
224 @MainActor
225 func isProtectedDataAvailable() async -> Bool {
226 UIApplication.shared.isProtectedDataAvailable
227 }
228
229 private func launchArgument(_ flag: String) -> String? {
230 let arguments = ProcessInfo.processInfo.arguments
231 guard
232 let flagIndex = arguments.firstIndex(of: flag),
233 arguments.indices.contains(flagIndex + 1)
234 else {
235 return nil
236 }
237 return arguments[flagIndex + 1]
238 }
239
240 @JS
241 func getShowcasePairingUrl() -> String? {
242 launchArgument("--showcasePairingUrl")
243 }
244
245 @JS
246 func getShowcaseScene() -> String? {
247 let scenePath = NSHomeDirectory() + "/Library/Caches/T3ShowcaseScene"
248 if let storedScene = try? String(contentsOfFile: scenePath, encoding: .utf8)
249 .trimmingCharacters(in: .whitespacesAndNewlines), !storedScene.isEmpty {
250 return storedScene
251 }
252 return launchArgument("--showcaseScene")
253 }
254
255 // The palette is fixed for the whole capture, so it only ever arrives as a
256 // launch argument — unlike the scene, which the runner rewrites in place.
257 @JS
258 func getShowcaseTheme() -> String? {
259 launchArgument("--showcaseTheme")
260 }
261
262 @JS
263 func getShowcaseOrientation() -> String? {
264 launchArgument("--showcaseOrientation")
265 }
266
267 @JS
268 func prepareShowcaseCapture() {
269 for itemClass in [kSecClassGenericPassword, kSecClassInternetPassword] {
270 SecItemDelete([kSecClass as String: itemClass] as CFDictionary)
271 }
272 }
273
274 @JS(.concurrent)
275 func markShowcaseReady(scene: String) async {
276 let readyPath = NSHomeDirectory() + "/Library/Caches/T3ShowcaseReadyScene"
277 try? scene.write(toFile: readyPath, atomically: true, encoding: .utf8)
278 }
279
280 private func presentVideo(url: URL, title: String, sourceIdentifier: String, identifier: String, promise: Promise) throws {
281 let isPlayableURL = url.isFileURL
282 ? FileManager.default.isReadableFile(atPath: url.path)
283 : (["https", "http"].contains(url.scheme?.lowercased() ?? "") && url.host != nil)
284 guard videoPresentation == nil, filePresentation == nil, imageViewer == nil,
285 let presenter = appContext?.utilities?.currentViewController(),
286 isPlayableURL
287 else {
288 throw NSError(
289 domain: "T3NativeVideo",
290 code: 2,
291 userInfo: [NSLocalizedDescriptionKey: "The video preview is no longer available."]
292 )
293 }
294 let presentation = T3NativeVideoPresentation(
295 identifier: identifier, url: url, title: title,
296 onPlaybackChange: { [weak self] isPlaying in
297 self?.sendEvent("videoPlaybackChange", ["identifier": identifier, "isPlaying": isPlaying])
298 },
299 completion: { [weak self] error in
300 self?.videoPresentation = nil
301 if let error { promise.reject(error) } else { promise.resolve(nil) }
302 }
303 )
304 videoPresentation = presentation
305 presentation.present(from: presenter, sources: presentationSources, sourceIdentifier: sourceIdentifier)
306 }
307
308 private func dismissVideoPresentation(identifier: String) {
309 if videoPresentation?.identifier == identifier { videoPresentation?.dismiss() }
310 }
311
312 private func makeFilePresentation(sourceIdentifier: String, identifier: String,
313 promise: Promise) throws -> T3NativeFilePresentation {
314 guard filePresentation == nil, videoPresentation == nil, imageViewer == nil,
315 appContext?.utilities?.currentViewController() != nil
316 else { throw URLError(.cannotLoadFromNetwork) }
317 let file = T3NativeFilePresentation(
318 identifier: identifier, sources: presentationSources, sourceIdentifier: sourceIdentifier,
319 onFinish: { [weak self] in self?.setFinishedFilePreview(identifier) },
320 completion: { [weak self] error in
321 self?.filePresentation = nil
322 if let error { promise.reject(error) } else { promise.resolve(nil) }
323 }
324 )
325 filePresentation = file
326 return file
327 }
328
329 private func setFinishedFilePreview(_ identifier: String) {
330 filePreviewActivity.withLock { finishedFilePreview = identifier }
331 }
332
333 private func presentFile(url: URL, title: String, sourceIdentifier: String,
334 identifier: String, promise: Promise) throws {
335 let file = try makeFilePresentation(sourceIdentifier: sourceIdentifier, identifier: identifier, promise: promise)
336 guard let presenter = appContext?.utilities?.currentViewController() else { file.dismiss(); return }
337 file.present(url: url, title: title, from: presenter)
338 }
339
340 private func dismissFilePresentation(identifier: String) {
341 if filePresentation?.identifier == identifier { filePresentation?.dismiss() }
342 if imageViewer?.identifier == identifier { imageViewer?.dismiss() }
343 }
344
345 private static func saveImageToLibrary(url: URL, mimeType: String, name: String, promise: Promise) {
346 PHPhotoLibrary.requestAuthorization(for: .addOnly) { status in
347 guard status == .authorized || status == .limited else {
348 promise.reject(NSError(
349 domain: "T3NativePhotos",
350 code: 1,
351 userInfo: [NSLocalizedDescriptionKey: "Allow T3 Code to add photos in Settings."]
352 ))
353 return
354 }
355 PHPhotoLibrary.shared().performChanges({
356 let options = PHAssetResourceCreationOptions()
357 options.uniformTypeIdentifier = UTType(mimeType: mimeType)?.identifier
358 options.originalFilename = name
359 PHAssetCreationRequest.forAsset().addResource(with: .photo, fileURL: url, options: options)
360 }, completionHandler: { saved, error in
361 if saved {
362 promise.resolve(nil)
363 } else {
364 promise.reject(error ?? NSError(
365 domain: "T3NativePhotos",
366 code: 2,
367 userInfo: [NSLocalizedDescriptionKey: "Photos could not save this image."]
368 ))
369 }
370 })
371 }
372 }
373
374 private func shareFile(url: URL, title: String, sourceIdentifier: String, promise: Promise) throws {
375 guard let presenter = appContext?.utilities?.currentViewController() else {
376 throw NSError(
377 domain: "T3NativePresentation",
378 code: 2,
379 userInfo: [NSLocalizedDescriptionKey: "The presenting screen is no longer open."]
380 )
381 }
382 try presentFileShare(
383 url: url,
384 title: title,
385 source: presentationSources.view(for: sourceIdentifier),
386 presenter: presenter,
387 promise: promise
388 )
389 }
390 }