| @@ -104,32 +104,73 @@ struct HabitsView: View { |
| 104 | } |
104 | } |
| 105 | |
105 | |
| 106 | /// org-habit's graph: a bar per day in the due-state color, lighter for days to come, a dot |
106 | /// org-habit's graph: a bar per day in the due-state color, lighter for days to come, a dot |
| 107 | /// where it was done, today outlined. |
107 | /// where it was done, today outlined. With Differentiate Without Colour, the state is also |
| |
108 | /// how solid the bar is, from clear (faintest) to overdue (solid), and days to come are |
| |
109 | /// outlined. |
| 108 | struct HabitCells: View { |
110 | struct HabitCells: View { |
| 109 | let cells: [Habit.Cell] |
111 | let cells: [Habit.Cell] |
| 110 | let colors: [String: AgendaSnapshot.Hex] |
112 | let colors: [String: AgendaSnapshot.Hex] |
| 111 | let width: CGFloat |
113 | let width: CGFloat |
| 112 | let height: CGFloat |
114 | let height: CGFloat |
| 113 | @Environment(\.colorScheme) private var scheme |
115 | @Environment(\.colorScheme) private var scheme |
| |
116 | @Environment(\.accessibilityDifferentiateWithoutColor) private var differentiateWithoutColor |
| |
117 | |
| |
118 | private var stepped: Bool { differentiateWithoutColor } |
| 114 | |
119 | |
| 115 | var body: some View { |
120 | var body: some View { |
| 116 | HStack(spacing: 1) { |
121 | HStack(spacing: 1) { |
| 117 | ForEach(cells, id: \.day) { cell in |
122 | ForEach(cells, id: \.day) { cell in |
| 118 | RoundedRectangle(cornerRadius: 1.5) |
123 | if stepped { |
| 119 | .fill(color(cell.face)) |
124 | steppedCell(cell) |
| 120 | .frame(width: width, height: height) |
125 | } else { |
| 121 | .overlay { |
126 | RoundedRectangle(cornerRadius: 1.5) |
| 122 | if cell.glyph == "*" { Circle().fill(.white.opacity(0.9)).frame(width: min(width, height) * 0.6) } |
127 | .fill(color(cell.face)) |
| 123 | } |
128 | .frame(width: width, height: height) |
| 124 | .overlay { |
129 | .overlay { |
| 125 | if cell.glyph == "!" { RoundedRectangle(cornerRadius: 1.5).strokeBorder(Color.primary, lineWidth: 1) } |
130 | if cell.glyph == "*" { Circle().fill(.white.opacity(0.9)).frame(width: min(width, height) * 0.6) } |
| 126 | } |
131 | } |
| |
132 | .overlay { |
| |
133 | if cell.glyph == "!" { RoundedRectangle(cornerRadius: 1.5).strokeBorder(Color.primary, lineWidth: 1) } |
| |
134 | } |
| |
135 | } |
| 127 | } |
136 | } |
| 128 | } |
137 | } |
| 129 | .accessibilityElement(children: .ignore) |
138 | .accessibilityElement(children: .ignore) |
| 130 | .accessibilityLabel("Done \(cells.filter { $0.glyph == "*" }.count) of \(cells.count) days") |
139 | .accessibilityLabel("Done \(cells.filter { $0.glyph == "*" }.count) of \(cells.count) days") |
| 131 | } |
140 | } |
| 132 | |
141 | |
| |
142 | /// A filled bar for past days and an outline for days to come, at the state's step; a |
| |
143 | /// done day has a dot, cut out of the darker steps. |
| |
144 | private func steppedCell(_ cell: Habit.Cell) -> some View { |
| |
145 | let (key, fallback, step, future) = Self.state(cell.face) |
| |
146 | let color = (colors[key]?.color(scheme) ?? fallback).opacity(step) |
| |
147 | let shape = RoundedRectangle(cornerRadius: 1.5) |
| |
148 | let dot = Circle().frame(width: min(width, height) * 0.6) |
| |
149 | return ZStack { |
| |
150 | if future { shape.strokeBorder(color, lineWidth: 1) } else { shape.fill(color) } |
| |
151 | if cell.glyph == "*" { |
| |
152 | if step >= 0.75 { dot.blendMode(.destinationOut) } else { dot.foregroundStyle(Color.primary) } |
| |
153 | } |
| |
154 | if cell.glyph == "!" { shape.strokeBorder(Color.primary, lineWidth: 1) } |
| |
155 | } |
| |
156 | .compositingGroup() |
| |
157 | .frame(width: width, height: height) |
| |
158 | } |
| |
159 | |
| |
160 | /// The theme key, its fallback, the opacity step and whether the day is to come. |
| |
161 | private static func state(_ face: Habit.Face) -> (String, Color, Double, Bool) { |
| |
162 | switch face { |
| |
163 | case .clear: ("habit-clear", .blue, 0.25, false) |
| |
164 | case .clearFuture: ("habit-clear", .blue, 0.25, true) |
| |
165 | case .ready: ("habit-ready", .green, 0.5, false) |
| |
166 | case .readyFuture: ("habit-ready", .green, 0.5, true) |
| |
167 | case .alert: ("habit-alert", .yellow, 0.75, false) |
| |
168 | case .alertFuture: ("habit-alert", .yellow, 0.75, true) |
| |
169 | case .overdue: ("habit-overdue", .red, 1, false) |
| |
170 | case .overdueFuture: ("habit-overdue", .red, 1, true) |
| |
171 | } |
| |
172 | } |
| |
173 | |
| 133 | private func color(_ face: Habit.Face) -> Color { |
174 | private func color(_ face: Habit.Face) -> Color { |
| 134 | func themed(_ key: String, _ fallback: Color) -> Color { colors[key]?.color(scheme) ?? fallback } |
175 | func themed(_ key: String, _ fallback: Color) -> Color { colors[key]?.color(scheme) ?? fallback } |
| 135 | switch face { |
176 | switch face { |