270 lines
19 KiB
Swift
270 lines
19 KiB
Swift
// SPDX-License-Identifier: GPL-3.0-or-later
|
||
import SwiftUI
|
||
|
||
struct CartridgeControlsView: View {
|
||
@ObservedObject var machine: MachineModel
|
||
@Environment(\.openWindow) private var openWindow
|
||
@State private var expandedSlots = Set<Int>()
|
||
|
||
var body: some View {
|
||
VStack(spacing: 0) {
|
||
VStack(alignment: .leading, spacing: 14) {
|
||
SecondaryHeader(title: "Cartridges", badge: "mpi")
|
||
Picker("Cartridge connection", selection: Binding(get: { machine.cartridgeAssignments.multiPak }, set: machine.setMultiPak)) {
|
||
Text("Single cartridge").tag(false)
|
||
Text("Multi-Pak").tag(true)
|
||
}.pickerStyle(.segmented).accessibilityIdentifier("cartridge-connection")
|
||
Label("Changes restart the CoCo and clear the working program. Floppy and hard disks are retained; SDC mounts return to startup.cfg.", systemImage: "arrow.counterclockwise")
|
||
.font(.callout).foregroundStyle(.secondary).fixedSize(horizontal: false, vertical: true)
|
||
}.padding(20)
|
||
Divider()
|
||
ScrollView {
|
||
VStack(alignment: .leading, spacing: 12) {
|
||
if machine.cartridgeAssignments.multiPak {
|
||
ForEach(1...4, id: \.self) { slot in row(slot) }
|
||
} else { row(0) }
|
||
if !machine.cartridgeRestoreErrors.isEmpty {
|
||
SecondaryAction(title: "Retry Unavailable Files", systemImage: "arrow.clockwise", action: machine.retryRememberedCartridges)
|
||
}
|
||
if let error = machine.diskSettingsError {
|
||
Label(error, systemImage: "exclamationmark.circle").foregroundStyle(.red).textSelection(.enabled)
|
||
if machine.diskSettingsNeedsSave {
|
||
SecondaryAction(title: "Retry Saving Settings", systemImage: "arrow.clockwise", action: machine.saveDiskAssignments)
|
||
}
|
||
}
|
||
if let error = machine.fileError ?? machine.error {
|
||
Label(error, systemImage: "exclamationmark.circle").foregroundStyle(.red).textSelection(.enabled)
|
||
}
|
||
SecondaryHelp(title: "About cartridge connections") {
|
||
Text("The single-cartridge assignment is retained while Multi-Pak is enabled. Each controller or sound device can occupy one Multi-Pak slot; choosing it elsewhere moves it. Software can route ROM and I/O ports independently of the startup slot.")
|
||
}
|
||
}.padding(20)
|
||
}
|
||
Divider()
|
||
VStack(alignment: .leading, spacing: 12) {
|
||
if machine.cartridgeAssignments.multiPak {
|
||
HStack(spacing: 16) {
|
||
Text("Startup slot").fontWeight(.medium)
|
||
Picker("Startup slot", selection: Binding(get: { machine.cartridgeAssignments.physicalSlot }, set: machine.selectPhysicalSlot)) {
|
||
ForEach(1...4, id: \.self) { Text("Slot \($0)").tag($0) }
|
||
}.pickerStyle(.segmented).labelsHidden().frame(maxWidth: 330)
|
||
.accessibilityIdentifier("cartridge-startup-slot")
|
||
Spacer(minLength: 0)
|
||
}
|
||
HStack {
|
||
Label("Live routing", systemImage: "arrow.triangle.branch")
|
||
Text("ROM: Slot \(machine.romSlot) · I/O ports: Slot \(machine.ioSlot)").monospacedDigit()
|
||
}.font(.caption).foregroundStyle(.secondary)
|
||
.accessibilityIdentifier("cartridge-live-routing")
|
||
}
|
||
MediaExecutionFooter(machine: machine, note: "After a change, press Run to continue.")
|
||
}.padding(20)
|
||
}.frame(minWidth: 680, minHeight: 440)
|
||
.disabled(machine.busy || machine.diskSaving || machine.diskBusy || machine.state == .closed)
|
||
}
|
||
|
||
private func row(_ slot: Int) -> some View {
|
||
let assignment = machine.cartridgeAssignments.slots[slot]
|
||
return SecondaryCard {
|
||
VStack(alignment: .leading, spacing: 12) {
|
||
HStack(alignment: .center, spacing: 14) {
|
||
Text(slot == 0 ? "ROM" : "\(slot)")
|
||
.font(.system(.body, design: .monospaced).weight(.semibold))
|
||
.frame(width: 38, height: 38)
|
||
.background(Color.accentColor.opacity(0.12), in: RoundedRectangle(cornerRadius: 7))
|
||
.accessibilityLabel(slot == 0 ? "Single cartridge" : "Slot \(slot)")
|
||
VStack(alignment: .leading, spacing: 4) {
|
||
Text(title(assignment.kind)).fontWeight(.semibold)
|
||
Text(summary(slot)).font(.caption).foregroundStyle(.secondary)
|
||
.lineLimit(2).truncationMode(.middle)
|
||
}.frame(maxWidth: .infinity, alignment: .leading)
|
||
deviceMenu(slot)
|
||
contextualAction(slot)
|
||
}
|
||
if let warning = readiness(slot) {
|
||
Label(warning, systemImage: "exclamationmark.circle")
|
||
.font(.callout).foregroundStyle(.orange).fixedSize(horizontal: false, vertical: true)
|
||
}
|
||
if let error = machine.cartridgeRestoreErrors[slot] {
|
||
Text(error).font(.callout).foregroundStyle(.red).textSelection(.enabled)
|
||
SecondaryAction(title: "Retry Files", systemImage: "arrow.clockwise", action: machine.retryRememberedCartridges)
|
||
.accessibilityLabel("Retry unavailable files for slot \(slot)")
|
||
}
|
||
if assignment.kind != .empty {
|
||
DisclosureGroup(isExpanded: Binding(get: { expandedSlots.contains(slot) }, set: { value in
|
||
if value { expandedSlots.insert(slot) } else { expandedSlots.remove(slot) }
|
||
})) {
|
||
details(slot).padding(.top, 10)
|
||
} label: { Label("Details", systemImage: "info.circle").font(.callout).accessibilityLabel("Details for slot \(slot)") }
|
||
}
|
||
}
|
||
}.modifier(MediaDropTarget(machine: machine, destination: .cartridge(slot)))
|
||
}
|
||
|
||
private func title(_ kind: CartridgeAssignments.Kind) -> String {
|
||
switch kind {
|
||
case .empty: return "Empty slot"
|
||
case .rom: return "ROM cartridge"
|
||
case .floppy: return "Floppy controller (FD-502)"
|
||
case .hardDisk: return "Hard-disk controller"
|
||
case .ramDisk: return "RAM disk (512 KB)"
|
||
case .orchestra90: return "Orchestra-90"
|
||
case .speechSound: return "Speech/Sound Cartridge"
|
||
case .sdc: return "CoCo SDC"
|
||
}
|
||
}
|
||
private func summary(_ slot: Int) -> String {
|
||
let assignment = machine.cartridgeAssignments.slots[slot]
|
||
switch assignment.kind {
|
||
case .empty: return "Choose a device or drop a ROM file here"
|
||
case .rom: return assignment.url?.lastPathComponent ?? "No ROM selected"
|
||
case .floppy: return "FD0–FD3 · Mounted: \(machine.disks.filter(\.mounted).count)"
|
||
case .hardDisk: return "HD0–HD1 · Mounted: \(machine.hardDisks.filter(\.mounted).count)"
|
||
case .ramDisk: return "Volatile storage for supporting software"
|
||
case .orchestra90: return assignment.url?.lastPathComponent ?? "Stereo sound only · No program ROM"
|
||
case .sdc: return assignment.sdcFolderURL.map { "SD card: " + $0.lastPathComponent } ?? "Choose an SD card folder and SDC-DOS ROM"
|
||
case .speechSound:
|
||
return assignment.url != nil && assignment.speechROMURL != nil ? "Speech and music · Two chip ROMs selected" : "Two internal chip ROMs required"
|
||
}
|
||
}
|
||
private func readiness(_ slot: Int) -> String? {
|
||
let assignment = machine.cartridgeAssignments.slots[slot]
|
||
if assignment.kind == .rom && machine.cartridgeNames[slot].isEmpty { return "ROM unavailable — choose a replacement or retry." }
|
||
if assignment.kind == .floppy && !machine.diskEnabled { return "Choose a disk ROM in Floppy Disks to enable this controller." }
|
||
if assignment.kind == .sdc && (assignment.url == nil || assignment.sdcFolderURL == nil) {
|
||
return "Inactive — choose an SDC-DOS ROM and SD card folder in Details."
|
||
}
|
||
if assignment.kind == .speechSound && (assignment.url == nil || assignment.speechROMURL == nil) {
|
||
return "Inactive — choose both ROMs to enable speech and music."
|
||
}
|
||
return nil
|
||
}
|
||
private func deviceMenu(_ slot: Int) -> some View {
|
||
let current = machine.cartridgeAssignments.slots[slot].kind
|
||
return Menu {
|
||
Button { machine.openCartridgeROM(slot) } label: {
|
||
Label("ROM Cartridge…", systemImage: current == .rom ? "checkmark" : "folder")
|
||
}.accessibilityLabel("Choose ROM for slot \(slot)")
|
||
ForEach([CartridgeAssignments.Kind.floppy, .hardDisk, .ramDisk, .orchestra90, .speechSound, .sdc], id: \.rawValue) { kind in
|
||
Button { machine.requestCartridge(kind, slot: slot) } label: {
|
||
Label(menuTitle(kind, slot: slot), systemImage: current == kind ? "checkmark" : symbol(kind))
|
||
}.disabled(current == kind).accessibilityLabel("Use \(kind == .floppy ? "floppy controller" : kind == .hardDisk ? "hard disk" : kind == .ramDisk ? "RAM disk" : kind == .orchestra90 ? "Orchestra-90" : kind == .sdc ? "CoCo SDC" : "Speech/Sound") in slot \(slot)")
|
||
}
|
||
Divider()
|
||
Button { machine.requestCartridge(.empty, slot: slot) } label: {
|
||
Label("Remove Cartridge", systemImage: "minus.circle")
|
||
}.disabled(current == .empty).accessibilityLabel("Remove cartridge from slot \(slot)")
|
||
} label: { Label("Device", systemImage: "slider.horizontal.3") }
|
||
.menuStyle(.borderlessButton).fixedSize().padding(.horizontal, 10).padding(.vertical, 6)
|
||
.background(.quaternary.opacity(0.6), in: RoundedRectangle(cornerRadius: 6))
|
||
.accessibilityIdentifier("cartridge-device-\(slot)")
|
||
}
|
||
private func menuTitle(_ kind: CartridgeAssignments.Kind, slot: Int) -> String {
|
||
var result = title(kind)
|
||
if slot > 0, let previous = (1...4).first(where: { $0 != slot && machine.cartridgeAssignments.slots[$0].kind == kind }) {
|
||
result += " — moves from Slot \(previous)"
|
||
}
|
||
return result
|
||
}
|
||
private func symbol(_ kind: CartridgeAssignments.Kind) -> String {
|
||
switch kind {
|
||
case .floppy, .hardDisk: return "externaldrive"
|
||
case .ramDisk, .rom, .empty: return "memorychip"
|
||
case .orchestra90: return "music.note"
|
||
case .speechSound: return "waveform"
|
||
case .sdc: return "sdcard"
|
||
}
|
||
}
|
||
@ViewBuilder private func contextualAction(_ slot: Int) -> some View {
|
||
switch machine.cartridgeAssignments.slots[slot].kind {
|
||
case .rom:
|
||
SecondaryAction(title: "Choose ROM…", systemImage: "folder") { machine.openCartridgeROM(slot) }
|
||
.accessibilityLabel("Choose ROM for slot \(slot)")
|
||
case .floppy:
|
||
SecondaryAction(title: "Floppy Disks…", systemImage: "externaldrive") { openWindow(id: "disks") }
|
||
case .hardDisk:
|
||
SecondaryAction(title: "Hard Disks…", systemImage: "externaldrive") { openWindow(id: "hard-disks") }
|
||
case .orchestra90:
|
||
SecondaryAction(title: "Choose ROM…", systemImage: "folder") { machine.openCartridgeROM(slot, kind: .orchestra90) }
|
||
.accessibilityLabel("Choose Orchestra-90 ROM for slot \(slot)")
|
||
case .sdc:
|
||
SecondaryAction(title: "SD Card…", systemImage: "sdcard") { expandedSlots.insert(slot) }
|
||
.accessibilityLabel("Show CoCo SDC settings for slot \(slot)")
|
||
case .speechSound:
|
||
SecondaryAction(title: "ROMs…", systemImage: "memorychip") { expandedSlots.insert(slot) }
|
||
.accessibilityLabel("Show Speech/Sound ROMs for slot \(slot)")
|
||
case .ramDisk, .empty: EmptyView()
|
||
}
|
||
}
|
||
@ViewBuilder private func details(_ slot: Int) -> some View {
|
||
let assignment = machine.cartridgeAssignments.slots[slot]
|
||
VStack(alignment: .leading, spacing: 10) {
|
||
switch assignment.kind {
|
||
case .sdc:
|
||
Text("The selected folder represents an SD card. Choose this cartridge as the startup slot to boot SDC-DOS. Changing the ROM, folder or protection restarts the CoCo.")
|
||
romRow("SDC-DOS ROM", filename: assignment.url?.lastPathComponent, button: "Choose ROM…", label: "Choose SDC-DOS ROM for slot \(slot)") { machine.openCartridgeROM(slot, kind: .sdc) }
|
||
romRow("SD card folder", filename: assignment.sdcFolderURL?.path, button: "Choose Folder…", label: "Choose SD card folder for slot \(slot)") { machine.openSDCFolder(slot) }
|
||
if let folder = assignment.sdcFolderURL {
|
||
Button { NSWorkspace.shared.activateFileViewerSelecting([folder]) } label: { Label("Show SD Card Folder in Finder", systemImage: "folder") }
|
||
}
|
||
Toggle("Protect SD card files (read-only)", isOn: Binding(get: { assignment.sdcReadOnly ?? true }, set: { machine.setSDCReadOnly($0, slot: slot) }))
|
||
.accessibilityIdentifier("sdc-protected-\(slot)")
|
||
Text((assignment.sdcReadOnly ?? true) ? "Files are protected from guest writes and file-management commands." : "Guest writes save directly. File-management commands can create, rename or delete files in this folder.")
|
||
if (machine.cartridgeAssignments.multiPak ? slot > 0 : slot == 0), !machine.cartridgeNames[slot].isEmpty {
|
||
VStack(alignment: .leading, spacing: 5) {
|
||
Text("Current folder: \(machine.sdcDirectory.isEmpty ? "/" : machine.sdcDirectory)").textSelection(.enabled)
|
||
ForEach(0..<2) { drive in
|
||
Text("SDC\(drive): \(machine.sdcDriveNames.indices.contains(drive) && !machine.sdcDriveNames[drive].isEmpty ? machine.sdcDriveNames[drive] : "No image mounted")")
|
||
}
|
||
Text("Transfers · Read \(machine.sdcReadCount) · Written \(machine.sdcWriteCount)")
|
||
.monospacedDigit().accessibilityIdentifier("sdc-transfer-counts")
|
||
if let error = machine.sdcError, !error.isEmpty {
|
||
Label(error, systemImage: "exclamationmark.circle").foregroundStyle(.orange).textSelection(.enabled)
|
||
}
|
||
}
|
||
}
|
||
SecondaryHelp(title: "Using an SD card folder") {
|
||
Text("Keep extracted collections together with their original filenames and directories. In SDC-DOS, DRIVE 0,\"NAME.DSK\" mounts an image; DIR lists its BASIC files. Follow the software's launch instructions. SDC0–SDC1 are independent of FD0–FD3 and HD0–HD1.")
|
||
Text("Double Dragon: select the folder containing DDRAGON1.DSK through DDRAGON9.DSK, use 2 MB RAM, then enter DRIVE 0,\"DDRAGON1.DSK\" followed by RUN \"AUTOEXEC:0\". The game selects its later disks. Arrow keys move; Z starts and punches, X kicks, C jumps.")
|
||
Text("Profiles remember the folder, ROM and protection. Guest-selected mounts and current directory return to startup.cfg on restart. In System view, click the CoCo SDC cartridge or its slot for SD card controls. The transfer light reflects actual reads and writes.")
|
||
}
|
||
case .speechSound:
|
||
Text("Speech and music for supporting programs. This cartridge has no boot program; keep your disk controller as the startup slot.")
|
||
romRow("PIC firmware", filename: assignment.url?.lastPathComponent, button: "Choose PIC Firmware…", label: "Choose PIC firmware for slot \(slot)") { machine.openSpeechROM(slot, speech: false) }
|
||
romRow("Speech ROM", filename: assignment.speechROMURL?.lastPathComponent, button: "Choose Speech ROM…", label: "Choose speech ROM for slot \(slot)") { machine.openSpeechROM(slot, speech: true) }
|
||
Text("Sound, volume and mute use the normal Mac audio controls.")
|
||
case .orchestra90:
|
||
Text("Stereo playback works without a ROM in supporting software. The optional 8 KiB ROM adds the music program; choose its startup slot to boot it.")
|
||
Text(assignment.url.map { "Program ROM: \($0.lastPathComponent)" } ?? "No program ROM — stereo sound only").textSelection(.enabled)
|
||
if assignment.url != nil {
|
||
SecondaryAction(title: "Remove ROM…", systemImage: "minus.circle") { machine.requestOrchestraWithoutROM(slot) }
|
||
.accessibilityLabel("Remove Orchestra-90 ROM from slot \(slot)")
|
||
}
|
||
if assignment.url != nil && machine.cartridgeRestoreErrors[slot] == nil {
|
||
Text("William Tell demo: press a key at startup, then type S and Return to score. Wait for the cursor, then P and Return to play. Escape sends CoCo Break to stop.")
|
||
}
|
||
case .ramDisk:
|
||
Text("Requires RAM-disk-aware software; this is not a Disk BASIC drive. Resets retain data. Loading a profile, quitting, or removing the last RAM-disk assignment discards its contents. Profiles remember the device, not its data. This layout uses conventional controls.")
|
||
case .floppy:
|
||
Text("One FD-502 controller provides FD0–FD3. The selected disk ROM and mounted media are managed in Floppy Disks. Moving this controller retains its disks.")
|
||
case .hardDisk:
|
||
Text("This controller provides HD0 and HD1 and has no boot ROM. Use a compatible disk ROM or OS-9 boot disk. Moving it retains mounted images.")
|
||
case .rom:
|
||
Text("ROM file: \(assignment.url?.path ?? "Not selected")").textSelection(.enabled)
|
||
Text("Choosing another ROM restarts the CoCo. Removing this assignment does not delete its file.")
|
||
case .empty: EmptyView()
|
||
}
|
||
}.font(.callout).foregroundStyle(.secondary).fixedSize(horizontal: false, vertical: true)
|
||
}
|
||
private func romRow(_ title: String, filename: String?, button: String, label: String, action: @escaping () -> Void) -> some View {
|
||
HStack {
|
||
VStack(alignment: .leading, spacing: 3) {
|
||
Text(title).fontWeight(.medium)
|
||
Text(filename ?? "Not selected").lineLimit(1).truncationMode(.middle)
|
||
}
|
||
Spacer()
|
||
SecondaryAction(title: button, systemImage: "folder", action: action).accessibilityLabel(label)
|
||
}
|
||
}
|
||
}
|