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