╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ thread.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1605 const aa = session.page_arena.allocator(); 1606 const route = switch (confirmation_kind) { 1607 .remove => ui.RoutablePage.repoThreadRemoveRoute(kind, data.identity, data.selected_id, data.comment_id), 1608 .publish => publishRoute(data.identity, data.selected_id), 1609 .merge => mergeRoute(data.identity, data.selected_id), 1610 } orelse return error.RouteTooLong; 1611 const page_url = try route.toUrl(session.page_arena); 1612 const action = try std.fmt.allocPrint(aa, "form:{s}", .{page_url}); 1613 const event_name = if (data.comment_id.len == 0) thread_name else "comment"; 1614 var label_buf: ["remove ".len + @max(thread_name.len, "comment".len)]u8 = undefined; 1615 var buttons: [2]ConfirmationButton = undefined; 1616 buttons[0] = .{ .label = switch (confirmation_kind) { 1617 .remove => try std.fmt.bufPrint(&label_buf, "remove {s}", .{event_name}), 1618 .publish => "publish patch", 1619 .merge => "merge patch", 1620 } }; 1621 var button_count: usize = 1; 1622 if (confirmation_kind == .merge) { 1623 if (comptime supports_merge) { 1624 const availability = if (data.selectedThread()) |entry| 1625 if (Data.canMerge(entry.*, session)) entry.mergeability else pch.Mergeability{} 1626 else 1627 pch.Mergeability{}; 1628 buttons[0].bottom_label = mergeBottomLabel(availability.source); 1629 if (availability.source != .clean) buttons[0].action = ""; 1630 buttons[1] = .{ 1631 .label = "squash and merge patch", 1632 .bottom_label = mergeBottomLabel(availability.squash), 1633 .action = if (availability.squash == .clean) try std.fmt.allocPrint(aa, "{s}{s}/squash", .{ ui.submit_action_prefix, page_url }) else "", 1634 }; 1635 button_count += 1; 1636 } else unreachable; 1637 } 1638 var center = try initConfirmationForm(allocator, action, buttons[0..button_count]); 1639 errdefer center.deinit(allocator); 1640 try stack.children.put(allocator, center.getFocus().id, .{ .center = center }); 1641 } else if (data.view == .new_comment or data.view == .edit_comment) { 1642 const editing = data.view == .edit_comment; 1643 const route = if (editing) 1644 ui.RoutablePage.repoThreadCommentEditRoute(kind, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong 1645 else 1646 ui.RoutablePage.repoThreadCommentNewRoute(kind, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong; 1647 const page_url = try route.toUrl(session.page_arena); 1648 const action = try std.fmt.allocPrint(session.page_arena.allocator(), "form:{s}", .{page_url}); 1649 const page = data.comment_page; 1650 const top_level = if (page) |p| std.mem.eql(u8, &p.selected.comment.event.parent_id, &p.selected.comment.event.thread_id) else false; 1651 const parent_route = if (editing) 1652 if (top_level) 1653 ui.RoutablePage.repoThreadCommentsRoute(kind, data.identity, data.selected_id, 0) 1654 else if (page) |p| 1655 ui.RoutablePage.repoThreadCommentRoute(kind, data.identity, data.selected_id, &p.selected.comment.event.parent_id, 0) 1656 else 1657 null 1658 else if (page) |p| 1659 ui.RoutablePage.repoThreadCommentRoute(kind, data.identity, data.selected_id, &p.selected.id, 0) 1660 else 1661 ui.RoutablePage.repoThreadCommentsRoute(kind, data.identity, data.selected_id, 0); 1662 const parent_author: ui.Author = if (editing) 1663 if (top_level) 1664 if (data.selectedThread()) |entry| entry.author else .unknown 1665 else if (page) |p| 1666 p.selected.parent_author orelse .unknown 1667 else 1668 .unknown 1669 else if (page) |p| 1670 p.selected.author 1671 else if (data.selectedThread()) |entry| 1672 entry.author 1673 else 1674 .unknown; 1675 const initial_body: ?[]const u8 = if (editing) if (page) |p| p.selected.comment.event.body else null else null; 1676 var form = try initCommentForm(allocator, session, action, parent_author, parent_route orelse return error.RouteTooLong, initial_body); 1677 errdefer form.deinit(allocator); 1678 try stack.children.put(allocator, form.getFocus().id, .{ .box = form }); 1679 } else if (supports_conflicts and data.view == .resolve) { 1680 // on the web the page grows to the form's height and the 1681 // browser scrolls it; the terminal scrolls the form itself 1682 var form_widget: Widget = blk: { 1683 var form = try initResolveForm(allocator, session, data); 1684 errdefer form.deinit(allocator); 1685 break :blk if (session.is_terminal) 1686 .{ .scroll = try wgt.Scroll(Widget).init(allocator, .{ .box = form }, .{ .direction = .vert }) } 1687 else 1688 .{ .box = form }; 1689 }; 1690 errdefer form_widget.deinit(allocator); 1691 try stack.children.put(allocator, form_widget.getFocus().id, form_widget); 1692 } else { 1693 const aa = session.page_arena.allocator(); 1694 const route = if (data.view == .edit) 1695 ui.RoutablePage.repoThreadEditRoute(kind, data.identity, data.selected_id) orelse return error.RouteTooLong 1696 else 1697 ui.RoutablePage.repoThreadNewRoute(kind, data.identity) orelse return error.RouteTooLong; 1698 const action = try std.fmt.allocPrint(aa, "form:{s}", .{try route.toUrl(session.page_arena)}); 1699 const record = if (data.view == .edit) 1700 (if (data.selectedThread()) |entry| &entry.record else null) 1701 else 1702 null; 1703 var form = try initThreadForm(allocator, session, data, action, record); 1704 errdefer form.deinit(allocator); 1705 try stack.children.put(allocator, form.getFocus().id, .{ .box = form }); 1706 } 1707 } else { 1708 var message = try ui.Unauthorized.View.init(allocator); 1709 errdefer message.deinit(allocator); 1710 try stack.children.put(allocator, message.getFocus().id, .{ .unauthorized = message }); 1711 } 1712 1713 if (supports_drafts) { 1714 var split = try initSplit(allocator, session, data, null, true); 1715 errdefer split.deinit(allocator); 1716 try stack.children.put(allocator, split.getFocus().id, .{ .box = split }); 1717 } 1718 1719 // the conflicts split; its tab exists only when the repo has 1720 // conflicts, so skip the child too to keep the stack 1:1 with the 1721 // tabs. 1722 if (supports_conflicts and data.conflicts.count > 0) { 1723 var split = try initSplit(allocator, session, data, null, false); 1724 errdefer split.deinit(allocator); 1725 try stack.children.put(allocator, split.getFocus().id, .{ .box = split }); 1726 } 1727 1728 // the stack starts on the page's view. 1729 stack.getFocus().child_id = stack.children.keys()[viewIndex(data.view)]; 1730 1731 // focus entering the view lands on the tabs first. 1732 outer.getFocus().child_id = outer.children.keys()[header_index]; 1733 1734 return .{ 1735 .box = outer, 1736 .data = data, 1737 .session = session, 1738 }; 1739 } 1740 1741 // the master-detail split showing `status`'s window, or the conflicts 1742 // window when null. 1743 fn initSplit(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self, status_maybe: ?Status, drafts: bool) !wgt.Box(Widget) { 1744 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 1745 errdefer box.deinit(allocator); 1746 1747 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; 1748 1749 // the thread list (one focusable row per title), plus a "next" link that 1750 // reloads the page rooted at the following thread. 1751 { 1752 var list_scroll = blk: { 1753 var list_box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .vert, .stretch = true }); 1754 errdefer list_box.deinit(allocator); 1755 if (win.prev_id) |prev| 1756 try addRow(allocator, &list_box, "← previous", "", try windowLink(session.page_arena, data, status_maybe, drafts, prev)); 1757 for (win.items) |entry| { 1758 const label: []const u8 = if (entry.comments.count > 0) 1759 try std.fmt.allocPrint(session.page_arena.allocator(), " {d} comments ", .{entry.comments.count}) 1760 else 1761 ""; 1762 try addRow(allocator, &list_box, entry.record.event.title, label, try rowLink(session.page_arena, data, entry.id)); 1763 } 1764 if (win.next_id) |next| 1765 try addRow(allocator, &list_box, "next →", "", try windowLink(session.page_arena, data, status_maybe, drafts, next)); 1766 // select the window's first thread (past a leading "previous" 1767 // row) so its description shows on load. 1768 if (win.items.len > 0) 1769 list_box.getFocus().child_id = list_box.children.keys()[if (win.prev_id != null) 1 else 0] 1770 else if (list_box.children.count() > 0) 1771 list_box.getFocus().child_id = list_box.children.keys()[0]; 1772 break :blk try wgt.Scroll(Widget).init(allocator, .{ .box = list_box }, .{ .direction = .vert, .web_native = !session.is_terminal }); 1773 }; 1774 errdefer list_scroll.deinit(allocator); 1775 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 } }); 1776 } 1777 1778 // frame the detail pane inside the master-detail split 1779 { 1780 var frame = try wgt.Box(Widget).init(allocator, .{ .border_style = .hidden, .direction = .vert }); 1781 errdefer frame.deinit(allocator); 1782 frame.getFocus().mode = .mouse; 1783 var detail_view = try DetailType.init(allocator, data, session, null, .{}); 1784 errdefer detail_view.deinit(allocator); 1785 frame.getFocus().child_id = detail_view.getFocus().id; 1786 try frame.children.put(allocator, detail_view.getFocus().id, .{ .widget = @unionInit(Widget, Data.detail_widget_name, detail_view), .rect = null, .min_size = null }); 1787 try box.children.put(allocator, frame.getFocus().id, .{ .widget = .{ .box = frame }, .rect = null, .min_size = .{ .width = detail_min_width, .height = null } }); 1788 } 1789 1790 box.getFocus().child_id = box.children.keys()[list_index]; 1791 return box; 1792 } 1793 1794 // a thread form, plus the target branch for patches, prefilled from 1795 // `record` when given. its form: subtree makes the web renderer POST 1796 // the inputs to `action`'s route. 1797 fn initThreadForm(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self, action: []const u8, record: ?*const Event.Record) !wgt.Box(Widget) { 1798 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 1799 errdefer box.deinit(allocator); 1800 box.getFocus().kind = .{ .custom = action }; 1801 1802 const saved_form = session.formFeedback(feedback_tag); 1803 const saved_fields = if (saved_form) |saved| saved.fields else null; 1804 1805 { 1806 var title = try wgt.TextInput.init(allocator, .{ .label = " title ", .name = "title", .visible_width = null, .rounded_corners = true, .render_content = session.is_terminal }); 1807 errdefer title.deinit(allocator); 1808 title.getFocus().mode = .all; 1809 if (saved_fields) |saved| 1810 try title.setContent(allocator, saved.title) 1811 else if (record) |r| 1812 try title.setContent(allocator, r.event.title); 1813 try box.children.put(allocator, title.getFocus().id, .{ .widget = .{ .text_input = title }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 1814 box.getFocus().child_id = title.getFocus().id; 1815 } 1816 1817 { 1818 var tags = try wgt.TextInput.init(allocator, .{ .label = " tags (separate with spaces) ", .name = "tags", .visible_width = null, .rounded_corners = true, .render_content = session.is_terminal }); 1819 errdefer tags.deinit(allocator); 1820 tags.getFocus().mode = .all; 1821 if (saved_fields) |saved| 1822 try tags.setContent(allocator, saved.tags) 1823 else if (record) |r| 1824 try tags.setContent(allocator, r.event.tags); 1825 try box.children.put(allocator, tags.getFocus().id, .{ .widget = .{ .text_input = tags }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 1826 } 1827 1828 { 1829 var description = try wgt.TextInput.init(allocator, .{ .label = " description ", .name = "description", .visible_width = null, .rounded_corners = true, .render_content = session.is_terminal, .multiline = true, .scroll = .{ .fill = true } }); 1830 errdefer description.deinit(allocator); 1831 description.getFocus().mode = .all; 1832 if (saved_fields) |saved| 1833 try description.setContent(allocator, saved.description) 1834 else if (record) |r| 1835 try description.setContent(allocator, r.event.description); 1836 try box.children.put(allocator, description.getFocus().id, .{ .widget = .{ .text_input = description }, .rect = null, .min_size = null }); 1837 } 1838 1839 if (supports_drafts and record == null) { 1840 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); 1841 errdefer source.deinit(allocator); 1842 try box.children.put(allocator, source.getFocus().id, .{ .widget = .{ .patch_source = source }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 1843 } 1844 1845 if (supports_drafts) { 1846 var target = try wgt.TextInput.init(allocator, .{ .label = " target branch ", .name = "target_branch", .visible_width = null, .rounded_corners = true, .render_content = session.is_terminal }); 1847 errdefer target.deinit(allocator); 1848 target.getFocus().mode = .all; 1849 const content = if (saved_fields) |saved| saved.target_branch else if (record) |r| r.event.target_branch else data.default_target_branch; 1850 try target.setContent(allocator, content); 1851 try box.children.put(allocator, target.getFocus().id, .{ .widget = .{ .text_input = target }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 1852 } 1853 1854 try addSubmitButtonLabeled(allocator, &box, if (supports_drafts and record == null) "submit draft" else "submit"); 1855 1856 return box; 1857 } 1858 1859 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) { 1860 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 1861 errdefer box.deinit(allocator); 1862 box.getFocus().kind = .{ .custom = action }; 1863 1864 { 1865 var parent = try ui.authorBox(allocator, session.page_arena, author); 1866 errdefer parent.deinit(allocator); 1867 parent.options.label = " replying to "; 1868 parent.getFocus().kind = .{ .custom = try std.fmt.allocPrint(session.page_arena.allocator(), "a:{s}", .{try parent_route.toUrl(session.page_arena)}) }; 1869 try box.children.put(allocator, parent.getFocus().id, .{ .widget = .{ .text_box = parent }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 1870 box.getFocus().child_id = parent.getFocus().id; 1871 } 1872 1873 { 1874 var body = try wgt.TextInput.init(allocator, .{ 1875 .label = " comment ", 1876 .name = "body", 1877 .visible_width = null, 1878 .rounded_corners = true, 1879 .render_content = session.is_terminal, 1880 .multiline = true, 1881 .visible_height = 5, 1882 .scroll = .{ .fill = true }, 1883 }); 1884 errdefer body.deinit(allocator); 1885 body.getFocus().mode = .all; 1886 if (initial_body) |text| try body.setContent(allocator, text); 1887 try box.children.put(allocator, body.getFocus().id, .{ .widget = .{ .text_input = body }, .rect = null, .min_size = null }); 1888 } 1889 1890 try addSubmitButtonLabeled(allocator, &box, "submit"); 1891 return box; 1892 } 1893 1894 const ConfirmationButton = struct { 1895 label: []const u8, 1896 bottom_label: []const u8 = "", 1897 action: []const u8 = "submit", 1898 }; 1899 1900 fn initConfirmationForm(allocator: std.mem.Allocator, action: []const u8, buttons: []const ConfirmationButton) !Center { 1901 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .rounded_corners = true, .direction = .vert }); 1902 errdefer box.deinit(allocator); 1903 box.getFocus().kind = .{ .custom = action }; 1904 1905 var prompt = try wgt.Text.init(allocator, "are you sure?"); 1906 errdefer prompt.deinit(allocator); 1907 try box.children.put(allocator, prompt.getFocus().id, .{ .widget = .{ .text = prompt }, .rect = null, .min_size = null }); 1908 1909 for (buttons, 0..) |options, index| { 1910 var button = try wgt.TextBox.init(allocator, options.label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none, .bottom_label = options.bottom_label }); 1911 errdefer button.deinit(allocator); 1912 button.getFocus().mode = .all; 1913 button.getFocus().kind = .{ .custom = options.action }; 1914 try box.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = null }); 1915 if (index == 0) box.getFocus().child_id = button.getFocus().id; 1916 } 1917 1918 return Center.init(allocator, .{ .box = box }); 1919 } 1920 1921 // a form's submit button, then a spacer absorbing the leftover 1922 // min-height the box hands its last child, so the button keeps its 1923 // natural height 1924 fn addSubmitButtonLabeled(allocator: std.mem.Allocator, box: *wgt.Box(Widget), label: []const u8) !void { 1925 { 1926 var submit = try SubmitButton.initLabeled(allocator, label); 1927 errdefer submit.deinit(allocator); 1928 try box.children.put(allocator, submit.getFocus().id, .{ .widget = .{ .submit_button = submit }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 1929 } 1930 { 1931 var spacer = try Spacer.init(allocator); 1932 errdefer spacer.deinit(allocator); 1933 try box.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 1934 } 1935 } 1936 1937 // the conflict resolve form. the "use this" links are navigations that 1938 // flip the url's theirs: pick, reloading the prefills; one submit 1939 // settles every conflict. the form: subtree makes the web renderer POST 1940 // to the resolve route. 1941 fn initResolveForm(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self) !wgt.Box(Widget) { 1942 const aa = session.page_arena.allocator(); 1943 // init only picks the resolve view once it has read the conflict 1944 const conflict = if (data.conflict) |*c| c else unreachable; 1945 1946 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 1947 errdefer box.deinit(allocator); 1948 const route = resolveRoute(data.identity, data.selected_id, data.theirs_picks) orelse return error.RouteTooLong; 1949 box.getFocus().kind = .{ .custom = try std.fmt.allocPrint(aa, "form:{s}", .{try route.toUrl(session.page_arena)}) }; 1950 1951 if (conflict.title) |*fc| { 1952 try addLabel(allocator, &box, "title conflict:"); 1953 try addFieldConflict(allocator, &box, session, data, "title", fc); 1954 } 1955 if (conflict.tags) |*fc| { 1956 try addLabel(allocator, &box, "tags conflict:"); 1957 try addFieldConflict(allocator, &box, session, data, "tags", fc); 1958 } 1959 if (comptime @hasField(Data.Conflict, "status")) { 1960 if (conflict.status) |*fc| { 1961 try addLabel(allocator, &box, "status conflict:"); 1962 try addAtomicConflict(allocator, &box, session, data, "status", fc); 1963 } 1964 } 1965 if (comptime @hasField(Data.Conflict, "revision")) { 1966 if (conflict.revision) |*fc| { 1967 try addLabel(allocator, &box, "revision conflict:"); 1968 try addAtomicConflict(allocator, &box, session, data, "revision", fc); 1969 } 1970 if (conflict.target_branch) |*fc| { 1971 try addLabel(allocator, &box, "target branch conflict:"); 1972 try addAtomicConflict(allocator, &box, session, data, "target_branch", fc); 1973 } 1974 } 1975 1976 if (conflict.description) |*desc| { 1977 try addLabel(allocator, &box, "description conflict:"); 1978 var hunk_index: usize = 0; 1979 for (desc.chunks, 0..) |*chunk, chunk_index| { 1980 switch (chunk.*) { 1981 .same => |text| { 1982 var tb = try wgt.TextBox.init(allocator, text, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .word }); 1983 errdefer tb.deinit(allocator); 1984 tb.getFocus().mode = .all; 1985 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1986 }, 1987 // an auto-resolved chunk: only one side changed it, so its 1988 // version stands, shown distinctly with its author 1989 .auto => |auto| { 1990 const author = if (auto.theirs) desc.theirs_author else desc.ours_author; 1991 const verb: []const u8 = if (auto.text == null) "removed" else "edited"; 1992 const label = switch (author) { 1993 .user_name, .email => |name| try std.fmt.allocPrint(aa, " {s} by {s} ", .{ verb, name }), 1994 .unknown => try std.fmt.allocPrint(aa, " {s} by {s} ", .{ verb, if (auto.theirs) @as([]const u8, "them") else "us" }), 1995 }; 1996 var tb = try wgt.TextBox.init(allocator, auto.text orelse "(removed)", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .word, .label = label }); 1997 errdefer tb.deinit(allocator); 1998 tb.getFocus().mode = .all; 1999 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 2000 }, 2001 .conflict => |hunk| { 2002 // a blank row on each side sets the group apart from 2003 // the flowing shared and auto-resolved chunks 2004 if (chunk_index > 0) try addGap(allocator, &box); 2005 const name = try std.fmt.allocPrint(aa, "d{d}", .{hunk_index}); 2006 hunk_index += 1; 2007 const picked_theirs = data.theirsPicked(name); 2008 try addVersionRow(allocator, &box, try sideLabel(aa, desc.ours_author, true), hunk.ours orelse "", try useThisLink(session, data, name, false)); 2009 try addVersionRow(allocator, &box, try sideLabel(aa, desc.theirs_author, false), hunk.theirs orelse "", try useThisLink(session, data, name, true)); 2010 var resolution_input = try wgt.TextInput.init(allocator, .{ .label = " resolution ", .name = name, .visible_width = null, .rounded_corners = true, .render_content = session.is_terminal, .multiline = true }); 2011 errdefer resolution_input.deinit(allocator); 2012 resolution_input.getFocus().mode = .all; 2013 try resolution_input.setContent(allocator, (if (picked_theirs) hunk.theirs else hunk.ours) orelse ""); 2014 try box.children.put(allocator, resolution_input.getFocus().id, .{ .widget = .{ .text_input = resolution_input }, .rect = null, .min_size = null }); 2015 if (chunk_index + 1 < desc.chunks.len) try addGap(allocator, &box); 2016 }, 2017 } 2018 } 2019 } 2020 2021 try addSubmitButtonLabeled(allocator, &box, "submit"); 2022 2023 box.getFocus().child_id = box.children.keys()[0]; 2024 return box; 2025 } 2026 2027 // one conflicted scalar field: both sides' version rows, then the 2028 // resolution input prefilled from the picked side. 2029 fn addFieldConflict(allocator: std.mem.Allocator, box: *wgt.Box(Widget), session: *ui.Session, data: *const Self, comptime name: []const u8, fc: *const FieldConflict) !void { 2030 const aa = session.page_arena.allocator(); 2031 try addVersionRow(allocator, box, try sideLabel(aa, fc.ours.author, true), fc.ours.text, try useThisLink(session, data, name, false)); 2032 try addVersionRow(allocator, box, try sideLabel(aa, fc.theirs.author, false), fc.theirs.text, try useThisLink(session, data, name, true)); 2033 2034 var resolution_input = try wgt.TextInput.init(allocator, .{ .label = " resolution ", .name = name, .visible_width = null, .rounded_corners = true, .render_content = session.is_terminal }); 2035 errdefer resolution_input.deinit(allocator); 2036 resolution_input.getFocus().mode = .all; 2037 try resolution_input.setContent(allocator, if (data.theirsPicked(name)) fc.theirs.text else fc.ours.text); 2038 try box.children.put(allocator, resolution_input.getFocus().id, .{ .widget = .{ .text_input = resolution_input }, .rect = null, .min_size = null }); 2039 } 2040 2041 // atomic fields can only pick one complete side 2042 fn addAtomicConflict(allocator: std.mem.Allocator, box: *wgt.Box(Widget), session: *ui.Session, data: *const Self, comptime name: []const u8, fc: *const FieldConflict) !void { 2043 const aa = session.page_arena.allocator(); 2044 try addVersionRow(allocator, box, try sideLabel(aa, fc.ours.author, true), fc.ours.text, try useThisLink(session, data, name, false)); 2045 try addVersionRow(allocator, box, try sideLabel(aa, fc.theirs.author, false), fc.theirs.text, try useThisLink(session, data, name, true)); 2046 var selected = try wgt.TextBox.init(allocator, if (data.theirsPicked(name)) fc.theirs.text else fc.ours.text, .{ 2047 .border_style = .single, 2048 .rounded_corners = true, 2049 .wrap_kind = .word, 2050 .label = " resolution ", 2051 }); 2052 errdefer selected.deinit(allocator); 2053 selected.getFocus().mode = .all; 2054 try box.children.put(allocator, selected.getFocus().id, .{ .widget = .{ .text_box = selected }, .rect = null, .min_size = null }); 2055 } 2056 2057 // a blank row setting a conflict group apart from its neighbors 2058 fn addGap(allocator: std.mem.Allocator, box: *wgt.Box(Widget)) !void { 2059 var text = try wgt.Text.init(allocator, " "); 2060 errdefer text.deinit(allocator); 2061 try box.children.put(allocator, text.getFocus().id, .{ .widget = .{ .text = text }, .rect = null, .min_size = null }); 2062 } 2063 2064 fn addLabel(allocator: std.mem.Allocator, box: *wgt.Box(Widget), content: []const u8) !void { 2065 var label = try SectionLabel.init(allocator, content); 2066 errdefer label.deinit(allocator); 2067 try box.children.put(allocator, label.getFocus().id, .{ .widget = .{ .section_label = label }, .rect = null, .min_size = null }); 2068 } 2069 2070 // one side of a conflict: the "use this" link on the left, the version 2071 // itself as a labeled read-only box. 2072 fn addVersionRow(allocator: std.mem.Allocator, box: *wgt.Box(Widget), label: []const u8, text: []const u8, link: []const u8) !void { 2073 var row = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 2074 errdefer row.deinit(allocator); 2075 2076 { 2077 const use_label = "use this"; 2078 var use = try wgt.TextBox.init(allocator, use_label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 2079 errdefer use.deinit(allocator); 2080 use.getFocus().mode = .all; 2081 use.getFocus().kind = .{ .custom = link }; 2082 try row.children.put(allocator, use.getFocus().id, .{ 2083 .widget = .{ .text_box = use }, 2084 .rect = null, 2085 .min_size = .{ .width = use_label.len + 2, .height = null }, 2086 .max_size = .{ .width = use_label.len + 2, .height = null }, 2087 }); 2088 } 2089 2090 { 2091 var tb = try wgt.TextBox.init(allocator, if (text.len > 0) text else "(removed)", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .word, .label = label }); 2092 errdefer tb.deinit(allocator); 2093 tb.getFocus().mode = .all; 2094 try row.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 2095 } 2096 2097 row.getFocus().child_id = row.children.keys()[0]; 2098 try box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .box = row }, .rect = null, .min_size = null }); 2099 } 2100 2101 // the label of one side's version box, naming its author 2102 fn sideLabel(aa: std.mem.Allocator, author: ui.Author, ours: bool) ![]const u8 { 2103 return switch (author) { 2104 .user_name, .email => |name| try std.fmt.allocPrint(aa, " edited by {s} ", .{name}), 2105 .unknown => if (ours) " current version " else " their version ", 2106 }; 2107 } 2108 2109 // the a: link that reloads the resolve page with `name` picked from ours 2110 // or theirs, reprefilling its resolution input 2111 fn useThisLink(session: *ui.Session, data: *const Self, name: []const u8, theirs: bool) ![]const u8 { 2112 const aa = session.page_arena.allocator(); 2113 var picks: std.ArrayList(u8) = .empty; 2114 var it = std.mem.splitScalar(u8, data.theirs_picks, ','); 2115 while (it.next()) |pick| { 2116 if (pick.len == 0 or std.mem.eql(u8, pick, name)) continue; 2117 if (picks.items.len > 0) try picks.append(aa, ','); 2118 try picks.appendSlice(aa, pick); 2119 } 2120 if (theirs) { 2121 if (picks.items.len > 0) try picks.append(aa, ','); 2122 try picks.appendSlice(aa, name); 2123 } 2124 const route = resolveRoute(data.identity, data.selected_id, picks.items) orelse return error.RouteTooLong; 2125 return std.fmt.allocPrint(aa, "a:{s}", .{try route.toUrl(session.page_arena)}); 2126 } 2127 2128 fn addRow(allocator: std.mem.Allocator, box: *wgt.Box(Widget), text: []const u8, bottom_label: []const u8, link: []const u8) !void { 2129 var row = try wgt.TextBox.init(allocator, text, .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .word, .bottom_label = bottom_label }); 2130 errdefer row.deinit(allocator); 2131 row.getFocus().mode = .all; 2132 if (link.len != 0) row.getFocus().kind = .{ .custom = link }; 2133 try box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .text_box = row }, .rect = null, .min_size = null, .max_size = .{ .width = null, .height = 5 } }); 2134 } 2135 2136 pub fn deinit(self: *This, allocator: std.mem.Allocator) void { 2137 self.box.deinit(allocator); 2138 } 2139 2140 fn header(self: *This) *HeaderType { 2141 return &@field(self.box.children.values()[header_index].widget, Data.header_widget_name); 2142 } 2143 2144 fn viewStack(self: *This) *wgt.Stack(Widget) { 2145 return &self.box.children.values()[stack_index].widget.stack; 2146 } 2147 2148 // `index`'s master-detail split inside the stack. 2149 fn resultsBox(self: *This, index: usize) *wgt.Box(Widget) { 2150 return &self.viewStack().children.values()[index].box; 2151 } 2152 2153 // the tags view's flow inside the stack. 2154 fn tagsView(self: *This) *TagFlow { 2155 return &self.viewStack().children.values()[tags_view_index].tag_flow; 2156 } 2157 2158 // the non-confirmation form inside the stack 2159 fn formBox(self: *This) ?*wgt.Box(Widget) { 2160 return switch (self.viewStack().children.values()[form_view_index]) { 2161 .box => |*box| box, 2162 // the resolve form sits inside a scroll on the terminal 2163 .scroll => |*scroll| &scroll.child.box, 2164 else => null, 2165 }; 2166 } 2167 2168 fn threadForm(self: *This) ?*wgt.Box(Widget) { 2169 if (supports_conflicts and self.data.view == .resolve) return null; 2170 return switch (self.data.view) { 2171 .new_comment, .edit_comment => null, 2172 else => self.formBox(), 2173 }; 2174 } 2175 2176 fn formField(form: *wgt.Box(Widget), name: []const u8) !*wgt.TextInput { 2177 for (form.children.values()) |*child| switch (child.widget) { 2178 .text_input => |*text_input| if (std.mem.eql(u8, text_input.options.name, name)) return text_input, 2179 else => {}, 2180 }; 2181 return error.MissingFormField; 2182 } 2183 2184 fn formWidget(form: *wgt.Box(Widget), comptime widget_kind: std.meta.Tag(Widget)) ?*@FieldType(Widget, @tagName(widget_kind)) { 2185 for (form.children.values()) |*child| { 2186 if (child.widget == widget_kind) return &@field(child.widget, @tagName(widget_kind)); 2187 } 2188 return null; 2189 } 2190 2191 fn confirmationForm(self: *This) ?*wgt.Box(Widget) { 2192 return switch (self.viewStack().children.values()[form_view_index]) { 2193 .center => |*center| switch (center.child.*) { 2194 .box => |*box| box, 2195 else => null, 2196 }, 2197 else => null, 2198 }; 2199 } 2200 2201 // the resolve form's scroll on the terminal (the web page scrolls itself) 2202 fn resolveScroll(self: *This) ?*wgt.Scroll(Widget) { 2203 return switch (self.viewStack().children.values()[form_view_index]) { 2204 .scroll => |*scroll| scroll, 2205 else => null, 2206 }; 2207 } 2208 2209 fn listScroll(self: *This, index: usize) *wgt.Scroll(Widget) { 2210 return &self.resultsBox(index).children.values()[list_index].widget.scroll; 2211 } 2212 2213 fn listBox(self: *This, index: usize) *wgt.Box(Widget) { 2214 return &self.listScroll(index).child.box; 2215 } 2216 2217 fn detail(self: *This, index: usize) *DetailType { 2218 const frame = &self.resultsBox(index).children.values()[detail_index].widget.box; 2219 return &@field(frame.children.values()[0].widget, Data.detail_widget_name); 2220 } 2221 2222 fn window(self: *This, index: usize) *const Window { 2223 if (supports_conflicts and index == conflict_view_index) return &self.data.conflicts; 2224 if (supports_drafts and index == drafts_view_index) return &self.data.drafts; 2225 return self.data.window(splitStatus(index)); 2226 } 2227 2228 fn stackSelectedIndex(self: *This) ?usize { 2229 const stack = self.viewStack(); 2230 const cid = stack.getFocus().child_id orelse return null; 2231 return stack.children.getIndex(cid); 2232 } 2233 2234 // the stack's selected master-detail split (null when the tags view or the 2235 // new-thread form shows). 2236 fn selectedSplitIndex(self: *This) ?usize { 2237 const idx = self.stackSelectedIndex() orelse return null; 2238 return if (idx < status_count or (supports_drafts and idx == drafts_view_index) or idx == conflict_view_index) idx else null; 2239 } 2240 2241 fn detailActive(self: *This, index: usize) bool { 2242 const rb = self.resultsBox(index); 2243 const cid = rb.getFocus().child_id orelse return false; 2244 return rb.children.getIndex(cid) == detail_index; 2245 } 2246 2247 fn headerActive(self: *This) bool { 2248 const cid = self.box.getFocus().child_id orelse return false; 2249 return self.box.children.getIndex(cid) == header_index; 2250 } 2251 2252 fn tagsViewActive(self: *This) bool { 2253 if (self.headerActive()) return false; 2254 return self.stackSelectedIndex() == tags_view_index; 2255 } 2256 2257 fn formViewActive(self: *This) bool { 2258 if (self.headerActive()) return false; 2259 return self.stackSelectedIndex() == form_view_index; 2260 } 2261 2262 // the selected thread's index, or null when a window-navigation row is 2263 // selected (a leading "previous" row shifts the thread rows down by one). 2264 fn selectedThreadIndex(self: *This, index: usize) ?usize { 2265 const lb = self.listBox(index); 2266 const cid = lb.getFocus().child_id orelse return null; 2267 const idx = lb.children.getIndex(cid) orelse return null; 2268 const win = self.window(index); 2269 const lead: usize = if (win.prev_id != null) 1 else 0; 2270 if (idx < lead or idx - lead >= win.items.len) return null; 2271 return idx - lead; 2272 } 2273 2274 pub fn build(self: *This, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2275 self.clearGrid(); 2276 2277 // each header tab maps 1:1 to a stack child by position 2278 if (self.header().getSelectedIndex()) |index| { 2279 const stack = self.viewStack(); 2280 stack.getFocus().child_id = stack.children.keys()[index]; 2281 } 2282 2283 if (self.selectedSplitIndex()) |i| { 2284 // swap the detail pane to the selected thread when it changes. 2285 if (self.selectedThreadIndex(i)) |selected| { 2286 const entry = self.window(i).items[selected]; 2287 const changed = if (self.detail(i).entry) |current| !std.mem.eql(u8, current.id, entry.id) else true; 2288 if (changed) { 2289 try self.detail(i).setEntry(allocator, entry); 2290 } 2291 } 2292 2293 // the selected list row shows a border (the focused TextBox 2294 // upgrades it to a double border itself); the rest stay borderless. 2295 const lb = self.listBox(i); 2296 for (lb.children.keys(), lb.children.values()) |id, *child| { 2297 switch (child.widget) { 2298 .text_box => |*tb| tb.options.border_style = if (lb.getFocus().child_id == id) .single else .hidden, 2299 else => {}, 2300 } 2301 } 2302 2303 // cap the list at list_max_width only while the detail pane fits 2304 // beside it. the box drops the detail when the width can't hold 2305 // both minimums, so when it's that narrow we lift the cap and let 2306 // the list fill the whole width. 2307 const both_panes_fit = if (constraint.max_size.width) |w| w >= list_max_width + detail_min_width else true; 2308 self.resultsBox(i).children.values()[list_index].max_size = if (both_panes_fit) .{ .width = list_max_width, .height = null } else null; 2309 2310 // stretch the detail pane across the rest of the width so it fills 2311 // the area rather than shrinking to its content; its scroll fills 2312 // the pane. 2313 if (constraint.max_size.width) |w| { 2314 self.resultsBox(i).children.values()[detail_index].min_size = .{ .width = if (both_panes_fit) w - list_max_width else w, .height = null }; 2315 } else { 2316 self.resultsBox(i).children.values()[detail_index].min_size = .{ .width = detail_min_width, .height = null }; 2317 } 2318 } 2319 2320 // refresh the form inputs' entries in the session's focus-id -> input 2321 // map with this frame's addresses, so the web/wasm form handling can 2322 // find them by focus id 2323 if (self.threadForm()) |form| { 2324 const failure = if (self.session.formFeedback(feedback_tag)) |saved| saved.failure else null; 2325 (try formField(form, "title")).options.label = if (failure == .required_title) " title (required) " else " title "; 2326 if (comptime supports_drafts) { 2327 if (formWidget(form, .patch_source)) |source| { 2328 source.field().options.label = if (failure == .invalid_source_branch) " source branch (not found) " else if (failure == .same_branch) " source branch (same as target) " else if (failure == .unrelated_branches) " source branch (unrelated) " else " source branch "; 2329 const submit = formWidget(form, .submit_button) orelse return error.MissingFormField; 2330 try submit.setLabel(allocator, switch (source.selected) { 2331 .fork => "submit draft", 2332 .branch => "submit patch", 2333 }); 2334 } 2335 (try formField(form, "target_branch")).options.label = 2336 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 "; 2337 } 2338 } 2339 if (self.formBox()) |form| { 2340 const inputs_arena = self.session.arena.allocator(); 2341 for (form.children.values()) |*child| switch (child.widget) { 2342 .text_input => |*ti| try self.session.text_inputs.put(inputs_arena, ti.getFocus().id, ti), 2343 else => {}, 2344 }; 2345 } 2346 2347 try self.box.build(allocator, constraint, root_focus); 2348 } 2349 2350 pub fn input(self: *This, allocator: std.mem.Allocator, raw_key: Key, root_focus: *Focus) !void { 2351 // wheel input moves focus like the arrows 2352 const key: Key = if (raw_key == .mouse and raw_key.mouse.action == .scroll) 2353 (if (raw_key.mouse.action.scroll == .up) .arrow_up else .arrow_down) 2354 else 2355 raw_key; 2356 2357 if (self.headerActive()) { 2358 // down from the sub-header re-enters the stack if the selected 2359 // view has something to focus; enter in the search box submits 2360 // the query; other keys move within the sub-header. 2361 if (inp.vertDirection(key) == .down) { 2362 const enterable = if (self.viewStack().getSelected()) |selected| switch (selected.*) { 2363 .tag_flow => |*tf| tf.text_boxes.items.len > 0, 2364 // nothing focusable in the unauthorized view 2365 .unauthorized => false, 2366 else => true, 2367 } else false; 2368 if (enterable) root_focus.setFocus(self.box.children.keys()[stack_index]); 2369 } else if (key == .enter) { 2370 if (try self.header().submittedText(allocator)) |text| { 2371 defer allocator.free(text); 2372 try self.submitSearch(text); 2373 } 2374 } else { 2375 try self.header().input(allocator, key, root_focus); 2376 } 2377 return; 2378 } 2379 if (self.tagsViewActive()) { 2380 self.tagsViewInput(key, root_focus); 2381 return; 2382 } 2383 if (self.formViewActive()) { 2384 if (confirmation(self.data.view) != null) { 2385 try self.confirmationFormInput(allocator, key, root_focus); 2386 } else if (supports_conflicts and self.data.view == .resolve) { 2387 try self.resolveInput(allocator, key, root_focus); 2388 } else { 2389 try self.formInput(allocator, key, root_focus); 2390 } 2391 return; 2392 } 2393 const i = self.selectedSplitIndex() orelse return; 2394 if (self.detailActive(i)) { 2395 switch (try self.detail(i).inputWithExit(allocator, key, root_focus)) { 2396 .none => {}, 2397 .list => self.focusList(i, root_focus), 2398 .header => self.focusHeader(root_focus), 2399 } 2400 } else { 2401 self.listInput(i, key, root_focus); 2402 } 2403 } 2404 2405 // navigate to the typed query's results, keeping the tag filter and the 2406 // status list the page shows. an empty box clears a filtered page and 2407 // does nothing on any other one. 2408 fn submitSearch(self: *This, text: []const u8) !void { 2409 if (text.len == 0 and self.data.search == null) return; 2410 const selected = self.header().getSelectedIndex() orelse 0; 2411 const status: Status = if (selected < status_count) splitStatus(selected) else @enumFromInt(0); 2412 const route = listRoute(self.data.identity, status, self.data.tag, "") orelse return; 2413 try self.session.navigate(route.withSearch(text) orelse return); 2414 } 2415 2416 // arrow keys move the tag selection; up from the top row crosses to the 2417 // header tabs. 2418 fn tagsViewInput(self: *This, key: Key, root_focus: *Focus) void { 2419 const tf = self.tagsView(); 2420 const cid = tf.focus.child_id orelse return; 2421 const cur = tf.indexOfFocusId(cid) orelse return; 2422 const count = tf.text_boxes.items.len; 2423 switch (key) { 2424 .arrow_left => if (cur > 0) root_focus.setFocus(tf.text_boxes.items[cur - 1].getFocus().id), 2425 .arrow_right => if (cur + 1 < count) root_focus.setFocus(tf.text_boxes.items[cur + 1].getFocus().id), 2426 .arrow_up => if (tf.rowStep(cur, false)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id) else self.focusHeader(root_focus), 2427 .arrow_down => if (tf.rowStep(cur, true)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id), 2428 .home => root_focus.setFocus(tf.text_boxes.items[0].getFocus().id), 2429 .end => root_focus.setFocus(tf.text_boxes.items[count - 1].getFocus().id), 2430 else => {}, 2431 } 2432 } 2433 2434 fn listInput(self: *This, index: usize, key: Key, root_focus: *Focus) void { 2435 // up/down (and the scroll wheel) move the selection a row; page up/down 2436 // jump a fixed amount. right/Enter cross into the detail pane. up from 2437 // the top row crosses into the header tabs. 2438 if (inp.rowDelta(key, @intCast(self.listBox(index).children.count()))) |delta| { 2439 const lb = self.listBox(index); 2440 const at_top = if (lb.getFocus().child_id) |cid| lb.children.getIndex(cid) == 0 else true; 2441 if (delta < 0 and at_top) return self.focusHeader(root_focus); 2442 moveRowFocus(lb, self.listScroll(index), root_focus, delta); 2443 return; 2444 } 2445 switch (key) { 2446 .enter, .arrow_right => _ = self.detail(index).focusFirst(root_focus), 2447 else => {}, 2448 } 2449 } 2450 2451 fn formInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2452 const form = self.formBox() orelse return; 2453 const cid = form.getFocus().child_id orelse return; 2454 const cur = form.children.getIndex(cid) orelse return; 2455 const child = &form.children.values()[cur]; 2456 2457 if (child.widget == .text_input) { 2458 const text_input = &child.widget.text_input; 2459 if (text_input.options.multiline and try multilineKeepsKey(text_input, allocator, key)) 2460 return child.widget.input(allocator, key, root_focus); 2461 } 2462 2463 const on_submit = child.widget == .submit_button; 2464 switch (key) { 2465 .arrow_up, .back_tab => return if (formStep(form, cur, false)) |i| 2466 root_focus.setFocus(form.children.keys()[i]) 2467 else 2468 self.focusHeader(root_focus), 2469 .arrow_down, .tab => return if (formStep(form, cur, true)) |i| root_focus.setFocus(form.children.keys()[i]), 2470 .enter => if (on_submit) return self.submitForm(allocator), 2471 .mouse => |mouse| if (on_submit) { 2472 if (inp.leftClickOn(root_focus, child.widget.submit_button.buttonId(), mouse)) return self.submitForm(allocator); 2473 }, 2474 else => {}, 2475 } 2476 try child.widget.input(allocator, key, root_focus); 2477 } 2478 2479 fn confirmationFormInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2480 const form = self.confirmationForm() orelse return; 2481 const child_id = form.getFocus().child_id orelse return; 2482 const index = form.children.getIndex(child_id) orelse return; 2483 switch (key) { 2484 .arrow_up, .back_tab => if (index > 1) 2485 root_focus.setFocus(form.children.keys()[index - 1]) 2486 else 2487 self.focusHeader(root_focus), 2488 .arrow_down, .tab => if (index + 1 < form.children.count()) root_focus.setFocus(form.children.keys()[index + 1]), 2489 .enter => try self.submitConfirmation(allocator, index), 2490 .mouse => |mouse| for (form.children.keys()[1..], 1..) |button_id, button_index| { 2491 if (inp.leftClickOn(root_focus, button_id, mouse)) { 2492 try self.submitConfirmation(allocator, button_index); 2493 break; 2494 } 2495 }, 2496 else => {}, 2497 } 2498 } 2499 2500 // the multiline hunk inputs keep enter and any up/down that has a row to 2501 // move to; enter on a "use this" link is a navigation the host follows 2502 // before this runs. 2503 fn resolveInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2504 const form = self.formBox() orelse return; 2505 const cid = form.getFocus().child_id orelse return; 2506 const cur = form.children.getIndex(cid) orelse return; 2507 const child = &form.children.values()[cur]; 2508 2509 if (child.widget == .text_input) { 2510 const ti = &child.widget.text_input; 2511 if (ti.options.multiline and try multilineKeepsKey(ti, allocator, key)) 2512 return child.widget.input(allocator, key, root_focus); 2513 } 2514 2515 const on_submit = child.widget == .submit_button; 2516 2517 switch (key) { 2518 .arrow_up, .back_tab => if (formStep(form, cur, false)) |i| self.focusResolveChild(form, i, root_focus) else self.focusHeader(root_focus), 2519 .arrow_down, .tab => if (formStep(form, cur, true)) |i| self.focusResolveChild(form, i, root_focus), 2520 .arrow_left, .arrow_right => switch (child.widget) { 2521 .box => |*row| { 2522 const row_cid = row.getFocus().child_id orelse return; 2523 const row_index = row.children.getIndex(row_cid) orelse return; 2524 if (key == .arrow_left) { 2525 if (row_index > 0) root_focus.setFocus(row.children.keys()[row_index - 1]); 2526 } else if (row_index + 1 < row.children.count()) { 2527 root_focus.setFocus(row.children.keys()[row_index + 1]); 2528 } 2529 }, 2530 else => try child.widget.input(allocator, key, root_focus), 2531 }, 2532 .enter => if (on_submit) try self.submitResolution(allocator), 2533 .mouse => |mouse| if (on_submit) { 2534 if (inp.leftClickOn(root_focus, child.widget.submit_button.buttonId(), mouse)) try self.submitResolution(allocator); 2535 }, 2536 else => try child.widget.input(allocator, key, root_focus), 2537 } 2538 } 2539 2540 // a multiline input keeps enter and any up/down that has a row to move to 2541 fn multilineKeepsKey(text_input: *wgt.TextInput, allocator: std.mem.Allocator, key: Key) !bool { 2542 return switch (key) { 2543 .enter => true, 2544 .arrow_up => !try text_input.cursorOnFirstRow(allocator), 2545 .arrow_down => !try text_input.cursorOnLastRow(allocator), 2546 else => false, 2547 }; 2548 } 2549 2550 // the neighboring form control, skipping spacers and blank rows 2551 fn formStep(form: *wgt.Box(Widget), cur: usize, down: bool) ?usize { 2552 var i = cur; 2553 while (true) { 2554 if (down) { 2555 i += 1; 2556 if (i >= form.children.count()) return null; 2557 } else { 2558 if (i == 0) return null; 2559 i -= 1; 2560 } 2561 const child = &form.children.values()[i]; 2562 if (child.hidden) continue; 2563 switch (child.widget) { 2564 .spacer, .text => continue, 2565 else => return i, 2566 } 2567 } 2568 } 2569 2570 // focus the resolve-form child at `i` (a version row focuses its selected 2571 // child) and keep it visible on the terminal 2572 fn focusResolveChild(self: *This, form: *wgt.Box(Widget), i: usize, root_focus: *Focus) void { 2573 const child = &form.children.values()[i]; 2574 switch (child.widget) { 2575 .box => |*row| root_focus.setFocus(row.getFocus().child_id orelse form.children.keys()[i]), 2576 else => root_focus.setFocus(form.children.keys()[i]), 2577 } 2578 if (self.session.is_terminal) { 2579 if (self.resolveScroll()) |sc| { 2580 if (child.rect) |rect| sc.scrollToRect(rect); 2581 } 2582 } 2583 } 2584 2585 // re-emit the conflicted thread's event with the resolved content; any 2586 // event on the thread settles its conflict entry. this is the terminal 2587 // path; the web posts the form to the resolve route. 2588 fn submitResolution(self: *This, allocator: std.mem.Allocator) !void { 2589 if (comptime wasm) return; 2590 const io = self.session.io orelse return; 2591 const src = self.data.repo_source orelse return; 2592 const entry = self.data.selectedThread() orelse return; 2593 const author = ((try self.session.authorize(self.data.identity, .write)) orelse return).author; 2594 const form = self.formBox() orelse return; 2595 2596 // gather the inputs by name; the d<n> hunk inputs appear in chunk order 2597 var title: ?[]u8 = null; 2598 var tags: ?[]u8 = null; 2599 var hunks: std.ArrayList([]const u8) = .empty; 2600 defer { 2601 if (title) |t| allocator.free(t); 2602 if (tags) |t| allocator.free(t); 2603 for (hunks.items) |h| allocator.free(h); 2604 hunks.deinit(allocator); 2605 } 2606 for (form.children.values()) |*child| switch (child.widget) { 2607 .text_input => |*ti| { 2608 const text = try ti.text(allocator); 2609 errdefer allocator.free(text); 2610 if (std.mem.eql(u8, ti.options.name, "title")) { 2611 title = text; 2612 } else if (std.mem.eql(u8, ti.options.name, "tags")) { 2613 tags = text; 2614 } else { 2615 try hunks.append(allocator, text); 2616 } 2617 }, 2618 else => {}, 2619 }; 2620 2621 const id_bytes = try evt.parseEventId(entry.id); 2622 2623 switch (src.repo_kind) { 2624 inline else => |repo_kind| { 2625 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2626 defer any_repo.deinit(io, allocator); 2627 switch (any_repo) { 2628 inline else => |*repo| { 2629 var resolution: Event.Resolve = .{ 2630 .title = title, 2631 .tags = tags, 2632 .hunks = hunks.items, 2633 }; 2634 if (comptime @hasField(Event.Resolve, "theirs")) resolution.theirs = self.data.theirs_picks; 2635 const change = Event.Update{ .resolve = resolution }; 2636 Event.update(self.session.data.host_kind, repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, change, author) catch |err| switch (err) { 2637 // leave the form up for correction 2638 error.InvalidFields => return, 2639 else => |e| return e, 2640 }; 2641 }, 2642 } 2643 }, 2644 } 2645 2646 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2647 try self.session.navigate(route); 2648 } 2649 2650 // commit a new or edited comment and navigate to its permalink. this is the 2651 // terminal path; the web posts the form to its comment route. 2652 fn submitComment(self: *This, allocator: std.mem.Allocator) !void { 2653 if (comptime wasm) return; 2654 const io = self.session.io orelse return; 2655 const src = self.data.repo_source orelse return; 2656 const actor = (try self.session.authorize(self.data.identity, .read)) orelse return; 2657 const author = actor.author; 2658 const form = self.formBox() orelse return; 2659 const body_input = try formField(form, "body"); 2660 const body = try body_input.text(allocator); 2661 defer allocator.free(body); 2662 if (!evt.Comment.fieldsValid(body)) return; 2663 2664 var thread_id: [evt.event_id_size * 2]u8 = undefined; 2665 @memcpy(&thread_id, self.data.selected_id); 2666 const parent_id = if (self.data.comment_id.len == 0) thread_id else blk: { 2667 var parent: [evt.event_id_size * 2]u8 = undefined; 2668 @memcpy(&parent, self.data.comment_id); 2669 break :blk parent; 2670 }; 2671 var event_id_hex: [evt.event_id_size * 2]u8 = undefined; 2672 const editing = self.data.view == .edit_comment; 2673 if (editing) { 2674 const comment_id = evt.parseEventId(self.data.comment_id) catch return; 2675 if (!try src.canModify(io, allocator, actor, .comment, &comment_id)) return; 2676 } 2677 switch (src.repo_kind) { 2678 inline else => |repo_kind| { 2679 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2680 defer any_repo.deinit(io, allocator); 2681 switch (any_repo) { 2682 inline else => |*repo| if (editing) { 2683 const comment_id = evt.parseEventId(self.data.comment_id) catch return; 2684 try evt.Comment.update(self.session.data.host_kind, repo_kind, repo.self_repo_opts, io, allocator, repo, &thread_id, &comment_id, body, author); 2685 event_id_hex = std.fmt.bytesToHex(comment_id, .lower); 2686 } else { 2687 event_id_hex = try evt.Comment.create(self.session.data.host_kind, repo_kind, repo.self_repo_opts, io, allocator, repo, &thread_id, &parent_id, body, author); 2688 }, 2689 } 2690 }, 2691 } 2692 2693 body_input.clear(allocator); 2694 const route = ui.RoutablePage.repoThreadCommentRoute(kind, self.data.identity, &thread_id, &event_id_hex, 0) orelse return; 2695 try self.session.navigate(route); 2696 } 2697 2698 // terminal form submissions; the web posts to the forms' routes instead 2699 fn submitForm(self: *This, allocator: std.mem.Allocator) !void { 2700 switch (self.data.view) { 2701 .new_comment, .edit_comment => try self.submitComment(allocator), 2702 .edit => try self.submitEditedThread(allocator), 2703 else => try self.submitNewThread(allocator), 2704 } 2705 } 2706 2707 // commit the new thread to the repo's events branch and navigate to it. 2708 // this is the terminal path; the web posts the form to the thread route, 2709 // so the wasm side never runs (or compiles) the repo access below. 2710 fn submitNewThread(self: *This, allocator: std.mem.Allocator) !void { 2711 if (comptime wasm) return; 2712 const io = self.session.io orelse return; 2713 const src = self.data.repo_source orelse return; 2714 const author = ((try self.session.authorize(self.data.identity, .read)) orelse return).author; 2715 2716 const form = self.threadForm() orelse return; 2717 const title_input = try formField(form, "title"); 2718 const tags_input = try formField(form, "tags"); 2719 const description_input = try formField(form, "description"); 2720 2721 const title = try title_input.text(allocator); 2722 defer allocator.free(title); 2723 const tags = try tags_input.text(allocator); 2724 defer allocator.free(tags); 2725 const description = try description_input.text(allocator); 2726 defer allocator.free(description); 2727 const target_branch = if (comptime supports_drafts) try (try formField(form, "target_branch")).text(allocator) else ""; 2728 defer if (comptime supports_drafts) allocator.free(target_branch); 2729 2730 if (!Event.fieldsValid(title, tags)) { 2731 if (!evt.titleValid(title)) try self.rememberThreadFeedback(.required_title, title, tags, description, target_branch); 2732 return; 2733 } 2734 2735 if (comptime supports_drafts) { 2736 const source = formWidget(form, .patch_source) orelse return error.MissingFormField; 2737 const source_branch = switch (source.selected) { 2738 .fork => null, 2739 .branch => try source.field().text(allocator), 2740 }; 2741 defer if (source_branch) |branch| allocator.free(branch); 2742 const event_id_hex = Data.create(self.data, self.session, allocator, author, title, tags, description, target_branch, source_branch) catch |err| { 2743 const failure = FeedbackFailure.fromError(err) orelse return err; 2744 try self.rememberThreadFeedback(failure, title, tags, description, target_branch); 2745 return; 2746 }; 2747 title_input.clear(allocator); 2748 tags_input.clear(allocator); 2749 description_input.clear(allocator); 2750 (try formField(form, "target_branch")).clear(allocator); 2751 const route = ui.RoutablePage.repoThreadCommentsRoute(kind, self.data.identity, &event_id_hex, 0) orelse return; 2752 try self.session.navigate(route); 2753 return; 2754 } 2755 2756 var id_bytes: [evt.event_id_size]u8 = undefined; 2757 io.random(&id_bytes); 2758 const event_id_hex = std.fmt.bytesToHex(id_bytes, .lower); 2759 2760 const event = evt.EventWithId{ 2761 .id = event_id_hex, 2762 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 2763 .author = author, 2764 .event = @unionInit(evt.Event, @tagName(kind), Event{ 2765 .title = title, 2766 .description = description, 2767 .tags = tags, 2768 }), 2769 }; 2770 2771 switch (src.repo_kind) { 2772 inline else => |repo_kind| { 2773 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2774 defer any_repo.deinit(io, allocator); 2775 switch (any_repo) { 2776 inline else => |*repo| try evt.consume(self.session.data.host_kind, .repo, repo_kind, repo.self_repo_opts, io, allocator, repo, evt.events_ref, &.{event}), 2777 } 2778 }, 2779 } 2780 2781 // wipe the form so a return visit starts fresh 2782 title_input.clear(allocator); 2783 tags_input.clear(allocator); 2784 description_input.clear(allocator); 2785 2786 const route = listRoute(self.data.identity, @enumFromInt(0), "", &event_id_hex) orelse return; 2787 try self.session.navigate(route); 2788 } 2789 2790 // re-emit the selected thread's event with the form's content (status 2791 // preserved), then reload the thread's page. this is the terminal path; 2792 // the web posts the form to the edit route. 2793 fn submitEditedThread(self: *This, allocator: std.mem.Allocator) !void { 2794 if (comptime wasm) return; 2795 const io = self.session.io orelse return; 2796 const entry = self.data.selectedThread() orelse return; 2797 const actor = (try self.session.authorize(self.data.identity, .read)) orelse return; 2798 const author = actor.author; 2799 2800 const form = self.threadForm() orelse return; 2801 const title_input = try formField(form, "title"); 2802 const tags_input = try formField(form, "tags"); 2803 const description_input = try formField(form, "description"); 2804 2805 const title = try title_input.text(allocator); 2806 defer allocator.free(title); 2807 const tags = try tags_input.text(allocator); 2808 defer allocator.free(tags); 2809 const description = try description_input.text(allocator); 2810 defer allocator.free(description); 2811 const target_branch = if (comptime supports_drafts) try (try formField(form, "target_branch")).text(allocator) else ""; 2812 defer if (comptime supports_drafts) allocator.free(target_branch); 2813 2814 if (!Event.fieldsValid(title, tags)) { 2815 if (!evt.titleValid(title)) try self.rememberThreadFeedback(.required_title, title, tags, description, target_branch); 2816 return; 2817 } 2818 const src = self.data.repo_source orelse return; 2819 2820 if (comptime supports_drafts) { 2821 if (entryDraft(entry.*)) { 2822 self.data.editDraft(self.session, allocator, author, entry.id, title, tags, description, target_branch) catch |err| { 2823 const failure = FeedbackFailure.fromError(err) orelse return err; 2824 try self.rememberThreadFeedback(failure, title, tags, description, target_branch); 2825 return; 2826 }; 2827 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2828 try self.session.navigate(route); 2829 return; 2830 } 2831 } 2832 2833 const id_bytes = try evt.parseEventId(entry.id); 2834 if (!try src.canModify(io, allocator, actor, kind, &id_bytes)) return; 2835 2836 switch (src.repo_kind) { 2837 inline else => |repo_kind| { 2838 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2839 defer any_repo.deinit(io, allocator); 2840 switch (any_repo) { 2841 inline else => |*repo| if (supports_drafts) { 2842 Event.update(self.session.data.host_kind, repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 2843 .title = title, 2844 .tags = tags, 2845 .description = description, 2846 .target_branch = target_branch, 2847 } }, author) catch |err| { 2848 const failure = FeedbackFailure.fromError(err) orelse return err; 2849 try self.rememberThreadFeedback(failure, title, tags, description, target_branch); 2850 return; 2851 }; 2852 } else if (has_status) 2853 try Event.update(self.session.data.host_kind, repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 2854 .title = title, 2855 .tags = tags, 2856 .description = description, 2857 } }, author) 2858 else 2859 try Event.update(self.session.data.host_kind, repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, title, tags, description, author), 2860 } 2861 }, 2862 } 2863 2864 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2865 try self.session.navigate(route); 2866 } 2867 2868 fn rememberThreadFeedback(self: *This, failure: FeedbackFailure, title: []const u8, tags: []const u8, description: []const u8, target_branch: []const u8) !void { 2869 const aa = self.session.arena.allocator(); 2870 const source = if (supports_drafts) blk: { 2871 const form = self.threadForm() orelse break :blk null; 2872 break :blk formWidget(form, .patch_source); 2873 } else null; 2874 self.session.data.form_feedback = @unionInit(ui.Session.FormFeedback, @tagName(feedback_tag), .{ 2875 .failure = failure, 2876 .fields = if (comptime supports_drafts) .{ 2877 .title = try aa.dupe(u8, title), 2878 .tags = try aa.dupe(u8, tags), 2879 .description = try aa.dupe(u8, description), 2880 .target_branch = try aa.dupe(u8, target_branch), 2881 .source_branch = if (source) |value| try value.field().text(aa) else "", 2882 .source_kind = if (source) |value| value.selected else .fork, 2883 } else .{ 2884 .title = try aa.dupe(u8, title), 2885 .tags = try aa.dupe(u8, tags), 2886 .description = try aa.dupe(u8, description), 2887 }, 2888 }); 2889 } 2890 2891 fn submitConfirmation(self: *This, allocator: std.mem.Allocator, button_index: usize) !void { 2892 switch (confirmation(self.data.view) orelse return) { 2893 .publish => if (comptime supports_drafts) { 2894 if (comptime wasm) return; 2895 const entry = self.data.selectedThread() orelse return; 2896 if (!entryDraft(entry.*)) return; 2897 const author = ((try self.session.authorize(self.data.identity, .read)) orelse return).author; 2898 try self.data.publishDraft(self.session, allocator, author, entry.id); 2899 const route = listRoute(self.data.identity, @enumFromInt(0), "", entry.id) orelse return; 2900 try self.session.navigate(route); 2901 } else unreachable, 2902 .merge => if (comptime supports_merge) { 2903 if (comptime wasm) return; 2904 const entry = self.data.selectedThread() orelse return; 2905 const revision: evt.Patch.MergeRevision = if (button_index > 1) .squash else .source; 2906 if (!Data.canMerge(entry.*, self.session) or entry.mergeability.get(revision) != .clean) return; 2907 const author = ((try self.session.authorize(self.data.identity, .write)) orelse return).author; 2908 self.data.mergePatch(self.session, allocator, author, entry.id, revision) catch |err| switch (err) { 2909 error.MergeConflict, error.MergeCheckUnavailable, error.PatchOutOfDate, error.PatchDataUnavailable => { 2910 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2911 try self.session.navigate(route); 2912 return; 2913 }, 2914 else => return err, 2915 }; 2916 const merged = std.meta.stringToEnum(Status, "merged") orelse return; 2917 const route = listRoute(self.data.identity, merged, "", entry.id) orelse return; 2918 try self.session.navigate(route); 2919 } else unreachable, 2920 .remove => try self.submitRemove(allocator), 2921 } 2922 } 2923 2924 fn submitRemove(self: *This, allocator: std.mem.Allocator) !void { 2925 const comment = self.data.comment_id.len != 0; 2926 const event_id = if (comment) self.data.comment_id else self.data.selected_id; 2927 const id = evt.parseEventId(event_id) catch return; 2928 try self.removeEvent(allocator, if (comment) .comment else kind, id); 2929 } 2930 2931 fn removeEvent(self: *This, allocator: std.mem.Allocator, event_kind: evt.EventKind, id: [evt.event_id_size]u8) !void { 2932 if (comptime wasm) return; 2933 const actor = (try self.session.authorize(self.data.identity, .read)) orelse return; 2934 const author = actor.author; 2935 2936 if (comptime supports_drafts) { 2937 const selected = self.data.selectedThread() orelse return; 2938 if (event_kind == kind and entryDraft(selected.*)) { 2939 try Data.removeDraft(self.session, allocator, author, &id); 2940 const route = draftsRoute(self.data.identity) orelse return; 2941 try self.session.navigate(route); 2942 return; 2943 } 2944 } 2945 2946 const io = self.session.io orelse return; 2947 const src = self.data.repo_source orelse return; 2948 if (!try src.canModify(io, allocator, actor, event_kind, &id)) return; 2949 2950 switch (src.repo_kind) { 2951 inline else => |repo_kind| { 2952 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2953 defer any_repo.deinit(io, allocator); 2954 switch (any_repo) { 2955 inline else => |*repo| try evt.remove(self.session.data.host_kind, .repo, repo_kind, repo.self_repo_opts, io, allocator, repo, &id, event_kind, author), 2956 } 2957 }, 2958 } 2959 2960 const id_hex = std.fmt.bytesToHex(id, .lower); 2961 const route = ui.RoutablePage.repoEventsRoute(self.data.identity, .removed, event_kind, &id_hex, null) orelse return; 2962 try self.session.navigate(route); 2963 } 2964 2965 // return to the list. 2966 fn focusList(self: *This, index: usize, root_focus: *Focus) void { 2967 root_focus.setFocus(self.listScroll(index).getFocus().id); 2968 } 2969 2970 // cross to the header tabs above the stack. 2971 fn focusHeader(self: *This, root_focus: *Focus) void { 2972 root_focus.setFocus(self.box.children.keys()[header_index]); 2973 } 2974 2975 pub fn clearGrid(self: *This) void { 2976 self.box.clearGrid(); 2977 } 2978 2979 pub fn getGrid(self: This) ?Grid { 2980 return self.box.getGrid(); 2981 } 2982 2983 pub fn getFocus(self: *This) *Focus { 2984 return self.box.getFocus(); 2985 } 2986 2987 // for the parent's "scroll up at the top jumps to the header" check: at the 2988 // top only while the header tabs hold focus, so up from the split 2989 // crosses into the tabs first. 2990 pub fn atTop(self: *This) bool { 2991 return self.headerActive(); 2992 } 2993 }; 2994 } 2995