╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Issues.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
453 454 // each side's field->oid map, for attributing the versions; a field a 455 // merge left unattributed reads as an unknown author 456 var their_field_oids: ?DB.SortedMap(.read_only) = null; 457 if (try conflict_entry.getCursor(hash.hashInt(repo_opts.hash, evt.their_field_to_oid_key))) |cursor| { 458 their_field_oids = try DB.SortedMap(.read_only).init(cursor); 459 } 460 var our_field_oids: ?DB.SortedMap(.read_only) = null; 461 if (try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Issue.id_to_field_to_oid_key))) |map_cursor| { 462 const id_to_field_to_oid = try DB.HashMap(.read_only).init(map_cursor); 463 if (try id_to_field_to_oid.getCursor(hash.hashInt(repo_opts.hash, id_bytes))) |cursor| { 464 our_field_oids = try DB.SortedMap(.read_only).init(cursor); 465 } 466 } 467 468 var conflict = Conflict{}; 469 var field_iter = std.mem.splitScalar(u8, conflicted_fields, ' '); 470 while (field_iter.next()) |field| { 471 // status can't conflict (two changes of a two-value field agree) 472 const known = std.meta.stringToEnum(enum { title, tags, description }, field) orelse continue; 473 const our_author = try oidAuthor(repo_kind, repo_opts, arena, repo, io, admin_moment, try readFieldOid(repo_opts.hash, our_field_oids, field)); 474 const their_author = try oidAuthor(repo_kind, repo_opts, arena, repo, io, admin_moment, try readFieldOid(repo_opts.hash, their_field_oids, field)); 475 switch (known) { 476 .title => conflict.title = .{ 477 .ours = .{ .text = ours.event.title, .author = our_author }, 478 .theirs = .{ .text = theirs.event.title, .author = their_author }, 479 }, 480 .tags => conflict.tags = .{ 481 .ours = .{ .text = ours.event.tags, .author = our_author }, 482 .theirs = .{ .text = theirs.event.tags, .author = their_author }, 483 }, 484 .description => conflict.description = .{ 485 .chunks = try diff3.chunks(io, gpa, arena, if (base) |b| b.event.description else "", ours.event.description, theirs.event.description), 486 .ours_author = our_author, 487 .theirs_author = their_author, 488 }, 489 } 490 } 491 return conflict; 492 } 493 494 // the oid `field` maps to in a field->oid map, or null 495 fn readFieldOid( 496 comptime hash_kind: hash.HashKind, 497 field_oids_maybe: ?evt.EventDB(hash_kind).SortedMap(.read_only), 498 field: []const u8, 499 ) !?[hash.byteLen(hash_kind)]u8 { 500 const field_oids = field_oids_maybe orelse return null; 501 const cursor = (try field_oids.getCursor(field)) orelse return null; 502 var oid: [hash.byteLen(hash_kind)]u8 = undefined; 503 _ = try cursor.readBytes(&oid); 504 return oid; 505 } 506 507 // the author of the event commit `oid_maybe` names 508 fn oidAuthor( 509 comptime repo_kind: rp.RepoKind, 510 comptime repo_opts: rp.RepoOpts(repo_kind), 511 arena: *std.heap.ArenaAllocator, 512 repo: *rp.Repo(repo_kind, repo_opts), 513 io: std.Io, 514 admin_moment: ?evt.AdminDB.HashMap(.read_only), 515 oid_maybe: ?[hash.byteLen(repo_opts.hash)]u8, 516 ) !ui.Author { 517 const oid = oid_maybe orelse return .unknown; 518 const gpa = arena.child_allocator; 519 var start_oids = [_][hash.hexLen(repo_opts.hash)]u8{std.fmt.bytesToHex(oid, .lower)}; 520 var commit_iter = repo.log(io, gpa, start_oids[0..1]) catch return .unknown; 521 defer commit_iter.deinit(); 522 const commit_object = (commit_iter.next(gpa) catch return .unknown) orelse return .unknown; 523 defer commit_object.deinit(); 524 return try ui.Author.init(admin_moment, arena, commit_object.content.commit.metadata.author orelse ""); 525 } 526 527 pub const View = struct { 528 // a vertical box: the header tabs on top, then a stack holding a 529 // master-detail split (issue list + description pane) per status list, 530 // plus the tags view. 531 box: wgt.Box(ui.Widget), // vert: [header_index] = tabs, [stack_index] = stack 532 data: *const Self, 533 session: *ui.Session, 534 // per-split state, indexed like the stack's split children: the issue the 535 // pane shows, and its description and author text boxes' focus ids. 536 detailed_index: [stack_child_max]?usize, 537 description_id: [stack_child_max]?usize, 538 author_id: [stack_child_max]?usize, 539 540 const header_index: usize = 0; 541 const stack_index: usize = 1; 542 // indices within the stack, 1:1 with the header tabs. 543 const open_view_index: usize = 0; 544 const closed_view_index: usize = 1; 545 const tags_view_index: usize = 2; 546 // the new-issue form, or the edit or resolve form when the page was 547 // loaded at one of their urls. 548 const form_view_index: usize = 3; 549 // the conflicts split; the tab and stack child exist only when the repo 550 // has conflicted issues. 551 const conflicts_view_index: usize = 4; 552 const stack_child_max: usize = conflicts_view_index + 1; 553 // the leading stack children that are status splits; the conflicts split 554 // sits apart at conflicts_view_index. 555 const split_count: usize = 2; 556 // indices within a split (the horizontal box inside the stack). 557 const list_index: usize = 0; 558 const detail_index: usize = 1; 559 const list_max_width: usize = 40; 560 const detail_min_width: usize = 40; 561 // indices within the new-issue form. 562 const title_field_index: usize = 0; 563 const tags_field_index: usize = 1; 564 const description_field_index: usize = 2; 565 const submit_field_index: usize = 3; 566 567 fn viewIndex(view: ui.RoutablePage.IssuesView) usize { 568 return switch (view) { 569 .open => open_view_index, 570 .closed => closed_view_index, 571 .tags => tags_view_index, 572 .new, .edit, .resolve => form_view_index, 573 .conflicts => conflicts_view_index, 574 // init resolves a description url to its issue's status list 575 .description => unreachable, 576 }; 577 } 578 579 // the status a status split lists; the conflicts split has none (its 580 // issues carry their own). 581 fn splitStatus(index: usize) evt.Issue.Status { 582 return if (index == open_view_index) .open else .closed; 583 } 584 585 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { 586 var outer = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 587 errdefer outer.deinit(allocator); 588 589 // the tabs at the top. 590 { 591 var hdr = try Header.init(allocator, session, data); 592 errdefer hdr.deinit(allocator); 593 try outer.children.put(allocator, hdr.getFocus().id, .{ .widget = .{ .repo_issues_header = hdr }, .rect = null, .min_size = null }); 594 } 595 596 // the stack enters `outer` before its children enter it, so an error 597 // frees each child exactly once. 598 { 599 var stack = try wgt.Stack(ui.Widget).init(allocator); 600 errdefer stack.deinit(allocator); 601 try outer.children.put(allocator, stack.getFocus().id, .{ .widget = .{ .stack = stack }, .rect = null, .min_size = null }); 602 } 603 const stack = &outer.children.values()[stack_index].widget.stack; 604 605 // a master-detail split per status list. 606 for ([_]evt.Issue.Status{ .open, .closed }) |status| { 607 var split = try initSplit(allocator, session, data, status); 608 errdefer split.deinit(allocator); 609 try stack.children.put(allocator, split.getFocus().id, .{ .box = split }); 610 } 611 612 // the tags view 613 { 614 var tf = try ui.TagFlow.init(allocator); 615 errdefer tf.deinit(allocator); 616 var items: std.ArrayList(ui.TagFlow.Item) = .empty; 617 defer items.deinit(allocator); 618 // when filtered, the first item clears the filter 619 if (data.tag.len != 0) 620 try items.append(allocator, .{ .text = "✕", .link = try issuesLink(session.page_arena, data.identity, .open, "", "") }); 621 for (data.tags) |tag| 622 try items.append(allocator, .{ .text = tag, .link = try tagLink(session.page_arena, data.identity, .open, tag) }); 623 try tf.setItems(allocator, items.items); 624 try stack.children.put(allocator, tf.getFocus().id, .{ .tag_flow = tf }); 625 } 626 627 // the new-issue form, or — on an edit or resolve url — that form in 628 // its place, prefilled with the selected issue's content. a 629 // logged-out session can't create events, so the unauthorized view 630 // stands in. 631 if (session.data.is_local or session.data.user_id != null) { 632 if (data.view == .resolve) { 633 // on the web the page grows to the form's height and the 634 // browser scrolls it; the terminal scrolls the form itself 635 var form_widget: ui.Widget = blk: { 636 var form = try initResolveForm(allocator, session, data); 637 errdefer form.deinit(allocator); 638 break :blk if (session.is_terminal) 639 .{ .scroll = try wgt.Scroll(ui.Widget).init(allocator, .{ .box = form }, .{ .direction = .vert }) } 640 else 641 .{ .box = form }; 642 }; 643 errdefer form_widget.deinit(allocator); 644 try stack.children.put(allocator, form_widget.getFocus().id, form_widget); 645 } else { 646 const aa = session.page_arena.allocator(); 647 const action = if (data.view == .edit) 648 (if (data.identity.len == 0) 649 try std.fmt.allocPrint(aa, "form:/issues/{s}/edit", .{data.selected_id}) 650 else 651 try std.fmt.allocPrint(aa, "form:/repo/{s}/issues/{s}/edit", .{ data.identity, data.selected_id })) 652 else if (data.identity.len == 0) 653 "form:/issue" 654 else 655 try std.fmt.allocPrint(aa, "form:/repo/{s}/issue", .{data.identity}); 656 const issue = if (data.view == .edit) 657 (if (data.selectedIssue()) |entry| &entry.issue else null) 658 else 659 null; 660 var form = try initIssueForm(allocator, session, action, issue); 661 errdefer form.deinit(allocator); 662 try stack.children.put(allocator, form.getFocus().id, .{ .box = form }); 663 } 664 } else { 665 var message = try ui.Unauthorized.View.init(allocator); 666 errdefer message.deinit(allocator); 667 try stack.children.put(allocator, message.getFocus().id, .{ .unauthorized = message }); 668 } 669 670 // the conflicts split; its tab exists only when the repo has 671 // conflicts, so skip the child too to keep the stack 1:1 with the 672 // tabs. 673 if (data.conflicts.count > 0) { 674 var split = try initSplit(allocator, session, data, null); 675 errdefer split.deinit(allocator); 676 try stack.children.put(allocator, split.getFocus().id, .{ .box = split }); 677 } 678 679 // the stack starts on the page's view. 680 stack.getFocus().child_id = stack.children.keys()[viewIndex(data.view)]; 681 682 // focus entering the view lands on the tabs first. 683 outer.getFocus().child_id = outer.children.keys()[header_index]; 684 685 return .{ 686 .box = outer, 687 .data = data, 688 .session = session, 689 .detailed_index = @splat(null), 690 .description_id = @splat(null), 691 .author_id = @splat(null), 692 }; 693 } 694 695 // the master-detail split showing `status`'s window, or the conflicts 696 // window when null. 697 fn initSplit(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self, status_maybe: ?evt.Issue.Status) !wgt.Box(ui.Widget) { 698 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 699 errdefer box.deinit(allocator); 700 701 const win = if (status_maybe) |status| data.window(status) else &data.conflicts; 702 703 // the issue list (one focusable row per title), plus a "next" link that 704 // reloads the page rooted at the following issue. 705 { 706 var list_scroll = blk: { 707 var list_box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert, .stretch = true }); 708 errdefer list_box.deinit(allocator); 709 if (win.prev_id) |prev| 710 try addRow(allocator, &list_box, "← previous", "", try windowLink(session.page_arena, data, status_maybe, prev)); 711 for (win.issues) |entry| { 712 // conflicted issues carry a marker in the status lists 713 const label: []const u8 = if (entry.conflicted and status_maybe != null) "[conflict]" else ""; 714 try addRow(allocator, &list_box, entry.issue.event.title, label, try issueRowLink(session.page_arena, data.identity, entry.id)); 715 } 716 if (win.next_id) |next| 717 try addRow(allocator, &list_box, "next →", "", try windowLink(session.page_arena, data, status_maybe, next)); 718 // select the window's first issue (past a leading "previous" 719 // row) so its description shows on load. 720 if (win.issues.len > 0) 721 list_box.getFocus().child_id = list_box.children.keys()[if (win.prev_id != null) 1 else 0] 722 else if (list_box.children.count() > 0) 723 list_box.getFocus().child_id = list_box.children.keys()[0]; 724 break :blk try wgt.Scroll(ui.Widget).init(allocator, .{ .box = list_box }, .{ .direction = .vert, .web_native = !session.is_terminal }); 725 }; 726 errdefer list_scroll.deinit(allocator); 727 try box.children.put(allocator, list_scroll.getFocus().id, .{ .widget = .{ .scroll = list_scroll }, .rect = null, .min_size = .{ .width = list_max_width, .height = null }, .max_size = .{ .width = list_max_width, .height = null } }); 728 } 729 730 // the detail pane — a frame around a scroll of the description 731 { 732 var detail_outer = blk: { 733 var detail_scroll = blk2: { 734 var detail_inner = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 735 errdefer detail_inner.deinit(allocator); 736 // fill the pane (content top-left, scroll bar pinned to the 737 // edge) rather than shrinking to the description. 738 break :blk2 try wgt.Scroll(ui.Widget).init(allocator, .{ .box = detail_inner }, .{ .direction = .vert, .web_native = !session.is_terminal, .fill = true }); 739 }; 740 errdefer detail_scroll.deinit(allocator); 741 var frame = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = .hidden, .direction = .vert }); 742 errdefer frame.deinit(allocator); 743 // the tool row sits above the scroll (populateDetail fills 744 // it per issue) so it can't scroll out from under the web 745 // overlay <form>, whose position doesn't track pane scrolling. 746 { 747 var row = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 748 errdefer row.deinit(allocator); 749 try frame.children.put(allocator, row.getFocus().id, .{ .widget = .{ .box = row }, .rect = null, .min_size = null }); 750 } 751 // the frame's selected child is its scroll, so the focus chain 752 // reaches the description (populateDetail points the scroll's 753 // inner box at it), letting focus recovery descend into the pane. 754 frame.getFocus().child_id = detail_scroll.getFocus().id; 755 try frame.children.put(allocator, detail_scroll.getFocus().id, .{ .widget = .{ .scroll = detail_scroll }, .rect = null, .min_size = null }); 756 break :blk frame; 757 }; 758 errdefer detail_outer.deinit(allocator); 759 try box.children.put(allocator, detail_outer.getFocus().id, .{ .widget = .{ .box = detail_outer }, .rect = null, .min_size = .{ .width = detail_min_width, .height = null } }); 760 } 761 762 box.getFocus().child_id = box.children.keys()[list_index]; 763 return box; 764 } 765 766 // an issue form: title/tags/description inputs and a submit button, 767 // prefilled from `issue` when given. its form: subtree makes the web 768 // overlay wrap them in a <form> POSTing to `action`'s route. 769 fn initIssueForm(allocator: std.mem.Allocator, session: *ui.Session, action: []const u8, issue: ?*const evt.Issue.Record) !wgt.Box(ui.Widget) { 770 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 771 errdefer box.deinit(allocator); 772 box.getFocus().kind = .{ .custom = action }; 773 774 { 775 var title = try wgt.TextInput(ui.Widget).init(allocator, .{ .label = " title ", .name = "title", .visible_width = null, .rounded_corners = true, .render_content = session.is_terminal }); 776 errdefer title.deinit(allocator); 777 title.getFocus().focusable = true; 778 if (issue) |i| try title.setContent(allocator, i.event.title); 779 try box.children.put(allocator, title.getFocus().id, .{ .widget = .{ .text_input = title }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 780 } 781 782 { 783 var tags = try wgt.TextInput(ui.Widget).init(allocator, .{ .label = " tags (separate with spaces) ", .name = "tags", .visible_width = null, .rounded_corners = true, .render_content = session.is_terminal }); 784 errdefer tags.deinit(allocator); 785 tags.getFocus().focusable = true; 786 if (issue) |i| try tags.setContent(allocator, i.event.tags); 787 try box.children.put(allocator, tags.getFocus().id, .{ .widget = .{ .text_input = tags }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 788 } 789 790 { 791 var description = try wgt.TextInput(ui.Widget).init(allocator, .{ .label = " description ", .name = "description", .visible_width = null, .rounded_corners = true, .render_content = session.is_terminal, .multiline = true, .scroll = .{ .fill = true } }); 792 errdefer description.deinit(allocator); 793 description.getFocus().focusable = true; 794 if (issue) |i| try description.setContent(allocator, i.event.description); 795 try box.children.put(allocator, description.getFocus().id, .{ .widget = .{ .text_input = description }, .rect = null, .min_size = null }); 796 } 797 798 try addSubmitButton(allocator, &box); 799 800 box.getFocus().child_id = box.children.keys()[title_field_index]; 801 return box; 802 } 803 804 // a form's submit button, then a spacer absorbing the leftover 805 // min-height the box hands its last child, so the button keeps its 806 // natural height 807 fn addSubmitButton(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget)) !void { 808 { 809 var submit = try ui.SubmitButton.init(allocator); 810 errdefer submit.deinit(allocator); 811 try box.children.put(allocator, submit.getFocus().id, .{ .widget = .{ .submit_button = submit }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 812 } 813 { 814 var spacer = try ui.Spacer.init(allocator); 815 errdefer spacer.deinit(allocator); 816 try box.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 817 } 818 } 819 820 // the conflict resolve form. the "use this" links are navigations that 821 // flip the url's theirs: pick, reloading the prefills; one submit 822 // settles every conflict. the form: subtree makes the web overlay POST 823 // to the resolve route. 824 fn initResolveForm(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self) !wgt.Box(ui.Widget) { 825 const aa = session.page_arena.allocator(); 826 // init only picks the resolve view once it has read the conflict 827 const conflict = if (data.conflict) |*c| c else unreachable; 828 829 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 830 errdefer box.deinit(allocator); 831 box.getFocus().kind = .{ .custom = if (data.identity.len == 0) 832 try std.fmt.allocPrint(aa, "form:/issues/{s}/resolve", .{data.selected_id}) 833 else 834 try std.fmt.allocPrint(aa, "form:/repo/{s}/issues/{s}/resolve", .{ data.identity, data.selected_id }) }; 835 836 if (conflict.title) |*fc| { 837 try addLabel(allocator, &box, "title conflict:"); 838 try addFieldConflict(allocator, &box, session, data, "title", fc); 839 } 840 if (conflict.tags) |*fc| { 841 try addLabel(allocator, &box, "tags conflict:"); 842 try addFieldConflict(allocator, &box, session, data, "tags", fc); 843 } 844 845 if (conflict.description) |*desc| { 846 try addLabel(allocator, &box, "description conflict:"); 847 var hunk_index: usize = 0; 848 for (desc.chunks, 0..) |*chunk, chunk_index| { 849 switch (chunk.*) { 850 .same => |text| { 851 var tb = try wgt.TextBox(ui.Widget).init(allocator, text, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .word }); 852 errdefer tb.deinit(allocator); 853 tb.getFocus().focusable = true; 854 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 855 }, 856 // an auto-resolved chunk: only one side changed it, so its 857 // version stands, shown distinctly with its author 858 .auto => |auto| { 859 const author = if (auto.theirs) desc.theirs_author else desc.ours_author; 860 const verb: []const u8 = if (auto.text == null) "removed" else "edited"; 861 const label = switch (author) { 862 .user_name, .email => |name| try std.fmt.allocPrint(aa, " {s} by {s} ", .{ verb, name }), 863 .unknown => try std.fmt.allocPrint(aa, " {s} by {s} ", .{ verb, if (auto.theirs) @as([]const u8, "them") else "us" }), 864 }; 865 var tb = try wgt.TextBox(ui.Widget).init(allocator, auto.text orelse "(removed)", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .word, .label = label }); 866 errdefer tb.deinit(allocator); 867 tb.getFocus().focusable = true; 868 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 869 }, 870 .conflict => |hunk| { 871 // a blank row on each side sets the group apart from 872 // the flowing shared and auto-resolved chunks 873 if (chunk_index > 0) try addGap(allocator, &box); 874 const name = try std.fmt.allocPrint(aa, "d{d}", .{hunk_index}); 875 hunk_index += 1; 876 const picked_theirs = data.theirsPicked(name); 877 try addVersionRow(allocator, &box, try sideLabel(aa, desc.ours_author, true), hunk.ours orelse "", try useThisLink(session, data, name, false)); 878 try addVersionRow(allocator, &box, try sideLabel(aa, desc.theirs_author, false), hunk.theirs orelse "", try useThisLink(session, data, name, true)); 879 var resolution_input = try wgt.TextInput(ui.Widget).init(allocator, .{ .label = " resolution ", .name = name, .visible_width = null, .rounded_corners = true, .render_content = session.is_terminal, .multiline = true }); 880 errdefer resolution_input.deinit(allocator); 881 resolution_input.getFocus().focusable = true; 882 try resolution_input.setContent(allocator, (if (picked_theirs) hunk.theirs else hunk.ours) orelse ""); 883 try box.children.put(allocator, resolution_input.getFocus().id, .{ .widget = .{ .text_input = resolution_input }, .rect = null, .min_size = null }); 884 if (chunk_index + 1 < desc.chunks.len) try addGap(allocator, &box); 885 }, 886 } 887 } 888 } 889 890 try addSubmitButton(allocator, &box); 891 892 box.getFocus().child_id = box.children.keys()[0]; 893 return box; 894 } 895 896 // one conflicted scalar field: both sides' version rows, then the 897 // resolution input prefilled from the picked side. 898 fn addFieldConflict(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), session: *ui.Session, data: *const Self, comptime name: []const u8, fc: *const FieldConflict) !void { 899 const aa = session.page_arena.allocator(); 900 try addVersionRow(allocator, box, try sideLabel(aa, fc.ours.author, true), fc.ours.text, try useThisLink(session, data, name, false)); 901 try addVersionRow(allocator, box, try sideLabel(aa, fc.theirs.author, false), fc.theirs.text, try useThisLink(session, data, name, true)); 902 903 var resolution_input = try wgt.TextInput(ui.Widget).init(allocator, .{ .label = " resolution ", .name = name, .visible_width = null, .rounded_corners = true, .render_content = session.is_terminal }); 904 errdefer resolution_input.deinit(allocator); 905 resolution_input.getFocus().focusable = true; 906 try resolution_input.setContent(allocator, if (data.theirsPicked(name)) fc.theirs.text else fc.ours.text); 907 try box.children.put(allocator, resolution_input.getFocus().id, .{ .widget = .{ .text_input = resolution_input }, .rect = null, .min_size = null }); 908 } 909 910 // a blank row setting a conflict group apart from its neighbors 911 fn addGap(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget)) !void { 912 var text = try wgt.Text(ui.Widget).init(allocator, " "); 913 errdefer text.deinit(allocator); 914 try box.children.put(allocator, text.getFocus().id, .{ .widget = .{ .text = text }, .rect = null, .min_size = null }); 915 } 916 917 fn addLabel(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), content: []const u8) !void { 918 var label = try ui.SectionLabel.init(allocator, content); 919 errdefer label.deinit(allocator); 920 try box.children.put(allocator, label.getFocus().id, .{ .widget = .{ .section_label = label }, .rect = null, .min_size = null }); 921 } 922 923 // one side of a conflict: the "use this" link on the left, the version 924 // itself as a labeled read-only box. 925 fn addVersionRow(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), label: []const u8, text: []const u8, link: []const u8) !void { 926 var row = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 927 errdefer row.deinit(allocator); 928 929 { 930 const use_label = "use this"; 931 var use = try wgt.TextBox(ui.Widget).init(allocator, use_label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 932 errdefer use.deinit(allocator); 933 use.getFocus().focusable = true; 934 use.getFocus().kind = .{ .custom = link }; 935 try row.children.put(allocator, use.getFocus().id, .{ 936 .widget = .{ .text_box = use }, 937 .rect = null, 938 .min_size = .{ .width = use_label.len + 2, .height = null }, 939 .max_size = .{ .width = use_label.len + 2, .height = null }, 940 }); 941 } 942 943 { 944 var tb = try wgt.TextBox(ui.Widget).init(allocator, if (text.len > 0) text else "(removed)", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .word, .label = label }); 945 errdefer tb.deinit(allocator); 946 tb.getFocus().focusable = true; 947 try row.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 948 } 949 950 row.getFocus().child_id = row.children.keys()[0]; 951 try box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .box = row }, .rect = null, .min_size = null }); 952 } 953 954 // the label of one side's version box, naming its author 955 fn sideLabel(aa: std.mem.Allocator, author: ui.Author, ours: bool) ![]const u8 { 956 return switch (author) { 957 .user_name, .email => |name| try std.fmt.allocPrint(aa, " edited by {s} ", .{name}), 958 .unknown => if (ours) " current version " else " their version ", 959 }; 960 } 961 962 // the a: link that reloads the resolve page with `name` picked from ours 963 // or theirs, reprefilling its resolution input 964 fn useThisLink(session: *ui.Session, data: *const Self, name: []const u8, theirs: bool) ![]const u8 { 965 const aa = session.page_arena.allocator(); 966 var picks: std.ArrayList(u8) = .empty; 967 var it = std.mem.splitScalar(u8, data.theirs_picks, ','); 968 while (it.next()) |pick| { 969 if (pick.len == 0 or std.mem.eql(u8, pick, name)) continue; 970 if (picks.items.len > 0) try picks.append(aa, ','); 971 try picks.appendSlice(aa, pick); 972 } 973 if (theirs) { 974 if (picks.items.len > 0) try picks.append(aa, ','); 975 try picks.appendSlice(aa, name); 976 } 977 const route = ui.RoutablePage.repoIssuesResolveRoute(data.identity, data.selected_id, picks.items) orelse return error.RouteTooLong; 978 return std.fmt.allocPrint(aa, "a:{s}", .{try route.toUrl(session.page_arena)}); 979 } 980 981 // the resolve form's page-constant initial value for the input `name`: 982 // the picked side of its conflicted field or hunk 983 fn resolvePrefill(self: *View, name: []const u8) ?[]const u8 { 984 const conflict = if (self.data.conflict) |*c| c else return null; 985 const theirs = self.data.theirsPicked(name); 986 if (std.mem.eql(u8, name, "title")) { 987 const fc = if (conflict.title) |*f| f else return null; 988 return if (theirs) fc.theirs.text else fc.ours.text; 989 } 990 if (std.mem.eql(u8, name, "tags")) { 991 const fc = if (conflict.tags) |*f| f else return null; 992 return if (theirs) fc.theirs.text else fc.ours.text; 993 } 994 if (std.mem.startsWith(u8, name, "d")) { 995 const desc = if (conflict.description) |*d| d else return null; 996 const hunk_index = std.fmt.parseInt(usize, name[1..], 10) catch return null; 997 var seen: usize = 0; 998 for (desc.chunks) |chunk| switch (chunk) { 999 .conflict => |hunk| { 1000 if (seen == hunk_index) return (if (theirs) hunk.theirs else hunk.ours) orelse ""; 1001 seen += 1; 1002 }, 1003 else => {}, 1004 }; 1005 } 1006 return null; 1007 } 1008 1009 fn addRow(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), text: []const u8, bottom_label: []const u8, link: []const u8) !void { 1010 var row = try wgt.TextBox(ui.Widget).init(allocator, text, .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .word, .bottom_label = bottom_label }); 1011 errdefer row.deinit(allocator); 1012 row.getFocus().focusable = true; 1013 if (link.len != 0) row.getFocus().kind = .{ .custom = link }; 1014 try box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .text_box = row }, .rect = null, .min_size = null, .max_size = .{ .width = null, .height = 5 } }); 1015 } 1016 1017 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 1018 self.box.deinit(allocator); 1019 } 1020 1021 fn header(self: *View) *Header { 1022 return &self.box.children.values()[header_index].widget.repo_issues_header; 1023 } 1024 1025 fn viewStack(self: *View) *wgt.Stack(ui.Widget) { 1026 return &self.box.children.values()[stack_index].widget.stack; 1027 } 1028 1029 // `index`'s master-detail split inside the stack. 1030 fn resultsBox(self: *View, index: usize) *wgt.Box(ui.Widget) { 1031 return &self.viewStack().children.values()[index].box; 1032 } 1033 1034 // the tags view's flow inside the stack. 1035 fn tagsView(self: *View) *ui.TagFlow { 1036 return &self.viewStack().children.values()[tags_view_index].tag_flow; 1037 } 1038 1039 // the new-issue, edit, or resolve form inside the stack, or null when the 1040 // unauthorized view stands in for it. 1041 fn issueForm(self: *View) ?*wgt.Box(ui.Widget) { 1042 return switch (self.viewStack().children.values()[form_view_index]) { 1043 .box => |*box| box, 1044 // the resolve form sits inside a scroll on the terminal 1045 .scroll => |*scroll| &scroll.child.box, 1046 else => null, 1047 }; 1048 } 1049 1050 // the resolve form's scroll on the terminal (the web page scrolls itself) 1051 fn resolveScroll(self: *View) ?*wgt.Scroll(ui.Widget) { 1052 return switch (self.viewStack().children.values()[form_view_index]) { 1053 .scroll => |*scroll| scroll, 1054 else => null, 1055 }; 1056 } 1057 1058 fn listScroll(self: *View, index: usize) *wgt.Scroll(ui.Widget) { 1059 return &self.resultsBox(index).children.values()[list_index].widget.scroll; 1060 } 1061 1062 fn listBox(self: *View, index: usize) *wgt.Box(ui.Widget) { 1063 return &self.listScroll(index).child.box; 1064 } 1065 1066 // the detail frame's children: the tool row above the scroll pane. 1067 const tool_row_index: usize = 0; 1068 const detail_scroll_index: usize = 1; 1069 1070 fn detailOuter(self: *View, index: usize) *wgt.Box(ui.Widget) { 1071 return &self.resultsBox(index).children.values()[detail_index].widget.box; 1072 } 1073 1074 fn toolRow(self: *View, index: usize) *wgt.Box(ui.Widget) { 1075 return &self.detailOuter(index).children.values()[tool_row_index].widget.box; 1076 } 1077 1078 fn detailScroll(self: *View, index: usize) *wgt.Scroll(ui.Widget) { 1079 return &self.detailOuter(index).children.values()[detail_scroll_index].widget.scroll; 1080 } 1081 1082 fn detailInner(self: *View, index: usize) *wgt.Box(ui.Widget) { 1083 return &self.detailScroll(index).child.box; 1084 } 1085 1086 fn window(self: *View, index: usize) *const Window { 1087 if (index == conflicts_view_index) return &self.data.conflicts; 1088 return self.data.window(splitStatus(index)); 1089 } 1090 1091 fn stackSelectedIndex(self: *View) ?usize { 1092 const stack = self.viewStack(); 1093 const cid = stack.getFocus().child_id orelse return null; 1094 return stack.children.getIndex(cid); 1095 } 1096 1097 // the stack's selected master-detail split (null when the tags view or the 1098 // new-issue form shows). 1099 fn selectedSplitIndex(self: *View) ?usize { 1100 const idx = self.stackSelectedIndex() orelse return null; 1101 return if (idx < split_count or idx == conflicts_view_index) idx else null; 1102 } 1103 1104 fn detailActive(self: *View, index: usize) bool { 1105 const rb = self.resultsBox(index); 1106 const cid = rb.getFocus().child_id orelse return false; 1107 return rb.children.getIndex(cid) == detail_index; 1108 } 1109 1110 fn headerActive(self: *View) bool { 1111 const cid = self.box.getFocus().child_id orelse return false; 1112 return self.box.children.getIndex(cid) == header_index; 1113 } 1114 1115 fn tagsViewActive(self: *View) bool { 1116 if (self.headerActive()) return false; 1117 return self.stackSelectedIndex() == tags_view_index; 1118 } 1119 1120 fn formViewActive(self: *View) bool { 1121 if (self.headerActive()) return false; 1122 return self.stackSelectedIndex() == form_view_index; 1123 } 1124 1125 // the selected issue's index, or null when a window-navigation row is 1126 // selected (a leading "previous" row shifts the issue rows down by one). 1127 fn selectedIssueIndex(self: *View, index: usize) ?usize { 1128 const lb = self.listBox(index); 1129 const cid = lb.getFocus().child_id orelse return null; 1130 const idx = lb.children.getIndex(cid) orelse return null; 1131 const win = self.window(index); 1132 const lead: usize = if (win.prev_id != null) 1 else 0; 1133 if (idx < lead or idx - lead >= win.issues.len) return null; 1134 return idx - lead; 1135 } 1136 1137 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1138 self.clearGrid(); 1139 1140 // each header tab maps 1:1 to a stack child by position. mirror the 1141 // selection into the url: the view the page's window is rooted at 1142 // keeps its rooted url, the others get their list route (keeping the 1143 // tag filter). 1144 if (self.header().getSelectedIndex()) |index| { 1145 const stack = self.viewStack(); 1146 stack.getFocus().child_id = stack.children.keys()[index]; 1147 self.session.data.current_page = (if (index == form_view_index) 1148 (switch (self.data.view) { 1149 .edit => ui.RoutablePage.repoIssuesEditRoute(self.data.identity, self.data.selected_id), 1150 .resolve => ui.RoutablePage.repoIssuesResolveRoute(self.data.identity, self.data.selected_id, self.data.theirs_picks), 1151 else => ui.RoutablePage.repoIssuesNewRoute(self.data.identity), 1152 }) 1153 else if (index == conflicts_view_index) 1154 ui.RoutablePage.repoIssuesConflictsRoute(self.data.identity, if (self.data.view == .conflicts) self.data.selected_id else "") 1155 else if (index == viewIndex(self.data.view) and self.data.selected_id.len != 0) 1156 (if (self.data.description_page) 1157 ui.RoutablePage.repoIssuesDescriptionRoute(self.data.identity, self.data.selected_id) 1158 else 1159 ui.RoutablePage.repoIssuesRoute(self.data.identity, .open, self.data.tag, self.data.selected_id)) 1160 else if (index == tags_view_index) 1161 ui.RoutablePage.repoIssuesTagsRoute(self.data.identity, self.data.tag) 1162 else 1163 ui.RoutablePage.repoIssuesRoute(self.data.identity, splitStatus(index), self.data.tag, "")) orelse self.session.data.current_page; 1164 } 1165 1166 if (self.selectedSplitIndex()) |i| { 1167 // swap the detail pane to the selected issue when it changes. 1168 try self.refreshDetail(allocator, i); 1169 1170 // mirror the focused issue into the url, but only while focus is 1171 // inside the split. an issue's url is the same whether or not the 1172 // list is filtered, so the mirror drops the tag. 1173 if (root_focus.grandchild_id) |g| { 1174 if (self.resultsBox(i).getFocus().children.contains(g)) { 1175 if (self.selectedIssueIndex(i)) |sel| { 1176 // selecting another issue leaves the description page 1177 // behind, like it leaves the tag filter behind. 1178 const entry = &self.window(i).issues[sel]; 1179 const mirrored = if (self.data.description_page and std.mem.eql(u8, entry.id, self.data.selected_id)) 1180 ui.RoutablePage.repoIssuesDescriptionRoute(self.data.identity, entry.id) 1181 else 1182 ui.RoutablePage.repoIssuesRoute(self.data.identity, entry.issue.event.status, "", entry.id); 1183 if (mirrored) |route| self.session.data.current_page = route; 1184 } 1185 } 1186 } 1187 1188 // the selected list row shows a border (the focused TextBox 1189 // upgrades it to a double border itself); the rest stay borderless. 1190 const lb = self.listBox(i); 1191 for (lb.children.keys(), lb.children.values()) |id, *child| { 1192 switch (child.widget) { 1193 .text_box => |*tb| tb.options.border_style = if (lb.getFocus().child_id == id) .single else .hidden, 1194 else => {}, 1195 } 1196 } 1197 1198 // the pane's selected child shows the selection border: the 1199 // description directly, the tags via the flow's selected item. 1200 // cap the list at list_max_width only while the detail pane fits 1201 // beside it. the box drops the detail when the width can't hold 1202 // both minimums, so when it's that narrow we lift the cap and let 1203 // the list fill the whole width. 1204 const both_panes_fit = if (constraint.max_size.width) |w| w >= list_max_width + detail_min_width else true; 1205 self.resultsBox(i).children.values()[list_index].max_size = if (both_panes_fit) .{ .width = list_max_width, .height = null } else null; 1206 1207 // stretch the detail pane across the rest of the width so it fills 1208 // the area rather than shrinking to its content; its scroll fills 1209 // the pane. 1210 if (constraint.max_size.width) |w| { 1211 self.resultsBox(i).children.values()[detail_index].min_size = .{ .width = if (both_panes_fit) w - list_max_width else w, .height = null }; 1212 } else { 1213 self.resultsBox(i).children.values()[detail_index].min_size = .{ .width = detail_min_width, .height = null }; 1214 } 1215 } 1216 1217 // refresh the form inputs' entries in the session's focus-id -> input 1218 // map with this frame's addresses, so the web/wasm form handling can 1219 // find them by focus id 1220 if (self.issueForm()) |form| { 1221 const inputs_arena = self.session.arena.allocator(); 1222 for (form.children.values()) |*child| switch (child.widget) { 1223 .text_input => |*ti| try self.session.text_inputs.put(inputs_arena, ti.getFocus().id, ti), 1224 else => {}, 1225 }; 1226 1227 // on an edit page, register the issue's content as the inputs' 1228 // page-constant initial values, which the web overlay renders 1229 // into them. 1230 if (self.data.view == .edit) { 1231 if (self.data.selectedIssue()) |entry| { 1232 const fields = form.children.values(); 1233 try self.session.input_values.put(inputs_arena, fields[title_field_index].widget.text_input.getFocus().id, entry.issue.event.title); 1234 try self.session.input_values.put(inputs_arena, fields[tags_field_index].widget.text_input.getFocus().id, entry.issue.event.tags); 1235 try self.session.input_values.put(inputs_arena, fields[description_field_index].widget.text_input.getFocus().id, entry.issue.event.description); 1236 } 1237 } 1238 1239 // the resolve form's inputs prefill from the picked side, which 1240 // is url state and so page-constant like the edit values above 1241 if (self.data.view == .resolve) { 1242 for (form.children.values()) |*child| switch (child.widget) { 1243 .text_input => |*ti| if (self.resolvePrefill(ti.options.name)) |value| { 1244 try self.session.input_values.put(inputs_arena, ti.getFocus().id, value); 1245 }, 1246 else => {}, 1247 }; 1248 } 1249 } 1250 1251 try self.box.build(allocator, constraint, root_focus); 1252 } 1253 1254 fn refreshDetail(self: *View, allocator: std.mem.Allocator, index: usize) !void { 1255 const sel = self.selectedIssueIndex(index) orelse return; 1256 if (self.detailed_index[index]) |d| if (d == sel) return; 1257 try self.populateDetail(allocator, index, sel); 1258 self.detailed_index[index] = sel; 1259 } 1260 1261 fn populateDetail(self: *View, allocator: std.mem.Allocator, index: usize, sel: usize) !void { 1262 const entry = self.window(index).issues[sel]; 1263 const inner = self.detailInner(index); 1264 // the /description page replaces its issue's detail with a back link 1265 // and the whole description; other issues keep their normal detail. 1266 const description_page = self.data.description_page and std.mem.eql(u8, entry.id, self.data.selected_id); 1267 1268 for (inner.children.values()) |*child| child.widget.deinit(allocator); 1269 inner.children.clearAndFree(allocator); 1270 inner.getFocus().child_id = null; 1271 self.author_id[index] = null; 1272 1273 // the tool row: the open/close and edit buttons; the description page 1274 // shows none. 1275 { 1276 const row = self.toolRow(index); 1277 for (row.children.values()) |*child| child.widget.deinit(allocator); 1278 row.children.clearAndFree(allocator); 1279 row.getFocus().child_id = null; 1280 row.getFocus().kind = .container; 1281 } 1282 if (!description_page) { 1283 const row = self.toolRow(index); 1284 const pa = self.session.page_arena.allocator(); 1285 1286 { 1287 var spacer = try ui.Spacer.init(allocator); 1288 errdefer spacer.deinit(allocator); 1289 try row.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 1290 } 1291 1292 // a conflicted issue's only action is resolving it. the button 1293 // stays visible logged out; the resolve page shows the 1294 // unauthorized view then. 1295 if (entry.conflicted) { 1296 const label = "resolve conflict"; 1297 var button = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1298 errdefer button.deinit(allocator); 1299 button.getFocus().focusable = true; 1300 const route = ui.RoutablePage.repoIssuesResolveRoute(self.data.identity, entry.id, "") orelse return error.RouteTooLong; 1301 button.getFocus().kind = .{ .custom = try std.fmt.allocPrint(pa, "a:{s}", .{try route.toUrl(self.session.page_arena)}) }; 1302 try row.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = .{ .width = label.len + 2, .height = null } }); 1303 } else { 1304 // a logged-out session can't flip the status, so it gets no 1305 // open/close button (and no form action on the row) 1306 if (self.session.data.is_local or self.session.data.user_id != null) { 1307 const action: []const u8 = switch (entry.issue.event.status) { 1308 .open => "close", 1309 .closed => "open", 1310 }; 1311 row.getFocus().kind = .{ .custom = if (self.data.identity.len == 0) 1312 try std.fmt.allocPrint(pa, "form:/issues/{s}/{s}", .{ entry.id, action }) 1313 else 1314 try std.fmt.allocPrint(pa, "form:/repo/{s}/issues/{s}/{s}", .{ self.data.identity, entry.id, action }) }; 1315 1316 var button = try wgt.TextBox(ui.Widget).init(allocator, action, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1317 errdefer button.deinit(allocator); 1318 button.getFocus().focusable = true; 1319 // the renderer distinguishes plain clickables from buttons that 1320 // should POST to a server route by this kind. 1321 button.getFocus().kind = .{ .custom = "submit" }; 1322 try row.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = .{ .width = action.len + 2, .height = null } }); 1323 } 1324 1325 // the edit button links to the issue's edit page. 1326 { 1327 var button = try wgt.TextBox(ui.Widget).init(allocator, "edit", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1328 errdefer button.deinit(allocator); 1329 button.getFocus().focusable = true; 1330 const route = ui.RoutablePage.repoIssuesEditRoute(self.data.identity, entry.id) orelse return error.RouteTooLong; 1331 button.getFocus().kind = .{ .custom = try std.fmt.allocPrint(pa, "a:{s}", .{try route.toUrl(self.session.page_arena)}) }; 1332 try row.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = .{ .width = "edit".len + 2, .height = null } }); 1333 } 1334 } 1335 1336 // the resolve button on a conflicted issue, else the open/close 1337 // button when present, the edit button otherwise 1338 row.getFocus().child_id = row.children.keys()[button_in_row_index]; 1339 } 1340 1341 if (description_page) { 1342 // the back link, in the title slot so the pane's input handling 1343 // applies to it unchanged. 1344 var tb = try wgt.TextBox(ui.Widget).init(allocator, "← back to issue", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1345 errdefer tb.deinit(allocator); 1346 tb.getFocus().focusable = true; 1347 tb.getFocus().kind = .{ .custom = try issuesLink(self.session.page_arena, self.data.identity, entry.issue.event.status, "", entry.id) }; 1348 try inner.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1349 } else { 1350 // the issue's title as a focusable word-wrapped text box. 1351 { 1352 var tb = try wgt.TextBox(ui.Widget).init(allocator, entry.issue.event.title, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .word, .label = " title " }); 1353 errdefer tb.deinit(allocator); 1354 tb.getFocus().focusable = true; 1355 try inner.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1356 } 1357 1358 self.author_id[index] = blk: { 1359 var tb = try ui.authorBox(allocator, self.session.page_arena, entry.author); 1360 errdefer tb.deinit(allocator); 1361 try inner.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1362 break :blk tb.getFocus().id; 1363 }; 1364 1365 // the issue's tags, each linking to this status's list filtered to 1366 // that tag. 1367 { 1368 var items: std.ArrayList(ui.TagFlow.Item) = .empty; 1369 defer items.deinit(allocator); 1370 var tag_iter = evt.Issue.tagIterator(entry.issue.event.tags); 1371 while (tag_iter.next()) |tag| { 1372 if (tag.len == 0) continue; 1373 try items.append(allocator, .{ .text = tag, .link = try tagLink(self.session.page_arena, self.data.identity, entry.issue.event.status, tag) }); 1374 } 1375 if (items.items.len > 0) { 1376 var tf = try ui.TagFlow.init(allocator); 1377 errdefer tf.deinit(allocator); 1378 try tf.setItems(allocator, items.items); 1379 try inner.children.put(allocator, tf.getFocus().id, .{ .widget = .{ .tag_flow = tf }, .rect = null, .min_size = null }); 1380 } 1381 } 1382 } 1383 1384 // the description as a focusable word-wrapped text box. past the limit 1385 // it's cut at the last whole line and links to the /description page, 1386 // which shows the whole thing. 1387 self.description_id[index] = blk: { 1388 const whole = entry.issue.event.description; 1389 const cut_short = !description_page and whole.len > max_description_size; 1390 const shown = if (cut_short) 1391 std.mem.trimEnd(u8, whole[0 .. std.mem.lastIndexOfScalar(u8, whole[0..max_description_size], '\n') orelse 0], " \t\r\n") 1392 else if (whole.len == 0) 1393 "(no description)" 1394 else 1395 whole; 1396 var tb = try wgt.TextBox(ui.Widget).init(allocator, shown, .{ 1397 .border_style = .single, 1398 .rounded_corners = true, 1399 .wrap_kind = .word, 1400 .label = " description ", 1401 .bottom_label = if (cut_short) " click or press enter to see more " else "", 1402 }); 1403 errdefer tb.deinit(allocator); 1404 tb.getFocus().focusable = true; 1405 if (cut_short) tb.getFocus().kind = .{ .custom = try descriptionLink(self.session.page_arena, self.data.identity, entry.id) }; 1406 try inner.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1407 break :blk tb.getFocus().id; 1408 }; 1409 1410 // select the title by default 1411 inner.getFocus().child_id = inner.children.keys()[title_child_index]; 1412 1413 // reset the scroll to the top for the newly-shown issue: directly on the 1414 // terminal (the wasm offset), and via a version bump on the web (so the 1415 // renderer's scroll id changes and JS drops the preserved position). 1416 const sc = self.detailScroll(index); 1417 sc.x = 0; 1418 sc.y = 0; 1419 sc.getFocus().version +%= 1; 1420 } 1421 1422 pub fn input(self: *View, allocator: std.mem.Allocator, raw_key: Key, root_focus: *Focus) !void { 1423 // wheel input moves focus like the arrows 1424 const key: Key = if (raw_key == .mouse and raw_key.mouse.action == .scroll) 1425 (if (raw_key.mouse.action.scroll == .up) .arrow_up else .arrow_down) 1426 else 1427 raw_key; 1428 1429 if (self.headerActive()) { 1430 // down from the tabs re-enters the stack if the selected view has 1431 // something to focus; other keys move the tabs. 1432 if (inp.vertDirection(key) == .down) { 1433 const enterable = if (self.viewStack().getSelected()) |selected| switch (selected.*) { 1434 .tag_flow => |*tf| tf.text_boxes.items.len > 0, 1435 // nothing focusable in the unauthorized view 1436 .unauthorized => false, 1437 else => true, 1438 } else false; 1439 if (enterable) root_focus.setFocus(self.box.children.keys()[stack_index]); 1440 } else { 1441 try self.header().input(allocator, key, root_focus); 1442 } 1443 return; 1444 } 1445 if (self.tagsViewActive()) { 1446 self.tagsViewInput(key, root_focus); 1447 return; 1448 } 1449 if (self.formViewActive()) { 1450 if (self.data.view == .resolve) { 1451 try self.resolveInput(allocator, key, root_focus); 1452 } else { 1453 try self.formInput(allocator, key, root_focus); 1454 } 1455 return; 1456 } 1457 const i = self.selectedSplitIndex() orelse return; 1458 if (self.detailActive(i)) { 1459 try self.detailInput(allocator, i, key, root_focus); 1460 } else { 1461 try self.listInput(i, key, root_focus); 1462 } 1463 } 1464 1465 // arrow keys move the tag selection; up from the top row crosses to the 1466 // header tabs. 1467 fn tagsViewInput(self: *View, key: Key, root_focus: *Focus) void { 1468 const tf = self.tagsView(); 1469 const cid = tf.focus.child_id orelse return; 1470 const cur = tf.indexOfFocusId(cid) orelse return; 1471 const count = tf.text_boxes.items.len; 1472 switch (key) { 1473 .arrow_left => if (cur > 0) root_focus.setFocus(tf.text_boxes.items[cur - 1].getFocus().id), 1474 .arrow_right => if (cur + 1 < count) root_focus.setFocus(tf.text_boxes.items[cur + 1].getFocus().id), 1475 .arrow_up => if (tf.rowStep(cur, false)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id) else self.focusHeader(root_focus), 1476 .arrow_down => if (tf.rowStep(cur, true)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id), 1477 .home => root_focus.setFocus(tf.text_boxes.items[0].getFocus().id), 1478 .end => root_focus.setFocus(tf.text_boxes.items[count - 1].getFocus().id), 1479 else => {}, 1480 } 1481 } 1482 1483 fn listInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 1484 // up/down (and the scroll wheel) move the selection a row; page up/down 1485 // jump a fixed amount. right/Enter cross into the detail pane. up from 1486 // the top row crosses into the header tabs. 1487 if (inp.rowDelta(key, @intCast(self.listBox(index).children.count()))) |delta| { 1488 const lb = self.listBox(index); 1489 const at_top = if (lb.getFocus().child_id) |cid| lb.children.getIndex(cid) == 0 else true; 1490 if (delta < 0 and at_top) return self.focusHeader(root_focus); 1491 ui.moveRowFocus(lb, self.listScroll(index), root_focus, delta); 1492 return; 1493 } 1494 switch (key) { 1495 .enter, .arrow_right => try self.focusDetail(index, root_focus), 1496 else => {}, 1497 } 1498 } 1499 1500 fn detailInput(self: *View, allocator: std.mem.Allocator, index: usize, key: Key, root_focus: *Focus) !void { 1501 if (self.toolRowFocused(index)) { 1502 try self.toolRowInput(allocator, index, key, root_focus); 1503 } else if (self.titleFocused(index)) { 1504 try self.titleInput(index, key, root_focus); 1505 } else if (self.authorFocused(index)) { 1506 try self.authorInput(index, key, root_focus); 1507 } else if (self.tagsFocused(index)) { 1508 try self.tagsInput(index, key, root_focus); 1509 } else { 1510 try self.descriptionInput(index, key, root_focus); 1511 } 1512 } 1513 1514 // the tool row: enter or a click on the open/close button flips the 1515 // issue's status; the edit and resolve buttons are a: links the host 1516 // follows. arrows cross between the buttons and to the neighboring 1517 // widgets. 1518 fn toolRowInput(self: *View, allocator: std.mem.Allocator, index: usize, key: Key, root_focus: *Focus) !void { 1519 const row = self.toolRow(index); 1520 const cur = if (row.getFocus().child_id) |cid| row.children.getIndex(cid) orelse return else return; 1521 const on_status = cur == button_in_row_index and self.statusButton(index) != null; 1522 switch (key) { 1523 .arrow_left => if (cur > button_in_row_index) root_focus.setFocus(row.children.keys()[cur - 1]) else try self.focusList(index, root_focus), 1524 .arrow_right => if (cur + 1 < row.children.count()) root_focus.setFocus(row.children.keys()[cur + 1]), 1525 .arrow_up => self.focusHeader(root_focus), 1526 .arrow_down => self.focusTitle(index, root_focus), 1527 .enter => if (on_status) try self.toggleIssueStatus(allocator, index), 1528 .mouse => |mouse| if (self.statusButton(index)) |button| { 1529 if (inp.leftClickOn(root_focus, button.getFocus().id, mouse)) try self.toggleIssueStatus(allocator, index); 1530 }, 1531 else => {}, 1532 } 1533 } 1534 1535 fn titleInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 1536 switch (key) { 1537 .arrow_left => try self.focusList(index, root_focus), 1538 .arrow_up => self.focusToolRow(index, root_focus), 1539 .arrow_down => if (self.authorPresent(index)) self.focusAuthor(index, root_focus) else try self.focusDescription(index, root_focus), 1540 else => {}, 1541 } 1542 } 1543 1544 // the author box's a: link (when it has one) is followed by the host on 1545 // enter / a click; arrows cross to the neighboring widgets. 1546 fn authorInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 1547 switch (key) { 1548 .arrow_left => try self.focusList(index, root_focus), 1549 .arrow_up => self.focusTitle(index, root_focus), 1550 .arrow_down => if (self.tagFlow(index) != null) try self.focusTags(index, root_focus) else try self.focusDescription(index, root_focus), 1551 else => {}, 1552 } 1553 } 1554 1555 fn descriptionInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 1556 const sc = self.detailScroll(index); 1557 switch (key) { 1558 .arrow_left => return self.focusList(index, root_focus), 1559 // once the scroll can't move further, cross into the tags (or the 1560 // open/close button when the issue has none). 1561 .arrow_up => { 1562 const before = sc.y; 1563 sc.y -= 1; 1564 sc.clampToContent(); 1565 if (sc.y == before) { 1566 if (self.tagFlow(index) != null) 1567 try self.focusTags(index, root_focus) 1568 else if (self.authorPresent(index)) 1569 self.focusAuthor(index, root_focus) 1570 else 1571 self.focusTitle(index, root_focus); 1572 } 1573 return; 1574 }, 1575 .arrow_down => sc.y += 1, 1576 .page_up => sc.y -= 10, 1577 .page_down => sc.y += 10, 1578 .home => sc.y = 0, 1579 .end => sc.y = std.math.maxInt(isize), 1580 else => return, 1581 } 1582 sc.clampToContent(); 1583 } 1584 1585 // up/down (and tab/shift+tab) move between a form's fields; up from the 1586 // title crosses into the header tabs. the multiline description keeps 1587 // enter and any up/down that has a row to move to. 1588 fn formInput(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1589 const form = self.issueForm() orelse return; 1590 const cid = form.getFocus().child_id orelse return; 1591 const cur = form.children.getIndex(cid) orelse return; 1592 1593 if (cur == description_field_index) { 1594 const child = &form.children.values()[cur]; 1595 if (try multilineKeepsKey(&child.widget.text_input, allocator, key)) 1596 return child.widget.input(allocator, key, root_focus); 1597 } 1598 1599 switch (key) { 1600 .arrow_up, .back_tab => if (cur > 0) 1601 root_focus.setFocus(form.children.keys()[cur - 1]) 1602 else 1603 self.focusHeader(root_focus), 1604 .arrow_down, .tab => if (cur < submit_field_index) { 1605 root_focus.setFocus(form.children.keys()[cur + 1]); 1606 }, 1607 .enter => if (cur == submit_field_index) try self.submitForm(allocator), 1608 .mouse => |mouse| if (cur == submit_field_index) { 1609 const submit = &form.children.values()[cur].widget.submit_button; 1610 if (inp.leftClickOn(root_focus, submit.buttonId(), mouse)) try self.submitForm(allocator); 1611 }, 1612 else => try form.children.values()[cur].widget.input(allocator, key, root_focus), 1613 } 1614 } 1615 1616 // the multiline hunk inputs keep enter and any up/down that has a row to 1617 // move to; enter on a "use this" link is a navigation the host follows 1618 // before this runs. 1619 fn resolveInput(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1620 const form = self.issueForm() orelse return; 1621 const cid = form.getFocus().child_id orelse return; 1622 const cur = form.children.getIndex(cid) orelse return; 1623 const child = &form.children.values()[cur]; 1624 1625 if (child.widget == .text_input) { 1626 const ti = &child.widget.text_input; 1627 if (ti.options.multiline and try multilineKeepsKey(ti, allocator, key)) 1628 return child.widget.input(allocator, key, root_focus); 1629 } 1630 1631 const on_submit = child.widget == .submit_button; 1632 1633 switch (key) { 1634 .arrow_up, .back_tab => if (resolveStep(form, cur, false)) |i| self.focusResolveChild(form, i, root_focus) else self.focusHeader(root_focus), 1635 .arrow_down, .tab => if (resolveStep(form, cur, true)) |i| self.focusResolveChild(form, i, root_focus), 1636 .arrow_left, .arrow_right => switch (child.widget) { 1637 .box => |*row| { 1638 const row_cid = row.getFocus().child_id orelse return; 1639 const row_index = row.children.getIndex(row_cid) orelse return; 1640 if (key == .arrow_left) { 1641 if (row_index > 0) root_focus.setFocus(row.children.keys()[row_index - 1]); 1642 } else if (row_index + 1 < row.children.count()) { 1643 root_focus.setFocus(row.children.keys()[row_index + 1]); 1644 } 1645 }, 1646 else => try child.widget.input(allocator, key, root_focus), 1647 }, 1648 .enter => if (on_submit) try self.submitResolution(allocator), 1649 .mouse => |mouse| if (on_submit) { 1650 if (inp.leftClickOn(root_focus, child.widget.submit_button.buttonId(), mouse)) try self.submitResolution(allocator); 1651 }, 1652 else => try child.widget.input(allocator, key, root_focus), 1653 } 1654 } 1655 1656 // a multiline input keeps enter and any up/down that has a row to move to 1657 fn multilineKeepsKey(text_input: *wgt.TextInput(ui.Widget), allocator: std.mem.Allocator, key: Key) !bool { 1658 return switch (key) { 1659 .enter => true, 1660 .arrow_up => !try text_input.cursorOnFirstRow(allocator), 1661 .arrow_down => !try text_input.cursorOnLastRow(allocator), 1662 else => false, 1663 }; 1664 } 1665 1666 // the neighboring focusable resolve-form child, skipping the spacer and 1667 // the blank gap rows 1668 fn resolveStep(form: *wgt.Box(ui.Widget), cur: usize, down: bool) ?usize { 1669 var i = cur; 1670 while (true) { 1671 if (down) { 1672 i += 1; 1673 if (i >= form.children.count()) return null; 1674 } else { 1675 if (i == 0) return null; 1676 i -= 1; 1677 } 1678 switch (form.children.values()[i].widget) { 1679 .spacer, .text => continue, 1680 else => return i, 1681 } 1682 } 1683 } 1684 1685 // focus the resolve-form child at `i` (a version row focuses its selected 1686 // child) and keep it visible on the terminal 1687 fn focusResolveChild(self: *View, form: *wgt.Box(ui.Widget), i: usize, root_focus: *Focus) void { 1688 const child = &form.children.values()[i]; 1689 switch (child.widget) { 1690 .box => |*row| root_focus.setFocus(row.getFocus().child_id orelse form.children.keys()[i]), 1691 else => root_focus.setFocus(form.children.keys()[i]), 1692 } 1693 if (self.session.is_terminal) { 1694 if (self.resolveScroll()) |sc| { 1695 if (child.rect) |rect| sc.scrollToRect(rect); 1696 } 1697 } 1698 } 1699 1700 // re-emit the conflicted issue's event with the resolved content; any 1701 // event on the issue settles its conflict entry. this is the terminal 1702 // path; the web posts the form to the resolve route. 1703 fn submitResolution(self: *View, allocator: std.mem.Allocator) !void { 1704 if (comptime wasm) return; 1705 const io = self.session.io orelse return; 1706 const src = self.data.repo_source orelse return; 1707 const entry = self.data.selectedIssue() orelse return; 1708 const author_email = (try self.session.eventAuthorEmail()) orelse return; 1709 const form = self.issueForm() orelse return; 1710 1711 // gather the inputs by name; the d<n> hunk inputs appear in chunk order 1712 var title: ?[]u8 = null; 1713 var tags: ?[]u8 = null; 1714 var hunks: std.ArrayList([]const u8) = .empty; 1715 defer { 1716 if (title) |t| allocator.free(t); 1717 if (tags) |t| allocator.free(t); 1718 for (hunks.items) |h| allocator.free(h); 1719 hunks.deinit(allocator); 1720 } 1721 for (form.children.values()) |*child| switch (child.widget) { 1722 .text_input => |*ti| { 1723 const text = try ti.text(allocator); 1724 errdefer allocator.free(text); 1725 if (std.mem.eql(u8, ti.options.name, "title")) { 1726 title = text; 1727 } else if (std.mem.eql(u8, ti.options.name, "tags")) { 1728 tags = text; 1729 } else { 1730 try hunks.append(allocator, text); 1731 } 1732 }, 1733 else => {}, 1734 }; 1735 1736 var id_bytes: [evt.event_id_size]u8 = undefined; 1737 _ = try std.fmt.hexToBytes(&id_bytes, entry.id[0 .. evt.event_id_size * 2]); 1738 1739 switch (src.repo_kind) { 1740 inline else => |repo_kind| { 1741 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, .{ .path = src.path }); 1742 defer any_repo.deinit(io, allocator); 1743 switch (any_repo) { 1744 inline else => |*repo| evt.Issue.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .resolve = .{ 1745 .title = title, 1746 .tags = tags, 1747 .hunks = hunks.items, 1748 } }, author_email) catch |err| switch (err) { 1749 // leave the form up for correction 1750 error.InvalidFields => return, 1751 else => |e| return e, 1752 }, 1753 } 1754 }, 1755 } 1756 1757 const route = ui.RoutablePage.repoIssuesRoute(self.data.identity, entry.issue.event.status, "", entry.id) orelse return; 1758 try self.session.navigate(route); 1759 } 1760 1761 // the terminal submit paths: the new form commits a new-issue event, the 1762 // edit form re-emits the selected issue's event with the form's content. 1763 // the web posts the forms to their routes instead. 1764 fn submitForm(self: *View, allocator: std.mem.Allocator) !void { 1765 if (self.data.view == .edit) { 1766 try self.submitEditedIssue(allocator); 1767 } else { 1768 try self.submitNewIssue(allocator); 1769 } 1770 } 1771 1772 // commit the new issue to the repo's events branch and navigate to it. 1773 // this is the terminal path; the web posts the form to the issue route, 1774 // so the wasm side never runs (or compiles) the repo access below. 1775 fn submitNewIssue(self: *View, allocator: std.mem.Allocator) !void { 1776 if (comptime wasm) return; 1777 const io = self.session.io orelse return; 1778 const src = self.data.repo_source orelse return; 1779 const author_email = (try self.session.eventAuthorEmail()) orelse return; 1780 1781 const form = self.issueForm() orelse return; 1782 const title_input = &form.children.values()[title_field_index].widget.text_input; 1783 const tags_input = &form.children.values()[tags_field_index].widget.text_input; 1784 const description_input = &form.children.values()[description_field_index].widget.text_input; 1785 1786 const title = try title_input.text(allocator); 1787 defer allocator.free(title); 1788 const tags = try tags_input.text(allocator); 1789 defer allocator.free(tags); 1790 const description = try description_input.text(allocator); 1791 defer allocator.free(description); 1792 1793 if (!evt.Issue.fieldsValid(title, tags)) return; 1794 1795 var id_bytes: [evt.event_id_size]u8 = undefined; 1796 io.random(&id_bytes); 1797 const event_id_hex = std.fmt.bytesToHex(id_bytes, .lower); 1798 1799 const event = evt.EventWithId{ 1800 .id = event_id_hex, 1801 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 1802 .author_email = author_email, 1803 .event = .{ .issue = .{ 1804 .title = title, 1805 .description = description, 1806 .tags = tags, 1807 } }, 1808 }; 1809 1810 switch (src.repo_kind) { 1811 inline else => |repo_kind| { 1812 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, .{ .path = src.path }); 1813 defer any_repo.deinit(io, allocator); 1814 switch (any_repo) { 1815 inline else => |*repo| try evt.consume(repo_kind, repo.self_repo_opts, io, allocator, repo, evt.events_ref, &.{event}), 1816 } 1817 }, 1818 } 1819 1820 // wipe the form so a return visit starts fresh 1821 title_input.clear(allocator); 1822 tags_input.clear(allocator); 1823 description_input.clear(allocator); 1824 1825 const route = ui.RoutablePage.repoIssuesRoute(self.data.identity, .open, "", &event_id_hex) orelse return; 1826 try self.session.navigate(route); 1827 } 1828 1829 // re-emit the selected issue's event with the form's content (status 1830 // preserved), then reload the issue's page. this is the terminal path; 1831 // the web posts the form to the edit route. 1832 fn submitEditedIssue(self: *View, allocator: std.mem.Allocator) !void { 1833 if (comptime wasm) return; 1834 const io = self.session.io orelse return; 1835 const src = self.data.repo_source orelse return; 1836 const entry = self.data.selectedIssue() orelse return; 1837 const author_email = (try self.session.eventAuthorEmail()) orelse return; 1838 1839 const form = self.issueForm() orelse return; 1840 const title_input = &form.children.values()[title_field_index].widget.text_input; 1841 const tags_input = &form.children.values()[tags_field_index].widget.text_input; 1842 const description_input = &form.children.values()[description_field_index].widget.text_input; 1843 1844 const title = try title_input.text(allocator); 1845 defer allocator.free(title); 1846 const tags = try tags_input.text(allocator); 1847 defer allocator.free(tags); 1848 const description = try description_input.text(allocator); 1849 defer allocator.free(description); 1850 1851 if (!evt.Issue.fieldsValid(title, tags)) return; 1852 1853 var id_bytes: [evt.event_id_size]u8 = undefined; 1854 _ = try std.fmt.hexToBytes(&id_bytes, entry.id[0 .. evt.event_id_size * 2]); 1855 1856 switch (src.repo_kind) { 1857 inline else => |repo_kind| { 1858 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, .{ .path = src.path }); 1859 defer any_repo.deinit(io, allocator); 1860 switch (any_repo) { 1861 inline else => |*repo| try evt.Issue.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 1862 .title = title, 1863 .tags = tags, 1864 .description = description, 1865 } }, author_email), 1866 } 1867 }, 1868 } 1869 1870 const route = ui.RoutablePage.repoIssuesRoute(self.data.identity, entry.issue.event.status, "", entry.id) orelse return; 1871 try self.session.navigate(route); 1872 } 1873 1874 // flip the shown issue's status by re-emitting its event, then reload the 1875 // page rooted at the issue so the view reflects the change. this is the 1876 // terminal path; the web posts the button's form to the status route. 1877 fn toggleIssueStatus(self: *View, allocator: std.mem.Allocator, index: usize) !void { 1878 if (comptime wasm) return; 1879 const io = self.session.io orelse return; 1880 const src = self.data.repo_source orelse return; 1881 const sel = self.detailed_index[index] orelse return; 1882 const entry = self.window(index).issues[sel]; 1883 const author_email = (try self.session.eventAuthorEmail()) orelse return; 1884 1885 const status: evt.Issue.Status = switch (entry.issue.event.status) { 1886 .open => .closed, 1887 .closed => .open, 1888 }; 1889 1890 var id_bytes: [evt.event_id_size]u8 = undefined; 1891 _ = try std.fmt.hexToBytes(&id_bytes, entry.id[0 .. evt.event_id_size * 2]); 1892 1893 switch (src.repo_kind) { 1894 inline else => |repo_kind| { 1895 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, .{ .path = src.path }); 1896 defer any_repo.deinit(io, allocator); 1897 switch (any_repo) { 1898 inline else => |*repo| try evt.Issue.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .status = status }, author_email), 1899 } 1900 }, 1901 } 1902 1903 const route = ui.RoutablePage.repoIssuesRoute(self.data.identity, status, "", entry.id) orelse return; 1904 try self.session.navigate(route); 1905 } 1906 1907 // arrow keys move the tag selection; at the flow's edges focus crosses to 1908 // the neighboring widgets. 1909 fn tagsInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 1910 const tf = self.tagFlow(index) orelse return; 1911 const cid = tf.focus.child_id orelse return; 1912 const cur = tf.indexOfFocusId(cid) orelse return; 1913 const count = tf.text_boxes.items.len; 1914 switch (key) { 1915 .arrow_left => if (cur > 0) self.focusTag(index, tf, root_focus, cur - 1) else try self.focusList(index, root_focus), 1916 .arrow_right => if (cur + 1 < count) self.focusTag(index, tf, root_focus, cur + 1), 1917 .arrow_up => if (tf.rowStep(cur, false)) |i| self.focusTag(index, tf, root_focus, i) else self.focusAuthor(index, root_focus), 1918 .arrow_down => if (tf.rowStep(cur, true)) |i| self.focusTag(index, tf, root_focus, i) else try self.focusDescription(index, root_focus), 1919 .home => self.focusTag(index, tf, root_focus, 0), 1920 .end => self.focusTag(index, tf, root_focus, count - 1), 1921 else => {}, 1922 } 1923 } 1924 1925 const button_in_row_index: usize = 1; 1926 const title_child_index: usize = 0; 1927 const tags_child_index: usize = 2; 1928 1929 fn tagFlow(self: *View, index: usize) ?*ui.TagFlow { 1930 const inner = self.detailInner(index); 1931 if (inner.children.count() <= tags_child_index) return null; 1932 return switch (inner.children.values()[tags_child_index].widget) { 1933 .tag_flow => |*tf| tf, 1934 else => null, 1935 }; 1936 } 1937 1938 fn tagsFocused(self: *View, index: usize) bool { 1939 const inner = self.detailInner(index); 1940 const cid = inner.getFocus().child_id orelse return false; 1941 return inner.children.getIndex(cid) == tags_child_index and self.tagFlow(index) != null; 1942 } 1943 1944 // the open/close button inside the detail frame's tool row, identified 1945 // by its posting kind; a logged-out session's or conflicted issue's row 1946 // has none. 1947 fn statusButton(self: *View, index: usize) ?*wgt.TextBox(ui.Widget) { 1948 const row = self.toolRow(index); 1949 if (row.children.count() <= button_in_row_index) return null; 1950 switch (row.children.values()[button_in_row_index].widget) { 1951 .text_box => |*tb| switch (tb.box.focus.kind) { 1952 .custom => |custom| if (std.mem.eql(u8, custom, "submit")) return tb, 1953 else => {}, 1954 }, 1955 else => {}, 1956 } 1957 return null; 1958 } 1959 1960 fn toolRowFocused(self: *View, index: usize) bool { 1961 const outer = self.detailOuter(index); 1962 const cid = outer.getFocus().child_id orelse return false; 1963 return outer.children.getIndex(cid) == tool_row_index; 1964 } 1965 1966 fn focusTag(self: *View, index: usize, tf: *ui.TagFlow, root_focus: *Focus, item: usize) void { 1967 root_focus.setFocus(tf.text_boxes.items[item].getFocus().id); 1968 // keep the tag visible on the terminal: its rect offset by the flow's 1969 // position in the pane. 1970 if (self.session.is_terminal and item < tf.rects.items.len) { 1971 if (self.detailInner(index).children.values()[tags_child_index].rect) |flow_rect| { 1972 var rect = tf.rects.items[item]; 1973 rect.x += flow_rect.x; 1974 rect.y += flow_rect.y; 1975 self.detailScroll(index).scrollToRect(rect); 1976 } 1977 } 1978 } 1979 1980 fn titleFocused(self: *View, index: usize) bool { 1981 const inner = self.detailInner(index); 1982 const cid = inner.getFocus().child_id orelse return false; 1983 return inner.children.getIndex(cid) == title_child_index; 1984 } 1985 1986 fn authorPresent(self: *View, index: usize) bool { 1987 return self.author_id[index] != null; 1988 } 1989 1990 fn authorFocused(self: *View, index: usize) bool { 1991 const inner = self.detailInner(index); 1992 const cid = inner.getFocus().child_id orelse return false; 1993 return cid == self.author_id[index]; 1994 } 1995 1996 // the author sits right under the title at the top of the pane, so 1997 // focusing it scrolls there. 1998 fn focusAuthor(self: *View, index: usize, root_focus: *Focus) void { 1999 const id = self.author_id[index] orelse return; 2000 root_focus.setFocus(id); 2001 self.detailScroll(index).y = 0; 2002 } 2003 2004 // the title sits at the top of the pane, so focusing it scrolls there. 2005 fn focusTitle(self: *View, index: usize, root_focus: *Focus) void { 2006 const inner = self.detailInner(index); 2007 if (inner.children.count() == 0) return; 2008 root_focus.setFocus(inner.children.keys()[title_child_index]); 2009 self.detailScroll(index).y = 0; 2010 } 2011 2012 fn focusTags(self: *View, index: usize, root_focus: *Focus) !void { 2013 const tf = self.tagFlow(index) orelse return; 2014 if (tf.text_boxes.items.len == 0) return; 2015 const cid = tf.focus.child_id orelse tf.text_boxes.items[0].getFocus().id; 2016 const item = tf.indexOfFocusId(cid) orelse 0; 2017 self.focusTag(index, tf, root_focus, item); 2018 } 2019 2020 fn focusDescription(self: *View, index: usize, root_focus: *Focus) !void { 2021 if (self.description_id[index]) |id| root_focus.setFocus(id); 2022 } 2023 2024 // return to the tool row's last-focused button (the header when the 2025 // description page shows no row). 2026 fn focusToolRow(self: *View, index: usize, root_focus: *Focus) void { 2027 const cid = self.toolRow(index).getFocus().child_id orelse return self.focusHeader(root_focus); 2028 root_focus.setFocus(cid); 2029 } 2030 2031 // enter the detail pane. an empty pane (no issues) can't be entered. 2032 fn focusDetail(self: *View, index: usize, root_focus: *Focus) !void { 2033 if (self.detailInner(index).children.count() == 0) return; 2034 root_focus.setFocus(self.detailOuter(index).getFocus().id); 2035 } 2036 2037 // return to the list. 2038 fn focusList(self: *View, index: usize, root_focus: *Focus) !void { 2039 root_focus.setFocus(self.listScroll(index).getFocus().id); 2040 } 2041 2042 // cross to the header tabs above the stack. 2043 fn focusHeader(self: *View, root_focus: *Focus) void { 2044 root_focus.setFocus(self.box.children.keys()[header_index]); 2045 } 2046 2047 pub fn clearGrid(self: *View) void { 2048 self.box.clearGrid(); 2049 } 2050 2051 pub fn getGrid(self: View) ?Grid { 2052 return self.box.getGrid(); 2053 } 2054 2055 pub fn getFocus(self: *View) *Focus { 2056 return self.box.getFocus(); 2057 } 2058 2059 // for the parent's "scroll up at the top jumps to the header" check: at the 2060 // top only while the header tabs hold focus, so up from the split 2061 // crosses into the tabs first. 2062 pub fn getSelectedIndex(self: *View) ?usize { 2063 return if (self.headerActive()) 0 else 1; 2064 } 2065 }; 2066 2067 // the "a:" navigation link for `status`'s issues page filtered to the 2068 // url-encoded `tag` and rooted at issue `id` within `identity` ("owner/name"). 2069 // a rooted url ignores `status` (it derives its view from the issue's own); 2070 // with an empty `id` this is the bare list link. 2071 fn issuesLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, status: evt.Issue.Status, tag: []const u8, id: []const u8) ![]const u8 { 2072 const route = ui.RoutablePage.repoIssuesRoute(identity, status, tag, id) orelse return error.RouteTooLong; 2073 const url = try route.toUrl(page_arena); 2074 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2075 } 2076 2077 // the in-page "ai:" anchor for selecting issue `id` in `identity`'s list; the 2078 // href is only followed with js off. 2079 fn issueRowLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, id: []const u8) ![]const u8 { 2080 const route = ui.RoutablePage.repoIssuesRoute(identity, .open, "", id) orelse return error.RouteTooLong; 2081 const url = try route.toUrl(page_arena); 2082 return std.fmt.allocPrint(page_arena.allocator(), "ai:{s}", .{url}); 2083 } 2084 2085 // the "a:" link to the window `status_maybe` names, keeping the tag filter, 2086 // or the conflicts window when null, rooted at issue `id`. 2087 fn windowLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, status_maybe: ?evt.Issue.Status, id: []const u8) ![]const u8 { 2088 if (status_maybe) |status| return issuesLink(page_arena, data.identity, status, data.tag, id); 2089 return conflictsLink(page_arena, data.identity, id); 2090 } 2091 2092 // the "a:" link to the conflicts list rooted at issue `start` ("" = the 2093 // first window). 2094 fn conflictsLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, start: []const u8) ![]const u8 { 2095 const route = ui.RoutablePage.repoIssuesConflictsRoute(identity, start) orelse return error.RouteTooLong; 2096 const url = try route.toUrl(page_arena); 2097 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2098 } 2099 2100 // the "a:" link to the whole-description page of issue `id` within `identity`. 2101 fn descriptionLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, id: []const u8) ![]const u8 { 2102 const route = ui.RoutablePage.repoIssuesDescriptionRoute(identity, id) orelse return error.RouteTooLong; 2103 const url = try route.toUrl(page_arena); 2104 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2105 } 2106 2107 // the "a:" link to `status`'s issues list filtered to `tag` (raw; encoded here). 2108 fn tagLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, status: evt.Issue.Status, tag: []const u8) ![]const u8 { 2109 const encoded = try ui.urlEncodeRef(page_arena.allocator(), tag); 2110 const route = ui.RoutablePage.repoIssuesRoute(identity, status, encoded, "") orelse return error.RouteTooLong; 2111 const url = try route.toUrl(page_arena); 2112 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2113 } 2114 2115 // tabs switching between the issues page's views. 2116 pub const Header = struct { 2117 box: wgt.Box(ui.Widget), 2118 tab_ids: std.AutoArrayHashMapUnmanaged(usize, void), 2119 2120 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self) !Header { 2121 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 2122 errdefer box.deinit(allocator); 2123 2124 var tab_ids: std.AutoArrayHashMapUnmanaged(usize, void) = .empty; 2125 errdefer tab_ids.deinit(allocator); 2126 2127 const aa = session.page_arena.allocator(); 2128 2129 // a list tab per status, labeled with its listing's issue count 2130 for ([_]evt.Issue.Status{ .open, .closed }) |status| { 2131 const route = ui.RoutablePage.repoIssuesRoute(data.identity, status, data.tag, "") orelse return error.RouteTooLong; 2132 const link = try std.fmt.allocPrint(aa, "ai:{s}", .{try route.toUrl(session.page_arena)}); 2133 const label = try std.fmt.allocPrint(aa, "{s} ({d})", .{ @tagName(status), data.window(status).count }); 2134 try addTab(allocator, &box, &tab_ids, label, link); 2135 } 2136 2137 // tags tab, labeled with the active tag filter 2138 { 2139 const tags_route = ui.RoutablePage.repoIssuesTagsRoute(data.identity, data.tag) orelse return error.RouteTooLong; 2140 const tags_link = try std.fmt.allocPrint(aa, "ai:{s}", .{try tags_route.toUrl(session.page_arena)}); 2141 const label = if (data.tag.len == 0) "tags" else blk: { 2142 const decoded = std.Uri.percentDecodeInPlace(try aa.dupe(u8, data.tag)); 2143 break :blk try std.fmt.allocPrint(aa, "tags ({s})", .{decoded}); 2144 }; 2145 try addTab(allocator, &box, &tab_ids, label, tags_link); 2146 } 2147 2148 // new-issue tab; an edit or resolve url shows its tab in this place 2149 { 2150 const route = switch (data.view) { 2151 .edit => ui.RoutablePage.repoIssuesEditRoute(data.identity, data.selected_id) orelse return error.RouteTooLong, 2152 .resolve => ui.RoutablePage.repoIssuesResolveRoute(data.identity, data.selected_id, data.theirs_picks) orelse return error.RouteTooLong, 2153 else => ui.RoutablePage.repoIssuesNewRoute(data.identity) orelse return error.RouteTooLong, 2154 }; 2155 const link = try std.fmt.allocPrint(aa, "ai:{s}", .{try route.toUrl(session.page_arena)}); 2156 const label: []const u8 = switch (data.view) { 2157 .edit => "edit", 2158 .resolve => "resolve", 2159 else => "new", 2160 }; 2161 try addTab(allocator, &box, &tab_ids, label, link); 2162 } 2163 2164 // conflicts tab, labeled with the conflict count 2165 if (data.conflicts.count > 0) { 2166 const route = ui.RoutablePage.repoIssuesConflictsRoute(data.identity, "") orelse return error.RouteTooLong; 2167 const link = try std.fmt.allocPrint(aa, "ai:{s}", .{try route.toUrl(session.page_arena)}); 2168 const label = try std.fmt.allocPrint(aa, "conflicts ({d})", .{data.conflicts.count}); 2169 try addTab(allocator, &box, &tab_ids, label, link); 2170 } 2171 2172 var self = Header{ .box = box, .tab_ids = tab_ids }; 2173 // the tab matching the page's view is selected initially. 2174 self.getFocus().child_id = self.tab_ids.keys()[View.viewIndex(data.view)]; 2175 return self; 2176 } 2177 2178 fn addTab(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), tab_ids: *std.AutoArrayHashMapUnmanaged(usize, void), label: []const u8, link: []const u8) !void { 2179 var text_box = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 2180 errdefer text_box.deinit(allocator); 2181 text_box.getFocus().focusable = true; 2182 text_box.getFocus().kind = .{ .custom = link }; 2183 try tab_ids.put(allocator, text_box.getFocus().id, {}); 2184 try box.children.put(allocator, text_box.getFocus().id, .{ 2185 .widget = .{ .text_box = text_box }, 2186 .rect = null, 2187 .min_size = .{ .width = label.len + 2, .height = null }, 2188 }); 2189 } 2190 2191 pub fn deinit(self: *Header, allocator: std.mem.Allocator) void { 2192 self.box.deinit(allocator); 2193 self.tab_ids.deinit(allocator); 2194 } 2195 2196 pub fn build(self: *Header, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2197 self.clearGrid(); 2198 // only the selected tab shows its border 2199 for (self.box.children.keys(), self.box.children.values()) |id, *child| { 2200 switch (child.widget) { 2201 .text_box => |*tb| tb.options.border_style = if (self.getFocus().child_id == id) .single else .hidden, 2202 else => {}, 2203 } 2204 } 2205 try self.box.build(allocator, constraint, root_focus); 2206 } 2207 2208 pub fn input(self: *Header, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2209 _ = allocator; 2210 const current_tab = self.currentTabIndex() orelse return; 2211 if (inp.moveTab(key, current_tab, self.tab_ids.count())) |new_tab| { 2212 root_focus.setFocus(self.tab_ids.keys()[new_tab]); 2213 } 2214 } 2215 2216 pub fn clearGrid(self: *Header) void { 2217 self.box.clearGrid(); 2218 } 2219 2220 pub fn getGrid(self: Header) ?Grid { 2221 return self.box.getGrid(); 2222 } 2223 2224 pub fn getFocus(self: *Header) *Focus { 2225 return self.box.getFocus(); 2226 } 2227 2228 pub fn getSelectedIndex(self: Header) ?usize { 2229 return self.currentTabIndex(); 2230 } 2231 2232 fn currentTabIndex(self: Header) ?usize { 2233 const child_id = self.box.focus.child_id orelse return null; 2234 return self.tab_ids.getIndex(child_id); 2235 } 2236 }; 2237