Open History to see the last commit
1import AVFoundation
2import ExpoModulesCore
3import Security
4import UIKit
5
6@ExpoModule("T3NativeControls")
7public final class T3NativeControlsModule: Module {
8 private let presentationSources = T3PresentationSources()
9 private var videoPresentation: T3NativeVideoPresentation?
10 private var filePresentation: T3NativeFilePresentation?
11
12 public func definition() -> ModuleDefinition {
13 Constants {
14 if #available(iOS 26.0, *) {
15 return [
16 "supportsWorkspaceColumns": NSClassFromString("RNSSplitHostComponentView") != nil,
17 ]
18 }
19 return ["supportsWorkspaceColumns": false]
20 }
21 View(T3SegmentedControlView.self) {
22 ViewName("SegmentedControl")
23 Events("onSelectionChange")
24 Prop("labels") { (view: T3SegmentedControlView, value: [String]) in view.labels = value }
25 Prop("selectedIndex") { (view: T3SegmentedControlView, value: Int) in view.selectedIndex = value }
26 Prop("trackColor") { (view: T3SegmentedControlView, value: UIColor) in view.trackColor = value }
27 Prop("selectedColor") { (view: T3SegmentedControlView, value: UIColor) in view.selectedColor = value }
28 Prop("textColor") { (view: T3SegmentedControlView, value: UIColor) in view.textColor = value }
29 Prop("mutedColor") { (view: T3SegmentedControlView, value: UIColor) in view.mutedColor = value }
30 Prop("appearance") { (view: T3SegmentedControlView, value: String) in view.appearance = value }
31 }
32
33 View(T3SidebarTabBarView.self) {
34 ViewName("SidebarTabBar")
35 Events("onSelectionChange", "onCompose")
36 Prop("tabs") { (view: T3SidebarTabBarView, value: [String]) in view.tabs = value }
37 Prop("selectedTab") { (view: T3SidebarTabBarView, value: String) in view.selectedTab = value }
38 Prop("selectedColor") { (view: T3SidebarTabBarView, value: UIColor) in view.selectedColor = value }
39 Prop("showsCompose") { (view: T3SidebarTabBarView, value: Bool) in view.showsCompose = value }
40 Prop("compact") { (view: T3SidebarTabBarView, value: Bool) in view.compact = value }
41 Prop("appearance") { (view: T3SidebarTabBarView, value: String) in view.appearance = value }
42 }
43
44 View(T3LayoutMetricsView.self) {
45 ViewName("LayoutMetrics")
46 Events("onMetricsChange")
47 }
48
49 View(T3ScrollEdgeBleedView.self) {
50 ViewName("ScrollEdgeBleed")
51 }
52
53 AsyncFunction("presentVideo") { (url: URL, title: String, sourceIdentifier: String, identifier: String, promise: Promise) in
54 try self.presentVideo(
55 url: url,
56 title: title,
57 sourceIdentifier: sourceIdentifier,
58 identifier: identifier,
59 promise: promise
60 )
61 }.runOnQueue(.main)
62
63 AsyncFunction("presentFile") { (url: URL, title: String, sourceIdentifier: String, identifier: String, promise: Promise) in
64 try self.presentFile(url: url, title: title, sourceIdentifier: sourceIdentifier,
65 identifier: identifier, promise: promise)
66 }.runOnQueue(.main)
67
68 AsyncFunction("presentFiles") { (urls: [URL], titles: [String], selectedIndex: Int, sourceIdentifiers: [String], identifier: String, promise: Promise) in
69 guard !urls.isEmpty, urls.count == titles.count, urls.count == sourceIdentifiers.count,
70 urls.indices.contains(selectedIndex)
71 else {
72 throw URLError(.badURL)
73 }
74 let file = try self.makeFilePresentation(sourceIdentifiers: sourceIdentifiers, identifier: identifier,
75 promise: promise)
76 guard let presenter = self.appContext?.utilities?.currentViewController() else {
77 file.dismiss()
78 return
79 }
80 file.present(urls: urls, titles: titles, selectedIndex: selectedIndex, from: presenter)
81 }.runOnQueue(.main)
82
83 OnDestroy {
84 let presentation = self.videoPresentation
85 let file = self.filePresentation
86 DispatchQueue.main.async {
87 presentation?.dismiss()
88 file?.dismiss()
89 }
90 }
91
92 View(T3PresentationSourceView.self) {
93 ViewName("PresentationSource")
94 Prop("identifier") { (view: T3PresentationSourceView, identifier: String) in
95 view.sources = self.presentationSources
96 view.identifier = identifier
97 }
98 }
99
100 View(T3FrostedCutoutView.self) {
101 ViewName("FrostedCutout")
102 Prop("cutoutTop") { (view: T3FrostedCutoutView, value: Double) in view.cutoutTop = CGFloat(value) }
103 Prop("cutoutWidth") { (view: T3FrostedCutoutView, value: Double) in view.cutoutWidth = CGFloat(value) }
104 Prop("cutoutHeight") { (view: T3FrostedCutoutView, value: Double) in view.cutoutHeight = CGFloat(value) }
105 Prop("cutoutRadius") { (view: T3FrostedCutoutView, value: Double) in view.cutoutRadius = CGFloat(value) }
106 Prop("appearance") { (view: T3FrostedCutoutView, value: String) in view.appearance = value }
107 }
108
109 View(T3ContextSheetSizeView.self) {
110 ViewName("ContextSheetSize")
111 Prop("contentHeight") { (view: T3ContextSheetSizeView, height: Double) in
112 view.contentHeight = CGFloat(height)
113 }
114 }
115
116 AsyncFunction("shareFileFromSource") { (url: URL, title: String, identifier: String, promise: Promise) in
117 try self.shareFile(url: url, title: title, sourceIdentifier: identifier, promise: promise)
118 }.runOnQueue(.main)
119 }
120
121 // UIKit presenters are main-actor isolated rather than run on the JS thread
122 // the macro would otherwise pick.
123 @JS
124 @MainActor
125 func dismissVideo(identifier: String) async {
126 dismissVideoPresentation(identifier: identifier)
127 }
128
129 // expo-video activates the session for inline playback but never deactivates
130 // it. Any other category or mode means another feature (recording) owns it.
131 @JS
132 @MainActor
133 func releaseVideoAudioSession() async {
134 // The fullscreen presentation releases the session in its own teardown.
135 guard videoPresentation == nil else { return }
136 let session = AVAudioSession.sharedInstance()
137 guard session.category == .playback, session.mode == .moviePlayback else { return }
138 try? session.setActive(false, options: [.notifyOthersOnDeactivation])
139 }
140
141 @JS
142 @MainActor
143 func dismissFile(identifier: String) async {
144 dismissFilePresentation(identifier: identifier)
145 }
146
147 // Rotates the interface without Simulator menu UI scripting, which CI
148 // runners cannot perform (osascript is denied Accessibility access there).
149 @JS
150 @MainActor
151 func applyShowcaseOrientation(orientation: String) async {
152 let mask: UIInterfaceOrientationMask = orientation == "landscape" ? .landscapeRight : .portrait
153 for case let windowScene as UIWindowScene in UIApplication.shared.connectedScenes {
154 windowScene.requestGeometryUpdate(.iOS(interfaceOrientations: mask)) { error in
155 NSLog("T3NativeControls applyShowcaseOrientation(\(orientation)) failed: \(error)")
156 }
157 for window in windowScene.windows {
158 window.rootViewController?.setNeedsUpdateOfSupportedInterfaceOrientations()
159 }
160 }
161 }
162
163 // Geometry requests can fail before foreground activation; callers retry until bounds match.
164 // Use screen bounds because iPadOS floating-window orientation can differ from the captured framebuffer.
165 @JS
166 @MainActor
167 func getInterfaceOrientation() async -> String {
168 guard
169 let windowScene = UIApplication.shared.connectedScenes
170 .compactMap({ $0 as? UIWindowScene })
171 .first
172 else {
173 return "unknown"
174 }
175 let bounds = windowScene.screen.coordinateSpace.bounds
176 return bounds.width > bounds.height ? "landscape" : "portrait"
177 }
178
179 @JS
180 @MainActor
181 func isProtectedDataAvailable() async -> Bool {
182 UIApplication.shared.isProtectedDataAvailable
183 }
184
185 private func launchArgument(_ flag: String) -> String? {
186 let arguments = ProcessInfo.processInfo.arguments
187 guard
188 let flagIndex = arguments.firstIndex(of: flag),
189 arguments.indices.contains(flagIndex + 1)
190 else {
191 return nil
192 }
193 return arguments[flagIndex + 1]
194 }
195
196 @JS
197 func getShowcasePairingUrl() -> String? {
198 launchArgument("--showcasePairingUrl")
199 }
200
201 @JS
202 func getShowcaseScene() -> String? {
203 let scenePath = NSHomeDirectory() + "/Library/Caches/T3ShowcaseScene"
204 if let storedScene = try? String(contentsOfFile: scenePath, encoding: .utf8)
205 .trimmingCharacters(in: .whitespacesAndNewlines), !storedScene.isEmpty {
206 return storedScene
207 }
208 return launchArgument("--showcaseScene")
209 }
210
211 // The palette is fixed for the whole capture, so it only ever arrives as a
212 // launch argument — unlike the scene, which the runner rewrites in place.
213 @JS
214 func getShowcaseTheme() -> String? {
215 launchArgument("--showcaseTheme")
216 }
217
218 @JS
219 func getShowcaseOrientation() -> String? {
220 launchArgument("--showcaseOrientation")
221 }
222
223 @JS
224 func prepareShowcaseCapture() {
225 for itemClass in [kSecClassGenericPassword, kSecClassInternetPassword] {
226 SecItemDelete([kSecClass as String: itemClass] as CFDictionary)
227 }
228 }
229
230 @JS(.concurrent)
231 func markShowcaseReady(scene: String) async {
232 let readyPath = NSHomeDirectory() + "/Library/Caches/T3ShowcaseReadyScene"
233 try? scene.write(toFile: readyPath, atomically: true, encoding: .utf8)
234 }
235
236 private func presentVideo(url: URL, title: String, sourceIdentifier: String, identifier: String, promise: Promise) throws {
237 let isPlayableURL = url.isFileURL
238 ? FileManager.default.isReadableFile(atPath: url.path)
239 : (["https", "http"].contains(url.scheme?.lowercased() ?? "") && url.host != nil)
240 guard videoPresentation == nil, filePresentation == nil,
241 let presenter = appContext?.utilities?.currentViewController(),
242 isPlayableURL
243 else {
244 throw NSError(
245 domain: "T3NativeVideo",
246 code: 2,
247 userInfo: [NSLocalizedDescriptionKey: "The video preview is no longer available."]
248 )
249 }
250 let presentation = T3NativeVideoPresentation(identifier: identifier, url: url, title: title) { [weak self] error in
251 self?.videoPresentation = nil
252 if let error { promise.reject(error) } else { promise.resolve(nil) }
253 }
254 videoPresentation = presentation
255 presentation.present(from: presenter, sources: presentationSources, sourceIdentifier: sourceIdentifier)
256 }
257
258 private func dismissVideoPresentation(identifier: String) {
259 if videoPresentation?.identifier == identifier { videoPresentation?.dismiss() }
260 }
261
262 private func makeFilePresentation(sourceIdentifiers: [String], identifier: String,
263 promise: Promise) throws -> T3NativeFilePresentation {
264 guard filePresentation == nil, videoPresentation == nil,
265 appContext?.utilities?.currentViewController() != nil
266 else { throw URLError(.cannotLoadFromNetwork) }
267 let file = T3NativeFilePresentation(identifier: identifier, sources: presentationSources,
268 sourceIdentifiers: sourceIdentifiers) { [weak self] error in
269 self?.filePresentation = nil
270 if let error { promise.reject(error) } else { promise.resolve(nil) }
271 }
272 filePresentation = file
273 return file
274 }
275
276 private func presentFile(url: URL, title: String, sourceIdentifier: String,
277 identifier: String, promise: Promise) throws {
278 let file = try makeFilePresentation(sourceIdentifiers: [sourceIdentifier], identifier: identifier, promise: promise)
279 guard let presenter = appContext?.utilities?.currentViewController() else { file.dismiss(); return }
280 file.present(url: url, title: title, from: presenter)
281 }
282
283 private func dismissFilePresentation(identifier: String) {
284 if filePresentation?.identifier == identifier { filePresentation?.dismiss() }
285 }
286
287 private func shareFile(url: URL, title: String, sourceIdentifier: String, promise: Promise) throws {
288 guard let presenter = appContext?.utilities?.currentViewController() else {
289 throw NSError(
290 domain: "T3NativePresentation",
291 code: 2,
292 userInfo: [NSLocalizedDescriptionKey: "The presenting screen is no longer open."]
293 )
294 }
295 try presentFileShare(
296 url: url,
297 title: title,
298 source: presentationSources.view(for: sourceIdentifier),
299 presenter: presenter,
300 promise: promise
301 )
302 }
303}