import RastrilloApple import SwiftUI #if os(macOS) import AppKit #else import UIKit #endif /// The Share panel (spec 2026-11-22 §1, "Share popover"), one for one with /// the web's (views/share.html "docs-share-panel") and in its words: the /// head and Close, the callout, the invite row, the people with access, and /// general access with the team and link switches and the once-only URL. /// The Mac shows it in a popover under Share…, the iPhone in a sheet. @MainActor struct DocsSharePanel: View { let model: DocsSharePanelModel /// Who "(you)" is: the account's label, when the app knows one. var me: String? /// The snapshot gate draws it unfocused: a caret would blink. var focusOnAppear = true let close: () -> Void @Environment(\.locale) private var locale @Environment(\.dynamicTypeSize) private var typeSize /// The avatars' and icons' circles: 28pt, growing with their letters /// and symbols under Dynamic Type (iPhone and iPad; the Mac keeps 28). @ScaledMetric(relativeTo: .footnote) private var circle: CGFloat = 28 @FocusState private var focus: Field? @AccessibilityFocusState private var headFocused: Bool enum Field: Hashable { case email } var body: some View { let strings = DocsStrings(locale: locale) VStack(alignment: .leading, spacing: DocsSpacing.sp3) { head(strings) if let key = model.errorKey { callout(key) } if model.information != nil { invite(strings) section("docs.share.panel.people") people(strings) section("docs.share.panel.general") general(strings) } else if model.inFlight == .load { ProgressView().controlSize(.small).frame(maxWidth: .infinity) } } .padding(DocsSpacing.sp4) #if os(macOS) .frame(width: 420, alignment: .topLeading) #endif .accessibilityElement(children: .contain) .accessibilityIdentifier("docs.share.panel") .task { guard focusOnAppear else { return } // Focus goes to the email field; to the head under VoiceOver on // iPhone, where a field taking focus would raise the keyboard // over what is being read. #if os(iOS) if UIAccessibility.isVoiceOverRunning { headFocused = true } else { focus = .email } #else focus = .email #endif } } // MARK: Parts private func head(_ strings: DocsStrings) -> some View { HStack(alignment: .firstTextBaseline, spacing: DocsSpacing.sp2) { Text(verbatim: strings("docs.share.panel.heading").replacingOccurrences(of: "{title}", with: model.title)) .font(.headline) .foregroundStyle(OficinaPalette.text) .lineLimit(headingLines) .fixedSize(horizontal: false, vertical: true) .accessibilityAddTraits(.isHeader) .accessibilityFocused($headFocused) .accessibilityIdentifier("docs.share.heading") Spacer(minLength: DocsSpacing.sp2) Button(action: close) { Image(systemName: "xmark").font(DocsType.font(12, .semibold, ios: .callout)) .frame(minWidth: buttonSide, minHeight: buttonSide).contentShape(Rectangle()) } .buttonStyle(.plain) .foregroundStyle(OficinaPalette.textMuted) .keyboardShortcut(.cancelAction) .help(Text("docs.share.panel.close", bundle: DocsResources.bundle)) .accessibilityLabel(Text("docs.share.panel.close", bundle: DocsResources.bundle)) .accessibilityIdentifier("docs.share.close") } } private func callout(_ key: String) -> some View { HStack(spacing: DocsSpacing.sp2) { Text(LocalizedStringKey(key), bundle: DocsResources.bundle) .font(.callout).foregroundStyle(OficinaPalette.negative) .fixedSize(horizontal: false, vertical: true) if model.information == nil { Spacer(minLength: DocsSpacing.sp2) Button { Task { await model.load() } } label: { Text("native.retry", bundle: DocsResources.bundle) } .controlSize(.small).disabled(model.isBusy) } } .padding(.horizontal, DocsSpacing.sp3).padding(.vertical, DocsSpacing.sp2) .frame(maxWidth: .infinity, alignment: .leading) .background(RoundedRectangle(cornerRadius: OficinaPalette.radiusSmall).fill(OficinaPalette.negativeSoft)) .accessibilityElement(children: .combine) .accessibilityAddTraits(.updatesFrequently) .accessibilityIdentifier("docs.share.error") } private func invite(_ strings: DocsStrings) -> some View { // One row, as the web's; the field on a row of its own when its // words would not fit beside the menu and Invite (ru, ga: spec §9). DocsInviteLayout(spacing: DocsSpacing.sp2) { ZStack(alignment: .leading) { // The placeholder's own width, so the layout knows what fits; no // height, so it is never an element over the field's own text. Text("docs.share.panel.invite_placeholder", bundle: DocsResources.bundle) .lineLimit(1).fixedSize().frame(height: 0).hidden().accessibilityHidden(true) TextField(text: Binding(get: { model.inviteAddress }, set: { model.inviteAddress = $0 })) { Text("docs.share.panel.invite_placeholder", bundle: DocsResources.bundle) } .textFieldStyle(.plain) .textContentType(.emailAddress) .autocorrectionDisabled() #if os(iOS) .textInputAutocapitalization(.never) .keyboardType(.emailAddress) #endif .focused($focus, equals: .email) .onSubmit { Task { await model.invite() } } .accessibilityLabel(Text("docs.share.address", bundle: DocsResources.bundle)) .accessibilityIdentifier("docs.share.email") } // Without it iOS folds the field into one unlabelled element and // VoiceOver cannot reach it (the accessibility audit's "text not // represented"). .accessibilityElement(children: .contain) Picker(selection: Binding(get: { model.inviteRole }, set: { model.inviteRole = $0 })) { Text("docs.share.panel.can_read", bundle: DocsResources.bundle).tag(DocsSharePanelModel.Role.reader) Text("docs.share.panel.can_edit", bundle: DocsResources.bundle).tag(DocsSharePanelModel.Role.writer) } label: { Text("docs.share.panel.access", bundle: DocsResources.bundle) } .labelsHidden() .pickerStyle(.menu) .fixedSize() .accessibilityIdentifier("docs.share.role") Button { Task { await model.invite() } } label: { ZStack { Text("docs.share.panel.invite", bundle: DocsResources.bundle).opacity(model.spinning == .invite ? 0 : 1) if model.spinning == .invite { ProgressView().controlSize(.small) } } } .buttonStyle(DocsSharePillStyle()) // Only an empty field greys it: a submit while another is in // flight is refused by the model, and nothing else looks busy. .disabled(model.inviteAddress.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty) .accessibilityIdentifier("docs.share.invite") } .padding(.leading, DocsSpacing.sp3).padding(.trailing, DocsSpacing.sp1).padding(.vertical, DocsSpacing.sp1) .frame(minHeight: rowHeight) .background(RoundedRectangle(cornerRadius: OficinaPalette.radiusSmall).fill(OficinaPalette.surface)) .overlay(RoundedRectangle(cornerRadius: OficinaPalette.radiusSmall).strokeBorder(OficinaPalette.line, lineWidth: 1)) } private func section(_ key: String) -> some View { Text(LocalizedStringKey(key), bundle: DocsResources.bundle) .font(DocsType.font(11, .semibold, ios: .footnote)) .foregroundStyle(OficinaPalette.textMuted) .accessibilityAddTraits(.isHeader) .padding(.top, DocsSpacing.sp1) } private func people(_ strings: DocsStrings) -> some View { VStack(alignment: .leading, spacing: DocsSpacing.sp2) { person(initial: me.map(Self.initial) ?? "?", mine: true) { if let me, !me.isEmpty { Text(verbatim: me).lineLimit(addressLines).truncationMode(.middle) Text("docs.share.panel.you", bundle: DocsResources.bundle).italic().foregroundStyle(OficinaPalette.textMuted) } else { Text("docs.share.panel.you", bundle: DocsResources.bundle).italic() } } trailing: { Text("docs.share.panel.owner", bundle: DocsResources.bundle).foregroundStyle(OficinaPalette.textMuted) } ForEach(model.information?.grants ?? []) { grant in let remove = strings("docs.share.panel.remove").replacingOccurrences(of: "{address}", with: grant.address) person(initial: Self.initial(grant.address), mine: false) { Text(verbatim: grant.address).lineLimit(addressLines).truncationMode(.middle).textSelection(.enabled) } trailing: { Text(grant.role == "writer" ? "docs.share.panel.can_edit" : "docs.share.panel.can_read", bundle: DocsResources.bundle) .foregroundStyle(OficinaPalette.textMuted) Button { Task { await model.remove(grant.address) } } label: { ZStack { Image(systemName: "xmark").font(DocsType.font(11, .semibold, ios: .callout)).opacity(model.spinning == .remove(grant.address) ? 0 : 1) if model.spinning == .remove(grant.address) { ProgressView().controlSize(.mini) } } .frame(minWidth: buttonSide, minHeight: buttonSide).contentShape(Rectangle()) } .buttonStyle(.plain) .foregroundStyle(OficinaPalette.textMuted) .help(Text(verbatim: remove)) .accessibilityLabel(Text(verbatim: remove)) .accessibilityIdentifier("docs.share.remove") } } } } /// At accessibility sizes a person's row stacks: the address, whole, /// above the role (the coordinator's ruling on #180). private var stacked: Bool { typeSize.isAccessibilitySize } private var addressLines: Int { stacked ? 3 : 1 } @ViewBuilder private func person(initial: String, mine: Bool, @ViewBuilder name: () -> Name, @ViewBuilder trailing: () -> Trailing) -> some View { let avatar = Text(verbatim: initial) .font(DocsType.font(12, .semibold, ios: .footnote)) .frame(width: circle, height: circle) .foregroundStyle(mine ? OficinaPalette.onAccent : OficinaPalette.accent(.docs)) .background(Circle().fill(mine ? OficinaPalette.accent(.docs) : OficinaPalette.accentSoft(.docs))) .accessibilityHidden(true) Group { if stacked { HStack(alignment: .top, spacing: DocsSpacing.sp2) { avatar VStack(alignment: .leading, spacing: DocsSpacing.sp1) { HStack(spacing: DocsSpacing.sp1) { name() } .foregroundStyle(OficinaPalette.text) .fixedSize(horizontal: false, vertical: true) HStack(spacing: DocsSpacing.sp2) { trailing() } } Spacer(minLength: 0) } } else { HStack(spacing: DocsSpacing.sp2) { avatar HStack(spacing: DocsSpacing.sp1) { name() } .foregroundStyle(OficinaPalette.text) Spacer(minLength: DocsSpacing.sp2) trailing() } } } .font(.callout) .frame(minHeight: rowHeight) .accessibilityElement(children: .combine) } @ViewBuilder private func general(_ strings: DocsStrings) -> some View { VStack(alignment: .leading, spacing: DocsSpacing.sp2) { rung(icon: "person.2", heading: "docs.share.team.heading", line: model.teamOn ? "docs.share.panel.team_on" : "docs.share.panel.team_off", switchLabel: "docs.share.panel.team_switch", isOn: model.teamOn, control: .team, identifier: "docs.share.team") { on in Task { await model.setTeam(on) } } rung(icon: "link", heading: "docs.share.panel.link_heading", line: model.linkOn ? "docs.share.panel.link_on" : "docs.share.panel.link_off", switchLabel: "docs.share.panel.link_switch", isOn: model.linkOn, control: .link, identifier: "docs.share.link") { on in Task { await model.setLink(on) } } if let url = model.onceURL { onceLink(url) } else if model.information?.published == true { HStack(spacing: DocsSpacing.sp2) { Text("docs.share.panel.shown", bundle: DocsResources.bundle) .font(.callout).foregroundStyle(OficinaPalette.textMuted) .fixedSize(horizontal: false, vertical: true) Spacer(minLength: DocsSpacing.sp2) Button { Task { await model.newLink() } } label: { ZStack { Text("docs.share.panel.new_link", bundle: DocsResources.bundle).opacity(model.spinning == .newLink ? 0 : 1) if model.spinning == .newLink { ProgressView().controlSize(.small) } } } .controlSize(.small) .accessibilityIdentifier("docs.share.newLink") } } } } private func rung(icon: String, heading: String, line: String, switchLabel: String, isOn: Bool, control: DocsSharePanelModel.Control, identifier: String, set: @escaping (Bool) -> Void) -> some View { HStack(spacing: DocsSpacing.sp3) { Image(systemName: icon) .font(DocsType.font(14, .medium, ios: .callout)) .frame(width: circle, height: circle) .foregroundStyle(OficinaPalette.textMuted) .background(Circle().fill(OficinaPalette.sunk)) .accessibilityHidden(true) VStack(alignment: .leading, spacing: 2) { Text(LocalizedStringKey(heading), bundle: DocsResources.bundle).font(.callout.weight(.semibold)).foregroundStyle(OficinaPalette.text) Text(LocalizedStringKey(line), bundle: DocsResources.bundle).font(.caption).foregroundStyle(OficinaPalette.textMuted) .fixedSize(horizontal: false, vertical: true) } Spacer(minLength: DocsSpacing.sp2) if model.spinning == control { ProgressView().controlSize(.small) } Toggle(isOn: Binding(get: { isOn }, set: set)) { Text(LocalizedStringKey(switchLabel), bundle: DocsResources.bundle) } #if os(macOS) .toggleStyle(DocsSwitchStyle()) #else .toggleStyle(.switch) .tint(OficinaPalette.accent(.docs)) #endif .labelsHidden() .accessibilityLabel(Text(LocalizedStringKey(switchLabel), bundle: DocsResources.bundle)) .accessibilityIdentifier(identifier) } .frame(minHeight: rowHeight) } private func onceLink(_ url: URL) -> some View { VStack(alignment: .leading, spacing: DocsSpacing.sp2) { HStack(spacing: DocsSpacing.sp2) { Text(verbatim: url.absoluteString) .font(.callout.monospaced()) .lineLimit(1).truncationMode(.middle) .textSelection(.enabled) .frame(maxWidth: .infinity, alignment: .leading) .accessibilityLabel(Text("docs.share.public.url", bundle: DocsResources.bundle)) .accessibilityValue(Text(verbatim: url.absoluteString)) .accessibilityIdentifier("docs.share.url") Button { model.copyLink(Self.copy) } label: { Text(model.copied ? "docs.share.public.copied" : "docs.share.public.copy", bundle: DocsResources.bundle) } .buttonStyle(DocsSharePillStyle()) .accessibilityIdentifier("docs.share.copy") } .padding(.leading, DocsSpacing.sp3).padding(.trailing, DocsSpacing.sp1).padding(.vertical, DocsSpacing.sp1) .frame(minHeight: rowHeight) .background(RoundedRectangle(cornerRadius: OficinaPalette.radiusSmall).fill(OficinaPalette.sunk)) Text("docs.share.panel.once", bundle: DocsResources.bundle) .font(.caption.weight(.semibold)).foregroundStyle(OficinaPalette.text) .fixedSize(horizontal: false, vertical: true) .accessibilityAddTraits(.updatesFrequently) .accessibilityIdentifier("docs.share.once") } } /// The heading's lines: two in the Mac's popover; on iPhone all it /// needs, as the sheet scrolls and Dynamic Type reaches AX5 (spec §9). private var headingLines: Int? { #if os(macOS) 2 #else nil #endif } /// The panel's icon buttons (close, remove): 28pt on the Mac, a 44pt /// target on iPhone (spec §9). private var buttonSide: CGFloat { #if os(macOS) 28 #else 44 #endif } /// The panel's rows: 32pt on the Mac, 44pt on iPhone (spec §9). private var rowHeight: CGFloat { #if os(macOS) 32 #else 44 #endif } /// The avatar letter, estilo's rule for an address: its first letter. static func initial(_ address: String) -> String { address.first.map { String($0).uppercased() } ?? "?" } /// Copy link: the URL reaches the pasteboard here and nowhere else. static func copy(_ url: URL) { #if os(macOS) NSPasteboard.general.clearContents() NSPasteboard.general.setString(url.absoluteString, forType: .string) #else UIPasteboard.general.url = url #endif } } /// The invite row: the field, the access menu and Invite on one line when /// the field's placeholder fits beside the other two; otherwise the field /// across the top and the menu and Invite under it, at the trailing edge. struct DocsInviteLayout: Layout { var spacing: CGFloat private func sizes(_ subviews: Subviews) -> [CGSize] { subviews.map { $0.sizeThatFits(.unspecified) } } private func oneRow(_ sizes: [CGSize], width: CGFloat) -> Bool { sizes.reduce(0) { $0 + $1.width } + spacing * CGFloat(max(sizes.count - 1, 0)) <= width } func sizeThatFits(proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) -> CGSize { let sizes = sizes(subviews) let natural = sizes.reduce(0) { $0 + $1.width } + spacing * CGFloat(max(sizes.count - 1, 0)) let width = proposal.width ?? natural let tallest = sizes.map(\.height).max() ?? 0 if oneRow(sizes, width: width) { return CGSize(width: width, height: tallest) } let below = sizes.dropFirst().map(\.height).max() ?? 0 return CGSize(width: width, height: (sizes.first?.height ?? 0) + spacing + below) } func placeSubviews(in bounds: CGRect, proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) { guard !subviews.isEmpty else { return } let sizes = sizes(subviews) let rest = sizes.dropFirst().reduce(0) { $0 + $1.width } + spacing * CGFloat(max(sizes.count - 1, 0)) if oneRow(sizes, width: bounds.width) { let tallest = sizes.map(\.height).max() ?? 0 subviews[0].place(at: CGPoint(x: bounds.minX, y: bounds.minY + tallest / 2), anchor: .leading, proposal: ProposedViewSize(width: bounds.width - rest, height: sizes[0].height)) var x = bounds.maxX for index in subviews.indices.dropFirst().reversed() { x -= sizes[index].width subviews[index].place(at: CGPoint(x: x, y: bounds.minY + tallest / 2), anchor: .leading, proposal: ProposedViewSize(sizes[index])) x -= spacing } } else { subviews[0].place(at: bounds.origin, proposal: ProposedViewSize(width: bounds.width, height: sizes[0].height)) let top = bounds.minY + sizes[0].height + spacing let below = sizes.dropFirst().map(\.height).max() ?? 0 var x = bounds.maxX for index in subviews.indices.dropFirst().reversed() { x -= sizes[index].width subviews[index].place(at: CGPoint(x: x, y: top + below / 2), anchor: .leading, proposal: ProposedViewSize(sizes[index])) x -= spacing } } } } #if os(macOS) /// The web panel's switch (rastrillo's rst-switch, as the Calma look draws /// it): the accent track when on, whatever the window's state. AppKit's /// own switch greys its "on" in a window that is not key, which reads as /// off. Still a Toggle, so VoiceOver says "switch, on/off", and Space flips /// it with Full Keyboard Access. struct DocsSwitchStyle: ToggleStyle { @Environment(\.isEnabled) private var isEnabled @Environment(\.accessibilityReduceMotion) private var reduceMotion func makeBody(configuration: Configuration) -> some View { let on = configuration.isOn Button { configuration.isOn.toggle() } label: { Capsule() .fill(on ? OficinaPalette.accent(.docs) : OficinaPalette.sunk) .overlay(Capsule().strokeBorder(on ? Color.clear : OficinaPalette.line, lineWidth: 1)) .frame(width: 36, height: 20) .overlay(alignment: on ? .trailing : .leading) { Circle().fill(.white).shadow(color: .black.opacity(0.2), radius: 1, y: 0.5) .frame(width: 16, height: 16).padding(2) } .animation(reduceMotion ? nil : .easeOut(duration: 0.15), value: on) .opacity(isEnabled ? 1 : 0.5) .contentShape(Capsule()) } .buttonStyle(.plain) .accessibilityRepresentation { Toggle(isOn: configuration.$isOn) { configuration.label } } } } #endif /// The panel's one accent action (Invite, Copy link): a small filled pill. struct DocsSharePillStyle: ButtonStyle { /// 26pt on the Mac; a 44pt target on iPhone (spec §9). static var height: CGFloat { #if os(macOS) 26 #else 44 #endif } @Environment(\.isEnabled) private var isEnabled func makeBody(configuration: Configuration) -> some View { configuration.label .font(DocsType.font(13, .semibold, ios: .subheadline)) .lineLimit(1) .fixedSize() .padding(.horizontal, DocsSpacing.sp3) .frame(minHeight: DocsSharePillStyle.height) .foregroundStyle(OficinaPalette.onAccent) .background(Capsule().fill(OficinaPalette.accent(.docs))) .opacity(configuration.isPressed ? 0.8 : isEnabled ? 1 : 0.45) .contentShape(Capsule()) } }