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