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