// A face's longer words a page at a time: the room the face leaves them is// measured off the typewriter at the face's size (`FaceRoom`), the page// cutter the app installed cuts the text into pages for it// (`facePageCutter`), and a sideways swipe turns them, the page's label// ("2 / 3") at the room's foot. A text that fits is one page, drawn as it// is, with no label and no swipe; with no cutter installed the whole text is// one page.//// THE CUTTER. A face does not know who cuts its pages: the app installs the// core's cut (CottonUI's `CorePageCutter`, wherever it installs the core),// the Loom app and the face shots a plain one of their own// (`PlainPageCutter`). Every word is the cutter's.importCottonPaperimportSwiftUIenumFacePagesKnobs {/// How far a swipe travels before it turns a page.staticlet turn: CGFloat = 40staticlet labelSize: CGFloat = 10.5/// The label's line under the pages.staticlet labelRoom: CGFloat = 18}/// Cuts a face's words into pages of `first` lines, then `rest`, of/// `columns` characters, labelled "2 / 3".publicprotocolFacePageCutter: Sendable {func cut(_ text: String, columns: Int, first: Int, rest: Int) async -> [NotePage]}extensionEnvironmentValues {/// Who cuts a face's words into pages; nil leaves them one page. @Entry publicvar facePageCutter: (any FacePageCutter)? = nil}/// A cut by the words alone, with no core: each line wrapped at its/// characters, between words where it can, and the lines dealt into pages./// What the specimens are cut by in the Loom app and the face shots.publicstructPlainPageCutter: FacePageCutter {publicinit() {}publicfunc cut(_ text: String, columns: Int, first: Int, rest: Int) async -> [NotePage] {Self.pages(text, columns: columns, first: first, rest: rest) }staticfunc pages(_ text: String, columns: Int, first: Int, rest: Int) -> [NotePage] {let lines = text.components(separatedBy: "\n").flatMap { wrap($0, columns: max(1, columns)) }var cut: [[String]] = []var left = lines[...]while !left.isEmpty {let room = max(1, cut.isEmpty ? first : rest) cut.append(Array(left.prefix(room))) left = left.dropFirst(room) }guard cut.count > 1else { return [NotePage(text: text, label: "")] }return cut.enumerated().map { index, page inNotePage(text: page.joined(separator: "\n"), label: "\(index + 1) / \(cut.count)") } }/// One line wrapped at `columns` characters, between words where a word fits.staticfunc wrap(_ line: String, columns: Int) -> [String] {var wrapped: [String] = []var current = ""for word in line.split(separator: " ", omittingEmptySubsequences: false).map(String.init) {var word = wordwhile word.count > columns {if !current.isEmpty { wrapped.append(current); current = "" } wrapped.append(String(word.prefix(columns))) word = String(word.dropFirst(columns)) }if current.isEmpty { current = word } elseif current.count + 1 + word.count <= columns { current += " " + word } else { wrapped.append(current) current = word } } wrapped.append(current)return wrapped }}publicstructFacePages: View {let text: Stringlet size: CGFloatvar leading: CGFloat = 4var ink: PaperInk = .ink/// The face the words are set in (a business's reading face), the/// typewriter unless named.var face: ThemeFace = .courier/// A colour the words are set in instead of the ink (a business's body).var colour: Color?/// The words are Markdown: each page is set (`FaceMarkdown`), never/// typed with its marks.var markdown = false @Environment(\.facePageCutter) privatevar cutter @Environment(\.theme) privatevar theme @State privatevar pages: [NotePage] = [] @State privatevar page = 0 @Environment(\.deckPassOn) privatevar passOnpublicinit( text: String, size: CGFloat, leading: CGFloat = 4, ink: PaperInk = .ink, face: ThemeFace = .courier, colour: Color? = nil, markdown: Bool = false ) {self.text = textself.size = sizeself.leading = leadingself.ink = inkself.face = faceself.colour = colourself.markdown = markdown }publicvar body: someView {GeometryReader { room inlet shown = pages.isEmpty ? [NotePage(text: text, label: "")] : pageslet current = shown[min(page, shown.count - 1)] VStack(alignment: .leading, spacing: 0) {Text(markdown ? FaceMarkdown.print(FaceMarkdown.Document(text, theme: theme).pages(shown.map(\.text))[min(page, shown.count - 1)], size: size, font: face == .courier ? CottonFonts.type(size) : face.font(size)) : AttributedString(current.text)) .font(face == .courier ? CottonFonts.type(size) : face.font(size)) .lineSpacing(leading) .modifier(PagesInk(ink: ink, colour: colour)) .frame(maxWidth: .infinity, alignment: .topLeading) .accessibilityIdentifier("face-page")Spacer(minLength: 0)if shown.count > 1 {Text(current.label) .font(CottonFonts.type(FacePagesKnobs.labelSize)) .faceInk(.pencil) .frame(maxWidth: .infinity, alignment: .trailing) .accessibilityIdentifier("face-page-label") } } .clipped() .contentShape(Rectangle())// A tap turns to the next page, the last back to the first; a// swipe turns either way, and past either end moves card. .onTapGesture { if shown.count > 1 { page = (page + 1) % shown.count } } .facePages(shown.count > 1 ? turning(count: shown.count) : nil, at: "\(page)/\(shown.count)") { forward inlet next = page + (forward ? 1 : -1)if (0..<shown.count).contains(next) { page = next } else { passOn?(forward) } } .animation(.easeOut(duration: 0.18), value: page) .task(id: "\(text.count)|\(text.hashValue)|\(face.rawValue)|\(Int(room.size.width))x\(Int(room.size.height))") {guardlet cutter, room.size.height > 0else { return }// The label takes its line only when there is more than one page.let whole = FaceRoom.measure(width: room.size.width, height: room.size.height, size: size, leading: leading, face: face)let paged = FaceRoom.measure( width: room.size.width, height: room.size.height - FacePagesKnobs.labelRoom, size: size, leading: leading, face: face)let lines = text.count <= whole.columns * whole.lines ? whole.lines : paged.lineslet cut = await cutter.cut(text, columns: whole.columns, first: lines, rest: lines)// A cut for a room since left is never shown (`NoteCard`).guard !Task.isCancelled else { return } pages = cut page = min(page, max(0, pages.count - 1)) } } }privatefunc turning(count: Int) -> someGesture {DragGesture(minimumDistance: 16) .onEnded { end inlet dx = end.translation.widthguard abs(dx) > FacePagesKnobs.turn, abs(dx) > abs(end.translation.height) else { return }let next = page + (dx < 0 ? 1 : -1)if (0..<count).contains(next) { page = next } else { passOn?(dx < 0) } } }}/// A page's words in their ink, or in a colour a business's look names.privatestructPagesInk: ViewModifier {let ink: PaperInklet colour: Color? @Environment(\.theme) privatevar themefunc body(content: Content) -> someView {iflet colour { content.foregroundStyle(colour).blendMode(theme.printBlend) } else { content.faceInk(ink) } }}