Open History to see the last commit
1import ExpoModulesCore
2import Security
3import UIKit
4
5public final class T3NativeControlsModule: Module {
6 public func definition() -> ModuleDefinition {
7 Name("T3NativeControls")
8
9 Function("getShowcasePairingUrl") {
10 let arguments = ProcessInfo.processInfo.arguments
11 guard
12 let flagIndex = arguments.firstIndex(of: "--showcasePairingUrl"),
13 arguments.indices.contains(flagIndex + 1)
14 else {
15 return nil as String?
16 }
17 return arguments[flagIndex + 1]
18 }
19
20 Function("getShowcaseScene") { () -> String? in
21 let scenePath = NSHomeDirectory() + "/Library/Caches/T3ShowcaseScene"
22 if let storedScene = try? String(contentsOfFile: scenePath, encoding: .utf8)
23 .trimmingCharacters(in: .whitespacesAndNewlines), !storedScene.isEmpty {
24 return storedScene
25 }
26 let arguments = ProcessInfo.processInfo.arguments
27 guard
28 let flagIndex = arguments.firstIndex(of: "--showcaseScene"),
29 arguments.indices.contains(flagIndex + 1)
30 else {
31 return nil as String?
32 }
33 return arguments[flagIndex + 1]
34 }
35
36 Function("getShowcaseOrientation") { () -> String? in
37 let arguments = ProcessInfo.processInfo.arguments
38 guard
39 let flagIndex = arguments.firstIndex(of: "--showcaseOrientation"),
40 arguments.indices.contains(flagIndex + 1)
41 else {
42 return nil as String?
43 }
44 return arguments[flagIndex + 1]
45 }
46
47 // Rotates the interface without Simulator menu UI scripting, which CI
48 // runners cannot perform (osascript is denied Accessibility access there).
49 AsyncFunction("applyShowcaseOrientation") { (orientation: String) in
50 guard #available(iOS 16.0, *) else { return }
51 let mask: UIInterfaceOrientationMask = orientation == "landscape" ? .landscapeRight : .portrait
52 for case let windowScene as UIWindowScene in UIApplication.shared.connectedScenes {
53 windowScene.requestGeometryUpdate(.iOS(interfaceOrientations: mask)) { error in
54 NSLog("T3NativeControls applyShowcaseOrientation(\(orientation)) failed: \(error)")
55 }
56 for window in windowScene.windows {
57 window.rootViewController?.setNeedsUpdateOfSupportedInterfaceOrientations()
58 }
59 }
60 }.runOnQueue(.main)
61
62 // The geometry request above can fail transiently (for example before the
63 // scene is foreground-active), so callers poll this until it settles.
64 // Screen bounds — not the scene's interface orientation — decide the
65 // answer because they match the captured framebuffer: with iPadOS
66 // windowing active, a floating landscape window still reports a portrait
67 // screen, and screenshots would come out portrait.
68 AsyncFunction("getInterfaceOrientation") { () -> String in
69 guard
70 let windowScene = UIApplication.shared.connectedScenes
71 .compactMap({ $0 as? UIWindowScene })
72 .first
73 else {
74 return "unknown"
75 }
76 let bounds = windowScene.screen.coordinateSpace.bounds
77 return bounds.width > bounds.height ? "landscape" : "portrait"
78 }.runOnQueue(.main)
79
80 Function("prepareShowcaseCapture") {
81 for itemClass in [kSecClassGenericPassword, kSecClassInternetPassword] {
82 SecItemDelete([kSecClass as String: itemClass] as CFDictionary)
83 }
84 }
85
86 Function("markShowcaseReady") { (scene: String) in
87 let readyPath = NSHomeDirectory() + "/Library/Caches/T3ShowcaseReadyScene"
88 try? scene.write(toFile: readyPath, atomically: true, encoding: .utf8)
89 }
90
91 View(T3HeaderButtonView.self) {
92 Prop("label") { (view: T3HeaderButtonView, label: String) in
93 view.setLabel(label)
94 }
95 Prop("systemImage") { (view: T3HeaderButtonView, systemImage: String) in
96 view.setSystemImage(systemImage)
97 }
98
99 Events("onTriggered")
100 }
101 }
102}