SPB Git

Repository Public

SPMetrics.swift 7.1 KB 166 lines swift

1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Sources/Shared/Design/SPMetrics.swift3// Description: SP Mail Premium spacing, radii, motion (interruptible springs, nothing under Reduce Motion), typography mapped to Dynamic Type, list density and haptics.4import SwiftUI5#if os(iOS)6import UIKit7#endif89enum SPSpacing {10    static let xxs: CGFloat = 2, xs: CGFloat = 4, s: CGFloat = 6, m: CGFloat = 8, l: CGFloat = 12, xl: CGFloat = 16, xxl: CGFloat = 20, x3: CGFloat = 24, x4: CGFloat = 32, x5: CGFloat = 40, x6: CGFloat = 5611}1213enum SPRadius {14    static let chip: CGFloat = 6, card: CGFloat = 10, dialog: CGFloat = 14, pill: CGFloat = 99915}1617/// Durations ≤ 240 ms, always interruptible springs. Callers go through `spAnimation` so that Reduce Motion removes them.18enum SPMotion {19    static let press = Animation.snappy(duration: 0.12)20    static let quick = Animation.smooth(duration: 0.18)21    static let pane = Animation.snappy(duration: 0.24)22    static let fade = Animation.easeOut(duration: 0.1)23    /// The animation to use for a state change, or nil when motion is reduced.24    static func resolved(_ animation: Animation, reduceMotion: Bool) -> Animation? { reduceMotion ? nil : animation }25}2627private struct SPAnimationModifier<V: Equatable>: ViewModifier {28    let animation: Animation29    let value: V30    @Environment(\.accessibilityReduceMotion) private var reduceMotion31    func body(content: Content) -> some View { content.animation(reduceMotion ? nil : animation, value: value) }32}3334extension View {35    /// `.animation(_:value:)` that disappears under Reduce Motion.36    func spAnimation<V: Equatable>(_ animation: Animation = SPMotion.quick, value: V) -> some View { modifier(SPAnimationModifier(animation: animation, value: value)) }37}3839@MainActor40func spWithAnimation(_ animation: Animation = SPMotion.quick, _ body: () -> Void) {41    #if os(iOS)42    let reduce = UIAccessibility.isReduceMotionEnabled43    #else44    let reduce = NSWorkspace.shared.accessibilityDisplayShouldReduceMotion45    #endif46    if reduce { body() } else { withAnimation(animation, body) }47}4849// MARK: - typography5051/// The reading font of message bodies.52enum ReadingFont: String, CaseIterable, Identifiable, Sendable {53    case system, serif, mono54    var id: String { rawValue }55    var design: Font.Design { self == .serif ? .serif : self == .mono ? .monospaced : .default }56    var title: String { switch self { case .system: L("Système"); case .serif: L("Avec empattements"); case .mono: L("Chasse fixe") } }57    /// The CSS family used inside the isolated HTML renderer.58    var cssFamily: String {59        switch self {60        case .system: "-apple-system, system-ui, sans-serif"61        case .serif: "ui-serif, 'New York', Georgia, serif"62        case .mono: "ui-monospace, 'SF Mono', Menlo, monospace"63        }64    }65}6667/// Roles of the type scale, each mapped to a Dynamic Type text style (§3 of the design system).68enum SPFont {69    static let display = Font.largeTitle.weight(.bold)70    static let title = Font.title2.weight(.semibold)71    static let headline = Font.headline72    static let body = Font.body73    static let bodyStrong = Font.body.weight(.semibold)74    static let callout = Font.subheadline75    static let caption = Font.caption.weight(.medium).monospacedDigit()76    static let mono = Font.body.monospaced()77    static func reading(_ font: ReadingFont, size: CGFloat) -> Font { .system(size: size, design: font.design) }78}7980// MARK: - density8182/// List density. Touch platforms never go below 44 pt targets.83enum Density: String, CaseIterable, Identifiable, Sendable {84    case comfortable, cozy, compact85    var id: String { rawValue }86    var title: String { switch self { case .comfortable: L("Confortable"); case .cozy: L("Intermédiaire"); case .compact: L("Compacte") } }8788    /// Minimum row height before Dynamic Type grows the content.89    var minRowHeight: CGFloat {90        #if os(iOS)91        switch self { case .comfortable: 72; case .cozy: 60; case .compact: 44 }92        #else93        switch self { case .comfortable: 58; case .cozy: 46; case .compact: 28 }94        #endif95    }96    var verticalPadding: CGFloat {97        #if os(iOS)98        switch self { case .comfortable: 12; case .cozy: 8; case .compact: 6 }99        #else100        switch self { case .comfortable: 9; case .cozy: 6; case .compact: 4 }101        #endif102    }103    var avatarSize: CGFloat {104        #if os(iOS)105        switch self { case .comfortable: 40; case .cozy: 34; case .compact: 26 }106        #else107        switch self { case .comfortable: 32; case .cozy: 26; case .compact: 18 }108        #endif109    }110    /// Compact rows are a single line: sender, subject and date side by side.111    var isSingleLine: Bool { self == .compact }112}113114// MARK: - haptics115116/// Haptic feedback on iOS (selection for swipes, success on an accepted send, warning on a failure). No-op on macOS.117@MainActor118enum SPHaptics {119    static func selection() {120        #if os(iOS)121        UISelectionFeedbackGenerator().selectionChanged()122        #endif123    }124    static func success() {125        #if os(iOS)126        UINotificationFeedbackGenerator().notificationOccurred(.success)127        #endif128    }129    static func warning() {130        #if os(iOS)131        UINotificationFeedbackGenerator().notificationOccurred(.warning)132        #endif133    }134    static func impact() {135        #if os(iOS)136        UIImpactFeedbackGenerator(style: .light).impactOccurred()137        #endif138    }139}140141// MARK: - layout142143/// Lays its subviews out on one line and wraps to the next when they no longer fit (badges, chips) — nothing is truncated at large text sizes.144struct WrapLayout: Layout {145    var spacing: CGFloat = 6146    func sizeThatFits(proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) -> CGSize { arrange(width: proposal.width ?? .infinity, subviews: subviews).size }147    func placeSubviews(in bounds: CGRect, proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) {148        let r = arrange(width: bounds.width, subviews: subviews)149        for (i, p) in r.points.enumerated() { subviews[i].place(at: CGPoint(x: bounds.minX + p.x, y: bounds.minY + p.y), proposal: ProposedViewSize(width: min(r.sizes[i].width, bounds.width), height: nil)) }150    }151    private func arrange(width: CGFloat, subviews: Subviews) -> (size: CGSize, points: [CGPoint], sizes: [CGSize]) {152        var x: CGFloat = 0, y: CGFloat = 0, row: CGFloat = 0, widest: CGFloat = 0, rowStart = 0153        var points: [CGPoint] = [], sizes: [CGSize] = []154        // items of one row are centred on the tallest of them155        func closeRow(_ end: Int) { for i in rowStart..<end { points[i].y += (row - sizes[i].height) / 2 } }156        for (n, v) in subviews.enumerated() {157            let s = v.sizeThatFits(ProposedViewSize(width: width.isFinite ? width : nil, height: nil))158            if x > 0, x + s.width > width { closeRow(n); rowStart = n; x = 0; y += row + spacing; row = 0 }159            points.append(CGPoint(x: x, y: y)); sizes.append(s)160            x += s.width + spacing; row = max(row, s.height); widest = max(widest, x - spacing)161        }162        closeRow(subviews.count)163        return (CGSize(width: width.isFinite ? width : widest, height: y + row), points, sizes)164    }165}166
Files main
Menu

Theme