Repository Public
1// SP Notes Core2// File: Sources/SPNotesFeatures/Work/WorkShared.swift3// Description: Small pieces shared by the "work" screens (tasks, inbox, databases, search): option colours mapped on the4// design tones, a wrapping chip layout, the undo bar, the field style, the highlighted search snippet, the5// inspector / sheet container of a detail.6//7// Author: Simon-Pierre Boucher8// Contact: contact@spboucher.ai9import SPNotesDesign10import SPNotesStore11import SwiftUI1213enum WorkTone {14 /// The design tone of a server option colour name (`green`, `red`, `blue`…). Violet is reserved for AI: purple and15 /// pink options use the accent.16 static func tone(_ name: String?) -> NVTone {17 switch name?.lowercased() {18 case "green": return .success19 case "red": return .danger20 case "orange", "yellow", "brown": return .warning21 case "blue", "purple", "pink": return .accent22 default: return .neutral23 }24 }25}2627/// Lays children out left to right and wraps (chips, tags).28struct WorkFlow: Layout {29 var spacing: CGFloat = NVSpace.xs3031 func sizeThatFits(proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) -> CGSize {32 arrange(width: proposal.width ?? .infinity, subviews: subviews).size33 }3435 func placeSubviews(in bounds: CGRect, proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) {36 let result = arrange(width: bounds.width, subviews: subviews)37 for (index, origin) in result.origins.enumerated() {38 subviews[index].place(at: CGPoint(x: bounds.minX + origin.x, y: bounds.minY + origin.y), proposal: ProposedViewSize(width: min(result.sizes[index].width, bounds.width), height: nil))39 }40 }4142 private func arrange(width: CGFloat, subviews: Subviews) -> (size: CGSize, origins: [CGPoint], sizes: [CGSize]) {43 var origins: [CGPoint] = [], sizes: [CGSize] = []44 var x: CGFloat = 0, y: CGFloat = 0, rowHeight: CGFloat = 0, maxX: CGFloat = 045 for subview in subviews {46 let size = subview.sizeThatFits(.unspecified)47 if x > 0, x + size.width > width { x = 0; y += rowHeight + spacing; rowHeight = 0 }48 origins.append(CGPoint(x: x, y: y)); sizes.append(size)49 x += size.width + spacing50 rowHeight = max(rowHeight, size.height)51 maxX = max(maxX, x - spacing)52 }53 return (CGSize(width: min(maxX, width), height: y + rowHeight), origins, sizes)54 }55}5657/// Text field chrome of the work screens.58struct WorkFieldStyle: ViewModifier {59 func body(content: Content) -> some View {60 content61 .textFieldStyle(.plain)62 .nvFont(.body)63 .foregroundStyle(NVColor.fg)64 .padding(.horizontal, NVSpace.md)65 .frame(minHeight: 36)66 .background(NVColor.bgElevated, in: NVRadius.shape(NVRadius.sm))67 .overlay(NVRadius.shape(NVRadius.sm).strokeBorder(NVColor.lineStrong, lineWidth: 1))68 }69}7071extension View {72 func workField() -> some View { modifier(WorkFieldStyle()) }7374 /// Minimum 44 pt touch target on compact layouts.75 func workHitTarget(_ compact: Bool) -> some View { frame(minWidth: compact ? 44 : nil, minHeight: compact ? 44 : nil) }76}7778/// The bar that offers to undo the last action (or just confirms it).79struct WorkUndoBar: View {80 let message: String81 var undo: (() -> Void)?82 let dismiss: () -> Void8384 var body: some View {85 HStack(spacing: NVSpace.md) {86 Image(systemName: "checkmark.circle.fill").foregroundStyle(NVColor.success).accessibilityHidden(true)87 Text(message).nvFont(.ui).foregroundStyle(NVColor.fg).fixedSize(horizontal: false, vertical: true)88 Spacer(minLength: NVSpace.sm)89 if let undo {90 Button(L("Annuler"), action: undo).buttonStyle(.plain).nvFont(.uiStrong).foregroundStyle(NVColor.accent)91 }92 Button(action: dismiss) { Image(systemName: "xmark").foregroundStyle(NVColor.fgMuted) }93 .buttonStyle(.plain)94 .accessibilityLabel(L("Fermer"))95 }96 .padding(.horizontal, NVSpace.lg)97 .padding(.vertical, NVSpace.md)98 .nvPop(radius: NVRadius.md)99 .padding(NVSpace.lg)100 }101}102103/// An inline, calm failure of an action (a write refused or impossible offline).104struct WorkActionFailure: View {105 let failure: FeatureFailure106 let dismiss: () -> Void107108 var body: some View {109 HStack(alignment: .firstTextBaseline, spacing: NVSpace.sm) {110 Image(systemName: failure.isOffline ? "wifi.slash" : "exclamationmark.triangle")111 .foregroundStyle(failure.isOffline ? NVColor.fgMuted : NVColor.danger)112 .accessibilityHidden(true)113 Text(failure.message).nvFont(.ui).foregroundStyle(failure.isOffline ? NVColor.fgMuted : NVColor.dangerText).fixedSize(horizontal: false, vertical: true)114 Spacer(minLength: NVSpace.sm)115 Button(action: dismiss) { Image(systemName: "xmark").foregroundStyle(NVColor.fgMuted) }116 .buttonStyle(.plain)117 .accessibilityLabel(L("Fermer"))118 }119 .padding(.horizontal, NVSpace.md)120 .padding(.vertical, NVSpace.sm)121 .background(NVColor.bgSunken, in: NVRadius.shape(NVRadius.md))122 .overlay(NVRadius.shape(NVRadius.md).strokeBorder(NVColor.line, lineWidth: 1))123 .accessibilityElement(children: .combine)124 }125}126127/// A snippet whose matched terms (between the store's highlight markers) are emphasised.128struct WorkHighlightedText: View {129 let snippet: String130131 /// The plain text and the ranges to emphasise. Exposed for tests.132 static func segments(_ snippet: String) -> [(text: String, isMatch: Bool)] {133 var out: [(String, Bool)] = []134 var current = ""135 var inMatch = false136 for character in snippet {137 if character == SearchHit.highlightStart || character == SearchHit.highlightEnd {138 if !current.isEmpty { out.append((current, inMatch)) }139 current = ""140 inMatch = character == SearchHit.highlightStart141 } else {142 current.append(character)143 }144 }145 if !current.isEmpty { out.append((current, inMatch)) }146 return out147 }148149 var body: some View {150 var attributed = AttributedString()151 for segment in Self.segments(snippet) {152 var part = AttributedString(segment.text)153 if segment.isMatch {154 part.foregroundColor = NVColor.fg155 part.backgroundColor = NVColor.accentSoft156 part.inlinePresentationIntent = .stronglyEmphasized157 }158 attributed.append(part)159 }160 return Text(attributed).nvFont(.ui).foregroundStyle(NVColor.fgMuted)161 }162}163164/// Container of a detail: a side inspector on regular, the content of a sheet on compact.165struct WorkDetailContainer<Content: View>: View {166 let title: String167 let close: () -> Void168 @ViewBuilder var content: () -> Content169170 var body: some View {171 VStack(alignment: .leading, spacing: 0) {172 HStack {173 Text(title).nvFont(.h3).foregroundStyle(NVColor.fg).lineLimit(2).accessibilityAddTraits(.isHeader)174 Spacer(minLength: NVSpace.sm)175 FeatureIconButton("xmark", label: L("Fermer"), action: close)176 }177 .padding(NVSpace.lg)178 Divider().overlay(NVColor.line)179 ScrollView { content().padding(NVSpace.lg).frame(maxWidth: .infinity, alignment: .leading) }180 }181 .background(NVColor.bgElevated)182 }183}184185/// A labelled row of a detail form.186struct WorkFormRow<Content: View>: View {187 let label: String188 @ViewBuilder var content: () -> Content189190 var body: some View {191 VStack(alignment: .leading, spacing: NVSpace.xs) {192 Text(label).nvFont(.micro).foregroundStyle(NVColor.fgMuted).textCase(.uppercase)193 content()194 }195 .frame(maxWidth: .infinity, alignment: .leading)196 }197}198199enum WorkDay {200 /// `YYYY-MM-DD` of an instant in a time zone.201 static func string(_ date: Date, timeZone: TimeZone) -> String {202 var calendar = Calendar(identifier: .gregorian)203 calendar.timeZone = timeZone204 let c = calendar.dateComponents([.year, .month, .day], from: date)205 return String(format: "%04d-%02d-%02d", c.year ?? 0, c.month ?? 0, c.day ?? 0)206 }207208 /// The instant of a `YYYY-MM-DD` at a given hour in a time zone (nil when malformed).209 static func date(_ day: String, hour: Int = 0, timeZone: TimeZone) -> Date? {210 let parts = day.split(separator: "-").compactMap { Int($0) }211 guard parts.count == 3 else { return nil }212 var calendar = Calendar(identifier: .gregorian)213 calendar.timeZone = timeZone214 return calendar.date(from: DateComponents(year: parts[0], month: parts[1], day: parts[2], hour: hour))215 }216217 static let utc = TimeZone(identifier: "UTC") ?? .gmt218}219