Sources/Orgstar/AgendaRowView.swift
219 lines · 9781 bytes
5 symbols in this file
1import AppKit
2import OrgApp
3import OrgDocument
4import OrgCore
5import OrgPresentation
6import SwiftUI
7
8/// One agenda line: the time or status over an icon; above the title the category, progress and
9/// tags, with the TODO keyword and priority at the end; a habit's graph under the title.
10struct AgendaRowView: View {
11 let row: AgendaRow
12 /// The agenda day the row is on, for habit graphs.
13 var day: Int
14 var showsGutter = true
15 var selected = false
16 var hovered = false
17 /// Marked for a bulk action.
18 var marked = false
19 @Environment(\.orgTheme) private var theme
20 @ScaledMetric(relativeTo: .callout) private var gutterWidth = AgendaRowView.gutterWidth
21 @Environment(\.colorSchemeContrast) private var contrast
22 @Environment(\.accessibilityDifferentiateWithoutColor) private var withoutColor
23 @ScaledMetric(relativeTo: .caption) private var badgeSize: CGFloat = 18
24
25 static let gutterWidth: CGFloat = 92
26
27 var body: some View {
28 HStack(alignment: .top, spacing: 12) {
29 if showsGutter { gutter }
30 VStack(alignment: .leading, spacing: 2) {
31 HStack(alignment: .top, spacing: 8) {
32 metaLine
33 Spacer(minLength: 0)
34 marks
35 }
36 Text(row.title.isEmpty ? " " : row.title)
37 .font(.body)
38 .strikethrough(row.isDone)
39 .foregroundStyle(row.isDone ? AnyShapeStyle(.secondary) : AnyShapeStyle(theme.color("foreground") ?? .primary))
40 .lineLimit(1)
41 .truncationMode(.tail)
42 if let habit = row.item?.habit {
43 HabitGraph(cells: habit.graph(today: day), theme: theme).padding(.top, 2)
44 }
45 }
46 }
47 .padding(.horizontal, 16)
48 .padding(.vertical, 6)
49 .frame(minHeight: 44)
50 // `org-agenda-dim-blocked-tasks`.
51 .opacity(row.isBlocked ? 0.5 : 1)
52 .background {
53 RoundedRectangle(cornerRadius: 6)
54 .fill(selected ? theme.color("selection") ?? Color.accentColor.opacity(0.2) : hovered ? Color.primary.opacity(0.05) : .clear)
55 .padding(.horizontal, 6)
56 }
57 .overlay(alignment: .leading) {
58 if marked {
59 Capsule().fill(theme.color("agenda-today") ?? .accentColor).frame(width: 3).padding(.vertical, 8).padding(.leading, 8)
60 }
61 }
62 .accessibilityElement(children: .combine)
63 }
64
65 /// Increase Contrast: keyword and priority badges are filled with their colour, tags outlined in full.
66 private var highContrast: Bool { contrast == .increased }
67
68 private var background: Color { theme.color("agenda-background") ?? Color(nsColor: .textBackgroundColor) }
69
70 private var statusColor: Color {
71 theme.dynamic(.secondaryLabelColor) { [theme, row] in theme.statusColor(row.status, dark: $0) }
72 }
73
74 /// The start over the end for timed rows, else the status; the icon under them.
75 private var gutter: some View {
76 VStack(alignment: .leading, spacing: 2) {
77 let lines = row.gutterLines
78 Text(lines[0])
79 .font(.callout.weight(row.status == .overdue || row.status == .dueSoon ? .semibold : .regular).monospacedDigit())
80 .foregroundStyle(statusColor)
81 .lineLimit(2)
82 .fixedSize(horizontal: false, vertical: true)
83 if lines.count > 1 {
84 Text(lines[1]).font(.caption.monospacedDigit()).foregroundStyle(.secondary)
85 }
86 if let symbol = row.symbol {
87 Image(systemName: symbol)
88 .font(.caption.weight(.semibold))
89 .imageScale(row.icon == .event ? .small : .medium)
90 .foregroundStyle(iconColor)
91 }
92 }
93 .frame(width: gutterWidth, alignment: .leading)
94 }
95
96 private var iconColor: Color {
97 if let event = row.event { return theme.dynamic(.systemPurple) { [theme] in theme.eventColor(event, dark: $0) } }
98 return row.status == .normal ? Color.secondary.opacity(0.8) : statusColor
99 }
100
101 /// The category, the prefix's detail, progress and tags, wrapping to more lines as needed.
102 private var metaLine: some View {
103 FlowLayout(spacing: 5, lineSpacing: 3) {
104 if !row.category.isEmpty {
105 HStack(spacing: 5) {
106 Circle()
107 .fill(row.event.map { event in theme.dynamic(.systemPurple) { [theme] in theme.eventColor(event, dark: $0) } }
108 ?? theme.dynamic { [theme, row] in theme.categoryColor(row.category, dark: $0) })
109 .frame(width: 6, height: 6)
110 Text(row.category)
111 .font(.subheadline)
112 .foregroundStyle(theme.color("agenda-category") ?? .secondary)
113 .lineLimit(1)
114 }
115 }
116 if let detail = row.prefixDetail {
117 Text(detail)
118 .font(.subheadline)
119 .foregroundStyle(.secondary)
120 .lineLimit(1)
121 .truncationMode(.middle)
122 }
123 ForEach([row.progress, row.detail].compactMap { $0 }, id: \.self) { progress in
124 Text(progress)
125 .font(.caption.weight(.semibold).monospacedDigit())
126 .foregroundStyle(.secondary)
127 .padding(.horizontal, 6)
128 .padding(.vertical, 1)
129 .background(Capsule().fill(Color.secondary.opacity(0.14)))
130 }
131 ForEach(row.tags, id: \.self) { tag in
132 Text(tag)
133 .font(.caption.weight(.medium))
134 .foregroundStyle(theme.color("tags") ?? .secondary)
135 .padding(.horizontal, 6)
136 .padding(.vertical, 1)
137 .background(Capsule().strokeBorder((theme.color("tags") ?? .secondary).opacity(highContrast ? 1 : 0.35), lineWidth: highContrast ? 1 : 0.5))
138 }
139 }
140 }
141
142 /// The TODO keyword and the priority, at the end of the category's line.
143 private var marks: some View {
144 HStack(spacing: 6) {
145 if let todo = row.todo {
146 let color = theme.dynamic(row.isDone ? .systemGreen : .systemRed) { [theme, row] in theme.todoColor(todo, isDone: row.isDone, dark: $0) }
147 HStack(spacing: 2) {
148 // Done or not without telling green from red.
149 if withoutColor, row.isDone { Image(systemName: "checkmark") }
150 Text(todo)
151 }
152 .font(.caption.weight(.bold))
153 .foregroundStyle(highContrast ? background : color)
154 .padding(.horizontal, 7)
155 .padding(.vertical, 2)
156 .background(Capsule().fill(highContrast ? color : color.opacity(0.14)))
157 .overlay { if withoutColor, !highContrast { Capsule().strokeBorder(color, lineWidth: 1) } }
158 }
159 priorityBadge
160 }
161 .fixedSize()
162 }
163
164 @ViewBuilder
165 private var priorityBadge: some View {
166 if let priority = row.priority {
167 let color = theme.dynamic(.systemOrange) { [theme] in theme.priorityColor(priority, dark: $0) }
168 Text(priority)
169 .font(.caption.weight(.bold))
170 .foregroundStyle(highContrast ? background : color)
171 .frame(width: badgeSize, height: badgeSize)
172 .background(Circle().fill(highContrast ? color : color.opacity(0.14)))
173 .overlay(Circle().strokeBorder(color.opacity(highContrast ? 1 : 0.6), lineWidth: 1))
174 .help("Priority \(priority)")
175 } else {
176 Color.clear.frame(width: badgeSize, height: badgeSize)
177 }
178 }
179}
180
181/// org-habit's consistency graph: a cell per day, `*` where the habit was done, `!` today.
182struct HabitGraph: View {
183 let cells: [Habit.Cell]
184 var theme: ThemeSpec = .default
185
186 var body: some View {
187 HStack(spacing: 1) {
188 ForEach(cells, id: \.day) { cell in
189 Text(String(cell.glyph))
190 .font(.caption2.monospaced().weight(.bold))
191 .frame(width: 8, height: 14)
192 .background(color(cell.face))
193 .help(Self.label(cell))
194 }
195 }
196 .accessibilityElement(children: .ignore)
197 .accessibilityLabel("Done \(cells.filter { $0.glyph == "*" }.count) of the last \(Habit.precedingDays) days")
198 }
199
200 /// Due-state colors, lighter for days to come, as org-habit's faces.
201 func color(_ face: Habit.Face) -> Color {
202 func themed(_ key: String, _ fallback: Color) -> Color { theme.color(key) ?? fallback }
203 switch face {
204 case .clear: return themed("habit-clear", .blue).opacity(0.7)
205 case .clearFuture: return themed("habit-clear", .blue).opacity(0.35)
206 case .ready: return themed("habit-ready", .green).opacity(0.75)
207 case .readyFuture: return themed("habit-ready", .green).opacity(0.35)
208 case .alert: return themed("habit-alert", .yellow).opacity(0.85)
209 case .alertFuture: return themed("habit-alert", .yellow).opacity(0.4)
210 case .overdue: return themed("habit-overdue", .red).opacity(0.75)
211 case .overdueFuture: return themed("habit-overdue", .red).opacity(0.35)
212 }
213 }
214
215 static func label(_ cell: Habit.Cell) -> String {
216 let date = Days.date(cell.day)
217 return String(format: "%04d-%02d-%02d", date.year, date.month, date.day) + (cell.glyph == "*" ? " DONE" : "")
218 }
219}