// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown // SPDX-License-Identifier: GPL-3.0-or-later import SwiftUI #if canImport(UIKit) import UIKit #endif /// A device's editor, like the Android app's: tabs of titled cards, each tinted by /// its role, with knobs for numbers, switches for short choices, menus for long /// ones, faders for the EQ and live drawings. Used by the plugin extension and by /// the app for the devices it hosts. struct DeviceEditor: View { @StateObject var model: DeviceModel @State var page = 0 /// Render without a ScrollView (the Mac snapshot tool; ImageRenderer can't draw one). static var snapshot = false var body: some View { let pages = Layouts.pages(for: model.au.toolboxType, allKeys: model.order) VStack(alignment: .leading, spacing: 8) { HStack { Text(model.au.displayName.uppercased()).font(Theme.mono(15)).foregroundColor(Theme.accent) Spacer() if let presets = model.au.factoryPresets, !presets.isEmpty { Menu { ForEach(presets, id: \.number) { p in Button(p.name) { model.applyPreset(p) } } } label: { Text("PRESET: \(model.presetName ?? "—") ▾").font(Theme.mono(12)).foregroundColor(Theme.accent) } } } if pages.count > 1 { HStack(spacing: 4) { ForEach(pages.indices, id: \.self) { i in let on = i == page let tone = Theme.tone(pages[i].panels.first?.tone ?? .SOURCE) Text(pages[i].title.uppercased()).font(Theme.mono(pages.count > 5 ? 10 : 12)) .lineLimit(1).minimumScaleFactor(0.7) .foregroundColor(on ? tone : Theme.dim) .frame(maxWidth: .infinity, minHeight: 30) .background(on ? tone.opacity(0.22) : Theme.surface) .overlay(Rectangle().stroke(on ? tone : Theme.grid, lineWidth: 1)) .contentShape(Rectangle()) .onTapGesture { page = i } } } } let cards = VStack(spacing: 8) { ForEach(pages[Swift.min(page, pages.count - 1)].panels) { panel in CardView(panel: panel, model: model) } } if DeviceEditor.snapshot { cards; Spacer(minLength: 0) } else { ScrollView { cards } } } .padding(10) .background(Theme.background) } } /// One card: header, optional drawing, then choices, switches and knobs (or faders). struct CardView: View { let panel: LayoutPanel @ObservedObject var model: DeviceModel var body: some View { let tone = Theme.tone(panel.tone) let infos = panel.keys.compactMap { model.params[$0] } let enums = infos.filter { $0.isEnum } let segmented = enums.filter { $0.choices.count <= 6 && $0.choices.joined().count <= 34 } let menus = enums.filter { !($0.choices.count <= 6 && $0.choices.joined().count <= 34) } let toggles = infos.filter { $0.isToggle } let knobs = infos.filter { !$0.isEnum && !$0.isToggle } VStack(alignment: .leading, spacing: 7) { HStack(spacing: 6) { Rectangle().fill(tone).frame(width: 8, height: 8) Text(panel.title.uppercased()).font(Theme.mono(11)).foregroundColor(tone) } if let viz = panel.viz, viz.kind != "wavetable" { VizView(viz: viz, tone: tone, model: model) } if let captions = panel.faders { FaderBank(infos: infos, captions: captions, scale: panel.faderScale ?? 1, tone: tone, model: model) } else { ForEach(Array(stride(from: 0, to: menus.count, by: 2)), id: \.self) { i in HStack(spacing: 8) { ForEach(menus[i..= 0.5, tone: tone) { model.set(p.key, model.value(p.key) >= 0.5 ? 0 : 1) } } } } knobRows(knobs, tone: tone) } } .padding(.horizontal, 8).padding(.vertical, 7) .background(Theme.surface) .overlay(Rectangle().stroke(tone.opacity(0.35), lineWidth: 1)) } /// Up to six knobs a row, rows evenly filled, centred (Android's PanelCard). @ViewBuilder private func knobRows(_ knobs: [ParamInfo], tone: Color) -> some View { let n = knobs.count if n > 0 { let perRow = n <= 6 ? n : Int(ceil(Double(n) / ceil(Double(n) / 6))) let slots = Swift.max(perRow, 4) ForEach(Array(stride(from: 0, to: n, by: perRow)), id: \.self) { start in let row = Array(knobs[start.. UIViewController { let vc = UIHostingController(rootView: DeviceEditor(model: DeviceModel(au: au))) vc.view.backgroundColor = UIColor(red: 0.07, green: 0.08, blue: 0.09, alpha: 1) return vc } } #endif