blob: af540944d16564de77b62c6aba15dbb3248c01c1 (
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
import SwiftUI
// MARK: - Theme & Density Types
enum AppTheme: String, CaseIterable, Identifiable {
case system
case light
case dark
case amoled
var id: String { rawValue }
var label: String {
switch self {
case .system: "System"
case .light: "Light"
case .dark: "Dark"
case .amoled: "AMOLED"
}
}
var colorScheme: ColorScheme? {
switch self {
case .system: nil
case .light: .light
case .dark: .dark
case .amoled: .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
}
private struct IsAMOLEDThemeKey: EnvironmentKey {
static let defaultValue: Bool = false
}
extension EnvironmentValues {
var displayDensity: DisplayDensity {
get { self[DisplayDensityKey.self] }
set { self[DisplayDensityKey.self] = newValue }
}
var isAMOLEDTheme: Bool {
get { self[IsAMOLEDThemeKey.self] }
set { self[IsAMOLEDThemeKey.self] = newValue }
}
}
// MARK: - Themed List Modifier
/// Combined modifier for List/Form that applies compact section spacing and AMOLED black backgrounds.
/// Apply once per List or Form.
struct ThemedListStyle: ViewModifier {
@Environment(\.displayDensity) private var density
@Environment(\.isAMOLEDTheme) private var isAMOLED
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)
.modifier(AMOLEDListBackground(isAMOLED: isAMOLED))
}
}
/// Hides the default grouped list background and replaces it with true black for OLED screens.
private struct AMOLEDListBackground: ViewModifier {
let isAMOLED: Bool
func body(content: Content) -> some View {
if isAMOLED {
content
.scrollContentBackground(.hidden)
.background(Color.black)
} else {
content
}
}
}
extension View {
/// Apply themed appearance (density spacing) to a List or Form.
func themedList() -> some View {
modifier(ThemedListStyle())
}
}
// MARK: - Themed Row Modifier
struct ThemedRowStyle: ViewModifier {
@Environment(\.isAMOLEDTheme) private var isAMOLED
func body(content: Content) -> some View {
content
.listRowBackground(isAMOLED ? Color.black : nil)
}
}
extension View {
/// Apply to each row view inside a List or Form section to get a true-black background in AMOLED mode.
func themedRow() -> some View {
modifier(ThemedRowStyle())
}
}
|