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

135 lines
6.4 KiB
Swift

// SPDX-License-Identifier: GPL-3.0-or-later
import SwiftUI
// Shared presentation only. Commands and live state remain in the existing
// machine/window models; a disclosure never changes emulation configuration.
struct SecondaryHeader: View {
let title: String
var badge: String? = nil
var symbol: String? = nil
var subtitle: String? = nil
var body: some View {
VStack(alignment: .leading, spacing: 8) {
if let badge {
HardwareBadge(name: badge, label: title)
.frame(maxWidth: 370, alignment: .leading).frame(height: 36)
} else if let symbol {
Label(title, systemImage: symbol).font(.title2.weight(.semibold))
} else {
Text(title).font(.title2.weight(.semibold))
}
if let subtitle {
Text(subtitle).font(.callout).foregroundStyle(.secondary)
.fixedSize(horizontal: false, vertical: true)
}
}.frame(maxWidth: .infinity, alignment: .leading)
}
}
/// The ruler paints the existing markers. A transparent native button on each
/// visible target supplies its action, hover help and VoiceOver semantics.
class BreakpointRuler: NSRulerView {
typealias Row = (number: Int, rect: NSRect)
private var buttons: [NSButton] = []
var points: Set<Int> = [] { didSet { if oldValue != points { invalidateMarkers() } } }
var active = true { didSet { if oldValue != active { invalidateMarkers() } } }
var onClick: (Int) -> Void = { _ in }
override init(scrollView: NSScrollView?, orientation: NSRulerView.Orientation) {
super.init(scrollView: scrollView, orientation: orientation)
if let clip = scrollView?.contentView {
clip.postsBoundsChangedNotifications = true
NotificationCenter.default.addObserver(self, selector: #selector(invalidateMarkers),
name: NSView.boundsDidChangeNotification, object: clip)
}
setAccessibilityElement(true)
setAccessibilityRole(.group)
setAccessibilityLabel("Source breakpoints")
}
required init(coder: NSCoder) { fatalError("init(coder:) is not supported") }
@objc func invalidateMarkers() { needsLayout = true; needsDisplay = true }
func visibleRows() -> [Row] { [] }
override func accessibilityChildren() -> [Any]? { buttons }
static func actionName(_ number: Int, set: Bool) -> String {
"\(set ? "Remove" : "Set") breakpoint on line \(number)"
}
override func layout() {
super.layout()
let rows = visibleRows()
while buttons.count > rows.count { buttons.removeLast().removeFromSuperview() }
for (index, row) in rows.enumerated() {
if index == buttons.count {
let button = NSButton(title: "", target: self, action: #selector(toggleLine(_:)))
button.isTransparent = true
button.refusesFirstResponder = true
addSubview(button)
buttons.append(button)
}
let button = buttons[index]
let name = Self.actionName(row.number, set: points.contains(row.number))
if button.frame != row.rect { button.frame = row.rect }
if button.toolTip != name {
button.toolTip = name
button.setAccessibilityLabel(name)
button.setAccessibilityIdentifier("breakpoint.gutter.line.\(row.number)")
}
button.tag = row.number
button.isEnabled = active
}
}
@objc private func toggleLine(_ sender: NSButton) {
guard active else { return }
onClick(sender.tag)
}
}
/// Line numbers align with the editor's actual laid-out lines, including scroll.
final class CLineRuler: BreakpointRuler {
override var isFlipped: Bool { true }
override init(scrollView: NSScrollView?, orientation: NSRulerView.Orientation) {
super.init(scrollView: scrollView, orientation: orientation)
// Keep the native ruler and its breakpoint buttons from painting over the source.
clipsToBounds = true
ruleThickness = 48
clientView = scrollView?.documentView
}
required init(coder: NSCoder) { fatalError("init(coder:) is not supported") }
override func visibleRows() -> [Row] {
guard let editor = clientView as? NSTextView, let layout = editor.layoutManager,
let container = editor.textContainer else { return [] }
let text = editor.string as NSString
let origin = editor.textContainerOrigin
// Horizontal scrolling must not change which rows appear in the gutter.
let viewport = NSRect(x: 0, y: editor.visibleRect.minY - origin.y,
width: max(1, editor.bounds.width), height: editor.visibleRect.height)
let visible = layout.glyphRange(forBoundingRect: viewport, in: container)
guard visible.length > 0 else { return [] }
var rows: [Row] = []
var start = 0
var number = 1
layout.enumerateLineFragments(forGlyphRange: visible) { fragment, _, _, range, _ in
let character = layout.characterIndexForGlyph(at: range.location)
number += text.substring(with: NSRange(location: start, length: character - start)).filter { $0 == "\n" || $0 == "\r\n" || $0 == "\r" }.count
start = character
let r = self.convert(fragment.offsetBy(dx: origin.x, dy: origin.y), from: editor)
let hit = NSRect(x: 0, y: r.minY, width: self.bounds.width, height: r.height)
if hit.intersects(self.bounds) { rows.append((number, hit)) }
}
return rows
}
override func drawHashMarksAndLabels(in rect: NSRect) {
NSColor.controlBackgroundColor.setFill(); bounds.fill()
let attributes: [NSAttributedString.Key: Any] = [.font: NSFont.monospacedDigitSystemFont(ofSize: 11, weight: .regular), .foregroundColor: NSColor.secondaryLabelColor]
for (number, row) in visibleRows() {
let label = "\(number)" as NSString
if points.contains(number) {
NSColor.controlAccentColor.setFill()
NSBezierPath(ovalIn: NSRect(x: 3, y: row.minY + 3, width: 8, height: 8)).fill()
}
label.draw(at: NSPoint(x: 42 - label.size(withAttributes: attributes).width, y: row.minY + 1), withAttributes: attributes)
}
}
}