╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ thread.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1450 if (child.widget == .repo_comment) { 1451 const comment = &child.widget.repo_comment; 1452 const row = comment.rowRect(comment.bodyFocused()) orelse return null; 1453 rect.x += row.x; 1454 rect.y += row.y; 1455 rect.size = row.size; 1456 } 1457 return rect; 1458 } 1459 1460 fn moveHorizontal(self: *This, root_focus: *Focus, right: bool) bool { 1461 const inner_box = self.inner(); 1462 const current = self.focusedChild(root_focus) orelse return false; 1463 const child = &inner_box.children.values()[current]; 1464 if (child.widget == .repo_comment) { 1465 const moved = child.widget.repo_comment.moveHorizontal(root_focus, right); 1466 if (moved) if (child.rect) |rect| self.scroll.scrollToRect(rect); 1467 return moved; 1468 } 1469 const row = switch (child.widget) { 1470 .box => |*box| box, 1471 else => return false, 1472 }; 1473 if (!widget.moveInRow(row, root_focus, right)) return false; 1474 if (child.rect) |rect| self.scroll.scrollToRect(rect); 1475 return true; 1476 } 1477 1478 // the input takes typing and assigns on enter 1479 fn assigneeInput(self: *This, allocator: std.mem.Allocator, row: *wgt.Box(Widget), key: Key, root_focus: *Focus) !void { 1480 const text_input = &row.children.values()[0].widget.text_input; 1481 switch (key) { 1482 .arrow_up => _ = self.moveVertical(root_focus, false), 1483 .arrow_down => _ = self.moveVertical(root_focus, true), 1484 .arrow_left => if (text_input.cursor == 0) { 1485 self.exit = .list; 1486 } else try text_input.input(allocator, key, root_focus), 1487 .enter => { 1488 const text = try text_input.text(allocator); 1489 defer allocator.free(text); 1490 try self.assign(allocator, text); 1491 }, 1492 else => try text_input.input(allocator, key, root_focus), 1493 } 1494 } 1495 1496 // arrows move along a row that ends in a ✕. returns whether enter or a 1497 // click hit the ✕. 1498 fn removableRowInput(self: *This, row: *wgt.Box(Widget), key: Key, root_focus: *Focus) bool { 1499 const remove_id = row.children.keys()[row.children.count() - 1]; 1500 switch (key) { 1501 .arrow_left => if (!self.moveHorizontal(root_focus, false)) { 1502 self.exit = .list; 1503 }, 1504 .arrow_right => _ = self.moveHorizontal(root_focus, true), 1505 .arrow_up => _ = self.moveVertical(root_focus, false), 1506 .arrow_down => _ = self.moveVertical(root_focus, true), 1507 .enter => return row.getFocus().child_id == remove_id, 1508 .mouse => |mouse| return inp.leftClickOn(root_focus, remove_id, mouse), 1509 else => {}, 1510 } 1511 return false; 1512 } 1513 1514 fn labelsInput(self: *This, child_index: usize, labels: *WordFlow, key: Key, root_focus: *Focus) void { 1515 const child_id = labels.focus.child_id orelse return; 1516 const cur = labels.indexOfFocusId(child_id) orelse return; 1517 const count = labels.text_boxes.items.len; 1518 switch (key) { 1519 .arrow_left => if (cur > 0) self.focusLabel(child_index, labels, root_focus, cur - 1) else { 1520 self.exit = .list; 1521 }, 1522 .arrow_right => if (cur + 1 < count) self.focusLabel(child_index, labels, root_focus, cur + 1), 1523 .arrow_up => if (labels.rowStep(cur, false)) |index| { 1524 self.focusLabel(child_index, labels, root_focus, index); 1525 } else { 1526 _ = self.moveVertical(root_focus, false); 1527 }, 1528 .arrow_down => if (labels.rowStep(cur, true)) |index| { 1529 self.focusLabel(child_index, labels, root_focus, index); 1530 } else { 1531 _ = self.moveVertical(root_focus, true); 1532 }, 1533 .home => self.focusLabel(child_index, labels, root_focus, 0), 1534 .end => self.focusLabel(child_index, labels, root_focus, count - 1), 1535 else => {}, 1536 } 1537 } 1538 1539 fn focusLabel(self: *This, child_index: usize, labels: *WordFlow, root_focus: *Focus, item: usize) void { 1540 root_focus.setFocus(labels.text_boxes.items[item].getFocus().id); 1541 if (self.session.is_terminal and item < labels.rects.items.len) { 1542 if (self.inner().children.values()[child_index].rect) |flow_rect| { 1543 var rect = labels.rects.items[item]; 1544 rect.x += flow_rect.x; 1545 rect.y += flow_rect.y; 1546 self.scroll.scrollToRect(rect); 1547 } 1548 } 1549 } 1550 1551 fn toolRowFocused(self: *This, root_focus: *Focus) bool { 1552 return self.focusedChild(root_focus) == tool_row_index; 1553 } 1554 1555 pub fn focusFirst(self: *This, root_focus: ?*Focus) bool { 1556 const inner_box = self.inner(); 1557 for (inner_box.children.values(), 0..) |*child, child_index| { 1558 switch (child.widget) { 1559 .box => |*box| { 1560 for (box.children.keys(), box.children.values()) |id, *item| { 1561 if (item.widget.getFocus().mode != .all) continue; 1562 box.getFocus().child_id = id; 1563 break; 1564 } else continue; 1565 }, 1566 else => if (!self.childFocusable(child_index)) continue, 1567 } 1568 if (root_focus) |focus| 1569 self.focusChild(child_index, false, true, focus) 1570 else 1571 inner_box.getFocus().child_id = inner_box.children.keys()[child_index]; 1572 return true; 1573 } 1574 return false; 1575 } 1576 1577 pub fn atTop(self: *This, root_focus: *Focus) bool { 1578 const focused = self.focusedChild(root_focus) orelse return false; 1579 for (0..self.inner().children.count()) |child_index| { 1580 if (!self.childFocusable(child_index)) continue; 1581 return focused == child_index; 1582 } 1583 return false; 1584 } 1585 1586 fn removeEvent(self: *This, allocator: std.mem.Allocator, event_kind: evt.EventKind, id: [evt.event_id_size]u8) !void { 1587 if (comptime wasm) return; 1588 const actor = (try self.session.authorize(self.data.identity, .read, kind)) orelse return; 1589 const author = actor.author; 1590 1591 if (comptime supports_drafts) { 1592 const entry = self.entry orelse return; 1593 if (event_kind == kind and entryDraft(entry)) { 1594 try Data.removeDraft(self.session, allocator, author, &id); 1595 const route = draftsRoute(self.data.identity) orelse return; 1596 try self.session.navigate(route); 1597 return; 1598 } 1599 } 1600 1601 const io = self.session.io orelse return; 1602 const source = self.data.repo_source orelse return; 1603 if (!try source.canModify(io, allocator, actor, event_kind, &id)) return; 1604 const thread_id = evt.parseEventId((self.entry orelse return).id) catch return; 1605 1606 switch (source.repo_kind) { 1607 inline else => |repo_kind| { 1608 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, source.localInitOpts()); 1609 defer any_repo.deinit(io, allocator); 1610 switch (any_repo) { 1611 inline else => |*repo| try evt.removeInThread(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, kind, &thread_id, event_kind, &id, author), 1612 } 1613 }, 1614 } 1615 1616 const id_hex = std.fmt.bytesToHex(id, .lower); 1617 const route = ui.RoutablePage.repoEventsRoute(self.data.identity, .removed, event_kind, &id_hex, null) orelse return; 1618 try self.session.navigate(route); 1619 } 1620 1621 // assign the typed email, or the email of the user the text names 1622 fn assign(self: *This, allocator: std.mem.Allocator, text: []const u8) !void { 1623 if (comptime wasm) return; 1624 const entry = self.entry orelse return; 1625 const io = self.session.io orelse return; 1626 const source = self.data.repo_source orelse return; 1627 const actor = (try self.session.authorize(self.data.identity, .read, kind)) orelse return; 1628 const thread_id = try evt.parseEventId(entry.id); 1629 if (!try source.canModify(io, allocator, actor, kind, &thread_id)) return; 1630 1631 var arena = std.heap.ArenaAllocator.init(allocator); 1632 defer arena.deinit(); 1633 const failure: ?ui.Session.FormFeedback.AssignmentFailure = blk: { 1634 const moment = if (self.session.data.host_kind == .local) null else self.session.haxy_moment; 1635 const email = switch (try ui.assigneeEmail(moment, &arena, text)) { 1636 .email => |email| email, 1637 .failure => |value| break :blk value, 1638 }; 1639 const thread_id_hex = std.fmt.bytesToHex(thread_id, .lower); 1640 switch (source.repo_kind) { 1641 inline else => |repo_kind| { 1642 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, source.localInitOpts()); 1643 defer any_repo.deinit(io, allocator); 1644 switch (any_repo) { 1645 inline else => |*repo| _ = evt.Assignment.create(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, kind, &thread_id_hex, email, actor.author) catch |err| switch (err) { 1646 error.AlreadyAssigned => break :blk .already_assigned, 1647 error.InvalidFields => break :blk .not_an_email, 1648 else => |e| return e, 1649 }, 1650 } 1651 }, 1652 } 1653 break :blk null; 1654 }; 1655 if (failure) |value| { 1656 self.session.data.form_feedback = .{ .assignment = .{ .failure = value, .text = try self.session.arena.allocator().dupe(u8, text) } }; 1657 return; 1658 } 1659 try self.session.navigate(ui.RoutablePage.repoThreadCommentsRoute(kind, self.data.identity, entry.id, 0) orelse return error.RouteTooLong); 1660 } 1661 1662 fn unassign(self: *This, allocator: std.mem.Allocator, email: []const u8) !void { 1663 if (comptime wasm) return; 1664 const entry = self.entry orelse return; 1665 const io = self.session.io orelse return; 1666 const source = self.data.repo_source orelse return; 1667 const actor = (try self.session.authorize(self.data.identity, .read, kind)) orelse return; 1668 const thread_id = try evt.parseEventId(entry.id); 1669 if (!try source.canModify(io, allocator, actor, kind, &thread_id)) return; 1670 1671 switch (source.repo_kind) { 1672 inline else => |repo_kind| { 1673 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, source.localInitOpts()); 1674 defer any_repo.deinit(io, allocator); 1675 switch (any_repo) { 1676 inline else => |*repo| evt.removeInThread(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, kind, &thread_id, .assign, &evt.Assignment.idOf(&thread_id, email), actor.author) catch |err| switch (err) { 1677 // a stale button; the refresh below shows the row as it is 1678 error.EventNotFound => {}, 1679 else => |e| return e, 1680 }, 1681 } 1682 }, 1683 } 1684 try self.session.navigate(ui.RoutablePage.repoThreadCommentsRoute(kind, self.data.identity, entry.id, 0) orelse return error.RouteTooLong); 1685 } 1686 1687 // subscribe to the thread or mute it, as its button says 1688 fn changeNotify(self: *This, allocator: std.mem.Allocator) !void { 1689 if (comptime wasm) return; 1690 const entry = self.entry orelse return; 1691 const mode = entry.notify_action orelse return; 1692 const io = self.session.io orelse return; 1693 const users_dir = self.session.users_dir orelse return; 1694 const source = self.data.repo_source orelse return; 1695 const actor = (try self.session.authorize(self.data.identity, .read, null)) orelse return; 1696 // a server actor always has both ids 1697 const user_id = actor.user_id orelse unreachable; 1698 const location: evt.RepoLocation = .{ 1699 .owner_id = actor.repo_user_id orelse unreachable, 1700 .repo_id = try evt.parseEventId(std.Io.Dir.path.basename(source.path)), 1701 }; 1702 const thread_id = try evt.parseEventId(entry.id); 1703 switch (mode) { 1704 .subscribe => try notify.subscribe(io, allocator, users_dir, &user_id, location, &thread_id, actor.author), 1705 .mute => try notify.mute(io, allocator, users_dir, &user_id, location, &thread_id, actor.author), 1706 } 1707 try self.session.navigate(ui.RoutablePage.repoThreadCommentsRoute(kind, self.data.identity, entry.id, 0) orelse return error.RouteTooLong); 1708 } 1709 1710 fn toggleStatus(self: *This, allocator: std.mem.Allocator) !void { 1711 if (comptime (!has_status or wasm)) return; 1712 const entry = self.entry orelse return; 1713 const io = self.session.io orelse return; 1714 const source = self.data.repo_source orelse return; 1715 const actor = (try self.session.authorize(self.data.identity, .read, kind)) orelse return; 1716 const author = actor.author; 1717 const status = (statusChange(entryStatus(entry)) orelse return).status; 1718 const id = try evt.parseEventId(entry.id); 1719 if (!try source.canModify(io, allocator, actor, kind, &id)) return; 1720 1721 switch (source.repo_kind) { 1722 inline else => |repo_kind| { 1723 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, source.localInitOpts()); 1724 defer any_repo.deinit(io, allocator); 1725 switch (any_repo) { 1726 inline else => |*repo| try Event.update(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, &id, .{ .status = status }, author), 1727 } 1728 }, 1729 } 1730 1731 const route = listRoute(self.data.identity, status, "", entry.id) orelse return; 1732 try self.session.navigate(route); 1733 } 1734 }; 1735 } 1736 1737 pub fn View(comptime kind: evt.EventKind, comptime Data: type) type { 1738 const Self = Data; 1739 const HeaderType = Data.Header; 1740 const Window = Data.Window; 1741 const Entry = Data.Entry; 1742 const Event = Data.Event; 1743 const feedback_tag: std.meta.Tag(ui.Session.FormFeedback) = switch (kind) { 1744 .issue => .issue, 1745 .patch => .patch, 1746 .discuss => .discussion, 1747 else => unreachable, 1748 }; 1749 const FeedbackFailure = if (kind == .patch) ui.Session.FormFeedback.PatchFailure else ui.Session.FormFeedback.ThreadFailure; 1750 const Status = Data.Status; 1751 const has_status = @hasField(Event, "status"); 1752 const supports_conflicts = Event.merge_policy == .field_conflicts; 1753 const supports_drafts = @hasField(Self, "drafts"); 1754 const supports_merge = @hasDecl(Data, "mergeRoute"); 1755 const FieldConflict = if (supports_conflicts) Data.FieldConflict else void; 1756 const ViewKind = Data.ViewKind; 1757 const thread_name = Data.thread_name; 1758 const DetailType = Data.Detail; 1759 1760 return struct { 1761 const This = @This(); 1762 // a vertical box: the header tabs on top, then a stack holding a 1763 // master-detail split (thread list + description pane) per status list, 1764 // plus the labels view. 1765 box: wgt.Box(Widget), // vert: [header_index] = tabs, [stack_index] = stack 1766 data: *const Self, 1767 session: *ui.Session, 1768 1769 const header_index: usize = 0; 1770 const stack_index: usize = 1; 1771 // indices within the stack, 1:1 with the header tabs. 1772 const status_count = @typeInfo(Status).@"enum".field_names.len; 1773 const labels_view_index: usize = status_count; 1774 // the new-thread form, or the edit, comment, or resolve form when the page was 1775 // loaded at one of their urls. 1776 const form_view_index: usize = labels_view_index + 1; 1777 // the conflicts split; the tab and stack child exist only when the repo 1778 // has conflicted threads. 1779 const drafts_view_index: usize = form_view_index + 1; 1780 const conflict_view_index: usize = drafts_view_index + @intFromBool(supports_drafts); 1781 const view_count: usize = conflict_view_index + @intFromBool(supports_conflicts); 1782 // indices within a split (the horizontal box inside the stack). 1783 const list_index: usize = 0; 1784 const detail_index: usize = 1; 1785 const list_max_width: usize = 35; 1786 const detail_min_width: usize = 40; 1787 1788 pub fn viewIndex(view: ViewKind) usize { 1789 const name = @tagName(view); 1790 if (std.mem.eql(u8, name, "labels")) return labels_view_index; 1791 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; 1792 if (std.mem.eql(u8, name, "conflicts")) return conflict_view_index; 1793 if (std.mem.eql(u8, name, "drafts")) return drafts_view_index; 1794 if (std.mem.eql(u8, name, "description")) unreachable; 1795 inline for (@typeInfo(Status).@"enum".field_names, 0..) |status_name, index| { 1796 if (std.mem.eql(u8, name, status_name)) return index; 1797 } 1798 unreachable; 1799 } 1800 1801 const Confirmation = enum { remove, publish, merge }; 1802 1803 fn confirmation(view: ViewKind) ?Confirmation { 1804 if (view == .remove) return .remove; 1805 if (supports_drafts and std.mem.eql(u8, @tagName(view), "publish")) return .publish; 1806 if (supports_merge and std.mem.eql(u8, @tagName(view), "merge")) return .merge; 1807 return null; 1808 } 1809 1810 // the status a status split lists; the conflicts split has none (its 1811 // threads carry their own). 1812 fn splitStatus(index: usize) Status { 1813 return @fromBackingInt(@intCast(index)); 1814 } 1815 1816 fn entryConflicted(entry: Entry) bool { 1817 return if (supports_conflicts) entry.conflicted else false; 1818 } 1819 1820 fn entryStatus(entry: Entry) Status { 1821 if (comptime has_status) return Data.statusKind(entry.record.event); 1822 return @fromBackingInt(@intCast(0)); 1823 } 1824 1825 fn entryDraft(entry: Entry) bool { 1826 return if (supports_drafts) entry.draft else false; 1827 } 1828 1829 fn listRoute(identity: []const u8, status: Status, label: []const u8, selected: []const u8) ?ui.RoutablePage { 1830 return Data.listRoute(identity, status, label, selected); 1831 } 1832 1833 fn draftsRoute(identity: []const u8) ?ui.RoutablePage { 1834 return if (supports_drafts) Data.draftsRoute(identity) else null; 1835 } 1836 1837 fn publishRoute(identity: []const u8, selected: []const u8) ?ui.RoutablePage { 1838 return if (supports_drafts) Data.publishRoute(identity, selected) else null; 1839 } 1840 1841 fn mergeRoute(identity: []const u8, selected: []const u8) ?ui.RoutablePage { 1842 return if (supports_merge) Data.mergeRoute(identity, selected) else null; 1843 } 1844 1845 fn conflictsRoute(identity: []const u8, selected: []const u8) ?ui.RoutablePage { 1846 return if (supports_conflicts) Data.conflictsRoute(identity, selected) else null; 1847 } 1848 1849 fn resolveRoute(identity: []const u8, selected: []const u8, picks: []const u8) ?ui.RoutablePage { 1850 return if (supports_conflicts) Data.resolveRoute(identity, selected, picks) else null; 1851 } 1852 1853 // a list link keeps the filters the page carries: the label, and the 1854 // search query and url-encoded assignee when set 1855 fn listLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, status: Status, label: []const u8, id: []const u8, search: []const u8, assignee: []const u8) ![]const u8 { 1856 var route = listRoute(identity, status, label, id) orelse return error.RouteTooLong; 1857 if (search.len != 0) route = route.withSearch(search) orelse return error.RouteTooLong; 1858 if (assignee.len != 0) route = route.withEncodedAssignee(assignee) orelse return error.RouteTooLong; 1859 return page_arena.allocator().print("a:{s}", .{try route.toUrl(page_arena)}); 1860 } 1861 1862 fn rowLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, id: []const u8) ![]const u8 { 1863 const selected = std.mem.eql(u8, id, data.selected_id); 1864 const route = (if (selected and data.description_page) 1865 ui.RoutablePage.repoThreadDescriptionRoute(kind, data.identity, id) 1866 else if (selected) 1867 if (data.comment_page) |page| 1868 ui.RoutablePage.repoThreadCommentRoute(kind, data.identity, id, &page.selected.id, page.replies.start) 1869 else 1870 ui.RoutablePage.repoThreadCommentsRoute(kind, data.identity, id, data.comments_start) 1871 else 1872 ui.RoutablePage.repoThreadCommentsRoute(kind, data.identity, id, 0)) orelse return error.RouteTooLong; 1873 return page_arena.allocator().print("ai:{s}", .{try route.toUrl(page_arena)}); 1874 } 1875 1876 fn windowLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, status_maybe: ?Status, drafts: bool, id: []const u8) ![]const u8 { 1877 if (supports_conflicts and status_maybe == null and !drafts) { 1878 const route = conflictsRoute(data.identity, id) orelse return error.RouteTooLong; 1879 return page_arena.allocator().print("a:{s}", .{try route.toUrl(page_arena)}); 1880 } 1881 if (drafts and id.len == 0) { 1882 const route = draftsRoute(data.identity) orelse return error.RouteTooLong; 1883 return page_arena.allocator().print("a:{s}", .{try route.toUrl(page_arena)}); 1884 } 1885 return listLink(page_arena, data.identity, status_maybe orelse @fromBackingInt(@intCast(0)), data.label, id, data.search orelse "", assigneeFilter(data)); 1886 } 1887 1888 fn labelLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, status: Status, label: []const u8, search: []const u8, assignee: []const u8) ![]const u8 { 1889 const encoded = try ui.urlEncodeRef(page_arena.allocator(), label); 1890 return listLink(page_arena, identity, status, encoded, "", search, assignee); 1891 } 1892 1893 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !This { 1894 var outer = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .vert }); 1895 errdefer outer.deinit(allocator); 1896 1897 // the tabs at the top. 1898 { 1899 var hdr = try Data.initHeader(allocator, session, data); 1900 errdefer hdr.deinit(allocator); 1901 try outer.children.put(allocator, hdr.getFocus().id, .{ .widget = @unionInit(Widget, Data.header_widget_name, hdr), .rect = null, .min_size = null }); 1902 } 1903 1904 // the stack enters `outer` before its children enter it, so an error 1905 // frees each child exactly once. 1906 { 1907 var stack = try wgt.Stack(Widget).init(allocator); 1908 errdefer stack.deinit(allocator); 1909 try outer.children.put(allocator, stack.getFocus().id, .{ .widget = .{ .stack = stack }, .rect = null, .min_size = null }); 1910 } 1911 const stack = &outer.children.values()[stack_index].widget.stack; 1912 1913 inline for (@typeInfo(Status).@"enum".field_values) |value| { 1914 var split = try initSplit(allocator, session, data, @fromBackingInt(@intCast(value)), false); 1915 errdefer split.deinit(allocator); 1916 try stack.children.put(allocator, split.getFocus().id, split); 1917 } 1918 1919 // the labels view, or the empty message without labels 1920 if (labelsEmpty(data)) { 1921 var center = try widget.initEmpty(allocator); 1922 errdefer center.deinit(allocator); 1923 try stack.children.put(allocator, center.getFocus().id, .{ .center = center }); 1924 } else { 1925 var tf = try WordFlow.init(allocator); 1926 errdefer tf.deinit(allocator); 1927 var items: std.ArrayList(WordFlow.Item) = .empty; 1928 defer items.deinit(allocator); 1929 // when filtered, the first item clears the filter 1930 if (data.label.len != 0) 1931 try items.append(allocator, .{ .text = "✕", .link = try listLink(session.page_arena, data.identity, @fromBackingInt(@intCast(0)), "", "", data.search orelse "", assigneeFilter(data)) }); 1932 for (data.labels) |label| 1933 try items.append(allocator, .{ .text = label, .link = try labelLink(session.page_arena, data.identity, @fromBackingInt(@intCast(0)), label, data.search orelse "", assigneeFilter(data)) }); 1934 try tf.setItems(allocator, items.items); 1935 try stack.children.put(allocator, tf.getFocus().id, .{ .word_flow = tf }); 1936 } 1937 1938 // the new-thread form, or — on an edit, comment, or resolve url — that form in 1939 // its place, prefilled with the selected thread's content. a viewer 1940 // who can't write in this tab gets the unauthorized view instead, 1941 // while local mode always writes. 1942 if (session.data.host_kind == .local or data.viewer != null) { 1943 if (confirmation(data.view)) |confirmation_kind| { 1944 const aa = session.page_arena.allocator(); 1945 const route = switch (confirmation_kind) { 1946 .remove => ui.RoutablePage.repoThreadRemoveRoute(kind, data.identity, data.selected_id, data.comment_id), 1947 .publish => publishRoute(data.identity, data.selected_id), 1948 .merge => mergeRoute(data.identity, data.selected_id), 1949 } orelse return error.RouteTooLong; 1950 const page_url = try route.toUrl(session.page_arena); 1951 const action = try aa.print("form:{s}", .{page_url}); 1952 const event_name = if (data.comment_id.len == 0) thread_name else "comment"; 1953 var label_buf: ["remove ".len + @max(thread_name.len, "comment".len)]u8 = undefined; 1954 var buttons: [2]ConfirmationButton = undefined; 1955 buttons[0] = .{ .label = switch (confirmation_kind) { 1956 .remove => try std.fmt.bufPrint(&label_buf, "remove {s}", .{event_name}), 1957 .publish => "publish patch", 1958 .merge => "merge patch", 1959 } }; 1960 var button_count: usize = 1; 1961 if (confirmation_kind == .merge) { 1962 if (comptime supports_merge) { 1963 const availability = if (data.selectedThread()) |entry| 1964 if (Data.canMerge(entry.*, session)) entry.mergeability else pch.Mergeability{} 1965 else 1966 pch.Mergeability{}; 1967 buttons[0].bottom_label = mergeBottomLabel(availability.source); 1968 if (availability.source != .clean) buttons[0].action = ""; 1969 buttons[1] = .{ 1970 .label = "squash and merge patch", 1971 .bottom_label = mergeBottomLabel(availability.squash), 1972 .action = if (availability.squash == .clean) try aa.print("{s}{s}/squash", .{ ui.submit_action_prefix, page_url }) else "", 1973 }; 1974 button_count += 1; 1975 } else unreachable; 1976 } 1977 var center = try initConfirmationForm(allocator, action, buttons[0..button_count]); 1978 errdefer center.deinit(allocator); 1979 try stack.children.put(allocator, center.getFocus().id, .{ .center = center }); 1980 } else if (data.view == .new_comment or data.view == .edit_comment) { 1981 const editing = data.view == .edit_comment; 1982 const route = if (editing) 1983 ui.RoutablePage.repoThreadCommentEditRoute(kind, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong 1984 else 1985 ui.RoutablePage.repoThreadCommentNewRoute(kind, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong; 1986 const page_url = try route.toUrl(session.page_arena); 1987 const action = try session.page_arena.allocator().print("form:{s}", .{page_url}); 1988 const page = data.comment_page; 1989 const top_level = if (page) |p| std.mem.eql(u8, &p.selected.comment.event.parent_id, &p.selected.comment.event.thread_id) else false; 1990 const parent_route = if (editing) 1991 if (top_level) 1992 ui.RoutablePage.repoThreadCommentsRoute(kind, data.identity, data.selected_id, 0) 1993 else if (page) |p| 1994 ui.RoutablePage.repoThreadCommentRoute(kind, data.identity, data.selected_id, &p.selected.comment.event.parent_id, 0) 1995 else 1996 null 1997 else if (page) |p| 1998 ui.RoutablePage.repoThreadCommentRoute(kind, data.identity, data.selected_id, &p.selected.id, 0) 1999 else 2000 ui.RoutablePage.repoThreadCommentsRoute(kind, data.identity, data.selected_id, 0); 2001 const parent_author: ui.Author = if (editing) 2002 if (top_level) 2003 if (data.selectedThread()) |entry| entry.author else .unknown 2004 else if (page) |p| 2005 p.selected.parent_author orelse .unknown 2006 else 2007 .unknown 2008 else if (page) |p| 2009 p.selected.author 2010 else if (data.selectedThread()) |entry| 2011 entry.author 2012 else 2013 .unknown; 2014 const initial_body: ?[]const u8 = if (editing) if (page) |p| p.selected.comment.event.body else null else null; 2015 var form = try initCommentForm(allocator, session, action, parent_author, parent_route orelse return error.RouteTooLong, initial_body); 2016 errdefer form.deinit(allocator); 2017 try stack.children.put(allocator, form.getFocus().id, .{ .box = form }); 2018 } else if (supports_conflicts and data.view == .resolve) { 2019 // on the web the page grows to the form's height and the 2020 // browser scrolls it; the terminal scrolls the form itself 2021 var form_widget: Widget = blk: { 2022 var form = try initResolveForm(allocator, session, data); 2023 errdefer form.deinit(allocator); 2024 break :blk if (session.is_terminal) 2025 .{ .scroll = try wgt.Scroll(Widget).init(allocator, .{ .box = form }, .{ .direction = .vert }) } 2026 else 2027 .{ .box = form }; 2028 }; 2029 errdefer form_widget.deinit(allocator); 2030 try stack.children.put(allocator, form_widget.getFocus().id, form_widget); 2031 } else { 2032 const aa = session.page_arena.allocator(); 2033 const route = if (data.view == .edit) 2034 ui.RoutablePage.repoThreadEditRoute(kind, data.identity, data.selected_id) orelse return error.RouteTooLong 2035 else 2036 ui.RoutablePage.repoThreadNewRoute(kind, data.identity) orelse return error.RouteTooLong; 2037 const action = try aa.print("form:{s}", .{try route.toUrl(session.page_arena)}); 2038 const record = if (data.view == .edit) 2039 (if (data.selectedThread()) |entry| &entry.record else null) 2040 else 2041 null; 2042 var form = try initThreadForm(allocator, session, data, action, record); 2043 errdefer form.deinit(allocator); 2044 try stack.children.put(allocator, form.getFocus().id, .{ .box = form }); 2045 } 2046 } else { 2047 var message = try ui.Unauthorized.View.init(allocator); 2048 errdefer message.deinit(allocator); 2049 try stack.children.put(allocator, message.getFocus().id, .{ .unauthorized = message }); 2050 } 2051 2052 if (supports_drafts) { 2053 var split = try initSplit(allocator, session, data, null, true); 2054 errdefer split.deinit(allocator); 2055 try stack.children.put(allocator, split.getFocus().id, split); 2056 } 2057 2058 // the conflicts split; its tab exists only when the repo has 2059 // conflicts, so skip the child too to keep the stack 1:1 with the 2060 // tabs. 2061 if (supports_conflicts and data.conflicts.count > 0) { 2062 var split = try initSplit(allocator, session, data, null, false); 2063 errdefer split.deinit(allocator); 2064 try stack.children.put(allocator, split.getFocus().id, split); 2065 } 2066 2067 // the stack starts on the page's view. 2068 stack.getFocus().child_id = stack.children.keys()[viewIndex(data.view)]; 2069 2070 // focus entering the view lands on the tabs first. 2071 outer.getFocus().child_id = outer.children.keys()[header_index]; 2072 2073 return .{ 2074 .box = outer, 2075 .data = data, 2076 .session = session, 2077 }; 2078 } 2079 2080 // the master-detail split showing `status`'s window, or the conflicts 2081 // window when null. an empty first window shows the empty message instead. 2082 fn initSplit(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self, status_maybe: ?Status, drafts: bool) !Widget { 2083 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; 2084 if (windowEmpty(win)) return .{ .center = try widget.initEmpty(allocator) }; 2085 2086 var box = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .horiz }); 2087 errdefer box.deinit(allocator); 2088 2089 // the thread list (one focusable row per title), plus a "next" link that 2090 // reloads the page rooted at the following thread. 2091 { 2092 var list_scroll = blk: { 2093 var list_box = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .vert, .stretch = true }); 2094 errdefer list_box.deinit(allocator); 2095 if (win.prev_id) |prev| 2096 try addRow(allocator, &list_box, "← previous", "", try windowLink(session.page_arena, data, status_maybe, drafts, prev)); 2097 for (win.items) |entry| { 2098 const label: []const u8 = if (entry.comments.count > 0) 2099 try session.page_arena.allocator().print(" {d} comments ", .{entry.comments.count}) 2100 else 2101 ""; 2102 try addRow(allocator, &list_box, entry.record.event.title, label, try rowLink(session.page_arena, data, entry.id)); 2103 } 2104 if (win.next_id) |next| 2105 try addRow(allocator, &list_box, "next →", "", try windowLink(session.page_arena, data, status_maybe, drafts, next)); 2106 // select the window's first thread (past a leading "previous" 2107 // row) so its description shows on load. 2108 if (win.items.len > 0) 2109 list_box.getFocus().child_id = list_box.children.keys()[if (win.prev_id != null) 1 else 0] 2110 else if (list_box.children.count() > 0) 2111 list_box.getFocus().child_id = list_box.children.keys()[0]; 2112 break :blk try wgt.Scroll(Widget).init(allocator, .{ .box = list_box }, .{ .direction = .vert, .web_native = !session.is_terminal }); 2113 }; 2114 errdefer list_scroll.deinit(allocator); 2115 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 } }); 2116 } 2117 2118 // frame the detail pane inside the master-detail split, below the 2119 // way back to the list, shown only while the list is hidden 2120 { 2121 var frame = try wgt.Box(Widget).init(allocator, .{ .border = .hidden, .direction = .vert }); 2122 errdefer frame.deinit(allocator); 2123 frame.getFocus().mode = .mouse; 2124 try widget.addListBack(allocator, &frame, "← back to " ++ thread_name ++ " list"); 2125 var detail_view = try DetailType.init(allocator, data, session, null, .{}); 2126 errdefer detail_view.deinit(allocator); 2127 frame.getFocus().child_id = detail_view.getFocus().id; 2128 try frame.children.put(allocator, detail_view.getFocus().id, .{ .widget = @unionInit(Widget, Data.detail_widget_name, detail_view), .rect = null, .min_size = null }); 2129 try box.children.put(allocator, frame.getFocus().id, .{ .widget = .{ .box = frame }, .rect = null, .min_size = .{ .width = detail_min_width, .height = null } }); 2130 } 2131 2132 // the page opens on the list, unless the url names a thread 2133 box.getFocus().child_id = box.children.keys()[if (data.selected_id.len > 0) detail_index else list_index]; 2134 return .{ .box = box }; 2135 } 2136 2137 // a thread form, plus the target branch for patches, prefilled from 2138 // `record` when given. its form: subtree makes the web renderer POST 2139 // the inputs to `action`'s route. 2140 fn initThreadForm(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self, action: []const u8, record: ?*const Event.Record) !wgt.Box(Widget) { 2141 var box = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .vert }); 2142 errdefer box.deinit(allocator); 2143 box.getFocus().kind = .{ .custom = action }; 2144 2145 const saved_form = session.formFeedback(feedback_tag); 2146 const saved_fields = if (saved_form) |saved| saved.fields else null; 2147 2148 { 2149 var title = try wgt.TextInput.init(allocator, .{ .top_label = .{ .text = " title " }, .name = "title", .visible_width = null, .round_corners = true, .render_content = session.is_terminal }); 2150 errdefer title.deinit(allocator); 2151 title.getFocus().mode = .all; 2152 if (saved_fields) |saved| 2153 try title.setContent(allocator, saved.title) 2154 else if (record) |r| 2155 try title.setContent(allocator, r.event.title); 2156 try box.children.put(allocator, title.getFocus().id, .{ .widget = .{ .text_input = title }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 2157 box.getFocus().child_id = title.getFocus().id; 2158 } 2159 2160 { 2161 var labels = try wgt.TextInput.init(allocator, .{ .top_label = .{ .text = " labels (separate with spaces) " }, .name = "labels", .visible_width = null, .round_corners = true, .render_content = session.is_terminal }); 2162 errdefer labels.deinit(allocator); 2163 labels.getFocus().mode = .all; 2164 if (saved_fields) |saved| 2165 try labels.setContent(allocator, saved.labels) 2166 else if (record) |r| 2167 try labels.setContent(allocator, r.event.labels); 2168 try box.children.put(allocator, labels.getFocus().id, .{ .widget = .{ .text_input = labels }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 2169 } 2170 2171 { 2172 var description = try wgt.TextInput.init(allocator, .{ .top_label = .{ .text = " description (markdown supported) " }, .name = "description", .visible_width = null, .round_corners = true, .render_content = session.is_terminal, .multiline = true, .scroll = .{ .fill = true } }); 2173 errdefer description.deinit(allocator); 2174 description.getFocus().mode = .all; 2175 if (saved_fields) |saved| 2176 try description.setContent(allocator, saved.description) 2177 else if (record) |r| 2178 try description.setContent(allocator, r.event.description); 2179 try box.children.put(allocator, description.getFocus().id, .{ .widget = .{ .text_input = description }, .rect = null, .min_size = null }); 2180 } 2181 2182 if (supports_drafts and record == null) { 2183 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); 2184 errdefer source.deinit(allocator); 2185 try box.children.put(allocator, source.getFocus().id, .{ .widget = .{ .patch_source = source }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 2186 } 2187 2188 if (supports_drafts) { 2189 var target = try wgt.TextInput.init(allocator, .{ .top_label = .{ .text = " target branch " }, .name = "target_branch", .visible_width = null, .round_corners = true, .render_content = session.is_terminal }); 2190 errdefer target.deinit(allocator); 2191 target.getFocus().mode = .all; 2192 const content = if (saved_fields) |saved| saved.target_branch else if (record) |r| r.event.target_branch else data.default_target_branch; 2193 try target.setContent(allocator, content); 2194 try box.children.put(allocator, target.getFocus().id, .{ .widget = .{ .text_input = target }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 2195 } 2196 2197 try addSubmitButtonLabeled(allocator, &box, if (supports_drafts and record == null) "submit draft" else "submit"); 2198 2199 return box; 2200 } 2201 2202 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) { 2203 var box = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .vert }); 2204 errdefer box.deinit(allocator); 2205 box.getFocus().kind = .{ .custom = action }; 2206 2207 { 2208 var parent = try ui.authorBox(allocator, session.page_arena, author); 2209 errdefer parent.deinit(allocator); 2210 parent.options.top_label.text = " replying to "; 2211 parent.getFocus().kind = .{ .custom = try session.page_arena.allocator().print("a:{s}", .{try parent_route.toUrl(session.page_arena)}) }; 2212 try box.children.put(allocator, parent.getFocus().id, .{ .widget = .{ .text_box = parent }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 2213 box.getFocus().child_id = parent.getFocus().id; 2214 } 2215 2216 { 2217 var body = try wgt.TextInput.init(allocator, .{ 2218 .top_label = .{ .text = " comment (markdown supported) " }, 2219 .name = "body", 2220 .visible_width = null, 2221 .round_corners = true, 2222 .render_content = session.is_terminal, 2223 .multiline = true, 2224 .visible_height = 5, 2225 .scroll = .{ .fill = true }, 2226 }); 2227 errdefer body.deinit(allocator); 2228 body.getFocus().mode = .all; 2229 if (initial_body) |text| try body.setContent(allocator, text); 2230 try box.children.put(allocator, body.getFocus().id, .{ .widget = .{ .text_input = body }, .rect = null, .min_size = null }); 2231 } 2232 2233 try addSubmitButtonLabeled(allocator, &box, "submit"); 2234 return box; 2235 } 2236 2237 const ConfirmationButton = struct { 2238 label: []const u8, 2239 bottom_label: []const u8 = "", 2240 action: []const u8 = "submit", 2241 }; 2242 2243 fn initConfirmationForm(allocator: std.mem.Allocator, action: []const u8, buttons: []const ConfirmationButton) !Center { 2244 var box = try wgt.Box(Widget).init(allocator, .{ .border = null, .round_corners = true, .direction = .vert }); 2245 errdefer box.deinit(allocator); 2246 box.getFocus().kind = .{ .custom = action }; 2247 2248 var prompt = try wgt.Text.init(allocator, "are you sure?"); 2249 errdefer prompt.deinit(allocator); 2250 try box.children.put(allocator, prompt.getFocus().id, .{ .widget = .{ .text = prompt }, .rect = null, .min_size = null }); 2251 2252 for (buttons, 0..) |options, index| { 2253 var button = try wgt.TextBox.init(allocator, options.label, .{ .border = .single, .round_corners = true, .wrap_kind = .none, .bottom_label = .{ .text = options.bottom_label } }); 2254 errdefer button.deinit(allocator); 2255 button.getFocus().mode = .all; 2256 button.getFocus().kind = .{ .custom = options.action }; 2257 try box.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = null }); 2258 if (index == 0) box.getFocus().child_id = button.getFocus().id; 2259 } 2260 2261 return Center.init(allocator, .{ .box = box }); 2262 } 2263 2264 // a form's submit button, then a spacer absorbing the leftover 2265 // min-height the box hands its last child, so the button keeps its 2266 // natural height 2267 fn addSubmitButtonLabeled(allocator: std.mem.Allocator, box: *wgt.Box(Widget), label: []const u8) !void { 2268 { 2269 var submit = try SubmitButton.initLabeled(allocator, label); 2270 errdefer submit.deinit(allocator); 2271 try box.children.put(allocator, submit.getFocus().id, .{ .widget = .{ .submit_button = submit }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 2272 } 2273 { 2274 var spacer = try Spacer.init(allocator); 2275 errdefer spacer.deinit(allocator); 2276 try box.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 2277 } 2278 } 2279 2280 // the conflict resolve form. the "use this" links are navigations that 2281 // flip the url's theirs: pick, reloading the prefills; one submit 2282 // settles every conflict. the form: subtree makes the web renderer POST 2283 // to the resolve route. 2284 fn initResolveForm(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self) !wgt.Box(Widget) { 2285 const aa = session.page_arena.allocator(); 2286 // init only picks the resolve view once it has read the conflict 2287 const conflict = if (data.conflict) |*c| c else unreachable; 2288 2289 var box = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .vert }); 2290 errdefer box.deinit(allocator); 2291 const route = resolveRoute(data.identity, data.selected_id, data.theirs_picks) orelse return error.RouteTooLong; 2292 box.getFocus().kind = .{ .custom = try aa.print("form:{s}", .{try route.toUrl(session.page_arena)}) }; 2293 2294 if (conflict.title) |*fc| { 2295 try addLabel(allocator, &box, "title conflict:"); 2296 try addFieldConflict(allocator, &box, session, data, "title", fc); 2297 } 2298 if (conflict.labels) |*fc| { 2299 try addLabel(allocator, &box, "labels conflict:"); 2300 try addFieldConflict(allocator, &box, session, data, "labels", fc); 2301 } 2302 if (comptime @hasField(Data.Conflict, "status")) { 2303 if (conflict.status) |*fc| { 2304 try addLabel(allocator, &box, "status conflict:"); 2305 try addAtomicConflict(allocator, &box, session, data, "status", fc); 2306 } 2307 } 2308 if (comptime @hasField(Data.Conflict, "revision")) { 2309 if (conflict.revision) |*fc| { 2310 try addLabel(allocator, &box, "revision conflict:"); 2311 try addAtomicConflict(allocator, &box, session, data, "revision", fc); 2312 } 2313 if (conflict.target_branch) |*fc| { 2314 try addLabel(allocator, &box, "target branch conflict:"); 2315 try addAtomicConflict(allocator, &box, session, data, "target_branch", fc); 2316 } 2317 } 2318 2319 if (conflict.description) |*desc| { 2320 try addLabel(allocator, &box, "description conflict:"); 2321 var hunk_index: usize = 0; 2322 for (desc.chunks, 0..) |*chunk, chunk_index| { 2323 switch (chunk.*) { 2324 .same => |text| { 2325 var tb = try wgt.TextBox.init(allocator, text, .{ .border = .single, .round_corners = true, .wrap_kind = .word }); 2326 errdefer tb.deinit(allocator); 2327 tb.getFocus().mode = .all; 2328 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 2329 }, 2330 // an auto-resolved chunk: only one side changed it, so its 2331 // version stands, shown distinctly with its author 2332 .auto => |auto| { 2333 const author = if (auto.theirs) desc.theirs_author else desc.ours_author; 2334 const verb: []const u8 = if (auto.text == null) "removed" else "edited"; 2335 const label = switch (author) { 2336 .user_name, .email => |name| try aa.print(" {s} by {s} ", .{ verb, name }), 2337 .unknown => try aa.print(" {s} by {s} ", .{ verb, if (auto.theirs) @as([]const u8, "them") else "us" }), 2338 }; 2339 var tb = try wgt.TextBox.init(allocator, auto.text orelse "(removed)", .{ .border = .single, .round_corners = true, .wrap_kind = .word, .top_label = .{ .text = label } }); 2340 errdefer tb.deinit(allocator); 2341 tb.getFocus().mode = .all; 2342 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 2343 }, 2344 .conflict => |hunk| { 2345 // a blank row on each side sets the group apart from 2346 // the flowing shared and auto-resolved chunks 2347 if (chunk_index > 0) try addGap(allocator, &box); 2348 const name = try aa.print("d{d}", .{hunk_index}); 2349 hunk_index += 1; 2350 const picked_theirs = data.theirsPicked(name); 2351 try addVersionRow(allocator, &box, try sideLabel(aa, desc.ours_author, true), hunk.ours orelse "", try useThisLink(session, data, name, false)); 2352 try addVersionRow(allocator, &box, try sideLabel(aa, desc.theirs_author, false), hunk.theirs orelse "", try useThisLink(session, data, name, true)); 2353 var resolution_input = try wgt.TextInput.init(allocator, .{ .top_label = .{ .text = " resolution " }, .name = name, .visible_width = null, .round_corners = true, .render_content = session.is_terminal, .multiline = true }); 2354 errdefer resolution_input.deinit(allocator); 2355 resolution_input.getFocus().mode = .all; 2356 try resolution_input.setContent(allocator, (if (picked_theirs) hunk.theirs else hunk.ours) orelse ""); 2357 try box.children.put(allocator, resolution_input.getFocus().id, .{ .widget = .{ .text_input = resolution_input }, .rect = null, .min_size = null }); 2358 if (chunk_index + 1 < desc.chunks.len) try addGap(allocator, &box); 2359 }, 2360 } 2361 } 2362 } 2363 2364 try addSubmitButtonLabeled(allocator, &box, "submit"); 2365 2366 box.getFocus().child_id = box.children.keys()[0]; 2367 return box; 2368 } 2369 2370 // one conflicted scalar field: both sides' version rows, then the 2371 // resolution input prefilled from the picked side. 2372 fn addFieldConflict(allocator: std.mem.Allocator, box: *wgt.Box(Widget), session: *ui.Session, data: *const Self, comptime name: []const u8, fc: *const FieldConflict) !void { 2373 const aa = session.page_arena.allocator(); 2374 try addVersionRow(allocator, box, try sideLabel(aa, fc.ours.author, true), fc.ours.text, try useThisLink(session, data, name, false)); 2375 try addVersionRow(allocator, box, try sideLabel(aa, fc.theirs.author, false), fc.theirs.text, try useThisLink(session, data, name, true)); 2376 2377 var resolution_input = try wgt.TextInput.init(allocator, .{ .top_label = .{ .text = " resolution " }, .name = name, .visible_width = null, .round_corners = true, .render_content = session.is_terminal }); 2378 errdefer resolution_input.deinit(allocator); 2379 resolution_input.getFocus().mode = .all; 2380 try resolution_input.setContent(allocator, if (data.theirsPicked(name)) fc.theirs.text else fc.ours.text); 2381 try box.children.put(allocator, resolution_input.getFocus().id, .{ .widget = .{ .text_input = resolution_input }, .rect = null, .min_size = null }); 2382 } 2383 2384 // atomic fields can only pick one complete side 2385 fn addAtomicConflict(allocator: std.mem.Allocator, box: *wgt.Box(Widget), session: *ui.Session, data: *const Self, comptime name: []const u8, fc: *const FieldConflict) !void { 2386 const aa = session.page_arena.allocator(); 2387 try addVersionRow(allocator, box, try sideLabel(aa, fc.ours.author, true), fc.ours.text, try useThisLink(session, data, name, false)); 2388 try addVersionRow(allocator, box, try sideLabel(aa, fc.theirs.author, false), fc.theirs.text, try useThisLink(session, data, name, true)); 2389 var selected = try wgt.TextBox.init(allocator, if (data.theirsPicked(name)) fc.theirs.text else fc.ours.text, .{ 2390 .border = .single, 2391 .round_corners = true, 2392 .wrap_kind = .word, 2393 .top_label = .{ .text = " resolution " }, 2394 }); 2395 errdefer selected.deinit(allocator); 2396 selected.getFocus().mode = .all; 2397 try box.children.put(allocator, selected.getFocus().id, .{ .widget = .{ .text_box = selected }, .rect = null, .min_size = null }); 2398 } 2399 2400 // a blank row setting a conflict group apart from its neighbors 2401 fn addGap(allocator: std.mem.Allocator, box: *wgt.Box(Widget)) !void { 2402 var text = try wgt.Text.init(allocator, " "); 2403 errdefer text.deinit(allocator); 2404 try box.children.put(allocator, text.getFocus().id, .{ .widget = .{ .text = text }, .rect = null, .min_size = null }); 2405 } 2406 2407 fn addLabel(allocator: std.mem.Allocator, box: *wgt.Box(Widget), content: []const u8) !void { 2408 var label = try SectionLabel.init(allocator, content); 2409 errdefer label.deinit(allocator); 2410 try box.children.put(allocator, label.getFocus().id, .{ .widget = .{ .section_label = label }, .rect = null, .min_size = null }); 2411 } 2412 2413 // one side of a conflict: the "use this" link on the left, the version 2414 // itself as a labeled read-only box. 2415 fn addVersionRow(allocator: std.mem.Allocator, box: *wgt.Box(Widget), label: []const u8, text: []const u8, link: []const u8) !void { 2416 var row = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .horiz }); 2417 errdefer row.deinit(allocator); 2418 2419 { 2420 const use_label = "use this"; 2421 var use = try wgt.TextBox.init(allocator, use_label, .{ .border = .single, .round_corners = true, .wrap_kind = .none }); 2422 errdefer use.deinit(allocator); 2423 use.getFocus().mode = .all; 2424 use.getFocus().kind = .{ .custom = link }; 2425 try row.children.put(allocator, use.getFocus().id, .{ 2426 .widget = .{ .text_box = use }, 2427 .rect = null, 2428 .min_size = .{ .width = use_label.len + 2, .height = null }, 2429 .max_size = .{ .width = use_label.len + 2, .height = null }, 2430 }); 2431 } 2432 2433 { 2434 var tb = try wgt.TextBox.init(allocator, if (text.len > 0) text else "(removed)", .{ .border = .single, .round_corners = true, .wrap_kind = .word, .top_label = .{ .text = label } }); 2435 errdefer tb.deinit(allocator); 2436 tb.getFocus().mode = .all; 2437 try row.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 2438 } 2439 2440 row.getFocus().child_id = row.children.keys()[0]; 2441 try box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .box = row }, .rect = null, .min_size = null }); 2442 } 2443 2444 // the label of one side's version box, naming its author 2445 fn sideLabel(aa: std.mem.Allocator, author: ui.Author, ours: bool) ![]const u8 { 2446 return switch (author) { 2447 .user_name, .email => |name| try aa.print(" edited by {s} ", .{name}), 2448 .unknown => if (ours) " current version " else " their version ", 2449 }; 2450 } 2451 2452 // the a: link that reloads the resolve page with `name` picked from ours 2453 // or theirs, reprefilling its resolution input 2454 fn useThisLink(session: *ui.Session, data: *const Self, name: []const u8, theirs: bool) ![]const u8 { 2455 const aa = session.page_arena.allocator(); 2456 var picks: std.ArrayList(u8) = .empty; 2457 var it = std.mem.splitScalar(u8, data.theirs_picks, ','); 2458 while (it.next()) |pick| { 2459 if (pick.len == 0 or std.mem.eql(u8, pick, name)) continue; 2460 if (picks.items.len > 0) try picks.append(aa, ','); 2461 try picks.appendSlice(aa, pick); 2462 } 2463 if (theirs) { 2464 if (picks.items.len > 0) try picks.append(aa, ','); 2465 try picks.appendSlice(aa, name); 2466 } 2467 const route = resolveRoute(data.identity, data.selected_id, picks.items) orelse return error.RouteTooLong; 2468 return aa.print("a:{s}", .{try route.toUrl(session.page_arena)}); 2469 } 2470 2471 fn addRow(allocator: std.mem.Allocator, box: *wgt.Box(Widget), text: []const u8, bottom_label: []const u8, link: []const u8) !void { 2472 var row = try wgt.TextBox.init(allocator, text, .{ .border = .hidden, .round_corners = true, .wrap_kind = .word, .bottom_label = .{ .text = bottom_label } }); 2473 errdefer row.deinit(allocator); 2474 row.getFocus().mode = .all; 2475 if (link.len != 0) row.getFocus().kind = .{ .custom = link }; 2476 try box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .text_box = row }, .rect = null, .min_size = null, .max_size = .{ .width = null, .height = 5 } }); 2477 } 2478 2479 pub fn deinit(self: *This, allocator: std.mem.Allocator) void { 2480 self.box.deinit(allocator); 2481 } 2482 2483 fn header(self: *This) *HeaderType { 2484 return &@field(self.box.children.values()[header_index].widget, Data.header_widget_name); 2485 } 2486 2487 fn viewStack(self: *This) *wgt.Stack(Widget) { 2488 return &self.box.children.values()[stack_index].widget.stack; 2489 } 2490 2491 // `index`'s master-detail split inside the stack. 2492 fn resultsBox(self: *This, index: usize) *wgt.Box(Widget) { 2493 return &self.viewStack().children.values()[index].box; 2494 } 2495 2496 // the labels view's flow inside the stack. 2497 fn labelsView(self: *This) *WordFlow { 2498 return &self.viewStack().children.values()[labels_view_index].word_flow; 2499 } 2500 2501 // no labels and no filter to clear, which shows the empty message 2502 fn labelsEmpty(data: *const Self) bool { 2503 return data.label.len == 0 and data.labels.len == 0; 2504 } 2505 2506 // the non-confirmation form inside the stack 2507 fn formBox(self: *This) ?*wgt.Box(Widget) { 2508 return switch (self.viewStack().children.values()[form_view_index]) { 2509 .box => |*box| box, 2510 // the resolve form sits inside a scroll on the terminal 2511 .scroll => |*scroll| &scroll.child.box, 2512 else => null, 2513 }; 2514 } 2515 2516 fn threadForm(self: *This) ?*wgt.Box(Widget) { 2517 if (supports_conflicts and self.data.view == .resolve) return null; 2518 return switch (self.data.view) { 2519 .new_comment, .edit_comment => null, 2520 else => self.formBox(), 2521 }; 2522 } 2523 2524 fn formField(form: *wgt.Box(Widget), name: []const u8) !*wgt.TextInput { 2525 for (form.children.values()) |*child| switch (child.widget) { 2526 .text_input => |*text_input| if (std.mem.eql(u8, text_input.options.name, name)) return text_input, 2527 else => {}, 2528 }; 2529 return error.MissingFormField; 2530 } 2531 2532 fn formWidget(form: *wgt.Box(Widget), comptime widget_kind: std.meta.Tag(Widget)) ?*@FieldType(Widget, @tagName(widget_kind)) { 2533 for (form.children.values()) |*child| { 2534 if (child.widget == widget_kind) return &@field(child.widget, @tagName(widget_kind)); 2535 } 2536 return null; 2537 } 2538 2539 fn confirmationForm(self: *This) ?*wgt.Box(Widget) { 2540 return switch (self.viewStack().children.values()[form_view_index]) { 2541 .center => |*center| switch (center.child.*) { 2542 .box => |*box| box, 2543 else => null, 2544 }, 2545 else => null, 2546 }; 2547 } 2548 2549 // the resolve form's scroll on the terminal (the web page scrolls itself) 2550 fn resolveScroll(self: *This) ?*wgt.Scroll(Widget) { 2551 return switch (self.viewStack().children.values()[form_view_index]) { 2552 .scroll => |*scroll| scroll, 2553 else => null, 2554 }; 2555 } 2556 2557 fn listScroll(self: *This, index: usize) *wgt.Scroll(Widget) { 2558 return &self.resultsBox(index).children.values()[list_index].widget.scroll; 2559 } 2560 2561 fn listBox(self: *This, index: usize) *wgt.Box(Widget) { 2562 return &self.listScroll(index).child.box; 2563 } 2564 2565 fn detailFrame(self: *This, index: usize) *wgt.Box(Widget) { 2566 return &self.resultsBox(index).children.values()[detail_index].widget.box; 2567 } 2568 2569 fn detail(self: *This, index: usize) *DetailType { 2570 return &@field(self.detailFrame(index).children.values()[widget.list_pane_index].widget, Data.detail_widget_name); 2571 } 2572 2573 fn window(self: *This, index: usize) *const Window { 2574 if (supports_conflicts and index == conflict_view_index) return &self.data.conflicts; 2575 if (supports_drafts and index == drafts_view_index) return &self.data.drafts; 2576 return self.data.window(splitStatus(index)); 2577 } 2578 2579 // an empty first window, which shows the empty message 2580 fn windowEmpty(win: *const Window) bool { 2581 return win.items.len == 0 and win.prev_id == null; 2582 } 2583 2584 fn stackSelectedIndex(self: *This) ?usize { 2585 const stack = self.viewStack(); 2586 const cid = stack.getFocus().child_id orelse return null; 2587 return stack.children.getIndex(cid); 2588 } 2589 2590 // the stack's selected master-detail split (null when the labels view, the 2591 // new-thread form, or the empty message shows). 2592 fn selectedSplitIndex(self: *This) ?usize { 2593 const idx = self.stackSelectedIndex() orelse return null; 2594 const split = idx < status_count or (supports_drafts and idx == drafts_view_index) or idx == conflict_view_index; 2595 return if (split and !windowEmpty(self.window(idx))) idx else null; 2596 } 2597 2598 fn detailActive(self: *This, index: usize) bool { 2599 const rb = self.resultsBox(index); 2600 const cid = rb.getFocus().child_id orelse return false; 2601 return rb.children.getIndex(cid) == detail_index; 2602 } 2603 2604 fn headerActive(self: *This) bool { 2605 const cid = self.box.getFocus().child_id orelse return false; 2606 return self.box.children.getIndex(cid) == header_index; 2607 } 2608 2609 fn labelsViewActive(self: *This) bool { 2610 if (self.headerActive()) return false; 2611 return self.stackSelectedIndex() == labels_view_index; 2612 } 2613 2614 fn formViewActive(self: *This) bool { 2615 if (self.headerActive()) return false; 2616 return self.stackSelectedIndex() == form_view_index; 2617 } 2618 2619 // the selected thread's index, or null when a window-navigation row is 2620 // selected (a leading "previous" row shifts the thread rows down by one). 2621 fn selectedThreadIndex(self: *This, index: usize) ?usize { 2622 const lb = self.listBox(index); 2623 const cid = lb.getFocus().child_id orelse return null; 2624 const idx = lb.children.getIndex(cid) orelse return null; 2625 const win = self.window(index); 2626 const lead: usize = if (win.prev_id != null) 1 else 0; 2627 if (idx < lead or idx - lead >= win.items.len) return null; 2628 return idx - lead; 2629 } 2630 2631 // swap the detail pane to the selected thread when it changes 2632 fn refreshDetail(self: *This, allocator: std.mem.Allocator, index: usize) !void { 2633 const selected = self.selectedThreadIndex(index) orelse return; 2634 const entry = self.window(index).items[selected]; 2635 const changed = if (self.detail(index).entry) |current| !std.mem.eql(u8, current.id, entry.id) else true; 2636 if (!changed) return; 2637 try self.detail(index).setEntry(allocator, entry); 2638 widget.setListBackLink(self.detailFrame(index), try rowLink(self.session.page_arena, self.data, entry.id)); 2639 } 2640 2641 pub fn build(self: *This, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2642 self.clearGrid(); 2643 2644 // each header tab maps 1:1 to a stack child by position 2645 if (self.header().getSelectedIndex()) |index| { 2646 const stack = self.viewStack(); 2647 stack.getFocus().child_id = stack.children.keys()[index]; 2648 } 2649 2650 if (self.selectedSplitIndex()) |i| { 2651 // whether the detail pane fits beside the list, which decides 2652 // the list cap below and whether the detail carries a way back 2653 const both_panes_fit = if (constraint.max_size.width) |w| w >= list_max_width + detail_min_width else true; 2654 try self.refreshDetail(allocator, i); 2655 widget.showListBack(self.detailFrame(i), !both_panes_fit); 2656 2657 // the selected list row shows a border (the focused TextBox 2658 // upgrades it to a double border itself); the rest stay borderless. 2659 const lb = self.listBox(i); 2660 for (lb.children.keys(), lb.children.values()) |id, *child| { 2661 switch (child.widget) { 2662 .text_box => |*tb| widget.markSelected(tb, lb.getFocus().child_id == id), 2663 else => {}, 2664 } 2665 } 2666 2667 // cap the list at list_max_width only while the detail pane fits 2668 // beside it. the box drops the detail when the width can't hold 2669 // both minimums, so when it's that narrow we lift the cap and let 2670 // the list fill the whole width. 2671 self.resultsBox(i).children.values()[list_index].max_size = if (both_panes_fit) .{ .width = list_max_width, .height = null } else null; 2672 2673 // stretch the detail pane across the rest of the width so it fills 2674 // the area rather than shrinking to its content; its scroll fills 2675 // the pane. 2676 if (constraint.max_size.width) |w| { 2677 self.resultsBox(i).children.values()[detail_index].min_size = .{ .width = if (both_panes_fit) w - list_max_width else w, .height = null }; 2678 } else { 2679 self.resultsBox(i).children.values()[detail_index].min_size = .{ .width = detail_min_width, .height = null }; 2680 } 2681 } 2682 2683 // refresh the form inputs' entries in the session's focus-id -> input 2684 // map with this frame's addresses, so the web/wasm form handling can 2685 // find them by focus id 2686 if (self.threadForm()) |form| { 2687 const failure = if (self.session.formFeedback(feedback_tag)) |saved| saved.failure else null; 2688 (try formField(form, "title")).options.top_label.text = if (failure == .required_title) " title (required) " else " title "; 2689 if (comptime supports_drafts) { 2690 if (formWidget(form, .patch_source)) |source| { 2691 source.field().options.top_label.text = 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 "; 2692 const submit = formWidget(form, .submit_button) orelse return error.MissingFormField; 2693 try submit.setLabel(allocator, switch (source.selectedKind()) { 2694 .fork => "submit draft", 2695 .branch => "submit patch", 2696 }); 2697 } 2698 (try formField(form, "target_branch")).options.top_label.text = 2699 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 "; 2700 } 2701 } 2702 if (self.formBox()) |form| { 2703 const inputs_arena = self.session.arena.allocator(); 2704 for (form.children.values()) |*child| switch (child.widget) { 2705 .text_input => |*ti| try self.session.text_inputs.put(inputs_arena, ti.getFocus().id, ti), 2706 else => {}, 2707 }; 2708 } 2709 2710 try self.box.build(allocator, constraint, root_focus); 2711 } 2712 2713 pub fn input(self: *This, allocator: std.mem.Allocator, raw_key: Key, root_focus: *Focus) !void { 2714 // wheel input moves focus like the arrows 2715 const key: Key = switch (raw_key) { 2716 .scroll_up => .arrow_up, 2717 .scroll_down => .arrow_down, 2718 else => raw_key, 2719 }; 2720 2721 if (self.headerActive()) { 2722 // down from the sub-header re-enters the stack if the selected 2723 // view has something to focus; enter in the search box submits 2724 // the query; other keys move within the sub-header. 2725 if (inp.vertDirection(key) == .down) { 2726 const enterable = if (self.stackSelectedIndex()) |idx| 2727 if (idx == labels_view_index) 2728 !labelsEmpty(self.data) 2729 else if (idx == form_view_index) 2730 // nothing focusable in the unauthorized view 2731 self.viewStack().children.values()[idx] != .unauthorized 2732 else 2733 !windowEmpty(self.window(idx)) 2734 else 2735 false; 2736 if (enterable) root_focus.setFocus(self.box.children.keys()[stack_index]); 2737 } else if (key == .enter) { 2738 if (try self.header().submittedText(allocator)) |submission| { 2739 defer allocator.free(submission.text); 2740 switch (submission.kind) { 2741 .search => try self.submitSearch(submission.text), 2742 .assignee => try self.submitAssignee(submission.text), 2743 } 2744 } 2745 } else { 2746 try self.header().input(allocator, key, root_focus); 2747 } 2748 return; 2749 } 2750 if (self.labelsViewActive()) { 2751 self.labelsViewInput(key, root_focus); 2752 return; 2753 } 2754 if (self.formViewActive()) { 2755 if (confirmation(self.data.view) != null) { 2756 try self.confirmationFormInput(allocator, key, root_focus); 2757 } else if (supports_conflicts and self.data.view == .resolve) { 2758 try self.resolveInput(allocator, key, root_focus); 2759 } else { 2760 try self.formInput(allocator, key, root_focus); 2761 } 2762 return; 2763 } 2764 const i = self.selectedSplitIndex() orelse return; 2765 if (self.detailActive(i)) { 2766 // the way back to the list, by enter or a click 2767 if (widget.listBackId(self.detailFrame(i))) |id| { 2768 if (inp.activated(root_focus, id, key)) return self.focusList(i, root_focus); 2769 if (root_focus.grandchild_id == id) return switch (key) { 2770 .arrow_left => self.focusList(i, root_focus), 2771 .arrow_up => self.focusHeader(root_focus), 2772 .arrow_down => _ = self.detail(i).focusFirst(root_focus), 2773 else => {}, 2774 }; 2775 } 2776 switch (try self.detail(i).inputWithExit(allocator, key, root_focus)) { 2777 .none => {}, 2778 .list => self.focusList(i, root_focus), 2779 .header => if (widget.listBackId(self.detailFrame(i))) |id| root_focus.setFocus(id) else self.focusHeader(root_focus), 2780 } 2781 } else { 2782 try self.listInput(allocator, i, key, root_focus); 2783 } 2784 } 2785 2786 // navigate to the typed query's results, keeping the label filter and the 2787 // status list the page shows. an empty box clears a filtered page and 2788 // does nothing on any other one. 2789 fn submitSearch(self: *This, text: []const u8) !void { 2790 if (text.len == 0 and self.data.search == null) return; 2791 const selected = self.header().getSelectedIndex() orelse 0; 2792 const status: Status = if (selected < status_count) splitStatus(selected) else @fromBackingInt(@intCast(0)); 2793 var route = listRoute(self.data.identity, status, self.data.label, "") orelse return; 2794 const assignee = assigneeFilter(self.data); 2795 if (assignee.len != 0) route = route.withEncodedAssignee(assignee) orelse return; 2796 try self.session.navigate(route.withSearch(text) orelse return); 2797 } 2798 2799 // navigate to the issues assigned to the typed name or email, keeping 2800 // the label, the search and the status list. an empty box clears the 2801 // filter and does nothing on an unfiltered page. 2802 fn submitAssignee(self: *This, typed: []const u8) !void { 2803 const text = std.mem.trim(u8, typed, " \t"); 2804 if (text.len == 0 and assigneeFilter(self.data).len == 0) return; 2805 const selected = self.header().getSelectedIndex() orelse 0; 2806 const status: Status = if (selected < status_count) splitStatus(selected) else @fromBackingInt(@intCast(0)); 2807 var route = listRoute(self.data.identity, status, self.data.label, "") orelse return; 2808 if (self.data.search) |search| route = route.withSearch(search) orelse return; 2809 try self.session.navigate(route.withEncodedAssignee(try ui.urlEncodeRef(self.session.page_arena.allocator(), text)) orelse return); 2810 } 2811 2812 // arrow keys move the label selection; up from the top row crosses to the 2813 // header tabs. 2814 fn labelsViewInput(self: *This, key: Key, root_focus: *Focus) void { 2815 const tf = self.labelsView(); 2816 const cid = tf.focus.child_id orelse return; 2817 const cur = tf.indexOfFocusId(cid) orelse return; 2818 const count = tf.text_boxes.items.len; 2819 switch (key) { 2820 .arrow_left => if (cur > 0) root_focus.setFocus(tf.text_boxes.items[cur - 1].getFocus().id), 2821 .arrow_right => if (cur + 1 < count) root_focus.setFocus(tf.text_boxes.items[cur + 1].getFocus().id), 2822 .arrow_up => if (tf.rowStep(cur, false)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id) else self.focusHeader(root_focus), 2823 .arrow_down => if (tf.rowStep(cur, true)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id), 2824 .home => root_focus.setFocus(tf.text_boxes.items[0].getFocus().id), 2825 .end => root_focus.setFocus(tf.text_boxes.items[count - 1].getFocus().id), 2826 else => {}, 2827 } 2828 } 2829 2830 fn listInput(self: *This, allocator: std.mem.Allocator, index: usize, key: Key, root_focus: *Focus) !void { 2831 // up/down (and the scroll wheel) move the selection a row; page up/down 2832 // jump a fixed amount. right/Enter cross into the detail pane. up from 2833 // the top row crosses into the header tabs. 2834 if (inp.rowDelta(key, @intCast(self.listBox(index).children.count()))) |delta| { 2835 const lb = self.listBox(index); 2836 const at_top = if (lb.getFocus().child_id) |cid| lb.children.getIndex(cid) == 0 else true; 2837 if (delta < 0 and at_top) return self.focusHeader(root_focus); 2838 moveRowFocus(lb, self.listScroll(index), root_focus, delta); 2839 return; 2840 } 2841 switch (key) { 2842 .enter, .arrow_right => self.focusDetail(index, root_focus), 2843 // when the window is too narrow to lay out the detail pane beside 2844 // the list, a click on a row opens it like enter. the row was just 2845 // selected, so the detail is swapped to it ahead of the build. 2846 .mouse => |mouse| if (self.resultsBox(index).children.values()[detail_index].rect == null and widget.clickOnSelectedRow(self.listBox(index), root_focus, mouse)) { 2847 try self.refreshDetail(allocator, index); 2848 self.focusDetail(index, root_focus); 2849 }, 2850 else => {}, 2851 } 2852 } 2853 2854 fn formInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2855 const form = self.formBox() orelse return; 2856 const cid = form.getFocus().child_id orelse return; 2857 const cur = form.children.getIndex(cid) orelse return; 2858 const child = &form.children.values()[cur]; 2859 2860 if (child.widget == .text_input) { 2861 const text_input = &child.widget.text_input; 2862 if (text_input.options.multiline and try multilineKeepsKey(text_input, allocator, key)) 2863 return child.widget.input(allocator, key, root_focus); 2864 } 2865 2866 const on_submit = child.widget == .submit_button; 2867 switch (key) { 2868 .arrow_up, .back_tab => return if (formStep(form, cur, false)) |i| 2869 root_focus.setFocus(form.children.keys()[i]) 2870 else 2871 self.focusHeader(root_focus), 2872 .arrow_down, .tab => return if (formStep(form, cur, true)) |i| root_focus.setFocus(form.children.keys()[i]), 2873 .enter => if (on_submit) return self.submitForm(allocator), 2874 .mouse => |mouse| if (on_submit) { 2875 if (inp.leftClickOn(root_focus, child.widget.submit_button.buttonId(), mouse)) return self.submitForm(allocator); 2876 }, 2877 else => {}, 2878 } 2879 try child.widget.input(allocator, key, root_focus); 2880 } 2881 2882 fn confirmationFormInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2883 const form = self.confirmationForm() orelse return; 2884 const child_id = form.getFocus().child_id orelse return; 2885 const index = form.children.getIndex(child_id) orelse return; 2886 switch (key) { 2887 .arrow_up, .back_tab => if (index > 1) 2888 root_focus.setFocus(form.children.keys()[index - 1]) 2889 else 2890 self.focusHeader(root_focus), 2891 .arrow_down, .tab => if (index + 1 < form.children.count()) root_focus.setFocus(form.children.keys()[index + 1]), 2892 .enter => try self.submitConfirmation(allocator, index), 2893 .mouse => |mouse| for (form.children.keys()[1..], 1..) |button_id, button_index| { 2894 if (inp.leftClickOn(root_focus, button_id, mouse)) { 2895 try self.submitConfirmation(allocator, button_index); 2896 break; 2897 } 2898 }, 2899 else => {}, 2900 } 2901 } 2902 2903 // the multiline hunk inputs keep enter and any up/down that has a row to 2904 // move to; enter on a "use this" link is a navigation the host follows 2905 // before this runs. 2906 fn resolveInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2907 const form = self.formBox() orelse return; 2908 const cid = form.getFocus().child_id orelse return; 2909 const cur = form.children.getIndex(cid) orelse return; 2910 const child = &form.children.values()[cur]; 2911 2912 if (child.widget == .text_input) { 2913 const ti = &child.widget.text_input; 2914 if (ti.options.multiline and try multilineKeepsKey(ti, allocator, key)) 2915 return child.widget.input(allocator, key, root_focus); 2916 } 2917 2918 const on_submit = child.widget == .submit_button; 2919 2920 switch (key) { 2921 .arrow_up, .back_tab => if (formStep(form, cur, false)) |i| self.focusResolveChild(form, i, root_focus) else self.focusHeader(root_focus), 2922 .arrow_down, .tab => if (formStep(form, cur, true)) |i| self.focusResolveChild(form, i, root_focus), 2923 .arrow_left, .arrow_right => switch (child.widget) { 2924 .box => |*row| _ = widget.moveInRow(row, root_focus, key == .arrow_right), 2925 else => try child.widget.input(allocator, key, root_focus), 2926 }, 2927 .enter => if (on_submit) try self.submitResolution(allocator), 2928 .mouse => |mouse| if (on_submit) { 2929 if (inp.leftClickOn(root_focus, child.widget.submit_button.buttonId(), mouse)) try self.submitResolution(allocator); 2930 }, 2931 else => try child.widget.input(allocator, key, root_focus), 2932 } 2933 } 2934 2935 // a multiline input keeps enter and any up/down that has a row to move to 2936 fn multilineKeepsKey(text_input: *wgt.TextInput, allocator: std.mem.Allocator, key: Key) !bool { 2937 return switch (key) { 2938 .enter => true, 2939 .arrow_up => !try text_input.cursorOnFirstRow(allocator), 2940 .arrow_down => !try text_input.cursorOnLastRow(allocator), 2941 else => false, 2942 }; 2943 } 2944 2945 // the neighboring form control, skipping spacers and blank rows 2946 fn formStep(form: *wgt.Box(Widget), cur: usize, down: bool) ?usize { 2947 var i = cur; 2948 while (true) { 2949 if (down) { 2950 i += 1; 2951 if (i >= form.children.count()) return null; 2952 } else { 2953 if (i == 0) return null; 2954 i -= 1; 2955 } 2956 const child = &form.children.values()[i]; 2957 if (child.hidden) continue; 2958 switch (child.widget) { 2959 .spacer, .text => continue, 2960 else => return i, 2961 } 2962 } 2963 } 2964 2965 // focus the resolve-form child at `i` (a version row focuses its selected 2966 // child) and keep it visible on the terminal 2967 fn focusResolveChild(self: *This, form: *wgt.Box(Widget), i: usize, root_focus: *Focus) void { 2968 const child = &form.children.values()[i]; 2969 switch (child.widget) { 2970 .box => |*row| root_focus.setFocus(row.getFocus().child_id orelse form.children.keys()[i]), 2971 else => root_focus.setFocus(form.children.keys()[i]), 2972 } 2973 if (self.session.is_terminal) { 2974 if (self.resolveScroll()) |sc| { 2975 if (child.rect) |rect| sc.scrollToRect(rect); 2976 } 2977 } 2978 } 2979 2980 // re-emit the conflicted thread's event with the resolved content; any 2981 // event on the thread settles its conflict entry. this is the terminal 2982 // path; the web posts the form to the resolve route. 2983 fn submitResolution(self: *This, allocator: std.mem.Allocator) !void { 2984 if (comptime wasm) return; 2985 const io = self.session.io orelse return; 2986 const src = self.data.repo_source orelse return; 2987 const entry = self.data.selectedThread() orelse return; 2988 const author = ((try self.session.authorize(self.data.identity, .write, kind)) orelse return).author; 2989 const form = self.formBox() orelse return; 2990 2991 // gather the inputs by name; the d<n> hunk inputs appear in chunk order 2992 var title: ?[]u8 = null; 2993 var labels: ?[]u8 = null; 2994 var hunks: std.ArrayList([]const u8) = .empty; 2995 defer { 2996 if (title) |t| allocator.free(t); 2997 if (labels) |t| allocator.free(t); 2998 for (hunks.items) |h| allocator.free(h); 2999 hunks.deinit(allocator); 3000 } 3001 for (form.children.values()) |*child| switch (child.widget) { 3002 .text_input => |*ti| { 3003 const text = try ti.text(allocator); 3004 errdefer allocator.free(text); 3005 if (std.mem.eql(u8, ti.options.name, "title")) { 3006 title = text; 3007 } else if (std.mem.eql(u8, ti.options.name, "labels")) { 3008 labels = text; 3009 } else { 3010 try hunks.append(allocator, text); 3011 } 3012 }, 3013 else => {}, 3014 }; 3015 3016 const id_bytes = try evt.parseEventId(entry.id); 3017 3018 switch (src.repo_kind) { 3019 inline else => |repo_kind| { 3020 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 3021 defer any_repo.deinit(io, allocator); 3022 switch (any_repo) { 3023 inline else => |*repo| { 3024 var resolution: Event.Resolve = .{ 3025 .title = title, 3026 .labels = labels, 3027 .hunks = hunks.items, 3028 }; 3029 if (comptime @hasField(Event.Resolve, "theirs")) resolution.theirs = self.data.theirs_picks; 3030 const change = Event.Update{ .resolve = resolution }; 3031 Event.update(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, change, author) catch |err| switch (err) { 3032 // leave the form up for correction 3033 error.InvalidFields => return, 3034 else => |e| return e, 3035 }; 3036 }, 3037 } 3038 }, 3039 } 3040 3041 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 3042 try self.session.navigate(route); 3043 } 3044 3045 // commit a new or edited comment and navigate to its permalink. this is the 3046 // terminal path; the web posts the form to its comment route. 3047 fn submitComment(self: *This, allocator: std.mem.Allocator) !void { 3048 if (comptime wasm) return; 3049 const io = self.session.io orelse return; 3050 const src = self.data.repo_source orelse return; 3051 const actor = (try self.session.authorize(self.data.identity, .read, kind)) orelse return; 3052 const author = actor.author; 3053 const form = self.formBox() orelse return; 3054 const body_input = try formField(form, "body"); 3055 const body = try body_input.text(allocator); 3056 defer allocator.free(body); 3057 if (!evt.Comment.fieldsValid(body)) return; 3058 3059 var thread_id: [evt.event_id_size * 2]u8 = undefined; 3060 @memcpy(&thread_id, self.data.selected_id); 3061 const parent_id = if (self.data.comment_id.len == 0) thread_id else blk: { 3062 var parent: [evt.event_id_size * 2]u8 = undefined; 3063 @memcpy(&parent, self.data.comment_id); 3064 break :blk parent; 3065 }; 3066 var event_id_hex: [evt.event_id_size * 2]u8 = undefined; 3067 const editing = self.data.view == .edit_comment; 3068 if (editing) { 3069 const comment_id = evt.parseEventId(self.data.comment_id) catch return; 3070 if (!try src.canModify(io, allocator, actor, .comment, &comment_id)) return; 3071 } 3072 switch (src.repo_kind) { 3073 inline else => |repo_kind| { 3074 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 3075 defer any_repo.deinit(io, allocator); 3076 switch (any_repo) { 3077 inline else => |*repo| if (editing) { 3078 const comment_id = evt.parseEventId(self.data.comment_id) catch return; 3079 try evt.Comment.update(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, kind, &thread_id, &comment_id, body, author); 3080 event_id_hex = std.fmt.bytesToHex(comment_id, .lower); 3081 } else { 3082 event_id_hex = try evt.Comment.create(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, kind, &thread_id, &parent_id, body, author); 3083 }, 3084 } 3085 }, 3086 } 3087 3088 body_input.clear(allocator); 3089 const route = ui.RoutablePage.repoThreadCommentRoute(kind, self.data.identity, &thread_id, &event_id_hex, 0) orelse return; 3090 try self.session.navigate(route); 3091 } 3092 3093 // terminal form submissions; the web posts to the forms' routes instead 3094 fn submitForm(self: *This, allocator: std.mem.Allocator) !void { 3095 switch (self.data.view) { 3096 .new_comment, .edit_comment => try self.submitComment(allocator), 3097 .edit => try self.submitEditedThread(allocator), 3098 else => try self.submitNewThread(allocator), 3099 } 3100 } 3101 3102 // commit the new thread to the repo's events branch and navigate to it. 3103 // this is the terminal path; the web posts the form to the thread route, 3104 // so the wasm side never runs (or compiles) the repo access below. 3105 fn submitNewThread(self: *This, allocator: std.mem.Allocator) !void { 3106 if (comptime wasm) return; 3107 const io = self.session.io orelse return; 3108 const src = self.data.repo_source orelse return; 3109 const actor = (try self.session.authorize(self.data.identity, .read, kind)) orelse return; 3110 3111 const form = self.threadForm() orelse return; 3112 const title_input = try formField(form, "title"); 3113 const labels_input = try formField(form, "labels"); 3114 const description_input = try formField(form, "description"); 3115 3116 const title = try title_input.text(allocator); 3117 defer allocator.free(title); 3118 const labels = try labels_input.text(allocator); 3119 defer allocator.free(labels); 3120 const description = try description_input.text(allocator); 3121 defer allocator.free(description); 3122 const target_branch = if (comptime supports_drafts) try (try formField(form, "target_branch")).text(allocator) else ""; 3123 defer if (comptime supports_drafts) allocator.free(target_branch); 3124 3125 if (!Event.fieldsValid(title, labels)) { 3126 if (!evt.titleValid(title)) try self.rememberThreadFeedback(.required_title, title, labels, description, target_branch); 3127 return; 3128 } 3129 3130 if (comptime supports_drafts) { 3131 const source = formWidget(form, .patch_source) orelse return error.MissingFormField; 3132 const source_branch = switch (source.selectedKind()) { 3133 .fork => null, 3134 .branch => try source.field().text(allocator), 3135 }; 3136 defer if (source_branch) |branch| allocator.free(branch); 3137 const event_id_hex = Data.create(self.data, self.session, allocator, actor, title, labels, description, target_branch, source_branch) catch |err| { 3138 const failure = FeedbackFailure.fromError(err) orelse return err; 3139 try self.rememberThreadFeedback(failure, title, labels, description, target_branch); 3140 return; 3141 }; 3142 title_input.clear(allocator); 3143 labels_input.clear(allocator); 3144 description_input.clear(allocator); 3145 (try formField(form, "target_branch")).clear(allocator); 3146 const route = ui.RoutablePage.repoThreadCommentsRoute(kind, self.data.identity, &event_id_hex, 0) orelse return; 3147 try self.session.navigate(route); 3148 return; 3149 } 3150 3151 var id_bytes: [evt.event_id_size]u8 = undefined; 3152 io.random(&id_bytes); 3153 const event_id_hex = std.fmt.bytesToHex(id_bytes, .lower); 3154 3155 const event = evt.EventWithId{ 3156 .id = event_id_hex, 3157 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 3158 .author = actor.author, 3159 .event = @unionInit(evt.Event, @tagName(kind), Event{ 3160 .title = title, 3161 .description = description, 3162 .labels = labels, 3163 }), 3164 }; 3165 3166 switch (src.repo_kind) { 3167 inline else => |repo_kind| { 3168 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 3169 defer any_repo.deinit(io, allocator); 3170 switch (any_repo) { 3171 inline else => |*repo| try evt.consume(self.session.eventHost(), .repo, repo_kind, repo.self_repo_opts, io, allocator, repo, evt.events_ref, &.{event}), 3172 } 3173 }, 3174 } 3175 3176 // wipe the form so a return visit starts fresh 3177 title_input.clear(allocator); 3178 labels_input.clear(allocator); 3179 description_input.clear(allocator); 3180 3181 const route = listRoute(self.data.identity, @fromBackingInt(@intCast(0)), "", &event_id_hex) orelse return; 3182 try self.session.navigate(route); 3183 } 3184 3185 // re-emit the selected thread's event with the form's content (status 3186 // preserved), then reload the thread's page. this is the terminal path; 3187 // the web posts the form to the edit route. 3188 fn submitEditedThread(self: *This, allocator: std.mem.Allocator) !void { 3189 if (comptime wasm) return; 3190 const io = self.session.io orelse return; 3191 const entry = self.data.selectedThread() orelse return; 3192 const actor = (try self.session.authorize(self.data.identity, .read, kind)) orelse return; 3193 const author = actor.author; 3194 3195 const form = self.threadForm() orelse return; 3196 const title_input = try formField(form, "title"); 3197 const labels_input = try formField(form, "labels"); 3198 const description_input = try formField(form, "description"); 3199 3200 const title = try title_input.text(allocator); 3201 defer allocator.free(title); 3202 const labels = try labels_input.text(allocator); 3203 defer allocator.free(labels); 3204 const description = try description_input.text(allocator); 3205 defer allocator.free(description); 3206 const target_branch = if (comptime supports_drafts) try (try formField(form, "target_branch")).text(allocator) else ""; 3207 defer if (comptime supports_drafts) allocator.free(target_branch); 3208 3209 if (!Event.fieldsValid(title, labels)) { 3210 if (!evt.titleValid(title)) try self.rememberThreadFeedback(.required_title, title, labels, description, target_branch); 3211 return; 3212 } 3213 const src = self.data.repo_source orelse return; 3214 3215 if (comptime supports_drafts) { 3216 if (entryDraft(entry.*)) { 3217 self.data.editDraft(self.session, allocator, author, entry.id, title, labels, description, target_branch) catch |err| { 3218 const failure = FeedbackFailure.fromError(err) orelse return err; 3219 try self.rememberThreadFeedback(failure, title, labels, description, target_branch); 3220 return; 3221 }; 3222 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 3223 try self.session.navigate(route); 3224 return; 3225 } 3226 } 3227 3228 const id_bytes = try evt.parseEventId(entry.id); 3229 if (!try src.canModify(io, allocator, actor, kind, &id_bytes)) return; 3230 3231 switch (src.repo_kind) { 3232 inline else => |repo_kind| { 3233 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 3234 defer any_repo.deinit(io, allocator); 3235 switch (any_repo) { 3236 inline else => |*repo| if (supports_drafts) { 3237 Event.update(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 3238 .title = title, 3239 .labels = labels, 3240 .description = description, 3241 .target_branch = target_branch, 3242 } }, author) catch |err| { 3243 const failure = FeedbackFailure.fromError(err) orelse return err; 3244 try self.rememberThreadFeedback(failure, title, labels, description, target_branch); 3245 return; 3246 }; 3247 } else if (has_status) 3248 try Event.update(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 3249 .title = title, 3250 .labels = labels, 3251 .description = description, 3252 } }, author) 3253 else 3254 try Event.update(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, title, labels, description, author), 3255 } 3256 }, 3257 } 3258 3259 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 3260 try self.session.navigate(route); 3261 } 3262 3263 fn rememberThreadFeedback(self: *This, failure: FeedbackFailure, title: []const u8, labels: []const u8, description: []const u8, target_branch: []const u8) !void { 3264 const aa = self.session.arena.allocator(); 3265 const source = if (supports_drafts) blk: { 3266 const form = self.threadForm() orelse break :blk null; 3267 break :blk formWidget(form, .patch_source); 3268 } else null; 3269 self.session.data.form_feedback = @unionInit(ui.Session.FormFeedback, @tagName(feedback_tag), .{ 3270 .failure = failure, 3271 .fields = if (comptime supports_drafts) .{ 3272 .title = try aa.dupe(u8, title), 3273 .labels = try aa.dupe(u8, labels), 3274 .description = try aa.dupe(u8, description), 3275 .target_branch = try aa.dupe(u8, target_branch), 3276 .source_branch = if (source) |value| try value.field().text(aa) else "", 3277 .source_kind = if (source) |value| value.selectedKind() else .fork, 3278 } else .{ 3279 .title = try aa.dupe(u8, title), 3280 .labels = try aa.dupe(u8, labels), 3281 .description = try aa.dupe(u8, description), 3282 }, 3283 }); 3284 } 3285 3286 fn submitConfirmation(self: *This, allocator: std.mem.Allocator, button_index: usize) !void { 3287 switch (confirmation(self.data.view) orelse return) { 3288 .publish => if (comptime supports_drafts) { 3289 if (comptime wasm) return; 3290 const entry = self.data.selectedThread() orelse return; 3291 if (!entryDraft(entry.*)) return; 3292 const author = ((try self.session.authorize(self.data.identity, .read, kind)) orelse return).author; 3293 try self.data.publishDraft(self.session, allocator, author, entry.id); 3294 const route = listRoute(self.data.identity, @fromBackingInt(@intCast(0)), "", entry.id) orelse return; 3295 try self.session.navigate(route); 3296 } else unreachable, 3297 .merge => if (comptime supports_merge) { 3298 if (comptime wasm) return; 3299 const entry = self.data.selectedThread() orelse return; 3300 const revision: evt.Patch.MergeRevision = if (button_index > 1) .squash else .source; 3301 if (!Data.canMerge(entry.*, self.session) or entry.mergeability.get(revision) != .clean) return; 3302 const author = ((try self.session.authorize(self.data.identity, .write, kind)) orelse return).author; 3303 self.data.mergePatch(self.session, allocator, author, entry.id, revision) catch |err| switch (err) { 3304 error.MergeConflict, error.MergeCheckUnavailable, error.PatchOutOfDate, error.PatchDataUnavailable => { 3305 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 3306 try self.session.navigate(route); 3307 return; 3308 }, 3309 else => return err, 3310 }; 3311 const merged = std.meta.stringToEnum(Status, "merged") orelse return; 3312 const route = listRoute(self.data.identity, merged, "", entry.id) orelse return; 3313 try self.session.navigate(route); 3314 } else unreachable, 3315 .remove => try self.submitRemove(allocator), 3316 } 3317 } 3318 3319 fn submitRemove(self: *This, allocator: std.mem.Allocator) !void { 3320 const comment = self.data.comment_id.len != 0; 3321 const event_id = if (comment) self.data.comment_id else self.data.selected_id; 3322 const id = evt.parseEventId(event_id) catch return; 3323 try self.removeEvent(allocator, if (comment) .comment else kind, id); 3324 } 3325 3326 fn removeEvent(self: *This, allocator: std.mem.Allocator, event_kind: evt.EventKind, id: [evt.event_id_size]u8) !void { 3327 if (comptime wasm) return; 3328 const actor = (try self.session.authorize(self.data.identity, .read, kind)) orelse return; 3329 const author = actor.author; 3330 3331 if (comptime supports_drafts) { 3332 const selected = self.data.selectedThread() orelse return; 3333 if (event_kind == kind and entryDraft(selected.*)) { 3334 try Data.removeDraft(self.session, allocator, author, &id); 3335 const route = draftsRoute(self.data.identity) orelse return; 3336 try self.session.navigate(route); 3337 return; 3338 } 3339 } 3340 3341 const io = self.session.io orelse return; 3342 const src = self.data.repo_source orelse return; 3343 if (!try src.canModify(io, allocator, actor, event_kind, &id)) return; 3344 const thread_id = evt.parseEventId(self.data.selected_id) catch return; 3345 3346 switch (src.repo_kind) { 3347 inline else => |repo_kind| { 3348 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 3349 defer any_repo.deinit(io, allocator); 3350 switch (any_repo) { 3351 inline else => |*repo| try evt.removeInThread(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, kind, &thread_id, event_kind, &id, author), 3352 } 3353 }, 3354 } 3355 3356 const id_hex = std.fmt.bytesToHex(id, .lower); 3357 const route = ui.RoutablePage.repoEventsRoute(self.data.identity, .removed, event_kind, &id_hex, null) orelse return; 3358 try self.session.navigate(route); 3359 } 3360 3361 // enter the detail pane, landing on the way back to the list when it shows 3362 fn focusDetail(self: *This, index: usize, root_focus: *Focus) void { 3363 if (widget.listBackId(self.detailFrame(index)) != null) return widget.focusListBack(self.detailFrame(index), root_focus); 3364 _ = self.detail(index).focusFirst(root_focus); 3365 } 3366 3367 // return to the list. 3368 fn focusList(self: *This, index: usize, root_focus: *Focus) void { 3369 root_focus.setFocus(self.listScroll(index).getFocus().id); 3370 } 3371 3372 // cross to the header tabs above the stack. 3373 fn focusHeader(self: *This, root_focus: *Focus) void { 3374 root_focus.setFocus(self.box.children.keys()[header_index]); 3375 } 3376 3377 pub fn clearGrid(self: *This) void { 3378 self.box.clearGrid(); 3379 } 3380 3381 pub fn getGrid(self: This) ?Grid { 3382 return self.box.getGrid(); 3383 } 3384 3385 pub fn getFocus(self: *This) *Focus { 3386 return self.box.getFocus(); 3387 } 3388 3389 // for the parent's "scroll up at the top jumps to the header" check: at the 3390 // top only while the header tabs hold focus, so up from the split 3391 // crosses into the tabs first. 3392 pub fn atTop(self: *This) bool { 3393 return self.headerActive(); 3394 } 3395 }; 3396 } 3397