| 1 | import SwiftUI |
| 2 | import Foundation |
| 3 | #if os(macOS) |
| 4 | import AppKit |
| 5 | #endif |
| 6 | |
| 7 | /// Where the page sits on the desk, the web's `show.html` rule in window |
| 8 | /// points (spec §1, "Page centring"). Pure arithmetic, so the six widths and |
| 9 | /// the 1,108pt boundary are a unit test. |
| 10 | /// |
| 11 | /// The trailing desk padding is `E = clamp(16, 16 + P + 2R − W, R)`, where |
| 12 | /// `R` is the floating inspector's reserve (248 + 12 + 16) and `P` the page's |
| 13 | /// width in window points; the page is centred in the band `[16, W − E]`. |
| 14 | /// When the band is narrower than the page its leading edge stays at 16 and |
| 15 | /// it runs under the inspector: the page never shrinks or reflows. |
| 16 | struct DocsDeskLayout: Equatable, Sendable { |
| 17 | static let padding: CGFloat = 16 |
| 18 | static let inspectorWidth: CGFloat = 248 |
| 19 | /// The inspector's gap from the toolbar and from the window's edge. |
| 20 | static let inspectorInset: CGFloat = 12 |
| 21 | static var reserve: CGFloat { inspectorWidth + inspectorInset + padding } |
| 22 | /// The paper's width in its own (unmagnified) points. |
| 23 | static let paperWidth: CGFloat = 612 |
| 24 | |
| 25 | /// Which side the inspector floats on, nil while it is hidden. It is the |
| 26 | /// trailing side: the right, or the left in a right-to-left language. |
| 27 | enum Side: Equatable, Sendable { case right, left } |
| 28 | |
| 29 | /// `E`, the padding on the inspector's side. |
| 30 | static func inspectorPadding(width: CGFloat, page: CGFloat, inspector: Bool) -> CGFloat { |
| 31 | guard inspector else { return padding } |
| 32 | return min(max(padding, padding + page + 2 * reserve - width), reserve) |
| 33 | } |
| 34 | |
| 35 | /// The page's distance from the inspector-free (leading) edge. |
| 36 | static func leadingEdge(width: CGFloat, page: CGFloat, inspector: Bool) -> CGFloat { |
| 37 | let band = width - padding - inspectorPadding(width: width, page: page, inspector: inspector) |
| 38 | return padding + max(0, (band - page) / 2) |
| 39 | } |
| 40 | |
| 41 | /// The page's left edge in window points, for either side. |
| 42 | static func pageX(width: CGFloat, page: CGFloat, inspector side: Side?) -> CGFloat { |
| 43 | let leading = leadingEdge(width: width, page: page, inspector: side != nil) |
| 44 | guard side == .left else { return leading } |
| 45 | // Mirrored: the leading edge is the right. A page wider than the |
| 46 | // window still starts at the left padding and scrolls to the right. |
| 47 | return max(padding, width - leading - page) |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | #if os(macOS) |
| 52 | /// The scroll view's document view: it draws the desk and holds the page |
| 53 | /// (`DocsPagedTextView`, a fixed paper band) as its only subview, at |
| 54 | /// `x = pageX / m`. Toggling the inspector or resizing the window moves that |
| 55 | /// one frame origin, so the page slides and the text never reflows |
| 56 | /// (spec §1, "Moving the page never relayouts the text"). |
| 57 | @MainActor final class DocsDeskView: NSView { |
| 58 | let page: NSView |
| 59 | var inspector: DocsDeskLayout.Side? { |
| 60 | didSet { if oldValue != inspector { arrange() } } |
| 61 | } |
| 62 | private var observers: [NSObjectProtocol] = [] |
| 63 | |
| 64 | init(page: NSView) { |
| 65 | self.page = page |
| 66 | super.init(frame: page.frame) |
| 67 | autoresizingMask = [.width] |
| 68 | page.autoresizingMask = [] |
| 69 | addSubview(page) |
| 70 | page.postsFrameChangedNotifications = true |
| 71 | observers.append(NotificationCenter.default.addObserver(forName: NSView.frameDidChangeNotification, object: page, queue: nil) { [weak self] _ in |
| 72 | MainActor.assumeIsolated { self?.arrange() } |
| 73 | }) |
| 74 | } |
| 75 | required init?(coder: NSCoder) { fatalError("init(coder:) is unavailable") } |
| 76 | isolated deinit { observers.forEach(NotificationCenter.default.removeObserver) } |
| 77 | |
| 78 | override var isFlipped: Bool { true } |
| 79 | override var isOpaque: Bool { true } |
| 80 | |
| 81 | override func viewDidMoveToSuperview() { |
| 82 | super.viewDidMoveToSuperview() |
| 83 | guard let clip = superview as? NSClipView else { return } |
| 84 | clip.postsBoundsChangedNotifications = true |
| 85 | clip.postsFrameChangedNotifications = true |
| 86 | for name in [NSView.boundsDidChangeNotification, NSView.frameDidChangeNotification] { |
| 87 | observers.append(NotificationCenter.default.addObserver(forName: name, object: clip, queue: nil) { [weak self] _ in |
| 88 | MainActor.assumeIsolated { self?.arrange() } |
| 89 | }) |
| 90 | } |
| 91 | arrange() |
| 92 | } |
| 93 | |
| 94 | /// The scroll view's magnification: document units to window points. |
| 95 | private var magnification: CGFloat { enclosingScrollView?.magnification ?? 1 } |
| 96 | |
| 97 | /// Sets the page's origin and the desk's size from the rule. O(1). |
| 98 | func arrange() { |
| 99 | guard let clip = superview as? NSClipView else { return } |
| 100 | let m = magnification |
| 101 | let width = clip.bounds.width * m |
| 102 | let pageWidth = page.frame.width * m |
| 103 | let x = DocsDeskLayout.pageX(width: width, page: pageWidth, inspector: inspector) / m |
| 104 | let origin = CGPoint(x: x, y: 0) |
| 105 | if page.frame.origin != origin { page.setFrameOrigin(origin) } |
| 106 | let size = CGSize(width: max(clip.bounds.width, x + page.frame.width + DocsDeskLayout.padding / m), height: page.frame.height) |
| 107 | if abs(frame.width - size.width) > 0.01 || abs(frame.height - size.height) > 0.01 { setFrameSize(size) } |
| 108 | } |
| 109 | |
| 110 | override func resize(withOldSuperviewSize oldSize: NSSize) { |
| 111 | super.resize(withOldSuperviewSize: oldSize) |
| 112 | arrange() |
| 113 | } |
| 114 | |
| 115 | override func draw(_ dirtyRect: NSRect) { |
| 116 | NSColor.windowBackgroundColor.setFill() |
| 117 | dirtyRect.fill() |
| 118 | } |
| 119 | |
| 120 | /// A click on the desk beside the page goes to the page, as it did when |
| 121 | /// the text view spanned the window. |
| 122 | override func mouseDown(with event: NSEvent) { page.mouseDown(with: event) } |
| 123 | } |
| 124 | #endif |
| 125 | |
| 126 | #if os(macOS) |
| 127 | extension NativeDocumentTextView { |
| 128 | /// The side the floating inspector covers, for the desk's centring. |
| 129 | func inspectorSide(_ side: DocsDeskLayout.Side?) -> NativeDocumentTextView { |
| 130 | var view = self |
| 131 | view.inspector = side |
| 132 | return view |
| 133 | } |
| 134 | } |
| 135 | #endif |
| 136 | |
| 137 | /// Fiesta's spacing scale in points (`--rst-sp-1` … `--rst-sp-6`: 4, 8, 12, |
| 138 | /// 16, 24, 40), so the native chrome keeps the web's rhythm (AGENTS.md, |
| 139 | /// "Spacing", Paul 2026-11-22: no ad-hoc spacing). Anything off the scale |
| 140 | /// says why where it is written (`spacing-allow:`). |
| 141 | enum DocsSpacing { |
| 142 | static let sp1: CGFloat = 4 |
| 143 | static let sp2: CGFloat = 8 |
| 144 | static let sp3: CGFloat = 12 |
| 145 | static let sp4: CGFloat = 16 |
| 146 | static let sp5: CGFloat = 24 |
| 147 | static let sp6: CGFloat = 40 |
| 148 | } |
| 149 | |
| 150 | /// Type for the chrome: the Mac's fixed sizes (no Dynamic Type there), and |
| 151 | /// on iPhone and iPad the text style of that size, which follows Dynamic |
| 152 | /// Type to AX5 (spec §9). A fixed size on iOS is one the accessibility |
| 153 | /// audit refuses. |
| 154 | enum DocsType { |
| 155 | static func font(_ size: CGFloat, _ weight: Font.Weight = .regular, ios style: Font.TextStyle) -> Font { |
| 156 | #if os(macOS) |
| 157 | .system(size: size, weight: weight) |
| 158 | #else |
| 159 | .system(style, weight: weight) |
| 160 | #endif |
| 161 | } |
| 162 | } |
| 163 | |