import SwiftUI /// A record page's title, drawn in at most `page_title_lines` lines and never /// cut mid-word (architect, 13 Oct 2026, from the owner's todo page): a longer /// title keeps the most whole words that fit, then an ellipsis. The whole text /// stays the record's title — read in full in its content and details. /// /// SwiftUI's own tail truncation cuts at a character, so the cut is chosen /// here: `ViewThatFits` draws the first of the whole title and its word /// prefixes whose height fits the height of that many lines of the same type, /// measured from a hidden reference of the same style. struct PaperTitleLines: View { @Environment(\.theme) private var theme let title: String /// The title's type and ink, applied to every candidate and the reference. let style: (Text) -> Styled var body: some View { let lines = Int(PaperTitleLinesKnobs.pageTitleLines) let candidates = Self.candidates(title, wordsConsidered: Int(PaperTitleLinesKnobs.pageTitleWordsConsidered)) let reference = style(Text(Array(repeating: "X", count: max(lines, 1)).joined(separator: "\n"))) drawn(lines: lines, candidates: candidates, reference: reference) } private func drawn(lines: Int, candidates: [String], reference: Styled) -> some View { LinesHigh { reference .fixedSize(horizontal: false, vertical: true) .hidden() .accessibilityHidden(true) ViewThatFits(in: .vertical) { ForEach(Array(candidates.enumerated()), id: \.offset) { _, words in style(Text(words)).fixedSize(horizontal: false, vertical: true) } // One word longer than the lines: nothing whole fits, so the // word is cut — the one case the type cannot avoid. style(Text(candidates.last ?? title)).lineLimit(max(lines, 1)) } .accessibilityLabel(title) } } /// The whole title, then each shorter prefix of whole words with an /// ellipsis, longest first. Only the first `wordsConsidered` words are /// tried: no title longer than that fits a page's few lines anyway. The /// covers' rule holds for a page's head too (3281edbeb; 295 E rule 12): a /// title with no space — a file's name — is words between the marks that /// join it (`CoverTitle.joins`), cut after one of them, and every /// candidate may break after a mark (`CoverTitle.breakable`), so it wraps /// to its two lines before it is cut. static func candidates(_ title: String, wordsConsidered: Int) -> [String] { let whole = title.trimmingCharacters(in: .whitespacesAndNewlines) let spaced = whole.split(whereSeparator: \.isWhitespace).map(String.init) let words = spaced.count > 1 ? spaced : joined(whole) let separator = spaced.count > 1 ? " " : "" guard words.count > 1 else { return [CoverTitle.breakable(whole)] } let considered = min(words.count - 1, max(wordsConsidered, 1)) return ([whole] + (1...considered).reversed().map { words.prefix($0).joined(separator: separator) + "\u{2026}" }) .map(CoverTitle.breakable) } /// A word with no space as the pieces its marks join, each mark kept /// with the piece before it ("scan_" "2019_" "final.pdf"). static func joined(_ word: String) -> [String] { var pieces: [String] = [] var piece = "" for character in word { piece.append(character) if CoverTitle.joins.contains(character) { pieces.append(piece) piece = "" } } if !piece.isEmpty { pieces.append(piece) } return pieces } } /// Two children: a hidden reference whose height is the most the title may /// take, and the title, offered the reference's height at the same width. private struct LinesHigh: Layout { func sizeThatFits(proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) -> CGSize { guard let reference = subviews.first, let drawn = subviews.last, subviews.count > 1 else { return .zero } let width = proposal.width let high = reference.sizeThatFits(ProposedViewSize(width: width, height: nil)).height let title = drawn.sizeThatFits(ProposedViewSize(width: width, height: high)) return CGSize(width: width ?? title.width, height: min(title.height, high)) } func placeSubviews(in bounds: CGRect, proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) { guard let reference = subviews.first, let drawn = subviews.last, subviews.count > 1 else { return } let high = reference.sizeThatFits(ProposedViewSize(width: bounds.width, height: nil)).height reference.place(at: bounds.origin, proposal: ProposedViewSize(width: bounds.width, height: high)) drawn.place(at: bounds.origin, proposal: ProposedViewSize(width: bounds.width, height: high)) } } /// The sizes, counts and timings PaperTitleLines draws with: Swift's own, beside /// their reader — a theme sets only its tokens (`scale.tokens`). enum PaperTitleLinesKnobs { static let pageTitleLines: CGFloat = 2 static let pageTitleWordsConsidered: CGFloat = 60 }