adds schema view and schema generating command

This commit is contained in:
2026-11-03 09:35:41 +01:00
parent cc41decccb
commit 600d4874d2
62 changed files with 4416 additions and 105 deletions
@@ -0,0 +1,282 @@
import AppKit
import HelderCore
import SwiftUI
/// The diagram: relationship lines under the cards, panned and zoomed by the store's viewport.
/// Each card is laid out at 100% and scaled into its on-screen frame, so its click and pointer area stay unscaled.
struct SchemaCanvas: View {
let model: WorkspaceModel
let store: SchemaStore
let schema: SchemaModel
/// Where a background drag started; nil between drags.
@State private var pan: (start: SchemaViewport, moved: Bool)?
/// The card being pressed and where it stood.
@State private var press: (name: String, origin: SchemaPoint, moved: Bool)?
private static let space = "schemaCanvas"
/// A press that moves less than this is a click.
private static let dragThreshold = 4.0
var body: some View {
let highlight = store.highlight ?? SchemaHighlight(model: schema, hovered: nil, selected: nil, hoveredColumn: nil, query: "")
let view = store.viewport
GeometryReader { proxy in
ZStack(alignment: .topLeading) {
Palette.editor
.contentShape(Rectangle())
.gesture(backgroundDrag)
SchemaEdges(schema: schema, positions: store.positions, viewport: view, highlight: highlight)
.allowsHitTesting(false)
ForEach(schema.tables, id: \.name) { table in
if let position = store.positions[table.name] {
card(table, at: position, view: view, highlight: highlight)
}
}
Text("drag to pan · ⌘ scroll to zoom · hover a table or a key to trace it · click to inspect")
.typeRole(Typography.meta)
.foregroundStyle(Palette.text4)
.lineLimit(1)
.allowsHitTesting(false)
.padding(SchemaViewMetrics.canvasInset)
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .bottomLeading)
SchemaZoomControl(store: store)
.padding(SchemaViewMetrics.canvasInset)
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .bottomTrailing)
}
.coordinateSpace(name: Self.space)
.clipped()
.background(SchemaScrollCatcher(
isActive: !model.overlay.isPresented,
onPan: { dx, dy in store.pan(dx: dx, dy: dy) },
onZoom: { factor, point in store.zoom(by: factor, around: SchemaPoint(x: point.x, y: point.y)) }
))
.onAppear { store.setCanvasSize(SchemaSize(width: proxy.size.width, height: proxy.size.height)) }
.onChange(of: proxy.size) { _, size in store.setCanvasSize(SchemaSize(width: size.width, height: size.height)) }
}
}
private func card(_ table: SchemaTable, at position: SchemaPoint, view: SchemaViewport, highlight: SchemaHighlight) -> some View {
let k = view.scale
let width = SchemaMetrics.cardWidth * k
let height = table.cardHeight * k
return SchemaTableCard(table: table, highlight: highlight, isSelected: store.selected == table.name) { id, inside in
if inside {
store.hoverColumn(id)
} else if store.hoveredColumn == id {
store.hoverColumn(nil)
}
}
.scaleEffect(k, anchor: .topLeading)
.frame(width: width, height: height, alignment: .topLeading)
.contentShape(Rectangle())
.pointerHand()
.onHover { inside in
if inside {
store.hover(table.name)
} else if store.hovered == table.name {
store.hover(nil)
}
}
.gesture(cardDrag(table.name, origin: position, scale: k))
.position(x: position.x * k + view.x + width / 2, y: position.y * k + view.y + height / 2)
}
/// Drags the card past a 4pt threshold; a shorter press selects it, or deselects it when it already was.
private func cardDrag(_ name: String, origin: SchemaPoint, scale: Double) -> some Gesture {
DragGesture(minimumDistance: 0, coordinateSpace: .named(Self.space))
.onChanged { value in
var current = press.flatMap { $0.name == name ? $0 : nil } ?? (name: name, origin: origin, moved: false)
let dx = value.translation.width, dy = value.translation.height
if !current.moved && abs(dx) + abs(dy) < Self.dragThreshold {
press = current
return
}
current.moved = true
press = current
store.moveTable(name, to: SchemaPoint(x: (current.origin.x + dx / scale).rounded(),
y: (current.origin.y + dy / scale).rounded()))
}
.onEnded { _ in
let moved = press?.moved ?? false
press = nil
if moved { store.endDrag() } else { store.toggleSelection(name) }
}
}
/// Pans past a 4pt threshold; a click on the empty canvas deselects.
private var backgroundDrag: some Gesture {
DragGesture(minimumDistance: 0, coordinateSpace: .named(Self.space))
.onChanged { value in
var current = pan ?? (start: store.viewport, moved: false)
let dx = value.translation.width, dy = value.translation.height
if !current.moved && abs(dx) + abs(dy) < Self.dragThreshold {
pan = current
return
}
current.moved = true
pan = current
var next = current.start
next.x += dx
next.y += dy
store.setViewport(next)
}
.onEnded { _ in
let moved = pan?.moved ?? false
pan = nil
if !moved { store.clearSelection() }
}
}
}
/// One cubic line per foreign key, hollow at the key end and filled at the referenced end; active lines on top.
private struct SchemaEdges: View {
let schema: SchemaModel
let positions: [String: SchemaPoint]
let viewport: SchemaViewport
let highlight: SchemaHighlight
var body: some View {
Canvas { context, _ in
let k = viewport.scale
func screen(_ p: SchemaPoint) -> CGPoint { CGPoint(x: p.x * k + viewport.x, y: p.y * k + viewport.y) }
for edge in highlight.drawingOrder(schema.edges) {
guard let geometry = SchemaGeometry.edgePath(edge, positions: positions) else { continue }
let active = highlight.isActive(edge)
let color = active ? Palette.amber : highlight.linesRecede ? Palette.raised : Palette.text5
let start = screen(geometry.start), end = screen(geometry.end)
var line = Path()
line.move(to: start)
line.addCurve(to: end, control1: screen(geometry.control1), control2: screen(geometry.control2))
context.stroke(line, with: .color(color), lineWidth: (active ? 1.75 : 1.25) * k)
let r = SchemaMetrics.endRadius * k
let hollow = Path(ellipseIn: CGRect(x: start.x - r, y: start.y - r, width: r * 2, height: r * 2))
context.fill(hollow, with: .color(Palette.editor))
context.stroke(hollow, with: .color(color), lineWidth: 1.5 * k)
context.fill(Path(ellipseIn: CGRect(x: end.x - r, y: end.y - r, width: r * 2, height: r * 2)), with: .color(color))
}
}
}
}
/// `−` · zoom % · `+` · Fit, bottom right of the canvas.
private struct SchemaZoomControl: View {
let store: SchemaStore
var body: some View {
HStack(spacing: 0) {
ZoomButton(tip: "Zoom out") { store.zoom(by: 1 / SchemaMetrics.zoomStep) } label: {
GitIconView(icon: .minus, size: 14).frame(width: SchemaViewMetrics.zoomButton)
}
Text(store.viewport.percentLabel)
.typeRole(Typography.schemaZoom)
.foregroundStyle(Palette.textMuted)
.frame(width: SchemaViewMetrics.zoomLabel, height: SchemaViewMetrics.zoomControl)
.hairline(.leading)
.hairline(.trailing)
ZoomButton(tip: "Zoom in") { store.zoom(by: SchemaMetrics.zoomStep) } label: {
GitIconView(icon: .plus, size: 14).frame(width: SchemaViewMetrics.zoomButton)
}
ZoomButton(tip: "Fit the whole schema") { store.fit() } label: {
Text("Fit").typeRole(Typography.button).padding(.horizontal, 11)
}
.hairline(.leading)
}
.fixedSize()
.background(Palette.panel)
.clipShape(RoundedRectangle(cornerRadius: Metrics.radiusSmall))
.overlay(RoundedRectangle(cornerRadius: Metrics.radiusSmall).strokeBorder(Palette.raised, lineWidth: Metrics.hairline))
}
}
private struct ZoomButton<Label: View>: View {
let tip: String
let action: () -> Void
@ViewBuilder let label: Label
@State private var isHovered = false
var body: some View {
Button(action: action) {
label
.foregroundStyle(isHovered ? Palette.text1 : Palette.text3)
.frame(height: SchemaViewMetrics.zoomControl)
.background(isHovered ? Palette.raised : .clear)
.contentShape(Rectangle())
}
.buttonStyle(.plain)
.pointerHand()
.tracksHover($isHovered)
.tip(tip, edge: .trailing)
}
}
/// Turns scroll and pinch over the canvas into pan and zoom: plain scroll pans, ⌘ or Ctrl scroll and pinch zoom
/// around the pointer. A local monitor, because the cards on top would otherwise receive the events.
private struct SchemaScrollCatcher: NSViewRepresentable {
let isActive: Bool
let onPan: (_ dx: Double, _ dy: Double) -> Void
let onZoom: (_ factor: Double, _ at: CGPoint) -> Void
func makeNSView(context: Context) -> CatcherView {
CatcherView()
}
func updateNSView(_ view: CatcherView, context: Context) {
view.isActive = isActive
view.onPan = onPan
view.onZoom = onZoom
}
static func dismantleNSView(_ view: CatcherView, coordinator: ()) {
view.removeMonitor()
}
final class CatcherView: NSView {
var isActive = true
var onPan: (Double, Double) -> Void = { _, _ in }
var onZoom: (Double, CGPoint) -> Void = { _, _ in }
private var monitor: Any?
override var isFlipped: Bool { true }
override func hitTest(_ point: NSPoint) -> NSView? { nil }
override func viewDidMoveToWindow() {
super.viewDidMoveToWindow()
if window == nil {
removeMonitor()
} else if monitor == nil {
monitor = NSEvent.addLocalMonitorForEvents(matching: [.scrollWheel, .magnify]) { [weak self] event in
MainActor.assumeIsolated { self?.handle(event) ?? false } ? nil : event
}
}
}
func removeMonitor() {
if let monitor { NSEvent.removeMonitor(monitor) }
monitor = nil
}
/// True when the event was over the canvas and moved it.
private func handle(_ event: NSEvent) -> Bool {
guard isActive, let window, event.window === window else { return false }
let point = convert(event.locationInWindow, from: nil)
guard bounds.contains(point) else { return false }
switch event.type {
case .magnify:
onZoom(1 + event.magnification, point)
case .scrollWheel:
// A mouse wheel reports lines, a trackpad points; both feel alike after this.
let unit = event.hasPreciseScrollingDeltas ? 1.0 : 10.0
let dx = event.scrollingDeltaX * unit, dy = event.scrollingDeltaY * unit
if event.modifierFlags.contains(.command) || event.modifierFlags.contains(.control) {
onZoom(exp(dy * 0.01), point)
} else {
onPan(dx, dy)
}
default:
return false
}
return true
}
}
}
@@ -0,0 +1,27 @@
import HelderCore
import SwiftUI
/// `[ console ] artisan`: the last runs of `helder:schema`, following the newest line; the chevron hides it until the next run.
struct SchemaConsolePanel: View {
let store: SchemaStore
var body: some View {
VStack(spacing: 0) {
HStack(spacing: 12) {
Text("[ console ] artisan").typeRole(Typography.panelLabel).foregroundStyle(Palette.text4).fixedSize()
Spacer(minLength: 0)
SchemaIconButton(icon: .chevronDown, tip: "Hide console") { store.hideConsole() }
}
.padding(.leading, 16)
.padding(.trailing, 12)
.frame(height: SchemaViewMetrics.consoleHeader)
ConsoleLogView(entries: store.lines.map { ConsoleLogLine(id: $0.id, text: $0.line.text, color: Palette.artisan($0.line.kind)) })
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .top)
.padding(.horizontal, 16)
.padding(.bottom, 12)
}
.frame(height: SchemaViewMetrics.console)
.background(Palette.panel)
.hairline(.top)
}
}
@@ -0,0 +1,152 @@
import HelderCore
import SwiftUI
/// The 320pt panel for the selected table: its columns, what it belongs to and has many, and the footer actions.
struct SchemaDetailPanel: View {
let model: WorkspaceModel
let store: SchemaStore
let schema: SchemaModel
let table: SchemaTable
var body: some View {
let outgoing = schema.outgoing(from: table.name)
let incoming = schema.incoming(to: table.name)
VStack(spacing: 0) {
header
ScrollView {
VStack(alignment: .leading, spacing: 20) {
columns
if !outgoing.isEmpty {
relations("[ belongs to ]", arrow: "→", rows: outgoing.map { ($0.id, $0.to, $0.column) })
}
if !incoming.isEmpty {
relations("[ has many ]", arrow: "←", rows: incoming.map { ($0.id, $0.from, $0.column) })
}
if outgoing.isEmpty && incoming.isEmpty {
Text("— No foreign keys in or out.")
.typeRole(Typography.gitEmpty)
.foregroundStyle(Palette.textMuted)
.padding(.horizontal, 16)
}
}
.padding(.top, 14)
.padding(.bottom, 20)
.frame(maxWidth: .infinity, alignment: .leading)
}
HStack(spacing: 8) {
SchemaOutlineButton(label: "Pass on to Claude", fills: true, padding: 8) { model.perform(.passSchemaTable(table.name)) }
SchemaOutlineButton(label: "Copy name", fills: true, padding: 8) { model.perform(.copySchemaTableName(table.name)) }
}
.padding(.horizontal, 16)
.padding(.vertical, 12)
.hairline(.top)
}
.background(Palette.panel)
.hairline(.leading)
}
private var header: some View {
HStack(spacing: 10) {
Text(table.name)
.typeRole(Typography.schemaDetailName)
.foregroundStyle(Palette.text1)
.lineLimit(1)
.truncationMode(.tail)
.frame(maxWidth: .infinity, alignment: .leading)
if table.rows != nil {
Text(table.rowsLabel + " rows").typeRole(Typography.meta).foregroundStyle(Palette.textMuted).fixedSize()
}
SchemaIconButton(icon: .x, tip: "Close (Esc)") { store.clearSelection() }
}
.padding(.leading, 16)
.padding(.trailing, 10)
.frame(height: SchemaViewMetrics.detailHeader)
.hairline(.bottom)
}
private var columns: some View {
VStack(alignment: .leading, spacing: 0) {
sectionLabel("[ columns ] \(table.columns.count)")
ForEach(Array(table.columns.enumerated()), id: \.offset) { _, column in
VStack(alignment: .leading, spacing: 2) {
HStack(alignment: .firstTextBaseline, spacing: 10) {
Text(column.name)
.typeRole(Typography.schemaDetailColumn)
.foregroundStyle(Palette.text2)
.lineLimit(1)
.truncationMode(.tail)
.frame(maxWidth: .infinity, alignment: .leading)
Text(column.type).typeRole(Typography.schemaDetailMeta).foregroundStyle(Palette.textMuted).lineLimit(1).fixedSize()
}
Text(column.flags)
.typeRole(Typography.schemaDetailMeta)
.foregroundStyle(column.isPrimary || column.isForeign ? Palette.amberSoft : Palette.text4)
}
.padding(.horizontal, 16)
.padding(.vertical, 7)
.hairline(.top)
}
}
}
private func relations(_ label: String, arrow: String, rows: [(id: String, table: String, via: String)]) -> some View {
VStack(alignment: .leading, spacing: 0) {
sectionLabel(label)
ForEach(rows, id: \.id) { row in
SchemaRelationRow(arrow: arrow, table: row.table, via: row.via,
onHover: { inside in hover(row.table, inside) },
onClick: { store.centre(row.table) })
}
}
}
private func sectionLabel(_ text: String) -> some View {
Text(text)
.typeRole(Typography.panelLabel)
.foregroundStyle(Palette.text4)
.padding(.horizontal, 16)
.padding(.bottom, 8)
}
private func hover(_ name: String, _ inside: Bool) {
if inside {
store.hover(name)
} else if store.hovered == name {
store.hover(nil)
}
}
}
/// `→ users … via user_id`: hover traces the table on the canvas, a click selects and centres it.
private struct SchemaRelationRow: View {
let arrow: String
let table: String
let via: String
let onHover: (Bool) -> Void
let onClick: () -> Void
@State private var isHovered = false
var body: some View {
Button(action: onClick) {
HStack(spacing: 8) {
Text(arrow).foregroundStyle(Palette.amber)
Text(table).foregroundStyle(Palette.text1).lineLimit(1)
Spacer(minLength: 0)
Text("via " + via).foregroundStyle(Palette.textMuted).lineLimit(1)
}
.typeRole(Typography.path)
.padding(.horizontal, 16)
.padding(.vertical, 8)
.background(isHovered ? Palette.gitRowHover : .clear)
.hairline(.top)
.contentShape(Rectangle())
}
.buttonStyle(.plain)
.pointerHand()
.onHover { inside in
isHovered = inside
onHover(inside)
}
.animation(Metrics.colorTransition, value: isHovered)
}
}
@@ -0,0 +1,81 @@
import HelderCore
import SwiftUI
/// The command exists but has not written `database/helder/schema.json` yet.
struct SchemaEmptyState: View {
let model: WorkspaceModel
let store: SchemaStore
var body: some View {
SchemaStateBlock(label: "[ no dump yet ]",
message: Text("— Helder draws the schema from ") + schemaMono(SchemaPaths.dumpFile)
+ Text(". Run the command to write it.")) {
HStack(spacing: 14) {
SchemaPrimaryButton(label: store.runLabel) { model.perform(.runSchemaDump) }
Text("$ " + SchemaPaths.commandLine).typeRole(Typography.path).foregroundStyle(Palette.text4).lineLimit(1)
}
}
}
}
/// The project is a Laravel app without `HelderSchemaCommand.php`: the prompt that asks Claude to write it.
struct SchemaCommandMissing: View {
let model: WorkspaceModel
var body: some View {
SchemaStateBlock(label: "[ no command yet ]",
message: Text("— Helder draws the schema from ") + schemaMono(SchemaPaths.dumpFile)
+ Text(". The Artisan command ") + schemaMono(SchemaPaths.commandFile)
+ Text(" writes it, and this app does not have it yet.")) {
HStack(spacing: 14) {
SchemaPrimaryButton(label: "Generate command", icon: .sparkle) { model.perform(.generateSchemaCommand) }
SchemaOutlineButton(label: "Copy prompt", height: SchemaViewMetrics.primaryButton) { model.perform(.copySchemaPrompt) }
}
ScrollView {
Text(SchemaCommandPrompt.text)
.typeRole(Typography.console)
.foregroundStyle(Palette.textMuted)
.textSelection(.enabled)
.frame(maxWidth: .infinity, alignment: .leading)
.padding(12)
.background(PointerTextRegion())
}
.frame(maxHeight: SchemaViewMetrics.promptPreview)
.background(Palette.panel, in: RoundedRectangle(cornerRadius: Metrics.radiusSmall))
.overlay(RoundedRectangle(cornerRadius: Metrics.radiusSmall).strokeBorder(Palette.raised, lineWidth: Metrics.hairline))
}
}
}
/// A path inside the state copy: mono at the copy's size, in text-1.
private func schemaMono(_ text: String) -> Text {
Text(text).font(Typography.schemaBodyMono.font).foregroundColor(Palette.text1)
}
/// The centred block both states share: an amber label, Sans 15/1.6 copy, then the actions; at most 440 wide, 16 apart.
private struct SchemaStateBlock<Content: View>: View {
let label: String
let message: Text
@ViewBuilder let content: Content
init(label: String, message: Text, @ViewBuilder content: () -> Content) {
self.label = label
self.message = message
self.content = content()
}
var body: some View {
VStack(alignment: .leading, spacing: 16) {
Text(label).typeRole(Typography.gitLabel).foregroundStyle(Palette.amber)
message
.typeRole(Typography.schemaBody)
.foregroundStyle(Palette.text3)
.fixedSize(horizontal: false, vertical: true)
content
}
.frame(maxWidth: SchemaViewMetrics.emptyStateWidth, alignment: .leading)
.padding(24)
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(Palette.editor)
}
}
@@ -0,0 +1,75 @@
import HelderCore
import SwiftUI
/// One table on the canvas at 100%: a 33pt header, then a 22pt row per column. The line geometry depends on these sizes.
struct SchemaTableCard: View {
let table: SchemaTable
let highlight: SchemaHighlight
let isSelected: Bool
let onHoverColumn: (_ id: String, _ inside: Bool) -> Void
var body: some View {
let isFocus = highlight.focus == table.name
let lit = isFocus || isSelected
VStack(spacing: 0) {
HStack(spacing: 8) {
Text(table.name)
.typeRole(Typography.schemaCardName)
.foregroundStyle(lit ? Palette.text1 : Palette.text2)
.lineLimit(1)
.truncationMode(.tail)
.frame(maxWidth: .infinity, alignment: .leading)
Text(table.rowsLabel).typeRole(Typography.meta).foregroundStyle(Palette.text4).lineLimit(1).fixedSize()
}
.padding(.horizontal, 10)
.frame(height: SchemaMetrics.headerHeight - Metrics.hairline)
.background(lit ? Palette.amberTint : Palette.panel)
Hairline()
ForEach(Array(table.columns.enumerated()), id: \.offset) { _, column in
row(column)
}
}
.padding(Metrics.hairline)
.frame(width: SchemaMetrics.cardWidth, height: table.cardHeight, alignment: .top)
.background(Palette.panel, in: RoundedRectangle(cornerRadius: Metrics.radiusMedium))
.clipShape(RoundedRectangle(cornerRadius: Metrics.radiusMedium))
.overlay(RoundedRectangle(cornerRadius: Metrics.radiusMedium).strokeBorder(border(isFocus: isFocus), lineWidth: Metrics.hairline))
.opacity(highlight.opacity(of: table.name))
.animation(Metrics.colorTransition, value: highlight)
.animation(Metrics.colorTransition, value: isSelected)
}
private func row(_ column: SchemaColumn) -> some View {
let lit = highlight.isHighlighted(table: table.name, column: column.name)
let isKey = column.isPrimary || column.isForeign
return HStack(spacing: 6) {
Text(column.keyLabel)
.typeRole(Typography.schemaKey)
.foregroundStyle(lit ? Palette.amber : column.isPrimary ? Palette.amberSoft : Palette.textMuted)
.frame(width: 20, alignment: .leading)
Text(column.name)
.typeRole(Typography.path)
.foregroundStyle(lit ? Palette.text1 : isKey ? Palette.text2 : Palette.text3)
.lineLimit(1)
.truncationMode(.tail)
.frame(maxWidth: .infinity, alignment: .leading)
Text(column.displayType).typeRole(Typography.meta).foregroundStyle(Palette.text4).lineLimit(1).fixedSize()
}
.padding(.horizontal, 10)
.frame(height: SchemaMetrics.rowHeight)
.background(lit ? Palette.amberTint : .clear)
.onHover { inside in
guard column.isForeign else { return }
onHoverColumn(SchemaColumnRef(table: table.name, column: column.name).id, inside)
}
}
/// Focus amber, a selection under another hover amber-deep, related text-4, a search match amber-soft.
private func border(isFocus: Bool) -> Color {
if isFocus { return Palette.amber }
if isSelected { return Palette.amberDeep }
if highlight.relatedTables.contains(table.name) { return Palette.text4 }
if highlight.searchMatches.contains(table.name) { return Palette.amberSoft }
return Palette.raised
}
}
@@ -0,0 +1,104 @@
import HelderCore
import SwiftUI
/// The 240pt list left of the canvas: the table search, `[ tables ] N`, and one row per matching table.
struct SchemaTableList: View {
let store: SchemaStore
let schema: SchemaModel
var body: some View {
let highlight = store.highlight
VStack(alignment: .leading, spacing: 0) {
HStack(spacing: 8) {
GitIconView(icon: .search, size: 14).foregroundStyle(Palette.textMuted)
ConsoleInputField(
text: Binding(get: { store.query }, set: { store.query = $0 }),
placeholder: "Find a table",
role: Typography.gitPath,
makeEditor: { SchemaSearchFieldEditor() },
onSubmit: { store.centreFirstMatch() }
)
.frame(maxWidth: .infinity)
.frame(height: 20)
}
.padding(.leading, 14)
.padding(.trailing, 12)
.frame(height: SchemaViewMetrics.searchRow)
.hairline(.bottom)
Text(SchemaSearch.listLabel(schema, query: store.query))
.typeRole(Typography.panelLabel)
.foregroundStyle(Palette.text4)
.lineLimit(1)
.padding(.horizontal, 14)
.frame(height: SchemaViewMetrics.listLabelRow)
ScrollView {
LazyVStack(spacing: 0) {
ForEach(store.listedTables, id: \.name) { table in
SchemaTableRow(table: table,
isSelected: store.selected == table.name,
isFocus: highlight?.focus == table.name,
isRelated: highlight.map { $0.linesRecede && $0.relatedTables.contains(table.name) } ?? false,
onHover: { inside in hover(table.name, inside) },
onClick: { store.centre(table.name) })
}
}
.padding(.bottom, 12)
}
}
.background(Palette.panel)
.hairline(.trailing)
}
private func hover(_ name: String, _ inside: Bool) {
if inside {
store.hover(name)
} else if store.hovered == name {
store.hover(nil)
}
}
}
/// One table: its name and row count, the amber bar while selected.
private struct SchemaTableRow: View {
let table: SchemaTable
let isSelected: Bool
let isFocus: Bool
let isRelated: Bool
let onHover: (Bool) -> Void
let onClick: () -> Void
@State private var isHovered = false
var body: some View {
Button(action: onClick) {
HStack(spacing: 8) {
Text(table.name)
.typeRole(Typography.path)
.foregroundStyle(isSelected || isFocus || isHovered ? Palette.text1 : isRelated ? Palette.text2 : Palette.text3)
.lineLimit(1)
.truncationMode(.tail)
.frame(maxWidth: .infinity, alignment: .leading)
Text(table.rowsLabel).typeRole(Typography.meta).foregroundStyle(Palette.text4).lineLimit(1).fixedSize()
}
.padding(.leading, 11)
.padding(.trailing, 14)
.frame(height: SchemaViewMetrics.listRow)
.background(background)
.overlay(alignment: .leading) {
Rectangle().fill(isSelected ? Palette.amber : .clear).frame(width: SchemaViewMetrics.accentBar)
}
.contentShape(Rectangle())
}
.buttonStyle(.plain)
.pointerHand()
.onHover { inside in
isHovered = inside
onHover(inside)
}
.animation(Metrics.colorTransition, value: isHovered)
}
private var background: Color {
if isSelected { return Palette.amberTint }
return isFocus || isHovered ? Palette.gitRowHover : .clear
}
}
@@ -0,0 +1,63 @@
import HelderCore
import SwiftUI
/// `[ schema ]`, the connection and the dump's meta, then the command path, Run and Close.
struct SchemaTopBar: View {
let model: WorkspaceModel
let store: SchemaStore
var body: some View {
HStack(spacing: 14) {
Text("[ schema ]").typeRole(Typography.gitLabel).foregroundStyle(Palette.textMuted).fixedSize()
if let schema = store.model {
HStack(spacing: 6) {
GitIconView(icon: .database, size: 13)
Text(schema.connectionLabel).typeRole(Typography.branch).lineLimit(1)
}
.foregroundStyle(Palette.amber)
.fixedSize()
}
Text(store.metaLabel)
.typeRole(Typography.path)
.foregroundStyle(Palette.textMuted)
.lineLimit(1)
.truncationMode(.tail)
Spacer(minLength: 0)
if store.hasCommand {
CommandPathButton { model.perform(.openSchemaCommandFile) }
}
SchemaPrimaryButton(label: store.runLabel, icon: .terminal, height: SchemaViewMetrics.topButton,
role: Typography.commitButton, padding: 12) {
model.perform(.runSchemaDump)
}
.disabledLook(!store.hasCommand)
GitBarButton(icon: .minimize, label: "Close", trailing: "⌘D") { model.perform(.closeSchemaView) }
}
.padding(.horizontal, 14)
.frame(height: SchemaViewMetrics.topBar)
.background(Palette.panel)
.hairline(.bottom)
}
}
/// `app/Console/Commands/HelderSchemaCommand.php` as a mono text button.
private struct CommandPathButton: View {
let action: () -> Void
@State private var isHovered = false
var body: some View {
Button(action: action) {
Text(SchemaPaths.commandFile)
.typeRole(Typography.path)
.foregroundStyle(isHovered ? Palette.text1 : Palette.textMuted)
.lineLimit(1)
.padding(.vertical, 4)
.contentShape(Rectangle())
}
.buttonStyle(.plain)
.fixedSize()
.pointerHand()
.tracksHover($isHovered)
.tip("Open the command in the editor", edge: .trailing)
}
}
@@ -0,0 +1,147 @@
import HelderCore
import SwiftUI
/// ⌘D: the full-screen Schema view over the title bar, the columns and the status bar.
struct SchemaView: View {
let model: WorkspaceModel
let store: SchemaStore
var body: some View {
VStack(spacing: 0) {
SchemaTopBar(model: model, store: store)
HStack(spacing: 0) {
switch store.body {
case .diagram:
if let schema = store.model {
SchemaTableList(store: store, schema: schema)
.frame(width: SchemaViewMetrics.tableList)
SchemaCanvas(model: model, store: store, schema: schema)
if let table = store.selectedTable {
SchemaDetailPanel(model: model, store: store, schema: schema, table: table)
.frame(width: SchemaViewMetrics.detailPanel)
}
}
case .noDump:
SchemaEmptyState(model: model, store: store)
case .commandMissing:
SchemaCommandMissing(model: model)
case .loading:
Color.clear
}
}
.frame(maxHeight: .infinity)
if store.isConsoleOpen && !store.lines.isEmpty && store.body != .commandMissing {
SchemaConsolePanel(store: store)
}
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(Palette.editor)
.contentShape(Rectangle())
}
}
/// The Schema view's fixed sizes, from the design handoff.
enum SchemaViewMetrics {
static let topBar: CGFloat = 44
static let topButton: CGFloat = 28
static let tableList: CGFloat = 240
static let searchRow: CGFloat = 44
static let listLabelRow: CGFloat = 34
static let listRow: CGFloat = 30
static let accentBar: CGFloat = 3
static let detailPanel: CGFloat = 320
static let detailHeader: CGFloat = 44
static let footerButton: CGFloat = 30
static let console: CGFloat = 150
static let consoleHeader: CGFloat = 32
static let primaryButton: CGFloat = 36
static let emptyStateWidth: CGFloat = 440
static let promptPreview: CGFloat = 240
static let zoomControl: CGFloat = 28
static let zoomButton: CGFloat = 30
static let zoomLabel: CGFloat = 52
static let canvasInset: CGFloat = 14
}
/// The amber primary of the empty and command-missing states: 36pt, Sans 600 13, amber-deep on hover.
struct SchemaPrimaryButton: View {
let label: String
var icon: GitIcon?
var height: CGFloat = SchemaViewMetrics.primaryButton
var role = Typography.gitCommitButton
var padding: CGFloat = 16
let action: () -> Void
@State private var isHovered = false
var body: some View {
Button(action: action) {
HStack(spacing: 7) {
if let icon { GitIconView(icon: icon, size: 14) }
Text(label).typeRole(role).lineLimit(1)
}
.foregroundStyle(Palette.badgeText)
.padding(.horizontal, padding)
.frame(height: height)
.background(isHovered ? Palette.amberDeep : Palette.amber, in: RoundedRectangle(cornerRadius: Metrics.radiusSmall))
.contentShape(Rectangle())
}
.buttonStyle(.plain)
.fixedSize()
.pointerHand()
.tracksHover($isHovered)
}
}
/// The outline button of the detail footer and the command-missing state: text-5 border, raised fill on hover.
struct SchemaOutlineButton: View {
let label: String
var height: CGFloat = SchemaViewMetrics.footerButton
var fills = false
var padding: CGFloat = 16
let action: () -> Void
@State private var isHovered = false
var body: some View {
Button(action: action) {
Text(label)
.typeRole(Typography.button)
.lineLimit(1)
.foregroundStyle(isHovered ? Palette.text1 : Palette.text3)
.padding(.horizontal, padding)
.frame(maxWidth: fills ? .infinity : nil)
.frame(height: height)
.background(isHovered ? Palette.raised : .clear, in: RoundedRectangle(cornerRadius: Metrics.radiusSmall))
.overlay(RoundedRectangle(cornerRadius: Metrics.radiusSmall)
.strokeBorder(isHovered ? Palette.text4 : Palette.text5, lineWidth: Metrics.hairline))
.contentShape(Rectangle())
}
.buttonStyle(.plain)
.fixedSize(horizontal: !fills, vertical: false)
.pointerHand()
.tracksHover($isHovered)
}
}
/// A borderless icon button: text-muted, text-1 on hover.
struct SchemaIconButton: View {
let icon: GitIcon
let tip: String
let action: () -> Void
@State private var isHovered = false
var body: some View {
Button(action: action) {
GitIconView(icon: icon, size: 14)
.foregroundStyle(isHovered ? Palette.text1 : Palette.textMuted)
.padding(4)
.contentShape(Rectangle())
}
.buttonStyle(.plain)
.pointerHand()
.tracksHover($isHovered)
.tip(tip, edge: .trailing)
}
}
/// The table search's field editor; the key router tells it apart from the other fields by its class.
final class SchemaSearchFieldEditor: PointerBoundTextView {}