// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown // SPDX-License-Identifier: GPL-3.0-or-later import SwiftUI /// The third tab (Android's ToolboxScreen): 16 slot tiles holding instruments or /// effects over (portrait) or beside (landscape) an audition keyboard. /// - tap: pick a device for an empty tile, or open a filled tile's settings; /// - long press: clear a filled tile, after confirming. /// The slot editor (AppModel.editingSlot) opens over the whole tab. struct ToolboxScreen: View { @EnvironmentObject var app: AppModel var body: some View { ToolboxBody(app: app, editing: app.editingSlot ?? Self.snapshotSlot, channel: app.kbdChannel, octave: app.kbdOctave, velocity: app.kbdVelocity) } /// The Mac snapshot tool can open a slot's editor: SIZZLE_SNAP_EDIT_SLOT=n. private static let snapshotSlot: Int? = Snapshot.active ? ProcessInfo.processInfo.environment["SIZZLE_SNAP_EDIT_SLOT"].flatMap { Int($0) } : nil } private struct ToolboxBody: View { let app: AppModel let editing: Int? let channel: Int let octave: Int let velocity: Int @ObservedObject private var watch = MixSongWatch.shared @ObservedObject private var nav = ToolboxNav.shared @State private var picking: Int? @State private var clearing: Int? private var session: SizzleSession { app.session } var body: some View { GeometryReader { geo in let landscape = geo.size.width > geo.size.height ZStack { if landscape { // A fixed 4x4 grid squashed to the height, beside the keyboard (1 : 1.3). HStack(spacing: 0) { tileGrid(side: nil).frame(width: (geo.size.width - 2) / 2.3) Theme.accent.frame(width: 2) ToolboxKeyboard(app: app, channel: channel, octave: octave, velocity: velocity) } } else { // A scrolling grid of square tiles above the keyboard (1.7 : 1). VStack(spacing: 0) { SnapScroll { tileGrid(side: (geo.size.width - 12 - 18) / 4) } .frame(height: (geo.size.height - 2) * 1.7 / 2.7, alignment: .top) .clipped() Theme.accent.frame(height: 2) ToolboxKeyboard(app: app, channel: channel, octave: octave, velocity: velocity) } } if let index = picking { DevicePicker(onDismiss: { picking = nil }, onPick: { type in session.fillSlot(index, withType: type) picking = nil app.editingSlot = index }) } if let index = editing, !session.type(ofSlot: index).isEmpty { let type = session.type(ofSlot: index) SlotEditor(slot: index, type: type, landscape: landscape, onClose: { nav.close(app: app) }) .id("\(index)|\(type)|\(nav.editorEpoch)") } } } .background(Theme.background) .alert("Clear tile", isPresented: Binding(get: { clearing != nil }, set: { if !$0 { clearing = nil } }), actions: { Button("Clear", role: .destructive) { if let i = clearing { session.clearSlot(i) } clearing = nil } Button("Cancel", role: .cancel) { clearing = nil } }, message: { if let i = clearing { Text("Remove \"\(tileLabel(i))\" from tile \(i + 1)? This clears the device and its settings and can't be undone.") } }) } private func tileLabel(_ i: Int) -> String { let name = session.name(ofSlot: i) if !name.trimmingCharacters(in: .whitespaces).isEmpty { return name } let t = session.type(ofSlot: i) return SizzleSession.deviceTypes.first { $0.type == t }?.displayName ?? "" } /// Four rows of four tiles: square in portrait, filling the height in landscape. /// [side]: square tiles of that size (portrait); nil fills the height (landscape). private func tileGrid(side: CGFloat?) -> some View { VStack(spacing: 6) { ForEach(0..<4, id: \.self) { row in HStack(spacing: 6) { ForEach(0..<4, id: \.self) { col in let i = row * 4 + col SlotTile(index: i, type: session.type(ofSlot: i), name: session.name(ofSlot: i)) .frame(width: side, height: side).frame(maxWidth: .infinity, maxHeight: side == nil ? .infinity : nil) .onTapGesture { nav.returnTab = nil if session.type(ofSlot: i).isEmpty { picking = i } else { app.editingSlot = i } } .onLongPressGesture { if !session.type(ofSlot: i).isEmpty { clearing = i } } } } .frame(maxHeight: side == nil ? .infinity : nil) } } .padding(6) } } /// One slot: its number, its name (or +) and INS / EFF. private struct SlotTile: View { let index: Int let type: String let name: String var body: some View { let empty = type.isEmpty let isInstrument = SizzleSession.deviceTypes.first { $0.type == type }?.isInstrument ?? false ZStack { (empty ? Theme.surface : Theme.accent.opacity(0.12)) VStack(spacing: 1) { Text(String(format: "%02d", index + 1)).font(Theme.mono(10)).foregroundColor(Theme.dim) Text(empty ? "+" : name).font(Theme.mono(11)).foregroundColor(empty ? Theme.dim : Theme.text) .lineLimit(1).truncationMode(.tail).multilineTextAlignment(.center) if !empty { Text(isInstrument ? "INS" : "EFF").font(Theme.mono(9)).foregroundColor(Theme.dim) } } .padding(.horizontal, 5).padding(.vertical, 3) } .overlay(Rectangle().stroke(empty ? Theme.grid : Theme.accent, lineWidth: 1)) .contentShape(Rectangle()) } } /// The device list: instruments, MIDI effects, audio effects. Tap outside to dismiss. private struct DevicePicker: View { let onDismiss: () -> Void let onPick: (String) -> Void var body: some View { let all = SizzleSession.deviceTypes let groups: [(String, [SizzleDeviceType])] = [ ("INSTRUMENTS", all.filter { $0.isInstrument }), ("MIDI EFFECTS", all.filter { !$0.isInstrument && $0.isMidiEffect }), ("AUDIO EFFECTS", all.filter { !$0.isInstrument && !$0.isMidiEffect }), ] GeometryReader { geo in SnapScroll { VStack(alignment: .leading, spacing: 0) { Text("SELECT DEVICE").font(Theme.mono(16)).foregroundColor(Theme.accent) ForEach(groups, id: \.0) { header, types in Text(header).font(Theme.mono(11)).foregroundColor(Theme.dim).padding(.top, 16).padding(.bottom, 2) ForEach(types, id: \.type) { t in Text(t.displayName).font(Theme.mono(18)).foregroundColor(Theme.text) .padding(.vertical, 8) .frame(maxWidth: .infinity, alignment: .leading) .contentShape(Rectangle()) .onTapGesture { onPick(t.type) } } } } .fixedSize(horizontal: true, vertical: false) .padding(16) .frame(maxWidth: .infinity, minHeight: geo.size.height, alignment: .top) .contentShape(Rectangle()) .onTapGesture(perform: onDismiss) // anywhere but a device } } .background(Theme.background.opacity(0.92)) } }