oficina / docs Public

Clone
git clone https://amadan.net/oficina/docs

Plain git — no account needed to clone.

Download

Download this file

1import SwiftUI
2
3@MainActor
4struct DocsToolsControls: View {
5 /// Read when the word count opens, never in the body: the counts come
6 /// from the live text, not from a render (spec 2026-11-22 §7 Design 9).
7 let model: DocsModel
8 @State private var showingCount = false
9 @State private var showingShortcuts = false
10
11 var body: some View {
12 Menu {
13 Button { showingCount = true } label: {
14 Label { Text("docs.editor.wordcount.title", bundle: DocsResources.bundle) } icon: { Image(systemName: "number") }
15 }
16 Button { showingShortcuts = true } label: {
17 Label { Text("docs.editor.shortcuts.title", bundle: DocsResources.bundle) } icon: { Image(systemName: "keyboard") }
18 }
19 } label: {
20 Label { Text("docs.editor.menu.tools", bundle: DocsResources.bundle) } icon: { Image(systemName: "wrench.and.screwdriver") }
21 }
22 .sheet(isPresented: $showingShortcuts) { DocsShortcutsSheet { showingShortcuts = false } }
23 .sheet(isPresented: $showingCount) { DocsLiveWordCountSheet(model: model) { showingCount = false } }
24 }
25
26}
27
28/// Keyboard shortcuts, as a sheet: Tools on the iPhone, Help ▸ Keyboard
29/// shortcuts… on the Mac.
30@MainActor struct DocsShortcutsSheet: View {
31 let close: () -> Void
32 var body: some View {
33 NavigationStack {
34 Form {
35 ForEach(DocumentKeyboardBindings.all) { binding in
36 LabeledContent {
37 Text(binding.display).monospaced()
38 } label: {
39 Text(LocalizedStringKey(binding.titleKey), bundle: DocsResources.bundle)
40 }
41 }
42 }
43 .navigationTitle(Text("docs.editor.shortcuts.title", bundle: DocsResources.bundle))
44 .toolbar {
45 ToolbarItem(placement: .confirmationAction) {
46 Button { close() } label: { Text("docs.editor.shortcuts.close", bundle: DocsResources.bundle) }
47 }
48 }
49 }
50 #if os(macOS)
51 .frame(minWidth: 420, minHeight: 420)
52 #endif
53 }
54}
55
56/// Word count, as a sheet: Tools on the iPhone, Tools ▸ Word count… on the Mac.
57@MainActor struct DocsWordCountSheet: View {
58 let projection: CRDTProjection
59 let content: NSAttributedString
60 let selection: NSRange
61 let close: () -> Void
62 var body: some View {
63 let count = DocumentWordCounts(projection: projection, content: content, selection: selection)
64 NavigationStack {
65 Form {
66 row("docs.editor.wordcount.words", count.words)
67 row("docs.editor.wordcount.characters", count.characters)
68 row("docs.editor.wordcount.charactersnospaces", count.charactersNoSpaces)
69 row("docs.editor.wordcount.paragraphs", count.paragraphs)
70 if let selected = count.selectedWords { row("docs.editor.wordcount.selection", selected) }
71 }
72 .navigationTitle(Text("docs.editor.wordcount.title", bundle: DocsResources.bundle))
73 .toolbar {
74 ToolbarItem(placement: .confirmationAction) {
75 Button { close() } label: { Text("docs.editor.wordcount.close", bundle: DocsResources.bundle) }
76 }
77 }
78 }
79 #if os(macOS)
80 .frame(minWidth: 360, minHeight: 290)
81 #else
82 .presentationDetents([.medium])
83 #endif
84 }
85 private func row(_ key: LocalizedStringKey, _ value: Int) -> some View {
86 LabeledContent { Text(value, format: .number).monospacedDigit() } label: { Text(key, bundle: DocsResources.bundle) }
87 }
88}
89
90/// Word count, live: the counts of the document as it is now, recomputed
91/// as it changes (Tools ▸ Word count… on the Mac, Tools on the iPhone). It
92/// watches the model's content revision, which moves after each draw, and
93/// counts from the projection's text and the text view's own storage: no
94/// render, so a publication costs the count's walk, not a whole render.
95@MainActor struct DocsLiveWordCountSheet: View {
96 let model: DocsModel
97 let close: () -> Void
98 var body: some View {
99 let _ = model.contentRevision
100 if let projection = model.projection {
101 DocsWordCountSheet(projection: projection, content: model.surface.content, selection: model.selection, close: close)
102 }
103 }
104}
105