import SwiftUI // MARK: - CardWindowView /// The card window's content: **two full-height, independently scrolling columns** — a wide body /// column leading, a narrow attributes sidebar trailing (05-card-window.md ▸ Composition). /// /// ### What this milestone builds, and what it deliberately does not /// /// The *shell*: the two columns, their scrolling, the sidebar's fixed width, and the read-only /// renderings of what the loader already knows — the card's title and its created/modified line — /// over the body column's two live surfaces (Preview and Edit, `CardBodySurface`). Everything that /// reads or writes beyond that is later work and is marked where it lands: /// /// - the title as an editable field (commit on Return / focus loss, Escape abandons), /// - the raw-source outlet, /// - the sidebar's five sections, which are section *headers* here and nothing more. /// /// The placeholders are structural rather than apologetic: the sidebar's inventory and its order are /// settled (05 ▸ The attributes sidebar), so the shell states them and the sections fill in /// underneath without the composition moving. /// /// ### The width rule, in one line /// /// The sidebar has a fixed width from `CardWindowMetrics`; the body column takes `.infinity`. That /// is the whole of "the window's resize flex goes to the body" — no split view, no stored divider /// position, nothing for a drag to disagree with. /// /// ### Why the title no longer scrolls with the body /// /// The body surface is a hosted `NSScrollView` (`CardBodySurface`), because ⌘F's find bar lives in /// one — "Edit ▸ Find (⌘F) is find-in-text here … the standard find bar" (05 ▸ Preview). A scroll /// view inside a scroll view is a scroll view that fights, so the column's header — the title and /// its created/modified line — sits above the body's scroller rather than inside it. 05 fixes the /// column's *order* ("Body column, top to bottom") and the columns' independent scrolling, and both /// still hold; which of the two things scrolls the title away was never settled, and pinning the /// card's name over its own body is the better reading of a window whose subtitle already follows it. struct CardWindowView: View { let card: Card /// The card's folder on disk — what relative images and links in the body resolve against /// (05 ▸ Preview). `nil` only where a caller has no board root to build it from. let cardFolder: URL? /// This window's body-column state: which mode it is in, and the find-bar hook. let bodyPresentation: CardBodyPresentation /// This window's Edit buffer. It holds the text **both** surfaces show: the editor writes into /// it, Preview renders it, and `adopt(diskBody:)` below is where the snapshot gets a say — /// which is exactly the point at which dirty-buffer-wins is decided. let bodySession: CardBodyEditSession /// Whether a checkbox may write — `false` under the board's read-only lock. let isEditable: Bool /// Commits a checkbox flip: the marker's byte offset in the body, and the state the user saw. let onToggleTask: (Int, Bool) -> Void /// The body font's point size, read once per body evaluation: every measurement in this view — /// the sidebar's width, both gutters, the vertical rhythm — is derived from it, so they scale /// together when the system text size changes. private var bodyPointSize: CGFloat { CardWindowMetrics.bodyPointSize } var body: some View { HStack(spacing: 0) { bodyColumn .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading) Divider() sidebar // Fixed, and the one place it comes from. .frame(width: CardWindowMetrics.sidebarWidth(bodyPointSize: bodyPointSize)) .frame(maxHeight: .infinity, alignment: .top) .background(.background.secondary) } } // MARK: - Body column /// Title, the quiet created/modified line, then the body — 05's top-to-bottom order. private var bodyColumn: some View { VStack(alignment: .leading, spacing: 0) { VStack(alignment: .leading, spacing: bodyPointSize * 0.5) { // m6-card-body: the title *field* — large and borderless, committing to frontmatter // on Return or focus loss, clearing to remove the `title` key, Escape abandoning to // the on-disk title. Read-only here; the placeholder rendering is already final. Text(card.title.value ?? "Untitled") .font(.largeTitle) // "Untitled" is a rendering, never a value (03-board-ui.md § Card face) — the // same secondary treatment the face gives it. .foregroundStyle(card.title.value == nil ? .secondary : .primary) .textSelection(.enabled) if let dateLine { Text(dateLine) .font(.caption) .foregroundStyle(.secondary) .textSelection(.enabled) } } .frame(maxWidth: .infinity, alignment: .leading) .padding(.horizontal, CardWindowMetrics.gutter(bodyPointSize: bodyPointSize)) .padding(.top, CardWindowMetrics.gutter(bodyPointSize: bodyPointSize)) CardBodySurface( // The session's text, never `card.body` directly: a dirty buffer outranks the // snapshot (05 ▸ Write rules) and a flushed one is ahead of it by a reload, so the // buffer is the truer of the two in both modes — which is also how Preview shows the // text that produced it the instant Edit is left. body: bodySession.text, mode: bodyPresentation.mode, cardFolder: cardFolder, presentation: bodyPresentation, session: bodySession, isTaskToggleEnabled: isEditable, onToggleTask: onToggleTask ) .frame(maxWidth: .infinity, maxHeight: .infinity) } // **Dirty-buffer-wins, applied on every snapshot** (05 ▸ Write rules): the session takes // disk's word for what the file says, and takes it into the editor only when the buffer has // nothing unsaved. `initial: true` is also how the buffer is filled at all — a window opens // by adopting its card's body. .onChange(of: card.body, initial: true) { _, body in bodySession.adopt(diskBody: body) } // **The opening rule, applied once** (05 ▸ Mode grammar): a card opens in Preview unless its // body is empty, in which case it opens straight into Edit. `openIfNeeded` is what makes it // "once" — a later reload that empties the file must not drag a reader into Edit. .task { bodyPresentation.openIfNeeded(body: card.body) } } /// "Created ⟨date⟩ · Modified ⟨date⟩ · by ⟨modified-by⟩", **omitting whichever keys are absent** /// (05 ▸ Composition) — the whole line disappears when the card carries none of the three. /// /// The "by" segment renders only with the self-reported provenance stamp present /// (01-storage-format.md), which is the point of showing it at all: provenance made visible where /// git history may not exist. private var dateLine: String? { var parts: [String] = [] if let created = card.created.value { parts.append("Created \(Self.dateText(created))") } if let modified = card.modified.value { parts.append("Modified \(Self.dateText(modified))") } if let by = card.modifiedBy.value, !by.isEmpty { parts.append("by \(by)") } return parts.isEmpty ? nil : parts.joined(separator: " · ") } private static func dateText(_ date: Date) -> String { date.formatted(date: .abbreviated, time: .shortened) } // MARK: - Attributes sidebar /// The sidebar's sections, **in 05's settled order**, as headers over empty space. /// /// Two of them are conditional once they have content — Details appears only when the card /// carries unknown frontmatter keys, and History is absent on boards without app-managed git — /// and the shell shows them unconditionally because it has neither the key inventory nor a git /// mode to consult yet. That is the one place these placeholders are not yet the final /// composition, and it resolves when the sections do. private var sidebar: some View { ScrollView(.vertical) { VStack(alignment: .leading, spacing: bodyPointSize * 1.25) { // m6-card-attachments: every top-level file of `attachments/`, compact rows with a // QuickLook thumbnail, keyboard-navigable. section("Attachments") // m6-card-sidebar: the embedded style editor — the same component the board popover // and Style… already host (`StyleEditor`). section("Style") // m6-card-sidebar: read-only key/value rows for every unknown frontmatter key, in // file order. section("Details") // m7-git: the card's commit trail, read-only; absent on mode none / repo-nested. section("History") // m6-card-sidebar: Delete (tombstones, the window then dismisses itself) and Reveal // in Finder. section("Actions") } .frame(maxWidth: .infinity, alignment: .leading) .padding(CardWindowMetrics.gutter(bodyPointSize: bodyPointSize)) } } /// A stacked small-caps header over the space its section will occupy (05: "Stacked sections /// under small-caps headers"). private func section(_ title: String) -> some View { VStack(alignment: .leading, spacing: 4) { Text(title) .font(.caption.weight(.semibold)) .textCase(.uppercase) .foregroundStyle(.secondary) Divider() } .frame(maxWidth: .infinity, alignment: .leading) .accessibilityElement(children: .combine) } }