| 1 | #if os(iOS) |
| 2 | import RastrilloApple |
| 3 | import SwiftUI |
| 4 | |
| 5 | /// What the iPhone document's ••• menu and controls ask of the window. |
| 6 | struct DocsPhoneActions { |
| 7 | var share: () -> Void = {} |
| 8 | var rename: () -> Void = {} |
| 9 | var export: (String) -> Void = { _ in } |
| 10 | var wordCount: () -> Void = {} |
| 11 | var openDocument: () -> Void = {} |
| 12 | var shortcuts: () -> Void = {} |
| 13 | var colophon: () -> Void = {} |
| 14 | var delete: () -> Void = {} |
| 15 | var format: () -> Void = {} |
| 16 | } |
| 17 | |
| 18 | /// The bar's trailing controls (spec §3): the undo/redo pill, Aa and ••• |
| 19 | /// for a writer; ••• alone for a reader. No Share pill (Paul's ruling 4): |
| 20 | /// Share lives in •••. While the title is folded into the bar, undo and |
| 21 | /// redo move to the top of ••• so the title has the room (the |
| 22 | /// coordinator's ruling on #180). Their own view, reading the access, the |
| 23 | /// history, the connectivity and the fold, so the text view's host reads |
| 24 | /// none of them. |
| 25 | @MainActor struct DocsPhoneDocumentControls: View { |
| 26 | let model: DocsModel |
| 27 | let origin: URL |
| 28 | @Binding var paged: Bool |
| 29 | let header: DocumentHeaderState |
| 30 | let actions: DocsPhoneActions |
| 31 | var body: some View { |
| 32 | let folded = header.collapsed |
| 33 | HStack(spacing: DocsSpacing.sp2) { |
| 34 | if model.documentAccess == .writable { |
| 35 | if !folded { DocsPhoneUndoRedoPill(model: model) } |
| 36 | Button(action: actions.format) { |
| 37 | Text(verbatim: "Aa").font(.body.weight(.semibold)).frame(minWidth: 44, minHeight: 44) |
| 38 | } |
| 39 | .accessibilityLabel(Text("docs.editor.inspector.toggle", bundle: DocsResources.bundle)) |
| 40 | .accessibilityIdentifier("docs.format.toggle") |
| 41 | } |
| 42 | DocsPhoneMoreMenu(model: model, origin: origin, paged: $paged, actions: actions, history: folded) |
| 43 | } |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | /// Undo and redo in one pill, each enabled from the history. |
| 48 | @MainActor struct DocsPhoneUndoRedoPill: View { |
| 49 | let model: DocsModel |
| 50 | var body: some View { |
| 51 | let history = model.history |
| 52 | HStack(spacing: 0) { |
| 53 | button("docs.editor.toolbar.undo", icon: "arrow.uturn.backward", op: "undo", enabled: history.canUndo) |
| 54 | Rectangle().fill(OficinaPalette.line).frame(width: 1, height: 20) |
| 55 | button("docs.editor.toolbar.redo", icon: "arrow.uturn.forward", op: "redo", enabled: history.canRedo) |
| 56 | } |
| 57 | .accessibilityElement(children: .contain) |
| 58 | .accessibilityLabel(Text("docs.editor.toolbar.history", bundle: DocsResources.bundle)) |
| 59 | } |
| 60 | private func button(_ key: String, icon: String, op: String, enabled: Bool) -> some View { |
| 61 | Button { |
| 62 | DocumentCommands(model: model, content: model.surface.content).perform(op) |
| 63 | } label: { |
| 64 | Image(systemName: icon).font(.body.weight(.semibold)) |
| 65 | .flipsForRightToLeftLayoutDirection(true) |
| 66 | .frame(minWidth: 44, minHeight: 44) |
| 67 | } |
| 68 | .disabled(!enabled) |
| 69 | .accessibilityLabel(Text(LocalizedStringKey(key), bundle: DocsResources.bundle)) |
| 70 | .accessibilityIdentifier("docs." + op) |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | /// ••• (spec §3): the web's File, View, Tools and Help, in that order. What |
| 75 | /// a capability forbids is absent, not disabled; what needs the server waits |
| 76 | /// while the origin is offline (disabled, as the Mac's menus are). |
| 77 | @MainActor struct DocsPhoneMoreMenu: View { |
| 78 | let model: DocsModel |
| 79 | let origin: URL |
| 80 | @Binding var paged: Bool |
| 81 | let actions: DocsPhoneActions |
| 82 | /// Undo and Redo at the top (the title folded into the bar). |
| 83 | var history = false |
| 84 | var body: some View { |
| 85 | let offline = model.connectivity.isOffline(origin) |
| 86 | let owner = model.detail?.can_share == true && !model.isDraft && model.documentAccess != .unavailable |
| 87 | let writer = model.documentAccess == .writable |
| 88 | let saved = model.detail != nil && !model.isDraft |
| 89 | Menu { |
| 90 | if history && writer { |
| 91 | Section { |
| 92 | Button { DocumentCommands(model: model, content: model.surface.content).perform("undo") } label: { |
| 93 | Label { Text("docs.editor.toolbar.undo", bundle: DocsResources.bundle) } icon: { Image(systemName: "arrow.uturn.backward") } |
| 94 | } |
| 95 | .disabled(!model.history.canUndo) |
| 96 | Button { DocumentCommands(model: model, content: model.surface.content).perform("redo") } label: { |
| 97 | Label { Text("docs.editor.toolbar.redo", bundle: DocsResources.bundle) } icon: { Image(systemName: "arrow.uturn.forward") } |
| 98 | } |
| 99 | .disabled(!model.history.canRedo) |
| 100 | } |
| 101 | } |
| 102 | Section { |
| 103 | if owner { |
| 104 | Button(action: actions.share) { Label { Text("docs.share.action", bundle: DocsResources.bundle) } icon: { Image(systemName: "person.crop.circle.badge.plus") } } |
| 105 | .disabled(offline) |
| 106 | } |
| 107 | if writer { |
| 108 | Button(action: actions.rename) { Label { Text("docs.document.rename", bundle: DocsResources.bundle) } icon: { Image(systemName: "pencil") } } |
| 109 | } |
| 110 | } |
| 111 | Section { |
| 112 | if saved { |
| 113 | Menu { |
| 114 | Button { actions.export("pdf") } label: { Text("docs.export.pdf.label", bundle: DocsResources.bundle) } |
| 115 | Button { actions.export("markdown") } label: { Text("docs.export.markdown.label", bundle: DocsResources.bundle) } |
| 116 | Button { actions.export("print") } label: { Text("docs.editor.menu.file.print", bundle: DocsResources.bundle) } |
| 117 | } label: { Label { Text("native.export", bundle: DocsResources.bundle) } icon: { Image(systemName: "square.and.arrow.up") } } |
| 118 | .disabled(offline) |
| 119 | } |
| 120 | Button(action: actions.wordCount) { Label { Text("docs.editor.menu.tools.wordcount", bundle: DocsResources.bundle) } icon: { Image(systemName: "number") } } |
| 121 | .disabled(!model.showsDocument) |
| 122 | Button(action: actions.openDocument) { Label { Text("docs.editor.menu.file.open", bundle: DocsResources.bundle) } icon: { Image(systemName: "doc.text.magnifyingglass") } } |
| 123 | Picker(selection: $paged) { |
| 124 | Text("docs.editor.menu.view.paged", bundle: DocsResources.bundle).tag(true) |
| 125 | Text("docs.editor.menu.view.infinite", bundle: DocsResources.bundle).tag(false) |
| 126 | } label: { EmptyView() } |
| 127 | .pickerStyle(.inline) |
| 128 | } |
| 129 | Section { |
| 130 | Menu { |
| 131 | Button(action: actions.shortcuts) { Text("docs.editor.menu.tools.shortcuts", bundle: DocsResources.bundle) } |
| 132 | Button(action: actions.colophon) { Text(verbatim: DocsColophonView.title) } |
| 133 | } label: { Label { Text("docs.editor.menu.help", bundle: DocsResources.bundle) } icon: { Image(systemName: "questionmark.circle") } } |
| 134 | if owner { |
| 135 | Button(role: .destructive, action: actions.delete) { Label { Text("docs.editor.menu.file.delete", bundle: DocsResources.bundle) } icon: { Image(systemName: "trash") } } |
| 136 | .disabled(offline) |
| 137 | } |
| 138 | } |
| 139 | } label: { |
| 140 | Image(systemName: "ellipsis").font(.body.weight(.semibold)).frame(minWidth: 44, minHeight: 44) |
| 141 | } |
| 142 | .accessibilityLabel(Text("native.more", bundle: DocsResources.bundle)) |
| 143 | .accessibilityIdentifier("docs.more") |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | /// The iPhone's callouts under the bar (spec §6, banners): the session |
| 148 | /// ended (rows 5 and 6), No longer available (row 1) with Back, and today's |
| 149 | /// `errorKey` text with Try again. Their own view. |
| 150 | @MainActor struct DocsPhoneCallouts: View { |
| 151 | let model: DocsModel |
| 152 | @Environment(\.locale) private var locale |
| 153 | let discard: () -> Void |
| 154 | let back: () -> Void |
| 155 | var signIn: () -> Void = {} |
| 156 | var body: some View { |
| 157 | VStack(spacing: DocsSpacing.sp2) { |
| 158 | if model.documentAccess != .unavailable, case .needsSignIn(let pending) = model.syncStatus { |
| 159 | let sync = NativeSyncStrings.kit |
| 160 | callout(Text(verbatim: pending > 0 ? sync.needsSignIn(pending) : DocsStrings(locale: locale)("native.subtitle.signedOut")), negative: true) { |
| 161 | Button { signIn() } label: { Text(verbatim: sync.signIn) } |
| 162 | } |
| 163 | } |
| 164 | if model.documentAccess == .unavailable { |
| 165 | callout(Text("native.unavailable.body", bundle: DocsResources.bundle), negative: true) { |
| 166 | Button(action: back) { Text("native.back", bundle: DocsResources.bundle) } |
| 167 | if model.hasUnsavedChanges { |
| 168 | Button(role: .destructive) { discard() } label: { Text("native.discard.action", bundle: DocsResources.bundle) } |
| 169 | } |
| 170 | } |
| 171 | } else if let key = model.errorKey { |
| 172 | callout(Text(LocalizedStringKey(key), bundle: DocsResources.bundle), negative: false) { |
| 173 | Button { Task { await model.retry() } } label: { Text("native.retry", bundle: DocsResources.bundle) } |
| 174 | if model.hasUnsavedChanges { |
| 175 | Button(role: .destructive) { discard() } label: { Text("native.discard.action", bundle: DocsResources.bundle) } |
| 176 | } |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | .padding(.horizontal, DocsSpacing.sp3) |
| 181 | } |
| 182 | private func callout<Actions: View>(_ text: Text, negative: Bool, @ViewBuilder actions: () -> Actions) -> some View { |
| 183 | VStack(alignment: .leading, spacing: DocsSpacing.sp2) { |
| 184 | text.font(.callout).foregroundStyle(negative ? OficinaPalette.negative : OficinaPalette.text) |
| 185 | .fixedSize(horizontal: false, vertical: true) |
| 186 | HStack(spacing: DocsSpacing.sp3) { actions() }.font(.callout.weight(.semibold)) |
| 187 | } |
| 188 | .frame(maxWidth: .infinity, alignment: .leading) |
| 189 | .padding(DocsSpacing.sp3) |
| 190 | .background(RoundedRectangle(cornerRadius: 12).fill(negative ? OficinaPalette.negativeSoft : OficinaPalette.surface)) |
| 191 | .overlay(RoundedRectangle(cornerRadius: 12).strokeBorder(OficinaPalette.line, lineWidth: 1)) |
| 192 | .accessibilityElement(children: .contain) |
| 193 | .accessibilityIdentifier(negative ? "docs.callout.negative" : "docs.callout.error") |
| 194 | } |
| 195 | } |
| 196 | #endif |
| 197 | |