618 lines
36 KiB
Swift
618 lines
36 KiB
Swift
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
import SwiftUI
|
|
import AppKit
|
|
import Combine
|
|
import UniformTypeIdentifiers
|
|
|
|
@MainActor
|
|
final class ApplicationDelegate: NSObject, NSApplicationDelegate, ObservableObject {
|
|
let machine: MachineModel
|
|
let presentation: PresentationSettings
|
|
lazy var development = CMOCDevelopmentModel(machine: machine)
|
|
private var openedAssemblyWorkspace: AssemblyDevelopmentModel?
|
|
var assemblyDevelopment: AssemblyDevelopmentModel {
|
|
if let workspace = openedAssemblyWorkspace { return workspace }
|
|
let workspace = AssemblyDevelopmentModel(machine: machine)
|
|
openedAssemblyWorkspace = workspace
|
|
return workspace
|
|
}
|
|
lazy var basicDevelopment = BASICDevelopmentModel(machine: machine, directory: profileStore.directory.deletingLastPathComponent().appendingPathComponent("BASICWorkspace"))
|
|
lazy var basicReference: Result<BASICReferenceModel, Error> = BASICReferenceCatalog.bundled.map { BASICReferenceModel(catalog: $0) }
|
|
lazy var manualReader = ManualReader()
|
|
private var basicExamples: [String: BASICDevelopmentModel] = [:]
|
|
func exampleWorkspace(_ id: String) -> BASICDevelopmentModel? {
|
|
if let existing = basicExamples[id] { return existing }
|
|
let source: String
|
|
let title: String
|
|
let directory = profileStore.directory.deletingLastPathComponent().appendingPathComponent("BASICExamples")
|
|
if id.hasPrefix("manual/") {
|
|
let path = String(id.dropFirst("manual/".count))
|
|
guard let url = ManualLibraryContent.bundled?.exampleURL(for: path),
|
|
let (text, _) = try? BASICDevelopmentModel.readText(url) else { return nil }
|
|
source = text
|
|
title = url.lastPathComponent
|
|
} else {
|
|
guard let reference = try? basicReference.get(),
|
|
let example = reference.catalog.examples.first(where: { $0.id == id }) else { return nil }
|
|
source = example.source
|
|
title = example.title
|
|
}
|
|
let workspace = BASICDevelopmentModel(machine: machine, directory: directory.appendingPathComponent(id),
|
|
initialSource: source, title: title)
|
|
basicExamples[id] = workspace
|
|
return workspace
|
|
}
|
|
func archiveWorkspace(_ id: String) async throws -> BASICDevelopmentModel? {
|
|
if let workspace = basicExamples[id] { return workspace }
|
|
guard id.hasPrefix("archive/"),
|
|
let example = try await ArchiveLibraryCatalog.shared.basicExample(sourceSHA256: String(id.dropFirst("archive/".count))) else { return nil }
|
|
try Task.checkCancellation()
|
|
if let workspace = basicExamples[id] { return workspace }
|
|
let root = profileStore.directory.deletingLastPathComponent().appendingPathComponent("BASICExamples")
|
|
let workspace = BASICDevelopmentModel(machine: machine, directory: root.appendingPathComponent(id),
|
|
initialSource: example.source, title: example.title)
|
|
basicExamples[id] = workspace
|
|
return workspace
|
|
}
|
|
lazy var softwareLibrary = SoftwareLibraryModel(state: SoftwareLibraryState(directory: profileStore.directory.deletingLastPathComponent().appendingPathComponent("SoftwareLibrary")), continuity: LibraryContinuityStore.shared, artworkDirectory: profileStore.directory.deletingLastPathComponent().appendingPathComponent("SoftwareLibrary/ArtworkCache"), machine: machine,
|
|
sourceWorkspaceIdentity: { [weak self] in self?.development.assistantProjectID ?? UUID() },
|
|
openSourceProject: { [weak self] directory, identity, configuration in
|
|
guard let self else { return false }
|
|
return try await self.development.openImportedProject(at: directory, expectedProjectID: identity, preferredConfiguration: configuration)
|
|
})
|
|
let profileStore: ProfileStore
|
|
private let startupValidation: StartupValidationContext?
|
|
private let companionServiceLaunch: Bool
|
|
@Published private(set) var companionLaunchDeferred = false
|
|
private var companionService: VCCCompanionService?
|
|
private weak var deferredMachineWindow: NSWindow?
|
|
private var finishedLaunching = false
|
|
private static var isolatedCompanionLaunch: Bool {
|
|
#if VCC_UI_TESTING
|
|
return true
|
|
#else
|
|
return CommandLine.arguments.contains("--validate-startup") || CommandLine.arguments.contains("--interface-validation")
|
|
#endif
|
|
}
|
|
var windowTitle: String {
|
|
#if VCC_UI_TESTING
|
|
return "VCC UI Tests"
|
|
#else
|
|
return startupValidation == nil ? "VCC Plus OSS" : "VCC Startup Validation"
|
|
#endif
|
|
}
|
|
#if DEBUG
|
|
private var validation: InterfaceValidation?
|
|
#endif
|
|
override init() {
|
|
let serviceLaunch = CommandLine.arguments.contains("--companion-service") && !Self.isolatedCompanionLaunch
|
|
companionServiceLaunch = serviceLaunch
|
|
_companionLaunchDeferred = Published(initialValue: serviceLaunch)
|
|
var startupValidationRequested = CommandLine.arguments.contains("--validate-startup")
|
|
#if !DEBUG
|
|
// A release build has no InterfaceValidation type, but still fails closed.
|
|
startupValidationRequested = startupValidationRequested || CommandLine.arguments.contains("--interface-validation")
|
|
#endif
|
|
if startupValidationRequested {
|
|
do { startupValidation = try StartupValidationContext() }
|
|
catch { fatalError("Cannot initialize isolated startup validation: \(error)") }
|
|
} else { startupValidation = nil }
|
|
if let startupValidation {
|
|
machine = startupValidation.model()
|
|
presentation = PresentationSettings(defaults: startupValidation.defaults)
|
|
profileStore = ProfileStore(directory: startupValidation.directory.appendingPathComponent("Profiles"))
|
|
} else {
|
|
#if DEBUG
|
|
#if VCC_UI_TESTING
|
|
let isolatedInterface = true // Fail closed even if launched without arguments.
|
|
#else
|
|
let isolatedInterface = CommandLine.arguments.contains("--interface-validation")
|
|
#endif
|
|
if isolatedInterface {
|
|
// Fail closed rather than accidentally validating against personal settings.
|
|
let fixture: InterfaceValidation
|
|
do { fixture = try InterfaceValidation() }
|
|
catch { fatalError("Cannot create isolated interface validation: \(error)") }
|
|
validation = fixture; machine = fixture.makeModel(); presentation = PresentationSettings(defaults: fixture.defaults)
|
|
profileStore = ProfileStore(directory: fixture.directory.appendingPathComponent("Profiles"))
|
|
} else { machine = MachineModel(startupStore: StartupStore(directory: DiskAssignmentStore.standard.url.deletingLastPathComponent(), kit: .bundled), restoreMediaOnLaunch: !serviceLaunch); presentation = PresentationSettings(); profileStore = .standard }
|
|
#else
|
|
machine = MachineModel(startupStore: StartupStore(directory: DiskAssignmentStore.standard.url.deletingLastPathComponent(), kit: .bundled), restoreMediaOnLaunch: !serviceLaunch); presentation = PresentationSettings(); profileStore = .standard
|
|
#endif
|
|
}
|
|
super.init()
|
|
LibraryContinuityStore.configureShared(directory: profileStore.directory.deletingLastPathComponent())
|
|
}
|
|
func applicationDidFinishLaunching(_ notification: Notification) {
|
|
#if VCC_UI_TESTING
|
|
// Per-process appearance for reproducible isolated UI inspection. This
|
|
// flag is absent from the shipping app and never changes Mac settings.
|
|
let testAppearances: [String: NSAppearance.Name] = [
|
|
"light": .aqua, "dark": .darkAqua,
|
|
"light-high-contrast": .accessibilityHighContrastAqua,
|
|
"dark-high-contrast": .accessibilityHighContrastDarkAqua
|
|
]
|
|
if let style = UserDefaults.standard.string(forKey: "VCCUITestAppearance"),
|
|
let appearance = testAppearances[style] {
|
|
NSApplication.shared.appearance = NSAppearance(named: appearance)
|
|
}
|
|
#endif
|
|
// Xcode's debugger launch can show a generic Dock tile even when Finder
|
|
// resolves CFBundleIconFile correctly. Load this build's bundled artwork
|
|
// directly instead of relying on the launch service's cached icon.
|
|
if let url = Bundle.main.url(forResource: "AppIcon", withExtension: "icns"),
|
|
let icon = NSImage(contentsOf: url) {
|
|
NSApplication.shared.applicationIconImage = icon
|
|
}
|
|
#if DEBUG
|
|
if let validation {
|
|
Task { @MainActor in
|
|
do {
|
|
let args = CommandLine.arguments
|
|
func directory(_ key: String) -> URL? {
|
|
guard let i = args.firstIndex(of: key), args.indices.contains(i + 1) else { return nil }
|
|
return URL(fileURLWithPath: args[i + 1])
|
|
}
|
|
if let roms = directory("--mcp-rom-dir"), let c = directory("--mcp-c-validation-dir") {
|
|
for _ in 0..<500 where machine.busy { try await Task.sleep(for: .milliseconds(10)) }
|
|
try await MCPCMOC.shared.fixture(development, directory: c, roms: roms)
|
|
return
|
|
}
|
|
if let roms = directory("--mcp-rom-dir"), let assembly = directory("--mcp-assembly-fixture") {
|
|
for _ in 0..<500 where machine.busy { try await Task.sleep(for: .milliseconds(10)) }
|
|
try await MCPAssembly.shared.startFixture(assemblyDevelopment, directory: assembly, romDirectory: roms)
|
|
return
|
|
}
|
|
if args.contains("--mcp-edit-review"), args.contains("--mcp-rom-dir"), args.contains("--mcp-validation-dir") {
|
|
for _ in 0..<500 where machine.busy { try await Task.sleep(nanoseconds: 10_000_000) }
|
|
} else {
|
|
try await validation.install(in: machine)
|
|
validation.measure(machine, presentation: presentation)
|
|
}
|
|
if let index = args.firstIndex(of: "--mcp-validation-dir"), args.indices.contains(index + 1) {
|
|
let other = BASICDevelopmentModel(machine: machine, directory: validation.directory.appendingPathComponent("PrivateBASIC"), title: "Other document")
|
|
try await MCPAccess.shared.validate(model: basicDevelopment, other: other, in: URL(fileURLWithPath: args[index + 1]))
|
|
}
|
|
}
|
|
catch { NSLog("Interface validation failed: %@", error.localizedDescription) }
|
|
}
|
|
}
|
|
#endif
|
|
if CompanionValidation.enabled || (!Self.isolatedCompanionLaunch && !CompanionValidation.requested) {
|
|
do {
|
|
let configuration = try CompanionValidation.configuration(forVCCPlus: true)
|
|
companionService = try VCCCompanionService(library: softwareLibrary, configuration: configuration)
|
|
} catch { NSLog("Companion service unavailable: %@", error.localizedDescription) }
|
|
}
|
|
if companionLaunchDeferred {
|
|
for window in NSApp.windows { window.orderOut(nil) }
|
|
}
|
|
finishedLaunching = true
|
|
if !Self.isolatedCompanionLaunch && !CompanionValidation.requested {
|
|
// OSS releases are obtained manually; no update service is scheduled.
|
|
}
|
|
}
|
|
|
|
func registerDeferredCompanionWindow(_ window: NSWindow) {
|
|
guard companionLaunchDeferred else { return }
|
|
deferredMachineWindow = window
|
|
window.isRestorable = false
|
|
window.orderOut(nil)
|
|
}
|
|
private func restoreCompanionMachineForForeground() {
|
|
guard companionLaunchDeferred else { return }
|
|
machine.restoreDeferredCompanionMachine()
|
|
companionLaunchDeferred = false
|
|
deferredMachineWindow?.isRestorable = true
|
|
deferredMachineWindow?.makeKeyAndOrderFront(nil)
|
|
}
|
|
func applicationDidBecomeActive(_ notification: Notification) {
|
|
guard finishedLaunching else { return }
|
|
restoreCompanionMachineForForeground()
|
|
}
|
|
func applicationShouldHandleReopen(_ sender: NSApplication, hasVisibleWindows flag: Bool) -> Bool {
|
|
if companionLaunchDeferred {
|
|
if sender.isActive { restoreCompanionMachineForForeground() }
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
func applicationShouldRestoreApplicationState(_ coder: NSCoder) -> Bool { !companionServiceLaunch }
|
|
func applicationShouldSaveApplicationState(_ coder: NSCoder) -> Bool { !companionLaunchDeferred }
|
|
|
|
func application(_ sender: NSApplication, open urls: [URL]) {
|
|
restoreCompanionMachineForForeground()
|
|
guard urls.count == 1, let url = urls.first else { machine.reportMediaError("Open one media file at a time."); return }
|
|
Task { @MainActor in
|
|
for _ in 0..<200 {
|
|
if !machine.busy && !machine.diskSaving { machine.offerMedia(url); return }
|
|
try? await Task.sleep(nanoseconds: 50_000_000)
|
|
}
|
|
machine.reportMediaError("The machine is busy. Try File → Open Media after the current operation finishes.")
|
|
}
|
|
}
|
|
func applicationWillTerminate(_ notification: Notification) {
|
|
companionService?.stop()
|
|
startupValidation?.cleanUp()
|
|
#if DEBUG
|
|
validation?.cleanUp()
|
|
#endif
|
|
}
|
|
private var terminationPending = false
|
|
func applicationShouldTerminate(_ sender: NSApplication) -> NSApplication.TerminateReply {
|
|
if machine.captures.saving {
|
|
let alert = NSAlert(); alert.messageText = "Wait for the CoCo image to finish saving."
|
|
alert.informativeText = "Your captured picture is still being written. Try quitting again after saving completes."
|
|
alert.runModal()
|
|
return .terminateCancel
|
|
}
|
|
if machine.captures.hasPendingImage {
|
|
let alert = NSAlert(); alert.messageText = "A captured CoCo image has not been saved."
|
|
alert.addButton(withTitle: "Cancel Quit"); alert.addButton(withTitle: "Save Image As…"); alert.addButton(withTitle: "Discard Image and Continue")
|
|
let choice = alert.runModal()
|
|
if choice == .alertSecondButtonReturn { machine.captures.saveImageAs(); return .terminateCancel }
|
|
guard choice == .alertThirdButtonReturn else { return .terminateCancel }
|
|
machine.captures.discardImage()
|
|
}
|
|
if machine.captures.active {
|
|
guard machine.captures.recording, !terminationPending else { return .terminateCancel }
|
|
let alert = NSAlert(); alert.messageText = "Stop and save the CoCo movie before quitting?"
|
|
alert.addButton(withTitle: "Cancel Quit"); alert.addButton(withTitle: "Stop and Save")
|
|
guard alert.runModal() == .alertSecondButtonReturn else { return .terminateCancel }
|
|
terminationPending = true
|
|
Task { @MainActor in
|
|
await machine.captures.stopRecording()
|
|
terminationPending = false
|
|
if machine.captures.error == nil { sender.terminate(nil) }
|
|
}
|
|
return .terminateCancel
|
|
}
|
|
if companionLaunchDeferred {
|
|
guard !terminationPending else { return .terminateCancel }
|
|
terminationPending = true
|
|
companionService?.stop()
|
|
Task { @MainActor in
|
|
await softwareLibrary.shutdown()
|
|
machine.shutdown { sender.reply(toApplicationShouldTerminate: true) }
|
|
}
|
|
return .terminateLater
|
|
}
|
|
guard DiskImageEditorCoordinator.shared.prepareToQuit() else { return .terminateCancel }
|
|
if machine.busy || machine.printer.busy || machine.printer.saving || terminationPending { return .terminateCancel }
|
|
guard development.confirmDiscard(), basicDevelopment.confirmDiscard(), basicExamples.values.allSatisfy({ $0.confirmDiscard() }) else { return .terminateCancel }
|
|
guard openedAssemblyWorkspace?.prepareToQuit() != false else { return .terminateCancel }
|
|
development.cancelBuild()
|
|
terminationPending = true
|
|
// Refresh after pausing on the emulation queue: the last UI poll can be
|
|
// older than the guest's most recent disk write or cassette sample.
|
|
machine.prepareToQuit { [self] in
|
|
if machine.unsavedMedia || machine.printer.unsaved {
|
|
let alert = NSAlert()
|
|
alert.messageText = "Quit without saving?"
|
|
alert.informativeText = "Unsaved disk changes, cassette recordings or printer output will be lost. Choose Cancel, then save your disks, export your tape, or open Media → Printer Output and save the capture. The machine will remain paused."
|
|
alert.addButton(withTitle: "Cancel"); alert.addButton(withTitle: "Quit Without Saving")
|
|
if alert.runModal() != .alertSecondButtonReturn {
|
|
terminationPending = false; sender.reply(toApplicationShouldTerminate: false); return
|
|
}
|
|
}
|
|
development.flushDraftRecovery { [self] in
|
|
Task { @MainActor in
|
|
await MCPArchiveAccess.shared.revoke().value
|
|
MCPAssembly.shared.revoke()
|
|
await MCPCMOC.shared.revoke().value
|
|
await MCPAccess.shared.revoke().value
|
|
await softwareLibrary.shutdown()
|
|
machine.shutdown { sender.reply(toApplicationShouldTerminate: true) }
|
|
}
|
|
}
|
|
}
|
|
return .terminateLater
|
|
}
|
|
}
|
|
|
|
@main
|
|
struct VCCMacApp: App {
|
|
@NSApplicationDelegateAdaptor(ApplicationDelegate.self) private var delegate
|
|
var body: some Scene {
|
|
Window(delegate.windowTitle, id: "machine") {
|
|
Group {
|
|
if delegate.companionLaunchDeferred {
|
|
VCCCompanionDeferredMachineView(onWindow: delegate.registerDeferredCompanionWindow)
|
|
} else {
|
|
MachineView(machine: delegate.machine, presentation: delegate.presentation, showManualHome: { delegate.manualReader.showHome() })
|
|
.focusedSceneValue(\.emulatorWindow, true).rememberPlacement("machine", in: delegate.presentation.windows)
|
|
}
|
|
}
|
|
}
|
|
.defaultSize(width: 1100, height: 820)
|
|
.commands { MachineCommands(machine: delegate.machine, presentation: delegate.presentation, connectAssistant: { MCPAccess.shared.show(for: delegate.basicDevelopment) }); SourceFindCommands() }
|
|
Window("Manual Library", id: "manual-library") {
|
|
ManualLibraryView(reader: delegate.manualReader)
|
|
.rememberPlacement("manual-library", in: delegate.presentation.windows)
|
|
}.defaultSize(width: 1000, height: 780)
|
|
Window("Archive Library", id: "archive-library") {
|
|
ArchiveLibraryView().rememberPlacement("archive-library", in: delegate.presentation.windows)
|
|
}.defaultSize(width: 1180, height: 780)
|
|
Window("Software Library", id: "software-library") {
|
|
SoftwareLibraryView(model: delegate.softwareLibrary).rememberPlacement("software-library", in: delegate.presentation.windows)
|
|
}.defaultSize(width: 1180, height: 780)
|
|
Window("Software Manual", id: "software-manual") {
|
|
SoftwareLibraryManualView(model: delegate.softwareLibrary.manual)
|
|
}.defaultSize(width: 850, height: 850)
|
|
Window("Printer Output", id: "printer") {
|
|
PrinterOutputView(printer: delegate.machine.printer).rememberPlacement("printer", in: delegate.presentation.windows)
|
|
}.defaultSize(width: 820, height: 700)
|
|
Window("BASIC Development", id: "basic-development") {
|
|
BASICDevelopmentView(model: delegate.basicDevelopment, reference: try? delegate.basicReference.get()).onDisappear { MCPAccess.shared.closing(delegate.basicDevelopment) }.rememberPlacement("basic-development", in: delegate.presentation.windows)
|
|
}.defaultSize(width: 1050, height: 760)
|
|
Window("BASIC Quick Reference", id: "basic-reference") {
|
|
BASICReferenceHost(result: delegate.basicReference, machine: delegate.machine) { entry in
|
|
delegate.manualReader.showBASICTopic(entry)
|
|
}
|
|
.rememberPlacement("basic-reference", in: delegate.presentation.windows)
|
|
}.defaultSize(width: 960, height: 760)
|
|
WindowGroup("BASIC Example", id: "basic-example", for: String.self) { $identifier in
|
|
if let identifier, identifier.hasPrefix("archive/") {
|
|
ArchiveExampleWindow(identifier: identifier, delegate: delegate)
|
|
} else if let identifier, let workspace = delegate.exampleWorkspace(identifier) {
|
|
BASICDevelopmentView(model: workspace, reference: try? delegate.basicReference.get())
|
|
.onDisappear { MCPAccess.shared.closing(workspace) }
|
|
} else { Text("This example is not available in the local reference.").padding() }
|
|
}.defaultSize(width: 1050, height: 760)
|
|
Window("C Development", id: "c-development") {
|
|
CMOCDevelopmentView(model: delegate.development).rememberPlacement("c-development", in: delegate.presentation.windows)
|
|
}.defaultSize(width: 1100, height: 780)
|
|
Window("Assembly Development", id: "assembly-development") {
|
|
AssemblyDevelopmentView(model: delegate.assemblyDevelopment)
|
|
.rememberPlacement("assembly-development", in: delegate.presentation.windows)
|
|
}.defaultSize(width: 1040, height: 740)
|
|
Window("Assembly Quick Reference", id: "assembly-reference") {
|
|
AssemblyReferenceWindow(model: delegate.assemblyDevelopment)
|
|
.rememberPlacement("assembly-reference", in: delegate.presentation.windows)
|
|
}.defaultSize(width: 960, height: 740)
|
|
Window("Debugger", id: "debugger") {
|
|
DebuggerView(machine: delegate.machine).rememberPlacement("debugger", in: delegate.presentation.windows)
|
|
}.defaultSize(width: 1120, height: 760)
|
|
Window("Release History", id: "release-history") { ReleaseHistoryView().rememberPlacement("release-history", in: delegate.presentation.windows) }.defaultSize(width: 760, height: 720)
|
|
Window("About VCC Plus OSS", id: "about") { AboutVCCView().rememberPlacement("about", in: delegate.presentation.windows) }.defaultSize(width: 760, height: 800)
|
|
Window("Licenses & Notices", id: "credits") { LicenseCreditsView().rememberPlacement("credits", in: delegate.presentation.windows) }.defaultSize(width: 760, height: 660)
|
|
Window("Display Settings", id: "display-settings") { DisplayControlsView(machine: delegate.machine).rememberPlacement("display-settings", in: delegate.presentation.windows) }.defaultSize(width: 600, height: 660)
|
|
Window("Keyboard & Shortcuts", id: "keyboard-help") { KeyboardHelpView().rememberPlacement("keyboard-help", in: delegate.presentation.windows) }.defaultSize(width: 760, height: 760)
|
|
Window("Configuration Profiles", id: "profiles") {
|
|
ProfileControlsView(machine: delegate.machine, store: delegate.profileStore).rememberPlacement("profiles", in: delegate.presentation.windows)
|
|
}.defaultSize(width: 820, height: 720)
|
|
Window("Machine Settings", id: "machine-settings") {
|
|
MachineSettingsView(machine: delegate.machine).rememberPlacement("machine-settings", in: delegate.presentation.windows)
|
|
}.defaultSize(width: 560, height: 660)
|
|
Window("Keyboard and Joysticks", id: "input") {
|
|
InputControlsView(input: delegate.machine.input).rememberPlacement("input", in: delegate.presentation.windows)
|
|
}.defaultSize(width: 700, height: 820)
|
|
Window("Virtual Hard Disks", id: "hard-disks") {
|
|
HardDiskControlsView(machine: delegate.machine).rememberPlacement("hard-disks", in: delegate.presentation.windows)
|
|
}.defaultSize(width: 780, height: 760)
|
|
Window("Cartridges", id: "cartridges") {
|
|
CartridgeControlsView(machine: delegate.machine).rememberPlacement("cartridges", in: delegate.presentation.windows)
|
|
}.defaultSize(width: 740, height: 660)
|
|
Window("Cassette", id: "cassette") {
|
|
CassetteControlsView(machine: delegate.machine).rememberPlacement("cassette", in: delegate.presentation.windows)
|
|
}.defaultSize(width: 800, height: 510)
|
|
Window("Floppy Disks", id: "disks") {
|
|
DiskControlsView(machine: delegate.machine).rememberPlacement("disks", in: delegate.presentation.windows)
|
|
}.defaultSize(width: 760, height: 650)
|
|
}
|
|
}
|
|
|
|
private struct AssemblyReferenceWindow: View {
|
|
let model: AssemblyDevelopmentModel
|
|
@Environment(\.openWindow) private var openWindow
|
|
|
|
var body: some View {
|
|
AssemblyReferenceView { example in
|
|
model.openExample(title: example.title, source: example.source, target6309: example.target6309)
|
|
openWindow(id: "assembly-development")
|
|
}
|
|
}
|
|
}
|
|
|
|
// Observe only command-relevant values; routine frame/status polling must not
|
|
// rebuild the native View menu while the user is choosing an item.
|
|
struct MachineCommands: Commands {
|
|
@FocusedValue(\.emulatorWindow) private var emulatorWindow
|
|
let machine: MachineModel
|
|
@ObservedObject private var menu: MachineMenuState
|
|
@ObservedObject private var captureMenu: CoCoCaptureMenuState
|
|
@ObservedObject var presentation: PresentationSettings
|
|
let connectAssistant: () -> Void
|
|
init(machine: MachineModel, presentation: PresentationSettings, connectAssistant: @escaping () -> Void) {
|
|
self.connectAssistant = connectAssistant
|
|
self.presentation = presentation
|
|
self.machine = machine
|
|
self.menu = machine.menuState
|
|
self.captureMenu = machine.captures.menuState
|
|
}
|
|
@Environment(\.openWindow) private var openWindow
|
|
@CommandsBuilder
|
|
private var primaryCommands: some Commands {
|
|
CommandGroup(replacing: .appInfo) {
|
|
Button("About VCC Plus OSS") { openWindow(id: "about") }
|
|
}
|
|
CommandGroup(replacing: .help) {
|
|
Button("Manual Library…") { openWindow(id: "manual-library") }
|
|
Button("Archive Library…") { openWindow(id: "archive-library") }
|
|
Divider()
|
|
Button("BASIC Quick Reference…") { openWindow(id: "basic-reference") }
|
|
Button("Assembly Quick Reference…") { openWindow(id: "assembly-reference") }
|
|
Button("Keyboard & Shortcuts…") { openWindow(id: "keyboard-help") }
|
|
Button("Release History…") { openWindow(id: "release-history") }
|
|
Button("Feedback…") {
|
|
NSWorkspace.shared.open(URL(string: "https://vccplus.brianholman.com/feedback/?product=vcc-plus")!)
|
|
}
|
|
Button("Licenses & Notices…") { openWindow(id: "credits") }
|
|
Button("About VCC Plus OSS") { openWindow(id: "about") }
|
|
}
|
|
CommandGroup(replacing: .appSettings) {
|
|
Button("Machine Settings…") { openWindow(id: "machine-settings") }.keyboardShortcut(",")
|
|
}
|
|
CommandMenu("Machine") {
|
|
Button("Configuration Profiles…") { openWindow(id: "profiles") }
|
|
.keyboardShortcut("p", modifiers: [.command, .shift])
|
|
Button("Machine Settings…") { openWindow(id: "machine-settings") }
|
|
Button("Restore App Defaults…") { machine.confirmRestoreAppDefaults() }
|
|
.disabled(!machine.hasBundledROMs || menu.values.busy || menu.values.diskSaving || menu.values.state == .closed)
|
|
Divider()
|
|
FastForwardMenuItem(machine: machine, values: menu.values, emulatorFocused: emulatorWindow == true)
|
|
}
|
|
CommandMenu("Develop") {
|
|
Button("Connect to ChatGPT…") {
|
|
openWindow(id: "basic-development")
|
|
DispatchQueue.main.async { connectAssistant() }
|
|
}
|
|
Divider()
|
|
Button("BASIC Development…") { openWindow(id: "basic-development") }
|
|
Button("BASIC Quick Reference…") { openWindow(id: "basic-reference") }
|
|
Button("C Development…") { openWindow(id: "c-development") }
|
|
.keyboardShortcut("c", modifiers: [.command, .option])
|
|
Button("Assembly Development…") { openWindow(id: "assembly-development") }
|
|
Button("Assembly Quick Reference…") { openWindow(id: "assembly-reference") }
|
|
}
|
|
CommandMenu("Debug") {
|
|
Button("Show Debugger") { openWindow(id: "debugger") }
|
|
.keyboardShortcut("d", modifiers: [.command, .option])
|
|
Divider()
|
|
Button(menu.values.state == .running ? "Pause Execution" : "Continue Execution") {
|
|
if menu.values.state == .running { machine.debuggerPause {} }
|
|
else { machine.debuggerContinue {} }
|
|
}
|
|
.keyboardShortcut("r", modifiers: [.command, .option])
|
|
.disabled(menu.values.busy || menu.values.diskSaving || !(menu.values.state == .running || menu.values.state == .paused))
|
|
Button("Step Instruction") { machine.debuggerStep {} }
|
|
.keyboardShortcut("i", modifiers: [.command, .option])
|
|
.disabled(menu.values.busy || menu.values.diskSaving || menu.values.state != .paused)
|
|
}
|
|
}
|
|
var body: some Commands {
|
|
primaryCommands
|
|
CommandMenu("Display") {
|
|
Picker("Presentation", selection: $presentation.mode) {
|
|
Text("Conventional Display").tag(PresentationSettings.Mode.display)
|
|
Text("Interactive System").tag(PresentationSettings.Mode.system)
|
|
}.pickerStyle(.inline)
|
|
Button(presentation.expanded ? "Return to System" : "Expand Monitor") { presentation.expanded.toggle() }
|
|
.keyboardShortcut("e", modifiers: [.command, .shift])
|
|
.disabled(presentation.mode != .system)
|
|
Divider()
|
|
Picker("Video Output", selection: Binding(get: { menu.values.videoOutput }, set: machine.setVideoOutput)) {
|
|
ForEach(DisplayOutput.allCases, id: \.rawValue) { output in
|
|
Text(output.label).tag(output)
|
|
}
|
|
}.pickerStyle(.inline).disabled(menu.values.busy || menu.values.state == .closed)
|
|
Divider()
|
|
DisplayAppearanceControls(preferences: machine.displayPreferences)
|
|
Button("Display Settings…") { openWindow(id: "display-settings") }
|
|
Button("Save CoCo Screen…", action: machine.saveScreenshot)
|
|
.keyboardShortcut("s", modifiers: [.command, .shift])
|
|
.disabled(menu.values.busy || !(menu.values.state == .running || menu.values.state == .paused))
|
|
Menu("Quick CoCo Capture") {
|
|
Button("Capture CoCo Screen", action: machine.captures.capture)
|
|
CoCoCaptureOptions(preferences: machine.captures.preferences)
|
|
Button("Cancel Timed Capture", action: machine.captures.cancelCountdown).disabled(!captureMenu.countdown)
|
|
}.disabled(!(menu.values.state == .running || menu.values.state == .paused))
|
|
Button(captureMenu.state == .recording ? "Stop CoCo Video Recording" : "Record CoCo Video") {
|
|
if machine.captures.recording { Task { await machine.captures.stopRecording() } }
|
|
else { machine.captures.startRecording() }
|
|
}.disabled(captureMenu.state == .starting || captureMenu.state == .finishing || (captureMenu.state != .recording && !(menu.values.state == .running || menu.values.state == .paused)))
|
|
Button("Capture Settings…") { openWindow(id: "machine"); machine.captures.settingsShown = true }
|
|
}
|
|
CommandGroup(after: .pasteboard) {
|
|
Divider()
|
|
Button("Copy CoCo Screen Text") { machine.copyScreenText() }
|
|
.keyboardShortcut("c", modifiers: [.command, .shift])
|
|
.disabled(menu.values.busy || !(menu.values.state == .running || menu.values.state == .paused))
|
|
Button("Paste Text into CoCo") { machine.pasteClipboard() }
|
|
.keyboardShortcut("v", modifiers: [.command, .shift])
|
|
.disabled(!menu.values.canPaste)
|
|
Button("Paste BASIC — Merge…") { machine.pasteClipboard(.mergeBASIC) }.disabled(!menu.values.canPaste)
|
|
Button("Paste BASIC — Replace…") { machine.pasteClipboard(.newBASIC) }.disabled(!menu.values.canPaste)
|
|
Button("Cancel Paste") { machine.cancelPaste() }.disabled(!menu.values.pasting)
|
|
}
|
|
CommandMenu("Input") {
|
|
Button("Focus CoCo Display") {
|
|
openWindow(id: "machine")
|
|
DispatchQueue.main.async { machine.focusDisplay() }
|
|
}.keyboardShortcut("k", modifiers: [.command, .option])
|
|
Button("Focus Mac Controls") {
|
|
openWindow(id: "machine")
|
|
DispatchQueue.main.async { presentation.focusControls() }
|
|
}.keyboardShortcut("u", modifiers: [.command, .option])
|
|
Divider()
|
|
Button("Keyboard and Joysticks…") { openWindow(id: "input") }
|
|
}
|
|
CommandMenu("Cartridge") {
|
|
Button("Cartridges…") { openWindow(id: "cartridges") }
|
|
if menu.values.hardDiskConnected {
|
|
Button("Hard Drives…") { openWindow(id: "hard-disks") }
|
|
}
|
|
if menu.values.diskConnected {
|
|
Button("Floppy Drives…") { openWindow(id: "disks") }
|
|
}
|
|
}
|
|
CommandMenu("Media") {
|
|
Button("Software Library…") { openWindow(id: "software-library") }
|
|
.keyboardShortcut("l", modifiers: [.command, .shift])
|
|
Divider()
|
|
Button("Printer Output…") { openWindow(id: "printer") }
|
|
Divider()
|
|
Button("Cassette…") { openWindow(id: "cassette") }.keyboardShortcut("t", modifiers: [.command, .shift])
|
|
Button("Virtual Hard Disks…") { openWindow(id: "hard-disks") }
|
|
Button("Floppy Disks…") { openWindow(id: "disks") }.keyboardShortcut("d", modifiers: [.command, .shift])
|
|
}
|
|
CommandGroup(replacing: .newItem) {
|
|
Button("Show Emulator") {
|
|
openWindow(id: "machine")
|
|
DispatchQueue.main.async { machine.focusDisplay() }
|
|
}.keyboardShortcut("1", modifiers: [.command])
|
|
Divider()
|
|
Button("Software Library…") { openWindow(id: "software-library") }
|
|
Divider()
|
|
Button("Open Media…", action: machine.openMedia).keyboardShortcut("o")
|
|
.disabled(menu.values.busy || menu.values.diskSaving)
|
|
RecentMediaCommands(machine: machine, recent: machine.recentMedia, menu: menu)
|
|
Divider()
|
|
Button("Open ROM…", action: machine.openROM).keyboardShortcut("o", modifiers: [.command, .shift])
|
|
.disabled(menu.values.busy)
|
|
Button("Load Binary…", action: machine.openBinary).keyboardShortcut("b", modifiers: [.command, .shift])
|
|
.disabled(!menu.values.canQuickLoad)
|
|
}
|
|
}
|
|
}
|
|
|
|
private struct ArchiveExampleWindow: View {
|
|
let identifier: String
|
|
let delegate: ApplicationDelegate
|
|
@State private var workspace: BASICDevelopmentModel?
|
|
@State private var message: String?
|
|
|
|
var body: some View {
|
|
Group {
|
|
if let workspace {
|
|
BASICDevelopmentView(model: workspace, reference: try? delegate.basicReference.get())
|
|
.onDisappear { MCPAccess.shared.closing(workspace) }
|
|
} else if let message {
|
|
Text(message).padding()
|
|
} else {
|
|
ProgressView("Opening example…")
|
|
}
|
|
}
|
|
.task(id: identifier) {
|
|
do {
|
|
workspace = try await delegate.archiveWorkspace(identifier)
|
|
if workspace == nil { message = "This example is unavailable in this Archive index." }
|
|
} catch is CancellationError {
|
|
return
|
|
} catch {
|
|
message = error.localizedDescription
|
|
}
|
|
}
|
|
}
|
|
}
|