|
| 1 | +import SwiftUI |
| 2 | + |
| 3 | +// MARK: - SwiftUIViewGenerator |
| 4 | + |
| 5 | +@objc(SwiftUIViewGenerator) class SwiftUIViewGenerator: TileableEffect { |
| 6 | + // MARK: Internal |
| 7 | + |
| 8 | + override var properties: [String: Any] { |
| 9 | + [ |
| 10 | + kFxPropertyKey_MayRemapTime: false, |
| 11 | + kFxPropertyKey_VariesWhenParamsAreStatic: true |
| 12 | + ] |
| 13 | + } |
| 14 | + |
| 15 | + override func addParameters() throws { |
| 16 | + parameterCreationAPI.addStringParameter( |
| 17 | + withName: "Package Path (Drag & Drop)", |
| 18 | + parameterID: 1, |
| 19 | + defaultValue: "", |
| 20 | + parameterFlags: FxParameterFlags(kFxParameterFlag_DEFAULT) |
| 21 | + ) |
| 22 | + |
| 23 | + parameterCreationAPI.addPushButton( |
| 24 | + withName: "Compile", |
| 25 | + parameterID: 2, |
| 26 | + selector: #selector(SwiftUIViewGenerator.compile), |
| 27 | + parameterFlags: FxParameterFlags(kFxParameterFlag_DEFAULT) |
| 28 | + ) |
| 29 | + } |
| 30 | + |
| 31 | + override func pluginInstanceAddedToDocument() { |
| 32 | + compile() |
| 33 | + } |
| 34 | + |
| 35 | + override func renderDestinationImage( |
| 36 | + sourceImages: [CIImage], |
| 37 | + pluginState _: Data?, |
| 38 | + at _: CMTime |
| 39 | + ) -> CIImage { |
| 40 | + guard let view else { return .clear } |
| 41 | + return DispatchQueue.main.sync { |
| 42 | + let renderer = ImageRenderer( |
| 43 | + content: view |
| 44 | + .frame(width: sourceImages[0].extent.width, height: sourceImages[0].extent.height) |
| 45 | + ) |
| 46 | + renderer.proposedSize = .init(sourceImages[0].extent.size) |
| 47 | + return CIImage(cgImage: renderer.cgImage!, options: [.applyOrientationProperty: true]) |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + // MARK: Private |
| 52 | + |
| 53 | + private var view: AnyView? |
| 54 | +} |
| 55 | + |
| 56 | +extension SwiftUIViewGenerator { |
| 57 | + @objc func compile() { |
| 58 | + var packagePath: NSString = "" |
| 59 | + parameterRetrievalAPI.getStringParameterValue(&packagePath, fromParameter: 1) |
| 60 | + let package = URL(filePath: packagePath as String) |
| 61 | + |
| 62 | + guard !(packagePath as String).isEmpty else { return } |
| 63 | + |
| 64 | + do { |
| 65 | + let dylib = try swiftBuild(package: package) |
| 66 | + view = try loadView(from: dylib) |
| 67 | + } catch { |
| 68 | + print(error.localizedDescription) |
| 69 | + Task { await showAlert(message: error.localizedDescription) } |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + private func swiftBuild(package: URL) throws -> URL { |
| 74 | + let dylib = package.appendingPathComponent(".build/debug/lib\(package.lastPathComponent).dylib") |
| 75 | + try? FileManager.default.removeItem(at: dylib) |
| 76 | + |
| 77 | + let process = Process() |
| 78 | + process.executableURL = URL(filePath: "/usr/bin/swift") |
| 79 | + process.arguments = ["build", "--package-path", package.path()] |
| 80 | + |
| 81 | + let standardError = Pipe() |
| 82 | + process.standardError = standardError |
| 83 | + |
| 84 | + let semaphore = DispatchSemaphore(value: 0) |
| 85 | + var error: Error? |
| 86 | + process.terminationHandler = { terminatedProcess in |
| 87 | + if terminatedProcess.terminationStatus != 0 { |
| 88 | + error = Error.swiftError( |
| 89 | + message: (try? standardError.fileHandleForReading.readToEnd()) |
| 90 | + .map { String(decoding: $0, as: UTF8.self) } ?? "Unknown Error" |
| 91 | + ) |
| 92 | + } |
| 93 | + semaphore.signal() |
| 94 | + } |
| 95 | + try process.run() |
| 96 | + semaphore.wait() |
| 97 | + if let error { throw error } |
| 98 | + return dylib |
| 99 | + } |
| 100 | + |
| 101 | + private func loadView(from dylib: URL) throws -> AnyView { |
| 102 | + guard let handle = dlopen(dylib.path, RTLD_NOW | RTLD_LOCAL) else { |
| 103 | + throw Error.missingDylib(path: dylib) |
| 104 | + } |
| 105 | + defer { dlclose(handle) } |
| 106 | + guard let symbol = dlsym(handle, "createView") else { |
| 107 | + throw Error.missingSymbol |
| 108 | + } |
| 109 | + let pointer = unsafeBitCast(symbol, to: (@convention(c) () -> UnsafeMutableRawPointer).self)() |
| 110 | + let view = Unmanaged<AnyObject>.fromOpaque(pointer).takeRetainedValue() |
| 111 | + return view as! AnyView |
| 112 | + } |
| 113 | + |
| 114 | + private func showAlert(message: String) async { |
| 115 | + CFUserNotificationDisplayNotice( |
| 116 | + 0, |
| 117 | + kCFUserNotificationCautionAlertLevel, |
| 118 | + nil, |
| 119 | + nil, |
| 120 | + nil, |
| 121 | + "SwiftUIFX" as CFString, |
| 122 | + message as CFString, |
| 123 | + "Ok" as CFString |
| 124 | + ) |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +// MARK: - Error |
| 129 | + |
| 130 | +enum Error: Swift.Error, LocalizedError { |
| 131 | + case swiftError(message: String) |
| 132 | + case missingDylib(path: URL) |
| 133 | + case missingSymbol |
| 134 | + |
| 135 | + // MARK: Internal |
| 136 | + |
| 137 | + var errorDescription: String? { |
| 138 | + switch self { |
| 139 | + case let .swiftError(message): |
| 140 | + message |
| 141 | + case let .missingDylib(path): |
| 142 | + "Could not find dylib at \(path.path). Ensure your library product is set to type: .dynamic in your Package.swift file, and that the library name matches the package name." |
| 143 | + case .missingSymbol: |
| 144 | + """ |
| 145 | + Failed to load view from dylib. Make sure you include a createView function that returns a pointer to your view. Example: |
| 146 | +
|
| 147 | + @_cdecl("createView") public func createView() -> UnsafeMutableRawPointer { |
| 148 | + return Unmanaged.passRetained( |
| 149 | + AnyView(MyView()) as AnyObject |
| 150 | + ).toOpaque() |
| 151 | + } |
| 152 | + """ |
| 153 | + } |
| 154 | + } |
| 155 | +} |
0 commit comments