Files
sizzletracker-android/ios/app/FX/Editor/DeviceEditor.swift
T
ReactorcoremeltdownandClaude Opus 5.5 29650b88d7 iOS milestone 3 groundwork: SizzleSession bridge, app shell with tabs, slot-backed editors, screen snapshots
SizzleSession owns the song, engine (192-frame chunks, try-lock on the audio
thread), samples and audio output; AppModel/RootView give the four tabs;
DeviceModel now works on toolbox slots too; make screen-snapshots renders the
app's tabs on the Mac.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 09:15:28 +01:00

147 lines
6.8 KiB
Swift

// 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
var body: some View {
let pages = Layouts.pages(for: model.toolboxType, allKeys: model.order)
VStack(alignment: .leading, spacing: 8) {
HStack {
Text(model.displayName.uppercased()).font(Theme.mono(15)).foregroundColor(Theme.accent)
Spacer()
if !model.presetNames.isEmpty {
Menu {
ForEach(model.presetNames, id: \.self) { name in Button(name) { model.applyPreset(named: name) } }
} 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)
}
}
SnapScroll { 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..<Swift.min(i + 2, menus.count)], id: \.key) { p in
ChoiceMenu(label: Format.shortLabel(p, labels: panel.labels), choices: p.choices,
selected: Int(model.value(p.key).rounded()), tone: tone) { model.set(p.key, Float($0)) }
}
}
}
ForEach(segmented, id: \.key) { p in
SegmentedView(label: Format.shortLabel(p, labels: panel.labels), choices: p.choices,
selected: Int(model.value(p.key).rounded()), tone: tone) { model.set(p.key, Float($0)) }
}
if !toggles.isEmpty {
HStack(spacing: 6) {
ForEach(toggles, id: \.key) { p in
ToggleChip(label: Format.shortLabel(p, labels: panel.labels), on: model.value(p.key) >= 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..<Swift.min(start + perRow, n)])
HStack(spacing: 4) {
let pad = slots - row.count
ForEach(0..<(pad / 2), id: \.self) { _ in Color.clear.frame(maxWidth: .infinity, maxHeight: 1) }
ForEach(row, id: \.key) { p in
KnobView(label: Format.shortLabel(p, labels: panel.labels),
valueText: Format.value(p, model.value(p.key)),
value: model.value(p.key), min: p.min, max: p.max, def: p.def, tone: tone) { model.set(p.key, $0) }
}
ForEach(0..<(pad - pad / 2), id: \.self) { _ in Color.clear.frame(maxWidth: .infinity, maxHeight: 1) }
}
}
}
}
}
#if canImport(UIKit)
/// For the Objective-C view controller: the editor for [au] in a hosting controller.
@objc(SizzleEditorFactory)
@MainActor
public final class SizzleEditorFactory: NSObject {
@objc public static func makeEditor(for au: SizzleDeviceAU) -> 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