╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ thread.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1024 var next = cur + 1; 1025 while (next < row.children.count()) : (next += 1) { 1026 if (row.children.values()[next].widget.getFocus().mode != .all) continue; 1027 root_focus.setFocus(row.children.keys()[next]); 1028 return; 1029 } 1030 }, 1031 .arrow_up => self.exit = .header, 1032 .arrow_down => _ = self.moveVertical(root_focus, true), 1033 .enter => if (on_primary) try self.primaryAction(allocator), 1034 .mouse => |mouse| if (on_primary and inp.leftClickOn(root_focus, child_id, mouse)) try self.primaryAction(allocator), 1035 else => {}, 1036 } 1037 } 1038 1039 fn primaryAction(self: *This, allocator: std.mem.Allocator) !void { 1040 try self.toggleStatus(allocator); 1041 } 1042 1043 fn descriptionInput(self: *This, key: Key, root_focus: *Focus) void { 1044 const detail_scroll = &self.scroll; 1045 switch (key) { 1046 .arrow_left => self.exit = .list, 1047 .arrow_up => { 1048 if (!self.session.is_terminal) { 1049 _ = self.moveVertical(root_focus, false); 1050 return; 1051 } 1052 if (self.focusAdjacentVisible(root_focus, false)) return; 1053 const before = detail_scroll.y; 1054 detail_scroll.y -= 1; 1055 detail_scroll.clampToContent(); 1056 if (detail_scroll.y == before) _ = self.moveVertical(root_focus, false); 1057 }, 1058 .arrow_down => { 1059 if (!self.session.is_terminal) { 1060 _ = self.moveVertical(root_focus, true); 1061 return; 1062 } 1063 if (self.focusAdjacentVisible(root_focus, true)) return; 1064 const before = detail_scroll.y; 1065 detail_scroll.y += 1; 1066 detail_scroll.clampToContent(); 1067 if (detail_scroll.y == before) _ = self.moveVertical(root_focus, true); 1068 }, 1069 else => {}, 1070 } 1071 } 1072 1073 fn commentInput(self: *This, key: Key, root_focus: *Focus) void { 1074 switch (key) { 1075 .arrow_left => if (!self.moveHorizontal(root_focus, false)) { 1076 self.exit = .list; 1077 }, 1078 .arrow_right => _ = self.moveHorizontal(root_focus, true), 1079 .arrow_up => _ = self.moveVertical(root_focus, false), 1080 .arrow_down => _ = self.moveVertical(root_focus, true), 1081 else => {}, 1082 } 1083 } 1084 1085 fn pageDetail(self: *This, root_focus: *Focus, delta: isize) void { 1086 if (!self.session.is_terminal) return; 1087 const detail_scroll = &self.scroll; 1088 detail_scroll.y += delta; 1089 detail_scroll.clampToContent(); 1090 self.focusVisible(root_focus, delta > 0); 1091 } 1092 1093 fn jump(self: *This, root_focus: *Focus, to_end: bool) void { 1094 if (self.session.is_terminal) { 1095 const detail_scroll = &self.scroll; 1096 detail_scroll.y = if (to_end) std.math.maxInt(isize) else 0; 1097 detail_scroll.clampToContent(); 1098 self.focusVisible(root_focus, to_end); 1099 } else { 1100 const focus = self.inner().getFocus(); 1101 var chosen: ?usize = null; 1102 for (focus.children.keys(), focus.children.values()) |id, child| { 1103 if (child.focus.mode != .all) continue; 1104 chosen = id; 1105 if (!to_end) break; 1106 } 1107 if (chosen) |id| root_focus.setFocus(id); 1108 } 1109 } 1110 1111 fn rectVisible(self: *This, rect: layout.IRect) bool { 1112 const detail_scroll = &self.scroll; 1113 const viewport = detail_scroll.grid orelse return false; 1114 const top = detail_scroll.y; 1115 const bottom = top + @as(isize, @intCast(viewport.size.height - detail_scroll.bar_h)); 1116 return rect.y + @as(isize, @intCast(rect.size.height)) > top and rect.y < bottom; 1117 } 1118 1119 fn focusVisible(self: *This, root_focus: *Focus, last: bool) void { 1120 const inner_box = self.inner(); 1121 var offset: usize = 0; 1122 while (offset < inner_box.children.count()) : (offset += 1) { 1123 const child_index = if (last) inner_box.children.count() - 1 - offset else offset; 1124 if (self.focusVisibleChild(child_index, root_focus, last)) return; 1125 } 1126 } 1127 1128 fn focusVisibleChild(self: *This, child_index: usize, root_focus: *Focus, last: bool) bool { 1129 const inner_box = self.inner(); 1130 const child = &inner_box.children.values()[child_index]; 1131 const child_rect = child.rect orelse return false; 1132 var chosen: ?usize = null; 1133 if (child.widget.getFocus().mode == .all and self.rectVisible(child_rect)) { 1134 const id = inner_box.children.keys()[child_index]; 1135 chosen = id; 1136 if (!last) { 1137 root_focus.setFocus(id); 1138 return true; 1139 } 1140 } 1141 1142 for (child.widget.getFocus().children.keys(), child.widget.getFocus().children.values()) |id, focus_child| { 1143 if (focus_child.focus.mode != .all) continue; 1144 const rect: layout.IRect = .{ 1145 .x = child_rect.x + @as(isize, @intCast(focus_child.rect.x)), 1146 .y = child_rect.y + @as(isize, @intCast(focus_child.rect.y)), 1147 .size = focus_child.rect.size, 1148 }; 1149 if (!self.rectVisible(rect)) continue; 1150 chosen = id; 1151 if (!last) break; 1152 } 1153 if (chosen) |id| { 1154 root_focus.setFocus(id); 1155 return true; 1156 } 1157 return false; 1158 } 1159 1160 fn focusAdjacentVisible(self: *This, root_focus: *Focus, down: bool) bool { 1161 const inner_box = self.inner(); 1162 var next = self.focusedChild(root_focus) orelse return false; 1163 while (true) { 1164 if (down) { 1165 next += 1; 1166 if (next >= inner_box.children.count()) return false; 1167 } else { 1168 if (next == 0) return false; 1169 next -= 1; 1170 } 1171 if (self.focusVisibleChild(next, root_focus, !down)) { 1172 self.focusChild(next, !down, root_focus); 1173 return true; 1174 } 1175 } 1176 } 1177 1178 fn focusedChild(self: *This, root_focus: *Focus) ?usize { 1179 const inner_box = self.inner(); 1180 var id = root_focus.grandchild_id orelse return null; 1181 while (inner_box.getFocus().children.get(id)) |child| { 1182 if (child.parent_id == inner_box.getFocus().id) return inner_box.children.getIndex(id); 1183 id = child.parent_id; 1184 } 1185 return null; 1186 } 1187 1188 fn childFocusable(self: *This, child_index: usize) bool { 1189 const focus = self.inner().children.values()[child_index].widget.getFocus(); 1190 if (focus.mode == .all) return true; 1191 for (focus.children.values()) |item| { 1192 if (item.focus.mode == .all) return true; 1193 } 1194 return false; 1195 } 1196 1197 fn focusChild(self: *This, child_index: usize, last: bool, root_focus: *Focus) void { 1198 const inner_box = self.inner(); 1199 const child = &inner_box.children.values()[child_index]; 1200 var target = inner_box.children.keys()[child_index]; 1201 var rect = child.rect orelse return; 1202 switch (child.widget) { 1203 .repo_comment => |*comment| { 1204 if (comment.rowRect(last)) |row_rect| { 1205 rect.x += row_rect.x; 1206 rect.y += row_rect.y; 1207 rect.size = row_rect.size; 1208 } 1209 if (last) comment.focusBody(root_focus) else comment.focusMetadata(root_focus); 1210 }, 1211 .box => |*box| if (box.children.count() > 0) { 1212 target = if (child_index == tool_row_index) 1213 box.getFocus().child_id orelse return 1214 else 1215 box.children.keys()[if (last) box.children.count() - 1 else 0]; 1216 root_focus.setFocus(target); 1217 }, 1218 else => root_focus.setFocus(target), 1219 } 1220 const detail_scroll = &self.scroll; 1221 if (detail_scroll.grid) |viewport| { 1222 const viewport_height = viewport.size.height - detail_scroll.bar_h; 1223 if (rect.size.height > viewport_height) { 1224 const bottom_aligned = rect.y + @as(isize, @intCast(rect.size.height - viewport_height)); 1225 detail_scroll.y = std.math.clamp(detail_scroll.y, rect.y, bottom_aligned); 1226 detail_scroll.clampToContent(); 1227 return; 1228 } 1229 } 1230 detail_scroll.scrollToRect(rect); 1231 } 1232 1233 fn moveVertical(self: *This, root_focus: *Focus, down: bool) bool { 1234 const inner_box = self.inner(); 1235 const current = self.focusedChild(root_focus) orelse return false; 1236 if (inner_box.children.values()[current].widget == .repo_comment) { 1237 const comment = &inner_box.children.values()[current].widget.repo_comment; 1238 if (down and !comment.bodyFocused()) { 1239 self.focusChild(current, true, root_focus); 1240 return true; 1241 } 1242 if (!down and comment.bodyFocused()) { 1243 self.focusChild(current, false, root_focus); 1244 return true; 1245 } 1246 } 1247 1248 var next = current; 1249 while (true) { 1250 if (down) { 1251 next += 1; 1252 if (next >= inner_box.children.count()) return false; 1253 } else { 1254 if (next == 0) return false; 1255 next -= 1; 1256 } 1257 if (!self.childFocusable(next)) continue; 1258 self.focusChild(next, !down, root_focus); 1259 return true; 1260 } 1261 } 1262 1263 fn moveHorizontal(self: *This, root_focus: *Focus, right: bool) bool { 1264 const inner_box = self.inner(); 1265 const current = self.focusedChild(root_focus) orelse return false; 1266 const child = &inner_box.children.values()[current]; 1267 if (child.widget == .repo_comment) { 1268 const moved = child.widget.repo_comment.moveHorizontal(root_focus, right); 1269 if (moved) if (child.rect) |rect| self.scroll.scrollToRect(rect); 1270 return moved; 1271 } 1272 const row = switch (child.widget) { 1273 .box => |*box| box, 1274 else => return false, 1275 }; 1276 if (!widget.moveInRow(row, root_focus, right)) return false; 1277 if (child.rect) |rect| self.scroll.scrollToRect(rect); 1278 return true; 1279 } 1280 1281 fn labelsInput(self: *This, child_index: usize, labels: *WordFlow, key: Key, root_focus: *Focus) void { 1282 const child_id = labels.focus.child_id orelse return; 1283 const cur = labels.indexOfFocusId(child_id) orelse return; 1284 const count = labels.text_boxes.items.len; 1285 switch (key) { 1286 .arrow_left => if (cur > 0) self.focusLabel(child_index, labels, root_focus, cur - 1) else { 1287 self.exit = .list; 1288 }, 1289 .arrow_right => if (cur + 1 < count) self.focusLabel(child_index, labels, root_focus, cur + 1), 1290 .arrow_up => if (labels.rowStep(cur, false)) |index| { 1291 self.focusLabel(child_index, labels, root_focus, index); 1292 } else { 1293 _ = self.moveVertical(root_focus, false); 1294 }, 1295 .arrow_down => if (labels.rowStep(cur, true)) |index| { 1296 self.focusLabel(child_index, labels, root_focus, index); 1297 } else { 1298 _ = self.moveVertical(root_focus, true); 1299 }, 1300 .home => self.focusLabel(child_index, labels, root_focus, 0), 1301 .end => self.focusLabel(child_index, labels, root_focus, count - 1), 1302 else => {}, 1303 } 1304 } 1305 1306 fn focusLabel(self: *This, child_index: usize, labels: *WordFlow, root_focus: *Focus, item: usize) void { 1307 root_focus.setFocus(labels.text_boxes.items[item].getFocus().id); 1308 if (self.session.is_terminal and item < labels.rects.items.len) { 1309 if (self.inner().children.values()[child_index].rect) |flow_rect| { 1310 var rect = labels.rects.items[item]; 1311 rect.x += flow_rect.x; 1312 rect.y += flow_rect.y; 1313 self.scroll.scrollToRect(rect); 1314 } 1315 } 1316 } 1317 1318 fn toolRowFocused(self: *This, root_focus: *Focus) bool { 1319 return self.focusedChild(root_focus) == tool_row_index; 1320 } 1321 1322 pub fn focusFirst(self: *This, root_focus: ?*Focus) bool { 1323 const inner_box = self.inner(); 1324 for (inner_box.children.values(), 0..) |*child, child_index| { 1325 switch (child.widget) { 1326 .box => |*box| { 1327 for (box.children.keys(), box.children.values()) |id, *item| { 1328 if (item.widget.getFocus().mode != .all) continue; 1329 box.getFocus().child_id = id; 1330 break; 1331 } else continue; 1332 }, 1333 else => if (!self.childFocusable(child_index)) continue, 1334 } 1335 if (root_focus) |focus| 1336 self.focusChild(child_index, false, focus) 1337 else 1338 inner_box.getFocus().child_id = inner_box.children.keys()[child_index]; 1339 return true; 1340 } 1341 return false; 1342 } 1343 1344 pub fn atTop(self: *This, root_focus: *Focus) bool { 1345 const focused = self.focusedChild(root_focus) orelse return false; 1346 for (0..self.inner().children.count()) |child_index| { 1347 if (!self.childFocusable(child_index)) continue; 1348 return focused == child_index; 1349 } 1350 return false; 1351 } 1352 1353 fn removeEvent(self: *This, allocator: std.mem.Allocator, event_kind: evt.EventKind, id: [evt.event_id_size]u8) !void { 1354 if (comptime wasm) return; 1355 const actor = (try self.session.authorize(self.data.identity, .read)) orelse return; 1356 const author = actor.author; 1357 1358 if (comptime supports_drafts) { 1359 const entry = self.entry orelse return; 1360 if (event_kind == kind and entryDraft(entry)) { 1361 try Data.removeDraft(self.session, allocator, author, &id); 1362 const route = draftsRoute(self.data.identity) orelse return; 1363 try self.session.navigate(route); 1364 return; 1365 } 1366 } 1367 1368 const io = self.session.io orelse return; 1369 const source = self.data.repo_source orelse return; 1370 if (!try source.canModify(io, allocator, actor, event_kind, &id)) return; 1371 1372 switch (source.repo_kind) { 1373 inline else => |repo_kind| { 1374 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, source.localInitOpts()); 1375 defer any_repo.deinit(io, allocator); 1376 switch (any_repo) { 1377 inline else => |*repo| try evt.remove(self.session.eventHost(), .repo, repo_kind, repo.self_repo_opts, io, allocator, repo, &id, event_kind, author), 1378 } 1379 }, 1380 } 1381 1382 const id_hex = std.fmt.bytesToHex(id, .lower); 1383 const route = ui.RoutablePage.repoEventsRoute(self.data.identity, .removed, event_kind, &id_hex, null) orelse return; 1384 try self.session.navigate(route); 1385 } 1386 1387 fn toggleStatus(self: *This, allocator: std.mem.Allocator) !void { 1388 if (comptime (!has_status or wasm)) return; 1389 const entry = self.entry orelse return; 1390 const io = self.session.io orelse return; 1391 const source = self.data.repo_source orelse return; 1392 const actor = (try self.session.authorize(self.data.identity, .read)) orelse return; 1393 const author = actor.author; 1394 const status = (statusChange(entryStatus(entry)) orelse return).status; 1395 const id = try evt.parseEventId(entry.id); 1396 if (!try source.canModify(io, allocator, actor, kind, &id)) return; 1397 1398 switch (source.repo_kind) { 1399 inline else => |repo_kind| { 1400 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, source.localInitOpts()); 1401 defer any_repo.deinit(io, allocator); 1402 switch (any_repo) { 1403 inline else => |*repo| try Event.update(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, &id, .{ .status = status }, author), 1404 } 1405 }, 1406 } 1407 1408 const route = listRoute(self.data.identity, status, "", entry.id) orelse return; 1409 try self.session.navigate(route); 1410 } 1411 }; 1412 } 1413 1414 pub fn View(comptime kind: evt.EventKind, comptime Data: type) type { 1415 const Self = Data; 1416 const HeaderType = Data.Header; 1417 const Window = Data.Window; 1418 const Entry = Data.Entry; 1419 const Event = Data.Event; 1420 const feedback_tag: std.meta.Tag(ui.Session.FormFeedback) = switch (kind) { 1421 .issue => .issue, 1422 .patch => .patch, 1423 .discuss => .discussion, 1424 else => unreachable, 1425 }; 1426 const FeedbackFailure = if (kind == .patch) ui.Session.FormFeedback.PatchFailure else ui.Session.FormFeedback.ThreadFailure; 1427 const Status = Data.Status; 1428 const has_status = @hasField(Event, "status"); 1429 const supports_conflicts = Event.merge_policy == .field_conflicts; 1430 const supports_drafts = @hasField(Self, "drafts"); 1431 const supports_merge = @hasDecl(Data, "mergeRoute"); 1432 const FieldConflict = if (supports_conflicts) Data.FieldConflict else void; 1433 const ViewKind = Data.ViewKind; 1434 const thread_name = Data.thread_name; 1435 const DetailType = Data.Detail; 1436 1437 return struct { 1438 const This = @This(); 1439 // a vertical box: the header tabs on top, then a stack holding a 1440 // master-detail split (thread list + description pane) per status list, 1441 // plus the labels view. 1442 box: wgt.Box(Widget), // vert: [header_index] = tabs, [stack_index] = stack 1443 data: *const Self, 1444 session: *ui.Session, 1445 1446 const header_index: usize = 0; 1447 const stack_index: usize = 1; 1448 // indices within the stack, 1:1 with the header tabs. 1449 const status_count = @typeInfo(Status).@"enum".fields.len; 1450 const labels_view_index: usize = status_count; 1451 // the new-thread form, or the edit, comment, or resolve form when the page was 1452 // loaded at one of their urls. 1453 const form_view_index: usize = labels_view_index + 1; 1454 // the conflicts split; the tab and stack child exist only when the repo 1455 // has conflicted threads. 1456 const drafts_view_index: usize = form_view_index + 1; 1457 const conflict_view_index: usize = drafts_view_index + @intFromBool(supports_drafts); 1458 const view_count: usize = conflict_view_index + @intFromBool(supports_conflicts); 1459 // indices within a split (the horizontal box inside the stack). 1460 const list_index: usize = 0; 1461 const detail_index: usize = 1; 1462 const list_max_width: usize = 35; 1463 const detail_min_width: usize = 40; 1464 1465 pub fn viewIndex(view: ViewKind) usize { 1466 const name = @tagName(view); 1467 if (std.mem.eql(u8, name, "labels")) return labels_view_index; 1468 if (std.mem.eql(u8, name, "new") or std.mem.eql(u8, name, "edit") or std.mem.eql(u8, name, "publish") or std.mem.eql(u8, name, "merge") or std.mem.eql(u8, name, "new_comment") or std.mem.eql(u8, name, "edit_comment") or std.mem.eql(u8, name, "remove") or std.mem.eql(u8, name, "resolve")) return form_view_index; 1469 if (std.mem.eql(u8, name, "conflicts")) return conflict_view_index; 1470 if (std.mem.eql(u8, name, "drafts")) return drafts_view_index; 1471 if (std.mem.eql(u8, name, "description")) unreachable; 1472 inline for (@typeInfo(Status).@"enum".fields, 0..) |field, index| { 1473 if (std.mem.eql(u8, name, field.name)) return index; 1474 } 1475 unreachable; 1476 } 1477 1478 const Confirmation = enum { remove, publish, merge }; 1479 1480 fn confirmation(view: ViewKind) ?Confirmation { 1481 if (view == .remove) return .remove; 1482 if (supports_drafts and std.mem.eql(u8, @tagName(view), "publish")) return .publish; 1483 if (supports_merge and std.mem.eql(u8, @tagName(view), "merge")) return .merge; 1484 return null; 1485 } 1486 1487 // the status a status split lists; the conflicts split has none (its 1488 // threads carry their own). 1489 fn splitStatus(index: usize) Status { 1490 return @enumFromInt(index); 1491 } 1492 1493 fn entryConflicted(entry: Entry) bool { 1494 return if (supports_conflicts) entry.conflicted else false; 1495 } 1496 1497 fn entryStatus(entry: Entry) Status { 1498 if (comptime has_status) return Data.statusKind(entry.record.event); 1499 return @enumFromInt(0); 1500 } 1501 1502 fn entryDraft(entry: Entry) bool { 1503 return if (supports_drafts) entry.draft else false; 1504 } 1505 1506 fn listRoute(identity: []const u8, status: Status, label: []const u8, selected: []const u8) ?ui.RoutablePage { 1507 return Data.listRoute(identity, status, label, selected); 1508 } 1509 1510 fn draftsRoute(identity: []const u8) ?ui.RoutablePage { 1511 return if (supports_drafts) Data.draftsRoute(identity) else null; 1512 } 1513 1514 fn publishRoute(identity: []const u8, selected: []const u8) ?ui.RoutablePage { 1515 return if (supports_drafts) Data.publishRoute(identity, selected) else null; 1516 } 1517 1518 fn mergeRoute(identity: []const u8, selected: []const u8) ?ui.RoutablePage { 1519 return if (supports_merge) Data.mergeRoute(identity, selected) else null; 1520 } 1521 1522 fn conflictsRoute(identity: []const u8, selected: []const u8) ?ui.RoutablePage { 1523 return if (supports_conflicts) Data.conflictsRoute(identity, selected) else null; 1524 } 1525 1526 fn resolveRoute(identity: []const u8, selected: []const u8, picks: []const u8) ?ui.RoutablePage { 1527 return if (supports_conflicts) Data.resolveRoute(identity, selected, picks) else null; 1528 } 1529 1530 // a list link keeps the filters the page carries: the label, and the 1531 // search query when one is set 1532 fn listLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, status: Status, label: []const u8, id: []const u8, search: []const u8) ![]const u8 { 1533 var route = listRoute(identity, status, label, id) orelse return error.RouteTooLong; 1534 if (search.len != 0) route = route.withSearch(search) orelse return error.RouteTooLong; 1535 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{try route.toUrl(page_arena)}); 1536 } 1537 1538 fn rowLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, id: []const u8) ![]const u8 { 1539 const selected = std.mem.eql(u8, id, data.selected_id); 1540 const route = (if (selected and data.description_page) 1541 ui.RoutablePage.repoThreadDescriptionRoute(kind, data.identity, id) 1542 else if (selected) 1543 if (data.comment_page) |page| 1544 ui.RoutablePage.repoThreadCommentRoute(kind, data.identity, id, &page.selected.id, page.replies.start) 1545 else 1546 ui.RoutablePage.repoThreadCommentsRoute(kind, data.identity, id, data.comments_start) 1547 else 1548 ui.RoutablePage.repoThreadCommentsRoute(kind, data.identity, id, 0)) orelse return error.RouteTooLong; 1549 return std.fmt.allocPrint(page_arena.allocator(), "ai:{s}", .{try route.toUrl(page_arena)}); 1550 } 1551 1552 fn windowLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, status_maybe: ?Status, drafts: bool, id: []const u8) ![]const u8 { 1553 if (supports_conflicts and status_maybe == null and !drafts) { 1554 const route = conflictsRoute(data.identity, id) orelse return error.RouteTooLong; 1555 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{try route.toUrl(page_arena)}); 1556 } 1557 if (drafts and id.len == 0) { 1558 const route = draftsRoute(data.identity) orelse return error.RouteTooLong; 1559 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{try route.toUrl(page_arena)}); 1560 } 1561 return listLink(page_arena, data.identity, status_maybe orelse @enumFromInt(0), data.label, id, data.search orelse ""); 1562 } 1563 1564 fn labelLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, status: Status, label: []const u8, search: []const u8) ![]const u8 { 1565 const encoded = try ui.urlEncodeRef(page_arena.allocator(), label); 1566 return listLink(page_arena, identity, status, encoded, "", search); 1567 } 1568 1569 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !This { 1570 var outer = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 1571 errdefer outer.deinit(allocator); 1572 1573 // the tabs at the top. 1574 { 1575 var hdr = try Data.initHeader(allocator, session, data); 1576 errdefer hdr.deinit(allocator); 1577 try outer.children.put(allocator, hdr.getFocus().id, .{ .widget = @unionInit(Widget, Data.header_widget_name, hdr), .rect = null, .min_size = null }); 1578 } 1579 1580 // the stack enters `outer` before its children enter it, so an error 1581 // frees each child exactly once. 1582 { 1583 var stack = try wgt.Stack(Widget).init(allocator); 1584 errdefer stack.deinit(allocator); 1585 try outer.children.put(allocator, stack.getFocus().id, .{ .widget = .{ .stack = stack }, .rect = null, .min_size = null }); 1586 } 1587 const stack = &outer.children.values()[stack_index].widget.stack; 1588 1589 inline for (@typeInfo(Status).@"enum".fields) |field| { 1590 var split = try initSplit(allocator, session, data, @enumFromInt(field.value), false); 1591 errdefer split.deinit(allocator); 1592 try stack.children.put(allocator, split.getFocus().id, .{ .box = split }); 1593 } 1594 1595 // the labels view 1596 { 1597 var tf = try WordFlow.init(allocator); 1598 errdefer tf.deinit(allocator); 1599 var items: std.ArrayList(WordFlow.Item) = .empty; 1600 defer items.deinit(allocator); 1601 // when filtered, the first item clears the filter 1602 if (data.label.len != 0) 1603 try items.append(allocator, .{ .text = "✕", .link = try listLink(session.page_arena, data.identity, @enumFromInt(0), "", "", data.search orelse "") }); 1604 for (data.labels) |label| 1605 try items.append(allocator, .{ .text = label, .link = try labelLink(session.page_arena, data.identity, @enumFromInt(0), label, data.search orelse "") }); 1606 try tf.setItems(allocator, items.items); 1607 try stack.children.put(allocator, tf.getFocus().id, .{ .word_flow = tf }); 1608 } 1609 1610 // the new-thread form, or — on an edit, comment, or resolve url — that form in 1611 // its place, prefilled with the selected thread's content. a 1612 // logged-out session can't create events, so the unauthorized view 1613 // stands in. 1614 if (session.data.host_kind == .local or session.data.user_id != null) { 1615 if (confirmation(data.view)) |confirmation_kind| { 1616 const aa = session.page_arena.allocator(); 1617 const route = switch (confirmation_kind) { 1618 .remove => ui.RoutablePage.repoThreadRemoveRoute(kind, data.identity, data.selected_id, data.comment_id), 1619 .publish => publishRoute(data.identity, data.selected_id), 1620 .merge => mergeRoute(data.identity, data.selected_id), 1621 } orelse return error.RouteTooLong; 1622 const page_url = try route.toUrl(session.page_arena); 1623 const action = try std.fmt.allocPrint(aa, "form:{s}", .{page_url}); 1624 const event_name = if (data.comment_id.len == 0) thread_name else "comment"; 1625 var label_buf: ["remove ".len + @max(thread_name.len, "comment".len)]u8 = undefined; 1626 var buttons: [2]ConfirmationButton = undefined; 1627 buttons[0] = .{ .label = switch (confirmation_kind) { 1628 .remove => try std.fmt.bufPrint(&label_buf, "remove {s}", .{event_name}), 1629 .publish => "publish patch", 1630 .merge => "merge patch", 1631 } }; 1632 var button_count: usize = 1; 1633 if (confirmation_kind == .merge) { 1634 if (comptime supports_merge) { 1635 const availability = if (data.selectedThread()) |entry| 1636 if (Data.canMerge(entry.*, session)) entry.mergeability else pch.Mergeability{} 1637 else 1638 pch.Mergeability{}; 1639 buttons[0].bottom_label = mergeBottomLabel(availability.source); 1640 if (availability.source != .clean) buttons[0].action = ""; 1641 buttons[1] = .{ 1642 .label = "squash and merge patch", 1643 .bottom_label = mergeBottomLabel(availability.squash), 1644 .action = if (availability.squash == .clean) try std.fmt.allocPrint(aa, "{s}{s}/squash", .{ ui.submit_action_prefix, page_url }) else "", 1645 }; 1646 button_count += 1; 1647 } else unreachable; 1648 } 1649 var center = try initConfirmationForm(allocator, action, buttons[0..button_count]); 1650 errdefer center.deinit(allocator); 1651 try stack.children.put(allocator, center.getFocus().id, .{ .center = center }); 1652 } else if (data.view == .new_comment or data.view == .edit_comment) { 1653 const editing = data.view == .edit_comment; 1654 const route = if (editing) 1655 ui.RoutablePage.repoThreadCommentEditRoute(kind, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong 1656 else 1657 ui.RoutablePage.repoThreadCommentNewRoute(kind, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong; 1658 const page_url = try route.toUrl(session.page_arena); 1659 const action = try std.fmt.allocPrint(session.page_arena.allocator(), "form:{s}", .{page_url}); 1660 const page = data.comment_page; 1661 const top_level = if (page) |p| std.mem.eql(u8, &p.selected.comment.event.parent_id, &p.selected.comment.event.thread_id) else false; 1662 const parent_route = if (editing) 1663 if (top_level) 1664 ui.RoutablePage.repoThreadCommentsRoute(kind, data.identity, data.selected_id, 0) 1665 else if (page) |p| 1666 ui.RoutablePage.repoThreadCommentRoute(kind, data.identity, data.selected_id, &p.selected.comment.event.parent_id, 0) 1667 else 1668 null 1669 else if (page) |p| 1670 ui.RoutablePage.repoThreadCommentRoute(kind, data.identity, data.selected_id, &p.selected.id, 0) 1671 else 1672 ui.RoutablePage.repoThreadCommentsRoute(kind, data.identity, data.selected_id, 0); 1673 const parent_author: ui.Author = if (editing) 1674 if (top_level) 1675 if (data.selectedThread()) |entry| entry.author else .unknown 1676 else if (page) |p| 1677 p.selected.parent_author orelse .unknown 1678 else 1679 .unknown 1680 else if (page) |p| 1681 p.selected.author 1682 else if (data.selectedThread()) |entry| 1683 entry.author 1684 else 1685 .unknown; 1686 const initial_body: ?[]const u8 = if (editing) if (page) |p| p.selected.comment.event.body else null else null; 1687 var form = try initCommentForm(allocator, session, action, parent_author, parent_route orelse return error.RouteTooLong, initial_body); 1688 errdefer form.deinit(allocator); 1689 try stack.children.put(allocator, form.getFocus().id, .{ .box = form }); 1690 } else if (supports_conflicts and data.view == .resolve) { 1691 // on the web the page grows to the form's height and the 1692 // browser scrolls it; the terminal scrolls the form itself 1693 var form_widget: Widget = blk: { 1694 var form = try initResolveForm(allocator, session, data); 1695 errdefer form.deinit(allocator); 1696 break :blk if (session.is_terminal) 1697 .{ .scroll = try wgt.Scroll(Widget).init(allocator, .{ .box = form }, .{ .direction = .vert }) } 1698 else 1699 .{ .box = form }; 1700 }; 1701 errdefer form_widget.deinit(allocator); 1702 try stack.children.put(allocator, form_widget.getFocus().id, form_widget); 1703 } else { 1704 const aa = session.page_arena.allocator(); 1705 const route = if (data.view == .edit) 1706 ui.RoutablePage.repoThreadEditRoute(kind, data.identity, data.selected_id) orelse return error.RouteTooLong 1707 else 1708 ui.RoutablePage.repoThreadNewRoute(kind, data.identity) orelse return error.RouteTooLong; 1709 const action = try std.fmt.allocPrint(aa, "form:{s}", .{try route.toUrl(session.page_arena)}); 1710 const record = if (data.view == .edit) 1711 (if (data.selectedThread()) |entry| &entry.record else null) 1712 else 1713 null; 1714 var form = try initThreadForm(allocator, session, data, action, record); 1715 errdefer form.deinit(allocator); 1716 try stack.children.put(allocator, form.getFocus().id, .{ .box = form }); 1717 } 1718 } else { 1719 var message = try ui.Unauthorized.View.init(allocator); 1720 errdefer message.deinit(allocator); 1721 try stack.children.put(allocator, message.getFocus().id, .{ .unauthorized = message }); 1722 } 1723 1724 if (supports_drafts) { 1725 var split = try initSplit(allocator, session, data, null, true); 1726 errdefer split.deinit(allocator); 1727 try stack.children.put(allocator, split.getFocus().id, .{ .box = split }); 1728 } 1729 1730 // the conflicts split; its tab exists only when the repo has 1731 // conflicts, so skip the child too to keep the stack 1:1 with the 1732 // tabs. 1733 if (supports_conflicts and data.conflicts.count > 0) { 1734 var split = try initSplit(allocator, session, data, null, false); 1735 errdefer split.deinit(allocator); 1736 try stack.children.put(allocator, split.getFocus().id, .{ .box = split }); 1737 } 1738 1739 // the stack starts on the page's view. 1740 stack.getFocus().child_id = stack.children.keys()[viewIndex(data.view)]; 1741 1742 // focus entering the view lands on the tabs first. 1743 outer.getFocus().child_id = outer.children.keys()[header_index]; 1744 1745 return .{ 1746 .box = outer, 1747 .data = data, 1748 .session = session, 1749 }; 1750 } 1751 1752 // the master-detail split showing `status`'s window, or the conflicts 1753 // window when null. 1754 fn initSplit(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self, status_maybe: ?Status, drafts: bool) !wgt.Box(Widget) { 1755 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 1756 errdefer box.deinit(allocator); 1757 1758 const win = if (status_maybe) |status| data.window(status) else if (drafts and supports_drafts) &data.drafts else if (supports_conflicts) &data.conflicts else unreachable; 1759 1760 // the thread list (one focusable row per title), plus a "next" link that 1761 // reloads the page rooted at the following thread. 1762 { 1763 var list_scroll = blk: { 1764 var list_box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .vert, .stretch = true }); 1765 errdefer list_box.deinit(allocator); 1766 if (win.prev_id) |prev| 1767 try addRow(allocator, &list_box, "← previous", "", try windowLink(session.page_arena, data, status_maybe, drafts, prev)); 1768 for (win.items) |entry| { 1769 const label: []const u8 = if (entry.comments.count > 0) 1770 try std.fmt.allocPrint(session.page_arena.allocator(), " {d} comments ", .{entry.comments.count}) 1771 else 1772 ""; 1773 try addRow(allocator, &list_box, entry.record.event.title, label, try rowLink(session.page_arena, data, entry.id)); 1774 } 1775 if (win.next_id) |next| 1776 try addRow(allocator, &list_box, "next →", "", try windowLink(session.page_arena, data, status_maybe, drafts, next)); 1777 // select the window's first thread (past a leading "previous" 1778 // row) so its description shows on load. 1779 if (win.items.len > 0) 1780 list_box.getFocus().child_id = list_box.children.keys()[if (win.prev_id != null) 1 else 0] 1781 else if (list_box.children.count() > 0) 1782 list_box.getFocus().child_id = list_box.children.keys()[0]; 1783 break :blk try wgt.Scroll(Widget).init(allocator, .{ .box = list_box }, .{ .direction = .vert, .web_native = !session.is_terminal }); 1784 }; 1785 errdefer list_scroll.deinit(allocator); 1786 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 } }); 1787 } 1788 1789 // frame the detail pane inside the master-detail split 1790 { 1791 var frame = try wgt.Box(Widget).init(allocator, .{ .border_style = .hidden, .direction = .vert }); 1792 errdefer frame.deinit(allocator); 1793 frame.getFocus().mode = .mouse; 1794 var detail_view = try DetailType.init(allocator, data, session, null, .{}); 1795 errdefer detail_view.deinit(allocator); 1796 frame.getFocus().child_id = detail_view.getFocus().id; 1797 try frame.children.put(allocator, detail_view.getFocus().id, .{ .widget = @unionInit(Widget, Data.detail_widget_name, detail_view), .rect = null, .min_size = null }); 1798 try box.children.put(allocator, frame.getFocus().id, .{ .widget = .{ .box = frame }, .rect = null, .min_size = .{ .width = detail_min_width, .height = null } }); 1799 } 1800 1801 box.getFocus().child_id = box.children.keys()[list_index]; 1802 return box; 1803 } 1804 1805 // a thread form, plus the target branch for patches, prefilled from 1806 // `record` when given. its form: subtree makes the web renderer POST 1807 // the inputs to `action`'s route. 1808 fn initThreadForm(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self, action: []const u8, record: ?*const Event.Record) !wgt.Box(Widget) { 1809 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 1810 errdefer box.deinit(allocator); 1811 box.getFocus().kind = .{ .custom = action }; 1812 1813 const saved_form = session.formFeedback(feedback_tag); 1814 const saved_fields = if (saved_form) |saved| saved.fields else null; 1815 1816 { 1817 var title = try wgt.TextInput.init(allocator, .{ .label = " title ", .name = "title", .visible_width = null, .round_corners = true, .render_content = session.is_terminal }); 1818 errdefer title.deinit(allocator); 1819 title.getFocus().mode = .all; 1820 if (saved_fields) |saved| 1821 try title.setContent(allocator, saved.title) 1822 else if (record) |r| 1823 try title.setContent(allocator, r.event.title); 1824 try box.children.put(allocator, title.getFocus().id, .{ .widget = .{ .text_input = title }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 1825 box.getFocus().child_id = title.getFocus().id; 1826 } 1827 1828 { 1829 var labels = try wgt.TextInput.init(allocator, .{ .label = " labels (separate with spaces) ", .name = "labels", .visible_width = null, .round_corners = true, .render_content = session.is_terminal }); 1830 errdefer labels.deinit(allocator); 1831 labels.getFocus().mode = .all; 1832 if (saved_fields) |saved| 1833 try labels.setContent(allocator, saved.labels) 1834 else if (record) |r| 1835 try labels.setContent(allocator, r.event.labels); 1836 try box.children.put(allocator, labels.getFocus().id, .{ .widget = .{ .text_input = labels }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 1837 } 1838 1839 { 1840 var description = try wgt.TextInput.init(allocator, .{ .label = " description ", .name = "description", .visible_width = null, .round_corners = true, .render_content = session.is_terminal, .multiline = true, .scroll = .{ .fill = true } }); 1841 errdefer description.deinit(allocator); 1842 description.getFocus().mode = .all; 1843 if (saved_fields) |saved| 1844 try description.setContent(allocator, saved.description) 1845 else if (record) |r| 1846 try description.setContent(allocator, r.event.description); 1847 try box.children.put(allocator, description.getFocus().id, .{ .widget = .{ .text_input = description }, .rect = null, .min_size = null }); 1848 } 1849 1850 if (supports_drafts and record == null) { 1851 var source = try Data.Source.init(allocator, session, if (saved_fields) |saved| saved.source_branch else "", if (saved_fields) |saved| saved.source_kind else .fork); 1852 errdefer source.deinit(allocator); 1853 try box.children.put(allocator, source.getFocus().id, .{ .widget = .{ .patch_source = source }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 1854 } 1855 1856 if (supports_drafts) { 1857 var target = try wgt.TextInput.init(allocator, .{ .label = " target branch ", .name = "target_branch", .visible_width = null, .round_corners = true, .render_content = session.is_terminal }); 1858 errdefer target.deinit(allocator); 1859 target.getFocus().mode = .all; 1860 const content = if (saved_fields) |saved| saved.target_branch else if (record) |r| r.event.target_branch else data.default_target_branch; 1861 try target.setContent(allocator, content); 1862 try box.children.put(allocator, target.getFocus().id, .{ .widget = .{ .text_input = target }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 1863 } 1864 1865 try addSubmitButtonLabeled(allocator, &box, if (supports_drafts and record == null) "submit draft" else "submit"); 1866 1867 return box; 1868 } 1869 1870 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(Widget) { 1871 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 1872 errdefer box.deinit(allocator); 1873 box.getFocus().kind = .{ .custom = action }; 1874 1875 { 1876 var parent = try ui.authorBox(allocator, session.page_arena, author); 1877 errdefer parent.deinit(allocator); 1878 parent.options.label = " replying to "; 1879 parent.getFocus().kind = .{ .custom = try std.fmt.allocPrint(session.page_arena.allocator(), "a:{s}", .{try parent_route.toUrl(session.page_arena)}) }; 1880 try box.children.put(allocator, parent.getFocus().id, .{ .widget = .{ .text_box = parent }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 1881 box.getFocus().child_id = parent.getFocus().id; 1882 } 1883 1884 { 1885 var body = try wgt.TextInput.init(allocator, .{ 1886 .label = " comment ", 1887 .name = "body", 1888 .visible_width = null, 1889 .round_corners = true, 1890 .render_content = session.is_terminal, 1891 .multiline = true, 1892 .visible_height = 5, 1893 .scroll = .{ .fill = true }, 1894 }); 1895 errdefer body.deinit(allocator); 1896 body.getFocus().mode = .all; 1897 if (initial_body) |text| try body.setContent(allocator, text); 1898 try box.children.put(allocator, body.getFocus().id, .{ .widget = .{ .text_input = body }, .rect = null, .min_size = null }); 1899 } 1900 1901 try addSubmitButtonLabeled(allocator, &box, "submit"); 1902 return box; 1903 } 1904 1905 const ConfirmationButton = struct { 1906 label: []const u8, 1907 bottom_label: []const u8 = "", 1908 action: []const u8 = "submit", 1909 }; 1910 1911 fn initConfirmationForm(allocator: std.mem.Allocator, action: []const u8, buttons: []const ConfirmationButton) !Center { 1912 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .round_corners = true, .direction = .vert }); 1913 errdefer box.deinit(allocator); 1914 box.getFocus().kind = .{ .custom = action }; 1915 1916 var prompt = try wgt.Text.init(allocator, "are you sure?"); 1917 errdefer prompt.deinit(allocator); 1918 try box.children.put(allocator, prompt.getFocus().id, .{ .widget = .{ .text = prompt }, .rect = null, .min_size = null }); 1919 1920 for (buttons, 0..) |options, index| { 1921 var button = try wgt.TextBox.init(allocator, options.label, .{ .border_style = .single, .round_corners = true, .wrap_kind = .none, .bottom_label = options.bottom_label }); 1922 errdefer button.deinit(allocator); 1923 button.getFocus().mode = .all; 1924 button.getFocus().kind = .{ .custom = options.action }; 1925 try box.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = null }); 1926 if (index == 0) box.getFocus().child_id = button.getFocus().id; 1927 } 1928 1929 return Center.init(allocator, .{ .box = box }); 1930 } 1931 1932 // a form's submit button, then a spacer absorbing the leftover 1933 // min-height the box hands its last child, so the button keeps its 1934 // natural height 1935 fn addSubmitButtonLabeled(allocator: std.mem.Allocator, box: *wgt.Box(Widget), label: []const u8) !void { 1936 { 1937 var submit = try SubmitButton.initLabeled(allocator, label); 1938 errdefer submit.deinit(allocator); 1939 try box.children.put(allocator, submit.getFocus().id, .{ .widget = .{ .submit_button = submit }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 1940 } 1941 { 1942 var spacer = try Spacer.init(allocator); 1943 errdefer spacer.deinit(allocator); 1944 try box.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 1945 } 1946 } 1947 1948 // the conflict resolve form. the "use this" links are navigations that 1949 // flip the url's theirs: pick, reloading the prefills; one submit 1950 // settles every conflict. the form: subtree makes the web renderer POST 1951 // to the resolve route. 1952 fn initResolveForm(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self) !wgt.Box(Widget) { 1953 const aa = session.page_arena.allocator(); 1954 // init only picks the resolve view once it has read the conflict 1955 const conflict = if (data.conflict) |*c| c else unreachable; 1956 1957 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 1958 errdefer box.deinit(allocator); 1959 const route = resolveRoute(data.identity, data.selected_id, data.theirs_picks) orelse return error.RouteTooLong; 1960 box.getFocus().kind = .{ .custom = try std.fmt.allocPrint(aa, "form:{s}", .{try route.toUrl(session.page_arena)}) }; 1961 1962 if (conflict.title) |*fc| { 1963 try addLabel(allocator, &box, "title conflict:"); 1964 try addFieldConflict(allocator, &box, session, data, "title", fc); 1965 } 1966 if (conflict.labels) |*fc| { 1967 try addLabel(allocator, &box, "labels conflict:"); 1968 try addFieldConflict(allocator, &box, session, data, "labels", fc); 1969 } 1970 if (comptime @hasField(Data.Conflict, "status")) { 1971 if (conflict.status) |*fc| { 1972 try addLabel(allocator, &box, "status conflict:"); 1973 try addAtomicConflict(allocator, &box, session, data, "status", fc); 1974 } 1975 } 1976 if (comptime @hasField(Data.Conflict, "revision")) { 1977 if (conflict.revision) |*fc| { 1978 try addLabel(allocator, &box, "revision conflict:"); 1979 try addAtomicConflict(allocator, &box, session, data, "revision", fc); 1980 } 1981 if (conflict.target_branch) |*fc| { 1982 try addLabel(allocator, &box, "target branch conflict:"); 1983 try addAtomicConflict(allocator, &box, session, data, "target_branch", fc); 1984 } 1985 } 1986 1987 if (conflict.description) |*desc| { 1988 try addLabel(allocator, &box, "description conflict:"); 1989 var hunk_index: usize = 0; 1990 for (desc.chunks, 0..) |*chunk, chunk_index| { 1991 switch (chunk.*) { 1992 .same => |text| { 1993 var tb = try wgt.TextBox.init(allocator, text, .{ .border_style = .single, .round_corners = true, .wrap_kind = .word }); 1994 errdefer tb.deinit(allocator); 1995 tb.getFocus().mode = .all; 1996 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1997 }, 1998 // an auto-resolved chunk: only one side changed it, so its 1999 // version stands, shown distinctly with its author 2000 .auto => |auto| { 2001 const author = if (auto.theirs) desc.theirs_author else desc.ours_author; 2002 const verb: []const u8 = if (auto.text == null) "removed" else "edited"; 2003 const label = switch (author) { 2004 .user_name, .email => |name| try std.fmt.allocPrint(aa, " {s} by {s} ", .{ verb, name }), 2005 .unknown => try std.fmt.allocPrint(aa, " {s} by {s} ", .{ verb, if (auto.theirs) @as([]const u8, "them") else "us" }), 2006 }; 2007 var tb = try wgt.TextBox.init(allocator, auto.text orelse "(removed)", .{ .border_style = .single, .round_corners = true, .wrap_kind = .word, .label = label }); 2008 errdefer tb.deinit(allocator); 2009 tb.getFocus().mode = .all; 2010 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 2011 }, 2012 .conflict => |hunk| { 2013 // a blank row on each side sets the group apart from 2014 // the flowing shared and auto-resolved chunks 2015 if (chunk_index > 0) try addGap(allocator, &box); 2016 const name = try std.fmt.allocPrint(aa, "d{d}", .{hunk_index}); 2017 hunk_index += 1; 2018 const picked_theirs = data.theirsPicked(name); 2019 try addVersionRow(allocator, &box, try sideLabel(aa, desc.ours_author, true), hunk.ours orelse "", try useThisLink(session, data, name, false)); 2020 try addVersionRow(allocator, &box, try sideLabel(aa, desc.theirs_author, false), hunk.theirs orelse "", try useThisLink(session, data, name, true)); 2021 var resolution_input = try wgt.TextInput.init(allocator, .{ .label = " resolution ", .name = name, .visible_width = null, .round_corners = true, .render_content = session.is_terminal, .multiline = true }); 2022 errdefer resolution_input.deinit(allocator); 2023 resolution_input.getFocus().mode = .all; 2024 try resolution_input.setContent(allocator, (if (picked_theirs) hunk.theirs else hunk.ours) orelse ""); 2025 try box.children.put(allocator, resolution_input.getFocus().id, .{ .widget = .{ .text_input = resolution_input }, .rect = null, .min_size = null }); 2026 if (chunk_index + 1 < desc.chunks.len) try addGap(allocator, &box); 2027 }, 2028 } 2029 } 2030 } 2031 2032 try addSubmitButtonLabeled(allocator, &box, "submit"); 2033 2034 box.getFocus().child_id = box.children.keys()[0]; 2035 return box; 2036 } 2037 2038 // one conflicted scalar field: both sides' version rows, then the 2039 // resolution input prefilled from the picked side. 2040 fn addFieldConflict(allocator: std.mem.Allocator, box: *wgt.Box(Widget), session: *ui.Session, data: *const Self, comptime name: []const u8, fc: *const FieldConflict) !void { 2041 const aa = session.page_arena.allocator(); 2042 try addVersionRow(allocator, box, try sideLabel(aa, fc.ours.author, true), fc.ours.text, try useThisLink(session, data, name, false)); 2043 try addVersionRow(allocator, box, try sideLabel(aa, fc.theirs.author, false), fc.theirs.text, try useThisLink(session, data, name, true)); 2044 2045 var resolution_input = try wgt.TextInput.init(allocator, .{ .label = " resolution ", .name = name, .visible_width = null, .round_corners = true, .render_content = session.is_terminal }); 2046 errdefer resolution_input.deinit(allocator); 2047 resolution_input.getFocus().mode = .all; 2048 try resolution_input.setContent(allocator, if (data.theirsPicked(name)) fc.theirs.text else fc.ours.text); 2049 try box.children.put(allocator, resolution_input.getFocus().id, .{ .widget = .{ .text_input = resolution_input }, .rect = null, .min_size = null }); 2050 } 2051 2052 // atomic fields can only pick one complete side 2053 fn addAtomicConflict(allocator: std.mem.Allocator, box: *wgt.Box(Widget), session: *ui.Session, data: *const Self, comptime name: []const u8, fc: *const FieldConflict) !void { 2054 const aa = session.page_arena.allocator(); 2055 try addVersionRow(allocator, box, try sideLabel(aa, fc.ours.author, true), fc.ours.text, try useThisLink(session, data, name, false)); 2056 try addVersionRow(allocator, box, try sideLabel(aa, fc.theirs.author, false), fc.theirs.text, try useThisLink(session, data, name, true)); 2057 var selected = try wgt.TextBox.init(allocator, if (data.theirsPicked(name)) fc.theirs.text else fc.ours.text, .{ 2058 .border_style = .single, 2059 .round_corners = true, 2060 .wrap_kind = .word, 2061 .label = " resolution ", 2062 }); 2063 errdefer selected.deinit(allocator); 2064 selected.getFocus().mode = .all; 2065 try box.children.put(allocator, selected.getFocus().id, .{ .widget = .{ .text_box = selected }, .rect = null, .min_size = null }); 2066 } 2067 2068 // a blank row setting a conflict group apart from its neighbors 2069 fn addGap(allocator: std.mem.Allocator, box: *wgt.Box(Widget)) !void { 2070 var text = try wgt.Text.init(allocator, " "); 2071 errdefer text.deinit(allocator); 2072 try box.children.put(allocator, text.getFocus().id, .{ .widget = .{ .text = text }, .rect = null, .min_size = null }); 2073 } 2074 2075 fn addLabel(allocator: std.mem.Allocator, box: *wgt.Box(Widget), content: []const u8) !void { 2076 var label = try SectionLabel.init(allocator, content); 2077 errdefer label.deinit(allocator); 2078 try box.children.put(allocator, label.getFocus().id, .{ .widget = .{ .section_label = label }, .rect = null, .min_size = null }); 2079 } 2080 2081 // one side of a conflict: the "use this" link on the left, the version 2082 // itself as a labeled read-only box. 2083 fn addVersionRow(allocator: std.mem.Allocator, box: *wgt.Box(Widget), label: []const u8, text: []const u8, link: []const u8) !void { 2084 var row = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 2085 errdefer row.deinit(allocator); 2086 2087 { 2088 const use_label = "use this"; 2089 var use = try wgt.TextBox.init(allocator, use_label, .{ .border_style = .single, .round_corners = true, .wrap_kind = .none }); 2090 errdefer use.deinit(allocator); 2091 use.getFocus().mode = .all; 2092 use.getFocus().kind = .{ .custom = link }; 2093 try row.children.put(allocator, use.getFocus().id, .{ 2094 .widget = .{ .text_box = use }, 2095 .rect = null, 2096 .min_size = .{ .width = use_label.len + 2, .height = null }, 2097 .max_size = .{ .width = use_label.len + 2, .height = null }, 2098 }); 2099 } 2100 2101 { 2102 var tb = try wgt.TextBox.init(allocator, if (text.len > 0) text else "(removed)", .{ .border_style = .single, .round_corners = true, .wrap_kind = .word, .label = label }); 2103 errdefer tb.deinit(allocator); 2104 tb.getFocus().mode = .all; 2105 try row.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 2106 } 2107 2108 row.getFocus().child_id = row.children.keys()[0]; 2109 try box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .box = row }, .rect = null, .min_size = null }); 2110 } 2111 2112 // the label of one side's version box, naming its author 2113 fn sideLabel(aa: std.mem.Allocator, author: ui.Author, ours: bool) ![]const u8 { 2114 return switch (author) { 2115 .user_name, .email => |name| try std.fmt.allocPrint(aa, " edited by {s} ", .{name}), 2116 .unknown => if (ours) " current version " else " their version ", 2117 }; 2118 } 2119 2120 // the a: link that reloads the resolve page with `name` picked from ours 2121 // or theirs, reprefilling its resolution input 2122 fn useThisLink(session: *ui.Session, data: *const Self, name: []const u8, theirs: bool) ![]const u8 { 2123 const aa = session.page_arena.allocator(); 2124 var picks: std.ArrayList(u8) = .empty; 2125 var it = std.mem.splitScalar(u8, data.theirs_picks, ','); 2126 while (it.next()) |pick| { 2127 if (pick.len == 0 or std.mem.eql(u8, pick, name)) continue; 2128 if (picks.items.len > 0) try picks.append(aa, ','); 2129 try picks.appendSlice(aa, pick); 2130 } 2131 if (theirs) { 2132 if (picks.items.len > 0) try picks.append(aa, ','); 2133 try picks.appendSlice(aa, name); 2134 } 2135 const route = resolveRoute(data.identity, data.selected_id, picks.items) orelse return error.RouteTooLong; 2136 return std.fmt.allocPrint(aa, "a:{s}", .{try route.toUrl(session.page_arena)}); 2137 } 2138 2139 fn addRow(allocator: std.mem.Allocator, box: *wgt.Box(Widget), text: []const u8, bottom_label: []const u8, link: []const u8) !void { 2140 var row = try wgt.TextBox.init(allocator, text, .{ .border_style = .hidden, .round_corners = true, .wrap_kind = .word, .bottom_label = bottom_label }); 2141 errdefer row.deinit(allocator); 2142 row.getFocus().mode = .all; 2143 if (link.len != 0) row.getFocus().kind = .{ .custom = link }; 2144 try box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .text_box = row }, .rect = null, .min_size = null, .max_size = .{ .width = null, .height = 5 } }); 2145 } 2146 2147 pub fn deinit(self: *This, allocator: std.mem.Allocator) void { 2148 self.box.deinit(allocator); 2149 } 2150 2151 fn header(self: *This) *HeaderType { 2152 return &@field(self.box.children.values()[header_index].widget, Data.header_widget_name); 2153 } 2154 2155 fn viewStack(self: *This) *wgt.Stack(Widget) { 2156 return &self.box.children.values()[stack_index].widget.stack; 2157 } 2158 2159 // `index`'s master-detail split inside the stack. 2160 fn resultsBox(self: *This, index: usize) *wgt.Box(Widget) { 2161 return &self.viewStack().children.values()[index].box; 2162 } 2163 2164 // the labels view's flow inside the stack. 2165 fn labelsView(self: *This) *WordFlow { 2166 return &self.viewStack().children.values()[labels_view_index].word_flow; 2167 } 2168 2169 // the non-confirmation form inside the stack 2170 fn formBox(self: *This) ?*wgt.Box(Widget) { 2171 return switch (self.viewStack().children.values()[form_view_index]) { 2172 .box => |*box| box, 2173 // the resolve form sits inside a scroll on the terminal 2174 .scroll => |*scroll| &scroll.child.box, 2175 else => null, 2176 }; 2177 } 2178 2179 fn threadForm(self: *This) ?*wgt.Box(Widget) { 2180 if (supports_conflicts and self.data.view == .resolve) return null; 2181 return switch (self.data.view) { 2182 .new_comment, .edit_comment => null, 2183 else => self.formBox(), 2184 }; 2185 } 2186 2187 fn formField(form: *wgt.Box(Widget), name: []const u8) !*wgt.TextInput { 2188 for (form.children.values()) |*child| switch (child.widget) { 2189 .text_input => |*text_input| if (std.mem.eql(u8, text_input.options.name, name)) return text_input, 2190 else => {}, 2191 }; 2192 return error.MissingFormField; 2193 } 2194 2195 fn formWidget(form: *wgt.Box(Widget), comptime widget_kind: std.meta.Tag(Widget)) ?*@FieldType(Widget, @tagName(widget_kind)) { 2196 for (form.children.values()) |*child| { 2197 if (child.widget == widget_kind) return &@field(child.widget, @tagName(widget_kind)); 2198 } 2199 return null; 2200 } 2201 2202 fn confirmationForm(self: *This) ?*wgt.Box(Widget) { 2203 return switch (self.viewStack().children.values()[form_view_index]) { 2204 .center => |*center| switch (center.child.*) { 2205 .box => |*box| box, 2206 else => null, 2207 }, 2208 else => null, 2209 }; 2210 } 2211 2212 // the resolve form's scroll on the terminal (the web page scrolls itself) 2213 fn resolveScroll(self: *This) ?*wgt.Scroll(Widget) { 2214 return switch (self.viewStack().children.values()[form_view_index]) { 2215 .scroll => |*scroll| scroll, 2216 else => null, 2217 }; 2218 } 2219 2220 fn listScroll(self: *This, index: usize) *wgt.Scroll(Widget) { 2221 return &self.resultsBox(index).children.values()[list_index].widget.scroll; 2222 } 2223 2224 fn listBox(self: *This, index: usize) *wgt.Box(Widget) { 2225 return &self.listScroll(index).child.box; 2226 } 2227 2228 fn detail(self: *This, index: usize) *DetailType { 2229 const frame = &self.resultsBox(index).children.values()[detail_index].widget.box; 2230 return &@field(frame.children.values()[0].widget, Data.detail_widget_name); 2231 } 2232 2233 fn window(self: *This, index: usize) *const Window { 2234 if (supports_conflicts and index == conflict_view_index) return &self.data.conflicts; 2235 if (supports_drafts and index == drafts_view_index) return &self.data.drafts; 2236 return self.data.window(splitStatus(index)); 2237 } 2238 2239 fn stackSelectedIndex(self: *This) ?usize { 2240 const stack = self.viewStack(); 2241 const cid = stack.getFocus().child_id orelse return null; 2242 return stack.children.getIndex(cid); 2243 } 2244 2245 // the stack's selected master-detail split (null when the labels view or the 2246 // new-thread form shows). 2247 fn selectedSplitIndex(self: *This) ?usize { 2248 const idx = self.stackSelectedIndex() orelse return null; 2249 return if (idx < status_count or (supports_drafts and idx == drafts_view_index) or idx == conflict_view_index) idx else null; 2250 } 2251 2252 fn detailActive(self: *This, index: usize) bool { 2253 const rb = self.resultsBox(index); 2254 const cid = rb.getFocus().child_id orelse return false; 2255 return rb.children.getIndex(cid) == detail_index; 2256 } 2257 2258 fn headerActive(self: *This) bool { 2259 const cid = self.box.getFocus().child_id orelse return false; 2260 return self.box.children.getIndex(cid) == header_index; 2261 } 2262 2263 fn labelsViewActive(self: *This) bool { 2264 if (self.headerActive()) return false; 2265 return self.stackSelectedIndex() == labels_view_index; 2266 } 2267 2268 fn formViewActive(self: *This) bool { 2269 if (self.headerActive()) return false; 2270 return self.stackSelectedIndex() == form_view_index; 2271 } 2272 2273 // the selected thread's index, or null when a window-navigation row is 2274 // selected (a leading "previous" row shifts the thread rows down by one). 2275 fn selectedThreadIndex(self: *This, index: usize) ?usize { 2276 const lb = self.listBox(index); 2277 const cid = lb.getFocus().child_id orelse return null; 2278 const idx = lb.children.getIndex(cid) orelse return null; 2279 const win = self.window(index); 2280 const lead: usize = if (win.prev_id != null) 1 else 0; 2281 if (idx < lead or idx - lead >= win.items.len) return null; 2282 return idx - lead; 2283 } 2284 2285 pub fn build(self: *This, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2286 self.clearGrid(); 2287 2288 // each header tab maps 1:1 to a stack child by position 2289 if (self.header().getSelectedIndex()) |index| { 2290 const stack = self.viewStack(); 2291 stack.getFocus().child_id = stack.children.keys()[index]; 2292 } 2293 2294 if (self.selectedSplitIndex()) |i| { 2295 // swap the detail pane to the selected thread when it changes. 2296 if (self.selectedThreadIndex(i)) |selected| { 2297 const entry = self.window(i).items[selected]; 2298 const changed = if (self.detail(i).entry) |current| !std.mem.eql(u8, current.id, entry.id) else true; 2299 if (changed) { 2300 try self.detail(i).setEntry(allocator, entry); 2301 } 2302 } 2303 2304 // the selected list row shows a border (the focused TextBox 2305 // upgrades it to a double border itself); the rest stay borderless. 2306 const lb = self.listBox(i); 2307 for (lb.children.keys(), lb.children.values()) |id, *child| { 2308 switch (child.widget) { 2309 .text_box => |*tb| widget.markSelected(tb, lb.getFocus().child_id == id), 2310 else => {}, 2311 } 2312 } 2313 2314 // cap the list at list_max_width only while the detail pane fits 2315 // beside it. the box drops the detail when the width can't hold 2316 // both minimums, so when it's that narrow we lift the cap and let 2317 // the list fill the whole width. 2318 const both_panes_fit = if (constraint.max_size.width) |w| w >= list_max_width + detail_min_width else true; 2319 self.resultsBox(i).children.values()[list_index].max_size = if (both_panes_fit) .{ .width = list_max_width, .height = null } else null; 2320 2321 // stretch the detail pane across the rest of the width so it fills 2322 // the area rather than shrinking to its content; its scroll fills 2323 // the pane. 2324 if (constraint.max_size.width) |w| { 2325 self.resultsBox(i).children.values()[detail_index].min_size = .{ .width = if (both_panes_fit) w - list_max_width else w, .height = null }; 2326 } else { 2327 self.resultsBox(i).children.values()[detail_index].min_size = .{ .width = detail_min_width, .height = null }; 2328 } 2329 } 2330 2331 // refresh the form inputs' entries in the session's focus-id -> input 2332 // map with this frame's addresses, so the web/wasm form handling can 2333 // find them by focus id 2334 if (self.threadForm()) |form| { 2335 const failure = if (self.session.formFeedback(feedback_tag)) |saved| saved.failure else null; 2336 (try formField(form, "title")).options.label = if (failure == .required_title) " title (required) " else " title "; 2337 if (comptime supports_drafts) { 2338 if (formWidget(form, .patch_source)) |source| { 2339 source.field().options.label = if (failure == .invalid_source_branch) " source branch (not found) " else if (failure == .same_branch) " source branch (same as target) " else if (failure == .unrelated_branches) " source branch (unrelated) " else " source branch "; 2340 const submit = formWidget(form, .submit_button) orelse return error.MissingFormField; 2341 try submit.setLabel(allocator, switch (source.selectedKind()) { 2342 .fork => "submit draft", 2343 .branch => "submit patch", 2344 }); 2345 } 2346 (try formField(form, "target_branch")).options.label = 2347 if (failure == .invalid_target_branch) " target branch (invalid) " else if (self.data.view == .edit and failure == .same_branch) " target branch (same as source) " else if (self.data.view == .edit and failure == .unrelated_branches) " target branch (unrelated to source) " else if (self.data.view == .edit and failure == .invalid_source_branch) " target branch (source branch not found) " else " target branch "; 2348 } 2349 } 2350 if (self.formBox()) |form| { 2351 const inputs_arena = self.session.arena.allocator(); 2352 for (form.children.values()) |*child| switch (child.widget) { 2353 .text_input => |*ti| try self.session.text_inputs.put(inputs_arena, ti.getFocus().id, ti), 2354 else => {}, 2355 }; 2356 } 2357 2358 try self.box.build(allocator, constraint, root_focus); 2359 } 2360 2361 pub fn input(self: *This, allocator: std.mem.Allocator, raw_key: Key, root_focus: *Focus) !void { 2362 // wheel input moves focus like the arrows 2363 const key: Key = if (raw_key == .mouse and raw_key.mouse.action == .scroll) 2364 (if (raw_key.mouse.action.scroll == .up) .arrow_up else .arrow_down) 2365 else 2366 raw_key; 2367 2368 if (self.headerActive()) { 2369 // down from the sub-header re-enters the stack if the selected 2370 // view has something to focus; enter in the search box submits 2371 // the query; other keys move within the sub-header. 2372 if (inp.vertDirection(key) == .down) { 2373 const enterable = if (self.viewStack().getSelected()) |selected| switch (selected.*) { 2374 .word_flow => |*tf| tf.text_boxes.items.len > 0, 2375 // nothing focusable in the unauthorized view 2376 .unauthorized => false, 2377 else => true, 2378 } else false; 2379 if (enterable) root_focus.setFocus(self.box.children.keys()[stack_index]); 2380 } else if (key == .enter) { 2381 if (try self.header().submittedText(allocator)) |text| { 2382 defer allocator.free(text); 2383 try self.submitSearch(text); 2384 } 2385 } else { 2386 try self.header().input(allocator, key, root_focus); 2387 } 2388 return; 2389 } 2390 if (self.labelsViewActive()) { 2391 self.labelsViewInput(key, root_focus); 2392 return; 2393 } 2394 if (self.formViewActive()) { 2395 if (confirmation(self.data.view) != null) { 2396 try self.confirmationFormInput(allocator, key, root_focus); 2397 } else if (supports_conflicts and self.data.view == .resolve) { 2398 try self.resolveInput(allocator, key, root_focus); 2399 } else { 2400 try self.formInput(allocator, key, root_focus); 2401 } 2402 return; 2403 } 2404 const i = self.selectedSplitIndex() orelse return; 2405 if (self.detailActive(i)) { 2406 switch (try self.detail(i).inputWithExit(allocator, key, root_focus)) { 2407 .none => {}, 2408 .list => self.focusList(i, root_focus), 2409 .header => self.focusHeader(root_focus), 2410 } 2411 } else { 2412 self.listInput(i, key, root_focus); 2413 } 2414 } 2415 2416 // navigate to the typed query's results, keeping the label filter and the 2417 // status list the page shows. an empty box clears a filtered page and 2418 // does nothing on any other one. 2419 fn submitSearch(self: *This, text: []const u8) !void { 2420 if (text.len == 0 and self.data.search == null) return; 2421 const selected = self.header().getSelectedIndex() orelse 0; 2422 const status: Status = if (selected < status_count) splitStatus(selected) else @enumFromInt(0); 2423 const route = listRoute(self.data.identity, status, self.data.label, "") orelse return; 2424 try self.session.navigate(route.withSearch(text) orelse return); 2425 } 2426 2427 // arrow keys move the label selection; up from the top row crosses to the 2428 // header tabs. 2429 fn labelsViewInput(self: *This, key: Key, root_focus: *Focus) void { 2430 const tf = self.labelsView(); 2431 const cid = tf.focus.child_id orelse return; 2432 const cur = tf.indexOfFocusId(cid) orelse return; 2433 const count = tf.text_boxes.items.len; 2434 switch (key) { 2435 .arrow_left => if (cur > 0) root_focus.setFocus(tf.text_boxes.items[cur - 1].getFocus().id), 2436 .arrow_right => if (cur + 1 < count) root_focus.setFocus(tf.text_boxes.items[cur + 1].getFocus().id), 2437 .arrow_up => if (tf.rowStep(cur, false)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id) else self.focusHeader(root_focus), 2438 .arrow_down => if (tf.rowStep(cur, true)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id), 2439 .home => root_focus.setFocus(tf.text_boxes.items[0].getFocus().id), 2440 .end => root_focus.setFocus(tf.text_boxes.items[count - 1].getFocus().id), 2441 else => {}, 2442 } 2443 } 2444 2445 fn listInput(self: *This, index: usize, key: Key, root_focus: *Focus) void { 2446 // up/down (and the scroll wheel) move the selection a row; page up/down 2447 // jump a fixed amount. right/Enter cross into the detail pane. up from 2448 // the top row crosses into the header tabs. 2449 if (inp.rowDelta(key, @intCast(self.listBox(index).children.count()))) |delta| { 2450 const lb = self.listBox(index); 2451 const at_top = if (lb.getFocus().child_id) |cid| lb.children.getIndex(cid) == 0 else true; 2452 if (delta < 0 and at_top) return self.focusHeader(root_focus); 2453 moveRowFocus(lb, self.listScroll(index), root_focus, delta); 2454 return; 2455 } 2456 switch (key) { 2457 .enter, .arrow_right => _ = self.detail(index).focusFirst(root_focus), 2458 else => {}, 2459 } 2460 } 2461 2462 fn formInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2463 const form = self.formBox() orelse return; 2464 const cid = form.getFocus().child_id orelse return; 2465 const cur = form.children.getIndex(cid) orelse return; 2466 const child = &form.children.values()[cur]; 2467 2468 if (child.widget == .text_input) { 2469 const text_input = &child.widget.text_input; 2470 if (text_input.options.multiline and try multilineKeepsKey(text_input, allocator, key)) 2471 return child.widget.input(allocator, key, root_focus); 2472 } 2473 2474 const on_submit = child.widget == .submit_button; 2475 switch (key) { 2476 .arrow_up, .back_tab => return if (formStep(form, cur, false)) |i| 2477 root_focus.setFocus(form.children.keys()[i]) 2478 else 2479 self.focusHeader(root_focus), 2480 .arrow_down, .tab => return if (formStep(form, cur, true)) |i| root_focus.setFocus(form.children.keys()[i]), 2481 .enter => if (on_submit) return self.submitForm(allocator), 2482 .mouse => |mouse| if (on_submit) { 2483 if (inp.leftClickOn(root_focus, child.widget.submit_button.buttonId(), mouse)) return self.submitForm(allocator); 2484 }, 2485 else => {}, 2486 } 2487 try child.widget.input(allocator, key, root_focus); 2488 } 2489 2490 fn confirmationFormInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2491 const form = self.confirmationForm() orelse return; 2492 const child_id = form.getFocus().child_id orelse return; 2493 const index = form.children.getIndex(child_id) orelse return; 2494 switch (key) { 2495 .arrow_up, .back_tab => if (index > 1) 2496 root_focus.setFocus(form.children.keys()[index - 1]) 2497 else 2498 self.focusHeader(root_focus), 2499 .arrow_down, .tab => if (index + 1 < form.children.count()) root_focus.setFocus(form.children.keys()[index + 1]), 2500 .enter => try self.submitConfirmation(allocator, index), 2501 .mouse => |mouse| for (form.children.keys()[1..], 1..) |button_id, button_index| { 2502 if (inp.leftClickOn(root_focus, button_id, mouse)) { 2503 try self.submitConfirmation(allocator, button_index); 2504 break; 2505 } 2506 }, 2507 else => {}, 2508 } 2509 } 2510 2511 // the multiline hunk inputs keep enter and any up/down that has a row to 2512 // move to; enter on a "use this" link is a navigation the host follows 2513 // before this runs. 2514 fn resolveInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2515 const form = self.formBox() orelse return; 2516 const cid = form.getFocus().child_id orelse return; 2517 const cur = form.children.getIndex(cid) orelse return; 2518 const child = &form.children.values()[cur]; 2519 2520 if (child.widget == .text_input) { 2521 const ti = &child.widget.text_input; 2522 if (ti.options.multiline and try multilineKeepsKey(ti, allocator, key)) 2523 return child.widget.input(allocator, key, root_focus); 2524 } 2525 2526 const on_submit = child.widget == .submit_button; 2527 2528 switch (key) { 2529 .arrow_up, .back_tab => if (formStep(form, cur, false)) |i| self.focusResolveChild(form, i, root_focus) else self.focusHeader(root_focus), 2530 .arrow_down, .tab => if (formStep(form, cur, true)) |i| self.focusResolveChild(form, i, root_focus), 2531 .arrow_left, .arrow_right => switch (child.widget) { 2532 .box => |*row| _ = widget.moveInRow(row, root_focus, key == .arrow_right), 2533 else => try child.widget.input(allocator, key, root_focus), 2534 }, 2535 .enter => if (on_submit) try self.submitResolution(allocator), 2536 .mouse => |mouse| if (on_submit) { 2537 if (inp.leftClickOn(root_focus, child.widget.submit_button.buttonId(), mouse)) try self.submitResolution(allocator); 2538 }, 2539 else => try child.widget.input(allocator, key, root_focus), 2540 } 2541 } 2542 2543 // a multiline input keeps enter and any up/down that has a row to move to 2544 fn multilineKeepsKey(text_input: *wgt.TextInput, allocator: std.mem.Allocator, key: Key) !bool { 2545 return switch (key) { 2546 .enter => true, 2547 .arrow_up => !try text_input.cursorOnFirstRow(allocator), 2548 .arrow_down => !try text_input.cursorOnLastRow(allocator), 2549 else => false, 2550 }; 2551 } 2552 2553 // the neighboring form control, skipping spacers and blank rows 2554 fn formStep(form: *wgt.Box(Widget), cur: usize, down: bool) ?usize { 2555 var i = cur; 2556 while (true) { 2557 if (down) { 2558 i += 1; 2559 if (i >= form.children.count()) return null; 2560 } else { 2561 if (i == 0) return null; 2562 i -= 1; 2563 } 2564 const child = &form.children.values()[i]; 2565 if (child.hidden) continue; 2566 switch (child.widget) { 2567 .spacer, .text => continue, 2568 else => return i, 2569 } 2570 } 2571 } 2572 2573 // focus the resolve-form child at `i` (a version row focuses its selected 2574 // child) and keep it visible on the terminal 2575 fn focusResolveChild(self: *This, form: *wgt.Box(Widget), i: usize, root_focus: *Focus) void { 2576 const child = &form.children.values()[i]; 2577 switch (child.widget) { 2578 .box => |*row| root_focus.setFocus(row.getFocus().child_id orelse form.children.keys()[i]), 2579 else => root_focus.setFocus(form.children.keys()[i]), 2580 } 2581 if (self.session.is_terminal) { 2582 if (self.resolveScroll()) |sc| { 2583 if (child.rect) |rect| sc.scrollToRect(rect); 2584 } 2585 } 2586 } 2587 2588 // re-emit the conflicted thread's event with the resolved content; any 2589 // event on the thread settles its conflict entry. this is the terminal 2590 // path; the web posts the form to the resolve route. 2591 fn submitResolution(self: *This, allocator: std.mem.Allocator) !void { 2592 if (comptime wasm) return; 2593 const io = self.session.io orelse return; 2594 const src = self.data.repo_source orelse return; 2595 const entry = self.data.selectedThread() orelse return; 2596 const author = ((try self.session.authorize(self.data.identity, .write)) orelse return).author; 2597 const form = self.formBox() orelse return; 2598 2599 // gather the inputs by name; the d<n> hunk inputs appear in chunk order 2600 var title: ?[]u8 = null; 2601 var labels: ?[]u8 = null; 2602 var hunks: std.ArrayList([]const u8) = .empty; 2603 defer { 2604 if (title) |t| allocator.free(t); 2605 if (labels) |t| allocator.free(t); 2606 for (hunks.items) |h| allocator.free(h); 2607 hunks.deinit(allocator); 2608 } 2609 for (form.children.values()) |*child| switch (child.widget) { 2610 .text_input => |*ti| { 2611 const text = try ti.text(allocator); 2612 errdefer allocator.free(text); 2613 if (std.mem.eql(u8, ti.options.name, "title")) { 2614 title = text; 2615 } else if (std.mem.eql(u8, ti.options.name, "labels")) { 2616 labels = text; 2617 } else { 2618 try hunks.append(allocator, text); 2619 } 2620 }, 2621 else => {}, 2622 }; 2623 2624 const id_bytes = try evt.parseEventId(entry.id); 2625 2626 switch (src.repo_kind) { 2627 inline else => |repo_kind| { 2628 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2629 defer any_repo.deinit(io, allocator); 2630 switch (any_repo) { 2631 inline else => |*repo| { 2632 var resolution: Event.Resolve = .{ 2633 .title = title, 2634 .labels = labels, 2635 .hunks = hunks.items, 2636 }; 2637 if (comptime @hasField(Event.Resolve, "theirs")) resolution.theirs = self.data.theirs_picks; 2638 const change = Event.Update{ .resolve = resolution }; 2639 Event.update(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, change, author) catch |err| switch (err) { 2640 // leave the form up for correction 2641 error.InvalidFields => return, 2642 else => |e| return e, 2643 }; 2644 }, 2645 } 2646 }, 2647 } 2648 2649 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2650 try self.session.navigate(route); 2651 } 2652 2653 // commit a new or edited comment and navigate to its permalink. this is the 2654 // terminal path; the web posts the form to its comment route. 2655 fn submitComment(self: *This, allocator: std.mem.Allocator) !void { 2656 if (comptime wasm) return; 2657 const io = self.session.io orelse return; 2658 const src = self.data.repo_source orelse return; 2659 const actor = (try self.session.authorize(self.data.identity, .read)) orelse return; 2660 const author = actor.author; 2661 const form = self.formBox() orelse return; 2662 const body_input = try formField(form, "body"); 2663 const body = try body_input.text(allocator); 2664 defer allocator.free(body); 2665 if (!evt.Comment.fieldsValid(body)) return; 2666 2667 var thread_id: [evt.event_id_size * 2]u8 = undefined; 2668 @memcpy(&thread_id, self.data.selected_id); 2669 const parent_id = if (self.data.comment_id.len == 0) thread_id else blk: { 2670 var parent: [evt.event_id_size * 2]u8 = undefined; 2671 @memcpy(&parent, self.data.comment_id); 2672 break :blk parent; 2673 }; 2674 var event_id_hex: [evt.event_id_size * 2]u8 = undefined; 2675 const editing = self.data.view == .edit_comment; 2676 if (editing) { 2677 const comment_id = evt.parseEventId(self.data.comment_id) catch return; 2678 if (!try src.canModify(io, allocator, actor, .comment, &comment_id)) return; 2679 } 2680 switch (src.repo_kind) { 2681 inline else => |repo_kind| { 2682 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2683 defer any_repo.deinit(io, allocator); 2684 switch (any_repo) { 2685 inline else => |*repo| if (editing) { 2686 const comment_id = evt.parseEventId(self.data.comment_id) catch return; 2687 try evt.Comment.update(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, &thread_id, &comment_id, body, author); 2688 event_id_hex = std.fmt.bytesToHex(comment_id, .lower); 2689 } else { 2690 event_id_hex = try evt.Comment.create(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, &thread_id, &parent_id, body, author); 2691 }, 2692 } 2693 }, 2694 } 2695 2696 body_input.clear(allocator); 2697 const route = ui.RoutablePage.repoThreadCommentRoute(kind, self.data.identity, &thread_id, &event_id_hex, 0) orelse return; 2698 try self.session.navigate(route); 2699 } 2700 2701 // terminal form submissions; the web posts to the forms' routes instead 2702 fn submitForm(self: *This, allocator: std.mem.Allocator) !void { 2703 switch (self.data.view) { 2704 .new_comment, .edit_comment => try self.submitComment(allocator), 2705 .edit => try self.submitEditedThread(allocator), 2706 else => try self.submitNewThread(allocator), 2707 } 2708 } 2709 2710 // commit the new thread to the repo's events branch and navigate to it. 2711 // this is the terminal path; the web posts the form to the thread route, 2712 // so the wasm side never runs (or compiles) the repo access below. 2713 fn submitNewThread(self: *This, allocator: std.mem.Allocator) !void { 2714 if (comptime wasm) return; 2715 const io = self.session.io orelse return; 2716 const src = self.data.repo_source orelse return; 2717 const actor = (try self.session.authorize(self.data.identity, .read)) orelse return; 2718 2719 const form = self.threadForm() orelse return; 2720 const title_input = try formField(form, "title"); 2721 const labels_input = try formField(form, "labels"); 2722 const description_input = try formField(form, "description"); 2723 2724 const title = try title_input.text(allocator); 2725 defer allocator.free(title); 2726 const labels = try labels_input.text(allocator); 2727 defer allocator.free(labels); 2728 const description = try description_input.text(allocator); 2729 defer allocator.free(description); 2730 const target_branch = if (comptime supports_drafts) try (try formField(form, "target_branch")).text(allocator) else ""; 2731 defer if (comptime supports_drafts) allocator.free(target_branch); 2732 2733 if (!Event.fieldsValid(title, labels)) { 2734 if (!evt.titleValid(title)) try self.rememberThreadFeedback(.required_title, title, labels, description, target_branch); 2735 return; 2736 } 2737 2738 if (comptime supports_drafts) { 2739 const source = formWidget(form, .patch_source) orelse return error.MissingFormField; 2740 const source_branch = switch (source.selectedKind()) { 2741 .fork => null, 2742 .branch => try source.field().text(allocator), 2743 }; 2744 defer if (source_branch) |branch| allocator.free(branch); 2745 const event_id_hex = Data.create(self.data, self.session, allocator, actor, title, labels, description, target_branch, source_branch) catch |err| { 2746 const failure = FeedbackFailure.fromError(err) orelse return err; 2747 try self.rememberThreadFeedback(failure, title, labels, description, target_branch); 2748 return; 2749 }; 2750 title_input.clear(allocator); 2751 labels_input.clear(allocator); 2752 description_input.clear(allocator); 2753 (try formField(form, "target_branch")).clear(allocator); 2754 const route = ui.RoutablePage.repoThreadCommentsRoute(kind, self.data.identity, &event_id_hex, 0) orelse return; 2755 try self.session.navigate(route); 2756 return; 2757 } 2758 2759 var id_bytes: [evt.event_id_size]u8 = undefined; 2760 io.random(&id_bytes); 2761 const event_id_hex = std.fmt.bytesToHex(id_bytes, .lower); 2762 2763 const event = evt.EventWithId{ 2764 .id = event_id_hex, 2765 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 2766 .author = actor.author, 2767 .event = @unionInit(evt.Event, @tagName(kind), Event{ 2768 .title = title, 2769 .description = description, 2770 .labels = labels, 2771 }), 2772 }; 2773 2774 switch (src.repo_kind) { 2775 inline else => |repo_kind| { 2776 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2777 defer any_repo.deinit(io, allocator); 2778 switch (any_repo) { 2779 inline else => |*repo| try evt.consume(self.session.eventHost(), .repo, repo_kind, repo.self_repo_opts, io, allocator, repo, evt.events_ref, &.{event}), 2780 } 2781 }, 2782 } 2783 2784 // wipe the form so a return visit starts fresh 2785 title_input.clear(allocator); 2786 labels_input.clear(allocator); 2787 description_input.clear(allocator); 2788 2789 const route = listRoute(self.data.identity, @enumFromInt(0), "", &event_id_hex) orelse return; 2790 try self.session.navigate(route); 2791 } 2792 2793 // re-emit the selected thread's event with the form's content (status 2794 // preserved), then reload the thread's page. this is the terminal path; 2795 // the web posts the form to the edit route. 2796 fn submitEditedThread(self: *This, allocator: std.mem.Allocator) !void { 2797 if (comptime wasm) return; 2798 const io = self.session.io orelse return; 2799 const entry = self.data.selectedThread() orelse return; 2800 const actor = (try self.session.authorize(self.data.identity, .read)) orelse return; 2801 const author = actor.author; 2802 2803 const form = self.threadForm() orelse return; 2804 const title_input = try formField(form, "title"); 2805 const labels_input = try formField(form, "labels"); 2806 const description_input = try formField(form, "description"); 2807 2808 const title = try title_input.text(allocator); 2809 defer allocator.free(title); 2810 const labels = try labels_input.text(allocator); 2811 defer allocator.free(labels); 2812 const description = try description_input.text(allocator); 2813 defer allocator.free(description); 2814 const target_branch = if (comptime supports_drafts) try (try formField(form, "target_branch")).text(allocator) else ""; 2815 defer if (comptime supports_drafts) allocator.free(target_branch); 2816 2817 if (!Event.fieldsValid(title, labels)) { 2818 if (!evt.titleValid(title)) try self.rememberThreadFeedback(.required_title, title, labels, description, target_branch); 2819 return; 2820 } 2821 const src = self.data.repo_source orelse return; 2822 2823 if (comptime supports_drafts) { 2824 if (entryDraft(entry.*)) { 2825 self.data.editDraft(self.session, allocator, author, entry.id, title, labels, description, target_branch) catch |err| { 2826 const failure = FeedbackFailure.fromError(err) orelse return err; 2827 try self.rememberThreadFeedback(failure, title, labels, description, target_branch); 2828 return; 2829 }; 2830 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2831 try self.session.navigate(route); 2832 return; 2833 } 2834 } 2835 2836 const id_bytes = try evt.parseEventId(entry.id); 2837 if (!try src.canModify(io, allocator, actor, kind, &id_bytes)) return; 2838 2839 switch (src.repo_kind) { 2840 inline else => |repo_kind| { 2841 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2842 defer any_repo.deinit(io, allocator); 2843 switch (any_repo) { 2844 inline else => |*repo| if (supports_drafts) { 2845 Event.update(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 2846 .title = title, 2847 .labels = labels, 2848 .description = description, 2849 .target_branch = target_branch, 2850 } }, author) catch |err| { 2851 const failure = FeedbackFailure.fromError(err) orelse return err; 2852 try self.rememberThreadFeedback(failure, title, labels, description, target_branch); 2853 return; 2854 }; 2855 } else if (has_status) 2856 try Event.update(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 2857 .title = title, 2858 .labels = labels, 2859 .description = description, 2860 } }, author) 2861 else 2862 try Event.update(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, title, labels, description, author), 2863 } 2864 }, 2865 } 2866 2867 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2868 try self.session.navigate(route); 2869 } 2870 2871 fn rememberThreadFeedback(self: *This, failure: FeedbackFailure, title: []const u8, labels: []const u8, description: []const u8, target_branch: []const u8) !void { 2872 const aa = self.session.arena.allocator(); 2873 const source = if (supports_drafts) blk: { 2874 const form = self.threadForm() orelse break :blk null; 2875 break :blk formWidget(form, .patch_source); 2876 } else null; 2877 self.session.data.form_feedback = @unionInit(ui.Session.FormFeedback, @tagName(feedback_tag), .{ 2878 .failure = failure, 2879 .fields = if (comptime supports_drafts) .{ 2880 .title = try aa.dupe(u8, title), 2881 .labels = try aa.dupe(u8, labels), 2882 .description = try aa.dupe(u8, description), 2883 .target_branch = try aa.dupe(u8, target_branch), 2884 .source_branch = if (source) |value| try value.field().text(aa) else "", 2885 .source_kind = if (source) |value| value.selectedKind() else .fork, 2886 } else .{ 2887 .title = try aa.dupe(u8, title), 2888 .labels = try aa.dupe(u8, labels), 2889 .description = try aa.dupe(u8, description), 2890 }, 2891 }); 2892 } 2893 2894 fn submitConfirmation(self: *This, allocator: std.mem.Allocator, button_index: usize) !void { 2895 switch (confirmation(self.data.view) orelse return) { 2896 .publish => if (comptime supports_drafts) { 2897 if (comptime wasm) return; 2898 const entry = self.data.selectedThread() orelse return; 2899 if (!entryDraft(entry.*)) return; 2900 const author = ((try self.session.authorize(self.data.identity, .read)) orelse return).author; 2901 try self.data.publishDraft(self.session, allocator, author, entry.id); 2902 const route = listRoute(self.data.identity, @enumFromInt(0), "", entry.id) orelse return; 2903 try self.session.navigate(route); 2904 } else unreachable, 2905 .merge => if (comptime supports_merge) { 2906 if (comptime wasm) return; 2907 const entry = self.data.selectedThread() orelse return; 2908 const revision: evt.Patch.MergeRevision = if (button_index > 1) .squash else .source; 2909 if (!Data.canMerge(entry.*, self.session) or entry.mergeability.get(revision) != .clean) return; 2910 const author = ((try self.session.authorize(self.data.identity, .write)) orelse return).author; 2911 self.data.mergePatch(self.session, allocator, author, entry.id, revision) catch |err| switch (err) { 2912 error.MergeConflict, error.MergeCheckUnavailable, error.PatchOutOfDate, error.PatchDataUnavailable => { 2913 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2914 try self.session.navigate(route); 2915 return; 2916 }, 2917 else => return err, 2918 }; 2919 const merged = std.meta.stringToEnum(Status, "merged") orelse return; 2920 const route = listRoute(self.data.identity, merged, "", entry.id) orelse return; 2921 try self.session.navigate(route); 2922 } else unreachable, 2923 .remove => try self.submitRemove(allocator), 2924 } 2925 } 2926 2927 fn submitRemove(self: *This, allocator: std.mem.Allocator) !void { 2928 const comment = self.data.comment_id.len != 0; 2929 const event_id = if (comment) self.data.comment_id else self.data.selected_id; 2930 const id = evt.parseEventId(event_id) catch return; 2931 try self.removeEvent(allocator, if (comment) .comment else kind, id); 2932 } 2933 2934 fn removeEvent(self: *This, allocator: std.mem.Allocator, event_kind: evt.EventKind, id: [evt.event_id_size]u8) !void { 2935 if (comptime wasm) return; 2936 const actor = (try self.session.authorize(self.data.identity, .read)) orelse return; 2937 const author = actor.author; 2938 2939 if (comptime supports_drafts) { 2940 const selected = self.data.selectedThread() orelse return; 2941 if (event_kind == kind and entryDraft(selected.*)) { 2942 try Data.removeDraft(self.session, allocator, author, &id); 2943 const route = draftsRoute(self.data.identity) orelse return; 2944 try self.session.navigate(route); 2945 return; 2946 } 2947 } 2948 2949 const io = self.session.io orelse return; 2950 const src = self.data.repo_source orelse return; 2951 if (!try src.canModify(io, allocator, actor, event_kind, &id)) return; 2952 2953 switch (src.repo_kind) { 2954 inline else => |repo_kind| { 2955 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2956 defer any_repo.deinit(io, allocator); 2957 switch (any_repo) { 2958 inline else => |*repo| try evt.remove(self.session.eventHost(), .repo, repo_kind, repo.self_repo_opts, io, allocator, repo, &id, event_kind, author), 2959 } 2960 }, 2961 } 2962 2963 const id_hex = std.fmt.bytesToHex(id, .lower); 2964 const route = ui.RoutablePage.repoEventsRoute(self.data.identity, .removed, event_kind, &id_hex, null) orelse return; 2965 try self.session.navigate(route); 2966 } 2967 2968 // return to the list. 2969 fn focusList(self: *This, index: usize, root_focus: *Focus) void { 2970 root_focus.setFocus(self.listScroll(index).getFocus().id); 2971 } 2972 2973 // cross to the header tabs above the stack. 2974 fn focusHeader(self: *This, root_focus: *Focus) void { 2975 root_focus.setFocus(self.box.children.keys()[header_index]); 2976 } 2977 2978 pub fn clearGrid(self: *This) void { 2979 self.box.clearGrid(); 2980 } 2981 2982 pub fn getGrid(self: This) ?Grid { 2983 return self.box.getGrid(); 2984 } 2985 2986 pub fn getFocus(self: *This) *Focus { 2987 return self.box.getFocus(); 2988 } 2989 2990 // for the parent's "scroll up at the top jumps to the header" check: at the 2991 // top only while the header tabs hold focus, so up from the split 2992 // crosses into the tabs first. 2993 pub fn atTop(self: *This) bool { 2994 return self.headerActive(); 2995 } 2996 }; 2997 } 2998