Sources/Orgstar/AgendaRowView.swift

98b41213c14d74d094f11cb5134cb8aac027be4e
orgstar/Sources/Orgstar/AgendaRowView.swift

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

Global

⌘K
Command palette & jump
/
Focus search
?
Show this help
g d
Dashboard
g e
Explore repositories
g n
Notifications

Repository & editor

t
Find file in repository
y
Copy immutable SHA permalink
g c
Code tab
g i
Issues tab
g m
Merge requests tab
g b
Builds tab
⌘↵
Submit active form