iOS 0.4.0: Sizzle's Tracker, Mix, Toolbox and Setup tabs on the C++ engine

Ported from the Android tabs by parallel agents (each screen with its own
SizzleSession category); slot editors use the card editor with a preset
library; songs load/save in the Android project format.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Reactorcoremeltdown
2026-11-16 09:32:30 +01:00
co-authored by Claude Opus 5.5
parent 29650b88d7
commit 962cfd1e2b
35 changed files with 4088 additions and 66 deletions
+208
View File
@@ -0,0 +1,208 @@
// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown
// SPDX-License-Identifier: GPL-3.0-or-later
import SwiftUI
/// The Mix and Toolbox screens' building blocks, ported from Android's
/// components/Widgets.kt (RetroButton, RepeatButton, RetroDropdown, SectionLabel).
/// Namespaced so they can't clash with the other screens' own.
enum MixKit {
/// Android palette colours the shared Theme doesn't carry.
static let playhead = Color(red: 0x7C / 255, green: 0xFC / 255, blue: 0x9A / 255)
static let danger = Color(red: 0xE0 / 255, green: 0x5A / 255, blue: 0x4B / 255)
/// Pitch.name: 60 -> "C-4", 61 -> "C#4".
static func pitchName(_ midi: Int) -> String {
guard (0...127).contains(midi) else { return "..." }
let names = ["C-", "C#", "D-", "D#", "E-", "F-", "F#", "G-", "G#", "A-", "A#", "B-"]
return names[midi % 12] + String(midi / 12 - 1)
}
/// RetroButtonSurface: square corners, 1 pt border, accent when active, red for danger.
struct Face: View {
let label: String
var active = false
var danger = false
var width: CGFloat? = nil
var fill = false
var body: some View {
let hue: Color? = danger ? MixKit.danger : (active ? Theme.accent : nil)
Text(label).font(Theme.mono(12)).lineLimit(1).truncationMode(.tail)
.foregroundColor(hue ?? Theme.text)
.padding(.horizontal, 10).padding(.vertical, 6)
.frame(width: width)
.frame(maxWidth: fill ? .infinity : nil)
.background(hue?.opacity(0.18) ?? Theme.surface)
.overlay(Rectangle().stroke(hue ?? Theme.grid, lineWidth: 1))
.contentShape(Rectangle())
}
}
/// RetroButton: tap to act.
struct Button: View {
let label: String
var active = false
var danger = false
var width: CGFloat? = nil
var fill = false
let action: () -> Void
var body: some View {
Face(label: label, active: active, danger: danger, width: width, fill: fill)
.onTapGesture(perform: action)
}
}
/// RepeatButton: steps once on press, then repeats while held, faster and faster
/// (320 ms, ×0.8 each time, down to 24 ms). Dragging off stops it.
struct RepeatButton: View {
let label: String
var width: CGFloat? = nil
let onStep: () -> Void
@StateObject private var repeater = Repeater()
var body: some View {
Face(label: label, width: width)
.gesture(DragGesture(minimumDistance: 0)
.onChanged { g in
if abs(g.translation.width) > 24 || abs(g.translation.height) > 24 { repeater.stop() }
else if !repeater.pressed { repeater.start(onStep) }
}
.onEnded { _ in repeater.stop(); repeater.pressed = false })
}
}
@MainActor final class Repeater: ObservableObject {
var pressed = false
private var timer: Timer?
private var delay = 0.32
private var step: (() -> Void)?
func start(_ step: @escaping () -> Void) {
pressed = true
self.step = step
delay = 0.32
step()
schedule()
}
private func schedule() {
timer = Timer.scheduledTimer(withTimeInterval: delay, repeats: false) { [weak self] _ in
Task { @MainActor in self?.fire() }
}
}
private func fire() {
guard timer != nil, let step = step else { return }
step()
delay = max(0.024, delay * 0.8)
schedule()
}
func stop() {
timer?.invalidate()
timer = nil
step = nil
}
}
/// SectionLabel: a dim upper-case header.
struct SectionLabel: View {
let text: String
var body: some View {
Text(text.uppercased()).font(Theme.mono(11)).foregroundColor(Theme.dim).padding(.vertical, 4)
}
}
/// RetroDropdown: `LABEL:value ▾`, as wide as its longest option so it never
/// jumps. Tap opens the list (in balanced columns past 12 rows, like Android's);
/// an optional long press does something else (the mixer opens the device).
struct Dropdown<T: Hashable>: View {
let label: String
let options: [T]
let selected: T
var fill = false
let optionLabel: (T) -> String
var onLongPress: (() -> Void)? = nil
let onSelect: (T) -> Void
@State private var open = false
var body: some View {
let prefix = label.isEmpty ? "" : label + ":"
let longest = options.map(optionLabel).max { $0.count < $1.count } ?? ""
ZStack(alignment: .leading) {
Text(prefix + longest + " ▾").foregroundColor(.clear)
Text(prefix + optionLabel(selected) + " ▾").foregroundColor(Theme.text).truncationMode(.tail)
}
.font(Theme.mono(12)).lineLimit(1)
.padding(.horizontal, 8).padding(.vertical, 6)
.frame(maxWidth: fill ? .infinity : nil, alignment: .leading)
.background(Theme.surface)
.overlay(Rectangle().stroke(Theme.grid, lineWidth: 1))
.contentShape(Rectangle())
.onTapGesture { open = true }
.onLongPressGesture { onLongPress?() }
.popover(isPresented: $open) { menu }
}
private static var maxRows: Int { 12 }
@ViewBuilder private var menu: some View {
let n = options.count
let cols = n <= Self.maxRows ? 1 : (n + Self.maxRows - 1) / Self.maxRows
if cols <= 4 {
let perCol = (n + cols - 1) / Swift.max(cols, 1)
HStack(alignment: .top, spacing: 0) {
ForEach(0..<cols, id: \.self) { c in
VStack(alignment: .leading, spacing: 0) {
ForEach(Array(options[Swift.min(n, c * perCol)..<Swift.min(n, (c + 1) * perCol)]), id: \.self) { opt in row(opt) }
}
}
}
.padding(4)
.background(Theme.surface.ignoresSafeArea())
} else {
// Very long lists (the LFO's targets) scroll in one column instead.
SnapScroll {
VStack(alignment: .leading, spacing: 0) { ForEach(options, id: \.self) { row($0) } }.padding(4)
}
.frame(minWidth: 320, minHeight: 420)
.background(Theme.surface.ignoresSafeArea())
}
}
private func row(_ opt: T) -> some View {
Text(optionLabel(opt)).font(Theme.mono(13)).lineLimit(1)
.foregroundColor(opt == selected ? Theme.accent : Theme.text)
.padding(.horizontal, 12).padding(.vertical, 8)
.frame(maxWidth: .infinity, alignment: .leading)
.contentShape(Rectangle())
.onTapGesture { onSelect(opt); open = false }
}
}
/// A horizontal ScrollView, or a plain row while snapshotting.
struct HScroll<Content: View>: View {
@ViewBuilder let content: () -> Content
var body: some View {
if Snapshot.active { content() }
else { ScrollView(.horizontal, showsIndicators: false) { content() } }
}
}
}
/// Re-renders the Mix and Toolbox screens when the song changes, without the
/// AppModel's transport polling (which would redraw them on every tracker line).
@MainActor
final class MixSongWatch: ObservableObject {
static let shared = MixSongWatch()
@Published private(set) var revision = 0
private var observer: NSObjectProtocol?
private init() {
observer = NotificationCenter.default.addObserver(forName: .SizzleSessionDidChange, object: nil, queue: .main) { [weak self] _ in
Task { @MainActor in self?.revision += 1 }
}
}
}
+185
View File
@@ -0,0 +1,185 @@
// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown
// SPDX-License-Identifier: GPL-3.0-or-later
import SwiftUI
/// The second tab (Android's MixerScreen): four channel strips, one per tracker
/// track. Each picks an instrument and up to four effects from the toolbox, sets a
/// MIDI channel, and has a volume fader with a live meter, a limiter and mute/solo.
/// Long-pressing an instrument or effect opens its settings in the Toolbox.
struct MixScreen: View {
@EnvironmentObject var app: AppModel
var body: some View { MixBody(app: app) }
}
/// Watches the song only (not the transport), so playback doesn't redraw the strips;
/// the meters redraw themselves at display rate.
private struct MixBody: View {
let app: AppModel
@ObservedObject private var watch = MixSongWatch.shared
var body: some View {
let session = app.session
let kinds = Dictionary(SizzleSession.deviceTypes.map { ($0.type, $0.isInstrument) }, uniquingKeysWith: { a, _ in a })
var instruments: [Int] = [], effects: [Int] = []
for i in 0..<16 {
let t = session.type(ofSlot: i)
guard !t.isEmpty, let inst = kinds[t] else { continue }
if inst { instruments.append(i) } else { effects.append(i) }
}
let names = (0..<16).map { session.name(ofSlot: $0) }
return GeometryReader { geo in
let landscape = geo.size.width > geo.size.height
HStack(spacing: 6) {
ForEach(0..<4, id: \.self) { ch in
ChannelStrip(ch: ch, landscape: landscape, instruments: instruments, effects: effects, names: names,
open: { ToolboxNav.open($0, app: app, returnTab: .mix) })
.frame(maxWidth: .infinity, maxHeight: .infinity)
}
}
.padding(6)
}
.background(Theme.background)
}
}
private struct ChannelStrip: View {
let ch: Int
let landscape: Bool
let instruments: [Int]
let effects: [Int]
let names: [String]
let open: (Int) -> Void
private var session: SizzleSession { SizzleSession.shared }
var body: some View {
VStack(alignment: .leading, spacing: 4) {
Text("CH \(ch + 1)").font(Theme.mono(13)).foregroundColor(Theme.accent)
if landscape {
// Routing left (scrolls if short), levels right: the fader and
// LIM / M / S stay visible in the short height.
HStack(spacing: 6) {
SnapScroll { VStack(alignment: .leading, spacing: 4) { routing } }
.frame(maxWidth: .infinity, maxHeight: .infinity)
VStack(alignment: .leading, spacing: 4) { levels }
.frame(maxWidth: .infinity, maxHeight: .infinity)
}
} else {
routing
levels
}
}
.padding(6)
.background(Theme.surface)
}
private func label(_ i: Int) -> String { i < 0 ? "—" : "\(i + 1):\(names[i])" }
@ViewBuilder private var routing: some View {
let inst = session.instrumentSlot(ofChannel: ch)
MixKit.SectionLabel(text: "Instrument")
MixKit.Dropdown(label: "", options: [-1] + instruments, selected: inst, fill: true, optionLabel: label,
onLongPress: { if inst >= 0 { open(inst) } },
onSelect: { session.setInstrumentSlot($0, ofChannel: ch) })
MixKit.SectionLabel(text: "FX 1-4")
ForEach(0..<4, id: \.self) { i in
let fx = session.fxSlot(i, ofChannel: ch)
MixKit.Dropdown(label: "", options: [-1] + effects, selected: fx, fill: true, optionLabel: label,
onLongPress: { if fx >= 0 { open(fx) } },
onSelect: { session.setFxSlot(i, to: $0, ofChannel: ch) })
}
MixKit.SectionLabel(text: "MIDI Ch")
HStack(spacing: 0) {
MixKit.RepeatButton(label: "-") {
session.setMidiChannel(max(0, session.midiChannel(ofChannel: ch) - 1), ofChannel: ch)
}
Text("\(session.midiChannel(ofChannel: ch))").font(Theme.mono(12)).foregroundColor(Theme.text)
.lineLimit(1).frame(width: 28)
MixKit.RepeatButton(label: "+") {
session.setMidiChannel(min(16, session.midiChannel(ofChannel: ch) + 1), ofChannel: ch)
}
}
}
@ViewBuilder private var levels: some View {
MixKit.SectionLabel(text: "Volume")
VolumeFader(ch: ch, value: session.volume(ofChannel: ch)) { session.setVolume($0, ofChannel: ch) }
.frame(maxWidth: .infinity, maxHeight: .infinity)
MixKit.Button(label: "LIM", active: session.hasLimiter(ch), fill: true) {
session.setChannel(ch, limiter: !session.hasLimiter(ch))
}
HStack(spacing: 4) {
MixKit.Button(label: "M", active: session.isMuted(ch), fill: true) { session.setChannel(ch, muted: !session.isMuted(ch)) }
MixKit.Button(label: "S", active: session.isSolo(ch), fill: true) { session.setChannel(ch, solo: !session.isSolo(ch)) }
}
}
}
/// Android's VerticalFader: the set level fills from the bottom with a thumb line;
/// tap or drag to set, snapping to the default (0.8) within ±0.03. The bus meter
/// rises inside the fill, green to amber to red over the track's height, all red
/// while the bus clips. Only the meter layer redraws each frame.
private struct VolumeFader: View {
let ch: Int
let value: Float
let onChange: (Float) -> Void
private let defaultVolume: Float = 0.8
var body: some View {
GeometryReader { g in
let h = g.size.height
let vol = CGFloat(min(max(value, 0), 1))
let top = h - vol * h
ZStack(alignment: .top) {
Theme.background
Rectangle().fill(Theme.accent.opacity(0.30)).frame(height: vol * h).offset(y: top)
MeterLayer(ch: ch, volume: vol)
Rectangle().fill(Theme.accent).frame(height: 3).offset(y: min(max(top - 1.5, 0), max(h - 3, 0)))
}
.frame(width: g.size.width, height: h, alignment: .top)
.clipped()
.overlay(Rectangle().stroke(Theme.grid, lineWidth: 1))
.contentShape(Rectangle())
.gesture(DragGesture(minimumDistance: 0).onChanged { d in
guard h > 0 else { return }
let raw = min(max(Float(1 - d.location.y / h), 0), 1)
onChange(abs(raw - defaultVolume) < 0.03 ? defaultVolume : raw)
})
}
}
}
/// The live meter: a Canvas in a display-rate TimelineView, reading the engine's
/// peak and clip flag directly (no state, nothing else redraws).
private struct MeterLayer: View {
let ch: Int
let volume: CGFloat
var body: some View {
if Snapshot.active {
Color.clear
} else {
TimelineView(.animation) { _ in
let session = SizzleSession.shared
let level = CGFloat(min(max(session.meter(ofChannel: ch), 0), 1))
let clipped = session.clippedChannel(ch)
let vol = volume
Canvas { ctx, size in
let frac = min(level, vol)
guard frac > 0.001 else { return }
let h = size.height, mh = frac * h
let rect = Path(CGRect(x: 0, y: h - mh, width: size.width, height: mh))
if clipped {
ctx.fill(rect, with: .color(MixKit.danger))
} else {
let g = Gradient(stops: [
.init(color: MixKit.playhead, location: 0), .init(color: MixKit.playhead, location: 0.65),
.init(color: Theme.accent, location: 0.85), .init(color: MixKit.danger, location: 1),
])
ctx.fill(rect, with: .linearGradient(g, startPoint: CGPoint(x: 0, y: h), endPoint: .zero))
}
}
}
}
}
}