Files
sizzletracker-android/ios/app/Host/Screens/Tracker/TrackerWidgets.swift
T
ReactorcoremeltdownandClaude Opus 5.5 64e21e0bcc iOS 0.6.0: milestone 5 (recording, MIDI, input bindings, themes, bundles, backups)
- Master recording (MasterSink ring + writer thread) and the sample recorder
- CoreMIDI in/out: sound module, clock send/follow, virtual "Sizzle" ports;
  platform-free parser/module/clock in core (midi_test)
- Hardware keyboard and gamepads via GameController; rebindable keyboard,
  gamepad and MIDI CC actions; per-parameter MIDI learn
- Colour themes (.szt), project bundles (.szgz), backup & restore;
  ZIP in core with zlib (zip_test against jar and unzip)
- Persistent app settings; audio pauses in the background unless sound module

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

187 lines
6.6 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// 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 var background: Color { Theme.background }
static var surface: Color { Theme.surface }
static var grid: Color { Theme.grid }
static var text: Color { Theme.text }
static var dim: Color { Theme.dim }
static var accent: Color { Theme.accent }
static var beat: Color { Theme.beat }
static var bar: Color { Theme.bar }
static var playhead: Color { Theme.playhead }
static var danger: Color { Theme.danger }
/// 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<T: Hashable>: 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<Content: View>: 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)
}
}