╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Issues.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
774 errdefer box.deinit(allocator); 775 776 const win = if (status_maybe) |status| data.window(status) else &data.conflicts; 777 778 // the issue list (one focusable row per title), plus a "next" link that 779 // reloads the page rooted at the following issue. 780 { 781 var list_scroll = blk: { 782 var list_box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert, .stretch = true }); 783 errdefer list_box.deinit(allocator); 784 if (win.prev_id) |prev| 785 try addRow(allocator, &list_box, "← previous", "", try windowLink(session.page_arena, data, status_maybe, prev)); 786 for (win.issues) |entry| { 787 // conflicts take precedence over the comment count 788 const label: []const u8 = if (entry.conflicted and status_maybe != null) 789 " conflict " 790 else if (entry.comments.count > 0) 791 try std.fmt.allocPrint(session.page_arena.allocator(), " {d} comments ", .{entry.comments.count}) 792 else 793 ""; 794 try addRow(allocator, &list_box, entry.issue.event.title, label, try issueRowLink(session.page_arena, data.identity, entry.id)); 795 } 796 if (win.next_id) |next| 797 try addRow(allocator, &list_box, "next →", "", try windowLink(session.page_arena, data, status_maybe, next)); 798 // select the window's first issue (past a leading "previous" 799 // row) so its description shows on load. 800 if (win.issues.len > 0) 801 list_box.getFocus().child_id = list_box.children.keys()[if (win.prev_id != null) 1 else 0] 802 else if (list_box.children.count() > 0) 803 list_box.getFocus().child_id = list_box.children.keys()[0]; 804 break :blk try wgt.Scroll(ui.Widget).init(allocator, .{ .box = list_box }, .{ .direction = .vert, .web_native = !session.is_terminal }); 805 }; 806 errdefer list_scroll.deinit(allocator); 807 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 } }); 808 } 809 810 // the detail pane — a frame around a scroll of the description 811 { 812 var detail_outer = blk: { 813 var detail_scroll = blk2: { 814 var detail_inner = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 815 errdefer detail_inner.deinit(allocator); 816 // fill the pane (content top-left, scroll bar pinned to the 817 // edge) rather than shrinking to the description. 818 break :blk2 try wgt.Scroll(ui.Widget).init(allocator, .{ .box = detail_inner }, .{ .direction = .vert, .web_native = !session.is_terminal, .fill = true }); 819 }; 820 errdefer detail_scroll.deinit(allocator); 821 var frame = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = .hidden, .direction = .vert }); 822 errdefer frame.deinit(allocator); 823 // the tool row sits above the scroll (populateDetail fills 824 // it per issue) so it can't scroll out from under the web 825 // overlay <form>, whose position doesn't track pane scrolling. 826 { 827 var row = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 828 errdefer row.deinit(allocator); 829 try frame.children.put(allocator, row.getFocus().id, .{ .widget = .{ .box = row }, .rect = null, .min_size = null }); 830 } 831 // the frame's selected child is its scroll, so the focus chain 832 // reaches the description (populateDetail points the scroll's 833 // inner box at it), letting focus recovery descend into the pane. 834 frame.getFocus().child_id = detail_scroll.getFocus().id; 835 try frame.children.put(allocator, detail_scroll.getFocus().id, .{ .widget = .{ .scroll = detail_scroll }, .rect = null, .min_size = null }); 836 break :blk frame; 837 }; 838 errdefer detail_outer.deinit(allocator); 839 try box.children.put(allocator, detail_outer.getFocus().id, .{ .widget = .{ .box = detail_outer }, .rect = null, .min_size = .{ .width = detail_min_width, .height = null } }); 840 } 841 842 box.getFocus().child_id = box.children.keys()[list_index]; 843 return box; 844 } 845 846 // an issue form: title/tags/description inputs and a submit button, 847 // prefilled from `issue` when given. its form: subtree makes the web 848 // overlay wrap them in a <form> POSTing to `action`'s route. 849 fn initIssueForm(allocator: std.mem.Allocator, session: *ui.Session, action: []const u8, issue: ?*const evt.Issue.Record) !wgt.Box(ui.Widget) { 850 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 851 errdefer box.deinit(allocator); 852 box.getFocus().kind = .{ .custom = action }; 853 854 { 855 var title = try wgt.TextInput(ui.Widget).init(allocator, .{ .label = " title ", .name = "title", .visible_width = null, .rounded_corners = true, .render_content = session.is_terminal }); 856 errdefer title.deinit(allocator); 857 title.getFocus().focusable = true; 858 if (issue) |i| try title.setContent(allocator, i.event.title); 859 try box.children.put(allocator, title.getFocus().id, .{ .widget = .{ .text_input = title }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 860 } 861 862 { 863 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 }); 864 errdefer tags.deinit(allocator); 865 tags.getFocus().focusable = true; 866 if (issue) |i| try tags.setContent(allocator, i.event.tags); 867 try box.children.put(allocator, tags.getFocus().id, .{ .widget = .{ .text_input = tags }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 868 } 869 870 { 871 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 } }); 872 errdefer description.deinit(allocator); 873 description.getFocus().focusable = true; 874 if (issue) |i| try description.setContent(allocator, i.event.description); 875 try box.children.put(allocator, description.getFocus().id, .{ .widget = .{ .text_input = description }, .rect = null, .min_size = null }); 876 } 877 878 try addSubmitButton(allocator, &box); 879 880 box.getFocus().child_id = box.children.keys()[title_field_index]; 881 return box; 882 } 883 884 fn initCommentForm(allocator: std.mem.Allocator, session: *ui.Session, action: []const u8, author: ui.Author, parent_route: ui.RoutablePage, initial_body: ?[]const u8) !wgt.Box(ui.Widget) { 885 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 886 errdefer box.deinit(allocator); 887 box.getFocus().kind = .{ .custom = action }; 888 889 { 890 var parent = try ui.authorBox(allocator, session.page_arena, author); 891 errdefer parent.deinit(allocator); 892 parent.options.label = " replying to "; 893 parent.getFocus().kind = .{ .custom = try std.fmt.allocPrint(session.page_arena.allocator(), "a:{s}", .{try parent_route.toUrl(session.page_arena)}) }; 894 try box.children.put(allocator, parent.getFocus().id, .{ .widget = .{ .text_box = parent }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 895 } 896 897 { 898 var body = try wgt.TextInput(ui.Widget).init(allocator, .{ 899 .label = " comment ", 900 .name = "body", 901 .visible_width = null, 902 .rounded_corners = true, 903 .render_content = session.is_terminal, 904 .multiline = true, 905 .visible_height = 5, 906 .scroll = .{ .fill = true }, 907 }); 908 errdefer body.deinit(allocator); 909 body.getFocus().focusable = true; 910 if (initial_body) |text| try body.setContent(allocator, text); 911 try box.children.put(allocator, body.getFocus().id, .{ .widget = .{ .text_input = body }, .rect = null, .min_size = null }); 912 } 913 914 try addSubmitButtonLabeled(allocator, &box, "submit"); 915 box.getFocus().child_id = box.children.keys()[comment_author_field_index]; 916 return box; 917 } 918 919 // a form's submit button, then a spacer absorbing the leftover 920 // min-height the box hands its last child, so the button keeps its 921 // natural height 922 fn addSubmitButton(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget)) !void { 923 return addSubmitButtonLabeled(allocator, box, "submit"); 924 } 925 926 fn addSubmitButtonLabeled(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), label: []const u8) !void { 927 { 928 var submit = try ui.SubmitButton.initLabeled(allocator, label); 929 errdefer submit.deinit(allocator); 930 try box.children.put(allocator, submit.getFocus().id, .{ .widget = .{ .submit_button = submit }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 931 } 932 { 933 var spacer = try ui.Spacer.init(allocator); 934 errdefer spacer.deinit(allocator); 935 try box.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 936 } 937 } 938 939 // the conflict resolve form. the "use this" links are navigations that 940 // flip the url's theirs: pick, reloading the prefills; one submit 941 // settles every conflict. the form: subtree makes the web overlay POST 942 // to the resolve route. 943 fn initResolveForm(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self) !wgt.Box(ui.Widget) { 944 const aa = session.page_arena.allocator(); 945 // init only picks the resolve view once it has read the conflict 946 const conflict = if (data.conflict) |*c| c else unreachable; 947 948 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 949 errdefer box.deinit(allocator); 950 box.getFocus().kind = .{ .custom = if (data.identity.len == 0) 951 try std.fmt.allocPrint(aa, "form:/issue:{s}/resolve", .{data.selected_id}) 952 else 953 try std.fmt.allocPrint(aa, "form:/repo/{s}/issue:{s}/resolve", .{ data.identity, data.selected_id }) }; 954 955 if (conflict.title) |*fc| { 956 try addLabel(allocator, &box, "title conflict:"); 957 try addFieldConflict(allocator, &box, session, data, "title", fc); 958 } 959 if (conflict.tags) |*fc| { 960 try addLabel(allocator, &box, "tags conflict:"); 961 try addFieldConflict(allocator, &box, session, data, "tags", fc); 962 } 963 964 if (conflict.description) |*desc| { 965 try addLabel(allocator, &box, "description conflict:"); 966 var hunk_index: usize = 0; 967 for (desc.chunks, 0..) |*chunk, chunk_index| { 968 switch (chunk.*) { 969 .same => |text| { 970 var tb = try wgt.TextBox(ui.Widget).init(allocator, text, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .word }); 971 errdefer tb.deinit(allocator); 972 tb.getFocus().focusable = true; 973 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 974 }, 975 // an auto-resolved chunk: only one side changed it, so its 976 // version stands, shown distinctly with its author 977 .auto => |auto| { 978 const author = if (auto.theirs) desc.theirs_author else desc.ours_author; 979 const verb: []const u8 = if (auto.text == null) "removed" else "edited"; 980 const label = switch (author) { 981 .user_name, .email => |name| try std.fmt.allocPrint(aa, " {s} by {s} ", .{ verb, name }), 982 .unknown => try std.fmt.allocPrint(aa, " {s} by {s} ", .{ verb, if (auto.theirs) @as([]const u8, "them") else "us" }), 983 }; 984 var tb = try wgt.TextBox(ui.Widget).init(allocator, auto.text orelse "(removed)", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .word, .label = label }); 985 errdefer tb.deinit(allocator); 986 tb.getFocus().focusable = true; 987 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 988 }, 989 .conflict => |hunk| { 990 // a blank row on each side sets the group apart from 991 // the flowing shared and auto-resolved chunks 992 if (chunk_index > 0) try addGap(allocator, &box); 993 const name = try std.fmt.allocPrint(aa, "d{d}", .{hunk_index}); 994 hunk_index += 1; 995 const picked_theirs = data.theirsPicked(name); 996 try addVersionRow(allocator, &box, try sideLabel(aa, desc.ours_author, true), hunk.ours orelse "", try useThisLink(session, data, name, false)); 997 try addVersionRow(allocator, &box, try sideLabel(aa, desc.theirs_author, false), hunk.theirs orelse "", try useThisLink(session, data, name, true)); 998 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 }); 999 errdefer resolution_input.deinit(allocator); 1000 resolution_input.getFocus().focusable = true; 1001 try resolution_input.setContent(allocator, (if (picked_theirs) hunk.theirs else hunk.ours) orelse ""); 1002 try box.children.put(allocator, resolution_input.getFocus().id, .{ .widget = .{ .text_input = resolution_input }, .rect = null, .min_size = null }); 1003 if (chunk_index + 1 < desc.chunks.len) try addGap(allocator, &box); 1004 }, 1005 } 1006 } 1007 } 1008 1009 try addSubmitButton(allocator, &box); 1010 1011 box.getFocus().child_id = box.children.keys()[0]; 1012 return box; 1013 } 1014 1015 // one conflicted scalar field: both sides' version rows, then the 1016 // resolution input prefilled from the picked side. 1017 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 { 1018 const aa = session.page_arena.allocator(); 1019 try addVersionRow(allocator, box, try sideLabel(aa, fc.ours.author, true), fc.ours.text, try useThisLink(session, data, name, false)); 1020 try addVersionRow(allocator, box, try sideLabel(aa, fc.theirs.author, false), fc.theirs.text, try useThisLink(session, data, name, true)); 1021 1022 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 }); 1023 errdefer resolution_input.deinit(allocator); 1024 resolution_input.getFocus().focusable = true; 1025 try resolution_input.setContent(allocator, if (data.theirsPicked(name)) fc.theirs.text else fc.ours.text); 1026 try box.children.put(allocator, resolution_input.getFocus().id, .{ .widget = .{ .text_input = resolution_input }, .rect = null, .min_size = null }); 1027 } 1028 1029 // a blank row setting a conflict group apart from its neighbors 1030 fn addGap(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget)) !void { 1031 var text = try wgt.Text(ui.Widget).init(allocator, " "); 1032 errdefer text.deinit(allocator); 1033 try box.children.put(allocator, text.getFocus().id, .{ .widget = .{ .text = text }, .rect = null, .min_size = null }); 1034 } 1035 1036 fn addLabel(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), content: []const u8) !void { 1037 var label = try ui.SectionLabel.init(allocator, content); 1038 errdefer label.deinit(allocator); 1039 try box.children.put(allocator, label.getFocus().id, .{ .widget = .{ .section_label = label }, .rect = null, .min_size = null }); 1040 } 1041 1042 // one side of a conflict: the "use this" link on the left, the version 1043 // itself as a labeled read-only box. 1044 fn addVersionRow(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), label: []const u8, text: []const u8, link: []const u8) !void { 1045 var row = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 1046 errdefer row.deinit(allocator); 1047 1048 { 1049 const use_label = "use this"; 1050 var use = try wgt.TextBox(ui.Widget).init(allocator, use_label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1051 errdefer use.deinit(allocator); 1052 use.getFocus().focusable = true; 1053 use.getFocus().kind = .{ .custom = link }; 1054 try row.children.put(allocator, use.getFocus().id, .{ 1055 .widget = .{ .text_box = use }, 1056 .rect = null, 1057 .min_size = .{ .width = use_label.len + 2, .height = null }, 1058 .max_size = .{ .width = use_label.len + 2, .height = null }, 1059 }); 1060 } 1061 1062 { 1063 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 }); 1064 errdefer tb.deinit(allocator); 1065 tb.getFocus().focusable = true; 1066 try row.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1067 } 1068 1069 row.getFocus().child_id = row.children.keys()[0]; 1070 try box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .box = row }, .rect = null, .min_size = null }); 1071 } 1072 1073 // the label of one side's version box, naming its author 1074 fn sideLabel(aa: std.mem.Allocator, author: ui.Author, ours: bool) ![]const u8 { 1075 return switch (author) { 1076 .user_name, .email => |name| try std.fmt.allocPrint(aa, " edited by {s} ", .{name}), 1077 .unknown => if (ours) " current version " else " their version ", 1078 }; 1079 } 1080 1081 // the a: link that reloads the resolve page with `name` picked from ours 1082 // or theirs, reprefilling its resolution input 1083 fn useThisLink(session: *ui.Session, data: *const Self, name: []const u8, theirs: bool) ![]const u8 { 1084 const aa = session.page_arena.allocator(); 1085 var picks: std.ArrayList(u8) = .empty; 1086 var it = std.mem.splitScalar(u8, data.theirs_picks, ','); 1087 while (it.next()) |pick| { 1088 if (pick.len == 0 or std.mem.eql(u8, pick, name)) continue; 1089 if (picks.items.len > 0) try picks.append(aa, ','); 1090 try picks.appendSlice(aa, pick); 1091 } 1092 if (theirs) { 1093 if (picks.items.len > 0) try picks.append(aa, ','); 1094 try picks.appendSlice(aa, name); 1095 } 1096 const route = ui.RoutablePage.repoIssuesResolveRoute(data.identity, data.selected_id, picks.items) orelse return error.RouteTooLong; 1097 return std.fmt.allocPrint(aa, "a:{s}", .{try route.toUrl(session.page_arena)}); 1098 } 1099 1100 // the resolve form's page-constant initial value for the input `name`: 1101 // the picked side of its conflicted field or hunk 1102 fn resolvePrefill(self: *View, name: []const u8) ?[]const u8 { 1103 const conflict = if (self.data.conflict) |*c| c else return null; 1104 const theirs = self.data.theirsPicked(name); 1105 if (std.mem.eql(u8, name, "title")) { 1106 const fc = if (conflict.title) |*f| f else return null; 1107 return if (theirs) fc.theirs.text else fc.ours.text; 1108 } 1109 if (std.mem.eql(u8, name, "tags")) { 1110 const fc = if (conflict.tags) |*f| f else return null; 1111 return if (theirs) fc.theirs.text else fc.ours.text; 1112 } 1113 if (std.mem.startsWith(u8, name, "d")) { 1114 const desc = if (conflict.description) |*d| d else return null; 1115 const hunk_index = std.fmt.parseInt(usize, name[1..], 10) catch return null; 1116 var seen: usize = 0; 1117 for (desc.chunks) |chunk| switch (chunk) { 1118 .conflict => |hunk| { 1119 if (seen == hunk_index) return (if (theirs) hunk.theirs else hunk.ours) orelse ""; 1120 seen += 1; 1121 }, 1122 else => {}, 1123 }; 1124 } 1125 return null; 1126 } 1127 1128 fn addRow(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), text: []const u8, bottom_label: []const u8, link: []const u8) !void { 1129 var row = try wgt.TextBox(ui.Widget).init(allocator, text, .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .word, .bottom_label = bottom_label }); 1130 errdefer row.deinit(allocator); 1131 row.getFocus().focusable = true; 1132 if (link.len != 0) row.getFocus().kind = .{ .custom = link }; 1133 try box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .text_box = row }, .rect = null, .min_size = null, .max_size = .{ .width = null, .height = 5 } }); 1134 } 1135 1136 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 1137 self.box.deinit(allocator); 1138 } 1139 1140 fn header(self: *View) *Header { 1141 return &self.box.children.values()[header_index].widget.repo_issues_header; 1142 } 1143 1144 fn viewStack(self: *View) *wgt.Stack(ui.Widget) { 1145 return &self.box.children.values()[stack_index].widget.stack; 1146 } 1147 1148 // `index`'s master-detail split inside the stack. 1149 fn resultsBox(self: *View, index: usize) *wgt.Box(ui.Widget) { 1150 return &self.viewStack().children.values()[index].box; 1151 } 1152 1153 // the tags view's flow inside the stack. 1154 fn tagsView(self: *View) *ui.TagFlow { 1155 return &self.viewStack().children.values()[tags_view_index].tag_flow; 1156 } 1157 1158 // the new-issue, edit, or resolve form inside the stack, or null when the 1159 // unauthorized view stands in for it. 1160 fn issueForm(self: *View) ?*wgt.Box(ui.Widget) { 1161 return switch (self.viewStack().children.values()[form_view_index]) { 1162 .box => |*box| box, 1163 // the resolve form sits inside a scroll on the terminal 1164 .scroll => |*scroll| &scroll.child.box, 1165 else => null, 1166 }; 1167 } 1168 1169 // the resolve form's scroll on the terminal (the web page scrolls itself) 1170 fn resolveScroll(self: *View) ?*wgt.Scroll(ui.Widget) { 1171 return switch (self.viewStack().children.values()[form_view_index]) { 1172 .scroll => |*scroll| scroll, 1173 else => null, 1174 }; 1175 } 1176 1177 fn listScroll(self: *View, index: usize) *wgt.Scroll(ui.Widget) { 1178 return &self.resultsBox(index).children.values()[list_index].widget.scroll; 1179 } 1180 1181 fn listBox(self: *View, index: usize) *wgt.Box(ui.Widget) { 1182 return &self.listScroll(index).child.box; 1183 } 1184 1185 // the detail frame's children: the tool row above the scroll pane. 1186 const tool_row_index: usize = 0; 1187 const detail_scroll_index: usize = 1; 1188 1189 fn detailOuter(self: *View, index: usize) *wgt.Box(ui.Widget) { 1190 return &self.resultsBox(index).children.values()[detail_index].widget.box; 1191 } 1192 1193 fn toolRow(self: *View, index: usize) *wgt.Box(ui.Widget) { 1194 return &self.detailOuter(index).children.values()[tool_row_index].widget.box; 1195 } 1196 1197 fn detailScroll(self: *View, index: usize) *wgt.Scroll(ui.Widget) { 1198 return &self.detailOuter(index).children.values()[detail_scroll_index].widget.scroll; 1199 } 1200 1201 fn detailInner(self: *View, index: usize) *wgt.Box(ui.Widget) { 1202 return &self.detailScroll(index).child.box; 1203 } 1204 1205 fn window(self: *View, index: usize) *const Window { 1206 if (index == conflicts_view_index) return &self.data.conflicts; 1207 return self.data.window(splitStatus(index)); 1208 } 1209 1210 fn stackSelectedIndex(self: *View) ?usize { 1211 const stack = self.viewStack(); 1212 const cid = stack.getFocus().child_id orelse return null; 1213 return stack.children.getIndex(cid); 1214 } 1215 1216 // the stack's selected master-detail split (null when the tags view or the 1217 // new-issue form shows). 1218 fn selectedSplitIndex(self: *View) ?usize { 1219 const idx = self.stackSelectedIndex() orelse return null; 1220 return if (idx < split_count or idx == conflicts_view_index) idx else null; 1221 } 1222 1223 fn detailActive(self: *View, index: usize) bool { 1224 const rb = self.resultsBox(index); 1225 const cid = rb.getFocus().child_id orelse return false; 1226 return rb.children.getIndex(cid) == detail_index; 1227 } 1228 1229 fn headerActive(self: *View) bool { 1230 const cid = self.box.getFocus().child_id orelse return false; 1231 return self.box.children.getIndex(cid) == header_index; 1232 } 1233 1234 fn tagsViewActive(self: *View) bool { 1235 if (self.headerActive()) return false; 1236 return self.stackSelectedIndex() == tags_view_index; 1237 } 1238 1239 fn formViewActive(self: *View) bool { 1240 if (self.headerActive()) return false; 1241 return self.stackSelectedIndex() == form_view_index; 1242 } 1243 1244 // the selected issue's index, or null when a window-navigation row is 1245 // selected (a leading "previous" row shifts the issue rows down by one). 1246 fn selectedIssueIndex(self: *View, index: usize) ?usize { 1247 const lb = self.listBox(index); 1248 const cid = lb.getFocus().child_id orelse return null; 1249 const idx = lb.children.getIndex(cid) orelse return null; 1250 const win = self.window(index); 1251 const lead: usize = if (win.prev_id != null) 1 else 0; 1252 if (idx < lead or idx - lead >= win.issues.len) return null; 1253 return idx - lead; 1254 } 1255 1256 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1257 self.clearGrid(); 1258 1259 // each header tab maps 1:1 to a stack child by position. mirror the 1260 // selection into the url: the view the page's window is rooted at 1261 // keeps its rooted url, the others get their list route (keeping the 1262 // tag filter). 1263 if (self.header().getSelectedIndex()) |index| { 1264 const stack = self.viewStack(); 1265 stack.getFocus().child_id = stack.children.keys()[index]; 1266 self.session.data.current_page = (if (index == form_view_index) 1267 (switch (self.data.view) { 1268 .edit => ui.RoutablePage.repoIssuesEditRoute(self.data.identity, self.data.selected_id), 1269 .new_comment => ui.RoutablePage.repoCommentNewRoute(self.data.identity, self.data.selected_id, self.data.comment_id), 1270 .edit_comment => ui.RoutablePage.repoCommentEditRoute(self.data.identity, self.data.selected_id, self.data.comment_id), 1271 .resolve => ui.RoutablePage.repoIssuesResolveRoute(self.data.identity, self.data.selected_id, self.data.theirs_picks), 1272 else => ui.RoutablePage.repoIssuesNewRoute(self.data.identity), 1273 }) 1274 else if (index == conflicts_view_index) 1275 ui.RoutablePage.repoIssuesConflictsRoute(self.data.identity, if (self.data.view == .conflicts) self.data.selected_id else "") 1276 else if (index == viewIndex(self.data.view) and self.data.selected_id.len != 0) 1277 (if (self.data.description_page) 1278 ui.RoutablePage.repoIssuesDescriptionRoute(self.data.identity, self.data.selected_id) 1279 else if (self.data.comment_page) |page| 1280 ui.RoutablePage.repoCommentsRoute(self.data.identity, self.data.selected_id, &page.selected.id, page.replies.start) 1281 else 1282 ui.RoutablePage.repoIssueCommentsRoute(self.data.identity, self.data.selected_id, self.data.comments_start)) 1283 else if (index == tags_view_index) 1284 ui.RoutablePage.repoIssuesTagsRoute(self.data.identity, self.data.tag) 1285 else 1286 ui.RoutablePage.repoIssuesRoute(self.data.identity, splitStatus(index), self.data.tag, "")) orelse self.session.data.current_page; 1287 } 1288 1289 if (self.selectedSplitIndex()) |i| { 1290 // swap the detail pane to the selected issue when it changes. 1291 try self.refreshDetail(allocator, i); 1292 1293 // mirror the focused issue into the url, but only while focus is 1294 // inside the split. an issue's url is the same whether or not the 1295 // list is filtered, so the mirror drops the tag. 1296 if (root_focus.grandchild_id) |g| { 1297 if (self.resultsBox(i).getFocus().children.contains(g)) { 1298 if (self.selectedIssueIndex(i)) |sel| { 1299 // selecting another issue leaves the description page 1300 // behind, like it leaves the tag filter behind. 1301 const entry = &self.window(i).issues[sel]; 1302 const mirrored = if (self.data.description_page and std.mem.eql(u8, entry.id, self.data.selected_id)) 1303 ui.RoutablePage.repoIssuesDescriptionRoute(self.data.identity, entry.id) 1304 else if (self.data.comment_page) |page| 1305 if (std.mem.eql(u8, entry.id, self.data.selected_id)) 1306 ui.RoutablePage.repoCommentsRoute(self.data.identity, entry.id, &page.selected.id, page.replies.start) 1307 else 1308 ui.RoutablePage.repoIssueCommentsRoute(self.data.identity, entry.id, 0) 1309 else if (std.mem.eql(u8, entry.id, self.data.selected_id)) 1310 ui.RoutablePage.repoIssueCommentsRoute(self.data.identity, entry.id, entry.comments.start) 1311 else 1312 ui.RoutablePage.repoIssueCommentsRoute(self.data.identity, entry.id, 0); 1313 if (mirrored) |route| self.session.data.current_page = route; 1314 } 1315 } 1316 } 1317 1318 // the selected list row shows a border (the focused TextBox 1319 // upgrades it to a double border itself); the rest stay borderless. 1320 const lb = self.listBox(i); 1321 for (lb.children.keys(), lb.children.values()) |id, *child| { 1322 switch (child.widget) { 1323 .text_box => |*tb| tb.options.border_style = if (lb.getFocus().child_id == id) .single else .hidden, 1324 else => {}, 1325 } 1326 } 1327 1328 // the pane's selected child shows the selection border: the 1329 // description directly, the tags via the flow's selected item. 1330 // cap the list at list_max_width only while the detail pane fits 1331 // beside it. the box drops the detail when the width can't hold 1332 // both minimums, so when it's that narrow we lift the cap and let 1333 // the list fill the whole width. 1334 const both_panes_fit = if (constraint.max_size.width) |w| w >= list_max_width + detail_min_width else true; 1335 self.resultsBox(i).children.values()[list_index].max_size = if (both_panes_fit) .{ .width = list_max_width, .height = null } else null; 1336 1337 // stretch the detail pane across the rest of the width so it fills 1338 // the area rather than shrinking to its content; its scroll fills 1339 // the pane. 1340 if (constraint.max_size.width) |w| { 1341 self.resultsBox(i).children.values()[detail_index].min_size = .{ .width = if (both_panes_fit) w - list_max_width else w, .height = null }; 1342 } else { 1343 self.resultsBox(i).children.values()[detail_index].min_size = .{ .width = detail_min_width, .height = null }; 1344 } 1345 } 1346 1347 // refresh the form inputs' entries in the session's focus-id -> input 1348 // map with this frame's addresses, so the web/wasm form handling can 1349 // find them by focus id 1350 if (self.issueForm()) |form| { 1351 const inputs_arena = self.session.arena.allocator(); 1352 for (form.children.values()) |*child| switch (child.widget) { 1353 .text_input => |*ti| try self.session.text_inputs.put(inputs_arena, ti.getFocus().id, ti), 1354 else => {}, 1355 }; 1356 1357 // on an edit page, register the issue's content as the inputs' 1358 // page-constant initial values, which the web overlay renders 1359 // into them. 1360 if (self.data.view == .edit) { 1361 if (self.data.selectedIssue()) |entry| { 1362 const fields = form.children.values(); 1363 try self.session.input_values.put(inputs_arena, fields[title_field_index].widget.text_input.getFocus().id, entry.issue.event.title); 1364 try self.session.input_values.put(inputs_arena, fields[tags_field_index].widget.text_input.getFocus().id, entry.issue.event.tags); 1365 try self.session.input_values.put(inputs_arena, fields[description_field_index].widget.text_input.getFocus().id, entry.issue.event.description); 1366 } 1367 } 1368 1369 if (self.data.view == .edit_comment) { 1370 if (self.data.comment_page) |page| { 1371 const body = &form.children.values()[comment_body_field_index].widget.text_input; 1372 try self.session.input_values.put(inputs_arena, body.getFocus().id, page.selected.comment.event.body); 1373 } 1374 } 1375 1376 // the resolve form's inputs prefill from the picked side, which 1377 // is url state and so page-constant like the edit values above 1378 if (self.data.view == .resolve) { 1379 for (form.children.values()) |*child| switch (child.widget) { 1380 .text_input => |*ti| if (self.resolvePrefill(ti.options.name)) |value| { 1381 try self.session.input_values.put(inputs_arena, ti.getFocus().id, value); 1382 }, 1383 else => {}, 1384 }; 1385 } 1386 } 1387 1388 try self.box.build(allocator, constraint, root_focus); 1389 } 1390 1391 fn refreshDetail(self: *View, allocator: std.mem.Allocator, index: usize) !void { 1392 const sel = self.selectedIssueIndex(index) orelse return; 1393 if (self.detailed_index[index]) |d| if (d == sel) return; 1394 try self.populateDetail(allocator, index, sel); 1395 self.detailed_index[index] = sel; 1396 } 1397 1398 fn populateDetail(self: *View, allocator: std.mem.Allocator, index: usize, sel: usize) !void { 1399 const entry = self.window(index).issues[sel]; 1400 const inner = self.detailInner(index); 1401 // the /description page replaces its issue's detail with a back link 1402 // and the whole description; other issues keep their normal detail. 1403 const description_page = self.data.description_page and std.mem.eql(u8, entry.id, self.data.selected_id); 1404 const comment_page = if (std.mem.eql(u8, entry.id, self.data.selected_id)) self.data.comment_page else null; 1405 1406 for (inner.children.values()) |*child| child.widget.deinit(allocator); 1407 inner.children.clearAndFree(allocator); 1408 inner.getFocus().child_id = null; 1409 self.author_id[index] = null; 1410 self.status_button_id[index] = null; 1411 1412 // the tool row: the open/close and edit buttons; the description page 1413 // shows none. 1414 { 1415 const row = self.toolRow(index); 1416 for (row.children.values()) |*child| child.widget.deinit(allocator); 1417 row.children.clearAndFree(allocator); 1418 row.getFocus().child_id = null; 1419 row.getFocus().kind = .container; 1420 } 1421 if (!description_page and comment_page == null) { 1422 const row = self.toolRow(index); 1423 const pa = self.session.page_arena.allocator(); 1424 1425 { 1426 var spacer = try ui.Spacer.init(allocator); 1427 errdefer spacer.deinit(allocator); 1428 try row.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 1429 } 1430 1431 // a terminal has no file picker, so attaching is web only. the 1432 // renderer covers this button with a file input, so clicking it 1433 // opens the browser's own picker and posts what it chose. 1434 if (!self.session.is_terminal and !entry.conflicted and (self.session.data.is_local or self.session.data.user_id != null)) { 1435 const label = "add attachment"; 1436 var button = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1437 errdefer button.deinit(allocator); 1438 button.getFocus().focusable = true; 1439 button.getFocus().kind = .{ .custom = if (self.data.identity.len == 0) 1440 try std.fmt.allocPrint(pa, "{s}/issue:{s}/attach", .{ ui.file_input_prefix, entry.id }) 1441 else 1442 try std.fmt.allocPrint(pa, "{s}/repo/{s}/issue:{s}/attach", .{ ui.file_input_prefix, self.data.identity, entry.id }) }; 1443 try row.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = .{ .width = label.len + 2, .height = null } }); 1444 } 1445 1446 // a conflicted issue's only action is resolving it. the button 1447 // stays visible logged out; the resolve page shows the 1448 // unauthorized view then. 1449 if (entry.conflicted) { 1450 const label = "resolve conflict"; 1451 var button = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1452 errdefer button.deinit(allocator); 1453 button.getFocus().focusable = true; 1454 const route = ui.RoutablePage.repoIssuesResolveRoute(self.data.identity, entry.id, "") orelse return error.RouteTooLong; 1455 button.getFocus().kind = .{ .custom = try std.fmt.allocPrint(pa, "a:{s}", .{try route.toUrl(self.session.page_arena)}) }; 1456 try row.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = .{ .width = label.len + 2, .height = null } }); 1457 } else { 1458 // a logged-out session can't flip the status, so it gets no 1459 // open/close button 1460 if (self.session.data.is_local or self.session.data.user_id != null) { 1461 const action: []const u8 = switch (entry.issue.event.status) { 1462 .open => "close", 1463 .closed => "open", 1464 }; 1465 // the renderer distinguishes plain clickables from buttons 1466 // that should POST to a server route by the submit kind. 1467 row.getFocus().kind = .{ .custom = if (self.data.identity.len == 0) 1468 try std.fmt.allocPrint(pa, "form:/issue:{s}/{s}", .{ entry.id, action }) 1469 else 1470 try std.fmt.allocPrint(pa, "form:/repo/{s}/issue:{s}/{s}", .{ self.data.identity, entry.id, action }) }; 1471 1472 var button = try wgt.TextBox(ui.Widget).init(allocator, action, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1473 errdefer button.deinit(allocator); 1474 button.getFocus().focusable = true; 1475 // the renderer distinguishes plain clickables from buttons 1476 // that should POST to a server route by this kind. 1477 button.getFocus().kind = .{ .custom = "submit" }; 1478 self.status_button_id[index] = button.getFocus().id; 1479 try row.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = .{ .width = action.len + 2, .height = null } }); 1480 } 1481 1482 // the edit button links to the issue's edit page. 1483 { 1484 var button = try wgt.TextBox(ui.Widget).init(allocator, "edit", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1485 errdefer button.deinit(allocator); 1486 button.getFocus().focusable = true; 1487 const route = ui.RoutablePage.repoIssuesEditRoute(self.data.identity, entry.id) orelse return error.RouteTooLong; 1488 button.getFocus().kind = .{ .custom = try std.fmt.allocPrint(pa, "a:{s}", .{try route.toUrl(self.session.page_arena)}) }; 1489 try row.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = .{ .width = "edit".len + 2, .height = null } }); 1490 } 1491 } 1492 1493 // the leftmost button: the attachment button when the session has 1494 // one, else the resolve button on a conflicted issue, the 1495 // open/close button when present, the edit button otherwise 1496 row.getFocus().child_id = row.children.keys()[first_in_row_index]; 1497 } 1498 1499 if (comment_page) |page| { 1500 var back = try Comment.linkBox(allocator, self.session, "← back to issue", ui.RoutablePage.repoIssueCommentsRoute(self.data.identity, entry.id, 0) orelse return error.RouteTooLong); 1501 errdefer back.deinit(allocator); 1502 try inner.children.put(allocator, back.getFocus().id, .{ .widget = .{ .text_box = back }, .rect = null, .min_size = null }); 1503 1504 try Comment.appendComment(allocator, inner, self.session, self.data.identity, page.selected); 1505 try Comment.appendCount(allocator, inner, page.replies.count, "reply", "replies", self.session.page_arena.allocator()); 1506 for (page.replies.comments) |comment| try Comment.appendComment(allocator, inner, self.session, self.data.identity, comment); 1507 try Comment.appendWindowNav(allocator, inner, self.session, self.data.identity, &page.selected.comment.event.thread_id, &page.selected.id, page.replies); 1508 1509 var spacer = try ui.Spacer.init(allocator); 1510 errdefer spacer.deinit(allocator); 1511 try inner.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 1512 1513 inner.getFocus().child_id = inner.children.keys()[0]; 1514 const sc = self.detailScroll(index); 1515 sc.x = 0; 1516 sc.y = 0; 1517 sc.getFocus().version +%= 1; 1518 return; 1519 } 1520 1521 if (description_page) { 1522 // the back link, in the title slot so the pane's input handling 1523 // applies to it unchanged. 1524 var tb = try wgt.TextBox(ui.Widget).init(allocator, "← back to issue", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1525 errdefer tb.deinit(allocator); 1526 tb.getFocus().focusable = true; 1527 tb.getFocus().kind = .{ .custom = try issuesLink(self.session.page_arena, self.data.identity, entry.issue.event.status, "", entry.id) }; 1528 try inner.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1529 } else { 1530 // the issue's title as a focusable word-wrapped text box. 1531 { 1532 var tb = try wgt.TextBox(ui.Widget).init(allocator, entry.issue.event.title, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .word, .label = " title " }); 1533 errdefer tb.deinit(allocator); 1534 tb.getFocus().focusable = true; 1535 try inner.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1536 } 1537 1538 self.author_id[index] = blk: { 1539 var tb = try ui.authorBox(allocator, self.session.page_arena, entry.author); 1540 errdefer tb.deinit(allocator); 1541 try inner.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1542 break :blk tb.getFocus().id; 1543 }; 1544 1545 // the issue's tags, each linking to this status's list filtered to 1546 // that tag. 1547 { 1548 var items: std.ArrayList(ui.TagFlow.Item) = .empty; 1549 defer items.deinit(allocator); 1550 var tag_iter = evt.Issue.tagIterator(entry.issue.event.tags); 1551 while (tag_iter.next()) |tag| { 1552 if (tag.len == 0) continue; 1553 try items.append(allocator, .{ .text = tag, .link = try tagLink(self.session.page_arena, self.data.identity, entry.issue.event.status, tag) }); 1554 } 1555 if (items.items.len > 0) { 1556 var tf = try ui.TagFlow.init(allocator); 1557 errdefer tf.deinit(allocator); 1558 try tf.setItems(allocator, items.items); 1559 try inner.children.put(allocator, tf.getFocus().id, .{ .widget = .{ .tag_flow = tf }, .rect = null, .min_size = null }); 1560 } 1561 } 1562 } 1563 1564 // the description as a focusable word-wrapped text box. past the limit 1565 // it's cut at the last whole line and links to the /description page, 1566 // which shows the whole thing. 1567 self.description_id[index] = blk: { 1568 const whole = entry.issue.event.description; 1569 const cut_short = !description_page and whole.len > max_description_size; 1570 const shown = if (cut_short) 1571 std.mem.trimEnd(u8, whole[0 .. std.mem.lastIndexOfScalar(u8, whole[0..max_description_size], '\n') orelse 0], " \t\r\n") 1572 else if (whole.len == 0) 1573 "(no description)" 1574 else 1575 whole; 1576 var tb = try wgt.TextBox(ui.Widget).init(allocator, shown, .{ 1577 .border_style = .single, 1578 .rounded_corners = true, 1579 .wrap_kind = .word, 1580 .label = " description ", 1581 .bottom_label = if (cut_short) " click or press enter to see more " else "", 1582 }); 1583 errdefer tb.deinit(allocator); 1584 tb.getFocus().focusable = true; 1585 if (cut_short) tb.getFocus().kind = .{ .custom = try descriptionLink(self.session.page_arena, self.data.identity, entry.id) }; 1586 try inner.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1587 break :blk tb.getFocus().id; 1588 }; 1589 1590 try Attachment.appendRows(allocator, inner, self.session, self.data.identity, entry.attachments); 1591 1592 if (!description_page) { 1593 var reply = try Comment.linkBox(allocator, self.session, "new comment", ui.RoutablePage.repoCommentNewRoute(self.data.identity, entry.id, "") orelse return error.RouteTooLong); 1594 errdefer reply.deinit(allocator); 1595 try inner.children.put(allocator, reply.getFocus().id, .{ .widget = .{ .text_box = reply }, .rect = null, .min_size = null }); 1596 1597 try Comment.appendCount(allocator, inner, entry.comments.count, "comment", "comments", self.session.page_arena.allocator()); 1598 for (entry.comments.comments) |comment| try Comment.appendComment(allocator, inner, self.session, self.data.identity, comment); 1599 try Comment.appendWindowNav(allocator, inner, self.session, self.data.identity, entry.id, null, entry.comments); 1600 1601 var spacer = try ui.Spacer.init(allocator); 1602 errdefer spacer.deinit(allocator); 1603 try inner.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 1604 } 1605 1606 // select the title by default 1607 inner.getFocus().child_id = inner.children.keys()[title_child_index]; 1608 1609 // reset the scroll to the top for the newly-shown issue: directly on the 1610 // terminal (the wasm offset), and via a version bump on the web (so the 1611 // renderer's scroll id changes and JS drops the preserved position). 1612 const sc = self.detailScroll(index); 1613 sc.x = 0; 1614 sc.y = 0; 1615 sc.getFocus().version +%= 1; 1616 } 1617 1618 pub fn input(self: *View, allocator: std.mem.Allocator, raw_key: Key, root_focus: *Focus) !void { 1619 // wheel input moves focus like the arrows 1620 const key: Key = if (raw_key == .mouse and raw_key.mouse.action == .scroll) 1621 (if (raw_key.mouse.action.scroll == .up) .arrow_up else .arrow_down) 1622 else 1623 raw_key; 1624 1625 if (self.headerActive()) { 1626 // down from the tabs re-enters the stack if the selected view has 1627 // something to focus; other keys move the tabs. 1628 if (inp.vertDirection(key) == .down) { 1629 const enterable = if (self.viewStack().getSelected()) |selected| switch (selected.*) { 1630 .tag_flow => |*tf| tf.text_boxes.items.len > 0, 1631 // nothing focusable in the unauthorized view 1632 .unauthorized => false, 1633 else => true, 1634 } else false; 1635 if (enterable) root_focus.setFocus(self.box.children.keys()[stack_index]); 1636 } else { 1637 try self.header().input(allocator, key, root_focus); 1638 } 1639 return; 1640 } 1641 if (self.tagsViewActive()) { 1642 self.tagsViewInput(key, root_focus); 1643 return; 1644 } 1645 if (self.formViewActive()) { 1646 if (self.data.view == .resolve) { 1647 try self.resolveInput(allocator, key, root_focus); 1648 } else if (self.data.view == .new_comment or self.data.view == .edit_comment) { 1649 try self.commentFormInput(allocator, key, root_focus); 1650 } else { 1651 try self.formInput(allocator, key, root_focus); 1652 } 1653 return; 1654 } 1655 const i = self.selectedSplitIndex() orelse return; 1656 if (self.detailActive(i)) { 1657 try self.detailInput(allocator, i, key, root_focus); 1658 } else { 1659 try self.listInput(i, key, root_focus); 1660 } 1661 } 1662 1663 // arrow keys move the tag selection; up from the top row crosses to the 1664 // header tabs. 1665 fn tagsViewInput(self: *View, key: Key, root_focus: *Focus) void { 1666 const tf = self.tagsView(); 1667 const cid = tf.focus.child_id orelse return; 1668 const cur = tf.indexOfFocusId(cid) orelse return; 1669 const count = tf.text_boxes.items.len; 1670 switch (key) { 1671 .arrow_left => if (cur > 0) root_focus.setFocus(tf.text_boxes.items[cur - 1].getFocus().id), 1672 .arrow_right => if (cur + 1 < count) root_focus.setFocus(tf.text_boxes.items[cur + 1].getFocus().id), 1673 .arrow_up => if (tf.rowStep(cur, false)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id) else self.focusHeader(root_focus), 1674 .arrow_down => if (tf.rowStep(cur, true)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id), 1675 .home => root_focus.setFocus(tf.text_boxes.items[0].getFocus().id), 1676 .end => root_focus.setFocus(tf.text_boxes.items[count - 1].getFocus().id), 1677 else => {}, 1678 } 1679 } 1680 1681 fn listInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 1682 // up/down (and the scroll wheel) move the selection a row; page up/down 1683 // jump a fixed amount. right/Enter cross into the detail pane. up from 1684 // the top row crosses into the header tabs. 1685 if (inp.rowDelta(key, @intCast(self.listBox(index).children.count()))) |delta| { 1686 const lb = self.listBox(index); 1687 const at_top = if (lb.getFocus().child_id) |cid| lb.children.getIndex(cid) == 0 else true; 1688 if (delta < 0 and at_top) return self.focusHeader(root_focus); 1689 ui.moveRowFocus(lb, self.listScroll(index), root_focus, delta); 1690 return; 1691 } 1692 switch (key) { 1693 .enter, .arrow_right => try self.focusDetail(index, root_focus), 1694 else => {}, 1695 } 1696 } 1697 1698 fn detailInput(self: *View, allocator: std.mem.Allocator, index: usize, key: Key, root_focus: *Focus) !void { 1699 switch (key) { 1700 .page_up => { 1701 self.pageDetail(index, root_focus, -10); 1702 return; 1703 }, 1704 .page_down => { 1705 self.pageDetail(index, root_focus, 10); 1706 return; 1707 }, 1708 .home => { 1709 self.jumpDetail(index, root_focus, false); 1710 return; 1711 }, 1712 .end => { 1713 self.jumpDetail(index, root_focus, true); 1714 return; 1715 }, 1716 else => {}, 1717 } 1718 if (self.data.comment_page != null and std.mem.eql(u8, self.window(index).issues[self.detailed_index[index] orelse return].id, self.data.selected_id)) { 1719 try self.commentInput(index, key, root_focus); 1720 } else if (self.toolRowFocused(index)) { 1721 try self.toolRowInput(allocator, index, key, root_focus); 1722 } else if (self.titleFocused(index)) { 1723 try self.titleInput(index, key, root_focus); 1724 } else if (self.authorFocused(index)) { 1725 try self.authorInput(index, key, root_focus); 1726 } else if (self.tagsFocused(index)) { 1727 try self.tagsInput(index, key, root_focus); 1728 } else if (self.descriptionFocused(index)) { 1729 try self.descriptionInput(index, key, root_focus); 1730 } else { 1731 try self.commentInput(index, key, root_focus); 1732 } 1733 } 1734 1735 // the tool row: enter or a click on the open/close button flips the 1736 // issue's status; the edit and resolve buttons are a: links the host 1737 // follows. arrows cross between the buttons and to the neighboring 1738 // widgets. 1739 fn toolRowInput(self: *View, allocator: std.mem.Allocator, index: usize, key: Key, root_focus: *Focus) !void { 1740 const row = self.toolRow(index); 1741 const cur = if (row.getFocus().child_id) |cid| row.children.getIndex(cid) orelse return else return; 1742 const on_status = if (self.status_button_id[index]) |id| row.getFocus().child_id == id else false; 1743 switch (key) { 1744 .arrow_left => if (cur > first_in_row_index) root_focus.setFocus(row.children.keys()[cur - 1]) else try self.focusList(index, root_focus), 1745 .arrow_right => if (cur + 1 < row.children.count()) root_focus.setFocus(row.children.keys()[cur + 1]), 1746 .arrow_up => self.focusHeader(root_focus), 1747 .arrow_down => { 1748 self.focusDetailEdge(index, root_focus, false); 1749 self.detailScroll(index).y = 0; 1750 }, 1751 .enter => if (on_status) try self.toggleIssueStatus(allocator, index), 1752 .mouse => |mouse| if (self.status_button_id[index]) |id| { 1753 if (inp.leftClickOn(root_focus, id, mouse)) try self.toggleIssueStatus(allocator, index); 1754 }, 1755 else => {}, 1756 } 1757 } 1758 1759 fn titleInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 1760 switch (key) { 1761 .arrow_left => try self.focusList(index, root_focus), 1762 .arrow_up => self.focusToolRow(index, root_focus), 1763 .arrow_down => _ = self.moveDetailVertical(index, root_focus, true), 1764 else => {}, 1765 } 1766 } 1767 1768 // the author box's a: link (when it has one) is followed by the host on 1769 // enter / a click; arrows cross to the neighboring widgets. 1770 fn authorInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 1771 switch (key) { 1772 .arrow_left => try self.focusList(index, root_focus), 1773 .arrow_up => _ = self.moveDetailVertical(index, root_focus, false), 1774 .arrow_down => _ = self.moveDetailVertical(index, root_focus, true), 1775 else => {}, 1776 } 1777 } 1778 1779 fn descriptionInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 1780 const sc = self.detailScroll(index); 1781 switch (key) { 1782 .arrow_left => return self.focusList(index, root_focus), 1783 // the terminal moves through the description one line at a time; 1784 // the web lets focus changes drive its native scroll. 1785 .arrow_up => { 1786 if (!self.session.is_terminal) { 1787 _ = self.moveDetailVertical(index, root_focus, false); 1788 return; 1789 } 1790 if (self.focusAdjacentVisible(index, root_focus, false)) return; 1791 const before = sc.y; 1792 sc.y -= 1; 1793 sc.clampToContent(); 1794 if (sc.y == before) _ = self.moveDetailVertical(index, root_focus, false); 1795 return; 1796 }, 1797 .arrow_down => { 1798 if (!self.session.is_terminal) { 1799 _ = self.moveDetailVertical(index, root_focus, true); 1800 return; 1801 } 1802 if (self.focusAdjacentVisible(index, root_focus, true)) return; 1803 const before = sc.y; 1804 sc.y += 1; 1805 sc.clampToContent(); 1806 if (sc.y == before) _ = self.moveDetailVertical(index, root_focus, true); 1807 return; 1808 }, 1809 else => return, 1810 } 1811 sc.clampToContent(); 1812 } 1813 1814 fn commentInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 1815 switch (key) { 1816 .arrow_left => if (!self.moveCommentHorizontal(index, root_focus, false)) return self.focusList(index, root_focus), 1817 .arrow_right => _ = self.moveCommentHorizontal(index, root_focus, true), 1818 .arrow_up => _ = self.moveDetailVertical(index, root_focus, false), 1819 .arrow_down => _ = self.moveDetailVertical(index, root_focus, true), 1820 else => return, 1821 } 1822 } 1823 1824 // page the detail scroll, then focus the first or last visible widget. 1825 fn pageDetail(self: *View, index: usize, root_focus: *Focus, delta: isize) void { 1826 if (!self.session.is_terminal) return; 1827 const sc = self.detailScroll(index); 1828 sc.y += delta; 1829 sc.clampToContent(); 1830 self.focusVisible(index, root_focus, delta > 0); 1831 } 1832 1833 // jump to the detail's first or last focusable widget. the terminal pins 1834 // its synthetic scroll too; the browser scrolls to the new focus itself. 1835 fn jumpDetail(self: *View, index: usize, root_focus: *Focus, to_end: bool) void { 1836 if (self.session.is_terminal) { 1837 const sc = self.detailScroll(index); 1838 sc.y = if (to_end) std.math.maxInt(isize) else 0; 1839 sc.clampToContent(); 1840 self.focusVisible(index, root_focus, to_end); 1841 } else { 1842 self.focusDetailEdge(index, root_focus, to_end); 1843 } 1844 } 1845 1846 fn focusDetailEdge(self: *View, index: usize, root_focus: *Focus, last: bool) void { 1847 const focus = self.detailInner(index).getFocus(); 1848 var chosen: ?usize = null; 1849 for (focus.children.keys(), focus.children.values()) |id, child| { 1850 if (!child.focus.focusable) continue; 1851 chosen = id; 1852 if (!last) break; 1853 } 1854 if (chosen) |id| root_focus.setFocus(id); 1855 } 1856 1857 fn detailRectVisible(self: *View, index: usize, rect: layout.IRect) bool { 1858 const sc = self.detailScroll(index); 1859 const viewport = sc.grid orelse return false; 1860 const top = sc.y; 1861 const bottom = top + @as(isize, @intCast(viewport.size.height - sc.bar_h)); 1862 return rect.y + @as(isize, @intCast(rect.size.height)) > top and rect.y < bottom; 1863 } 1864 1865 // focus the top- or bottom-most focusable widget intersecting the viewport. 1866 fn focusVisible(self: *View, index: usize, root_focus: *Focus, last: bool) void { 1867 const inner = self.detailInner(index); 1868 var offset: usize = 0; 1869 while (offset < inner.children.count()) : (offset += 1) { 1870 const child_index = if (last) inner.children.count() - 1 - offset else offset; 1871 if (self.focusVisibleChild(index, child_index, root_focus, last)) return; 1872 } 1873 } 1874 1875 fn focusVisibleChild(self: *View, index: usize, child_index: usize, root_focus: *Focus, last: bool) bool { 1876 const child = &self.detailInner(index).children.values()[child_index]; 1877 const child_rect = child.rect orelse return false; 1878 var chosen: ?usize = null; 1879 if (child.widget.getFocus().focusable and self.detailRectVisible(index, child_rect)) { 1880 const id = self.detailInner(index).children.keys()[child_index]; 1881 chosen = id; 1882 if (!last) { 1883 root_focus.setFocus(id); 1884 return true; 1885 } 1886 } 1887 1888 for (child.widget.getFocus().children.keys(), child.widget.getFocus().children.values()) |id, focus_child| { 1889 if (!focus_child.focus.focusable) continue; 1890 const rect: layout.IRect = .{ 1891 .x = child_rect.x + @as(isize, @intCast(focus_child.rect.x)), 1892 .y = child_rect.y + @as(isize, @intCast(focus_child.rect.y)), 1893 .size = focus_child.rect.size, 1894 }; 1895 if (!self.detailRectVisible(index, rect)) continue; 1896 chosen = id; 1897 if (!last) break; 1898 } 1899 if (chosen) |id| { 1900 root_focus.setFocus(id); 1901 return true; 1902 } 1903 return false; 1904 } 1905 1906 fn focusAdjacentVisible(self: *View, index: usize, root_focus: *Focus, down: bool) bool { 1907 const inner = self.detailInner(index); 1908 var next = self.focusedDetailChild(index, root_focus) orelse return false; 1909 while (true) { 1910 if (down) { 1911 next += 1; 1912 if (next >= inner.children.count()) return false; 1913 } else { 1914 if (next == 0) return false; 1915 next -= 1; 1916 } 1917 if (self.focusVisibleChild(index, next, root_focus, !down)) { 1918 self.focusDetailChild(index, next, !down, root_focus); 1919 return true; 1920 } 1921 } 1922 } 1923 1924 fn focusedDetailChild(self: *View, index: usize, root_focus: *Focus) ?usize { 1925 const inner = self.detailInner(index); 1926 var id = root_focus.grandchild_id orelse return null; 1927 while (inner.getFocus().children.get(id)) |child| { 1928 if (child.parent_id == inner.getFocus().id) return inner.children.getIndex(id); 1929 id = child.parent_id; 1930 } 1931 return null; 1932 } 1933 1934 fn detailChildFocusable(self: *View, index: usize, child_index: usize) bool { 1935 const child = &self.detailInner(index).children.values()[child_index].widget; 1936 const focus = child.getFocus(); 1937 if (focus.focusable) return true; 1938 for (focus.children.values()) |item| { 1939 if (item.focus.focusable) return true; 1940 } 1941 return false; 1942 } 1943 1944 fn focusDetailChild(self: *View, index: usize, child_index: usize, last: bool, root_focus: *Focus) void { 1945 const inner = self.detailInner(index); 1946 const child = &inner.children.values()[child_index]; 1947 var target = inner.children.keys()[child_index]; 1948 var rect = child.rect orelse return; 1949 switch (child.widget) { 1950 .repo_comment => |*comment| { 1951 if (comment.rowRect(last)) |row_rect| { 1952 rect.x += row_rect.x; 1953 rect.y += row_rect.y; 1954 rect.size = row_rect.size; 1955 } 1956 if (last) comment.focusBody(root_focus) else comment.focusMetadata(root_focus); 1957 }, 1958 .box => |*box| if (box.children.count() > 0) { 1959 target = box.children.keys()[if (last) box.children.count() - 1 else 0]; 1960 root_focus.setFocus(target); 1961 }, 1962 else => root_focus.setFocus(target), 1963 } 1964 const sc = self.detailScroll(index); 1965 if (sc.grid) |viewport| { 1966 const viewport_height = viewport.size.height - sc.bar_h; 1967 if (rect.size.height > viewport_height) { 1968 const bottom_aligned = rect.y + @as(isize, @intCast(rect.size.height - viewport_height)); 1969 sc.y = std.math.clamp(sc.y, rect.y, bottom_aligned); 1970 sc.clampToContent(); 1971 return; 1972 } 1973 } 1974 sc.scrollToRect(rect); 1975 } 1976 1977 fn moveDetailVertical(self: *View, index: usize, root_focus: *Focus, down: bool) bool { 1978 const inner = self.detailInner(index); 1979 const current = self.focusedDetailChild(index, root_focus) orelse return false; 1980 if (inner.children.values()[current].widget == .repo_comment) { 1981 const comment = &inner.children.values()[current].widget.repo_comment; 1982 if (down and !comment.bodyFocused()) { 1983 self.focusDetailChild(index, current, true, root_focus); 1984 return true; 1985 } 1986 if (!down and comment.bodyFocused()) { 1987 self.focusDetailChild(index, current, false, root_focus); 1988 return true; 1989 } 1990 } 1991 1992 var next = current; 1993 while (true) { 1994 if (down) { 1995 next += 1; 1996 if (next >= inner.children.count()) return false; 1997 } else { 1998 if (next == 0) return false; 1999 next -= 1; 2000 } 2001 if (!self.detailChildFocusable(index, next)) continue; 2002 self.focusDetailChild(index, next, !down, root_focus); 2003 return true; 2004 } 2005 } 2006 2007 fn moveCommentHorizontal(self: *View, index: usize, root_focus: *Focus, right: bool) bool { 2008 const inner = self.detailInner(index); 2009 const current = self.focusedDetailChild(index, root_focus) orelse return false; 2010 const child = &inner.children.values()[current]; 2011 if (child.widget == .repo_comment) { 2012 const moved = child.widget.repo_comment.moveHorizontal(root_focus, right); 2013 if (moved) if (child.rect) |rect| self.detailScroll(index).scrollToRect(rect); 2014 return moved; 2015 } 2016 const row = switch (child.widget) { 2017 .box => |*box| box, 2018 else => return false, 2019 }; 2020 const selected = row.getFocus().child_id orelse return false; 2021 const selected_index = row.children.getIndex(selected) orelse return false; 2022 const target = if (right) selected_index + 1 else selected_index -| 1; 2023 if (target == selected_index or target >= row.children.count()) return false; 2024 root_focus.setFocus(row.children.keys()[target]); 2025 if (child.rect) |rect| self.detailScroll(index).scrollToRect(rect); 2026 return true; 2027 } 2028 2029 // up/down (and tab/shift+tab) move between a form's fields; up from the 2030 // title crosses into the header tabs. the multiline description keeps 2031 // enter and any up/down that has a row to move to. 2032 fn formInput(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2033 const form = self.issueForm() orelse return; 2034 const cid = form.getFocus().child_id orelse return; 2035 const cur = form.children.getIndex(cid) orelse return; 2036 2037 if (cur == description_field_index) { 2038 const child = &form.children.values()[cur]; 2039 if (try multilineKeepsKey(&child.widget.text_input, allocator, key)) 2040 return child.widget.input(allocator, key, root_focus); 2041 } 2042 2043 switch (key) { 2044 .arrow_up, .back_tab => if (cur > 0) 2045 root_focus.setFocus(form.children.keys()[cur - 1]) 2046 else 2047 self.focusHeader(root_focus), 2048 .arrow_down, .tab => if (cur < submit_field_index) { 2049 root_focus.setFocus(form.children.keys()[cur + 1]); 2050 }, 2051 .enter => if (cur == submit_field_index) try self.submitForm(allocator), 2052 .mouse => |mouse| if (cur == submit_field_index) { 2053 const submit = &form.children.values()[cur].widget.submit_button; 2054 if (inp.leftClickOn(root_focus, submit.buttonId(), mouse)) try self.submitForm(allocator); 2055 }, 2056 else => try form.children.values()[cur].widget.input(allocator, key, root_focus), 2057 } 2058 } 2059 2060 fn commentFormInput(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2061 const form = self.issueForm() orelse return; 2062 const cid = form.getFocus().child_id orelse return; 2063 const cur = form.children.getIndex(cid) orelse return; 2064 const child = &form.children.values()[cur]; 2065 2066 if (cur == comment_body_field_index and try multilineKeepsKey(&child.widget.text_input, allocator, key)) 2067 return child.widget.input(allocator, key, root_focus); 2068 2069 switch (key) { 2070 .arrow_up, .back_tab => if (cur > comment_author_field_index) 2071 root_focus.setFocus(form.children.keys()[cur - 1]) 2072 else 2073 self.focusHeader(root_focus), 2074 .arrow_down, .tab => if (cur < comment_submit_field_index) 2075 root_focus.setFocus(form.children.keys()[cur + 1]), 2076 .enter => if (cur == comment_submit_field_index) try self.submitComment(allocator), 2077 .mouse => |mouse| if (cur == comment_submit_field_index) { 2078 const submit = &child.widget.submit_button; 2079 if (inp.leftClickOn(root_focus, submit.buttonId(), mouse)) try self.submitComment(allocator); 2080 }, 2081 else => try child.widget.input(allocator, key, root_focus), 2082 } 2083 } 2084 2085 // the multiline hunk inputs keep enter and any up/down that has a row to 2086 // move to; enter on a "use this" link is a navigation the host follows 2087 // before this runs. 2088 fn resolveInput(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2089 const form = self.issueForm() orelse return; 2090 const cid = form.getFocus().child_id orelse return; 2091 const cur = form.children.getIndex(cid) orelse return; 2092 const child = &form.children.values()[cur]; 2093 2094 if (child.widget == .text_input) { 2095 const ti = &child.widget.text_input; 2096 if (ti.options.multiline and try multilineKeepsKey(ti, allocator, key)) 2097 return child.widget.input(allocator, key, root_focus); 2098 } 2099 2100 const on_submit = child.widget == .submit_button; 2101 2102 switch (key) { 2103 .arrow_up, .back_tab => if (resolveStep(form, cur, false)) |i| self.focusResolveChild(form, i, root_focus) else self.focusHeader(root_focus), 2104 .arrow_down, .tab => if (resolveStep(form, cur, true)) |i| self.focusResolveChild(form, i, root_focus), 2105 .arrow_left, .arrow_right => switch (child.widget) { 2106 .box => |*row| { 2107 const row_cid = row.getFocus().child_id orelse return; 2108 const row_index = row.children.getIndex(row_cid) orelse return; 2109 if (key == .arrow_left) { 2110 if (row_index > 0) root_focus.setFocus(row.children.keys()[row_index - 1]); 2111 } else if (row_index + 1 < row.children.count()) { 2112 root_focus.setFocus(row.children.keys()[row_index + 1]); 2113 } 2114 }, 2115 else => try child.widget.input(allocator, key, root_focus), 2116 }, 2117 .enter => if (on_submit) try self.submitResolution(allocator), 2118 .mouse => |mouse| if (on_submit) { 2119 if (inp.leftClickOn(root_focus, child.widget.submit_button.buttonId(), mouse)) try self.submitResolution(allocator); 2120 }, 2121 else => try child.widget.input(allocator, key, root_focus), 2122 } 2123 } 2124 2125 // a multiline input keeps enter and any up/down that has a row to move to 2126 fn multilineKeepsKey(text_input: *wgt.TextInput(ui.Widget), allocator: std.mem.Allocator, key: Key) !bool { 2127 return switch (key) { 2128 .enter => true, 2129 .arrow_up => !try text_input.cursorOnFirstRow(allocator), 2130 .arrow_down => !try text_input.cursorOnLastRow(allocator), 2131 else => false, 2132 }; 2133 } 2134 2135 // the neighboring focusable resolve-form child, skipping the spacer and 2136 // the blank gap rows 2137 fn resolveStep(form: *wgt.Box(ui.Widget), cur: usize, down: bool) ?usize { 2138 var i = cur; 2139 while (true) { 2140 if (down) { 2141 i += 1; 2142 if (i >= form.children.count()) return null; 2143 } else { 2144 if (i == 0) return null; 2145 i -= 1; 2146 } 2147 switch (form.children.values()[i].widget) { 2148 .spacer, .text => continue, 2149 else => return i, 2150 } 2151 } 2152 } 2153 2154 // focus the resolve-form child at `i` (a version row focuses its selected 2155 // child) and keep it visible on the terminal 2156 fn focusResolveChild(self: *View, form: *wgt.Box(ui.Widget), i: usize, root_focus: *Focus) void { 2157 const child = &form.children.values()[i]; 2158 switch (child.widget) { 2159 .box => |*row| root_focus.setFocus(row.getFocus().child_id orelse form.children.keys()[i]), 2160 else => root_focus.setFocus(form.children.keys()[i]), 2161 } 2162 if (self.session.is_terminal) { 2163 if (self.resolveScroll()) |sc| { 2164 if (child.rect) |rect| sc.scrollToRect(rect); 2165 } 2166 } 2167 } 2168 2169 // re-emit the conflicted issue's event with the resolved content; any 2170 // event on the issue settles its conflict entry. this is the terminal 2171 // path; the web posts the form to the resolve route. 2172 fn submitResolution(self: *View, allocator: std.mem.Allocator) !void { 2173 if (comptime wasm) return; 2174 const io = self.session.io orelse return; 2175 const src = self.data.repo_source orelse return; 2176 const entry = self.data.selectedIssue() orelse return; 2177 const author = (try self.session.eventAuthor()) orelse return; 2178 const form = self.issueForm() orelse return; 2179 2180 // gather the inputs by name; the d<n> hunk inputs appear in chunk order 2181 var title: ?[]u8 = null; 2182 var tags: ?[]u8 = null; 2183 var hunks: std.ArrayList([]const u8) = .empty; 2184 defer { 2185 if (title) |t| allocator.free(t); 2186 if (tags) |t| allocator.free(t); 2187 for (hunks.items) |h| allocator.free(h); 2188 hunks.deinit(allocator); 2189 } 2190 for (form.children.values()) |*child| switch (child.widget) { 2191 .text_input => |*ti| { 2192 const text = try ti.text(allocator); 2193 errdefer allocator.free(text); 2194 if (std.mem.eql(u8, ti.options.name, "title")) { 2195 title = text; 2196 } else if (std.mem.eql(u8, ti.options.name, "tags")) { 2197 tags = text; 2198 } else { 2199 try hunks.append(allocator, text); 2200 } 2201 }, 2202 else => {}, 2203 }; 2204 2205 var id_bytes: [evt.event_id_size]u8 = undefined; 2206 _ = try std.fmt.hexToBytes(&id_bytes, entry.id[0 .. evt.event_id_size * 2]); 2207 2208 switch (src.repo_kind) { 2209 inline else => |repo_kind| { 2210 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2211 defer any_repo.deinit(io, allocator); 2212 switch (any_repo) { 2213 inline else => |*repo| evt.Issue.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .resolve = .{ 2214 .title = title, 2215 .tags = tags, 2216 .hunks = hunks.items, 2217 } }, author) catch |err| switch (err) { 2218 // leave the form up for correction 2219 error.InvalidFields => return, 2220 else => |e| return e, 2221 }, 2222 } 2223 }, 2224 } 2225 2226 const route = ui.RoutablePage.repoIssuesRoute(self.data.identity, entry.issue.event.status, "", entry.id) orelse return; 2227 try self.session.navigate(route); 2228 } 2229 2230 // commit a new or edited comment and navigate to its permalink. this is the 2231 // terminal path; the web posts the form to its comment route. 2232 fn submitComment(self: *View, allocator: std.mem.Allocator) !void { 2233 if (comptime wasm) return; 2234 const io = self.session.io orelse return; 2235 const src = self.data.repo_source orelse return; 2236 const author = (try self.session.eventAuthor()) orelse return; 2237 const form = self.issueForm() orelse return; 2238 const body_input = &form.children.values()[comment_body_field_index].widget.text_input; 2239 const body = try body_input.text(allocator); 2240 defer allocator.free(body); 2241 if (!evt.Comment.fieldsValid(body)) return; 2242 2243 var thread_id: [evt.event_id_size * 2]u8 = undefined; 2244 @memcpy(&thread_id, self.data.selected_id); 2245 const parent_id = if (self.data.comment_id.len == 0) thread_id else blk: { 2246 var parent: [evt.event_id_size * 2]u8 = undefined; 2247 @memcpy(&parent, self.data.comment_id); 2248 break :blk parent; 2249 }; 2250 var event_id_hex: [evt.event_id_size * 2]u8 = undefined; 2251 const editing = self.data.view == .edit_comment; 2252 switch (src.repo_kind) { 2253 inline else => |repo_kind| { 2254 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2255 defer any_repo.deinit(io, allocator); 2256 switch (any_repo) { 2257 inline else => |*repo| if (editing) { 2258 var comment_id: [evt.event_id_size]u8 = undefined; 2259 _ = std.fmt.hexToBytes(&comment_id, self.data.comment_id) catch return; 2260 try evt.Comment.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &thread_id, &comment_id, body, author); 2261 event_id_hex = std.fmt.bytesToHex(comment_id, .lower); 2262 } else { 2263 event_id_hex = try evt.Comment.create(repo_kind, repo.self_repo_opts, io, allocator, repo, &thread_id, &parent_id, body, author); 2264 }, 2265 } 2266 }, 2267 } 2268 2269 body_input.clear(allocator); 2270 const route = ui.RoutablePage.repoCommentsRoute(self.data.identity, &thread_id, &event_id_hex, 0) orelse return; 2271 try self.session.navigate(route); 2272 } 2273 2274 // the terminal submit paths: the new form commits a new-issue event, the 2275 // edit form re-emits the selected issue's event with the form's content. 2276 // the web posts the forms to their routes instead. 2277 fn submitForm(self: *View, allocator: std.mem.Allocator) !void { 2278 if (self.data.view == .edit) { 2279 try self.submitEditedIssue(allocator); 2280 } else { 2281 try self.submitNewIssue(allocator); 2282 } 2283 } 2284 2285 // commit the new issue to the repo's events branch and navigate to it. 2286 // this is the terminal path; the web posts the form to the issue route, 2287 // so the wasm side never runs (or compiles) the repo access below. 2288 fn submitNewIssue(self: *View, allocator: std.mem.Allocator) !void { 2289 if (comptime wasm) return; 2290 const io = self.session.io orelse return; 2291 const src = self.data.repo_source orelse return; 2292 const author = (try self.session.eventAuthor()) orelse return; 2293 2294 const form = self.issueForm() orelse return; 2295 const title_input = &form.children.values()[title_field_index].widget.text_input; 2296 const tags_input = &form.children.values()[tags_field_index].widget.text_input; 2297 const description_input = &form.children.values()[description_field_index].widget.text_input; 2298 2299 const title = try title_input.text(allocator); 2300 defer allocator.free(title); 2301 const tags = try tags_input.text(allocator); 2302 defer allocator.free(tags); 2303 const description = try description_input.text(allocator); 2304 defer allocator.free(description); 2305 2306 if (!evt.Issue.fieldsValid(title, tags)) return; 2307 2308 var id_bytes: [evt.event_id_size]u8 = undefined; 2309 io.random(&id_bytes); 2310 const event_id_hex = std.fmt.bytesToHex(id_bytes, .lower); 2311 2312 const event = evt.EventWithId{ 2313 .id = event_id_hex, 2314 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 2315 .author = author, 2316 .event = .{ .issue = .{ 2317 .title = title, 2318 .description = description, 2319 .tags = tags, 2320 } }, 2321 }; 2322 2323 switch (src.repo_kind) { 2324 inline else => |repo_kind| { 2325 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2326 defer any_repo.deinit(io, allocator); 2327 switch (any_repo) { 2328 inline else => |*repo| try evt.consume(repo_kind, repo.self_repo_opts, io, allocator, repo, evt.events_ref, &.{event}), 2329 } 2330 }, 2331 } 2332 2333 // wipe the form so a return visit starts fresh 2334 title_input.clear(allocator); 2335 tags_input.clear(allocator); 2336 description_input.clear(allocator); 2337 2338 const route = ui.RoutablePage.repoIssuesRoute(self.data.identity, .open, "", &event_id_hex) orelse return; 2339 try self.session.navigate(route); 2340 } 2341 2342 // re-emit the selected issue's event with the form's content (status 2343 // preserved), then reload the issue's page. this is the terminal path; 2344 // the web posts the form to the edit route. 2345 fn submitEditedIssue(self: *View, allocator: std.mem.Allocator) !void { 2346 if (comptime wasm) return; 2347 const io = self.session.io orelse return; 2348 const src = self.data.repo_source orelse return; 2349 const entry = self.data.selectedIssue() orelse return; 2350 const author = (try self.session.eventAuthor()) orelse return; 2351 2352 const form = self.issueForm() orelse return; 2353 const title_input = &form.children.values()[title_field_index].widget.text_input; 2354 const tags_input = &form.children.values()[tags_field_index].widget.text_input; 2355 const description_input = &form.children.values()[description_field_index].widget.text_input; 2356 2357 const title = try title_input.text(allocator); 2358 defer allocator.free(title); 2359 const tags = try tags_input.text(allocator); 2360 defer allocator.free(tags); 2361 const description = try description_input.text(allocator); 2362 defer allocator.free(description); 2363 2364 if (!evt.Issue.fieldsValid(title, tags)) return; 2365 2366 var id_bytes: [evt.event_id_size]u8 = undefined; 2367 _ = try std.fmt.hexToBytes(&id_bytes, entry.id[0 .. evt.event_id_size * 2]); 2368 2369 switch (src.repo_kind) { 2370 inline else => |repo_kind| { 2371 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2372 defer any_repo.deinit(io, allocator); 2373 switch (any_repo) { 2374 inline else => |*repo| try evt.Issue.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 2375 .title = title, 2376 .tags = tags, 2377 .description = description, 2378 } }, author), 2379 } 2380 }, 2381 } 2382 2383 const route = ui.RoutablePage.repoIssuesRoute(self.data.identity, entry.issue.event.status, "", entry.id) orelse return; 2384 try self.session.navigate(route); 2385 } 2386 2387 // flip the shown issue's status by re-emitting its event, then reload the 2388 // page rooted at the issue so the view reflects the change. this is the 2389 // terminal path; the web posts the button's form to the status route. 2390 fn toggleIssueStatus(self: *View, allocator: std.mem.Allocator, index: usize) !void { 2391 if (comptime wasm) return; 2392 const io = self.session.io orelse return; 2393 const src = self.data.repo_source orelse return; 2394 const sel = self.detailed_index[index] orelse return; 2395 const entry = self.window(index).issues[sel]; 2396 const author = (try self.session.eventAuthor()) orelse return; 2397 2398 const status: evt.Issue.Status = switch (entry.issue.event.status) { 2399 .open => .closed, 2400 .closed => .open, 2401 }; 2402 2403 var id_bytes: [evt.event_id_size]u8 = undefined; 2404 _ = try std.fmt.hexToBytes(&id_bytes, entry.id[0 .. evt.event_id_size * 2]); 2405 2406 switch (src.repo_kind) { 2407 inline else => |repo_kind| { 2408 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2409 defer any_repo.deinit(io, allocator); 2410 switch (any_repo) { 2411 inline else => |*repo| try evt.Issue.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .status = status }, author), 2412 } 2413 }, 2414 } 2415 2416 const route = ui.RoutablePage.repoIssuesRoute(self.data.identity, status, "", entry.id) orelse return; 2417 try self.session.navigate(route); 2418 } 2419 2420 // arrow keys move the tag selection; at the flow's edges focus crosses to 2421 // the neighboring widgets. 2422 fn tagsInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 2423 const tf = self.tagFlow(index) orelse return; 2424 const cid = tf.focus.child_id orelse return; 2425 const cur = tf.indexOfFocusId(cid) orelse return; 2426 const count = tf.text_boxes.items.len; 2427 switch (key) { 2428 .arrow_left => if (cur > 0) self.focusTag(index, tf, root_focus, cur - 1) else try self.focusList(index, root_focus), 2429 .arrow_right => if (cur + 1 < count) self.focusTag(index, tf, root_focus, cur + 1), 2430 .arrow_up => if (tf.rowStep(cur, false)) |i| { 2431 self.focusTag(index, tf, root_focus, i); 2432 } else { 2433 _ = self.moveDetailVertical(index, root_focus, false); 2434 }, 2435 .arrow_down => if (tf.rowStep(cur, true)) |i| { 2436 self.focusTag(index, tf, root_focus, i); 2437 } else { 2438 _ = self.moveDetailVertical(index, root_focus, true); 2439 }, 2440 .home => self.focusTag(index, tf, root_focus, 0), 2441 .end => self.focusTag(index, tf, root_focus, count - 1), 2442 else => {}, 2443 } 2444 } 2445 2446 const title_child_index: usize = 0; 2447 const tags_child_index: usize = 2; 2448 2449 // the tool row's first child after the spacer that pushes it right 2450 const first_in_row_index: usize = 1; 2451 2452 fn tagFlow(self: *View, index: usize) ?*ui.TagFlow { 2453 const inner = self.detailInner(index); 2454 if (inner.children.count() <= tags_child_index) return null; 2455 return switch (inner.children.values()[tags_child_index].widget) { 2456 .tag_flow => |*tf| tf, 2457 else => null, 2458 }; 2459 } 2460 2461 fn tagsFocused(self: *View, index: usize) bool { 2462 const inner = self.detailInner(index); 2463 const cid = inner.getFocus().child_id orelse return false; 2464 return inner.children.getIndex(cid) == tags_child_index and self.tagFlow(index) != null; 2465 } 2466 2467 fn toolRowFocused(self: *View, index: usize) bool { 2468 const outer = self.detailOuter(index); 2469 const cid = outer.getFocus().child_id orelse return false; 2470 return outer.children.getIndex(cid) == tool_row_index; 2471 } 2472 2473 fn focusTag(self: *View, index: usize, tf: *ui.TagFlow, root_focus: *Focus, item: usize) void { 2474 root_focus.setFocus(tf.text_boxes.items[item].getFocus().id); 2475 // keep the tag visible on the terminal: its rect offset by the flow's 2476 // position in the pane. 2477 if (self.session.is_terminal and item < tf.rects.items.len) { 2478 if (self.detailInner(index).children.values()[tags_child_index].rect) |flow_rect| { 2479 var rect = tf.rects.items[item]; 2480 rect.x += flow_rect.x; 2481 rect.y += flow_rect.y; 2482 self.detailScroll(index).scrollToRect(rect); 2483 } 2484 } 2485 } 2486 2487 fn titleFocused(self: *View, index: usize) bool { 2488 const inner = self.detailInner(index); 2489 const cid = inner.getFocus().child_id orelse return false; 2490 return inner.children.getIndex(cid) == title_child_index; 2491 } 2492 2493 fn authorFocused(self: *View, index: usize) bool { 2494 const inner = self.detailInner(index); 2495 const cid = inner.getFocus().child_id orelse return false; 2496 return cid == self.author_id[index]; 2497 } 2498 2499 fn descriptionFocused(self: *View, index: usize) bool { 2500 const cid = self.detailInner(index).getFocus().child_id orelse return false; 2501 return cid == self.description_id[index]; 2502 } 2503 2504 // return to the tool row's last-focused button (the header when the 2505 // description page shows no row). 2506 fn focusToolRow(self: *View, index: usize, root_focus: *Focus) void { 2507 const cid = self.toolRow(index).getFocus().child_id orelse return self.focusHeader(root_focus); 2508 root_focus.setFocus(cid); 2509 } 2510 2511 // enter the detail pane. an empty pane (no issues) can't be entered. 2512 fn focusDetail(self: *View, index: usize, root_focus: *Focus) !void { 2513 if (self.detailInner(index).children.count() == 0) return; 2514 root_focus.setFocus(self.detailOuter(index).getFocus().id); 2515 } 2516 2517 // return to the list. 2518 fn focusList(self: *View, index: usize, root_focus: *Focus) !void { 2519 root_focus.setFocus(self.listScroll(index).getFocus().id); 2520 } 2521 2522 // cross to the header tabs above the stack. 2523 fn focusHeader(self: *View, root_focus: *Focus) void { 2524 root_focus.setFocus(self.box.children.keys()[header_index]); 2525 } 2526 2527 pub fn clearGrid(self: *View) void { 2528 self.box.clearGrid(); 2529 } 2530 2531 pub fn getGrid(self: View) ?Grid { 2532 return self.box.getGrid(); 2533 } 2534 2535 pub fn getFocus(self: *View) *Focus { 2536 return self.box.getFocus(); 2537 } 2538 2539 // for the parent's "scroll up at the top jumps to the header" check: at the 2540 // top only while the header tabs hold focus, so up from the split 2541 // crosses into the tabs first. 2542 pub fn getSelectedIndex(self: *View) ?usize { 2543 return if (self.headerActive()) 0 else 1; 2544 } 2545 }; 2546 2547 // the "a:" navigation link for `status`'s issues page filtered to the 2548 // url-encoded `tag` and rooted at issue `id` within `identity` ("owner/name"). 2549 // a rooted url ignores `status` (it derives its view from the issue's own); 2550 // with an empty `id` this is the bare list link. 2551 fn issuesLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, status: evt.Issue.Status, tag: []const u8, id: []const u8) ![]const u8 { 2552 const route = ui.RoutablePage.repoIssuesRoute(identity, status, tag, id) orelse return error.RouteTooLong; 2553 const url = try route.toUrl(page_arena); 2554 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2555 } 2556 2557 // the in-page "ai:" anchor for selecting issue `id` in `identity`'s list; the 2558 // href is only followed with js off. 2559 fn issueRowLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, id: []const u8) ![]const u8 { 2560 const route = ui.RoutablePage.repoIssuesRoute(identity, .open, "", id) orelse return error.RouteTooLong; 2561 const url = try route.toUrl(page_arena); 2562 return std.fmt.allocPrint(page_arena.allocator(), "ai:{s}", .{url}); 2563 } 2564 2565 // the "a:" link to the window `status_maybe` names, keeping the tag filter, 2566 // or the conflicts window when null, rooted at issue `id`. 2567 fn windowLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, status_maybe: ?evt.Issue.Status, id: []const u8) ![]const u8 { 2568 if (status_maybe) |status| return issuesLink(page_arena, data.identity, status, data.tag, id); 2569 return conflictsLink(page_arena, data.identity, id); 2570 } 2571 2572 // the "a:" link to the conflicts list rooted at issue `start` ("" = the 2573 // first window). 2574 fn conflictsLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, start: []const u8) ![]const u8 { 2575 const route = ui.RoutablePage.repoIssuesConflictsRoute(identity, start) orelse return error.RouteTooLong; 2576 const url = try route.toUrl(page_arena); 2577 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2578 } 2579 2580 // the "a:" link to the whole-description page of issue `id` within `identity`. 2581 fn descriptionLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, id: []const u8) ![]const u8 { 2582 const route = ui.RoutablePage.repoIssuesDescriptionRoute(identity, id) orelse return error.RouteTooLong; 2583 const url = try route.toUrl(page_arena); 2584 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2585 } 2586 2587 // the "a:" link to `status`'s issues list filtered to `tag` (raw; encoded here). 2588 fn tagLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, status: evt.Issue.Status, tag: []const u8) ![]const u8 { 2589 const encoded = try ui.urlEncodeRef(page_arena.allocator(), tag); 2590 const route = ui.RoutablePage.repoIssuesRoute(identity, status, encoded, "") orelse return error.RouteTooLong; 2591 const url = try route.toUrl(page_arena); 2592 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2593 } 2594 2595 // tabs switching between the issues page's views. 2596 pub const Header = struct { 2597 box: wgt.Box(ui.Widget), 2598 tab_ids: std.AutoArrayHashMapUnmanaged(usize, void), 2599 2600 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self) !Header { 2601 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 2602 errdefer box.deinit(allocator); 2603 2604 var tab_ids: std.AutoArrayHashMapUnmanaged(usize, void) = .empty; 2605 errdefer tab_ids.deinit(allocator); 2606 2607 const aa = session.page_arena.allocator(); 2608 2609 // a list tab per status, labeled with its listing's issue count 2610 for ([_]evt.Issue.Status{ .open, .closed }) |status| { 2611 const route = ui.RoutablePage.repoIssuesRoute(data.identity, status, data.tag, "") orelse return error.RouteTooLong; 2612 const link = try std.fmt.allocPrint(aa, "ai:{s}", .{try route.toUrl(session.page_arena)}); 2613 const label = try std.fmt.allocPrint(aa, "{s} ({d})", .{ @tagName(status), data.window(status).count }); 2614 try addTab(allocator, &box, &tab_ids, label, link); 2615 } 2616 2617 // tags tab, labeled with the active tag filter 2618 { 2619 const tags_route = ui.RoutablePage.repoIssuesTagsRoute(data.identity, data.tag) orelse return error.RouteTooLong; 2620 const tags_link = try std.fmt.allocPrint(aa, "ai:{s}", .{try tags_route.toUrl(session.page_arena)}); 2621 const label = if (data.tag.len == 0) "tags" else blk: { 2622 const decoded = std.Uri.percentDecodeInPlace(try aa.dupe(u8, data.tag)); 2623 break :blk try std.fmt.allocPrint(aa, "tags ({s})", .{decoded}); 2624 }; 2625 try addTab(allocator, &box, &tab_ids, label, tags_link); 2626 } 2627 2628 // new-issue tab; an edit or resolve url shows its tab in this place 2629 { 2630 const route = switch (data.view) { 2631 .edit => ui.RoutablePage.repoIssuesEditRoute(data.identity, data.selected_id) orelse return error.RouteTooLong, 2632 .new_comment => ui.RoutablePage.repoCommentNewRoute(data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong, 2633 .edit_comment => ui.RoutablePage.repoCommentEditRoute(data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong, 2634 .resolve => ui.RoutablePage.repoIssuesResolveRoute(data.identity, data.selected_id, data.theirs_picks) orelse return error.RouteTooLong, 2635 else => ui.RoutablePage.repoIssuesNewRoute(data.identity) orelse return error.RouteTooLong, 2636 }; 2637 const link = try std.fmt.allocPrint(aa, "ai:{s}", .{try route.toUrl(session.page_arena)}); 2638 const label: []const u8 = switch (data.view) { 2639 .edit => "edit", 2640 .new_comment => "reply", 2641 .edit_comment => "edit", 2642 .resolve => "resolve", 2643 else => "new", 2644 }; 2645 try addTab(allocator, &box, &tab_ids, label, link); 2646 } 2647 2648 // conflicts tab, labeled with the conflict count 2649 if (data.conflicts.count > 0) { 2650 const route = ui.RoutablePage.repoIssuesConflictsRoute(data.identity, "") orelse return error.RouteTooLong; 2651 const link = try std.fmt.allocPrint(aa, "ai:{s}", .{try route.toUrl(session.page_arena)}); 2652 const label = try std.fmt.allocPrint(aa, "conflicts ({d})", .{data.conflicts.count}); 2653 try addTab(allocator, &box, &tab_ids, label, link); 2654 } 2655 2656 var self = Header{ .box = box, .tab_ids = tab_ids }; 2657 // the tab matching the page's view is selected initially. 2658 self.getFocus().child_id = self.tab_ids.keys()[View.viewIndex(data.view)]; 2659 return self; 2660 } 2661 2662 fn addTab(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), tab_ids: *std.AutoArrayHashMapUnmanaged(usize, void), label: []const u8, link: []const u8) !void { 2663 var text_box = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 2664 errdefer text_box.deinit(allocator); 2665 text_box.getFocus().focusable = true; 2666 text_box.getFocus().kind = .{ .custom = link }; 2667 try tab_ids.put(allocator, text_box.getFocus().id, {}); 2668 try box.children.put(allocator, text_box.getFocus().id, .{ 2669 .widget = .{ .text_box = text_box }, 2670 .rect = null, 2671 .min_size = .{ .width = label.len + 2, .height = null }, 2672 }); 2673 } 2674 2675 pub fn deinit(self: *Header, allocator: std.mem.Allocator) void { 2676 self.box.deinit(allocator); 2677 self.tab_ids.deinit(allocator); 2678 } 2679 2680 pub fn build(self: *Header, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2681 self.clearGrid(); 2682 // only the selected tab shows its border 2683 for (self.box.children.keys(), self.box.children.values()) |id, *child| { 2684 switch (child.widget) { 2685 .text_box => |*tb| tb.options.border_style = if (self.getFocus().child_id == id) .single else .hidden, 2686 else => {}, 2687 } 2688 } 2689 try self.box.build(allocator, constraint, root_focus); 2690 } 2691 2692 pub fn input(self: *Header, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2693 _ = allocator; 2694 const current_tab = self.currentTabIndex() orelse return; 2695 if (inp.moveTab(key, current_tab, self.tab_ids.count())) |new_tab| { 2696 root_focus.setFocus(self.tab_ids.keys()[new_tab]); 2697 } 2698 } 2699 2700 pub fn clearGrid(self: *Header) void { 2701 self.box.clearGrid(); 2702 } 2703 2704 pub fn getGrid(self: Header) ?Grid { 2705 return self.box.getGrid(); 2706 } 2707 2708 pub fn getFocus(self: *Header) *Focus { 2709 return self.box.getFocus(); 2710 } 2711 2712 pub fn getSelectedIndex(self: Header) ?usize { 2713 return self.currentTabIndex(); 2714 } 2715 2716 fn currentTabIndex(self: Header) ?usize { 2717 const child_id = self.box.focus.child_id orelse return null; 2718 return self.tab_ids.getIndex(child_id); 2719 } 2720 }; 2721