// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown // SPDX-License-Identifier: GPL-3.0-or-later import SwiftUI /// One device kind's bindings (Android's binding sections and BindingRow): each /// action with its current control, LEARN (then press the key / combo or move the /// MIDI control) and ✕ to unbind. struct BindingsSheet: View { let source: InputSource @ObservedObject private var bindings = InputBindings.shared @ObservedObject private var input = InputController.shared @Environment(\.dismiss) private var dismiss private struct Group { let title: String; let actions: [(id: String, label: String)] } private var groups: [Group] { var g = [ Group(title: "Transport", actions: [("playPause", "Play / Pause"), ("stop", "Stop"), ("loop", "Toggle Loop")]), Group(title: "Navigation", actions: [("up", "Cursor Up"), ("down", "Cursor Down"), ("left", "Cursor Left"), ("right", "Cursor Right")]), Group(title: "Editing", actions: [("increment", "Value +"), ("decrement", "Value -"), ("clear", "Clear Cell")] + (source == .keyboard ? [("noteOff", "Note Off")] : [])), ] if source == .keyboard { g.append(Group(title: "Columns", actions: [("nextColumn", "Next Column"), ("prevColumn", "Prev Column")])) g.append(Group(title: "Octave", actions: [("octaveDown", "Octave Down"), ("octaveUp", "Octave Up")])) } g.append(Group(title: "Tabs", actions: [("nextTab", "Next Tab"), ("prevTab", "Prev Tab")])) return g } private var title: String { switch source { case .gamepad: return "Gamepad Bindings" case .keyboard: return "Keyboard Hotkeys" default: return "MIDI Bindings" } } private var hint: String { switch source { case .gamepad: return "LEARN, then press a button, D-pad direction or stick direction — or hold one and press another for a combo." case .keyboard: return "LEARN, then press a key. Unbound letter keys play the tracker piano (Z…M, Q…U)." default: return "LEARN, then press a pad or move a control on your MIDI controller (CC)." } } private func label(_ id: String) -> String? { switch source { case .gamepad: return bindings.chordFor(id).map(InputBindings.chordLabel) case .keyboard: return bindings.keyFor(id).map(InputBindings.keyLabel) default: return bindings.ccFor(id).map { "CC \($0)" } } } var body: some View { VStack(alignment: .leading, spacing: 10) { HStack { Text(title).font(.system(size: 19, weight: .semibold)).foregroundColor(Theme.accent) Spacer() SetupButton(title: "Defaults") { bindings.resetDefaults(source) } SetupButton(title: "Done", kind: .filled) { dismiss() } } Text(hint).font(.system(size: 12)).foregroundColor(Theme.dim) ScrollView { VStack(alignment: .leading, spacing: 12) { ForEach(groups, id: \.title) { group in VStack(alignment: .leading, spacing: 4) { Text(group.title.uppercased()).font(.system(size: 11, weight: .semibold)).foregroundColor(Theme.dim) ForEach(group.actions, id: \.id) { a in row(a.id, a.label) } } } } } } .padding(20) .background(Theme.surface.ignoresSafeArea()) .preferredColorScheme(.dark) .onDisappear { input.cancelLearn() } } private func row(_ id: String, _ text: String) -> some View { let listening = input.learnTarget == id let bound = label(id) return HStack(spacing: 10) { VStack(alignment: .leading, spacing: 2) { Text(text).font(.system(size: 15)).foregroundColor(Theme.text) Text(bound ?? "unbound").font(.system(size: 12, design: .monospaced)).foregroundColor(bound == nil ? Theme.dim : Theme.accent) } Spacer() SetupButton(title: listening ? "Listening…" : "Learn", kind: listening ? .filled : .outlined) { if listening { input.cancelLearn() } else { input.beginLearn(id, source: source) } } if bound != nil { Button { bindings.clear(id, source: source) } label: { Image(systemName: "xmark").foregroundColor(Theme.dim).frame(width: 36, height: 36) } .buttonStyle(.plain) } } .padding(.vertical, 2) } }