// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown // SPDX-License-Identifier: GPL-3.0-or-later import SwiftUI /// Two octaves of piano keys from C of [octave] (MIDI 60 = C4). Each key tracks its /// own touch, so chords work; a key sends note-on on touch and note-off on release. struct KeyboardView: View { let octave: Int let noteOn: (Int) -> Void let noteOff: (Int) -> Void private let whiteSteps = [0, 2, 4, 5, 7, 9, 11] private let blackSteps: [(step: Int, afterWhite: Int)] = [(1, 0), (3, 1), (6, 3), (8, 4), (10, 5)] var body: some View { GeometryReader { geo in let whites = 14 let w = geo.size.width / CGFloat(whites) let base = (octave + 1) * 12 ZStack(alignment: .topLeading) { HStack(spacing: 1) { ForEach(0.. Void let noteOff: (Int) -> Void @State private var down = false var body: some View { Rectangle() .fill(down ? Color.orange : (black ? Color(white: 0.12) : Color(white: 0.92))) .overlay(alignment: .bottom) { if !black && note % 12 == 0 { Text("C\(note / 12 - 1)").font(.system(size: 10, design: .monospaced)) .foregroundColor(.gray).padding(.bottom, 4) } } .gesture(DragGesture(minimumDistance: 0) .onChanged { _ in if !down { down = true; noteOn(note) } } .onEnded { _ in down = false; noteOff(note) }) } } }