// 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 { /// The theme's playhead and danger colours. static var playhead: Color { Theme.playhead } static var danger: Color { Theme.danger } /// 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: 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.. 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: 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 } } } }