Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

T3NativeControlsModule.swift

104 lines4.0 KB

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 // The palette is fixed for the whole capture, so it only ever arrives as a
37 // launch argument — unlike the scene, which the runner rewrites in place.
38 Function("getShowcaseTheme") { () -> String? in
39 let arguments = ProcessInfo.processInfo.arguments
40 guard
41 let flagIndex = arguments.firstIndex(of: "--showcaseTheme"),
42 arguments.indices.contains(flagIndex + 1)
43 else {
44 return nil as String?
45 }
46 return arguments[flagIndex + 1]
47 }
48​
49 Function("getShowcaseOrientation") { () -> String? in
50 let arguments = ProcessInfo.processInfo.arguments
51 guard
52 let flagIndex = arguments.firstIndex(of: "--showcaseOrientation"),
53 arguments.indices.contains(flagIndex + 1)
54 else {
55 return nil as String?
56 }
57 return arguments[flagIndex + 1]
58 }
59​
60 // Rotates the interface without Simulator menu UI scripting, which CI
61 // runners cannot perform (osascript is denied Accessibility access there).
62 AsyncFunction("applyShowcaseOrientation") { (orientation: String) in
63 guard #available(iOS 16.0, *) else { return }
64 let mask: UIInterfaceOrientationMask = orientation == "landscape" ? .landscapeRight : .portrait
65 for case let windowScene as UIWindowScene in UIApplication.shared.connectedScenes {
66 windowScene.requestGeometryUpdate(.iOS(interfaceOrientations: mask)) { error in
67 NSLog("T3NativeControls applyShowcaseOrientation(\(orientation)) failed: \(error)")
68 }
69 for window in windowScene.windows {
70 window.rootViewController?.setNeedsUpdateOfSupportedInterfaceOrientations()
71 }
72 }
73 }.runOnQueue(.main)
74​
75 // The geometry request above can fail transiently (for example before the
76 // scene is foreground-active), so callers poll this until it settles.
77 // Screen bounds — not the scene's interface orientation — decide the
78 // answer because they match the captured framebuffer: with iPadOS
79 // windowing active, a floating landscape window still reports a portrait
80 // screen, and screenshots would come out portrait.
81 AsyncFunction("getInterfaceOrientation") { () -> String in
82 guard
83 let windowScene = UIApplication.shared.connectedScenes
84 .compactMap({ $0 as? UIWindowScene })
85 .first
86 else {
87 return "unknown"
88 }
89 let bounds = windowScene.screen.coordinateSpace.bounds
90 return bounds.width > bounds.height ? "landscape" : "portrait"
91 }.runOnQueue(.main)
92​
93 Function("prepareShowcaseCapture") {
94 for itemClass in [kSecClassGenericPassword, kSecClassInternetPassword] {
95 SecItemDelete([kSecClass as String: itemClass] as CFDictionary)
96 }
97 }
98​
99 Function("markShowcaseReady") { (scene: String) in
100 let readyPath = NSHomeDirectory() + "/Library/Caches/T3ShowcaseReadyScene"
101 try? scene.write(toFile: readyPath, atomically: true, encoding: .utf8)
102 }
103 }