summaryrefslogtreecommitdiff
path: root/Hutch/App/ThemeManager.swift
blob: ee74921f4ae2519be54e191bce34749b50a4a2e5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import SwiftUI

// MARK: - Theme & Density Types

enum AppTheme: String, CaseIterable, Identifiable {
    case system
    case light
    case dark

    var id: String { rawValue }

    var label: String {
        switch self {
        case .system: "System"
        case .light: "Light"
        case .dark: "Dark"
        }
    }

    var colorScheme: ColorScheme? {
        switch self {
        case .system: nil
        case .light: .light
        case .dark: .dark
        }
    }
}

enum DisplayDensity: String, CaseIterable, Identifiable {
    case standard
    case compact

    var id: String { rawValue }

    var label: String {
        switch self {
        case .standard: "Standard"
        case .compact: "Compact"
        }
    }
}

// MARK: - Environment Keys

private struct DisplayDensityKey: EnvironmentKey {
    static let defaultValue: DisplayDensity = .standard
}

extension EnvironmentValues {
    var displayDensity: DisplayDensity {
        get { self[DisplayDensityKey.self] }
        set { self[DisplayDensityKey.self] = newValue }
    }
}

// MARK: - Themed List Modifier

/// Combined modifier for List/Form that applies compact section spacing.
/// Apply once per List or Form.
struct ThemedListStyle: ViewModifier {
    @Environment(\.displayDensity) private var density

    func body(content: Content) -> some View {
        content
            .listSectionSpacing(density == .compact ? .compact : .default)
            .contentMargins(.vertical, density == .compact ? 2 : 8, for: .scrollContent)
            .controlSize(density == .compact ? .small : .regular)
    }
}

extension View {
    /// Apply themed appearance (density spacing) to a List or Form.
    func themedList() -> some View {
        modifier(ThemedListStyle())
    }
}