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