Sources/Orgstar/AgendaRowView.swift

72f19c1f379646b2cfc45c5053a72e5997fbef36
orgstar/Sources/Orgstar/AgendaRowView.swift

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