Cotton code
184 lines · 7.5 KB · SwiftRaw
// The theme reader: a writing theme as the core answers it, every colour// named by its role. The surface, the list row and the settings samples// read colours from here and nowhere else — none is written in Swift.import Foundationpublic struct WritingTheme: Hashable, Sendable, Identifiable {    public var id: String    public var name: String    /// Drawn for the phone's dark appearance.    public var dark: Bool    public var colours: Colours    /// One appearance's colours, by role (`notes.themes`, `writing.json`).    public struct Colours: Codable, Hashable, Sendable {        /// The page.        public var ground: String        /// The words.        public var text: String        /// The title and headings.        public var heading: String        /// Previews, dates, quotes, a mark at the cursor, the hint.        public var secondary: String        /// The one accent: the compose button, the caret, a bullet, a box, a tag, a link.        public var accent: String        /// Hairlines between rows, a divider, a table's grid.        public var rule: String        public var highlight: String        /// Code's ground, and a raised surface: the bar, the panel, a field.        public var codeGround: String        public var selection: String        /// Links: Notes' paper's text colour, drawn underlined so a link is never taken for the ink; the accent where        /// a theme names none.        public var link: String?        /// Deleting for good, Empty bin, every act a sheet marks red: the paper's red, whatever the ink (Notes CONTRACT        /// §10 "Ink"); the accent where a theme names none.        public var danger: String?        enum CodingKeys: String, CodingKey {            case ground, text, heading, secondary, accent, rule, highlight, selection, link, danger            case codeGround = "code_ground"        }        public init(ground: String, text: String, heading: String, secondary: String, accent: String, rule: String,                    highlight: String, codeGround: String, selection: String, link: String? = nil, danger: String? = nil) {            self.ground = ground            self.text = text            self.heading = heading            self.secondary = secondary            self.accent = accent            self.rule = rule            self.highlight = highlight            self.codeGround = codeGround            self.selection = selection            self.link = link            self.danger = danger        }    }    public init(id: String, name: String, dark: Bool, colours: Colours) {        self.id = id        self.name = name        self.dark = dark        self.colours = colours    }}/// The person's typography, as the core keeps it (`notes.settings`).public struct WritingType: Hashable, Sendable {    /// The face's id: `sans` (the bundled Nunito Sans), `figtree` (Notes'),    /// `serif`, `mono`, `system`.    public var face: String    /// Code's face: `mono` (the phone's), or `jetbrains` (Notes').    public var code: String = "mono"    /// The space after a paragraph, as a multiple of the body's size.    public var paragraph: Double = 0.15    /// The body's size in points at the phone's default text size; the    /// phone's text size scales it.    public var size: Double    /// The body's line height as a multiple of its size.    public var lineHeight: Double    /// The widest the text column grows, in characters of the body; 0 is the    /// screen's whole width.    public var width: Double    /// The title's and each heading level's size, as a multiple of the body's.    public var scale: Scale    /// A list row's sizes.    public var list: ListSizes    public struct Scale: Hashable, Sendable {        public var title: Double        public var h1: Double        public var h2: Double        public var h3: Double        public init(title: Double, h1: Double, h2: Double, h3: Double) {            self.title = title            self.h1 = h1            self.h2 = h2            self.h3 = h3        }        /// The surface's own steps: a title well above a heading, headings a        /// little above the body.        public static let standard = Scale(title: 1.6, h1: 1.4, h2: 1.22, h3: 1.08)    }    public struct ListSizes: Hashable, Sendable {        public var title: Double        public var preview: Double        public var date: Double        public init(title: Double, preview: Double, date: Double) {            self.title = title            self.preview = preview            self.date = date        }        /// A row's sizes follow the body's.        public static func standard(_ size: Double) -> ListSizes {            ListSizes(title: size, preview: (size * 0.88).rounded(), date: max(11, (size * 0.72).rounded()))        }    }    public init(face: String, size: Double, lineHeight: Double, width: Double, scale: Scale = .standard, list: ListSizes? = nil,                code: String = "mono", paragraph: Double = 0.15) {        self.face = face        self.code = code        self.paragraph = paragraph        self.size = size        self.lineHeight = lineHeight        self.width = width        self.scale = scale        self.list = list ?? .standard(size)    }}/// `#RRGGBB` or `#RRGGBBAA` as red, green, blue and alpha in 0…1; nil for/// anything else.public func writingRGBA(_ hex: String) -> (Double, Double, Double, Double)? {    var s = hex.trimmingCharacters(in: .whitespaces)    if s.hasPrefix("#") { s.removeFirst() }    guard s.count == 6 || s.count == 8, let v = UInt64(s, radix: 16) else { return nil }    if s.count == 6 {        return (Double((v >> 16) & 0xFF) / 255, Double((v >> 8) & 0xFF) / 255, Double(v & 0xFF) / 255, 1)    }    return (Double((v >> 24) & 0xFF) / 255, Double((v >> 16) & 0xFF) / 255, Double((v >> 8) & 0xFF) / 255, Double(v & 0xFF) / 255)}#if canImport(UIKit)import SwiftUIimport UIKitpublic extension WritingTheme {    /// A role's colour. A colour the core wrote wrongly is drawn magenta, so    /// it is seen and fixed, never quietly replaced.    static func ui(_ hex: String) -> UIColor {        guard let (r, g, b, a) = writingRGBA(hex) else { return .magenta }        return UIColor(red: r, green: g, blue: b, alpha: a)    }    var ground: UIColor { Self.ui(colours.ground) }    var ink: UIColor { Self.ui(colours.text) }    var heading: UIColor { Self.ui(colours.heading) }    var dim: UIColor { Self.ui(colours.secondary) }    /// The quietest words: the empty page's hint, a mark at the cursor.    var faint: UIColor { dim.withAlphaComponent(0.7) }    var rule: UIColor { Self.ui(colours.rule) }    var accent: UIColor { Self.ui(colours.accent) }    /// What is drawn on the accent: the page's own colour.    var onAccent: UIColor { ground }    /// A tag chip: the accent, faint, under the accent's words.    var chip: UIColor { accent.withAlphaComponent(dark ? 0.24 : 0.13) }    var chipInk: UIColor { accent }    var highlight: UIColor { Self.ui(colours.highlight) }    var code: UIColor { Self.ui(colours.codeGround) }    var link: UIColor { colours.link.map(Self.ui) ?? accent }    /// A red act's colour: the paper's red, whatever the ink.    public var danger: UIColor { colours.danger.map(Self.ui) ?? accent }    var check: UIColor { accent }    var selection: UIColor { Self.ui(colours.selection) }    /// A raised surface: the bar, the panel, the search field, a round button.    var sheet: UIColor { code }    var bar: UIColor { sheet }    func color(_ ui: UIColor) -> Color { Color(uiColor: ui) }}#endif