Open History to see the last commit
1import ExpoModulesCore
2import Security
3import UIKit
4
5// Expo Modules 2.0: plain members marked @JS are bound by the macro, while the
6// views and main-queue presenters stay in definition(). Both are merged.
7@ExpoModule("T3NativeControls")
8public final class T3NativeControlsModule: Module {
9 private let presentationSources = T3PresentationSources()
10 private var videoPresentation: T3NativeVideoPresentation?
11 private var filePresentation: T3NativeFilePresentation?
12
13 public func definition() -> ModuleDefinition {
14 Constants {
15 if #available(iOS 26.0, *) {
16 return ["supportsWorkspaceColumns": NSClassFromString("RNSSplitHostComponentView") != nil]
17 }
18 return ["supportsWorkspaceColumns": false]
19 }
20 View(T3LayoutMetricsView.self) {
21 ViewName("LayoutMetrics")
22 Events("onMetricsChange")
23 }
24
25 AsyncFunction("presentVideo") { (url: URL, title: String, sourceIdentifier: String, identifier: String, promise: Promise) in
26 try self.presentVideo(
27 url: url,
28 title: title,
29 sourceIdentifier: sourceIdentifier,
30 identifier: identifier,
31 promise: promise
32 )
33 }.runOnQueue(.main)
34
35 AsyncFunction("presentFile") { (url: URL, title: String, sourceIdentifier: String, identifier: String, promise: Promise) in
36 try self.presentFile(url: url, title: title, sourceIdentifier: sourceIdentifier,
37 identifier: identifier, promise: promise)
38 }.runOnQueue(.main)
39
40 OnDestroy {
41 let presentation = self.videoPresentation
42 let file = self.filePresentation
43 DispatchQueue.main.async {
44 presentation?.dismiss()
45 file?.dismiss()
46 }
47 }
48
49 View(T3PresentationSourceView.self) {
50 ViewName("PresentationSource")
51 Prop("identifier") { (view: T3PresentationSourceView, identifier: String) in
52 view.sources = self.presentationSources
53 view.identifier = identifier
54 }
55 }
56
57 View(T3FrostedCutoutView.self) {
58 ViewName("FrostedCutout")
59 Prop("cutoutTop") { (view: T3FrostedCutoutView, value: Double) in view.cutoutTop = CGFloat(value) }
60 Prop("cutoutWidth") { (view: T3FrostedCutoutView, value: Double) in view.cutoutWidth = CGFloat(value) }
61 Prop("cutoutHeight") { (view: T3FrostedCutoutView, value: Double) in view.cutoutHeight = CGFloat(value) }
62 Prop("cutoutRadius") { (view: T3FrostedCutoutView, value: Double) in view.cutoutRadius = CGFloat(value) }
63 Prop("appearance") { (view: T3FrostedCutoutView, value: String) in view.appearance = value }
64 }
65
66 View(T3ContextSheetSizeView.self) {
67 ViewName("ContextSheetSize")
68 Prop("contentHeight") { (view: T3ContextSheetSizeView, height: Double) in
69 view.contentHeight = CGFloat(height)
70 }
71 }
72
73 AsyncFunction("shareFileFromSource") { (url: URL, title: String, identifier: String, promise: Promise) in
74 try self.shareFile(url: url, title: title, sourceIdentifier: identifier, promise: promise)
75 }.runOnQueue(.main)
76 }
77
78 // UIKit presenters are main-actor isolated rather than run on the JS thread
79 // the macro would otherwise pick.
80 @JS
81 @MainActor
82 func dismissVideo(identifier: String) async {
83 dismissVideoPresentation(identifier: identifier)
84 }
85
86 @JS
87 @MainActor
88 func dismissFile(identifier: String) async {
89 dismissFilePresentation(identifier: identifier)
90 }
91
92 // Rotates the interface without Simulator menu UI scripting, which CI
93 // runners cannot perform (osascript is denied Accessibility access there).
94 @JS
95 @MainActor
96 func applyShowcaseOrientation(orientation: String) async {
97 let mask: UIInterfaceOrientationMask = orientation == "landscape" ? .landscapeRight : .portrait
98 for case let windowScene as UIWindowScene in UIApplication.shared.connectedScenes {
99 windowScene.requestGeometryUpdate(.iOS(interfaceOrientations: mask)) { error in
100 NSLog("T3NativeControls applyShowcaseOrientation(\(orientation)) failed: \(error)")
101 }
102 for window in windowScene.windows {
103 window.rootViewController?.setNeedsUpdateOfSupportedInterfaceOrientations()
104 }
105 }
106 }
107
108 // The geometry request above can fail transiently (for example before the
109 // scene is foreground-active), so callers poll this until it settles.
110 // Screen bounds — not the scene's interface orientation — decide the
111 // answer because they match the captured framebuffer: with iPadOS
112 // windowing active, a floating landscape window still reports a portrait
113 // screen, and screenshots would come out portrait.
114 @JS
115 @MainActor
116 func getInterfaceOrientation() async -> String {
117 guard
118 let windowScene = UIApplication.shared.connectedScenes
119 .compactMap({ $0 as? UIWindowScene })
120 .first
121 else {
122 return "unknown"
123 }
124 let bounds = windowScene.screen.coordinateSpace.bounds
125 return bounds.width > bounds.height ? "landscape" : "portrait"
126 }
127
128 private func launchArgument(_ flag: String) -> String? {
129 let arguments = ProcessInfo.processInfo.arguments
130 guard
131 let flagIndex = arguments.firstIndex(of: flag),
132 arguments.indices.contains(flagIndex + 1)
133 else {
134 return nil
135 }
136 return arguments[flagIndex + 1]
137 }
138
139 @JS
140 func getShowcasePairingUrl() -> String? {
141 launchArgument("--showcasePairingUrl")
142 }
143
144 @JS
145 func getShowcaseScene() -> String? {
146 let scenePath = NSHomeDirectory() + "/Library/Caches/T3ShowcaseScene"
147 if let storedScene = try? String(contentsOfFile: scenePath, encoding: .utf8)
148 .trimmingCharacters(in: .whitespacesAndNewlines), !storedScene.isEmpty {
149 return storedScene
150 }
151 return launchArgument("--showcaseScene")
152 }
153
154 // The palette is fixed for the whole capture, so it only ever arrives as a
155 // launch argument — unlike the scene, which the runner rewrites in place.
156 @JS
157 func getShowcaseTheme() -> String? {
158 launchArgument("--showcaseTheme")
159 }
160
161 @JS
162 func getShowcaseOrientation() -> String? {
163 launchArgument("--showcaseOrientation")
164 }
165
166 @JS
167 func prepareShowcaseCapture() {
168 for itemClass in [kSecClassGenericPassword, kSecClassInternetPassword] {
169 SecItemDelete([kSecClass as String: itemClass] as CFDictionary)
170 }
171 }
172
173 @JS(.concurrent)
174 func markShowcaseReady(scene: String) async {
175 let readyPath = NSHomeDirectory() + "/Library/Caches/T3ShowcaseReadyScene"
176 try? scene.write(toFile: readyPath, atomically: true, encoding: .utf8)
177 }
178
179 private func presentVideo(url: URL, title: String, sourceIdentifier: String, identifier: String, promise: Promise) throws {
180 let isPlayableURL = url.isFileURL
181 ? FileManager.default.isReadableFile(atPath: url.path)
182 : (["https", "http"].contains(url.scheme?.lowercased() ?? "") && url.host != nil)
183 guard videoPresentation == nil, filePresentation == nil,
184 let presenter = appContext?.utilities?.currentViewController(),
185 isPlayableURL
186 else {
187 throw NSError(
188 domain: "T3NativeVideo",
189 code: 2,
190 userInfo: [NSLocalizedDescriptionKey: "The video preview is no longer available."]
191 )
192 }
193 let presentation = T3NativeVideoPresentation(identifier: identifier, url: url, title: title) { [weak self] error in
194 self?.videoPresentation = nil
195 if let error { promise.reject(error) } else { promise.resolve(nil) }
196 }
197 videoPresentation = presentation
198 presentation.present(from: presenter, sources: presentationSources, sourceIdentifier: sourceIdentifier)
199 }
200
201 private func dismissVideoPresentation(identifier: String) {
202 if videoPresentation?.identifier == identifier { videoPresentation?.dismiss() }
203 }
204
205 private func presentFile(url: URL, title: String, sourceIdentifier: String,
206 identifier: String, promise: Promise) throws {
207 guard filePresentation == nil, videoPresentation == nil,
208 let presenter = appContext?.utilities?.currentViewController()
209 else { throw URLError(.cannotLoadFromNetwork) }
210 let file = T3NativeFilePresentation(identifier: identifier, sources: presentationSources,
211 sourceIdentifier: sourceIdentifier) { [weak self] error in
212 self?.filePresentation = nil
213 if let error { promise.reject(error) } else { promise.resolve(nil) }
214 }
215 filePresentation = file
216 file.present(url: url, title: title, from: presenter)
217 }
218
219 private func dismissFilePresentation(identifier: String) {
220 if filePresentation?.identifier == identifier { filePresentation?.dismiss() }
221 }
222
223 private func shareFile(url: URL, title: String, sourceIdentifier: String, promise: Promise) throws {
224 guard let presenter = appContext?.utilities?.currentViewController() else {
225 throw NSError(
226 domain: "T3NativePresentation",
227 code: 2,
228 userInfo: [NSLocalizedDescriptionKey: "The presenting screen is no longer open."]
229 )
230 }
231 try presentFileShare(
232 url: url,
233 title: title,
234 source: presentationSources.view(for: sourceIdentifier),
235 presenter: presenter,
236 promise: promise
237 )
238 }
239}