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