Sources/Orgstar/AgendaRowView.swift
211 lines · 8790 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 .reminder: return "circle"
98 case .timestamp: return row.start == nil ? "calendar" : "clock"
99 }
100 }
101
102 private var iconColor: Color {
103 if let event = row.event { return theme.dynamic(.systemPurple) { [theme] in theme.eventColor(event, dark: $0) } }
104 return row.status == .normal ? Color.secondary.opacity(0.8) : statusColor
105 }
106
107 private var categoryLine: some View {
108 HStack(spacing: 5) {
109 Circle()
110 .fill(row.event.map { event in theme.dynamic(.systemPurple) { [theme] in theme.eventColor(event, dark: $0) } }
111 ?? theme.dynamic { [theme, row] in theme.categoryColor(row.category, dark: $0) })
112 .frame(width: 6, height: 6)
113 Text(row.category)
114 .font(.system(size: 11))
115 .foregroundStyle(theme.color("agenda-category") ?? .secondary)
116 .lineLimit(1)
117 if let detail = row.prefixDetail {
118 Text(detail)
119 .font(.system(size: 11))
120 .foregroundStyle(.secondary)
121 .lineLimit(1)
122 .truncationMode(.middle)
123 }
124 }
125 }
126
127 private var titleLine: some View {
128 HStack(spacing: 6) {
129 Text(row.title.isEmpty ? " " : row.title)
130 .font(.system(size: 13))
131 .strikethrough(row.isDone)
132 .foregroundStyle(row.isDone ? AnyShapeStyle(.secondary) : AnyShapeStyle(theme.color("foreground") ?? .primary))
133 .lineLimit(1)
134 .truncationMode(.tail)
135 ForEach([row.progress, row.detail].compactMap { $0 }, id: \.self) { progress in
136 Text(progress)
137 .font(.system(size: 10, weight: .semibold).monospacedDigit())
138 .foregroundStyle(.secondary)
139 .padding(.horizontal, 6)
140 .padding(.vertical, 1)
141 .background(Capsule().fill(Color.secondary.opacity(0.14)))
142 .fixedSize()
143 }
144 ForEach(row.tags, id: \.self) { tag in
145 Text(tag)
146 .font(.system(size: 10, weight: .medium))
147 .foregroundStyle(theme.color("tags") ?? .secondary)
148 .padding(.horizontal, 6)
149 .padding(.vertical, 1)
150 .background(Capsule().strokeBorder((theme.color("tags") ?? .secondary).opacity(0.35), lineWidth: 0.5))
151 .fixedSize()
152 }
153 }
154 }
155
156 @ViewBuilder
157 private var priorityBadge: some View {
158 if let priority = row.priority {
159 let color = theme.dynamic(.systemOrange) { [theme] in theme.priorityColor(priority, dark: $0) }
160 Text(priority)
161 .font(.system(size: 10, weight: .bold))
162 .foregroundStyle(color)
163 .frame(width: 18, height: 18)
164 .background(Circle().fill(color.opacity(0.14)))
165 .overlay(Circle().strokeBorder(color.opacity(0.6), lineWidth: 1))
166 .help("Priority \(priority)")
167 } else {
168 Color.clear.frame(width: 18, height: 18)
169 }
170 }
171}
172
173/// org-habit's consistency graph: a cell per day, `*` where the habit was done, `!` today.
174struct HabitGraph: View {
175 let cells: [Habit.Cell]
176 var theme: ThemeSpec = .default
177
178 var body: some View {
179 HStack(spacing: 1) {
180 ForEach(cells, id: \.day) { cell in
181 Text(String(cell.glyph))
182 .font(.system(size: 9, weight: .bold, design: .monospaced))
183 .frame(width: 8, height: 14)
184 .background(color(cell.face))
185 .help(Self.label(cell))
186 }
187 }
188 .accessibilityElement(children: .ignore)
189 .accessibilityLabel("Done \(cells.filter { $0.glyph == "*" }.count) of the last \(Habit.precedingDays) days")
190 }
191
192 /// Due-state colors, lighter for days to come, as org-habit's faces.
193 func color(_ face: Habit.Face) -> Color {
194 func themed(_ key: String, _ fallback: Color) -> Color { theme.color(key) ?? fallback }
195 switch face {
196 case .clear: return themed("habit-clear", .blue).opacity(0.7)
197 case .clearFuture: return themed("habit-clear", .blue).opacity(0.35)
198 case .ready: return themed("habit-ready", .green).opacity(0.75)
199 case .readyFuture: return themed("habit-ready", .green).opacity(0.35)
200 case .alert: return themed("habit-alert", .yellow).opacity(0.85)
201 case .alertFuture: return themed("habit-alert", .yellow).opacity(0.4)
202 case .overdue: return themed("habit-overdue", .red).opacity(0.75)
203 case .overdueFuture: return themed("habit-overdue", .red).opacity(0.35)
204 }
205 }
206
207 static func label(_ cell: Habit.Cell) -> String {
208 let date = Days.date(cell.day)
209 return String(format: "%04d-%02d-%02d", date.year, date.month, date.day) + (cell.glyph == "*" ? " DONE" : "")
210 }
211}