// SPDX-License-Identifier: GPL-3.0-or-later import SwiftUI @MainActor final class AssemblyReferenceSelection: ObservableObject { static let shared = AssemblyReferenceSelection() struct Lookup { let token = UUID() let term: String? let target6309: Bool } @Published private(set) var lookup: Lookup? func show(_ term: String?, target6309: Bool) { lookup = .init(term: term, target6309: target6309) } } struct AssemblyReferenceView: View { let openExample: (AssemblyReferenceExampleRequest) -> Void @ObservedObject private var selection = AssemblyReferenceSelection.shared @FocusState private var searchFocused: Bool @State private var query = "" @State private var target6309: Bool? @State private var category: AssemblyReferenceCategory? @State private var selected: String? = AssemblyReference.shared.entry("LDA")?.id @State private var scrollRequest = UUID() @State private var bibliography = false @State private var citationKey = "" private let catalog = AssemblyReference.shared var body: some View { let matches = catalog.search(query, target6309: target6309, category: category) let current = matches.first { $0.id == selected } ?? matches.first VStack(spacing: 0) { HStack { TextField("Search instructions, registers, or tasks", text: $query) .textFieldStyle(.roundedBorder).focused($searchFocused) .accessibilityLabel("Search assembly reference").accessibilityIdentifier("assembly.reference.search") Picker("Processor", selection: $target6309) { Text("Both CPUs").tag(nil as Bool?) Text("MC6809").tag(Optional(false)) Text("HD6309").tag(Optional(true)) }.labelsHidden().accessibilityLabel("Processor").fixedSize().accessibilityIdentifier("assembly.reference.processor") Button { citationKey = ""; bibliography = true } label: { Image(systemName: "books.vertical") } .help("Bibliography").accessibilityLabel("Bibliography") }.padding(10) Divider() HSplitView { VStack(spacing: 0) { Picker("Category", selection: $category) { Text("All Topics").tag(nil as AssemblyReferenceCategory?) ForEach(AssemblyReferenceCategory.allCases, id: \.self) { Text($0.rawValue).tag(Optional($0)) } }.labelsHidden().accessibilityLabel("Category").accessibilityIdentifier("assembly.reference.category").padding(8) ScrollViewReader { proxy in List(matches, selection: $selected) { item in HStack { Text(item.name) if item.only6309 { Text("6309").font(.caption2).foregroundStyle(.secondary) } }.tag(item.id).id(item.id) } .accessibilityLabel("Reference topics").accessibilityIdentifier("assembly.reference.topics") .onChange(of: scrollRequest, initial: true) { _, _ in if let id = current?.id { proxy.scrollTo(id, anchor: .center) } } } }.frame(minWidth: 170, idealWidth: 225, maxWidth: 330) if let item = current { ScrollView { VStack(alignment: .leading, spacing: 14) { Text(item.category.rawValue + " ยท " + (item.only6309 ? "HD6309" : "MC6809 and HD6309")).font(.caption).foregroundStyle(.secondary) Text(item.name).font(.title.bold()) if !item.aliases.isEmpty { Text("Also spelled " + item.aliases.joined(separator: ", ")).foregroundStyle(.secondary) } if !item.syntax.isEmpty { Text(item.syntax).font(.system(.body, design: .monospaced)).textSelection(.enabled) } Text(item.operation).textSelection(.enabled) if !item.flags.isEmpty { Text("Condition codes").font(.headline) Text(item.flags).textSelection(.enabled) } if !item.detail.isEmpty { if item.category == .instructions { DisclosureGroup("Instruction details") { Text(item.detail).textSelection(.enabled).padding(.top, 4) } } else { Text(item.detail).textSelection(.enabled) } } if let id = item.example, let lesson = AssemblyReferenceLesson.all.first(where: { $0.id == id }) { if item.category == .examples { Text(lesson.testedScope).font(.caption).foregroundStyle(.secondary) Text(lesson.source).font(.system(.caption, design: .monospaced)).textSelection(.enabled) } Button("Open Example", systemImage: "square.and.pencil") { openExample(lesson.request) } .help("Open an editable copy of " + lesson.name).accessibilityIdentifier("assembly.reference.openExample") } if !item.links.isEmpty { Text("Related topics").font(.headline) ForEach(item.links.filter { catalog.entry($0).map { target6309 != false || !$0.only6309 } ?? false }, id: \.self) { link in Button(link) { navigate(to: link) }.buttonStyle(.link) } } DisclosureGroup("Sources") { VStack(alignment: .leading, spacing: 6) { ForEach(item.citations, id: \.self) { source in Button(source) { citationKey = AssemblyReference.bibliography.keys.first { source.hasPrefix($0) } ?? "" bibliography = true }.buttonStyle(.link) } }.frame(maxWidth: .infinity, alignment: .leading).padding(.top, 4) } }.padding(20).frame(maxWidth: .infinity, alignment: .leading) }.id(item.id).accessibilityIdentifier("assembly.reference.detail") } else { ContentUnavailableView.search(text: query).frame(maxWidth: .infinity) } } }.frame(minWidth: 610, minHeight: 380) .background { Button("Find in Assembly Reference") { searchFocused = true } .keyboardShortcut("f", modifiers: .command).hidden().accessibilityHidden(true) } .onChange(of: selection.lookup?.token, initial: true) { _, _ in guard let request = selection.lookup else { return } target6309 = request.target6309 if let term = request.term { navigate(to: term) } else { query = ""; category = nil } } .onChange(of: current?.id) { _, id in selected = id } .sheet(isPresented: $bibliography) { VStack(alignment: .leading, spacing: 16) { Text("Bibliography").font(.title.bold()) ScrollViewReader { proxy in ScrollView { VStack(alignment: .leading, spacing: 14) { ForEach(AssemblyReference.bibliography.keys.sorted(), id: \.self) { key in Text(AssemblyReference.bibliography[key] ?? "").textSelection(.enabled).id(key) } Divider() Text("Color Computer Archive preserves historical publications consulted for this reference. The explanations and teaching programs here were written for VCC Plus.").foregroundStyle(.secondary) } }.onAppear { if !citationKey.isEmpty { proxy.scrollTo(citationKey, anchor: .top) } } } HStack { Spacer(); Button("Done") { bibliography = false }.keyboardShortcut(.defaultAction) } }.padding(22).frame(width: 520, height: 405) } } private func navigate(to term: String) { category = nil if let entry = catalog.entry(term) { query = ""; selected = entry.id } else { query = term; selected = nil } scrollRequest = UUID() } }