feat(ui): design-council polish overhaul
CI / ci (push) Canceled after 0s
CI / ci (pull_request) Canceled after 0s
CI / ci (push) Canceled after 0s
CI / ci (pull_request) Canceled after 0s
Eight arbitrated rounds over the workspace surface: piece-scoped Mentor hosting, conversation-voice system messages, glyph-measured roster truncation, bounded learnings/archive columns, symmetric trends grid, top-aligned settings forms, serif editor titles, outline rail rhythm, launch auto-focus root fix. New seams pinned by SessionScopeTests + WordBoundaryTruncationTests; full suite green.
This commit is contained in:
1 parent
b2db859887
commit
3a7d495b4b
21 files changed
+2287
-516
No files matched your search
+279
-108
@@ -9,6 +9,7 @@
|
||||
// toggle there).
|
||||
//
|
||||
|
||||
import AppKit
|
||||
import SwiftUI
|
||||
|
||||
/// The Sessions rail destination: two-column body like Posts — roster list
|
||||
@@ -32,19 +33,23 @@ struct SessionsPaneView: View {
|
||||
@State private var sessionDraft: String?
|
||||
|
||||
var body: some View {
|
||||
VStack(spacing: 0) {
|
||||
PaneHeader(title: "Sessions") {
|
||||
newSessionControl
|
||||
// Round-1 polish (argon#3/sol#19/mercury#41): the pane header —
|
||||
// title + New Session + its hairline — confines to the roster
|
||||
// column (the Posts pane's language), and the roster|transcript
|
||||
// divider runs full height.
|
||||
HStack(spacing: 0) {
|
||||
VStack(spacing: 0) {
|
||||
PaneHeader(title: "Sessions") {
|
||||
newSessionControl
|
||||
}
|
||||
rosterList
|
||||
}
|
||||
.frame(width: DesignTokens.libraryWidth)
|
||||
paneBorder
|
||||
if model.roster.isEmpty {
|
||||
emptyState
|
||||
} else {
|
||||
HStack(spacing: 0) {
|
||||
rosterList
|
||||
.frame(width: DesignTokens.libraryWidth)
|
||||
paneBorder
|
||||
sessionDetail
|
||||
}
|
||||
sessionDetail
|
||||
}
|
||||
}
|
||||
.frame(maxWidth: .infinity, maxHeight: .infinity)
|
||||
@@ -79,6 +84,21 @@ struct SessionsPaneView: View {
|
||||
// real engine child there.
|
||||
guard ProcessInfo.processInfo.environment["XCTestConfigurationFilePath"] == nil else { return }
|
||||
await model.openSession()
|
||||
// Round-4 (all seats — Sessions scored 6 everywhere): entering
|
||||
// the archive with nothing active (the Mentor pane's piece
|
||||
// scoping clears activeEntry) auto-selects the newest roster
|
||||
// session — the blank-canvas + bare em-dash entry dies.
|
||||
// openSession is idempotent once ready, so this is the
|
||||
// pane-entry selection rail; Sessions stays the unscoped
|
||||
// archive (the Mentor pane re-scopes at ITS mount).
|
||||
// (#142 posture: the wrapped condition list's brace lands on
|
||||
// its own line and trips opening_brace — nested ifs, never
|
||||
// fight the formatter.)
|
||||
if model.activeEntry == nil, !model.isStreaming {
|
||||
if let newest = model.roster.first {
|
||||
await model.resumeSession(newest)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -96,6 +116,7 @@ struct SessionsPaneView: View {
|
||||
}
|
||||
}
|
||||
}
|
||||
.padding(.top, 14) // Round-1 polish: the topmost row clears the header hairline
|
||||
.padding(.bottom, 12)
|
||||
}
|
||||
}
|
||||
@@ -146,55 +167,108 @@ struct SessionsPaneView: View {
|
||||
}
|
||||
}
|
||||
|
||||
/// The active session's name + engine version, Fork/Compact beside them
|
||||
/// The active session's name + Fork/Compact beside them
|
||||
/// — disabled while a turn streams (canStartSession).
|
||||
/// Round-4: the row rides the #90 52pt header height so its hairline
|
||||
/// runs continuous with the roster header's (argon#14/mercury#29); the
|
||||
/// pills take the app's bordered secondary language at 28pt with a 16pt
|
||||
/// window margin (sol#43/argon#48); a no-selection state never renders
|
||||
/// a bare em-dash, and the session-scoped actions disable until a
|
||||
/// session is active (all seats, round 3).
|
||||
/// Round-5 (argon#47): a 'Pass N ·' prefix reads muted beside the ink
|
||||
/// title (same line).
|
||||
/// Round-8 (mercury#17 — FINAL RULING): the version badge sits BESIDE
|
||||
/// the trailing button group — title | spacer | badge | buttons. Its
|
||||
/// r5-r7 seat beside the title left a dead middle gap; it rides
|
||||
/// vertically centered with the 28pt pills, 12pt off the group
|
||||
/// (8 row spacing + the group's 4pt lead).
|
||||
private var actionsRow: some View {
|
||||
HStack(spacing: 8) {
|
||||
Text(model.activeEntry?.name ?? "—")
|
||||
.font(.system(size: 13, weight: .semibold, design: DesignTokens.serif))
|
||||
.lineLimit(1)
|
||||
.truncationMode(.tail)
|
||||
if let name = model.activeEntry?.name {
|
||||
passTitle(name)
|
||||
.lineLimit(1)
|
||||
.truncationMode(.tail)
|
||||
} else {
|
||||
Text("No session selected")
|
||||
.font(.system(size: 13, weight: .semibold, design: DesignTokens.serif))
|
||||
.foregroundStyle(DesignTokens.muted)
|
||||
.help("Select a session in the roster — or start a new conversation from the + above.")
|
||||
}
|
||||
Spacer()
|
||||
if let version = model.engineVersion {
|
||||
// Round-1 polish (sol#53/mercury#68): the engine version is its
|
||||
// own muted mono chunk on the fg-wash card. Round-6 (sol#35):
|
||||
// fixedSize — the badge never truncates or compresses; the
|
||||
// TITLE's budget accounts for it and truncates first.
|
||||
Text(version)
|
||||
.font(.system(size: 11, design: DesignTokens.mono))
|
||||
.foregroundStyle(DesignTokens.muted)
|
||||
.lineLimit(1)
|
||||
.fixedSize()
|
||||
.padding(.horizontal, 7)
|
||||
.padding(.vertical, 2)
|
||||
.background(
|
||||
RoundedRectangle(cornerRadius: 5)
|
||||
.fill(DesignTokens.foreground.opacity(0.06)),
|
||||
)
|
||||
.help(model.versionNote ?? version)
|
||||
}
|
||||
Spacer()
|
||||
// #30 (S2.7): the draft candidate prefills the ask sheet (empty
|
||||
// transcript → empty prefill — the sheet still works). Reads
|
||||
// in-memory transcript rows — engine-free, never gated on
|
||||
// canStartSession (a crashed engine must not strand the draft).
|
||||
Button("New Post") {
|
||||
sessionDraft = model.draftCandidateText() ?? ""
|
||||
HStack(spacing: 8) {
|
||||
Button("New Post") {
|
||||
sessionDraft = model.draftCandidateText() ?? ""
|
||||
}
|
||||
.buttonStyle(BorderedSecondaryButtonStyle(minHeight: 28))
|
||||
.disabled(model.activeEntry == nil)
|
||||
.help("Start a Post from this session's draft candidate")
|
||||
.accessibilityLabel("New Post from Session")
|
||||
Button("Fork") {
|
||||
Task { _ = await model.forkActiveSession() }
|
||||
}
|
||||
.buttonStyle(BorderedSecondaryButtonStyle(minHeight: 28))
|
||||
.disabled(!model.canStartSession || model.activeEntry == nil)
|
||||
.help("Copy this session's history into a new one")
|
||||
.accessibilityLabel("Fork session")
|
||||
Button("Compact") {
|
||||
Task { await model.compactActiveSession() }
|
||||
}
|
||||
.buttonStyle(BorderedSecondaryButtonStyle(minHeight: 28))
|
||||
.disabled(!model.canStartSession || model.activeEntry == nil)
|
||||
.help("Compress the session's history")
|
||||
.accessibilityLabel("Compact session")
|
||||
}
|
||||
.buttonStyle(.bordered)
|
||||
.controlSize(.small)
|
||||
.font(.system(size: 11, design: DesignTokens.mono))
|
||||
.help("Start a Post from this session's draft candidate")
|
||||
.accessibilityLabel("New Post from Session")
|
||||
Button("Fork") {
|
||||
Task { _ = await model.forkActiveSession() }
|
||||
}
|
||||
.buttonStyle(.bordered)
|
||||
.controlSize(.small)
|
||||
.font(.system(size: 11, design: DesignTokens.mono))
|
||||
.disabled(!model.canStartSession)
|
||||
.help("Copy this session's history into a new one")
|
||||
.accessibilityLabel("Fork session")
|
||||
Button("Compact") {
|
||||
Task { await model.compactActiveSession() }
|
||||
}
|
||||
.buttonStyle(.bordered)
|
||||
.controlSize(.small)
|
||||
.font(.system(size: 11, design: DesignTokens.mono))
|
||||
.disabled(!model.canStartSession)
|
||||
.help("Compress the session's history")
|
||||
.accessibilityLabel("Compact session")
|
||||
.padding(.leading, 4)
|
||||
}
|
||||
.padding(.horizontal, 12)
|
||||
.padding(.vertical, 8)
|
||||
.padding(.horizontal, 16)
|
||||
.frame(minHeight: 52)
|
||||
}
|
||||
|
||||
/// Round-5 (argon#47): the conversation title — a 'Pass N ·' prefix
|
||||
/// renders in muted beside the ink remainder (one line, two tones); any
|
||||
/// other name renders whole in ink. The serif face applies per piece:
|
||||
/// a concatenated Text keeps each segment's own styling.
|
||||
private func passTitle(_ name: String) -> Text {
|
||||
if let separator = name.range(of: " · "), Self.isPassLabel(String(name[..<separator.lowerBound])) {
|
||||
return Text(String(name[..<separator.upperBound]))
|
||||
.font(.system(size: 13, weight: .semibold, design: DesignTokens.serif))
|
||||
.foregroundStyle(DesignTokens.muted)
|
||||
+ Text(String(name[separator.upperBound...]))
|
||||
.font(.system(size: 13, weight: .semibold, design: DesignTokens.serif))
|
||||
.foregroundStyle(DesignTokens.foreground)
|
||||
}
|
||||
return Text(name)
|
||||
.font(.system(size: 13, weight: .semibold, design: DesignTokens.serif))
|
||||
.foregroundStyle(DesignTokens.foreground)
|
||||
}
|
||||
|
||||
/// The pass transcripts' label heads — "Pass 1"/"Pass 12" (the model's
|
||||
/// `runOneShot` names; "Blind Read" keeps its plain-ink title).
|
||||
private static func isPassLabel(_ head: String) -> Bool {
|
||||
let digits = head.dropFirst("Pass ".count)
|
||||
return head.hasPrefix("Pass ") && !digits.isEmpty && digits.allSatisfy(\.isNumber)
|
||||
}
|
||||
|
||||
/// No second restart flow — the Mentor pane owns Restart (map decision).
|
||||
@@ -202,12 +276,14 @@ struct SessionsPaneView: View {
|
||||
VStack(spacing: 8) {
|
||||
Image(systemName: "exclamationmark.triangle.fill")
|
||||
.font(.system(size: 30))
|
||||
.foregroundStyle(.orange)
|
||||
// Round-2 polish: the #156 danger voice — terracotta, never
|
||||
// the system orange (two-color world, no stray hues).
|
||||
.foregroundStyle(DesignTokens.accent)
|
||||
Text("The mentor engine stopped")
|
||||
.font(.title3)
|
||||
Text("Restart it in the Mentor pane — nothing is lost.")
|
||||
.font(.callout)
|
||||
.foregroundStyle(.secondary)
|
||||
.foregroundStyle(DesignTokens.muted)
|
||||
}
|
||||
.padding(24)
|
||||
.frame(maxWidth: .infinity, maxHeight: .infinity)
|
||||
@@ -230,11 +306,26 @@ struct SessionsPaneView: View {
|
||||
Task { _ = await model.beginBrainstormSession(pieceTitle: piece?.title) }
|
||||
}
|
||||
} label: {
|
||||
// Round-4 (mercury#57): accent tint + a heavier weight — the
|
||||
// plus reads as a control, not decoration. Round-7 (argon#39):
|
||||
// a real 24×24 hit-target container in the header pill family
|
||||
// (surface fill + hairline, BorderedSecondaryButtonStyle's
|
||||
// shape) — the bare glyph had no target.
|
||||
Image(systemName: "plus")
|
||||
.font(.system(size: 13, weight: .medium))
|
||||
.frame(width: 24, height: 24)
|
||||
.background(
|
||||
RoundedRectangle(cornerRadius: 8)
|
||||
.fill(DesignTokens.surface),
|
||||
)
|
||||
.overlay(
|
||||
RoundedRectangle(cornerRadius: 8)
|
||||
.strokeBorder(DesignTokens.border, lineWidth: 1),
|
||||
)
|
||||
}
|
||||
.menuStyle(.button)
|
||||
.buttonStyle(.plain)
|
||||
.foregroundStyle(DesignTokens.muted)
|
||||
.foregroundStyle(DesignTokens.accent)
|
||||
.disabled(!model.canStartSession)
|
||||
.help("New Session")
|
||||
.accessibilityLabel("New Session")
|
||||
@@ -268,7 +359,9 @@ struct SessionsPaneView: View {
|
||||
VStack(spacing: 8) {
|
||||
Text("No sessions yet.")
|
||||
.font(.system(size: 16, weight: .semibold, design: DesignTokens.serif))
|
||||
Text("Sessions arrive when a pass runs — or create one with New Session.")
|
||||
// Round-4 (all seats): 'select a session or start a new
|
||||
// conversation' — the empty canvas names its way out.
|
||||
Text("Start a new conversation with the + above — sessions also arrive when a pass runs.")
|
||||
.font(.callout)
|
||||
.foregroundStyle(DesignTokens.muted)
|
||||
.multilineTextAlignment(.center)
|
||||
@@ -278,28 +371,43 @@ struct SessionsPaneView: View {
|
||||
}
|
||||
}
|
||||
|
||||
/// One roster row: the library's post-row language — always-2px leading
|
||||
/// strip (accent when selected, no layout shift), surface card + border on
|
||||
/// selection, serif name over mono-11 meta (piece · created).
|
||||
/// One roster row: the library's post-row language — selected strip as a
|
||||
/// vertically-inset rounded capsule (round-6: part of the card, not
|
||||
/// pasted on), surface card + border on selection, serif name over
|
||||
/// mono-11 meta (piece · created).
|
||||
private struct SessionEntryRow: View {
|
||||
let entry: SessionEntry
|
||||
let selected: Bool
|
||||
let select: () -> Void
|
||||
@State private var hover = false
|
||||
|
||||
/// Round-6 (mercury#4/sol#9 — verified against the r5 live shot):
|
||||
/// the title budget is GLYPH-MEASURED. The r5 ~58-char estimate
|
||||
/// overshot the real two-line capacity of this width (the r5 shot
|
||||
/// still cut mid-word — "Markd…"); the seam now measures the row's
|
||||
/// exact serif-13 semibold face, so its output is what renders and
|
||||
/// lineLimit(2) below is a no-op safety net.
|
||||
/// libraryWidth 232 − 16 row pads − 14 leading (2 strip + 12 gap) −
|
||||
/// 10 trailing.
|
||||
private static let titleWidth = DesignTokens.libraryWidth - 16 - 14 - 10
|
||||
|
||||
var body: some View {
|
||||
// Same shape as LibraryPostRow: leading corners square (the 2px
|
||||
// strip), trailing rounded 8 — only the strip's COLOR changes.
|
||||
// Same card shape as LibraryPostRow; the selected strip rides as
|
||||
// an inset capsule overlay (below) — only the strip's LANGUAGE
|
||||
// diverges from LibraryPostRow, by the round-6 ruling.
|
||||
let shape = UnevenRoundedRectangle(cornerRadii: .init(bottomTrailing: 8, topTrailing: 8))
|
||||
return Button(action: select) {
|
||||
HStack(spacing: 0) {
|
||||
Rectangle()
|
||||
.fill(selected ? DesignTokens.accent : .clear)
|
||||
.frame(width: 2)
|
||||
VStack(alignment: .leading, spacing: 5) {
|
||||
Text(entry.name)
|
||||
// Round-4 (sol#7/argon#15 tail/mercury#30): two lines
|
||||
// — titles stop dying mid-word; meta stays below.
|
||||
// Round-5 (mercury#4/sol#8/argon#16): word-boundary
|
||||
// truncation replaces the native mid-word tail cut.
|
||||
// Round-6: glyph-measured (see titleWidth) — the seam
|
||||
// decides, lineLimit(2) is the safety net.
|
||||
Text(WordBoundaryTruncation.fit(entry.name, maxWidth: Self.titleWidth))
|
||||
.font(.system(size: 13, weight: .semibold, design: DesignTokens.serif))
|
||||
.lineLimit(1)
|
||||
.lineLimit(2)
|
||||
.truncationMode(.tail)
|
||||
.frame(maxWidth: .infinity, alignment: .leading)
|
||||
Text(metaLine)
|
||||
@@ -309,7 +417,12 @@ private struct SessionEntryRow: View {
|
||||
.truncationMode(.tail)
|
||||
.frame(maxWidth: .infinity, alignment: .leading)
|
||||
}
|
||||
.padding(10)
|
||||
// Round-5 (sol#38/mercury#54) kept 10-12pt between the
|
||||
// strip and the text; the overlay strip (2pt) + 12pt
|
||||
// internal leading = 14 off the card edge — same seat.
|
||||
.padding(.leading, 14)
|
||||
.padding(.trailing, 10)
|
||||
.padding(.vertical, 10)
|
||||
}
|
||||
.clipShape(shape)
|
||||
}
|
||||
@@ -320,10 +433,28 @@ private struct SessionEntryRow: View {
|
||||
shape
|
||||
.fill(selected ? DesignTokens.surface : DesignTokens.foreground.opacity(0.06))
|
||||
.overlay(
|
||||
shape.strokeBorder(selected ? DesignTokens.border : .clear),
|
||||
// Round-5 (sol#38/mercury#54): the selected border one
|
||||
// step stronger than the hairline — the #D8D4CC-equivalent
|
||||
// darkening of border #E8E5DF via foreground opacity
|
||||
// (no new hex) — so selection reads at a glance.
|
||||
shape.strokeBorder(
|
||||
selected ? DesignTokens.foreground.opacity(0.17) : .clear,
|
||||
),
|
||||
)
|
||||
}
|
||||
}
|
||||
// Round-6 (mercury#25/49, argon#13): the selected strip is a
|
||||
// vertically-inset rounded capsule — 7pt off the card's top and
|
||||
// bottom, 2pt wide, radius 1 — reading as part of the card, not
|
||||
// a full-height strip pasted beside it.
|
||||
.overlay(alignment: .leading) {
|
||||
if selected {
|
||||
RoundedRectangle(cornerRadius: 1)
|
||||
.fill(DesignTokens.accent)
|
||||
.frame(width: 2)
|
||||
.padding(.vertical, 7)
|
||||
}
|
||||
}
|
||||
.padding(.horizontal, 8)
|
||||
.onHover { hover = $0 }
|
||||
.help(entry.name)
|
||||
@@ -331,8 +462,12 @@ private struct SessionEntryRow: View {
|
||||
}
|
||||
|
||||
private var metaLine: String {
|
||||
// Round-2 polish (argon#23/sol#13/mercury#61): display-level dedupe
|
||||
// — when the session name already carries the piece title, the meta
|
||||
// line shows the timestamp only. Contract otherwise unchanged
|
||||
// (pieceTitle · yyyy-MM-dd HH:mm).
|
||||
var parts: [String] = []
|
||||
if let piece = entry.pieceTitle {
|
||||
if let piece = entry.pieceTitle, !entry.name.contains(piece) {
|
||||
parts.append(piece)
|
||||
}
|
||||
parts.append(FixCardStore.formatted(entry.createdAt, "yyyy-MM-dd HH:mm"))
|
||||
@@ -340,59 +475,95 @@ private struct SessionEntryRow: View {
|
||||
}
|
||||
}
|
||||
|
||||
/// #30 (S2.7): the New Post from Session ask — the draft candidate
|
||||
/// (the session's last mentor reply) prefills and stays editable.
|
||||
/// Confirm materializes the piece stamped `origin: ai` — never
|
||||
/// stats-eligible — and opens it in Posts; Cancel writes nothing.
|
||||
/// The error line is SHEET-LOCAL state: it dies with the presentation,
|
||||
/// so a re-opened ask never shows a stale refusal.
|
||||
private struct NewPostFromSessionSheet: View {
|
||||
@Binding var draft: String
|
||||
/// The confirm's failure reason — a failed write keeps the sheet up,
|
||||
/// draft intact; dismissal itself is the cancel.
|
||||
@State private var createError: String?
|
||||
let create: () -> String?
|
||||
/// Round-5 (mercury#4/sol#8/argon#16) → Round-6 glyph-measured
|
||||
/// (mercury#4/sol#9) → Round-7 middle-eliding (argon#14/sol#32/47) →
|
||||
/// Round-8 head-biased split (sol#3): pure title truncation —
|
||||
/// SwiftUI's tail truncation cuts mid-word ("Markd…") and, worse,
|
||||
/// killed the roster's shared titles at their common boilerplate. The
|
||||
/// seam measures the row's exact face (system serif 13 semibold,
|
||||
/// through NSFont) against `maxWidth`; an overflowing title splits
|
||||
/// HEAD-BIASED (~60/40): the head keeps a leading label ("Pass N ·")
|
||||
/// plus as many following words as fit in ~60% of the glyph budget —
|
||||
/// the unique topic survives — then the LONGEST word suffix that still
|
||||
/// fits (r7 kept only the label and maximized the tail; the topic died
|
||||
/// either way). Pinned in WordBoundaryTruncationTests.
|
||||
nonisolated enum WordBoundaryTruncation {
|
||||
/// The roster title's exact face — SessionEntryRow's serif-13
|
||||
/// semibold system design, the same metrics SwiftUI's Text renders.
|
||||
/// nonisolated(unsafe) — NSFont isn't Sendable (ProseTypography
|
||||
/// precedent); the descriptor-built font is effectively immutable.
|
||||
private nonisolated(unsafe) static let titleFont: NSFont = {
|
||||
let base = NSFont.systemFont(ofSize: 13, weight: .semibold)
|
||||
return base.fontDescriptor.withDesign(.serif).flatMap { NSFont(descriptor: $0, size: 13) } ?? base
|
||||
}()
|
||||
|
||||
var body: some View {
|
||||
VStack(alignment: .leading, spacing: 12) {
|
||||
Text("New Post from Session")
|
||||
.font(.system(size: 15, weight: .semibold, design: DesignTokens.serif))
|
||||
Text(
|
||||
"The draft starts from the session's last mentor reply. It lands in your library stamped origin: ai — never counted as your writing.",
|
||||
)
|
||||
.font(.callout)
|
||||
.foregroundStyle(DesignTokens.muted)
|
||||
TextEditor(text: $draft)
|
||||
.font(.system(size: 13, design: DesignTokens.serif))
|
||||
.frame(width: 380, height: 160)
|
||||
.overlay(
|
||||
RoundedRectangle(cornerRadius: DesignTokens.radius)
|
||||
.strokeBorder(DesignTokens.border),
|
||||
)
|
||||
if let createError {
|
||||
Text(createError)
|
||||
.font(.system(size: 11, design: DesignTokens.mono))
|
||||
.foregroundStyle(.orange)
|
||||
}
|
||||
HStack {
|
||||
Spacer()
|
||||
// Dismissal itself is the cancel (binding set(false) →
|
||||
// sessionDraft = nil) — Esc and ⌘. land here too.
|
||||
Button("Cancel", role: .cancel) {}
|
||||
.keyboardShortcut(.cancelAction)
|
||||
Button("Create Post", action: confirm)
|
||||
.keyboardShortcut(.defaultAction)
|
||||
.disabled(draft.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty)
|
||||
private static let spaceWidth = NSAttributedString(
|
||||
string: " ", attributes: [.font: titleFont],
|
||||
).size().width
|
||||
|
||||
/// Round-8 (sol#3): the head's share of the lines × maxWidth glyph
|
||||
/// budget — bias the split toward the unique head, away from the
|
||||
/// shared tail.
|
||||
private static let headShare: CGFloat = 0.6
|
||||
|
||||
private static func width(_ text: String) -> CGFloat {
|
||||
NSAttributedString(string: text, attributes: [.font: titleFont]).size().width
|
||||
}
|
||||
|
||||
/// Passthrough when `text` wraps within `lines` × `maxWidth`;
|
||||
/// otherwise (round-8, sol#3) MIDDLE-elide with a head-biased
|
||||
/// (~60/40) split — the head keeps a leading "Label ·" trio when the
|
||||
/// name carries one (the roster's "Pass N ·" shape) plus following
|
||||
/// words up to `headShare` of the budget; the tail keeps the LONGEST
|
||||
/// word suffix that still fits: more of the unique topic, less of
|
||||
/// the shared boilerplate. When no suffix fits beside the head, the
|
||||
/// head truncates alone (the r5/r6 shape — the ellipsis binds to its
|
||||
/// last word); a word wider than the line keeps its own line
|
||||
/// (lineLimit catches it).
|
||||
static func fit(_ text: String, maxWidth: CGFloat, lines: Int = 2) -> String {
|
||||
let words = text.split(separator: " ", omittingEmptySubsequences: true).map(String.init)
|
||||
guard !words.isEmpty, lineCount(words, maxWidth) > lines else { return text }
|
||||
var head = [words[0]]
|
||||
if words.count > 2, words[2] == "·" {
|
||||
head = [words[0], words[1], words[2]]
|
||||
}
|
||||
let budget = CGFloat(lines) * maxWidth * headShare
|
||||
// Grow the head into its budget — always leaving the tail at
|
||||
// least one word to keep (a head that consumed everything is
|
||||
// just the r5 prefix cut again).
|
||||
var index = head.count
|
||||
while index < words.count - 1, width((head + [words[index]]).joined(separator: " ")) <= budget {
|
||||
head.append(words[index])
|
||||
index += 1
|
||||
}
|
||||
let tail = Array(words.dropFirst(head.count))
|
||||
for count in stride(from: tail.count, through: 1, by: -1) {
|
||||
let candidate = head.joined(separator: " ") + " … " + tail.suffix(count).joined(separator: " ")
|
||||
if lineCount(candidate.split(separator: " ").map(String.init), maxWidth) <= lines {
|
||||
return candidate
|
||||
}
|
||||
}
|
||||
.padding(20)
|
||||
.frame(width: 420)
|
||||
return head.joined(separator: " ") + "…"
|
||||
}
|
||||
|
||||
/// No dismiss() here — success closes the sheet via the pane's
|
||||
/// `sessionDraft = nil` (the binding); failure keeps it up with the
|
||||
/// inline reason and the edited draft intact.
|
||||
private func confirm() {
|
||||
createError = create()
|
||||
/// Greedy word wrap (the text system's own algorithm): how many
|
||||
/// lines `words` need at `maxWidth`.
|
||||
private static func lineCount(_ words: [String], _ maxWidth: CGFloat) -> Int {
|
||||
var lines = 1
|
||||
var used: CGFloat = 0
|
||||
for word in words {
|
||||
let tokenWidth = width(word)
|
||||
if used == 0 {
|
||||
// A word wider than the line keeps its own line — the
|
||||
// mid-word break is SwiftUI's, never the seam's.
|
||||
used = tokenWidth
|
||||
} else if used + spaceWidth + tokenWidth <= maxWidth {
|
||||
used += spaceWidth + tokenWidth
|
||||
} else {
|
||||
lines += 1
|
||||
used = tokenWidth
|
||||
}
|
||||
}
|
||||
return lines
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user