919 lines · 40.5 KB · SwiftRaw
// COTTON DATA'S LOOK (assets/design/data/DECISIONS.md "The look", data.html `:root`): Stone by day, Night by dark,// Days' blue the accent, Instrument Sans for words and JetBrains Mono for values, ids, amounts and table and column// names. Thin 1 pt rules and space; no cards, shadows or gradients. The accent marks only where to act, an open// assistant window and the switches. Every Data screen draws from this file; the colours are the core's// `look {app: data}` as the person's Look stands (`LookModel`), the same as Android's `Stone.kt` and `Bits.kt`.#if os(iOS)import CoreTextimport CottonWritingimport SwiftUIimport UIKit/// data.html's `:root`, as Look stands: the chosen paper's half for the phone's appearance, in the chosen ink. Before/// the core has ever answered, Stone by day and Night by dark (DECISIONS "The look").@MainActorenum Stone { static var look: LookModel { LookModel.standing("data") } private static var night: Bool { LookAppearance.shared.systemDark } private static func token(_ name: String, _ day: UInt32, _ dark: UInt32) -> Color { look.colour(name, night ? dark : day) } static var ground: Color { token("ground", 0xEFF1EA, 0x141812) } static var ink: Color { token("ink", 0x1B2119, 0xE5EAE0) } static var quiet: Color { token("quiet", 0x576052, 0xA3AC9C) } /// Never for words that must be read: placeholders, dimmed lines, empty drawings. static var faint: Color { token("faint", 0x8A9285, 0x6E7768) } static var rule: Color { token("rule", 0xD2D7CB, 0x283025) } /// Days' blue: the main button, an open assistant window, the switches, the fill's bar. static var accent: Color { token("accent", 0x1D3FD8, 0x8EA2FF) } static var onAccent: Color { token("on_accent", 0xFFFFFF, 0x0E1017) } /// Behind a sheet. static var veil: Color { ink.opacity(look.groundIsLight ? 0.22 : 0.5) } /// `.ph` padding: the screen's side margin. static let side: CGFloat = 20}/// Instrument Sans and JetBrains Mono (SIL OFL, `assets/fonts/look`), variable on 'wght', registered once.enum DataFonts { enum Kind { case sans, mono } private static let weightAxis = 0x7767_6874 private static let registered: [String: String] = { var names: [String: String] = [:] for file in ["InstrumentSans", "JetBrainsMono"] { guard let url = Bundle.main.url(forResource: file, withExtension: "ttf", subdirectory: "look") else { continue } CTFontManagerRegisterFontsForURL(url as CFURL, .process, nil) let descriptors = CTFontManagerCreateFontDescriptorsFromURL(url as CFURL) as? [CTFontDescriptor] ?? [] if let name = descriptors.first.flatMap({ CTFontDescriptorCopyAttribute($0, kCTFontNameAttribute) as? String }) { names[file] = name } } return names }() static func wght(_ weight: UIFont.Weight) -> Double { switch weight { case .medium: 500 case .semibold: 600 case .bold, .heavy, .black: 700 default: 400 } } /// The face at `size`, not yet scaled; the system's when the file is not in this build. static func ui(_ kind: Kind, _ size: CGFloat, _ weight: UIFont.Weight) -> UIFont { guard let name = registered[kind == .mono ? "JetBrainsMono" : "InstrumentSans"] else { return kind == .mono ? .monospacedSystemFont(ofSize: size, weight: weight) : .systemFont(ofSize: size, weight: weight) } let variation = UIFontDescriptor.AttributeName(rawValue: kCTFontVariationAttribute as String) return UIFont(descriptor: UIFontDescriptor(name: name, size: size).addingAttributes([variation: [weightAxis: wght(weight)]]), size: size) }}/// data.html's type. Every size follows Look's size and the phone's text size, up to the largest accessibility size;/// another face chosen in Look stands in for Instrument Sans, never for the mono (values and names stay in it).@MainActorenum DataType { static func sans(_ size: CGFloat, _ weight: UIFont.Weight = .regular, _ style: UIFont.TextStyle = .body) -> Font { Font(uiSans(size, weight, style)) } static func uiSans(_ size: CGFloat, _ weight: UIFont.Weight = .regular, _ style: UIFont.TextStyle = .body) -> UIFont { let look = Stone.look let size = size * look.scale if look.face != LookFaces.own { return WritingFonts.font(LookFaces.drawn(look.face, own: "sans"), size: size, weight: weight) } return UIFontMetrics(forTextStyle: style).scaledFont(for: DataFonts.ui(.sans, size, weight)) } /// JetBrains Mono, whatever Look's face. static func mono(_ size: CGFloat, _ weight: UIFont.Weight = .regular, _ style: UIFont.TextStyle = .body) -> Font { Font(UIFontMetrics(forTextStyle: style).scaledFont(for: DataFonts.ui(.mono, size * Stone.look.scale, weight))) } /// Instrument Sans itself (Look's chip for the app's own face). static func own(_ size: CGFloat) -> Font { Font(UIFontMetrics(forTextStyle: .body).scaledFont(for: DataFonts.ui(.sans, size, .regular))) } /// `.ph h3`: a screen's title, 31 bold; a table's name 29 in the mono. static var title: Font { sans(31, .bold, .largeTitle) } static var monoTitle: Font { mono(27, .medium, .largeTitle) } /// `.r .t`: a row's name, 17 semibold; a table's name 16 mono. static var name: Font { sans(17, .semibold) } static var monoName: Font { mono(16, .medium) } /// `.r .s`: a second line, 14 quiet. static var line: Font { sans(14, .regular, .subheadline) } static var monoLine: Font { mono(13, .regular, .footnote) } static var body: Font { sans(16, .regular, .callout) } static var heading: Font { sans(15, .bold, .subheadline) } static var small: Font { sans(14, .semibold, .subheadline) } static var button: Font { sans(17, .semibold) }}/// The words of the `data` line set (`ShellLines`), the same as Android's.enum DataWords { static func line(_ path: String, _ fills: [String: String] = [:]) -> String { ShellLines.line("data", path, fills) }}// MARK: - The marks/// data.html's line marks (`Marks.kt`): one 22-unit box, stroke 1.6, round ends, in ink. A mark says nothing to/// VoiceOver: the row or button around it is named from the core's words.enum DataMark: String { case search, back, next, down, close, info, lock, unlock, app, assistant, phone, clock, link, broken, pin, photo, file, open, tick, sort, calendar, out, `in`, table, wifi, warn, cloud var path: String { switch self { case .search: "M3.5 9.5a6 6 0 1 0 12 0a6 6 0 1 0 -12 0M14 14L19 19" case .back: "M13.5 4.5L7 11l6.5 6.5" case .next: "M8.5 4.5L15 11l-6.5 6.5" case .down: "M5 8.5l6 6l6 -6" case .close: "M5.5 5.5l11 11M16.5 5.5l-11 11" case .info: "M2.5 11a8.5 8.5 0 1 0 17 0a8.5 8.5 0 1 0 -17 0M11 10v5.5M11 6.8v0.4" case .lock: "M6.5 9.5h9a2 2 0 0 1 2 2v5a2 2 0 0 1 -2 2h-9a2 2 0 0 1 -2 -2v-5a2 2 0 0 1 2 -2zM7.5 9.5V7a3.5 3.5 0 0 1 7 0v2.5" case .unlock: "M6.5 9.5h9a2 2 0 0 1 2 2v5a2 2 0 0 1 -2 2h-9a2 2 0 0 1 -2 -2v-5a2 2 0 0 1 2 -2zM7.5 9.5V7a3.5 3.5 0 0 1 6.8 -1.2" case .app: "M6 4.5h10a1.5 1.5 0 0 1 1.5 1.5v10a1.5 1.5 0 0 1 -1.5 1.5h-10a1.5 1.5 0 0 1 -1.5 -1.5v-10a1.5 1.5 0 0 1 1.5 -1.5z" case .assistant: "M4.5 5h13a1 1 0 0 1 1 1v8.5a1 1 0 0 1 -1 1H9l-4 3v-3h-0.5a1 1 0 0 1 -1 -1V6a1 1 0 0 1 1 -1zM8 10.2v0.3M11 10.2v0.3M14 10.2v0.3" case .phone: "M8 3h6a1.5 1.5 0 0 1 1.5 1.5v13a1.5 1.5 0 0 1 -1.5 1.5h-6a1.5 1.5 0 0 1 -1.5 -1.5v-13a1.5 1.5 0 0 1 1.5 -1.5zM10 16.5h2" case .clock: "M2.5 11a8.5 8.5 0 1 0 17 0a8.5 8.5 0 1 0 -17 0M11 6.5V11l3 2" case .link: "M9.5 12.5a3.5 3.5 0 0 0 5 0l3 -3a3.5 3.5 0 0 0 -5 -5l-1 1M12.5 9.5a3.5 3.5 0 0 0 -5 0l-3 3a3.5 3.5 0 0 0 5 5l1 -1" case .broken: "M12.5 5.5l1 -1a3.5 3.5 0 0 1 5 5l-1 1M9.5 16.5l-1 1a3.5 3.5 0 0 1 -5 -5l1 -1M7 3.5v2M3.5 7h2M15 18.5v-2M18.5 15h-2" case .pin: "M11 19.5s-6 -5.6 -6 -10a6 6 0 0 1 12 0c0 4.4 -6 10 -6 10zM8.8 9.5a2.2 2.2 0 1 0 4.4 0a2.2 2.2 0 1 0 -4.4 0" case .photo: "M4.5 5h13a1.5 1.5 0 0 1 1.5 1.5v9a1.5 1.5 0 0 1 -1.5 1.5h-13a1.5 1.5 0 0 1 -1.5 -1.5v-9a1.5 1.5 0 0 1 1.5 -1.5zM3 14l4.5 -4l4 3.5l2.5 -2l5 4M13.5 8.5v0.3" case .file: "M5 3h8l4 4v12H5zM13 3v4h4" case .open: "M12 4h6v6M18 4l-8 8M15 13v5H4V7h5" case .tick: "M6.8 11.2l2.8 2.8l5.6 -5.6" case .sort: "M7 4v14M3.5 7.5L7 4l3.5 3.5M15 18V4M11.5 14.5L15 18l3.5 -3.5" case .calendar: "M4.5 5.5h13a1 1 0 0 1 1 1v11a1 1 0 0 1 -1 1h-13a1 1 0 0 1 -1 -1v-11a1 1 0 0 1 1 -1zM3.5 9.5h15M7.5 3.5v3M14.5 3.5v3" case .out: "M3 11h3M8 11a2 2 0 1 0 4 0a2 2 0 1 0 -4 0M12 11h7M16 8l3 3l-3 3" case .in: "M3 11h10M10 8l3 3l-3 3M15 11a2 2 0 1 0 4 0a2 2 0 1 0 -4 0" case .table: "M4.5 4.5h13a1 1 0 0 1 1 1v11a1 1 0 0 1 -1 1h-13a1 1 0 0 1 -1 -1v-11a1 1 0 0 1 1 -1zM3.5 9h15M3.5 13h15M9.5 4.5v13" case .wifi: "M3 8.5a11.5 11.5 0 0 1 16 0M5.8 11.5a7.5 7.5 0 0 1 10.4 0M8.6 14.5a3.5 3.5 0 0 1 4.8 0M11 17.5v0.3" case .warn: "M11 3.5l8.5 15h-17zM11 9v4.5M11 15.8v0.3" case .cloud: "M6.5 17h9.5a3.5 3.5 0 0 0 0.5 -7a5.5 5.5 0 0 0 -10.6 1.2A3 3 0 0 0 6.5 17z" } } /// The mark About's facts name (`data.about` `facts[].icon`). static func of(fact icon: String?) -> DataMark { switch icon { case "lock": .lock case "open": .unlock case "app": .app case "assistant": .assistant case "phone": .phone case "clock": .clock default: .info } }}/// One drawn mark, `size` points square, in `ink`; it grows with the text size, to half again and a bit.struct DataGlyph: View { let mark: DataMark var ink: Color = Stone.ink var size: CGFloat = 22 /// Stroked in the foreground around it (a button's label) rather than `ink`. var foreground = false @ScaledMetric(relativeTo: .body) private var grow: CGFloat = 1 var body: some View { let side = size * min(max(grow, 1), 1.6) Canvas { context, box in let scale = box.width / 22 let transform = CGAffineTransform(scaleX: scale, y: scale) if foreground { context.stroke(SVGPath.parse(mark.path).applying(transform), with: .foreground, style: StrokeStyle(lineWidth: 1.6 * scale, lineCap: .round, lineJoin: .round)) } else if mark == .tick { // The filled tick: the disc in ink, the tick cut through it in the ground. context.fill(Path(ellipseIn: CGRect(x: 2, y: 2, width: 18, height: 18)).applying(transform), with: .color(ink)) context.stroke(SVGPath.parse(mark.path).applying(transform), with: .color(Stone.ground), style: StrokeStyle(lineWidth: 2 * scale, lineCap: .round, lineJoin: .round)) } else { context.stroke(SVGPath.parse(mark.path).applying(transform), with: .color(ink), style: StrokeStyle(lineWidth: 1.6 * scale, lineCap: .round, lineJoin: .round)) } } .frame(width: side, height: side) .accessibilityHidden(true) }}// MARK: - The pieces every screen shares (Bits.kt)/// A table's name, as stored, breaking only after an underscore when it must wrap: at the largest text/// "event_attendee" broke inside the word ("event_attende" / "e", walk of 26 Nov). A zero-width space after each/// underscore lets the line break there; the name read aloud is unchanged.enum DataWrap { static func atUnderscores(_ name: String) -> String { name.replacingOccurrences(of: "_", with: "_\u{200B}") }}/// A screen's title (`.ph h3`); a table's name in the mono, as stored, wrapping and never cut.struct DataTitle: View { let text: String var mono = false var body: some View { Text(verbatim: mono ? DataWrap.atUnderscores(text) : text) .font(mono ? DataType.monoTitle : DataType.title) .tracking(mono ? 0 : -0.5) .foregroundStyle(Stone.ink) .fixedSize(horizontal: false, vertical: true) .frame(maxWidth: .infinity, alignment: .leading) .padding(.top, 6).padding(.bottom, 2) .accessibilityAddTraits(.isHeader) }}/// The quiet line under a title (`.lead`).struct DataLead: View { let text: String var body: some View { Text(verbatim: text).font(DataType.body).foregroundStyle(Stone.quiet) .fixedSize(horizontal: false, vertical: true) .frame(maxWidth: .infinity, alignment: .leading) .padding(.top, 4) }}/// A screen whose first answer has not come: said at once, never a blank page (DATA iPhone 3). "Loading…" (or the/// screen's own words for it), and after a while that the database is slow to answer and it is still asking.struct DataWaiting: View { var words: String = DataWords.line("wait.loading") var tag = "data-loading" @State private var slow = false var body: some View { VStack(alignment: .leading, spacing: 6) { HStack(spacing: 10) { ProgressView().controlSize(.small).tint(Stone.quiet) Text(verbatim: words).font(DataType.body).foregroundStyle(Stone.quiet) .fixedSize(horizontal: false, vertical: true) } if slow { Text(verbatim: DataWords.line("wait.slow")).font(DataType.line).foregroundStyle(Stone.quiet) .fixedSize(horizontal: false, vertical: true) .accessibilityIdentifier("\(tag)-slow") } } .frame(maxWidth: .infinity, alignment: .leading) .padding(.top, 16) .accessibilityElement(children: .combine) .accessibilityIdentifier(tag) .task { try? await Task.sleep(for: .seconds(8)) guard !Task.isCancelled else { return } slow = true UIAccessibility.post(notification: .announcement, argument: DataWords.line("wait.slow")) } }}/// A read that failed, in the core's own words, with Try again.struct DataFailed: View { let words: String var tag = "data-failed" let again: () -> Void var body: some View { VStack(alignment: .leading, spacing: 0) { DataNotice(mark: .cloud, title: words, tag: tag) Button(action: again) { Text(verbatim: DataWords.line("wait.again")) } .buttonStyle(DataButtonStyle()) .padding(.top, 12) .accessibilityIdentifier("\(tag)-again") } }}/// A small quiet note (`.note`).struct DataNote: View { let text: String var center = false var body: some View { Text(verbatim: text).font(DataType.line).foregroundStyle(Stone.quiet) .lineSpacing(3) .multilineTextAlignment(center ? .center : .leading) .fixedSize(horizontal: false, vertical: true) .frame(maxWidth: .infinity, alignment: center ? .center : .leading) .padding(.top, 12) }}/// A part's heading with what it counts at its end (`.gh`: 15 bold, a rule below).struct DataHeading: View { let text: String var end: String? var body: some View { VStack(spacing: 0) { HStack(alignment: .lastTextBaseline, spacing: 10) { Text(verbatim: text).font(DataType.heading).foregroundStyle(Stone.ink) .fixedSize(horizontal: false, vertical: true) .frame(maxWidth: .infinity, alignment: .leading) if let end { Text(verbatim: end).font(DataType.line).foregroundStyle(Stone.quiet).multilineTextAlignment(.trailing) } } .padding(.bottom, 6) .accessibilityElement(children: .combine) .accessibilityAddTraits(.isHeader) DataRule() } .padding(.top, 22) }}/// A quiet small heading (`.ph h4`).struct DataSmall: View { let text: String var body: some View { Text(verbatim: text).font(DataType.small).foregroundStyle(Stone.quiet) .fixedSize(horizontal: false, vertical: true) .frame(maxWidth: .infinity, alignment: .leading) .padding(.top, 22).padding(.bottom, 4) .accessibilityAddTraits(.isHeader) }}/// A thin rule across.struct DataRule: View { var colour: Color = Stone.rule var thick = false var body: some View { Rectangle().fill(colour).frame(height: thick ? 2 : 1).accessibilityHidden(true) }}/// The bar at the top: a back word with its chevron on the left, what stands at the end on the right.struct DataBar<End: View>: View { let back: String let tap: () -> Void @ViewBuilder var end: () -> End var body: some View { HStack(spacing: 8) { Button(action: tap) { HStack(spacing: 2) { DataGlyph(mark: .back, size: 20) Text(verbatim: back).font(DataType.sans(16)).lineLimit(1) } .foregroundStyle(Stone.ink) .frame(minHeight: 44).contentShape(Rectangle()) } .buttonStyle(.plain) .accessibilityLabel(DataWords.line("a11y.back")) .accessibilityValue(back) .accessibilityIdentifier("data-back") Spacer(minLength: 0) end() } .frame(minHeight: 48) }}extension DataBar where End == EmptyView { init(back: String, tap: @escaping () -> Void) { self.init(back: back, tap: tap) { EmptyView() } }}/// `.btn`: 52 tall, rounded 14; `go` is the screen's one accent button.struct DataButtonStyle: ButtonStyle { var go = false func makeBody(configuration: Configuration) -> some View { configuration.label .font(DataType.button) .multilineTextAlignment(.center) .foregroundStyle(go ? Stone.onAccent : Stone.ink) .frame(maxWidth: .infinity, minHeight: 52) .padding(.horizontal, 16) .background(RoundedRectangle(cornerRadius: 14).fill(go ? Stone.accent : .clear)) .overlay(RoundedRectangle(cornerRadius: 14).strokeBorder(go ? Stone.accent : Stone.rule, lineWidth: 1)) .contentShape(RoundedRectangle(cornerRadius: 14)) .opacity(configuration.isPressed ? 0.6 : 1) }}/// A button with a mark before its words.struct DataMarkedLabel: View { let words: String var mark: DataMark? var body: some View { HStack(spacing: 8) { if let mark { DataGlyph(mark: mark, size: 18, foreground: true) } Text(verbatim: words) } }}/// A filter chip (`.chip`): a pill with a quiet edge, the one on in ink and bold.struct DataChip: View { let label: String let on: Bool var mark: DataMark? var tag: String? var tap: (() -> Void)? var body: some View { let inner = HStack(spacing: 6) { if let mark { DataGlyph(mark: mark, size: 16) } Text(verbatim: label).font(DataType.sans(14, on ? .bold : .regular, .subheadline)) .foregroundStyle(on ? Stone.ink : Stone.quiet) .fixedSize(horizontal: false, vertical: true) } .padding(.horizontal, 12).padding(.vertical, 6) .overlay(Capsule().strokeBorder(on ? Stone.ink : Stone.rule, lineWidth: on ? 1.5 : 1)) if let tap { Button(action: tap) { inner.frame(minHeight: 44).contentShape(Rectangle()) } .buttonStyle(.plain) .accessibilityAddTraits(on ? [.isSelected] : []) .accessibilityIdentifier(tag ?? "") } else { inner.padding(.vertical, 6).accessibilityElement(children: .combine) } }}/// Chips, wrapping onto as many lines as the words need.struct DataChips<Content: View>: View { @ViewBuilder let content: () -> Content var body: some View { DataFlow(spacing: 8, lineSpacing: 0) { content() } .frame(maxWidth: .infinity, alignment: .leading) .padding(.top, 6) }}/// A flowing layout: each piece beside the last while it fits, else on a new line.struct DataFlow: Layout { var spacing: CGFloat = 8 var lineSpacing: CGFloat = 6 func sizeThatFits(proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) -> CGSize { let width = proposal.width ?? .infinity var x: CGFloat = 0, y: CGFloat = 0, line: CGFloat = 0, widest: CGFloat = 0 for view in subviews { let size = view.sizeThatFits(ProposedViewSize(width: width, height: nil)) if x > 0, x + size.width > width { x = 0; y += line + lineSpacing; line = 0 } x += size.width + spacing widest = max(widest, x - spacing) line = max(line, size.height) } return CGSize(width: min(widest, width), height: y + line) } func placeSubviews(in bounds: CGRect, proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) { var x = bounds.minX, y = bounds.minY, line: CGFloat = 0 for view in subviews { let size = view.sizeThatFits(ProposedViewSize(width: bounds.width, height: nil)) if x > bounds.minX, x + size.width > bounds.maxX { x = bounds.minX; y += line + lineSpacing; line = 0 } view.place(at: CGPoint(x: x, y: y), proposal: ProposedViewSize(width: min(size.width, bounds.width), height: size.height)) x += size.width + spacing line = max(line, size.height) } }}/// A table's name boxed small in the mono (`.tag`), as the drawing names a table inside words.struct DataTableTag: View { let name: String var body: some View { Text(verbatim: name).font(DataType.mono(12, .regular, .caption1)).foregroundStyle(Stone.quiet) .padding(.horizontal, 5).padding(.vertical, 1) .overlay(RoundedRectangle(cornerRadius: 5).strokeBorder(Stone.rule, lineWidth: 1)) }}/// A row of a list (`.r`): a name (mono for a table) with what stands after it, its second line, and what stands at/// the end (a size, a date). Each wraps; at the accessibility sizes the end stands under the line.struct DataListRow<After: View>: View { let title: String var line: String? var end: String? var monoTitle = false var monoEnd = true var faint = false var lead: DataMark? var chevron = false var last = false var tag: String? var label: String? var tap: (() -> Void)? let after: () -> After @Environment(\.dynamicTypeSize) private var size init(title: String, line: String? = nil, end: String? = nil, monoTitle: Bool = false, monoEnd: Bool = true, faint: Bool = false, lead: DataMark? = nil, chevron: Bool = false, last: Bool = false, tag: String? = nil, label: String? = nil, tap: (() -> Void)?, @ViewBuilder after: @escaping () -> After) { self.title = title; self.line = line; self.end = end; self.monoTitle = monoTitle; self.monoEnd = monoEnd; self.faint = faint self.lead = lead; self.chevron = chevron; self.last = last; self.tag = tag; self.label = label; self.tap = tap; self.after = after } var body: some View { VStack(spacing: 0) { Group { if let tap { Button(action: tap) { content.contentShape(Rectangle()) }.buttonStyle(.plain) } else { content } } .accessibilityElement(children: .ignore) .accessibilityLabel(label ?? [title, line, end].compactMap { $0 }.filter { !$0.isEmpty }.joined(separator: ", ")) .accessibilityAddTraits(tap == nil ? [] : .isButton) .accessibilityIdentifier(tag ?? "") if !last { DataRule() } } } private var content: some View { HStack(alignment: .center, spacing: 10) { if let lead { DataGlyph(mark: lead, size: 18).frame(width: 26) } VStack(alignment: .leading, spacing: 2) { HStack(alignment: .firstTextBaseline, spacing: 6) { Text(verbatim: monoTitle ? DataWrap.atUnderscores(title) : title) .font(monoTitle ? DataType.monoName : DataType.name) .foregroundStyle(faint ? Stone.faint : Stone.ink) .fixedSize(horizontal: false, vertical: true) after() } if let line, !line.isEmpty { Text(verbatim: line).font(DataType.line).foregroundStyle(faint ? Stone.faint : Stone.quiet) .fixedSize(horizontal: false, vertical: true) } if size.isAccessibilitySize, let end { Text(verbatim: end).font(monoEnd ? DataType.monoLine : DataType.line).foregroundStyle(Stone.quiet) .fixedSize(horizontal: false, vertical: true) } } .frame(maxWidth: .infinity, alignment: .leading) if !size.isAccessibilitySize, let end { Text(verbatim: end).font(monoEnd ? DataType.monoLine : DataType.line).foregroundStyle(Stone.quiet) .multilineTextAlignment(.trailing) .frame(maxWidth: 120, alignment: .trailing) .fixedSize(horizontal: false, vertical: true) } if chevron { DataGlyph(mark: .next, ink: Stone.quiet, size: 18) } } .padding(.vertical, 12) .frame(minHeight: 44) }}extension DataListRow where After == EmptyView { init(title: String, line: String? = nil, end: String? = nil, monoTitle: Bool = false, monoEnd: Bool = true, faint: Bool = false, lead: DataMark? = nil, chevron: Bool = false, last: Bool = false, tag: String? = nil, label: String? = nil, tap: (() -> Void)? = nil) { self.init(title: title, line: line, end: end, monoTitle: monoTitle, monoEnd: monoEnd, faint: faint, lead: lead, chevron: chevron, last: last, tag: tag, label: label, tap: tap) { EmptyView() } }}/// A plain two-sided row (`.r.plain`): words on the left, a quiet value or chevron on the right.struct DataPlainRow: View { let left: String var right: String? var chevron = false var last = false var tag: String? var monoRight = false var tap: (() -> Void)? @Environment(\.dynamicTypeSize) private var size var body: some View { VStack(spacing: 0) { Group { if let tap { Button(action: tap) { content.contentShape(Rectangle()) }.buttonStyle(.plain) } else { content } } .accessibilityElement(children: .ignore) .accessibilityLabel([left, right].compactMap { $0 }.joined(separator: ", ")) .accessibilityAddTraits(tap == nil ? [] : .isButton) .accessibilityIdentifier(tag ?? "") if !last { DataRule() } } } private var content: some View { HStack(spacing: 12) { if size.isAccessibilitySize { VStack(alignment: .leading, spacing: 2) { Text(verbatim: left).font(DataType.body).foregroundStyle(Stone.ink).fixedSize(horizontal: false, vertical: true) if let right { Text(verbatim: right).font(monoRight ? DataType.monoLine : DataType.line).foregroundStyle(Stone.quiet).fixedSize(horizontal: false, vertical: true) } } .frame(maxWidth: .infinity, alignment: .leading) } else { Text(verbatim: left).font(DataType.body).foregroundStyle(Stone.ink) .fixedSize(horizontal: false, vertical: true) .frame(maxWidth: .infinity, alignment: .leading) if let right { Text(verbatim: right).font(monoRight ? DataType.monoLine : DataType.line).foregroundStyle(Stone.quiet) .multilineTextAlignment(.trailing).frame(maxWidth: 220, alignment: .trailing) .fixedSize(horizontal: false, vertical: true) } } if chevron { DataGlyph(mark: .next, ink: Stone.quiet, size: 18) } } .padding(.vertical, 12) .frame(minHeight: 44) }}/// A switch (`.tog`): on is filled with the accent, its knob the ground. VoiceOver hears it as the phone's switch.struct DataToggleStyle: ToggleStyle { @Environment(\.accessibilityReduceMotion) private var still func makeBody(configuration: Configuration) -> some View { Button { configuration.isOn.toggle() } label: { HStack(spacing: 12) { configuration.label.frame(maxWidth: .infinity, alignment: .leading) ZStack(alignment: configuration.isOn ? .trailing : .leading) { Capsule().fill(configuration.isOn ? Stone.accent : .clear) Capsule().strokeBorder(configuration.isOn ? Stone.accent : Stone.rule, lineWidth: 1.5) Circle().fill(configuration.isOn ? Stone.ground : Stone.faint).frame(width: 17, height: 17).padding(.horizontal, 4) } .frame(width: 44, height: 26) .animation(still ? nil : .easeOut(duration: 0.16), value: configuration.isOn) } .padding(.vertical, 12) .contentShape(Rectangle()) } .buttonStyle(.plain) }}/// A row that is a switch: its words, its second line under them.struct DataToggleRow: View { let label: String let on: Bool var line: String? var strong = false var last = false var tag: String let change: @MainActor (Bool) -> Void var body: some View { VStack(spacing: 0) { Toggle(isOn: Binding(get: { on }, set: { value in change(value) })) { VStack(alignment: .leading, spacing: 2) { Text(verbatim: label).font(DataType.sans(strong ? 17 : 16, strong ? .bold : .semibold)).foregroundStyle(Stone.ink) .fixedSize(horizontal: false, vertical: true) if let line { Text(verbatim: line).font(DataType.line).foregroundStyle(Stone.quiet).fixedSize(horizontal: false, vertical: true) } } } .toggleStyle(DataToggleStyle()) .accessibilityIdentifier(tag) if !last { DataRule() } } }}/// The search field (`.search`): a quiet edge, the mark, the words, and a clear mark once typed.struct DataSearchBox: View { @Binding var text: String let placeholder: String var tag: String @FocusState private var focused: Bool var body: some View { HStack(spacing: 8) { DataGlyph(mark: .search, ink: Stone.quiet, size: 18) TextField(text: $text, prompt: Text(verbatim: placeholder).foregroundStyle(Stone.quiet)) { Text(verbatim: placeholder) } .font(DataType.body) .foregroundStyle(Stone.ink) .tint(Stone.accent) .autocorrectionDisabled() .textInputAutocapitalization(.never) .submitLabel(.search) .focused($focused) .padding(.vertical, 10) .accessibilityIdentifier(tag) if !text.isEmpty { Button { text = "" } label: { DataGlyph(mark: .close, ink: Stone.quiet, size: 18).frame(minWidth: 44, minHeight: 44) } .buttonStyle(.plain) .accessibilityLabel(DataWords.line("a11y.close")) .accessibilityIdentifier("\(tag)-clear") } } .padding(.horizontal, 12) .overlay(RoundedRectangle(cornerRadius: 14).strokeBorder(text.isEmpty ? Stone.rule : Stone.ink, lineWidth: text.isEmpty ? 1 : 1.5)) .padding(.top, 10) }}/// A weight line (`.wt`): a thin bar whose ink part is `share` of the whole.struct DataWeight: View { let share: Double var colour: Color = Stone.ink var thick: CGFloat = 3 var body: some View { GeometryReader { box in ZStack(alignment: .leading) { Capsule().fill(Stone.rule) Capsule().fill(colour).frame(width: box.size.width * min(max(share, 0), 1)) } } .frame(height: thick) .accessibilityHidden(true) }}/// A line mark beside words (`.fact`): About's facts.struct DataFact: View { let mark: DataMark let title: String var line: String? var last = false var tag: String? var tap: (() -> Void)? var body: some View { VStack(spacing: 0) { Group { if let tap { Button(action: tap) { content.contentShape(Rectangle()) }.buttonStyle(.plain) } else { content } } .accessibilityElement(children: .ignore) .accessibilityLabel([title, line].compactMap { $0 }.joined(separator: ", ")) .accessibilityAddTraits(tap == nil ? [] : .isButton) .accessibilityIdentifier(tag ?? "") if !last { DataRule() } } } private var content: some View { HStack(alignment: .top, spacing: 8) { DataGlyph(mark: mark, size: 20).frame(width: 28, alignment: .leading).padding(.top, 1) VStack(alignment: .leading, spacing: 2) { Text(verbatim: title).font(DataType.name).foregroundStyle(Stone.ink).fixedSize(horizontal: false, vertical: true) if let line, !line.isEmpty { Text(verbatim: line).font(DataType.line).foregroundStyle(Stone.quiet).lineSpacing(3).fixedSize(horizontal: false, vertical: true) } } .frame(maxWidth: .infinity, alignment: .leading) if tap != nil { DataGlyph(mark: .next, ink: Stone.quiet, size: 18) } } .padding(.vertical, 12) }}/// A thick ink rule over a notice (`.notice`: Locked, Can't reach your database), its mark, title and line.struct DataNotice: View { let mark: DataMark let title: String var line: String? var tag: String? var body: some View { VStack(alignment: .leading, spacing: 0) { DataRule(colour: Stone.ink, thick: true) VStack(alignment: .leading, spacing: 4) { HStack(spacing: 8) { DataGlyph(mark: mark, size: 18) Text(verbatim: title).font(DataType.sans(17, .bold)).foregroundStyle(Stone.ink).fixedSize(horizontal: false, vertical: true) } if let line { Text(verbatim: line).font(DataType.sans(15)).foregroundStyle(Stone.quiet).fixedSize(horizontal: false, vertical: true) } } .padding(.vertical, 14) .accessibilityElement(children: .combine) DataRule() } .padding(.top, 12) .accessibilityIdentifier(tag ?? "") }}/// THE ONE DELIGHT (DECISIONS "One row": As stored): a value typed out as it really sits in the row, a character at a/// time, the caret following. With Reduce Motion on it simply stands. The whole value stands unseen beneath, so the/// field never changes height while it types.struct DataTypeOut: View { let text: String var wait: Double = 0 @Environment(\.accessibilityReduceMotion) private var still @State private var shown = 0 var body: some View { let font = DataType.mono(15, .regular, .callout) ZStack(alignment: .topLeading) { Text(verbatim: text).font(font).foregroundStyle(.clear) Text(verbatim: still ? text : String(text.prefix(shown)) + (shown < text.count ? "\u{258D}" : "")) .font(font).foregroundStyle(Stone.ink) } .fixedSize(horizontal: false, vertical: true) .frame(maxWidth: .infinity, alignment: .leading) .accessibilityElement(children: .ignore) .accessibilityLabel(text) .task(id: text) { guard !still else { return } shown = 0 try? await Task.sleep(for: .milliseconds(Int(wait * 1000))) // A long value is typed in steps, so no field takes more than about a second. let step = max(text.count / 60, 1) while shown < text.count, !Task.isCancelled { try? await Task.sleep(for: .milliseconds(14)) shown = min(shown + step, text.count) } } }}/// A sheet up from the foot: the veil over the screen, the ground with a rounded top and a thin edge, a grip, and what/// it holds, scrolling when tall. A tap on the veil, a swipe down, or VoiceOver's escape closes it.struct DataSheet<Content: View>: View { let close: () -> Void var tag: String @ViewBuilder let content: () -> Content @State private var drag: CGFloat = 0 @State private var tall: CGFloat = 0 var body: some View { GeometryReader { screen in ZStack(alignment: .bottom) { Stone.veil.ignoresSafeArea() .onTapGesture(perform: close) .accessibilityHidden(true) VStack(spacing: 0) { Capsule().fill(Stone.rule).frame(width: 36, height: 4).padding(.top, 10).padding(.bottom, 8) .frame(maxWidth: .infinity) .contentShape(Rectangle()) .accessibilityHidden(true) ScrollView { content() .padding(.horizontal, Stone.side) .padding(.bottom, 24) .onGeometryChange(for: CGFloat.self) { $0.size.height } action: { tall = $0 } } .scrollBounceBehavior(.basedOnSize) .frame(height: min(max(tall, 1), screen.size.height * 0.86)) } .frame(maxWidth: .infinity) .background(UnevenRoundedRectangle(topLeadingRadius: 24, topTrailingRadius: 24).fill(Stone.ground).ignoresSafeArea(edges: .bottom)) .overlay(alignment: .top) { UnevenRoundedRectangle(topLeadingRadius: 24, topTrailingRadius: 24).stroke(Stone.rule, lineWidth: 1) .frame(height: 28).mask(Rectangle().frame(height: 28)) .accessibilityHidden(true) } .offset(y: max(0, drag)) .simultaneousGesture(DragGesture(minimumDistance: 14) .onChanged { move in if move.translation.height > abs(move.translation.width) { drag = move.translation.height } } .onEnded { end in if end.translation.height > 80 || end.predictedEndTranslation.height > 240 { close() } withAnimation(.easeOut(duration: 0.2)) { drag = 0 } }) .accessibilityElement(children: .contain) .accessibilityIdentifier(tag) .accessibilityAction(.escape, close) } } }}/// A line passing at the foot ("Copied"), said to VoiceOver as it comes.struct DataToast: View { let words: String var body: some View { Text(verbatim: words) .font(DataType.sans(15)) .foregroundStyle(Stone.ink) .fixedSize(horizontal: false, vertical: true) .frame(maxWidth: 460, alignment: .leading) .padding(.horizontal, 16).padding(.vertical, 12) .background(RoundedRectangle(cornerRadius: 14).fill(Stone.ground)) .overlay(RoundedRectangle(cornerRadius: 14).strokeBorder(Stone.ink, lineWidth: 1)) .padding(.horizontal, Stone.side) .accessibilityIdentifier("data-toast") .onAppear { UIAccessibility.post(notification: .announcement, argument: words) } }}/// Runs of words, each in its own font and colour, as one text that wraps as one.@MainActorfunc dataRuns(_ runs: [(String, Font, Color)]) -> Text { var all = AttributedString() for (words, font, colour) in runs { var run = AttributedString(words) run.font = font run.foregroundColor = colour all += run } return Text(all)}/// The fill's bar in the accent (thick on its own page, thin atop the Tables list).struct DataFillBar: View { let percent: Int var thick = false var body: some View { DataWeight(share: Double(percent) / 100, colour: Stone.accent, thick: thick ? 6 : 3) }}/// A screen of its own: its column, scrolling, at the side margin, on the ground.struct DataPage<Content: View>: View { var tag: String @ViewBuilder let content: () -> Content var body: some View { ScrollView { VStack(alignment: .leading, spacing: 0) { content() } .padding(.horizontal, Stone.side) .padding(.bottom, 32) .frame(maxWidth: .infinity, alignment: .leading) } .scrollDismissesKeyboard(.interactively) // A page scrolled up stops at the status bar: its words never show behind the clock. .clipped() .background(Stone.ground) .accessibilityIdentifier(tag) }}#endif