Commit 0db705cded

0db705cdedea801e836e615598071a2db3503558

parent: 14b43eca4a

Verified · cmc

cmc <hello@cleberg.net> · 2026-10-16 17:10 UTC

Mac agenda rows keep their entries when the day's rows change

Day rows were identified by their place among the lines, and each row view
by its index, so a refresh that removed a row or moved the now line left
views showing other entries. Rows are now identified by entry; selection
still goes by place.

Layout: unified · split

Sources/Orgstar/AgendaView.swift +35 −9
@@ -229,11 +229,20 @@ struct AgendaView: View {
229229
230230 // MARK: - Lines
231231
232 /// A selectable line: its selection id, the row, and the day it is on.
232 /// A selectable line: its selection id, the row, and the day it is on. `key`, the view's
233 /// identity, follows the row where `id` follows its place; nil uses `id`.
233234 struct Line {
234235 let id: String
235236 let row: AgendaRow
236237 let day: Int
238 var key: String?
239
240 var viewID: String { key ?? id }
241 }
242
243 /// A day row's view identity.
244 static func key(_ row: AgendaRow, day: Int, prefix: String, overdue: Bool = false) -> String {
245 "\(prefix)\(day):\(overdue ? "o" : "")\(row.id)"
237246 }
238247
239248 private var groups: [AgendaDayGroup] {
@@ -260,11 +269,19 @@ struct AgendaView: View {
260269
261270 private func dayLines(_ groups: [AgendaDayGroup], prefix: String) -> [Line] {
262271 groups.flatMap { group in
263 (overdueShown ? group.overdue.enumerated().map { Line(id: "\(prefix)\(group.day):o\($0.offset)", row: $0.element, day: group.day) } : [])
264 + group.rows.enumerated().map { Line(id: "\(prefix)\(group.day):\($0.offset)", row: $0.element, day: group.day) }
272 (overdueShown ? group.overdue.enumerated().map { dayLine(group, $0.offset, prefix: prefix, overdue: true) } : [])
273 + group.rows.indices.map { dayLine(group, $0, prefix: prefix) }
265274 }
266275 }
267276
277 private func dayLine(_ group: AgendaDayGroup, _ index: Int, prefix: String, overdue: Bool = false) -> Line {
278 let row = overdue ? group.overdue[index] : group.rows[index]
279 return Line(
280 id: "\(prefix)\(group.day):\(overdue ? "o" : "")\(index)", row: row, day: group.day,
281 key: Self.key(row, day: group.day, prefix: prefix, overdue: overdue)
282 )
283 }
284
268285 func line(_ id: String) -> Line? { lines(groups).first { $0.id == id } }
269286
270287 var body: some View {
@@ -311,7 +328,7 @@ struct AgendaView: View {
311328 .padding(.bottom, 12)
312329 }
313330 .onChange(of: selection) {
314 if let selection { proxy.scrollTo(selection) }
331 if let selection { proxy.scrollTo(line(selection)?.viewID ?? selection) }
315332 }
316333 .onChange(of: agenda.days.first?.day) {
317334 if groups.contains(where: \.isToday) { proxy.scrollTo("day\(agenda.today)", anchor: .top) }
@@ -420,8 +437,8 @@ struct AgendaView: View {
420437 if group.isToday, !group.overdue.isEmpty {
421438 overdueHeader(count: group.overdue.count)
422439 if overdueShown {
423 ForEach(Array(group.overdue.enumerated()), id: \.element.id) { index, row in
424 rowView(Line(id: "\(prefix)\(group.day):o\(index)", row: row, day: group.day))
440 ForEach(Array(group.overdue.enumerated()), id: \.element.id) { index, _ in
441 rowView(dayLine(group, index, prefix: prefix, overdue: true))
425442 }
426443 Divider().padding(.leading, 16).padding(.vertical, 4)
427444 }
@@ -433,9 +450,18 @@ struct AgendaView: View {
433450 .padding(.horizontal, 16)
434451 .padding(.vertical, 10)
435452 }
436 ForEach(Array(group.lines(now: Self.hhmm(now)).enumerated()), id: \.offset) { _, line in
453 // Identified by row, not place, so a refresh that moves rows doesn't reuse one row's view
454 // for another.
455 let lines = group.lines(now: Self.hhmm(now)).map { line in
456 switch line {
457 case .row(let index): (Self.key(group.rows[index], day: group.day, prefix: prefix), line)
458 case .grid(let time): ("grid\(time)", line)
459 case .now: ("now", line)
460 }
461 }
462 ForEach(lines, id: \.0) { _, line in
437463 switch line {
438 case .row(let index): rowView(Line(id: "\(prefix)\(group.day):\(index)", row: group.rows[index], day: group.day))
464 case .row(let index): rowView(dayLine(group, index, prefix: prefix))
439465 case .grid(let time): gridLine(time)
440466 case .now: nowLine
441467 }
@@ -540,7 +566,7 @@ struct AgendaView: View {
540566 row: line.row, day: line.day, showsGutter: showsGutter, selected: selection == line.id, hovered: hovered == line.id,
541567 marked: line.row.item.flatMap(EntryRef.init).map(marked.contains) ?? false
542568 )
543 .id(line.id)
569 .id(line.viewID)
544570 .contentShape(Rectangle())
545571 .onHover { inside in
546572 if inside { hovered = line.id } else if hovered == line.id { hovered = nil }