Files
VCCPlusOSS/platforms/macos/App/HardDiskControlsView.swift

229 lines
15 KiB
Swift
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// SPDX-License-Identifier: GPL-3.0-or-later
import SwiftUI
struct HardDiskControlsView: View {
@ObservedObject var machine: MachineModel
@Environment(\.openWindow) private var openWindow
@State private var newSizeKiB = 132480
@State private var newDisk: NewHardDiskRequest?
@State private var clockExpanded = false
@State private var detailsExpanded = false
var body: some View {
VStack(spacing: 0) {
SecondaryHeader(title: "Virtual Hard Disks", badge: "hard").padding(20)
Divider()
ScrollView {
VStack(alignment: .leading, spacing: 14) {
if !machine.hardDiskConnected {
Label("Controller disconnected · Mounted images are retained", systemImage: "exclamationmark.circle")
.font(.callout).foregroundStyle(.orange)
SecondaryAction(title: "Cartridges…", systemImage: "slider.horizontal.3") { openWindow(id: "cartridges") }
} else {
HStack {
Label("Hard-disk controller connected", systemImage: "cable.connector")
Spacer()
Text(machine.diskSaving ? "Saving automatically…" : "Writable images save automatically")
}.font(.caption).foregroundStyle(.secondary).frame(minHeight: 18)
}
ForEach(Array(machine.hardDisks.enumerated()), id: \.offset) { index, disk in
HardDiskDriveRow(machine: machine, index: index, disk: disk) { newDisk = NewHardDiskRequest(id: index) }
}
if !machine.hardRestoreErrors.isEmpty {
SecondaryAction(title: "Retry Unavailable Hard Disks", systemImage: "arrow.clockwise", action: machine.retryHardDisks)
}
DisclosureGroup(isExpanded: $clockExpanded) {
VStack(alignment: .leading, spacing: 10) {
Toggle("Clock enabled", isOn: Binding(get: { machine.hardSettings.clockEnabled }, set: { machine.setHardClock(enabled: $0, readOnly: machine.hardSettings.clockReadOnly) }))
Toggle("Allow guest to set the virtual clock", isOn: Binding(get: { !machine.hardSettings.clockReadOnly }, set: { machine.setHardClock(enabled: machine.hardSettings.clockEnabled, readOnly: !$0) }))
.disabled(!machine.hardSettings.clockEnabled)
Text("Starts from Mac local time. Guest changes affect only this session's emulated clock, not your Mac clock.")
.font(.caption).foregroundStyle(.secondary).fixedSize(horizontal: false, vertical: true)
}.padding(.top, 10)
} label: {
Label("Virtual clock", systemImage: "clock").font(.callout).fontWeight(.medium)
Text(machine.hardSettings.clockEnabled ? "Enabled" : "Disabled").font(.caption).foregroundStyle(.secondary)
}.accessibilityIdentifier("hard-disk-clock")
DisclosureGroup(isExpanded: $detailsExpanded) {
VStack(alignment: .leading, spacing: 10) {
Text("Selected HD\(machine.hardSelectedDrive) · Sector \(machine.hardSector) · Status \(machine.hardStatus)")
.monospacedDigit().accessibilityIdentifier("hard-disk-diagnostics")
Text("Raw VHD, OS9 and IMG sector images are supported. This device has no boot ROM: use a compatible disk ROM or OS-9 boot disk.")
.foregroundStyle(.secondary).fixedSize(horizontal: false, vertical: true)
}.font(.callout).padding(.top, 10)
} label: { Label("Controller Details", systemImage: "slider.horizontal.3").font(.callout).fontWeight(.medium) }
.accessibilityIdentifier("hard-disk-controller-details")
SecondaryHelp(title: "About hard-disk images") {
Text("Writable images save automatically. Temporary changes stay in memory until Save As. Drive choices are remembered.")
Text("New images are blank. Format or partition them with guest software, then choose Save As to create the Mac file. Creating a disk does not install an operating system.")
Text("Edit opens the Disk Image Editor for OS-9 volumes and numbered HDB-DOS disks. The CoCo stays paused and locked until editing finishes; applying edits requires an explicit restart.")
Text("Discard and Eject loses only unsaved in-memory changes. Earlier automatic saves cannot be undone.")
}
if let error = machine.diskSettingsError {
Text(error).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 { Text(error).foregroundStyle(.red).textSelection(.enabled) }
}.padding(20)
}
Divider()
MediaExecutionFooter(machine: machine, note: "Media and clock changes pause the CoCo; press Run to continue.").padding(20)
}.frame(minWidth: 700, minHeight: 450)
.disabled(machine.busy || machine.diskSaving || machine.diskEditorLocked || machine.state == .closed)
.sheet(item: $newDisk) { request in
NewHardDiskSheet(machine: machine, drive: request.id, initialSize: newSizeKiB) { size in
newSizeKiB = size
machine.createHardDisk(request.id, sizeKiB: size)
}
}
}
}
private struct NewHardDiskRequest: Identifiable { let id: Int }
private struct NewHardDiskSheet: View {
@ObservedObject var machine: MachineModel
let drive: Int
let create: (Int) -> Void
@Environment(\.dismiss) private var dismiss
@State private var size: String
init(machine: MachineModel, drive: Int, initialSize: Int, create: @escaping (Int) -> Void) {
self.machine = machine; self.drive = drive; self.create = create
_size = State(initialValue: String(initialSize))
}
private var parsedSize: Int? {
let value = size.trimmingCharacters(in: .whitespacesAndNewlines)
guard !value.isEmpty, value.allSatisfy({ $0.isASCII && $0.isNumber }), let number = Int(value), (1...524288).contains(number) else { return nil }
return number
}
private var canCreate: Bool {
parsedSize != nil && !machine.busy && !machine.diskSaving && machine.state != .closed
&& machine.hardDisks.indices.contains(drive) && !machine.hardDisks[drive].dirty
}
var body: some View {
VStack(alignment: .leading, spacing: 18) {
Label("New disk in HD\(drive)", systemImage: "doc.badge.plus").font(.title2)
Text("Creates a blank in-memory image in this drive. Any currently mounted image is replaced; its Mac file is kept.")
.foregroundStyle(.secondary).fixedSize(horizontal: false, vertical: true)
HStack {
Text("Capacity (KiB)")
TextField("132480", text: $size).frame(width: 130)
.accessibilityLabel("New hard disk size in KiB").accessibilityIdentifier("new-hard-disk-size")
Text("1–524,288 KiB").font(.caption).foregroundStyle(.secondary)
}
Text(parsedSize == nil ? "Enter a whole number from 1 through 524288." : "132480 KiB is the conventional VCC image size.")
.font(.caption).foregroundStyle(parsedSize == nil ? Color.orange : Color.secondary)
.frame(minHeight: 16, alignment: .leading)
Label("Format or partition the disk in the CoCo, then use Save As to keep it. No operating system is installed.", systemImage: "info.circle")
.font(.callout).foregroundStyle(.secondary).fixedSize(horizontal: false, vertical: true)
HStack {
Spacer()
Button("Cancel") { dismiss() }.keyboardShortcut(.cancelAction)
Button { if let value = parsedSize, canCreate { dismiss(); create(value) } } label: { Label("Create Disk", systemImage: "doc.badge.plus") }
.keyboardShortcut(.defaultAction).disabled(!canCreate)
.accessibilityIdentifier("create-hard-disk-confirm")
}
}.padding(24).frame(width: 470)
}
}
private struct HardDiskDriveRow: View {
@ObservedObject var machine: MachineModel
let index: Int
let disk: VCCDiskSnapshot
let newDisk: () -> Void
@State private var options = false
private var assigned: Bool { disk.mounted || machine.hardSettings.drives[index] != nil }
private var manualSave: Bool {
disk.dirty && (machine.hardTemporary[index] || machine.hardSettings.drives[index] == nil || disk.writeProtected || disk.sourceReadOnly)
}
private var mode: String {
if disk.writeProtected { return "Write protected" }
if machine.hardTemporary[index] { return "Temporary changes" }
return machine.hardSettings.drives[index] == nil ? "No backing file · Use Save As" : "Saves automatically"
}
private var capacity: String {
let bytes = Double(disk.byteCount)
return bytes >= 1048576 ? String(format: "%.1f MiB", bytes / 1048576) : String(format: "%.0f KiB", bytes / 1024)
}
var body: some View {
SecondaryCard {
VStack(alignment: .leading, spacing: 12) {
HStack(spacing: 14) {
MediaDriveIdentity(name: "HD\(index)", mounted: disk.mounted)
VStack(alignment: .leading, spacing: 4) {
Text(disk.mounted ? disk.name : machine.hardSettings.drives[index]?.url.lastPathComponent ?? "No disk inserted")
.fontWeight(.semibold).lineLimit(1).truncationMode(.middle)
Text(disk.mounted ? "\(capacity) · \(mode)" : assigned ? "Unavailable — reconnect or replace the file" : "Insert an image or create a blank disk")
.font(.caption).foregroundStyle(assigned && !disk.mounted ? Color.orange : Color.secondary)
.fixedSize(horizontal: false, vertical: true)
}.frame(maxWidth: .infinity, alignment: .leading)
SecondaryAction(title: assigned ? "Replace…" : "Insert…", systemImage: "folder") { machine.openHardDisk(index) }
.disabled(disk.dirty).accessibilityLabel("Insert hard disk \(index)")
if disk.mounted {
SecondaryAction(title: "Edit…", systemImage: "pencil") {
DiskImageEditorCoordinator.shared.editHardDisk(index, machine: machine)
}.accessibilityLabel("Edit hard disk \(index)").accessibilityIdentifier("hard-disk-edit-\(index)")
.disabled(machine.diskEditorLocked)
}
if assigned {
SecondaryAction(title: "Eject", systemImage: "eject") { machine.ejectHardDisk(index) }
.disabled(disk.dirty).accessibilityLabel("Eject hard disk \(index)")
} else {
SecondaryAction(title: "New…", systemImage: "doc.badge.plus", action: newDisk)
.disabled(disk.dirty).accessibilityLabel("New hard disk \(index)")
}
}
if disk.mounted {
HStack {
DisclosureGroup(isExpanded: $options) { EmptyView() } label: { Text("Disk options").font(.callout) }
.accessibilityIdentifier("hard-disk-options-\(index)")
Spacer()
actions
}
if options {
HStack(spacing: 24) {
Toggle("Write protected", isOn: Binding(get: { disk.writeProtected }, set: { machine.setHardProtection(index, $0) }))
.disabled(disk.sourceReadOnly).accessibilityLabel("Write protect hard disk \(index)")
.help(disk.sourceReadOnly ? "The source file is read-only. Reopen a writable copy to enable writes." : "Prevent the CoCo from changing this image.")
Toggle("Temporary changes", isOn: Binding(get: { machine.hardTemporary[index] }, set: { machine.setHardTemporary(index, $0) }))
.accessibilityLabel("Temporary changes in hard disk \(index)")
Spacer(minLength: 0)
}.font(.callout)
Text("\(disk.byteCount / 256) sectors · Temporary changes stay in memory. Turning this off allows pending changes to save. Save As retains this mode.")
.font(.caption).foregroundStyle(.secondary).fixedSize(horizontal: false, vertical: true)
}
if manualSave {
HStack {
Label("Unsaved changes · Save As to keep them", systemImage: "exclamationmark.circle").foregroundStyle(.orange)
Spacer()
SecondaryAction(title: "Save As…", systemImage: "square.and.arrow.up") { machine.exportHardDisk(index) }
.accessibilityLabel("Save hard disk \(index) as")
}.font(.callout).frame(minHeight: 24)
}
}
if let error = machine.hardRestoreErrors[index] { Text(error).font(.callout).foregroundStyle(.red).textSelection(.enabled) }
if let error = machine.hardSaveErrors[index] {
Text(error).font(.callout).foregroundStyle(.red).textSelection(.enabled)
SecondaryAction(title: "Retry Save", systemImage: "arrow.clockwise") { machine.retryHardSave(index) }
.disabled(disk.writeProtected || machine.hardTemporary[index]).accessibilityLabel("Retry saving hard disk \(index)")
}
}
}.modifier(MediaDropTarget(machine: machine, destination: .hardDisk(index)))
}
private var actions: some View {
Menu {
Button { machine.exportHardDisk(index) } label: { Label("Save As…", systemImage: "square.and.arrow.up") }
.disabled(!disk.mounted).accessibilityLabel("Save hard disk \(index) as")
Button(action: newDisk) { Label("New Disk…", systemImage: "doc.badge.plus") }
.disabled(disk.dirty).accessibilityLabel("New hard disk \(index)")
if manualSave || machine.hardSaveErrors[index] != nil {
Divider()
Button { machine.ejectHardDisk(index, discard: true) } label: { Label("Discard Changes and Eject…", systemImage: "eject") }
.accessibilityLabel("Discard hard disk \(index) changes")
}
} label: { Label("Actions", systemImage: "ellipsis.circle") }
.fixedSize().accessibilityIdentifier("hard-disk-actions-\(index)")
}
}