Repository Public
1import MCCCore2import SwiftUI34/// Presents `app.presentedError` with its code and correlation id.5private struct ErrorAlertModifier: ViewModifier {6 @Bindable var app: AppModel7 func body(content: Content) -> some View {8 content.alert(tr("error.title"), isPresented: Binding(get: { app.presentedError != nil }, set: { if !$0 { app.presentedError = nil } }),9 presenting: app.presentedError) { error in10 if let c = error.correlationID {11 Button(tr("error.copy_correlation")) {12 NSPasteboard.general.clearContents()13 NSPasteboard.general.setString(c, forType: .string)14 }15 }16 Button(tr("action.close"), role: .cancel) {}17 } message: { error in18 Text(ErrorText.describe(error))19 }20 }21}2223enum ErrorText {24 /// Message, HTTP status and code, correlation id — everything needed to report the problem.25 static func describe(_ e: APIError) -> String {26 var lines = [friendly(e)]27 lines.append(e.status.map { "HTTP \($0) · \(e.code)" } ?? e.code)28 if let c = e.correlationID { lines.append("\(tr("error.correlation_id")) : \(c)") }29 return lines.joined(separator: "\n")30 }3132 /// The server's message, or a localized explanation for the codes the client itself produces.33 static func friendly(_ e: APIError) -> String {34 switch e.code {35 case APIError.codeOffline: return tr("mutation.offline")36 case APIError.codeNetwork, APIError.codeTimeout: return tr("error.network")37 case "step_up_required": return tr("error.step_up")38 case "confirmation_mismatch": return tr("error.confirmation_mismatch")39 case "target_locked": return tr("error.target_locked")40 case "orchestrator_frozen": return tr("unavailable.frozen.message")41 case "rate_limited": return tr("error.rate_limited")42 default: return e.message43 }44 }45}4647extension View {48 func errorAlert(_ app: AppModel) -> some View { modifier(ErrorAlertModifier(app: app)) }4950 /// Disables a control that changes something when the client is offline or the role lacks `permission`, and51 /// says why in its tooltip. The server remains the one that authorizes.52 func mutation(_ app: AppModel, permission: String? = nil) -> some View {53 let blocker = app.mutationBlocker(permission: permission)54 return self.disabled(blocker != nil).help(blocker ?? "")55 }56}5758/// Standard page: title, optional subtitle and trailing controls, offline banner, scrollable content.59struct Page<Accessory: View, Content: View>: View {60 @Environment(AppModel.self) private var app61 @Environment(\.mccTheme) private var theme62 let title: String63 var subtitle: String?64 var scrolls = true65 @ViewBuilder var accessory: Accessory66 @ViewBuilder var content: Content6768 var body: some View {69 VStack(alignment: .leading, spacing: 0) {70 HStack(alignment: .firstTextBaseline, spacing: Tokens.Space.s4) {71 VStack(alignment: .leading, spacing: 2) {72 Text(title).font(.title2.weight(.semibold)).foregroundStyle(theme.textPrimary).accessibilityAddTraits(.isHeader)73 if let subtitle { Text(subtitle).font(.callout).foregroundStyle(theme.textSecondary) }74 }75 Spacer(minLength: Tokens.Space.s4)76 accessory77 }78 .padding(.horizontal, Tokens.Space.s7)79 .padding(.top, Tokens.Space.s5)80 .padding(.bottom, Tokens.Space.s4)81 if scrolls {82 ScrollView { content.padding(.horizontal, Tokens.Space.s7).padding(.bottom, Tokens.Space.s7).frame(maxWidth: .infinity, alignment: .topLeading) }83 } else {84 content.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading)85 }86 }87 .background(theme.canvas)88 }89}9091extension Page where Accessory == EmptyView {92 init(title: String, subtitle: String? = nil, scrolls: Bool = true, @ViewBuilder content: () -> Content) {93 self.title = title; self.subtitle = subtitle; self.scrolls = scrolls94 self.accessory = EmptyView(); self.content = content()95 }96}9798/// "Offline since 14:05 — read-only" across the top of the window.99struct OfflineBanner: View {100 @Environment(AppModel.self) private var app101 var body: some View {102 if app.session.isOffline {103 InfoBanner(tone: .offline,104 title: app.session.offlineSince.map { tr("offline.banner.since", Format.time($0)) } ?? tr("offline.banner.title"),105 message: tr("offline.banner.message"))106 .padding(.horizontal, Tokens.Space.s5)107 .padding(.top, Tokens.Space.s3)108 .accessibilityIdentifier("offline-banner")109 }110 }111}112113/// Small text button used in card headers.114struct LinkButton: View {115 let title: String116 let action: () -> Void117 var body: some View {118 Button(title, action: action).buttonStyle(.textLink).font(.callout)119 }120}121122/// Copies a value to the pasteboard.123struct CopyButton: View {124 let value: String125 var label: String = tr("action.copy")126 var body: some View {127 Button {128 NSPasteboard.general.clearContents()129 NSPasteboard.general.setString(value, forType: .string)130 } label: {131 Label(label, systemImage: "doc.on.doc")132 }133 .labelStyle(.iconOnly)134 .buttonStyle(.borderless)135 .help(label)136 .accessibilityLabel(label)137 }138}139140/// Segmented switch between the sub-pages of a section.141struct SectionTabs<Tab: Hashable & CaseIterable & Identifiable>: View where Tab.AllCases: RandomAccessCollection {142 @Binding var selection: Tab143 let title: (Tab) -> String144 var body: some View {145 Picker("", selection: $selection) {146 ForEach(Tab.allCases) { Text(title($0)).tag($0) }147 }148 .pickerStyle(.segmented)149 .labelsHidden()150 .fixedSize()151 }152}153154/// A two-column "label / content" grid row for detail panes.155struct DetailSection<Content: View>: View {156 let title: String157 @ViewBuilder var content: Content158 var body: some View {159 Card {160 VStack(alignment: .leading, spacing: Tokens.Space.s3) {161 SectionTitle(title)162 content163 }164 }165 }166}167168/// Gives an identity to a contract value for `Table` / `List` (the generated types are not `Identifiable`).169struct Row<Value>: Identifiable {170 let id: String171 let value: Value172}173174extension Array {175 func rows(_ id: (Element) -> String) -> [Row<Element>] { map { Row(id: id($0), value: $0) } }176}177178/// Text-only button in the accent colour. Used instead of `.buttonStyle(.textLink)`: AppKit's link buttons install179/// cursor tracking areas that outlive the view when a whole screen is swapped (crash in180/// `-[NSTrackingArea _dispatchMouseEntered:]` observed on macOS 27.0 after the invitation screen is replaced).181struct TextLinkStyle: ButtonStyle {182 @Environment(\.mccTheme) private var theme183 @Environment(\.isEnabled) private var isEnabled184 func makeBody(configuration: Configuration) -> some View {185 configuration.label186 .foregroundStyle(isEnabled ? theme.accent : theme.textDisabled)187 .opacity(configuration.isPressed ? 0.6 : 1)188 .contentShape(Rectangle())189 }190}191192extension ButtonStyle where Self == TextLinkStyle {193 static var textLink: TextLinkStyle { TextLinkStyle() }194}195196/// Detail pane on the trailing side of a screen. Used instead of `.inspector`, which on macOS 27.0 sends the window197/// into an endless "Update Constraints" pass (NSGenericException) when hosted in the detail column of the main198/// `NavigationSplitView`.199private struct SidePaneModifier<Pane: View>: ViewModifier {200 @Environment(\.mccTheme) private var theme201 @Binding var isPresented: Bool202 @ViewBuilder var pane: Pane203 func body(content: Content) -> some View {204 HStack(spacing: 0) {205 content.frame(maxWidth: .infinity, maxHeight: .infinity)206 if isPresented {207 Divider()208 pane209 .frame(maxHeight: .infinity, alignment: .top)210 .background(theme.surface)211 }212 }213 }214}215216extension View {217 func sidePane<Pane: View>(isPresented: Binding<Bool>, @ViewBuilder content: () -> Pane) -> some View {218 modifier(SidePaneModifier(isPresented: isPresented, pane: content))219 }220}221