oficina / docs Public

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

Plain git — no account needed to clone.

Download

Download this file

1import RastrilloApple
2import SwiftUI
3#if os(macOS)
4import AppKit
5#else
6import UIKit
7#endif
8
9/// The Share panel (spec 2026-11-22 §1, "Share popover"), one for one with
10/// the web's (views/share.html "docs-share-panel") and in its words: the
11/// head and Close, the callout, the invite row, the people with access, and
12/// general access with the team and link switches and the once-only URL.
13/// The Mac shows it in a popover under Share…, the iPhone in a sheet.
14@MainActor struct DocsSharePanel: View {
15 let model: DocsSharePanelModel
16 /// Who "(you)" is: the account's label, when the app knows one.
17 var me: String?
18 /// The snapshot gate draws it unfocused: a caret would blink.
19 var focusOnAppear = true
20 let close: () -> Void
21 @Environment(\.locale) private var locale
22 @Environment(\.dynamicTypeSize) private var typeSize
23 /// The avatars' and icons' circles: 28pt, growing with their letters
24 /// and symbols under Dynamic Type (iPhone and iPad; the Mac keeps 28).
25 @ScaledMetric(relativeTo: .footnote) private var circle: CGFloat = 28
26 @FocusState private var focus: Field?
27 @AccessibilityFocusState private var headFocused: Bool
28 enum Field: Hashable { case email }
29
30 var body: some View {
31 let strings = DocsStrings(locale: locale)
32 VStack(alignment: .leading, spacing: DocsSpacing.sp3) {
33 head(strings)
34 if let key = model.errorKey {
35 callout(key)
36 }
37 if model.information != nil {
38 invite(strings)
39 section("docs.share.panel.people")
40 people(strings)
41 section("docs.share.panel.general")
42 general(strings)
43 } else if model.inFlight == .load {
44 ProgressView().controlSize(.small).frame(maxWidth: .infinity)
45 }
46 }
47 .padding(DocsSpacing.sp4)
48 #if os(macOS)
49 .frame(width: 420, alignment: .topLeading)
50 #endif
51 .accessibilityElement(children: .contain)
52 .accessibilityIdentifier("docs.share.panel")
53 .task {
54 guard focusOnAppear else { return }
55 // Focus goes to the email field; to the head under VoiceOver on
56 // iPhone, where a field taking focus would raise the keyboard
57 // over what is being read.
58 #if os(iOS)
59 if UIAccessibility.isVoiceOverRunning { headFocused = true } else { focus = .email }
60 #else
61 focus = .email
62 #endif
63 }
64 }
65
66 // MARK: Parts
67
68 private func head(_ strings: DocsStrings) -> some View {
69 HStack(alignment: .firstTextBaseline, spacing: DocsSpacing.sp2) {
70 Text(verbatim: strings("docs.share.panel.heading").replacingOccurrences(of: "{title}", with: model.title))
71 .font(.headline)
72 .foregroundStyle(OficinaPalette.text)
73 .lineLimit(headingLines)
74 .fixedSize(horizontal: false, vertical: true)
75 .accessibilityAddTraits(.isHeader)
76 .accessibilityFocused($headFocused)
77 .accessibilityIdentifier("docs.share.heading")
78 Spacer(minLength: DocsSpacing.sp2)
79 Button(action: close) {
80 Image(systemName: "xmark").font(DocsType.font(12, .semibold, ios: .callout))
81 .frame(minWidth: buttonSide, minHeight: buttonSide).contentShape(Rectangle())
82 }
83 .buttonStyle(.plain)
84 .foregroundStyle(OficinaPalette.textMuted)
85 .keyboardShortcut(.cancelAction)
86 .help(Text("docs.share.panel.close", bundle: DocsResources.bundle))
87 .accessibilityLabel(Text("docs.share.panel.close", bundle: DocsResources.bundle))
88 .accessibilityIdentifier("docs.share.close")
89 }
90 }
91
92 private func callout(_ key: String) -> some View {
93 HStack(spacing: DocsSpacing.sp2) {
94 Text(LocalizedStringKey(key), bundle: DocsResources.bundle)
95 .font(.callout).foregroundStyle(OficinaPalette.negative)
96 .fixedSize(horizontal: false, vertical: true)
97 if model.information == nil {
98 Spacer(minLength: DocsSpacing.sp2)
99 Button { Task { await model.load() } } label: { Text("native.retry", bundle: DocsResources.bundle) }
100 .controlSize(.small).disabled(model.isBusy)
101 }
102 }
103 .padding(.horizontal, DocsSpacing.sp3).padding(.vertical, DocsSpacing.sp2)
104 .frame(maxWidth: .infinity, alignment: .leading)
105 .background(RoundedRectangle(cornerRadius: OficinaPalette.radiusSmall).fill(OficinaPalette.negativeSoft))
106 .accessibilityElement(children: .combine)
107 .accessibilityAddTraits(.updatesFrequently)
108 .accessibilityIdentifier("docs.share.error")
109 }
110
111 private func invite(_ strings: DocsStrings) -> some View {
112 // One row, as the web's; the field on a row of its own when its
113 // words would not fit beside the menu and Invite (ru, ga: spec §9).
114 DocsInviteLayout(spacing: DocsSpacing.sp2) {
115 ZStack(alignment: .leading) {
116 // The placeholder's own width, so the layout knows what fits; no
117 // height, so it is never an element over the field's own text.
118 Text("docs.share.panel.invite_placeholder", bundle: DocsResources.bundle)
119 .lineLimit(1).fixedSize().frame(height: 0).hidden().accessibilityHidden(true)
120 TextField(text: Binding(get: { model.inviteAddress }, set: { model.inviteAddress = $0 })) {
121 Text("docs.share.panel.invite_placeholder", bundle: DocsResources.bundle)
122 }
123 .textFieldStyle(.plain)
124 .textContentType(.emailAddress)
125 .autocorrectionDisabled()
126 #if os(iOS)
127 .textInputAutocapitalization(.never)
128 .keyboardType(.emailAddress)
129 #endif
130 .focused($focus, equals: .email)
131 .onSubmit { Task { await model.invite() } }
132 .accessibilityLabel(Text("docs.share.address", bundle: DocsResources.bundle))
133 .accessibilityIdentifier("docs.share.email")
134 }
135 // Without it iOS folds the field into one unlabelled element and
136 // VoiceOver cannot reach it (the accessibility audit's "text not
137 // represented").
138 .accessibilityElement(children: .contain)
139 Picker(selection: Binding(get: { model.inviteRole }, set: { model.inviteRole = $0 })) {
140 Text("docs.share.panel.can_read", bundle: DocsResources.bundle).tag(DocsSharePanelModel.Role.reader)
141 Text("docs.share.panel.can_edit", bundle: DocsResources.bundle).tag(DocsSharePanelModel.Role.writer)
142 } label: { Text("docs.share.panel.access", bundle: DocsResources.bundle) }
143 .labelsHidden()
144 .pickerStyle(.menu)
145 .fixedSize()
146 .accessibilityIdentifier("docs.share.role")
147 Button { Task { await model.invite() } } label: {
148 ZStack {
149 Text("docs.share.panel.invite", bundle: DocsResources.bundle).opacity(model.spinning == .invite ? 0 : 1)
150 if model.spinning == .invite { ProgressView().controlSize(.small) }
151 }
152 }
153 .buttonStyle(DocsSharePillStyle())
154 // Only an empty field greys it: a submit while another is in
155 // flight is refused by the model, and nothing else looks busy.
156 .disabled(model.inviteAddress.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty)
157 .accessibilityIdentifier("docs.share.invite")
158 }
159 .padding(.leading, DocsSpacing.sp3).padding(.trailing, DocsSpacing.sp1).padding(.vertical, DocsSpacing.sp1)
160 .frame(minHeight: rowHeight)
161 .background(RoundedRectangle(cornerRadius: OficinaPalette.radiusSmall).fill(OficinaPalette.surface))
162 .overlay(RoundedRectangle(cornerRadius: OficinaPalette.radiusSmall).strokeBorder(OficinaPalette.line, lineWidth: 1))
163 }
164
165 private func section(_ key: String) -> some View {
166 Text(LocalizedStringKey(key), bundle: DocsResources.bundle)
167 .font(DocsType.font(11, .semibold, ios: .footnote))
168 .foregroundStyle(OficinaPalette.textMuted)
169 .accessibilityAddTraits(.isHeader)
170 .padding(.top, DocsSpacing.sp1)
171 }
172
173 private func people(_ strings: DocsStrings) -> some View {
174 VStack(alignment: .leading, spacing: DocsSpacing.sp2) {
175 person(initial: me.map(Self.initial) ?? "?", mine: true) {
176 if let me, !me.isEmpty {
177 Text(verbatim: me).lineLimit(addressLines).truncationMode(.middle)
178 Text("docs.share.panel.you", bundle: DocsResources.bundle).italic().foregroundStyle(OficinaPalette.textMuted)
179 } else {
180 Text("docs.share.panel.you", bundle: DocsResources.bundle).italic()
181 }
182 } trailing: {
183 Text("docs.share.panel.owner", bundle: DocsResources.bundle).foregroundStyle(OficinaPalette.textMuted)
184 }
185 ForEach(model.information?.grants ?? []) { grant in
186 let remove = strings("docs.share.panel.remove").replacingOccurrences(of: "{address}", with: grant.address)
187 person(initial: Self.initial(grant.address), mine: false) {
188 Text(verbatim: grant.address).lineLimit(addressLines).truncationMode(.middle).textSelection(.enabled)
189 } trailing: {
190 Text(grant.role == "writer" ? "docs.share.panel.can_edit" : "docs.share.panel.can_read", bundle: DocsResources.bundle)
191 .foregroundStyle(OficinaPalette.textMuted)
192 Button { Task { await model.remove(grant.address) } } label: {
193 ZStack {
194 Image(systemName: "xmark").font(DocsType.font(11, .semibold, ios: .callout)).opacity(model.spinning == .remove(grant.address) ? 0 : 1)
195 if model.spinning == .remove(grant.address) { ProgressView().controlSize(.mini) }
196 }
197 .frame(minWidth: buttonSide, minHeight: buttonSide).contentShape(Rectangle())
198 }
199 .buttonStyle(.plain)
200 .foregroundStyle(OficinaPalette.textMuted)
201 .help(Text(verbatim: remove))
202 .accessibilityLabel(Text(verbatim: remove))
203 .accessibilityIdentifier("docs.share.remove")
204 }
205 }
206 }
207 }
208
209 /// At accessibility sizes a person's row stacks: the address, whole,
210 /// above the role (the coordinator's ruling on #180).
211 private var stacked: Bool { typeSize.isAccessibilitySize }
212 private var addressLines: Int { stacked ? 3 : 1 }
213
214 @ViewBuilder private func person<Name: View, Trailing: View>(initial: String, mine: Bool, @ViewBuilder name: () -> Name,
215 @ViewBuilder trailing: () -> Trailing) -> some View {
216 let avatar = Text(verbatim: initial)
217 .font(DocsType.font(12, .semibold, ios: .footnote))
218 .frame(width: circle, height: circle)
219 .foregroundStyle(mine ? OficinaPalette.onAccent : OficinaPalette.accent(.docs))
220 .background(Circle().fill(mine ? OficinaPalette.accent(.docs) : OficinaPalette.accentSoft(.docs)))
221 .accessibilityHidden(true)
222 Group {
223 if stacked {
224 HStack(alignment: .top, spacing: DocsSpacing.sp2) {
225 avatar
226 VStack(alignment: .leading, spacing: DocsSpacing.sp1) {
227 HStack(spacing: DocsSpacing.sp1) { name() }
228 .foregroundStyle(OficinaPalette.text)
229 .fixedSize(horizontal: false, vertical: true)
230 HStack(spacing: DocsSpacing.sp2) { trailing() }
231 }
232 Spacer(minLength: 0)
233 }
234 } else {
235 HStack(spacing: DocsSpacing.sp2) {
236 avatar
237 HStack(spacing: DocsSpacing.sp1) { name() }
238 .foregroundStyle(OficinaPalette.text)
239 Spacer(minLength: DocsSpacing.sp2)
240 trailing()
241 }
242 }
243 }
244 .font(.callout)
245 .frame(minHeight: rowHeight)
246 .accessibilityElement(children: .combine)
247 }
248
249 @ViewBuilder private func general(_ strings: DocsStrings) -> some View {
250 VStack(alignment: .leading, spacing: DocsSpacing.sp2) {
251 rung(icon: "person.2", heading: "docs.share.team.heading", line: model.teamOn ? "docs.share.panel.team_on" : "docs.share.panel.team_off",
252 switchLabel: "docs.share.panel.team_switch", isOn: model.teamOn, control: .team, identifier: "docs.share.team") { on in
253 Task { await model.setTeam(on) }
254 }
255 rung(icon: "link", heading: "docs.share.panel.link_heading", line: model.linkOn ? "docs.share.panel.link_on" : "docs.share.panel.link_off",
256 switchLabel: "docs.share.panel.link_switch", isOn: model.linkOn, control: .link, identifier: "docs.share.link") { on in
257 Task { await model.setLink(on) }
258 }
259 if let url = model.onceURL {
260 onceLink(url)
261 } else if model.information?.published == true {
262 HStack(spacing: DocsSpacing.sp2) {
263 Text("docs.share.panel.shown", bundle: DocsResources.bundle)
264 .font(.callout).foregroundStyle(OficinaPalette.textMuted)
265 .fixedSize(horizontal: false, vertical: true)
266 Spacer(minLength: DocsSpacing.sp2)
267 Button { Task { await model.newLink() } } label: {
268 ZStack {
269 Text("docs.share.panel.new_link", bundle: DocsResources.bundle).opacity(model.spinning == .newLink ? 0 : 1)
270 if model.spinning == .newLink { ProgressView().controlSize(.small) }
271 }
272 }
273 .controlSize(.small)
274 .accessibilityIdentifier("docs.share.newLink")
275 }
276 }
277 }
278 }
279
280 private func rung(icon: String, heading: String, line: String, switchLabel: String, isOn: Bool, control: DocsSharePanelModel.Control,
281 identifier: String, set: @escaping (Bool) -> Void) -> some View {
282 HStack(spacing: DocsSpacing.sp3) {
283 Image(systemName: icon)
284 .font(DocsType.font(14, .medium, ios: .callout))
285 .frame(width: circle, height: circle)
286 .foregroundStyle(OficinaPalette.textMuted)
287 .background(Circle().fill(OficinaPalette.sunk))
288 .accessibilityHidden(true)
289 VStack(alignment: .leading, spacing: 2) {
290 Text(LocalizedStringKey(heading), bundle: DocsResources.bundle).font(.callout.weight(.semibold)).foregroundStyle(OficinaPalette.text)
291 Text(LocalizedStringKey(line), bundle: DocsResources.bundle).font(.caption).foregroundStyle(OficinaPalette.textMuted)
292 .fixedSize(horizontal: false, vertical: true)
293 }
294 Spacer(minLength: DocsSpacing.sp2)
295 if model.spinning == control { ProgressView().controlSize(.small) }
296 Toggle(isOn: Binding(get: { isOn }, set: set)) { Text(LocalizedStringKey(switchLabel), bundle: DocsResources.bundle) }
297 #if os(macOS)
298 .toggleStyle(DocsSwitchStyle())
299 #else
300 .toggleStyle(.switch)
301 .tint(OficinaPalette.accent(.docs))
302 #endif
303 .labelsHidden()
304 .accessibilityLabel(Text(LocalizedStringKey(switchLabel), bundle: DocsResources.bundle))
305 .accessibilityIdentifier(identifier)
306 }
307 .frame(minHeight: rowHeight)
308 }
309
310 private func onceLink(_ url: URL) -> some View {
311 VStack(alignment: .leading, spacing: DocsSpacing.sp2) {
312 HStack(spacing: DocsSpacing.sp2) {
313 Text(verbatim: url.absoluteString)
314 .font(.callout.monospaced())
315 .lineLimit(1).truncationMode(.middle)
316 .textSelection(.enabled)
317 .frame(maxWidth: .infinity, alignment: .leading)
318 .accessibilityLabel(Text("docs.share.public.url", bundle: DocsResources.bundle))
319 .accessibilityValue(Text(verbatim: url.absoluteString))
320 .accessibilityIdentifier("docs.share.url")
321 Button { model.copyLink(Self.copy) } label: {
322 Text(model.copied ? "docs.share.public.copied" : "docs.share.public.copy", bundle: DocsResources.bundle)
323 }
324 .buttonStyle(DocsSharePillStyle())
325 .accessibilityIdentifier("docs.share.copy")
326 }
327 .padding(.leading, DocsSpacing.sp3).padding(.trailing, DocsSpacing.sp1).padding(.vertical, DocsSpacing.sp1)
328 .frame(minHeight: rowHeight)
329 .background(RoundedRectangle(cornerRadius: OficinaPalette.radiusSmall).fill(OficinaPalette.sunk))
330 Text("docs.share.panel.once", bundle: DocsResources.bundle)
331 .font(.caption.weight(.semibold)).foregroundStyle(OficinaPalette.text)
332 .fixedSize(horizontal: false, vertical: true)
333 .accessibilityAddTraits(.updatesFrequently)
334 .accessibilityIdentifier("docs.share.once")
335 }
336 }
337
338 /// The heading's lines: two in the Mac's popover; on iPhone all it
339 /// needs, as the sheet scrolls and Dynamic Type reaches AX5 (spec §9).
340 private var headingLines: Int? {
341 #if os(macOS)
342 2
343 #else
344 nil
345 #endif
346 }
347
348 /// The panel's icon buttons (close, remove): 28pt on the Mac, a 44pt
349 /// target on iPhone (spec §9).
350 private var buttonSide: CGFloat {
351 #if os(macOS)
352 28
353 #else
354 44
355 #endif
356 }
357
358 /// The panel's rows: 32pt on the Mac, 44pt on iPhone (spec §9).
359 private var rowHeight: CGFloat {
360 #if os(macOS)
361 32
362 #else
363 44
364 #endif
365 }
366
367 /// The avatar letter, estilo's rule for an address: its first letter.
368 static func initial(_ address: String) -> String {
369 address.first.map { String($0).uppercased() } ?? "?"
370 }
371
372 /// Copy link: the URL reaches the pasteboard here and nowhere else.
373 static func copy(_ url: URL) {
374 #if os(macOS)
375 NSPasteboard.general.clearContents()
376 NSPasteboard.general.setString(url.absoluteString, forType: .string)
377 #else
378 UIPasteboard.general.url = url
379 #endif
380 }
381}
382
383/// The invite row: the field, the access menu and Invite on one line when
384/// the field's placeholder fits beside the other two; otherwise the field
385/// across the top and the menu and Invite under it, at the trailing edge.
386struct DocsInviteLayout: Layout {
387 var spacing: CGFloat
388 private func sizes(_ subviews: Subviews) -> [CGSize] { subviews.map { $0.sizeThatFits(.unspecified) } }
389 private func oneRow(_ sizes: [CGSize], width: CGFloat) -> Bool {
390 sizes.reduce(0) { $0 + $1.width } + spacing * CGFloat(max(sizes.count - 1, 0)) <= width
391 }
392 func sizeThatFits(proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) -> CGSize {
393 let sizes = sizes(subviews)
394 let natural = sizes.reduce(0) { $0 + $1.width } + spacing * CGFloat(max(sizes.count - 1, 0))
395 let width = proposal.width ?? natural
396 let tallest = sizes.map(\.height).max() ?? 0
397 if oneRow(sizes, width: width) { return CGSize(width: width, height: tallest) }
398 let below = sizes.dropFirst().map(\.height).max() ?? 0
399 return CGSize(width: width, height: (sizes.first?.height ?? 0) + spacing + below)
400 }
401 func placeSubviews(in bounds: CGRect, proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) {
402 guard !subviews.isEmpty else { return }
403 let sizes = sizes(subviews)
404 let rest = sizes.dropFirst().reduce(0) { $0 + $1.width } + spacing * CGFloat(max(sizes.count - 1, 0))
405 if oneRow(sizes, width: bounds.width) {
406 let tallest = sizes.map(\.height).max() ?? 0
407 subviews[0].place(at: CGPoint(x: bounds.minX, y: bounds.minY + tallest / 2), anchor: .leading,
408 proposal: ProposedViewSize(width: bounds.width - rest, height: sizes[0].height))
409 var x = bounds.maxX
410 for index in subviews.indices.dropFirst().reversed() {
411 x -= sizes[index].width
412 subviews[index].place(at: CGPoint(x: x, y: bounds.minY + tallest / 2), anchor: .leading, proposal: ProposedViewSize(sizes[index]))
413 x -= spacing
414 }
415 } else {
416 subviews[0].place(at: bounds.origin, proposal: ProposedViewSize(width: bounds.width, height: sizes[0].height))
417 let top = bounds.minY + sizes[0].height + spacing
418 let below = sizes.dropFirst().map(\.height).max() ?? 0
419 var x = bounds.maxX
420 for index in subviews.indices.dropFirst().reversed() {
421 x -= sizes[index].width
422 subviews[index].place(at: CGPoint(x: x, y: top + below / 2), anchor: .leading, proposal: ProposedViewSize(sizes[index]))
423 x -= spacing
424 }
425 }
426 }
427}
428
429#if os(macOS)
430/// The web panel's switch (rastrillo's rst-switch, as the Calma look draws
431/// it): the accent track when on, whatever the window's state. AppKit's
432/// own switch greys its "on" in a window that is not key, which reads as
433/// off. Still a Toggle, so VoiceOver says "switch, on/off", and Space flips
434/// it with Full Keyboard Access.
435struct DocsSwitchStyle: ToggleStyle {
436 @Environment(\.isEnabled) private var isEnabled
437 @Environment(\.accessibilityReduceMotion) private var reduceMotion
438 func makeBody(configuration: Configuration) -> some View {
439 let on = configuration.isOn
440 Button { configuration.isOn.toggle() } label: {
441 Capsule()
442 .fill(on ? OficinaPalette.accent(.docs) : OficinaPalette.sunk)
443 .overlay(Capsule().strokeBorder(on ? Color.clear : OficinaPalette.line, lineWidth: 1))
444 .frame(width: 36, height: 20)
445 .overlay(alignment: on ? .trailing : .leading) {
446 Circle().fill(.white).shadow(color: .black.opacity(0.2), radius: 1, y: 0.5)
447 .frame(width: 16, height: 16).padding(2)
448 }
449 .animation(reduceMotion ? nil : .easeOut(duration: 0.15), value: on)
450 .opacity(isEnabled ? 1 : 0.5)
451 .contentShape(Capsule())
452 }
453 .buttonStyle(.plain)
454 .accessibilityRepresentation { Toggle(isOn: configuration.$isOn) { configuration.label } }
455 }
456}
457#endif
458
459/// The panel's one accent action (Invite, Copy link): a small filled pill.
460struct DocsSharePillStyle: ButtonStyle {
461 /// 26pt on the Mac; a 44pt target on iPhone (spec §9).
462 static var height: CGFloat {
463 #if os(macOS)
464 26
465 #else
466 44
467 #endif
468 }
469 @Environment(\.isEnabled) private var isEnabled
470 func makeBody(configuration: Configuration) -> some View {
471 configuration.label
472 .font(DocsType.font(13, .semibold, ios: .subheadline))
473 .lineLimit(1)
474 .fixedSize()
475 .padding(.horizontal, DocsSpacing.sp3)
476 .frame(minHeight: DocsSharePillStyle.height)
477 .foregroundStyle(OficinaPalette.onAccent)
478 .background(Capsule().fill(OficinaPalette.accent(.docs)))
479 .opacity(configuration.isPressed ? 0.8 : isEnabled ? 1 : 0.45)
480 .contentShape(Capsule())
481 }
482}
483