Sources/Orgstar/AgendaRowView.swift

848c7b130274052f194a17fccd4e33e6bb30851d
orgstar/Sources/Orgstar/AgendaRowView.swift history · blame · raw

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