// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown // SPDX-License-Identifier: GPL-3.0-or-later import SwiftUI // The Setup tab's UIKit-backed pieces: the share sheet, the device lab and the // plugin check. The Mac snapshot tool has no UIKit (and doesn't compile // DeviceLabView / PluginCheck), so there they are no-ops. #if canImport(UIKit) import AVKit import UIKit enum ShareSheet { /// Presents the system share sheet for [url] over the frontmost view controller /// (a centred popover on iPad); [done] runs when it closes. @MainActor static func present(_ url: URL, done: @escaping () -> Void = {}) { let scenes = UIApplication.shared.connectedScenes.compactMap { $0 as? UIWindowScene } let scene = scenes.first { $0.activationState == .foregroundActive } ?? scenes.first guard var top = (scene?.windows.first { $0.isKeyWindow } ?? scene?.windows.first)?.rootViewController else { return } while let next = top.presentedViewController { top = next } let vc = UIActivityViewController(activityItems: [url], applicationActivities: nil) vc.completionWithItemsHandler = { _, _, _, _ in done() } if let pop = vc.popoverPresentationController { pop.sourceView = top.view pop.sourceRect = CGRect(x: top.view.bounds.midX, y: top.view.bounds.midY, width: 0, height: 0) pop.permittedArrowDirections = [] } top.present(vc, animated: true) } } extension View { /// The old device test screen (instruments, effects, benchmark), full screen. func deviceLab(isPresented: Binding) -> some View { fullScreenCover(isPresented: isPresented) { VStack(spacing: 0) { HStack { Text("Device lab").font(.headline) Spacer() Button("Done") { isPresented.wrappedValue = false }.font(.body.weight(.semibold)) } .padding(.horizontal).padding(.top, 12) DeviceLabView() } .background(Theme.background.ignoresSafeArea()) .preferredColorScheme(.dark) } } /// Loads every registered Sizzle plugin out of process and renders through it. func pluginCheck(isPresented: Binding) -> some View { sheet(isPresented: isPresented) { PluginCheckSheet { isPresented.wrappedValue = false } } } } private struct PluginCheckSheet: View { let close: () -> Void @StateObject private var check = PluginCheck() var body: some View { VStack(alignment: .leading, spacing: 10) { HStack { Text("Plugin check").font(.headline) if check.running { ProgressView().padding(.leading, 6) } Spacer() Button("Run again") { Task { await check.run() } }.disabled(check.running) Button("Done", action: close).font(.body.weight(.semibold)).padding(.leading, 12) } ScrollView { Text(check.lines.joined(separator: "\n")) .font(.system(.footnote, design: .monospaced)) .frame(maxWidth: .infinity, alignment: .leading) .textSelection(.enabled) } } .padding() .preferredColorScheme(.dark) .task { await check.run() } } } /// The system's audio route picker (AirPlay, Bluetooth, an interface) and the /// current route's names (Android's audio device rows; iOS chooses the route). struct AudioRouteView: View { @State private var output = AudioRouteView.names(\.outputs) @State private var input = AudioRouteView.names(\.inputs) var body: some View { VStack(alignment: .leading, spacing: 8) { HStack(spacing: 10) { VStack(alignment: .leading, spacing: 3) { Text("Output: " + output).font(.system(size: 14)).foregroundColor(Theme.text) Text("Input: " + input).font(.system(size: 14)).foregroundColor(Theme.text) } Spacer() RoutePicker().frame(width: 44, height: 44) } } .onReceive(NotificationCenter.default.publisher(for: AVAudioSession.routeChangeNotification).receive(on: RunLoop.main)) { _ in output = Self.names(\.outputs) input = Self.names(\.inputs) } } static func names(_ kp: KeyPath) -> String { let ports = AVAudioSession.sharedInstance().currentRoute[keyPath: kp].map(\.portName) return ports.isEmpty ? "—" : ports.joined(separator: ", ") } private struct RoutePicker: UIViewRepresentable { func makeUIView(context: Context) -> AVRoutePickerView { let v = AVRoutePickerView() v.tintColor = UIColor(Theme.accent) v.activeTintColor = UIColor(Theme.accent) return v } func updateUIView(_ v: AVRoutePickerView, context: Context) {} } } #else struct AudioRouteView: View { var body: some View { Text("Output: Speaker\nInput: Microphone").font(.system(size: 14)).foregroundColor(Theme.text) } } enum ShareSheet { @MainActor static func present(_ url: URL, done: @escaping () -> Void = {}) { done() } } extension View { func deviceLab(isPresented: Binding) -> some View { self } func pluginCheck(isPresented: Binding) -> some View { self } } #endif