← All repositories

reportage

A life-logging app for iPhone

git clone https://code.inqk.net/reportage.git


Reportage/Views/Theme/Theme.swift (5351B)

1import SwiftUI
2import UIKit
3
4/// The Almanac look: ink on paper, a serif for what the user wrote and a
5/// sans serif for everything around it, with one accent colour.
6enum Theme {
7    static let paper = Color(light: 0xF5F0E4, dark: 0x171513)
8    static let ink = Color(light: 0x1B1C1F, dark: 0xECEBE6)
9    /// Secondary text. At least 4.5:1 against `paper` in both modes.
10    static let muted = Color(light: 0x605B53, dark: 0xA29C93)
11    /// Hairlines between reports and unfilled progress.
12    static let rule = Color(light: 0xDAD2C3, dark: 0x38342E)
13    static let accent = Color(light: 0x8E3B22, dark: 0xE39577)
14    /// Text and icons drawn on `accent`.
15    static let onAccent = Color(light: 0xFFFFFF, dark: 0x171513)
16    /// The full stop of the 'Reportage.' wordmark: a brighter auburn than
17    /// `accent`, since a mark that small has to stand out by colour alone.
18    /// Decorative, so it needs only 3:1 against `paper`; it's 4.3:1.
19    static let mark = Color(light: 0xC4481A, dark: 0xF2724A)
20    /// Screen titles: a warm ink, softer than `ink` so the title doesn't
21    /// outweigh the reports beneath it.
22    static let titleInk = Color(light: 0x4A433B, dark: 0xC4BDB2)
23    /// Behind a text field, so it reads as somewhere to type: a faint wash
24    /// of `ink` on `paper`.
25    static let field = Color(light: 0xE8E3D8, dark: 0x242220)
26
27    // MARK: Spacing
28    //
29    // Named by role rather than size, so one role can change without the
30    // others that happen to share its value. Gaps tuned to particular fonts
31    // and the sizes of particular controls stay with their views.
32
33    /// The side margin of every screen.
34    static let margin: CGFloat = 24
35    /// Above the prompt on a survey page.
36    static let pageTop: CGFloat = 28
37    /// Between a survey prompt and its answer.
38    static let promptGap: CGFloat = 24
39    /// Between the parts of an answer: chosen tokens, the search field, the
40    /// map and the list.
41    static let blockGap: CGFloat = 20
42    /// Between items in a group: answer buttons, header buttons, a row's
43    /// columns.
44    static let itemGap: CGFloat = 12
45    /// Between an icon and its label.
46    static let iconGap: CGFloat = 10
47    /// Between chosen tokens, and between a token and its remove mark.
48    static let chipGap: CGFloat = 8
49    /// Between a title and the line of detail beneath it.
50    static let lineGap: CGFloat = 2
51    /// Above a section heading, such as a day in the timeline.
52    static let headingTop: CGFloat = 16
53    /// Above and below each report in the timeline.
54    static let rowPadding: CGFloat = 18
55
56    /// The serif, New York, at a Dynamic Type style.
57    static func serif(_ style: Font.TextStyle, weight: Font.Weight = .regular) -> Font {
58        .system(style, design: .serif, weight: weight)
59    }
60
61    /// The cap height of the system font at a text style's default size.
62    /// Scale it with `@ScaledMetric` relative to the same style.
63    static func capHeight(
64        _ style: UIFont.TextStyle, weight: UIFont.Weight,
65        design: UIFontDescriptor.SystemDesign = .default
66    ) -> CGFloat {
67        let traits = UITraitCollection(preferredContentSizeCategory: .large)
68        let base = UIFontDescriptor.preferredFontDescriptor(withTextStyle: style, compatibleWith: traits)
69            .addingAttributes([.traits: [UIFontDescriptor.TraitKey.weight: weight]])
70        let descriptor = base.withDesign(design) ?? base
71        return UIFont(descriptor: descriptor, size: 0).capHeight
72    }
73}
74
75extension VerticalAlignment {
76    private enum CapTop: AlignmentID {
77        static func defaultValue(in dimensions: ViewDimensions) -> CGFloat {
78            dimensions[.top]
79        }
80    }
81
82    /// The top of a text's capitals, for lining up texts of different sizes
83    /// by their tops. A text sets it to its first baseline less its cap
84    /// height.
85    static let capTop = VerticalAlignment(CapTop.self)
86}
87
88extension Color {
89    /// A colour that follows the light and dark appearance, from hex RGB.
90    init(light: UInt32, dark: UInt32) {
91        self.init(
92            uiColor: UIColor { traits in
93                UIColor(rgb: traits.userInterfaceStyle == .dark ? dark : light)
94            })
95    }
96}
97
98extension UIColor {
99    fileprivate convenience init(rgb: UInt32) {
100        self.init(
101            red: CGFloat((rgb >> 16) & 0xFF) / 255,
102            green: CGFloat((rgb >> 8) & 0xFF) / 255,
103            blue: CGFloat(rgb & 0xFF) / 255,
104            alpha: 1
105        )
106    }
107}
108
109extension View {
110    /// A screen's title, such as 'Reportage' over the timeline: the serif in
111    /// the softer title ink.
112    func screenTitle() -> some View {
113        font(Theme.serif(.largeTitle, weight: .medium))
114            .foregroundStyle(Theme.titleInk)
115            .accessibilityAddTraits(.isHeader)
116    }
117
118    /// Room between the end of a scrolling screen and the tab bar, which
119    /// otherwise sits right against the last row's rule.
120    func tabBarClearance() -> some View {
121        contentMargins(.bottom, Theme.blockGap, for: .scrollContent)
122    }
123
124    /// The small spaced capitals used for labels such as dates and report
125    /// kinds.
126    func eyebrow(_ color: Color = Theme.muted) -> some View {
127        font(.caption.weight(.semibold))
128            .tracking(1.4)
129            .textCase(.uppercase)
130            .foregroundStyle(color)
131    }
132}