SPB Git

Repository Public MIT

GlassSpacing.swift 4.7 KB 121 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project          : SP GlassKit7// File             : Sources/SPGlassTokens/GlassSpacing.swift8// Module           : SPGlassTokens9// Repository       : https://git.spboucher.ai/sp-glasskit10// Project Website  : https://glass.spboucher.ai11// Created          : 2026-12-0212// Last Modified    : 2026-12-0213//14// Author           : Simon-Pierre Boucher15// Position         : Professor of Finance16// Institution      : Université du Québec en Outaouais (UQO)17// Organization     : SP Boucher18// Website          : https://spboucher.ai19// Git              : https://git.spboucher.ai20//21// Preferred Contact: contact@spboucher.ai22// Development      : dev@spboucher.ai23// Support          : support@spboucher.ai24// Security/Admin   : admin@spboucher.ai25//26// Copyright (c) 202627// Simon-Pierre Boucher28// All rights reserved where applicable. This file is governed by the29// applicable project license, copyright notices and relevant law.30// Existing third-party rights and notices remain unaffected.31//32// License          : MIT33//34// Do not embed credentials, private keys, access tokens,35// certificates or confidential personal information.36//37// =============================================================================38// END OF SP BOUCHER SOURCE HEADER39// =============================================================================4041import Foundation4243/// Steps of the spacing scale.44public enum GlassSpacingStep: String, Codable, CaseIterable, Sendable {45    case xxs, xs, sm, md, lg, xl, xxl46}4748/// Spacing scale in points: 4, 8, 12, 16, 24, 32, 48 by default.49public struct GlassSpacing: Hashable, Codable, Sendable {50    public var xxs: Double51    public var xs: Double52    public var sm: Double53    public var md: Double54    public var lg: Double55    public var xl: Double56    public var xxl: Double5758    public init(xxs: Double = 4, xs: Double = 8, sm: Double = 12, md: Double = 16, lg: Double = 24, xl: Double = 32, xxl: Double = 48) {59        self.xxs = xxs; self.xs = xs; self.sm = sm; self.md = md; self.lg = lg; self.xl = xl; self.xxl = xxl60    }6162    public init(from decoder: any Decoder) throws {63        let c = try decoder.container(keyedBy: CodingKeys.self)64        let d = GlassSpacing()65        xxs = try c.decodeIfPresent(Double.self, forKey: .xxs) ?? d.xxs66        xs = try c.decodeIfPresent(Double.self, forKey: .xs) ?? d.xs67        sm = try c.decodeIfPresent(Double.self, forKey: .sm) ?? d.sm68        md = try c.decodeIfPresent(Double.self, forKey: .md) ?? d.md69        lg = try c.decodeIfPresent(Double.self, forKey: .lg) ?? d.lg70        xl = try c.decodeIfPresent(Double.self, forKey: .xl) ?? d.xl71        xxl = try c.decodeIfPresent(Double.self, forKey: .xxl) ?? d.xxl72    }7374    public subscript(step: GlassSpacingStep) -> Double {75        switch step {76        case .xxs: xxs77        case .xs: xs78        case .sm: sm79        case .md: md80        case .lg: lg81        case .xl: xl82        case .xxl: xxl83        }84    }8586    public var values: [Double] { GlassSpacingStep.allCases.map { self[$0] } }8788    /// The scale multiplied by a density factor, rounded to whole points.89    public func scaled(by factor: Double) -> GlassSpacing {90        func s(_ v: Double) -> Double { (v * factor).rounded() }91        return GlassSpacing(xxs: s(xxs), xs: s(xs), sm: s(sm), md: s(md), lg: s(lg), xl: s(xl), xxl: s(xxl))92    }9394    public static let standard = GlassSpacing()95}9697/// Corner radii for surfaces GlassKit draws itself. System controls keep the radii chosen by the system.98public struct GlassRadii: Hashable, Codable, Sendable {99    public var small: Double100    public var medium: Double101    public var large: Double102    public var extraLarge: Double103104    public init(small: Double = 8, medium: Double = 14, large: Double = 22, extraLarge: Double = 30) {105        self.small = small; self.medium = medium; self.large = large; self.extraLarge = extraLarge106    }107108    public init(from decoder: any Decoder) throws {109        let c = try decoder.container(keyedBy: CodingKeys.self)110        let d = GlassRadii()111        small = try c.decodeIfPresent(Double.self, forKey: .small) ?? d.small112        medium = try c.decodeIfPresent(Double.self, forKey: .medium) ?? d.medium113        large = try c.decodeIfPresent(Double.self, forKey: .large) ?? d.large114        extraLarge = try c.decodeIfPresent(Double.self, forKey: .extraLarge) ?? d.extraLarge115    }116117    public var values: [Double] { [small, medium, large, extraLarge] }118119    public static let standard = GlassRadii()120}121
Files main
Menu

Theme