// // PostsPaneView.swift // Museion // // #17 (S1.1): the Posts pane — library column mirroring the Home's posts/ // on disk. #18: lifecycle — New Post draft, ⌘S first-write/save, rename, // trash. #19: the TextKit2 editor core (PostEditorView) replaces the stub; // mentor column (#14). // import SwiftUI /// The Posts destination: the workspace's full four-column layout /// library 232 | editor ≥420 | mentor 356 (design contract grid). struct PostsPaneView: View { let home: URL /// #14: the app-scoped mentor model + the gate's winning binary (no re-probe). var mentor: MentorSessionModel var mentorBinaryPath: String? /// #18: app-scoped Posts state (watcher + lifecycle ops). var model: PostsModel /// Width budget after the rail — drives column collapse (export media queries: /// library hides ≤1100, mentor hides ≤920). var availableWidth: CGFloat @Environment(AppGateState.self) private var gate private var showsLibrary: Bool { availableWidth >= 1100 - DesignTokens.railWidth } private var showsMentor: Bool { availableWidth >= 920 - DesignTokens.railWidth } var body: some View { Group { if gate.isFocused { // #21: columns recede; the editor surface + centered measure on // the workspace background is all that remains. The mentor // returns by right-edge hover (overlay below) — only while the // width budget grants it (#17's collapse verdict stands here too). ZStack(alignment: .trailing) { editorColumn if showsMentor { FocusOverlayPanel(edge: .trailing, panelWidth: DesignTokens.mentorWidth) { mentorColumn } } } } else { HStack(spacing: 0) { if showsLibrary { PostsLibraryColumn(home: home, model: model) paneBorder } editorColumn if showsMentor { paneBorder mentorColumn } } } } .task(id: home) { // Bind: initial off-main scan + (re)start the FSEvents watcher (#61). model.bind(home: home) } } private var paneBorder: some View { Rectangle() .fill(DesignTokens.border) .frame(width: 1) } // MARK: Editor column (#19: TextKit2 editor core) private var editorColumn: some View { VStack(spacing: 0) { if let draft = model.draft { PostEditorView( session: nil, draftText: draft.text, draftEdited: { model.updateDraft($0) }, draftSave: { model.saveDraft(home: home) }, ) .id("draft") } else if let external = model.externalSession { // #92: the guest slot — an external file in the editor column; // one at a time, transient per launch. PostEditorView(session: external, draftText: nil) .id(external.fileURL?.absoluteString ?? "external") } else if let session = model.session, session.postID == model.selectedPostID { PostEditorView(session: session, draftText: nil) .id(session.postID ?? "post") } else { VStack(spacing: 10) { Image(systemName: "square.and.pencil") .font(.largeTitle) .foregroundStyle(DesignTokens.muted) Text("Select a post, or start a new one.") .font(.callout) .foregroundStyle(DesignTokens.muted) } .frame(maxWidth: .infinity, maxHeight: .infinity) } if let error = model.lastError { Text(error) .font(.system(size: 11, design: DesignTokens.mono)) .foregroundStyle(DesignTokens.accent) .padding(.vertical, 6) } } .frame(maxWidth: .infinity, maxHeight: .infinity) .background(DesignTokens.surface) } private var mentorColumn: some View { MentorPaneView(model: mentor, binaryPath: mentorBinaryPath) .frame(width: DesignTokens.mentorWidth) } } /// #21: the library column, shared by the unfocused layout and the focus-mode /// left reveal overlay — one implementation, both surfaces. Owns the rename /// sheet + context menu (they travel with the post rows). struct PostsLibraryColumn: View { let home: URL var model: PostsModel @State private var renamingPost: Post? var body: some View { VStack(spacing: 0) { PaneHeader(title: "Posts") { Button { model.newPost() } label: { Image(systemName: "plus") } .buttonStyle(.plain) .foregroundStyle(DesignTokens.muted) .help("New Post") .accessibilityLabel("New Post") } if model.posts.isEmpty { emptyState } else { ScrollView { LazyVStack(alignment: .leading, spacing: 4) { ForEach(Post.PassState.allCases, id: \.self) { state in let grouped = model.posts.filter { $0.passState == state } if !grouped.isEmpty { Text(state.rawValue.uppercased()) .font(.system(size: 11, design: DesignTokens.mono)) .kerning(0.6) .foregroundStyle(DesignTokens.muted) .padding(.horizontal, 14) .padding(.top, 18) .padding(.bottom, 7) ForEach(grouped) { post in PostRow(post: post, selected: post.id == model.selectedPostID) { model.select(post.id, home: home) } .contextMenu { Button("Rename…") { renamingPost = post } Button("Move to Trash", role: .destructive) { model.trash(post, home: home) } } } } } } .padding(.bottom, 12) } } } .frame(width: DesignTokens.libraryWidth) .sheet(item: $renamingPost) { post in RenamePostSheet(post: post) { title in model.rename(post, to: title, home: home) } } } private var emptyState: some View { VStack(spacing: 8) { Text("No posts yet") .font(.system(size: 16, weight: .semibold, design: DesignTokens.serif)) Text("Posts in this Home's posts/ folder appear here.") .font(.callout) .foregroundStyle(DesignTokens.muted) .multilineTextAlignment(.center) } .padding(38) .frame(maxWidth: .infinity, maxHeight: .infinity) } } /// #60: Explicit Rename dialog — asks the new Title (not a slug). private struct RenamePostSheet: View { let rename: (String) -> Void @Environment(\.dismiss) private var dismiss @State private var title: String init(post: Post, rename: @escaping (String) -> Void) { self.rename = rename _title = State(initialValue: post.title) } var body: some View { VStack(alignment: .leading, spacing: 12) { Text("Rename Post") .font(.system(size: 15, weight: .semibold, design: DesignTokens.serif)) Text("The folder moves to match the new Title. The file stays post.md.") .font(.callout) .foregroundStyle(DesignTokens.muted) TextField("Title", text: $title) .textFieldStyle(.roundedBorder) .onSubmit { confirm() } HStack { Spacer() Button("Cancel", role: .cancel) { dismiss() } .keyboardShortcut(.cancelAction) Button("Rename", action: confirm) .keyboardShortcut(.defaultAction) .disabled(title.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty) } } .padding(20) .frame(width: 360) } private func confirm() { let trimmed = title.trimmingCharacters(in: .whitespacesAndNewlines) guard !trimmed.isEmpty else { return } rename(trimmed) dismiss() } } /// One post row per the export: title, Pass State + Origin in mono 11. private struct PostRow: View { let post: Post let selected: Bool let select: () -> Void @State private var hover = false var body: some View { Button(action: select) { VStack(alignment: .leading, spacing: 5) { Text(post.title) .font(.callout.weight(.semibold)) .lineLimit(1) .truncationMode(.tail) .frame(maxWidth: .infinity, alignment: .leading) HStack { Text(post.passState.rawValue) Text(post.origin.rawValue) } .font(.system(size: 11, design: DesignTokens.mono)) .foregroundStyle(DesignTokens.muted) .frame(maxWidth: .infinity, alignment: .leading) } .padding(10) } .buttonStyle(.plain) .foregroundStyle(DesignTokens.foreground) .background { if selected || hover { RoundedRectangle(cornerRadius: DesignTokens.radius) .fill(selected ? DesignTokens.surface : DesignTokens.foreground.opacity(0.06)) .overlay( RoundedRectangle(cornerRadius: DesignTokens.radius) .strokeBorder(selected ? DesignTokens.border : .clear), ) } } .padding(.horizontal, 8) .onHover { hover = $0 } .accessibilityLabel("\(post.title), \(post.passState.rawValue), origin \(post.origin.rawValue)") } }