// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown // SPDX-License-Identifier: GPL-3.0-or-later import SwiftUI /// The Android "Amber CRT" palette slots the shared Theme doesn't have (beat/bar /// row tints, the playhead green, the danger red), plus Theme's own for brevity. enum TK { static let background = Theme.background static let surface = Theme.surface static let grid = Theme.grid static let text = Theme.text static let dim = Theme.dim static let accent = Theme.accent static let beat = Color(red: 0x14 / 255, green: 0x1C / 255, blue: 0x1A / 255) static let bar = Color(red: 0x1E / 255, green: 0x2A / 255, blue: 0x20 / 255) 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" (MIDI 12 == C0). static let noteNames: [String] = (0..<128).map { m in let names = ["C-", "C#", "D-", "D#", "E-", "F-", "F#", "G-", "G#", "A-", "A#", "B-"] return names[m % 12] + String(m / 12 - 1) } static func noteName(_ midi: Int) -> String { (0..<128).contains(midi) ? noteNames[midi] : "..." } static let hex2: [String] = (0..<256).map { String(format: "%02X", $0) } static let chan2: [String] = (0..<100).map { String(format: "%02d", $0) } } /// Android's RetroButton: flat, square, 1 pt border, monospace label. [active] /// paints it in the accent colour, [danger] in red; [width] pins the width. struct TKButton: View { let label: String var active = false var width: CGFloat? = nil var danger = false let action: () -> Void var body: some View { TKButtonFace(label: label, active: active, width: width, danger: danger) .contentShape(Rectangle()) .onTapGesture(perform: action) } } struct TKButtonFace: View { let label: String var active = false var width: CGFloat? = nil var danger = false var body: some View { let hue: Color? = danger ? TK.danger : (active ? TK.accent : nil) Text(label) .font(Theme.mono(12)).lineLimit(1).truncationMode(.tail) .foregroundColor(hue ?? TK.text) .padding(.horizontal, 10).padding(.vertical, 6) .frame(width: width) .background(hue.map { $0.opacity(0.18) } ?? TK.surface) .overlay(Rectangle().stroke(hue ?? TK.grid, lineWidth: 1)) .fixedSize(horizontal: width == nil, vertical: true) } } /// Android's RepeatButton: steps once on press, then repeats while held with the /// interval shrinking (320 ms × 0.8 per step, down to 24 ms). Lifting the finger /// or sliding off (more than a few points) stops it. struct TKRepeatButton: View { let label: String var active = false var width: CGFloat? = nil let onStep: () -> Void @State private var repeater = Repeater() var body: some View { TKButtonFace(label: label, active: active, width: width) .contentShape(Rectangle()) .gesture(DragGesture(minimumDistance: 0) .onChanged { g in if !repeater.pressed { repeater.start(onStep) } else if abs(g.translation.width) > 12 || abs(g.translation.height) > 12 { repeater.stop() } } .onEnded { _ in repeater.stop(); repeater.pressed = false }) .onDisappear { repeater.stop() } } final class Repeater { var pressed = false private var timer: Timer? private var delay: TimeInterval = 0.32 func start(_ step: @escaping () -> Void) { pressed = true step() delay = 0.32 schedule(step) } private func schedule(_ step: @escaping () -> Void) { let t = Timer(timeInterval: delay, repeats: false) { [weak self] _ in guard let self = self, self.timer != nil else { return } step() self.delay = max(0.024, self.delay * 0.8) self.schedule(step) } RunLoop.main.add(t, forMode: .common) timer = t } func stop() { timer?.invalidate(); timer = nil } } } /// Android's RetroDropdown, `LABEL:value ▾`, sized to its longest option so it /// never resizes. Tap opens a menu of the options. struct TKDropdown: View { let label: String let options: [T] let selected: T let optionLabel: (T) -> String let onSelect: (T) -> Void init(_ label: String, options: [T], selected: T, optionLabel: @escaping (T) -> String = { "\($0)" }, onSelect: @escaping (T) -> Void) { self.label = label; self.options = options; self.selected = selected self.optionLabel = optionLabel; self.onSelect = onSelect } var body: some View { if Snapshot.active { face } else { Menu { ForEach(options, id: \.self) { o in Button { onSelect(o) } label: { if o == selected { Label(optionLabel(o), systemImage: "checkmark") } else { Text(optionLabel(o)) } } } } label: { face } } } private var face: some View { let prefix = label.isEmpty ? "" : label + ":" let longest = options.map(optionLabel).max { $0.count < $1.count } ?? "" return ZStack(alignment: .leading) { Text(prefix + longest + " ▾").hidden() Text(prefix + optionLabel(selected) + " ▾").foregroundColor(TK.text) } .font(Theme.mono(12)).lineLimit(1) .padding(.horizontal, 8).padding(.vertical, 6) .background(TK.surface) .overlay(Rectangle().stroke(TK.grid, lineWidth: 1)) .fixedSize() } } /// A horizontally scrolling row (a plain row in snapshot mode: ImageRenderer /// can't draw scroll views). struct TKHScroll: View { @ViewBuilder let content: () -> Content var body: some View { if Snapshot.active { content().fixedSize(horizontal: true, vertical: false) .frame(minWidth: 0, maxWidth: .infinity, alignment: .leading).clipped() } else { ScrollView(.horizontal, showsIndicators: false) { content() } } } } /// Android's Row(horizontalArrangement = SpaceBetween): items spread edge to edge. struct TKJustifiedRow: View { let items: [AnyView] var body: some View { HStack(spacing: 0) { ForEach(items.indices, id: \.self) { i in if i > 0 { Spacer(minLength: 4) } items[i] } } .frame(maxWidth: .infinity) } }