Files
lanework/Kanban/UI/Card/CardCommentsPane.swift
rzen 9588f7b1f0 Comments, phase 3 — search, the thread find, announcements, and a11y
Board search reaches comment bodies through a search-owned transient
index: the first live-query keystroke sweeps comments/*/index.md
off-actor (.draft and comments/.trash excluded), keystrokes re-filter
in memory, the index discards on clear — the snapshot stays O(cards).
⌘F routes by focus: the comments pane gets an app-owned find bar
spanning the whole rendered thread (next/prev cross rows with
wraparound); body and composer keep NSTextFinder; Find Next/Previous
graduate from FutureCommands. Foreign comment changes speak
path-shaped beside the announcer's ladder ("New comment on 'X'",
plural folds), narrowed by EchoLedger receipts consumed through
CommentPath.classify — and that read fixed a latent footprint bug
where a comment receipt resolved against the card's attachment
listing, read .absent, and classified the user's own write as
foreign. The pane completes its a11y story: flattened comment
elements with Edit/Delete/Reveal custom actions (un-flattening
during inline edit), phrase-table vocabulary, labeled composer and
sort control, and an audit over the open pane on a comment-seeded
fixture (runnable only where automation permission exists).

Claude-Session: https://claude.ai/code/session_01SR4XGjmBE16ZUYWpfFHXwY
2026-07-30 21:30:22 -04:00

240 lines
12 KiB
Swift

import AppKit
import SwiftUI
import UniformTypeIdentifiers
// MARK: - CardCommentsPane
/// The card window's **comments pane** — the middle of the three componentized panes
/// (05-card-window.md ▸ Composition, ▸ The comments column).
///
/// ### It does not know where it is mounted
///
/// Beside the body or under it, the pane is identical — "the panes are identical in both mounts" —
/// so nothing in this file asks. It fills the frame it is given, scrolls its own content, and the
/// arrangement is `CardWindowView`'s (`CommentsMount`). That is the componentization the 2026-07-29
/// re-composition asks for, stated as an absence: there is no layout parameter here to get wrong.
///
/// ### Header, thread, composer — and the composer is at the newest end
///
/// > The composer sits at the thread's newest end (bottom ascending, top descending) and the window
/// > opens scrolled to it — a thread opens where the conversation is happening.
///
/// Both halves come from one value (`CommentSortDirection.placesComposerFirst`), so the scroll target
/// and the composer's position cannot disagree — a window that opened at the wrong end would be wrong
/// only for the users who had flipped the sort, which is exactly the bug that ships.
struct CardCommentsPane: View {
let comments: CardComments
/// The **card**'s folder — what relative images and links in every comment resolve against.
let cardFolder: URL?
/// The window's thumbnail memory, shared with the sidebar's attachment rows so a file shown in
/// both places is rendered once.
let thumbnails: AttachmentThumbnailCache
/// **App-wide and persisted** (05 ▸ The comments column; 11-command-nexus.md files the header
/// control under Configuration controls). Read here rather than mirrored onto the window's handle
/// because there is exactly one of it and every open pane obeys it.
@AppStorage(AppPreferences.commentsNewestFirstKey) private var newestFirst = false
/// The composer's scroll anchor. A constant rather than a generated id because there is one
/// composer and two possible places for it.
private static let composerAnchor = "comments.composer"
private var pointSize: CGFloat { CardWindowMetrics.bodyPointSize }
private var direction: CommentSortDirection { CommentSortDirection(newestFirst: newestFirst) }
private var ordered: [Comment] { direction.apply(to: comments.thread.comments) }
var body: some View {
VStack(alignment: .leading, spacing: 0) {
header
.padding(.horizontal, CardWindowMetrics.gutter(bodyPointSize: pointSize))
.padding(.top, CardWindowMetrics.gutter(bodyPointSize: pointSize))
// **The find bar sits above the thread it searches** — `findBarPosition = .aboveContent`,
// which is where every other find in this window puts its bar (`CardBodySurface`).
if comments.find.isShowing {
CommentFindBar(find: comments.find)
.padding(.top, CardWindowMetrics.previewPadding(bodyPointSize: pointSize))
}
thread
}
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading)
// 10-accessibility.md's container label for the pane ("Comments, N") — the count is the
// thread's, so the spoken container and the visible header can never disagree.
.accessibilityElement(children: .contain)
.accessibilityLabel(AccessibilityPhrases.commentsContainerLabel(count: comments.thread.comments.count))
// The find's model of the rendered thread, rebuilt only while the bar is up: the search runs
// over every comment, mounted or not (`CommentThreadFind`), and building it costs a render
// pass the pane has no reason to spend on a window nobody is searching.
.onChange(of: FindThreadKey(isShowing: comments.find.isShowing, thread: comments.thread), initial: true) { _, key in
guard key.isShowing else { return }
comments.find.setThread(key.thread.comments, pointSize: pointSize)
}
// **A find cannot outlive the surface it searches.** The pane unmounts on View ▸ Show Comments
// and on the raw-source swap, and a session left showing would keep ⌘F and ⌘G pointed at a bar
// nobody can see (`CardWindowFind.route`'s open-bar clause).
.onDisappear { comments.find.dismiss() }
}
/// The pair the find's rebuild is keyed on. A value rather than two `onChange`s so the bar opening
/// and the thread changing take the same path, and so the rebuild cannot run twice for one update.
private struct FindThreadKey: Equatable {
let isShowing: Bool
let thread: CommentThread
}
// MARK: - Header
/// "Comments · 3" with the sort control beside it — the sidebar's own small-caps section header,
/// shared rather than restated so the pane and the sidebar read as one window (05: "the section
/// header carries the count and the sort-direction control").
private var header: some View {
CardSidebarSectionHeader(title: CommentsHeader.title(count: comments.thread.comments.count)) {
sortControl
}
}
/// The sort-direction control — **Tab-reachable beside the count** (11-command-nexus.md ▸
/// Configuration controls).
///
/// A button rather than a segmented picker: there are two states and the second one is the
/// reverse of the first, so a toggle whose glyph says which way the thread currently runs is the
/// smaller thing that says the same. Its help text and its accessibility label are the same
/// string (`CommentSortDirection.controlLabel`) — one label, two readers.
private var sortControl: some View {
Button {
newestFirst.toggle()
} label: {
Image(systemName: direction == .ascending ? "arrow.down" : "arrow.up")
.font(.caption.weight(.semibold))
.foregroundStyle(.secondary)
}
.buttonStyle(.plain)
.help(direction.controlLabel)
.accessibilityLabel(AccessibilityPhrases.commentSortLabel)
.accessibilityValue(direction.controlLabel)
}
// MARK: - The thread
private var thread: some View {
ScrollViewReader { proxy in
ScrollView(.vertical) {
LazyVStack(alignment: .leading, spacing: CardWindowMetrics.commentSpacing(bodyPointSize: pointSize)) {
if direction.placesComposerFirst {
composer.id(Self.composerAnchor)
}
ForEach(ordered) { comment in
CommentRowView(
comment: comment,
comments: comments,
cardFolder: cardFolder,
thumbnails: thumbnails
)
.id(comment.id)
}
if !direction.placesComposerFirst {
composer.id(Self.composerAnchor)
}
}
.padding(CardWindowMetrics.gutter(bodyPointSize: pointSize))
.frame(maxWidth: .infinity, alignment: .leading)
}
// **The window opens scrolled to the composer** (05). Deferred one turn rather than run
// inline: `scrollTo` needs the content laid out to have somewhere to scroll to, and a
// thread's rows measure their own rendered height (`CommentBodyView`).
.task {
await Task.yield()
proxy.scrollTo(Self.composerAnchor, anchor: direction.placesComposerFirst ? .top : .bottom)
}
// File ▸ Add Comment focuses the composer — which is no use if the composer is off
// screen, so the same request scrolls to it. One request, both effects.
.onChange(of: comments.focusComposerRequests) { _, _ in
proxy.scrollTo(Self.composerAnchor, anchor: direction.placesComposerFirst ? .top : .bottom)
}
// **A find that steps off screen brings the reader with it** — the half of "it reads as one
// find" that the highlight alone cannot do. Scrolling to the comment rather than to the
// range is what a row-shaped thread allows: rows are the scroll targets
// (`ForEach(...).id(comment.id)`), and a comment is short enough that its top is the hit's
// neighbourhood.
.onChange(of: comments.find.currentMatch) { _, match in
guard let match else { return }
proxy.scrollTo(match.comment, anchor: .center)
}
}
}
// MARK: - The composer
/// **The composer edits `comments/.draft/`** — always visible, at the thread's newest end
/// (05 ▸ The comments column).
private var composer: some View {
CommentComposerView(comments: comments, thumbnails: thumbnails)
}
}
// MARK: - The authoring surfaces' drop carve-out
/// **A file dropped within an authoring surface's bounds lands in *that* surface's `attachments/`**
/// (05-card-window.md ▸ Attachments, ruled 2026-07-29 — the hover-target carve-out on the
/// window-wide card default).
///
/// The *arbitration* is SwiftUI's own innermost-target dispatch: this delegate is attached **inside**
/// `CardWindowDropDelegate`'s region, so a drag released over the composer or over an open inline
/// editor is offered here first and never reaches the window's card default.
///
/// The *destination* is `CommentDropCarveOut`, which is why this takes a hover rather than a folder:
/// the surface says what the pointer is over and the pure rule says where the files go, so the ruling
/// — including which authoring surface wins where they would ever overlap — is checkable without a
/// window and cannot drift from what the delegate actually does. A hover the rule resolves to the
/// **card** never reaches here at all (no authoring surface is under the pointer, so no authoring
/// surface has a drop target on screen), and this refuses it rather than guessing a folder.
///
/// Everything else is `CardWindowDropDelegate`'s, deliberately: the same payload predicate (files,
/// not folders, not text), the same read-only refusal, the same `.copy` badge, and the same
/// asynchronous URL load with the sandbox's security scope around it. Only the destination differs,
/// which is the entire point of the carve-out.
struct CommentAttachmentDropDelegate: DropDelegate {
let comments: CardComments
/// What the pointer is over, as this surface knows it — see `CommentDropCarveOut.Hover`.
let hover: CommentDropCarveOut.Hover
/// Where the rule says the files go, or `nil` for the window-wide card default.
private var target: CommentTarget? {
guard case let .comment(target) = CommentDropCarveOut.landing(for: hover) else { return nil }
return target
}
private var acceptsFileDrops: Bool { comments.isEditable && target != nil }
func validateDrop(info: DropInfo) -> Bool {
guard acceptsFileDrops else { return false }
return CardWindowDrop.accepts(
payloads: info.itemProviders(for: [.fileURL]).map(\.registeredTypeIdentifiers)
)
}
func dropUpdated(info: DropInfo) -> DropProposal? {
DropProposal(operation: validateDrop(info: info) ? .copy : .cancel)
}
func performDrop(info: DropInfo) -> Bool {
guard acceptsFileDrops, let target else { return false }
let providers = info.itemProviders(for: [.fileURL])
guard !providers.isEmpty else { return false }
let comments = comments
Task { @MainActor in
var urls: [URL] = []
for provider in providers {
if let url = await FileDropLoading.url(from: provider) { urls.append(url) }
}
guard !urls.isEmpty else { return }
comments.importFiles(urls, to: target)
}
return true
}
}