krz/hutch

an ios client for sourcehut

clone: git clone https://gitbay.org/krz/hutch.git

v3.0.0: Hutch/App/ThemeManager.swift · raw

 1import SwiftUI
 2
 3// MARK: - Theme & Density Types
 4
 5enum AppTheme: String, CaseIterable, Identifiable {
 6    case system
 7    case light
 8    case dark
 9
10    var id: String { rawValue }
11
12    var label: String {
13        switch self {
14        case .system: "System"
15        case .light: "Light"
16        case .dark: "Dark"
17        }
18    }
19
20    var colorScheme: ColorScheme? {
21        switch self {
22        case .system: nil
23        case .light: .light
24        case .dark: .dark
25        }
26    }
27}
28
29enum DisplayDensity: String, CaseIterable, Identifiable {
30    case standard
31    case compact
32
33    var id: String { rawValue }
34
35    var label: String {
36        switch self {
37        case .standard: "Standard"
38        case .compact: "Compact"
39        }
40    }
41}
42
43// MARK: - Environment Keys
44
45private struct DisplayDensityKey: EnvironmentKey {
46    static let defaultValue: DisplayDensity = .standard
47}
48
49extension EnvironmentValues {
50    var displayDensity: DisplayDensity {
51        get { self[DisplayDensityKey.self] }
52        set { self[DisplayDensityKey.self] = newValue }
53    }
54}
55
56// MARK: - Themed List Modifier
57
58/// Combined modifier for List/Form that applies compact section spacing.
59/// Apply once per List or Form.
60struct ThemedListStyle: ViewModifier {
61    @Environment(\.displayDensity) private var density
62
63    func body(content: Content) -> some View {
64        content
65            .listSectionSpacing(density == .compact ? .compact : .default)
66            .contentMargins(.vertical, density == .compact ? 2 : 8, for: .scrollContent)
67            .controlSize(density == .compact ? .small : .regular)
68    }
69}
70
71extension View {
72    /// Apply themed appearance (density spacing) to a List or Form.
73    func themedList() -> some View {
74        modifier(ThemedListStyle())
75    }
76}