import SwiftUI import Foundation #if os(macOS) import AppKit #endif /// Where the page sits on the desk, the web's `show.html` rule in window /// points (spec §1, "Page centring"). Pure arithmetic, so the six widths and /// the 1,108pt boundary are a unit test. /// /// The trailing desk padding is `E = clamp(16, 16 + P + 2R − W, R)`, where /// `R` is the floating inspector's reserve (248 + 12 + 16) and `P` the page's /// width in window points; the page is centred in the band `[16, W − E]`. /// When the band is narrower than the page its leading edge stays at 16 and /// it runs under the inspector: the page never shrinks or reflows. struct DocsDeskLayout: Equatable, Sendable { static let padding: CGFloat = 16 static let inspectorWidth: CGFloat = 248 /// The inspector's gap from the toolbar and from the window's edge. static let inspectorInset: CGFloat = 12 static var reserve: CGFloat { inspectorWidth + inspectorInset + padding } /// The paper's width in its own (unmagnified) points. static let paperWidth: CGFloat = 612 /// Which side the inspector floats on, nil while it is hidden. It is the /// trailing side: the right, or the left in a right-to-left language. enum Side: Equatable, Sendable { case right, left } /// `E`, the padding on the inspector's side. static func inspectorPadding(width: CGFloat, page: CGFloat, inspector: Bool) -> CGFloat { guard inspector else { return padding } return min(max(padding, padding + page + 2 * reserve - width), reserve) } /// The page's distance from the inspector-free (leading) edge. static func leadingEdge(width: CGFloat, page: CGFloat, inspector: Bool) -> CGFloat { let band = width - padding - inspectorPadding(width: width, page: page, inspector: inspector) return padding + max(0, (band - page) / 2) } /// The page's left edge in window points, for either side. static func pageX(width: CGFloat, page: CGFloat, inspector side: Side?) -> CGFloat { let leading = leadingEdge(width: width, page: page, inspector: side != nil) guard side == .left else { return leading } // Mirrored: the leading edge is the right. A page wider than the // window still starts at the left padding and scrolls to the right. return max(padding, width - leading - page) } } #if os(macOS) /// The scroll view's document view: it draws the desk and holds the page /// (`DocsPagedTextView`, a fixed paper band) as its only subview, at /// `x = pageX / m`. Toggling the inspector or resizing the window moves that /// one frame origin, so the page slides and the text never reflows /// (spec §1, "Moving the page never relayouts the text"). @MainActor final class DocsDeskView: NSView { let page: NSView var inspector: DocsDeskLayout.Side? { didSet { if oldValue != inspector { arrange() } } } private var observers: [NSObjectProtocol] = [] init(page: NSView) { self.page = page super.init(frame: page.frame) autoresizingMask = [.width] page.autoresizingMask = [] addSubview(page) page.postsFrameChangedNotifications = true observers.append(NotificationCenter.default.addObserver(forName: NSView.frameDidChangeNotification, object: page, queue: nil) { [weak self] _ in MainActor.assumeIsolated { self?.arrange() } }) } required init?(coder: NSCoder) { fatalError("init(coder:) is unavailable") } isolated deinit { observers.forEach(NotificationCenter.default.removeObserver) } override var isFlipped: Bool { true } override var isOpaque: Bool { true } override func viewDidMoveToSuperview() { super.viewDidMoveToSuperview() guard let clip = superview as? NSClipView else { return } clip.postsBoundsChangedNotifications = true clip.postsFrameChangedNotifications = true for name in [NSView.boundsDidChangeNotification, NSView.frameDidChangeNotification] { observers.append(NotificationCenter.default.addObserver(forName: name, object: clip, queue: nil) { [weak self] _ in MainActor.assumeIsolated { self?.arrange() } }) } arrange() } /// The scroll view's magnification: document units to window points. private var magnification: CGFloat { enclosingScrollView?.magnification ?? 1 } /// Sets the page's origin and the desk's size from the rule. O(1). func arrange() { guard let clip = superview as? NSClipView else { return } let m = magnification let width = clip.bounds.width * m let pageWidth = page.frame.width * m let x = DocsDeskLayout.pageX(width: width, page: pageWidth, inspector: inspector) / m let origin = CGPoint(x: x, y: 0) if page.frame.origin != origin { page.setFrameOrigin(origin) } let size = CGSize(width: max(clip.bounds.width, x + page.frame.width + DocsDeskLayout.padding / m), height: page.frame.height) if abs(frame.width - size.width) > 0.01 || abs(frame.height - size.height) > 0.01 { setFrameSize(size) } } override func resize(withOldSuperviewSize oldSize: NSSize) { super.resize(withOldSuperviewSize: oldSize) arrange() } override func draw(_ dirtyRect: NSRect) { NSColor.windowBackgroundColor.setFill() dirtyRect.fill() } /// A click on the desk beside the page goes to the page, as it did when /// the text view spanned the window. override func mouseDown(with event: NSEvent) { page.mouseDown(with: event) } } #endif #if os(macOS) extension NativeDocumentTextView { /// The side the floating inspector covers, for the desk's centring. func inspectorSide(_ side: DocsDeskLayout.Side?) -> NativeDocumentTextView { var view = self view.inspector = side return view } } #endif /// Fiesta's spacing scale in points (`--rst-sp-1` … `--rst-sp-6`: 4, 8, 12, /// 16, 24, 40), so the native chrome keeps the web's rhythm (AGENTS.md, /// "Spacing", Paul 2026-11-22: no ad-hoc spacing). Anything off the scale /// says why where it is written (`spacing-allow:`). enum DocsSpacing { static let sp1: CGFloat = 4 static let sp2: CGFloat = 8 static let sp3: CGFloat = 12 static let sp4: CGFloat = 16 static let sp5: CGFloat = 24 static let sp6: CGFloat = 40 } /// Type for the chrome: the Mac's fixed sizes (no Dynamic Type there), and /// on iPhone and iPad the text style of that size, which follows Dynamic /// Type to AX5 (spec §9). A fixed size on iOS is one the accessibility /// audit refuses. enum DocsType { static func font(_ size: CGFloat, _ weight: Font.Weight = .regular, ios style: Font.TextStyle) -> Font { #if os(macOS) .system(size: size, weight: weight) #else .system(style, weight: weight) #endif } }