Files
VCCPlusOSS/platforms/macos/App/GraphicsPreviewView.swift
T

177 lines
9.7 KiB
Swift
Raw 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 GraphicsPreviewView: View {
@ObservedObject var model: GraphicsPreviewModel
let machine: MachineModel
let showMemory: (UInt32, Bool) -> Void
@State private var zoom = 0
@State private var showChanges = true
@State private var pixelX = "0"
@State private var pixelY = "0"
@State private var pointError: String?
var body: some View {
VStack(spacing: 0) {
HStack {
Picker("Preview source", selection: $model.request.source) {
ForEach(GraphicsPreviewModel.Source.allCases, id: \.self) { Text($0.rawValue).tag($0) }
}.pickerStyle(.segmented).labelsHidden().frame(width: 250)
.accessibilityIdentifier("graphics-source")
Spacer()
Picker("Zoom", selection: $zoom) {
Text("Fit").tag(0)
ForEach([1, 2, 3, 4], id: \.self) { Text("\($0)×").tag($0) }
}.frame(width: 120).accessibilityIdentifier("graphics-zoom")
}.padding(.horizontal, 16).padding(.vertical, 10)
if model.request.source == .address { addressControls }
Divider()
if let frame = model.frame {
HStack(spacing: 0) {
viewport(frame)
Divider()
inspector(frame).frame(width: 220)
}
} else {
VStack(spacing: 10) {
if let error = model.error {
Image(systemName: "rectangle.slash").font(.title).foregroundStyle(.secondary)
Text(error).multilineTextAlignment(.center)
} else { ProgressView().controlSize(.small).accessibilityLabel("Reading graphics memory") }
}.padding().frame(maxWidth: .infinity, maxHeight: .infinity)
}
}
.task(id: model.request) { await model.poll(machine: machine) }
.onChange(of: model.selection, initial: true) { _, point in
pixelX = String(point.x); pixelY = String(point.y); pointError = nil
}
}
private var addressControls: some View {
VStack(alignment: .leading, spacing: 4) {
HStack(spacing: 10) {
Picker("Address space", selection: $model.request.physical) {
Text("Mapped CPU").tag(false)
Text("Physical RAM").tag(true)
}.labelsHidden().fixedSize().accessibilityIdentifier("graphics-space")
TextField("Hex address", text: $model.addressText).frame(width: 82)
.textFieldStyle(.roundedBorder).font(.system(.body, design: .monospaced))
.onSubmit { model.applyAddress() }.accessibilityLabel("Start address in hexadecimal")
.accessibilityIdentifier("graphics-address")
Button("Go", action: model.applyAddress).accessibilityIdentifier("graphics-go")
Divider().frame(height: 18)
Picker("Mode", selection: $model.request.mode) {
ForEach(GraphicsPreviewModel.Mode.allCases, id: \.self) { Text($0.label).tag($0) }
}.frame(width: 160).accessibilityIdentifier("graphics-mode")
if model.request.mode.supportsColorSet {
Picker("Colors", selection: $model.request.alternate) {
Text("Set 0").tag(false)
Text("Set 1").tag(true)
}.frame(width: 130).accessibilityIdentifier("graphics-palette")
}
Spacer()
}
if let error = model.addressError {
Text(error).font(.caption).foregroundStyle(.red)
}
}.padding(.horizontal, 16).padding(.bottom, 10)
}
private func viewport(_ frame: GraphicsPreviewModel.Frame) -> some View {
GeometryReader { bounds in
let width = CGFloat(frame.capture.width), height = CGFloat(frame.capture.height)
let availableScale = min(max(1, bounds.size.width - 24) / width, max(1, bounds.size.height - 24) / height)
let fit = availableScale >= 1 ? floor(availableScale) : availableScale
let scale = zoom == 0 ? fit : CGFloat(zoom)
ScrollView([.horizontal, .vertical]) {
ZStack(alignment: .topLeading) {
Image(decorative: frame.image, scale: 1).interpolation(.none).resizable()
if showChanges, let mask = frame.highlights {
Image(decorative: mask, scale: 1).interpolation(.none).resizable()
}
let size = max(7, scale)
Rectangle().strokeBorder(.white, lineWidth: 2)
.overlay(Rectangle().strokeBorder(.black, lineWidth: 1))
.frame(width: size, height: size)
.position(x: (CGFloat(model.selection.x)+0.5)*scale, y: (CGFloat(model.selection.y)+0.5)*scale)
.allowsHitTesting(false)
}
.frame(width: width*scale, height: height*scale).clipped()
.contentShape(Rectangle())
.onTapGesture { point in
model.selection = .init(x: min(Int(width)-1, max(0, Int(point.x / scale))),
y: min(Int(height)-1, max(0, Int(point.y / scale))))
}
.accessibilityElement(children: .ignore)
.accessibilityLabel("Graphics memory image, \(frame.capture.width) by \(frame.capture.height) pixels")
.accessibilityHint("Use the X and Y fields in Pixel Inspector to select a pixel.")
.accessibilityIdentifier("graphics-raster")
.padding(12)
.frame(minWidth: bounds.size.width, minHeight: bounds.size.height)
}.background(Color(nsColor: .underPageBackgroundColor))
}
}
private func inspector(_ frame: GraphicsPreviewModel.Frame) -> some View {
let capture = frame.capture
let width = capture.physical ? 6 : 4
let pixel = capture.pixel(x: model.selection.x, y: model.selection.y)
return ScrollView {
VStack(alignment: .leading, spacing: 14) {
VStack(alignment: .leading, spacing: 5) {
Text(capture.modeName).font(.headline)
Text("\(capture.width) × \(capture.height) pixels")
Text((capture.physical ? "Physical RAM" : "Mapped CPU") + " · $" + DebuggerModel.hex(capture.address, width: width))
.font(.caption).foregroundStyle(.secondary).textSelection(.enabled)
}
Divider()
Label("Pixel Inspector", systemImage: "scope").font(.headline)
HStack(spacing: 6) {
Text("X")
TextField("X", text: $pixelX).accessibilityIdentifier("graphics-x")
Text("Y")
TextField("Y", text: $pixelY).accessibilityIdentifier("graphics-y")
Button { select(capture) } label: { Image(systemName: "return") }
.help("Inspect the pixel at the entered X and Y coordinates").accessibilityLabel("Inspect pixel").accessibilityIdentifier("graphics-inspect")
}.textFieldStyle(.roundedBorder).onSubmit { select(capture) }
if let pointError { Text(pointError).font(.caption).foregroundStyle(.red) }
if let pixel {
Grid(alignment: .leading, horizontalSpacing: 12, verticalSpacing: 7) {
GridRow {
Text("Address")
Text("$" + DebuggerModel.hex(pixel.address, width: width)).accessibilityIdentifier("graphics-byte-address")
}
if pixel.available {
GridRow { Text("Byte"); Text("$" + DebuggerModel.hex(pixel.value, width: 2)) }
GridRow { Text("Bit mask"); Text("$" + DebuggerModel.hex(pixel.mask, width: 2)) }
}
}.monospacedDigit().textSelection(.enabled)
if !pixel.available { Text("This byte is unavailable.").font(.caption).foregroundStyle(.secondary) }
if let address = pixel.attributeAddress, let value = pixel.attributeValue {
Text("Attribute $\(DebuggerModel.hex(value.uint8Value, width: 2)) at $\(DebuggerModel.hex(address.uint32Value, width: width))")
.font(.caption).textSelection(.enabled)
}
Button { showMemory(pixel.address, capture.physical) } label: {
Label("Show Byte in Memory", systemImage: "memorychip")
}.disabled(!pixel.available).accessibilityIdentifier("graphics-reveal")
}
Divider()
Toggle("Highlight step changes", isOn: $showChanges).toggleStyle(.checkbox)
if let changes = frame.changes {
Text("\(changes.formatted()) \(changes == 1 ? "pixel" : "pixels") changed")
.font(.caption).foregroundStyle(.secondary).accessibilityIdentifier("graphics-changes")
}
}.padding(16).frame(maxWidth: .infinity, alignment: .leading)
}
}
private func select(_ capture: VCCDebugGraphicsCapture) {
guard let x = Int(pixelX), let y = Int(pixelY), (0..<capture.width).contains(x), (0..<capture.height).contains(y) else {
pointError = "X ranges from 0 to \(capture.width-1) and Y from 0 to \(capture.height-1)."
return
}
pointError = nil
model.selection = .init(x: x, y: y)
}
}