import Foundation /// A point on the schema canvas, in canvas units (before pan and zoom). public struct SchemaPoint: Sendable, Hashable, Codable { public var x: Double public var y: Double public init(x: Double, y: Double) { self.x = x self.y = y } } public struct SchemaSize: Sendable, Hashable { public var width: Double public var height: Double public init(width: Double, height: Double) { self.width = width self.height = height } } public struct SchemaRect: Sendable, Hashable { public var x: Double public var y: Double public var width: Double public var height: Double public init(x: Double, y: Double, width: Double, height: Double) { self.x = x self.y = y self.width = width self.height = height } public var maxX: Double { x + width } public var maxY: Double { y + height } } /// Pan and zoom: a canvas point `p` shows at `p * scale + (x, y)` in the viewport. public struct SchemaViewport: Sendable, Hashable { public var x: Double public var y: Double public var scale: Double public init(x: Double = 0, y: Double = 0, scale: Double = 1) { self.x = x self.y = y self.scale = scale } /// `100%` style label for the zoom control. public var percentLabel: String { "\(Int((scale * 100).rounded()))%" } } /// The fixed sizes of the Schema canvas; the line geometry depends on the card sizes. public enum SchemaMetrics { public static let cardWidth = 240.0 /// The header, including its bottom border. public static let headerHeight = 33.0 public static let rowHeight = 22.0 /// Card top to the centre of column 0: 1px border + 33 header + half a row. public static let firstRowCentre = 45.0 /// Header plus the card's top and bottom border. public static let cardChrome = 35.0 public static let laneSpacing = 340.0 public static let cardGap = 48.0 /// Where the default layout starts. public static let layoutOrigin = SchemaPoint(x: 40, y: 40) public static let fitMargin = 48.0 public static let fitMaxScale = 1.1 /// Room right of the rightmost card for same-lane loops, as the prototype's fit. public static let fitExtraWidth = 60.0 public static let minScale = 0.3 public static let maxScale = 2.0 public static let zoomStep = 1.2 /// Card centres closer than this count as one lane: the line loops out on the right. public static let sameLaneDistance = 240.0 public static let loopBase = 46.0 public static let loopFactor = 0.12 public static let minCurve = 40.0 public static let endRadius = 3.0 } /// A cubic Bézier with horizontal tangents, from the foreign-key row to the referenced row. public struct SchemaEdgePath: Sendable, Hashable { public var start: SchemaPoint public var control1: SchemaPoint public var control2: SchemaPoint public var end: SchemaPoint public init(start: SchemaPoint, control1: SchemaPoint, control2: SchemaPoint, end: SchemaPoint) { self.start = start self.control1 = control1 self.control2 = control2 self.end = end } } public enum SchemaGeometry { public static func cardHeight(columns: Int) -> Double { SchemaMetrics.cardChrome + Double(columns) * SchemaMetrics.rowHeight } /// The vertical centre of column `index` in a card whose top is `top`. public static func columnCentreY(top: Double, index: Int) -> Double { top + SchemaMetrics.firstRowCentre + Double(index) * SchemaMetrics.rowHeight } /// Right edge to left edge when the target is right, the mirror when left, a loop off the right edges in one lane. public static func edgePath(from source: SchemaPoint, columnIndex: Int, to target: SchemaPoint, targetColumnIndex: Int) -> SchemaEdgePath { let width = SchemaMetrics.cardWidth let y1 = columnCentreY(top: source.y, index: columnIndex) let y2 = columnCentreY(top: target.y, index: targetColumnIndex) let sourceCentre = source.x + width / 2 let targetCentre = target.x + width / 2 let x1: Double, x2: Double, c1: Double, c2: Double if abs(sourceCentre - targetCentre) < SchemaMetrics.sameLaneDistance { x1 = source.x + width x2 = target.x + width let loop = SchemaMetrics.loopBase + abs(y2 - y1) * SchemaMetrics.loopFactor c1 = x1 + loop c2 = x2 + loop } else if targetCentre > sourceCentre { x1 = source.x + width x2 = target.x let d = max(SchemaMetrics.minCurve, (x2 - x1) / 2) c1 = x1 + d c2 = x2 - d } else { x1 = source.x x2 = target.x + width let d = max(SchemaMetrics.minCurve, (x1 - x2) / 2) c1 = x1 - d c2 = x2 + d } return SchemaEdgePath(start: SchemaPoint(x: x1, y: y1), control1: SchemaPoint(x: c1, y: y1), control2: SchemaPoint(x: c2, y: y2), end: SchemaPoint(x: x2, y: y2)) } /// Nil when either table has no position. public static func edgePath(_ edge: SchemaEdge, positions: [String: SchemaPoint]) -> SchemaEdgePath? { guard let source = positions[edge.from], let target = positions[edge.to] else { return nil } return edgePath(from: source, columnIndex: edge.columnIndex, to: target, targetColumnIndex: edge.targetColumnIndex) } public static func cardRect(_ table: SchemaTable, at position: SchemaPoint) -> SchemaRect { SchemaRect(x: position.x, y: position.y, width: SchemaMetrics.cardWidth, height: table.cardHeight) } /// The box around every placed card; nil when none is placed. public static func bounds(_ model: SchemaModel, positions: [String: SchemaPoint]) -> SchemaRect? { var x0 = Double.infinity, y0 = Double.infinity, x1 = -Double.infinity, y1 = -Double.infinity for table in model.tables { guard let p = positions[table.name] else { continue } let rect = cardRect(table, at: p) x0 = min(x0, rect.x) y0 = min(y0, rect.y) x1 = max(x1, rect.maxX) y1 = max(y1, rect.maxY) } guard x0.isFinite else { return nil } return SchemaRect(x: x0, y: y0, width: x1 - x0, height: y1 - y0) } public static func clampScale(_ scale: Double) -> Double { min(SchemaMetrics.maxScale, max(SchemaMetrics.minScale, scale)) } /// Frames `bounds` in `viewport` with a 48px margin, never above 110% nor below 30%. public static func fit(bounds: SchemaRect, in viewport: SchemaSize) -> SchemaViewport { let margin = SchemaMetrics.fitMargin * 2 let width = bounds.width + SchemaMetrics.fitExtraWidth let height = bounds.height let fitting = min((viewport.width - margin) / max(width, 1), (viewport.height - margin) / max(height, 1)) let scale = min(SchemaMetrics.fitMaxScale, max(SchemaMetrics.minScale, fitting)) return SchemaViewport(x: (viewport.width - width * scale) / 2 - bounds.x * scale, y: (viewport.height - height * scale) / 2 - bounds.y * scale, scale: scale) } /// Scales by `factor` (clamped to 30–200%) keeping the canvas point under `anchor` (viewport coordinates) in place. public static func zoom(_ view: SchemaViewport, by factor: Double, around anchor: SchemaPoint) -> SchemaViewport { let scale = clampScale(view.scale * factor) let ratio = scale / view.scale return SchemaViewport(x: anchor.x - (anchor.x - view.x) * ratio, y: anchor.y - (anchor.y - view.y) * ratio, scale: scale) } /// Zooms around the viewport's centre, as the `−` and `+` buttons do. public static func zoom(_ view: SchemaViewport, by factor: Double, in viewport: SchemaSize) -> SchemaViewport { zoom(view, by: factor, around: SchemaPoint(x: viewport.width / 2, y: viewport.height / 2)) } /// Pans so the card sits in the middle of `viewport`, keeping the scale; values are rounded to whole points. /// /// Pass the width left of the detail panel when it is about to open, as the prototype does. public static func centre(_ table: SchemaTable, at position: SchemaPoint, in viewport: SchemaSize, view: SchemaViewport) -> SchemaViewport { let k = view.scale let midX = position.x + SchemaMetrics.cardWidth / 2 let midY = position.y + table.cardHeight / 2 return SchemaViewport(x: (viewport.width / 2 - midX * k).rounded(), y: (viewport.height / 2 - midY * k).rounded(), scale: k) } /// The canvas point under a viewport point. public static func canvasPoint(_ point: SchemaPoint, in view: SchemaViewport) -> SchemaPoint { SchemaPoint(x: (point.x - view.x) / view.scale, y: (point.y - view.y) / view.scale) } }