// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown // SPDX-License-Identifier: GPL-3.0-or-later import SwiftUI /// The tracker's punch-in keyboard (Android's StackedKeyboard with punchIn = true): /// a CH / OCT / VEL row over two stacked octaves (the upper one on top). A key /// sounds on the keyboard's channel; its initial press also writes the note into /// the cursor cell and advances the cursor by the skip step. Sliding across keys /// glides (each key sounds, none is recorded). Out-of-scale keys are dimmed and /// silent, unless the channel plays a Sampler (every pad must stay reachable). struct PunchKeyboard: View { @ObservedObject var app: AppModel let state: TrackerState @ObservedObject var mirror: TrackerMirror @ObservedObject private var held = TrackerHeldNotes.shared private static let velStep = 8 var body: some View { let _ = mirror.revision let inKey = inKeyPitchClasses() VStack(spacing: 4) { TKHScroll { HStack(spacing: 6) { selector("CH", app.kbdChannel == 0 ? "--" : "\(app.kbdChannel)", { app.kbdChannel = max(0, app.kbdChannel - 1) }, { app.kbdChannel = min(16, app.kbdChannel + 1) }) selector("OCT", "\(app.kbdOctave)–\(app.kbdOctave + 1)", { app.kbdOctave = max(0, app.kbdOctave - 1) }, { app.kbdOctave = min(8, app.kbdOctave + 1) }) selector("VEL", String(format: "%02X", app.kbdVelocity), { app.kbdVelocity = max(0, app.kbdVelocity - Self.velStep) }, { app.kbdVelocity = min(127, app.kbdVelocity + Self.velStep) }) } } .fixedSize(horizontal: false, vertical: true) octave(start: (app.kbdOctave + 2) * 12, inKey: inKey) octave(start: (app.kbdOctave + 1) * 12, inKey: inKey) } .padding(6) .background(TK.background) .onDisappear { held.releaseAll() } } @ViewBuilder private func selector(_ label: String, _ value: String, _ dec: @escaping () -> Void, _ inc: @escaping () -> Void) -> some View { Text(label).font(Theme.mono(11)).foregroundColor(TK.dim) TKRepeatButton(label: "-", onStep: dec) Text(value).font(Theme.mono(13)).foregroundColor(TK.accent).frame(width: 40) TKRepeatButton(label: "+", onStep: inc) } private func inKeyPitchClasses() -> Set { let s = SizzleSession.shared for ch in 0..<4 where s.midiChannel(ofChannel: ch) == app.kbdChannel { let slot = s.instrumentSlot(ofChannel: ch) if slot >= 0 && s.type(ofSlot: slot) == "SAMPLER" { return Set(0..<12) } } return Set(s.scalePitchClasses.map { $0.intValue }) } private func octave(start: Int, inKey: Set) -> some View { let app = self.app, state = self.state, held = self.held func midi(_ pc: Int) -> Int { min(max(start + pc, 0), 127) } return TrackerPianoOctave( label: { pc in TK.noteName(midi(pc)).replacingOccurrences(of: "-", with: "") }, enabled: { inKey.contains($0) }, pressed: { held.isHeld(midi($0)) }, onNoteOn: { pc, slid in let n = midi(pc) if !slid { state.punchNote(app, n) } held.on(n, app: app) }, onNoteOff: { pc in held.off(midi(pc), app: app) }) } } /// One octave as a piano (Android's PianoOctave): seven white keys with the five /// black keys over them. Each finger is tracked by the key it went down on and /// hit-tested against the whole octave as it moves, so a slide retriggers on each /// key it crosses and several fingers play chords. struct TrackerPianoOctave: View { let label: (Int) -> String let enabled: (Int) -> Bool let pressed: (Int) -> Bool let onNoteOn: (Int, Bool) -> Void let onNoteOff: (Int) -> Void private static let whites = [0, 2, 4, 5, 7, 9, 11] private static let blacks: [(pc: Int, after: Int)] = [(1, 0), (3, 1), (6, 3), (8, 4), (10, 5)] private static let blackW: CGFloat = 0.62, blackH: CGFloat = 0.62 var body: some View { GeometryReader { geo in let size = geo.size let whiteW = size.width / 7 ZStack(alignment: .topLeading) { HStack(spacing: 0) { ForEach(Self.whites, id: \.self) { pc in key(pc, black: false, size: size).frame(width: whiteW, height: size.height) } } ForEach(Self.blacks, id: \.pc) { b in key(b.pc, black: true, size: size) .frame(width: whiteW * Self.blackW, height: size.height * Self.blackH) .padding(.leading, whiteW * CGFloat(b.after + 1) - whiteW * Self.blackW / 2) } } .coordinateSpace(name: "tk-octave") } } private func key(_ pc: Int, black: Bool, size: CGSize) -> some View { PianoKeyView(label: label(pc), black: black, enabled: enabled(pc), lit: pressed(pc), hit: { p in hit(p, size: size) }, onNoteOn: onNoteOn, onNoteOff: onNoteOff) } /// Pitch class under a point in octave coordinates, or -1 (off the keyboard or /// on a disabled key). Black keys win in their upper region. private func hit(_ p: CGPoint, size: CGSize) -> Int { let w = size.width, h = size.height guard w > 0, h > 0, p.x >= 0, p.x < w, p.y >= 0, p.y < h else { return -1 } let whiteW = w / 7 var pc = Self.whites[min(max(Int(p.x / whiteW), 0), 6)] if p.y <= h * Self.blackH { let bw = whiteW * Self.blackW for b in Self.blacks { let bx = whiteW * CGFloat(b.after + 1) - bw / 2 if p.x >= bx && p.x <= bx + bw { pc = b.pc; break } } } return enabled(pc) ? pc : -1 } } /// A key face (Android's PianoKey) that owns the gesture of the finger that went /// down on it; [hit] maps the finger's position in the octave to a pitch class. private struct PianoKeyView: View { let label: String let black: Bool let enabled: Bool let lit: Bool let hit: (CGPoint) -> Int let onNoteOn: (Int, Bool) -> Void let onNoteOff: (Int) -> Void @State private var finger = Finger() final class Finger { var current: Int? } private static let white = Color(white: 0xE8 / 255), whiteOff = Color(white: 0x5A / 255) private static let blackOn = Color(red: 0x18 / 255, green: 0x18 / 255, blue: 0x20 / 255) private static let blackOff = Color(red: 0x0C / 255, green: 0x0C / 255, blue: 0x10 / 255) var body: some View { face .contentShape(Rectangle()) .gesture(DragGesture(minimumDistance: 0, coordinateSpace: .named("tk-octave")) .onChanged { g in let pc = hit(g.location) if let prev = finger.current { if pc != prev { if prev >= 0 { onNoteOff(prev) } if pc >= 0 { onNoteOn(pc, true) } finger.current = pc } } else { finger.current = pc if pc >= 0 { onNoteOn(pc, false) } } } .onEnded { _ in if let prev = finger.current, prev >= 0 { onNoteOff(prev) } finger.current = nil }) } private var face: some View { let bg: Color = lit ? TK.accent : black ? (enabled ? Self.blackOn : Self.blackOff) : (enabled ? Self.white : Self.whiteOff) let fg: Color = lit ? TK.background : black ? (enabled ? Color(white: 0xDD / 255) : Color(white: 0x55 / 255)) : (enabled ? Color(white: 0x14 / 255) : Color(white: 0x6A / 255)) return ZStack(alignment: .bottom) { bg Text(label).font(Theme.mono(12)).foregroundColor(fg).lineLimit(1).fixedSize().padding(.bottom, 3) } .overlay(Rectangle().stroke(lit ? TK.accent : Color.black, lineWidth: 1)) } }