╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Threads.zig ──────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1606 "(no description)" 1607 else 1608 whole; 1609 var tb = try wgt.TextBox(ui.Widget).init(allocator, shown, .{ 1610 .border_style = .single, 1611 .rounded_corners = true, 1612 .wrap_kind = .word, 1613 .label = " description ", 1614 .bottom_label = if (cut_short) " click or press enter to see more " else "", 1615 }); 1616 errdefer tb.deinit(allocator); 1617 tb.getFocus().focusable = true; 1618 if (cut_short) tb.getFocus().kind = .{ .custom = try descriptionLink(self.session.page_arena, self.data.identity, entry.id) }; 1619 try inner.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1620 break :blk tb.getFocus().id; 1621 }; 1622 1623 if (has_drafts and entryDraft(entry)) { 1624 if (self.data.target_branch.len != 0 and (self.session.data.clone_http_port != null or self.session.data.clone_ssh_port != null)) { 1625 var push = try ui.CloneUrl.View.initPush(allocator, self.session, self.data.identity, entry.id, self.data.target_branch); 1626 errdefer push.deinit(allocator); 1627 try inner.children.put(allocator, push.getFocus().id, .{ .widget = .{ .clone_url = push }, .rect = null, .min_size = null }); 1628 } 1629 inner.getFocus().child_id = inner.children.keys()[title_child_index]; 1630 const sc = self.detailScroll(index); 1631 sc.x = 0; 1632 sc.y = 0; 1633 sc.getFocus().version +%= 1; 1634 return; 1635 } 1636 1637 const parent_route = commentsRoute(self.data.identity, entry.id, 0) orelse return error.RouteTooLong; 1638 try Attachment.appendRows(allocator, inner, self.session, self.data.identity, try parent_route.toUrl(self.session.page_arena), entry.attachments); 1639 1640 if (!description_page) { 1641 var reply = try Comment.linkBox(allocator, self.session, "new comment", newCommentRoute(self.data.identity, entry.id, "") orelse return error.RouteTooLong); 1642 errdefer reply.deinit(allocator); 1643 try inner.children.put(allocator, reply.getFocus().id, .{ .widget = .{ .text_box = reply }, .rect = null, .min_size = null }); 1644 1645 try Comment.appendCount(allocator, inner, entry.comments.count, "comment", "comments"); 1646 for (entry.comments.comments) |comment| try Comment.appendComment(allocator, inner, self.session, self.data.identity, kind, comment); 1647 try Comment.appendWindowNav(allocator, inner, self.session, self.data.identity, kind, entry.id, null, entry.comments); 1648 1649 var spacer = try ui.Spacer.init(allocator); 1650 errdefer spacer.deinit(allocator); 1651 try inner.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 1652 } 1653 1654 // select the title by default 1655 inner.getFocus().child_id = inner.children.keys()[title_child_index]; 1656 1657 // reset the scroll to the top for the newly-shown issue: directly on the 1658 // terminal (the wasm offset), and via a version bump on the web (so the 1659 // renderer's scroll id changes and JS drops the preserved position). 1660 const sc = self.detailScroll(index); 1661 sc.x = 0; 1662 sc.y = 0; 1663 sc.getFocus().version +%= 1; 1664 } 1665 1666 pub fn input(self: *This, allocator: std.mem.Allocator, raw_key: Key, root_focus: *Focus) !void { 1667 // wheel input moves focus like the arrows 1668 const key: Key = if (raw_key == .mouse and raw_key.mouse.action == .scroll) 1669 (if (raw_key.mouse.action.scroll == .up) .arrow_up else .arrow_down) 1670 else 1671 raw_key; 1672 1673 if (self.headerActive()) { 1674 // down from the tabs re-enters the stack if the selected view has 1675 // something to focus; other keys move the tabs. 1676 if (inp.vertDirection(key) == .down) { 1677 const enterable = if (self.viewStack().getSelected()) |selected| switch (selected.*) { 1678 .tag_flow => |*tf| tf.text_boxes.items.len > 0, 1679 // nothing focusable in the unauthorized view 1680 .unauthorized => false, 1681 else => true, 1682 } else false; 1683 if (enterable) root_focus.setFocus(self.box.children.keys()[stack_index]); 1684 } else { 1685 try self.header().input(allocator, key, root_focus); 1686 } 1687 return; 1688 } 1689 if (self.tagsViewActive()) { 1690 self.tagsViewInput(key, root_focus); 1691 return; 1692 } 1693 if (self.formViewActive()) { 1694 if (self.data.view == .remove) { 1695 try self.removeFormInput(allocator, key, root_focus); 1696 } else if (has_conflicts and self.data.view == .resolve) { 1697 try self.resolveInput(allocator, key, root_focus); 1698 } else if (self.data.view == .new_comment or self.data.view == .edit_comment) { 1699 try self.commentFormInput(allocator, key, root_focus); 1700 } else { 1701 try self.formInput(allocator, key, root_focus); 1702 } 1703 return; 1704 } 1705 const i = self.selectedSplitIndex() orelse return; 1706 if (self.detailActive(i)) { 1707 try self.detailInput(allocator, i, key, root_focus); 1708 } else { 1709 try self.listInput(i, key, root_focus); 1710 } 1711 } 1712 1713 // arrow keys move the tag selection; up from the top row crosses to the 1714 // header tabs. 1715 fn tagsViewInput(self: *This, key: Key, root_focus: *Focus) void { 1716 const tf = self.tagsView(); 1717 const cid = tf.focus.child_id orelse return; 1718 const cur = tf.indexOfFocusId(cid) orelse return; 1719 const count = tf.text_boxes.items.len; 1720 switch (key) { 1721 .arrow_left => if (cur > 0) root_focus.setFocus(tf.text_boxes.items[cur - 1].getFocus().id), 1722 .arrow_right => if (cur + 1 < count) root_focus.setFocus(tf.text_boxes.items[cur + 1].getFocus().id), 1723 .arrow_up => if (tf.rowStep(cur, false)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id) else self.focusHeader(root_focus), 1724 .arrow_down => if (tf.rowStep(cur, true)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id), 1725 .home => root_focus.setFocus(tf.text_boxes.items[0].getFocus().id), 1726 .end => root_focus.setFocus(tf.text_boxes.items[count - 1].getFocus().id), 1727 else => {}, 1728 } 1729 } 1730 1731 fn listInput(self: *This, index: usize, key: Key, root_focus: *Focus) !void { 1732 // up/down (and the scroll wheel) move the selection a row; page up/down 1733 // jump a fixed amount. right/Enter cross into the detail pane. up from 1734 // the top row crosses into the header tabs. 1735 if (inp.rowDelta(key, @intCast(self.listBox(index).children.count()))) |delta| { 1736 const lb = self.listBox(index); 1737 const at_top = if (lb.getFocus().child_id) |cid| lb.children.getIndex(cid) == 0 else true; 1738 if (delta < 0 and at_top) return self.focusHeader(root_focus); 1739 ui.moveRowFocus(lb, self.listScroll(index), root_focus, delta); 1740 return; 1741 } 1742 switch (key) { 1743 .enter, .arrow_right => try self.focusDetail(index, root_focus), 1744 else => {}, 1745 } 1746 } 1747 1748 fn detailInput(self: *This, allocator: std.mem.Allocator, index: usize, key: Key, root_focus: *Focus) !void { 1749 switch (key) { 1750 .page_up => { 1751 self.pageDetail(index, root_focus, -10); 1752 return; 1753 }, 1754 .page_down => { 1755 self.pageDetail(index, root_focus, 10); 1756 return; 1757 }, 1758 .home => { 1759 self.jumpDetail(index, root_focus, false); 1760 return; 1761 }, 1762 .end => { 1763 self.jumpDetail(index, root_focus, true); 1764 return; 1765 }, 1766 else => {}, 1767 } 1768 if (self.data.comment_page != null and std.mem.eql(u8, self.window(index).items[self.detailed_index[index] orelse return].id, self.data.selected_id)) { 1769 try self.commentInput(allocator, index, key, root_focus); 1770 } else if (self.toolRowFocused(index)) { 1771 try self.toolRowInput(allocator, index, key, root_focus); 1772 } else if (self.titleFocused(index)) { 1773 try self.titleInput(index, key, root_focus); 1774 } else if (self.authorFocused(index)) { 1775 try self.authorInput(index, key, root_focus); 1776 } else if (self.tagsFocused(index)) { 1777 try self.tagsInput(index, key, root_focus); 1778 } else if (self.descriptionFocused(index)) { 1779 try self.descriptionInput(index, key, root_focus); 1780 } else if (try self.cloneUrlInput(allocator, index, key, root_focus)) {} else if (try self.attachmentInput(allocator, index, key, root_focus)) {} else { 1781 try self.commentInput(allocator, index, key, root_focus); 1782 } 1783 } 1784 1785 fn cloneUrlInput(self: *This, allocator: std.mem.Allocator, index: usize, key: Key, root_focus: *Focus) !bool { 1786 const child_index = self.focusedDetailChild(index, root_focus) orelse return false; 1787 const child = &self.detailInner(index).children.values()[child_index].widget; 1788 const clone_url = switch (child.*) { 1789 .clone_url => |*view| view, 1790 else => return false, 1791 }; 1792 switch (key) { 1793 .arrow_up => _ = self.moveDetailVertical(index, root_focus, false), 1794 .arrow_down => _ = self.moveDetailVertical(index, root_focus, true), 1795 .arrow_left => if (clone_url.selected == 0) 1796 try self.focusList(index, root_focus) 1797 else 1798 try clone_url.input(allocator, key, root_focus), 1799 else => try clone_url.input(allocator, key, root_focus), 1800 } 1801 return true; 1802 } 1803 1804 fn attachmentInput(self: *This, allocator: std.mem.Allocator, index: usize, key: Key, root_focus: *Focus) !bool { 1805 const child_index = self.focusedDetailChild(index, root_focus) orelse return false; 1806 const child = &self.detailInner(index).children.values()[child_index].widget; 1807 const row = switch (child.*) { 1808 .box => |*box| box, 1809 else => return false, 1810 }; 1811 const attachment_id = Attachment.removeId(row) orelse return false; 1812 if (row.children.count() < 2) return false; 1813 const remove_id = row.children.keys()[1]; 1814 switch (key) { 1815 .arrow_left => if (!self.moveCommentHorizontal(index, root_focus, false)) try self.focusList(index, root_focus), 1816 .arrow_right => _ = self.moveCommentHorizontal(index, root_focus, true), 1817 .arrow_up => _ = self.moveDetailVertical(index, root_focus, false), 1818 .arrow_down => _ = self.moveDetailVertical(index, root_focus, true), 1819 .enter => if (row.getFocus().child_id == remove_id) try self.removeEvent(allocator, .attach, attachment_id), 1820 .mouse => |mouse| if (inp.leftClickOn(root_focus, remove_id, mouse)) try self.removeEvent(allocator, .attach, attachment_id), 1821 else => {}, 1822 } 1823 return true; 1824 } 1825 1826 // enter or a click runs the primary action; links are handled by the host 1827 fn toolRowInput(self: *This, allocator: std.mem.Allocator, index: usize, key: Key, root_focus: *Focus) !void { 1828 const row = self.toolRow(index); 1829 const cur = if (row.getFocus().child_id) |cid| row.children.getIndex(cid) orelse return else return; 1830 const on_primary = if (self.primary_button_id[index]) |id| row.getFocus().child_id == id else false; 1831 switch (key) { 1832 .arrow_left => if (cur > first_in_row_index) root_focus.setFocus(row.children.keys()[cur - 1]) else try self.focusList(index, root_focus), 1833 .arrow_right => if (cur + 1 < row.children.count()) root_focus.setFocus(row.children.keys()[cur + 1]), 1834 .arrow_up => self.focusHeader(root_focus), 1835 .arrow_down => { 1836 self.focusDetailEdge(index, root_focus, false); 1837 self.detailScroll(index).y = 0; 1838 }, 1839 .enter => if (on_primary) try self.primaryAction(allocator, index), 1840 .mouse => |mouse| if (self.primary_button_id[index]) |id| { 1841 if (inp.leftClickOn(root_focus, id, mouse)) try self.primaryAction(allocator, index); 1842 }, 1843 else => {}, 1844 } 1845 } 1846 1847 fn primaryAction(self: *This, allocator: std.mem.Allocator, index: usize) !void { 1848 const selected = self.detailed_index[index] orelse return; 1849 const entry = self.window(index).items[selected]; 1850 if (has_drafts and entryDraft(entry)) 1851 try self.postDraft(allocator, index) 1852 else 1853 try self.toggleStatus(allocator, index); 1854 } 1855 1856 fn postDraft(self: *This, allocator: std.mem.Allocator, index: usize) !void { 1857 if (!has_drafts or comptime wasm) return; 1858 const selected = self.detailed_index[index] orelse return; 1859 const entry = self.window(index).items[selected]; 1860 if (!entryDraft(entry) or !entry.revision_ready) return; 1861 try self.data.postDraft(self.session, allocator, entry.id); 1862 const route = listRoute(self.data.identity, @enumFromInt(0), "", entry.id) orelse return; 1863 try self.session.navigate(route); 1864 } 1865 1866 fn titleInput(self: *This, index: usize, key: Key, root_focus: *Focus) !void { 1867 switch (key) { 1868 .arrow_left => try self.focusList(index, root_focus), 1869 .arrow_up => self.focusToolRow(index, root_focus), 1870 .arrow_down => _ = self.moveDetailVertical(index, root_focus, true), 1871 else => {}, 1872 } 1873 } 1874 1875 // the author box's a: link (when it has one) is followed by the host on 1876 // enter / a click; arrows cross to the neighboring widgets. 1877 fn authorInput(self: *This, index: usize, key: Key, root_focus: *Focus) !void { 1878 switch (key) { 1879 .arrow_left => try self.focusList(index, root_focus), 1880 .arrow_up => _ = self.moveDetailVertical(index, root_focus, false), 1881 .arrow_down => _ = self.moveDetailVertical(index, root_focus, true), 1882 else => {}, 1883 } 1884 } 1885 1886 fn descriptionInput(self: *This, index: usize, key: Key, root_focus: *Focus) !void { 1887 const sc = self.detailScroll(index); 1888 switch (key) { 1889 .arrow_left => return self.focusList(index, root_focus), 1890 // the terminal moves through the description one line at a time; 1891 // the web lets focus changes drive its native scroll. 1892 .arrow_up => { 1893 if (!self.session.is_terminal) { 1894 _ = self.moveDetailVertical(index, root_focus, false); 1895 return; 1896 } 1897 if (self.focusAdjacentVisible(index, root_focus, false)) return; 1898 const before = sc.y; 1899 sc.y -= 1; 1900 sc.clampToContent(); 1901 if (sc.y == before) _ = self.moveDetailVertical(index, root_focus, false); 1902 return; 1903 }, 1904 .arrow_down => { 1905 if (!self.session.is_terminal) { 1906 _ = self.moveDetailVertical(index, root_focus, true); 1907 return; 1908 } 1909 if (self.focusAdjacentVisible(index, root_focus, true)) return; 1910 const before = sc.y; 1911 sc.y += 1; 1912 sc.clampToContent(); 1913 if (sc.y == before) _ = self.moveDetailVertical(index, root_focus, true); 1914 return; 1915 }, 1916 else => return, 1917 } 1918 sc.clampToContent(); 1919 } 1920 1921 fn commentInput(self: *This, allocator: std.mem.Allocator, index: usize, key: Key, root_focus: *Focus) !void { 1922 _ = allocator; 1923 switch (key) { 1924 .arrow_left => if (!self.moveCommentHorizontal(index, root_focus, false)) return self.focusList(index, root_focus), 1925 .arrow_right => _ = self.moveCommentHorizontal(index, root_focus, true), 1926 .arrow_up => _ = self.moveDetailVertical(index, root_focus, false), 1927 .arrow_down => _ = self.moveDetailVertical(index, root_focus, true), 1928 else => return, 1929 } 1930 } 1931 1932 // page the detail scroll, then focus the first or last visible widget. 1933 fn pageDetail(self: *This, index: usize, root_focus: *Focus, delta: isize) void { 1934 if (!self.session.is_terminal) return; 1935 const sc = self.detailScroll(index); 1936 sc.y += delta; 1937 sc.clampToContent(); 1938 self.focusVisible(index, root_focus, delta > 0); 1939 } 1940 1941 // jump to the detail's first or last focusable widget. the terminal pins 1942 // its synthetic scroll too; the browser scrolls to the new focus itself. 1943 fn jumpDetail(self: *This, index: usize, root_focus: *Focus, to_end: bool) void { 1944 if (self.session.is_terminal) { 1945 const sc = self.detailScroll(index); 1946 sc.y = if (to_end) std.math.maxInt(isize) else 0; 1947 sc.clampToContent(); 1948 self.focusVisible(index, root_focus, to_end); 1949 } else { 1950 self.focusDetailEdge(index, root_focus, to_end); 1951 } 1952 } 1953 1954 fn focusDetailEdge(self: *This, index: usize, root_focus: *Focus, last: bool) void { 1955 const focus = self.detailInner(index).getFocus(); 1956 var chosen: ?usize = null; 1957 for (focus.children.keys(), focus.children.values()) |id, child| { 1958 if (!child.focus.focusable) continue; 1959 chosen = id; 1960 if (!last) break; 1961 } 1962 if (chosen) |id| root_focus.setFocus(id); 1963 } 1964 1965 fn detailRectVisible(self: *This, index: usize, rect: layout.IRect) bool { 1966 const sc = self.detailScroll(index); 1967 const viewport = sc.grid orelse return false; 1968 const top = sc.y; 1969 const bottom = top + @as(isize, @intCast(viewport.size.height - sc.bar_h)); 1970 return rect.y + @as(isize, @intCast(rect.size.height)) > top and rect.y < bottom; 1971 } 1972 1973 // focus the top- or bottom-most focusable widget intersecting the viewport. 1974 fn focusVisible(self: *This, index: usize, root_focus: *Focus, last: bool) void { 1975 const inner = self.detailInner(index); 1976 var offset: usize = 0; 1977 while (offset < inner.children.count()) : (offset += 1) { 1978 const child_index = if (last) inner.children.count() - 1 - offset else offset; 1979 if (self.focusVisibleChild(index, child_index, root_focus, last)) return; 1980 } 1981 } 1982 1983 fn focusVisibleChild(self: *This, index: usize, child_index: usize, root_focus: *Focus, last: bool) bool { 1984 const child = &self.detailInner(index).children.values()[child_index]; 1985 const child_rect = child.rect orelse return false; 1986 var chosen: ?usize = null; 1987 if (child.widget.getFocus().focusable and self.detailRectVisible(index, child_rect)) { 1988 const id = self.detailInner(index).children.keys()[child_index]; 1989 chosen = id; 1990 if (!last) { 1991 root_focus.setFocus(id); 1992 return true; 1993 } 1994 } 1995 1996 for (child.widget.getFocus().children.keys(), child.widget.getFocus().children.values()) |id, focus_child| { 1997 if (!focus_child.focus.focusable) continue; 1998 const rect: layout.IRect = .{ 1999 .x = child_rect.x + @as(isize, @intCast(focus_child.rect.x)), 2000 .y = child_rect.y + @as(isize, @intCast(focus_child.rect.y)), 2001 .size = focus_child.rect.size, 2002 }; 2003 if (!self.detailRectVisible(index, rect)) continue; 2004 chosen = id; 2005 if (!last) break; 2006 } 2007 if (chosen) |id| { 2008 root_focus.setFocus(id); 2009 return true; 2010 } 2011 return false; 2012 } 2013 2014 fn focusAdjacentVisible(self: *This, index: usize, root_focus: *Focus, down: bool) bool { 2015 const inner = self.detailInner(index); 2016 var next = self.focusedDetailChild(index, root_focus) orelse return false; 2017 while (true) { 2018 if (down) { 2019 next += 1; 2020 if (next >= inner.children.count()) return false; 2021 } else { 2022 if (next == 0) return false; 2023 next -= 1; 2024 } 2025 if (self.focusVisibleChild(index, next, root_focus, !down)) { 2026 self.focusDetailChild(index, next, !down, root_focus); 2027 return true; 2028 } 2029 } 2030 } 2031 2032 fn focusedDetailChild(self: *This, index: usize, root_focus: *Focus) ?usize { 2033 const inner = self.detailInner(index); 2034 var id = root_focus.grandchild_id orelse return null; 2035 while (inner.getFocus().children.get(id)) |child| { 2036 if (child.parent_id == inner.getFocus().id) return inner.children.getIndex(id); 2037 id = child.parent_id; 2038 } 2039 return null; 2040 } 2041 2042 fn detailChildFocusable(self: *This, index: usize, child_index: usize) bool { 2043 const child = &self.detailInner(index).children.values()[child_index].widget; 2044 const focus = child.getFocus(); 2045 if (focus.focusable) return true; 2046 for (focus.children.values()) |item| { 2047 if (item.focus.focusable) return true; 2048 } 2049 return false; 2050 } 2051 2052 fn focusDetailChild(self: *This, index: usize, child_index: usize, last: bool, root_focus: *Focus) void { 2053 const inner = self.detailInner(index); 2054 const child = &inner.children.values()[child_index]; 2055 var target = inner.children.keys()[child_index]; 2056 var rect = child.rect orelse return; 2057 switch (child.widget) { 2058 .repo_comment => |*comment| { 2059 if (comment.rowRect(last)) |row_rect| { 2060 rect.x += row_rect.x; 2061 rect.y += row_rect.y; 2062 rect.size = row_rect.size; 2063 } 2064 if (last) comment.focusBody(root_focus) else comment.focusMetadata(root_focus); 2065 }, 2066 .box => |*box| if (box.children.count() > 0) { 2067 target = box.children.keys()[if (last) box.children.count() - 1 else 0]; 2068 root_focus.setFocus(target); 2069 }, 2070 else => root_focus.setFocus(target), 2071 } 2072 const sc = self.detailScroll(index); 2073 if (sc.grid) |viewport| { 2074 const viewport_height = viewport.size.height - sc.bar_h; 2075 if (rect.size.height > viewport_height) { 2076 const bottom_aligned = rect.y + @as(isize, @intCast(rect.size.height - viewport_height)); 2077 sc.y = std.math.clamp(sc.y, rect.y, bottom_aligned); 2078 sc.clampToContent(); 2079 return; 2080 } 2081 } 2082 sc.scrollToRect(rect); 2083 } 2084 2085 fn moveDetailVertical(self: *This, index: usize, root_focus: *Focus, down: bool) bool { 2086 const inner = self.detailInner(index); 2087 const current = self.focusedDetailChild(index, root_focus) orelse return false; 2088 if (inner.children.values()[current].widget == .repo_comment) { 2089 const comment = &inner.children.values()[current].widget.repo_comment; 2090 if (down and !comment.bodyFocused()) { 2091 self.focusDetailChild(index, current, true, root_focus); 2092 return true; 2093 } 2094 if (!down and comment.bodyFocused()) { 2095 self.focusDetailChild(index, current, false, root_focus); 2096 return true; 2097 } 2098 } 2099 2100 var next = current; 2101 while (true) { 2102 if (down) { 2103 next += 1; 2104 if (next >= inner.children.count()) return false; 2105 } else { 2106 if (next == 0) return false; 2107 next -= 1; 2108 } 2109 if (!self.detailChildFocusable(index, next)) continue; 2110 self.focusDetailChild(index, next, !down, root_focus); 2111 return true; 2112 } 2113 } 2114 2115 fn moveCommentHorizontal(self: *This, index: usize, root_focus: *Focus, right: bool) bool { 2116 const inner = self.detailInner(index); 2117 const current = self.focusedDetailChild(index, root_focus) orelse return false; 2118 const child = &inner.children.values()[current]; 2119 if (child.widget == .repo_comment) { 2120 const moved = child.widget.repo_comment.moveHorizontal(root_focus, right); 2121 if (moved) if (child.rect) |rect| self.detailScroll(index).scrollToRect(rect); 2122 return moved; 2123 } 2124 const row = switch (child.widget) { 2125 .box => |*box| box, 2126 else => return false, 2127 }; 2128 const selected = row.getFocus().child_id orelse return false; 2129 const selected_index = row.children.getIndex(selected) orelse return false; 2130 const target = if (right) selected_index + 1 else selected_index -| 1; 2131 if (target == selected_index or target >= row.children.count()) return false; 2132 root_focus.setFocus(row.children.keys()[target]); 2133 if (child.rect) |rect| self.detailScroll(index).scrollToRect(rect); 2134 return true; 2135 } 2136 2137 // up/down (and tab/shift+tab) move between a form's fields; up from the 2138 // title crosses into the header tabs. the multiline description keeps 2139 // enter and any up/down that has a row to move to. 2140 fn formInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2141 const form = self.threadForm() orelse return; 2142 const cid = form.getFocus().child_id orelse return; 2143 const cur = form.children.getIndex(cid) orelse return; 2144 2145 if (cur == description_field_index) { 2146 const child = &form.children.values()[cur]; 2147 if (try multilineKeepsKey(&child.widget.text_input, allocator, key)) 2148 return child.widget.input(allocator, key, root_focus); 2149 } 2150 2151 switch (key) { 2152 .arrow_up, .back_tab => if (cur > 0) 2153 root_focus.setFocus(form.children.keys()[cur - 1]) 2154 else 2155 self.focusHeader(root_focus), 2156 .arrow_down, .tab => if (cur < submit_field_index) { 2157 root_focus.setFocus(form.children.keys()[cur + 1]); 2158 }, 2159 .enter => if (cur == submit_field_index) try self.submitForm(allocator), 2160 .mouse => |mouse| if (cur == submit_field_index) { 2161 const submit = &form.children.values()[cur].widget.submit_button; 2162 if (inp.leftClickOn(root_focus, submit.buttonId(), mouse)) try self.submitForm(allocator); 2163 }, 2164 else => try form.children.values()[cur].widget.input(allocator, key, root_focus), 2165 } 2166 } 2167 2168 fn commentFormInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2169 const form = self.threadForm() orelse return; 2170 const cid = form.getFocus().child_id orelse return; 2171 const cur = form.children.getIndex(cid) orelse return; 2172 const child = &form.children.values()[cur]; 2173 2174 if (cur == comment_body_field_index and try multilineKeepsKey(&child.widget.text_input, allocator, key)) 2175 return child.widget.input(allocator, key, root_focus); 2176 2177 switch (key) { 2178 .arrow_up, .back_tab => if (cur > comment_author_field_index) 2179 root_focus.setFocus(form.children.keys()[cur - 1]) 2180 else 2181 self.focusHeader(root_focus), 2182 .arrow_down, .tab => if (cur < comment_submit_field_index) 2183 root_focus.setFocus(form.children.keys()[cur + 1]), 2184 .enter => if (cur == comment_submit_field_index) try self.submitComment(allocator), 2185 .mouse => |mouse| if (cur == comment_submit_field_index) { 2186 const submit = &child.widget.submit_button; 2187 if (inp.leftClickOn(root_focus, submit.buttonId(), mouse)) try self.submitComment(allocator); 2188 }, 2189 else => try child.widget.input(allocator, key, root_focus), 2190 } 2191 } 2192 2193 fn removeFormInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2194 const form = self.removeForm() orelse return; 2195 const button_id = form.children.keys()[1]; 2196 switch (key) { 2197 .arrow_up, .back_tab => self.focusHeader(root_focus), 2198 .enter => try self.submitRemove(allocator), 2199 .mouse => |mouse| if (inp.leftClickOn(root_focus, button_id, mouse)) try self.submitRemove(allocator), 2200 else => {}, 2201 } 2202 } 2203 2204 // the multiline hunk inputs keep enter and any up/down that has a row to 2205 // move to; enter on a "use this" link is a navigation the host follows 2206 // before this runs. 2207 fn resolveInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2208 const form = self.threadForm() orelse return; 2209 const cid = form.getFocus().child_id orelse return; 2210 const cur = form.children.getIndex(cid) orelse return; 2211 const child = &form.children.values()[cur]; 2212 2213 if (child.widget == .text_input) { 2214 const ti = &child.widget.text_input; 2215 if (ti.options.multiline and try multilineKeepsKey(ti, allocator, key)) 2216 return child.widget.input(allocator, key, root_focus); 2217 } 2218 2219 const on_submit = child.widget == .submit_button; 2220 2221 switch (key) { 2222 .arrow_up, .back_tab => if (resolveStep(form, cur, false)) |i| self.focusResolveChild(form, i, root_focus) else self.focusHeader(root_focus), 2223 .arrow_down, .tab => if (resolveStep(form, cur, true)) |i| self.focusResolveChild(form, i, root_focus), 2224 .arrow_left, .arrow_right => switch (child.widget) { 2225 .box => |*row| { 2226 const row_cid = row.getFocus().child_id orelse return; 2227 const row_index = row.children.getIndex(row_cid) orelse return; 2228 if (key == .arrow_left) { 2229 if (row_index > 0) root_focus.setFocus(row.children.keys()[row_index - 1]); 2230 } else if (row_index + 1 < row.children.count()) { 2231 root_focus.setFocus(row.children.keys()[row_index + 1]); 2232 } 2233 }, 2234 else => try child.widget.input(allocator, key, root_focus), 2235 }, 2236 .enter => if (on_submit) try self.submitResolution(allocator), 2237 .mouse => |mouse| if (on_submit) { 2238 if (inp.leftClickOn(root_focus, child.widget.submit_button.buttonId(), mouse)) try self.submitResolution(allocator); 2239 }, 2240 else => try child.widget.input(allocator, key, root_focus), 2241 } 2242 } 2243 2244 // a multiline input keeps enter and any up/down that has a row to move to 2245 fn multilineKeepsKey(text_input: *wgt.TextInput(ui.Widget), allocator: std.mem.Allocator, key: Key) !bool { 2246 return switch (key) { 2247 .enter => true, 2248 .arrow_up => !try text_input.cursorOnFirstRow(allocator), 2249 .arrow_down => !try text_input.cursorOnLastRow(allocator), 2250 else => false, 2251 }; 2252 } 2253 2254 // the neighboring focusable resolve-form child, skipping the spacer and 2255 // the blank gap rows 2256 fn resolveStep(form: *wgt.Box(ui.Widget), cur: usize, down: bool) ?usize { 2257 var i = cur; 2258 while (true) { 2259 if (down) { 2260 i += 1; 2261 if (i >= form.children.count()) return null; 2262 } else { 2263 if (i == 0) return null; 2264 i -= 1; 2265 } 2266 switch (form.children.values()[i].widget) { 2267 .spacer, .text => continue, 2268 else => return i, 2269 } 2270 } 2271 } 2272 2273 // focus the resolve-form child at `i` (a version row focuses its selected 2274 // child) and keep it visible on the terminal 2275 fn focusResolveChild(self: *This, form: *wgt.Box(ui.Widget), i: usize, root_focus: *Focus) void { 2276 const child = &form.children.values()[i]; 2277 switch (child.widget) { 2278 .box => |*row| root_focus.setFocus(row.getFocus().child_id orelse form.children.keys()[i]), 2279 else => root_focus.setFocus(form.children.keys()[i]), 2280 } 2281 if (self.session.is_terminal) { 2282 if (self.resolveScroll()) |sc| { 2283 if (child.rect) |rect| sc.scrollToRect(rect); 2284 } 2285 } 2286 } 2287 2288 // re-emit the conflicted issue's event with the resolved content; any 2289 // event on the issue settles its conflict entry. this is the terminal 2290 // path; the web posts the form to the resolve route. 2291 fn submitResolution(self: *This, allocator: std.mem.Allocator) !void { 2292 if (comptime wasm) return; 2293 const io = self.session.io orelse return; 2294 const src = self.data.repo_source orelse return; 2295 const entry = self.data.selectedThread() orelse return; 2296 const author = (try self.session.eventAuthor()) orelse return; 2297 const form = self.threadForm() orelse return; 2298 2299 // gather the inputs by name; the d<n> hunk inputs appear in chunk order 2300 var title: ?[]u8 = null; 2301 var tags: ?[]u8 = null; 2302 var hunks: std.ArrayList([]const u8) = .empty; 2303 defer { 2304 if (title) |t| allocator.free(t); 2305 if (tags) |t| allocator.free(t); 2306 for (hunks.items) |h| allocator.free(h); 2307 hunks.deinit(allocator); 2308 } 2309 for (form.children.values()) |*child| switch (child.widget) { 2310 .text_input => |*ti| { 2311 const text = try ti.text(allocator); 2312 errdefer allocator.free(text); 2313 if (std.mem.eql(u8, ti.options.name, "title")) { 2314 title = text; 2315 } else if (std.mem.eql(u8, ti.options.name, "tags")) { 2316 tags = text; 2317 } else { 2318 try hunks.append(allocator, text); 2319 } 2320 }, 2321 else => {}, 2322 }; 2323 2324 const id_bytes = try evt.parseEventId(entry.id); 2325 2326 switch (src.repo_kind) { 2327 inline else => |repo_kind| { 2328 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2329 defer any_repo.deinit(io, allocator); 2330 switch (any_repo) { 2331 inline else => |*repo| { 2332 const change: Event.Update = if (comptime kind == .patch) 2333 .{ .resolve = .{ 2334 .title = title, 2335 .tags = tags, 2336 .hunks = hunks.items, 2337 .theirs = self.data.theirs_picks, 2338 } } 2339 else 2340 .{ .resolve = .{ 2341 .title = title, 2342 .tags = tags, 2343 .hunks = hunks.items, 2344 } }; 2345 Event.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, change, author) catch |err| switch (err) { 2346 // leave the form up for correction 2347 error.InvalidFields => return, 2348 else => |e| return e, 2349 }; 2350 }, 2351 } 2352 }, 2353 } 2354 2355 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2356 try self.session.navigate(route); 2357 } 2358 2359 // commit a new or edited comment and navigate to its permalink. this is the 2360 // terminal path; the web posts the form to its comment route. 2361 fn submitComment(self: *This, allocator: std.mem.Allocator) !void { 2362 if (comptime wasm) return; 2363 const io = self.session.io orelse return; 2364 const src = self.data.repo_source orelse return; 2365 const author = (try self.session.eventAuthor()) orelse return; 2366 const form = self.threadForm() orelse return; 2367 const body_input = &form.children.values()[comment_body_field_index].widget.text_input; 2368 const body = try body_input.text(allocator); 2369 defer allocator.free(body); 2370 if (!evt.Comment.fieldsValid(body)) return; 2371 2372 var thread_id: [evt.event_id_size * 2]u8 = undefined; 2373 @memcpy(&thread_id, self.data.selected_id); 2374 const parent_id = if (self.data.comment_id.len == 0) thread_id else blk: { 2375 var parent: [evt.event_id_size * 2]u8 = undefined; 2376 @memcpy(&parent, self.data.comment_id); 2377 break :blk parent; 2378 }; 2379 var event_id_hex: [evt.event_id_size * 2]u8 = undefined; 2380 const editing = self.data.view == .edit_comment; 2381 switch (src.repo_kind) { 2382 inline else => |repo_kind| { 2383 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2384 defer any_repo.deinit(io, allocator); 2385 switch (any_repo) { 2386 inline else => |*repo| if (editing) { 2387 const comment_id = evt.parseEventId(self.data.comment_id) catch return; 2388 try evt.Comment.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &thread_id, &comment_id, body, author); 2389 event_id_hex = std.fmt.bytesToHex(comment_id, .lower); 2390 } else { 2391 event_id_hex = try evt.Comment.create(repo_kind, repo.self_repo_opts, io, allocator, repo, &thread_id, &parent_id, body, author); 2392 }, 2393 } 2394 }, 2395 } 2396 2397 body_input.clear(allocator); 2398 const route = commentRoute(self.data.identity, &thread_id, &event_id_hex, 0) orelse return; 2399 try self.session.navigate(route); 2400 } 2401 2402 // the terminal submit paths: the new form commits a new-issue event, the 2403 // edit form re-emits the selected issue's event with the form's content. 2404 // the web posts the forms to their routes instead. 2405 fn submitForm(self: *This, allocator: std.mem.Allocator) !void { 2406 if (self.data.view == .edit) { 2407 try self.submitEditedThread(allocator); 2408 } else { 2409 try self.submitNewThread(allocator); 2410 } 2411 } 2412 2413 // commit the new issue to the repo's events branch and navigate to it. 2414 // this is the terminal path; the web posts the form to the issue route, 2415 // so the wasm side never runs (or compiles) the repo access below. 2416 fn submitNewThread(self: *This, allocator: std.mem.Allocator) !void { 2417 if (comptime wasm) return; 2418 const io = self.session.io orelse return; 2419 const src = self.data.repo_source orelse return; 2420 const author = (try self.session.eventAuthor()) orelse return; 2421 2422 const form = self.threadForm() orelse return; 2423 const title_input = &form.children.values()[title_field_index].widget.text_input; 2424 const tags_input = &form.children.values()[tags_field_index].widget.text_input; 2425 const description_input = &form.children.values()[description_field_index].widget.text_input; 2426 2427 const title = try title_input.text(allocator); 2428 defer allocator.free(title); 2429 const tags = try tags_input.text(allocator); 2430 defer allocator.free(tags); 2431 const description = try description_input.text(allocator); 2432 defer allocator.free(description); 2433 2434 if (!Event.fieldsValid(title, tags)) return; 2435 2436 if (comptime kind == .patch) { 2437 const event_id_hex = try Data.createDraft(self.data, self.session, allocator, title, tags, description); 2438 title_input.clear(allocator); 2439 tags_input.clear(allocator); 2440 description_input.clear(allocator); 2441 const route = commentsRoute(self.data.identity, &event_id_hex, 0) orelse return; 2442 try self.session.navigate(route); 2443 return; 2444 } 2445 2446 var id_bytes: [evt.event_id_size]u8 = undefined; 2447 io.random(&id_bytes); 2448 const event_id_hex = std.fmt.bytesToHex(id_bytes, .lower); 2449 2450 const event = evt.EventWithId{ 2451 .id = event_id_hex, 2452 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 2453 .author = author, 2454 .event = @unionInit(evt.Event, @tagName(kind), Event{ 2455 .title = title, 2456 .description = description, 2457 .tags = tags, 2458 }), 2459 }; 2460 2461 switch (src.repo_kind) { 2462 inline else => |repo_kind| { 2463 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2464 defer any_repo.deinit(io, allocator); 2465 switch (any_repo) { 2466 inline else => |*repo| try evt.consume(.repo, repo_kind, repo.self_repo_opts, io, allocator, repo, evt.events_ref, &.{event}), 2467 } 2468 }, 2469 } 2470 2471 // wipe the form so a return visit starts fresh 2472 title_input.clear(allocator); 2473 tags_input.clear(allocator); 2474 description_input.clear(allocator); 2475 2476 const route = listRoute(self.data.identity, @enumFromInt(0), "", &event_id_hex) orelse return; 2477 try self.session.navigate(route); 2478 } 2479 2480 // re-emit the selected issue's event with the form's content (status 2481 // preserved), then reload the issue's page. this is the terminal path; 2482 // the web posts the form to the edit route. 2483 fn submitEditedThread(self: *This, allocator: std.mem.Allocator) !void { 2484 if (comptime wasm) return; 2485 const io = self.session.io orelse return; 2486 const src = self.data.repo_source orelse return; 2487 const entry = self.data.selectedThread() orelse return; 2488 const author = (try self.session.eventAuthor()) orelse return; 2489 2490 const form = self.threadForm() orelse return; 2491 const title_input = &form.children.values()[title_field_index].widget.text_input; 2492 const tags_input = &form.children.values()[tags_field_index].widget.text_input; 2493 const description_input = &form.children.values()[description_field_index].widget.text_input; 2494 2495 const title = try title_input.text(allocator); 2496 defer allocator.free(title); 2497 const tags = try tags_input.text(allocator); 2498 defer allocator.free(tags); 2499 const description = try description_input.text(allocator); 2500 defer allocator.free(description); 2501 2502 if (!Event.fieldsValid(title, tags)) return; 2503 2504 const id_bytes = try evt.parseEventId(entry.id); 2505 2506 switch (src.repo_kind) { 2507 inline else => |repo_kind| { 2508 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2509 defer any_repo.deinit(io, allocator); 2510 switch (any_repo) { 2511 inline else => |*repo| if (has_status) 2512 try Event.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 2513 .title = title, 2514 .tags = tags, 2515 .description = description, 2516 } }, author) 2517 else 2518 try Event.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, title, tags, description, author), 2519 } 2520 }, 2521 } 2522 2523 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2524 try self.session.navigate(route); 2525 } 2526 2527 fn submitRemove(self: *This, allocator: std.mem.Allocator) !void { 2528 const comment = self.data.comment_id.len != 0; 2529 const event_id = if (comment) self.data.comment_id else self.data.selected_id; 2530 const id = evt.parseEventId(event_id) catch return; 2531 try self.removeEvent(allocator, if (comment) .comment else kind, id); 2532 } 2533 2534 fn removeEvent(self: *This, allocator: std.mem.Allocator, event_kind: evt.EventKind, id: [evt.event_id_size]u8) !void { 2535 if (comptime wasm) return; 2536 const io = self.session.io orelse return; 2537 const src = self.data.repo_source orelse return; 2538 const author = (try self.session.eventAuthor()) orelse return; 2539 2540 switch (src.repo_kind) { 2541 inline else => |repo_kind| { 2542 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2543 defer any_repo.deinit(io, allocator); 2544 switch (any_repo) { 2545 inline else => |*repo| try evt.remove(.repo, repo_kind, repo.self_repo_opts, io, allocator, repo, &id, event_kind, author), 2546 } 2547 }, 2548 } 2549 2550 const id_hex = std.fmt.bytesToHex(id, .lower); 2551 const route = ui.RoutablePage.repoEventsRoute(self.data.identity, .removed, event_kind, &id_hex) orelse return; 2552 try self.session.navigate(route); 2553 } 2554 2555 // flip the shown issue's status by re-emitting its event, then reload the 2556 // page rooted at the issue so the view reflects the change. this is the 2557 // terminal path; the web posts the button's form to the status route. 2558 fn toggleStatus(self: *This, allocator: std.mem.Allocator, index: usize) !void { 2559 if (comptime !has_status) return; 2560 if (comptime wasm) return; 2561 const io = self.session.io orelse return; 2562 const src = self.data.repo_source orelse return; 2563 const sel = self.detailed_index[index] orelse return; 2564 const entry = self.window(index).items[sel]; 2565 const author = (try self.session.eventAuthor()) orelse return; 2566 2567 const status = toggledStatus(entryStatus(entry)) orelse return; 2568 2569 const id_bytes = try evt.parseEventId(entry.id); 2570 2571 switch (src.repo_kind) { 2572 inline else => |repo_kind| { 2573 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2574 defer any_repo.deinit(io, allocator); 2575 switch (any_repo) { 2576 inline else => |*repo| try Event.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .status = status }, author), 2577 } 2578 }, 2579 } 2580 2581 const route = listRoute(self.data.identity, status, "", entry.id) orelse return; 2582 try self.session.navigate(route); 2583 } 2584 2585 // arrow keys move the tag selection; at the flow's edges focus crosses to 2586 // the neighboring widgets. 2587 fn tagsInput(self: *This, index: usize, key: Key, root_focus: *Focus) !void { 2588 const tf = self.tagFlow(index) orelse return; 2589 const cid = tf.focus.child_id orelse return; 2590 const cur = tf.indexOfFocusId(cid) orelse return; 2591 const count = tf.text_boxes.items.len; 2592 switch (key) { 2593 .arrow_left => if (cur > 0) self.focusTag(index, tf, root_focus, cur - 1) else try self.focusList(index, root_focus), 2594 .arrow_right => if (cur + 1 < count) self.focusTag(index, tf, root_focus, cur + 1), 2595 .arrow_up => if (tf.rowStep(cur, false)) |i| { 2596 self.focusTag(index, tf, root_focus, i); 2597 } else { 2598 _ = self.moveDetailVertical(index, root_focus, false); 2599 }, 2600 .arrow_down => if (tf.rowStep(cur, true)) |i| { 2601 self.focusTag(index, tf, root_focus, i); 2602 } else { 2603 _ = self.moveDetailVertical(index, root_focus, true); 2604 }, 2605 .home => self.focusTag(index, tf, root_focus, 0), 2606 .end => self.focusTag(index, tf, root_focus, count - 1), 2607 else => {}, 2608 } 2609 } 2610 2611 const title_child_index: usize = 0; 2612 const tags_child_index: usize = 2; 2613 2614 // the tool row's first child after the spacer that pushes it right 2615 const first_in_row_index: usize = 1; 2616 2617 fn tagFlow(self: *This, index: usize) ?*ui.TagFlow { 2618 const inner = self.detailInner(index); 2619 if (inner.children.count() <= tags_child_index) return null; 2620 return switch (inner.children.values()[tags_child_index].widget) { 2621 .tag_flow => |*tf| tf, 2622 else => null, 2623 }; 2624 } 2625 2626 fn tagsFocused(self: *This, index: usize) bool { 2627 const inner = self.detailInner(index); 2628 const cid = inner.getFocus().child_id orelse return false; 2629 return inner.children.getIndex(cid) == tags_child_index and self.tagFlow(index) != null; 2630 } 2631 2632 fn toolRowFocused(self: *This, index: usize) bool { 2633 const outer = self.detailOuter(index); 2634 const cid = outer.getFocus().child_id orelse return false; 2635 return outer.children.getIndex(cid) == tool_row_index; 2636 } 2637 2638 fn focusTag(self: *This, index: usize, tf: *ui.TagFlow, root_focus: *Focus, item: usize) void { 2639 root_focus.setFocus(tf.text_boxes.items[item].getFocus().id); 2640 // keep the tag visible on the terminal: its rect offset by the flow's 2641 // position in the pane. 2642 if (self.session.is_terminal and item < tf.rects.items.len) { 2643 if (self.detailInner(index).children.values()[tags_child_index].rect) |flow_rect| { 2644 var rect = tf.rects.items[item]; 2645 rect.x += flow_rect.x; 2646 rect.y += flow_rect.y; 2647 self.detailScroll(index).scrollToRect(rect); 2648 } 2649 } 2650 } 2651 2652 fn titleFocused(self: *This, index: usize) bool { 2653 const inner = self.detailInner(index); 2654 const cid = inner.getFocus().child_id orelse return false; 2655 return inner.children.getIndex(cid) == title_child_index; 2656 } 2657 2658 fn authorFocused(self: *This, index: usize) bool { 2659 const inner = self.detailInner(index); 2660 const cid = inner.getFocus().child_id orelse return false; 2661 return cid == self.author_id[index]; 2662 } 2663 2664 fn descriptionFocused(self: *This, index: usize) bool { 2665 const cid = self.detailInner(index).getFocus().child_id orelse return false; 2666 return cid == self.description_id[index]; 2667 } 2668 2669 // return to the tool row's last-focused button (the header when the 2670 // description page shows no row). 2671 fn focusToolRow(self: *This, index: usize, root_focus: *Focus) void { 2672 const cid = self.toolRow(index).getFocus().child_id orelse return self.focusHeader(root_focus); 2673 root_focus.setFocus(cid); 2674 } 2675 2676 // enter the detail pane. an empty pane (no issues) can't be entered. 2677 fn focusDetail(self: *This, index: usize, root_focus: *Focus) !void { 2678 if (self.detailInner(index).children.count() == 0) return; 2679 root_focus.setFocus(self.detailOuter(index).getFocus().id); 2680 } 2681 2682 // return to the list. 2683 fn focusList(self: *This, index: usize, root_focus: *Focus) !void { 2684 root_focus.setFocus(self.listScroll(index).getFocus().id); 2685 } 2686 2687 // cross to the header tabs above the stack. 2688 fn focusHeader(self: *This, root_focus: *Focus) void { 2689 root_focus.setFocus(self.box.children.keys()[header_index]); 2690 } 2691 2692 pub fn clearGrid(self: *This) void { 2693 self.box.clearGrid(); 2694 } 2695 2696 pub fn getGrid(self: This) ?Grid { 2697 return self.box.getGrid(); 2698 } 2699 2700 pub fn getFocus(self: *This) *Focus { 2701 return self.box.getFocus(); 2702 } 2703 2704 // for the parent's "scroll up at the top jumps to the header" check: at the 2705 // top only while the header tabs hold focus, so up from the split 2706 // crosses into the tabs first. 2707 pub fn getSelectedIndex(self: *This) ?usize { 2708 return if (self.headerActive()) 0 else 1; 2709 } 2710 }; 2711 } 2712