╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Threads.zig ──────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
761 }; 762 } 763 764 // the resolve form's scroll on the terminal (the web page scrolls itself) 765 fn resolveScroll(self: *This) ?*wgt.Scroll(ui.Widget) { 766 return switch (self.viewStack().children.values()[form_view_index]) { 767 .scroll => |*scroll| scroll, 768 else => null, 769 }; 770 } 771 772 fn listScroll(self: *This, index: usize) *wgt.Scroll(ui.Widget) { 773 return &self.resultsBox(index).children.values()[list_index].widget.scroll; 774 } 775 776 fn listBox(self: *This, index: usize) *wgt.Box(ui.Widget) { 777 return &self.listScroll(index).child.box; 778 } 779 780 // the detail frame's children: the tool row above the scroll pane. 781 const tool_row_index: usize = 0; 782 const detail_scroll_index: usize = 1; 783 784 fn detailOuter(self: *This, index: usize) *wgt.Box(ui.Widget) { 785 return &self.resultsBox(index).children.values()[detail_index].widget.box; 786 } 787 788 fn toolRow(self: *This, index: usize) *wgt.Box(ui.Widget) { 789 return &self.detailOuter(index).children.values()[tool_row_index].widget.box; 790 } 791 792 fn detailScroll(self: *This, index: usize) *wgt.Scroll(ui.Widget) { 793 return &self.detailOuter(index).children.values()[detail_scroll_index].widget.scroll; 794 } 795 796 fn detailInner(self: *This, index: usize) *wgt.Box(ui.Widget) { 797 return &self.detailScroll(index).child.box; 798 } 799 800 fn window(self: *This, index: usize) *const Window { 801 if (has_conflicts and index == conflict_view_index) return &self.data.conflicts; 802 return self.data.window(splitStatus(index)); 803 } 804 805 fn stackSelectedIndex(self: *This) ?usize { 806 const stack = self.viewStack(); 807 const cid = stack.getFocus().child_id orelse return null; 808 return stack.children.getIndex(cid); 809 } 810 811 // the stack's selected master-detail split (null when the tags view or the 812 // new-issue form shows). 813 fn selectedSplitIndex(self: *This) ?usize { 814 const idx = self.stackSelectedIndex() orelse return null; 815 return if (idx < status_count or idx == conflict_view_index) idx else null; 816 } 817 818 fn detailActive(self: *This, index: usize) bool { 819 const rb = self.resultsBox(index); 820 const cid = rb.getFocus().child_id orelse return false; 821 return rb.children.getIndex(cid) == detail_index; 822 } 823 824 fn headerActive(self: *This) bool { 825 const cid = self.box.getFocus().child_id orelse return false; 826 return self.box.children.getIndex(cid) == header_index; 827 } 828 829 fn tagsViewActive(self: *This) bool { 830 if (self.headerActive()) return false; 831 return self.stackSelectedIndex() == tags_view_index; 832 } 833 834 fn formViewActive(self: *This) bool { 835 if (self.headerActive()) return false; 836 return self.stackSelectedIndex() == form_view_index; 837 } 838 839 // the selected issue's index, or null when a window-navigation row is 840 // selected (a leading "previous" row shifts the issue rows down by one). 841 fn selectedThreadIndex(self: *This, index: usize) ?usize { 842 const lb = self.listBox(index); 843 const cid = lb.getFocus().child_id orelse return null; 844 const idx = lb.children.getIndex(cid) orelse return null; 845 const win = self.window(index); 846 const lead: usize = if (win.prev_id != null) 1 else 0; 847 if (idx < lead or idx - lead >= win.items.len) return null; 848 return idx - lead; 849 } 850 851 pub fn build(self: *This, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 852 self.clearGrid(); 853 854 // each header tab maps 1:1 to a stack child by position. mirror the 855 // selection into the url: the view the page's window is rooted at 856 // keeps its rooted url, the others get their list route (keeping the 857 // tag filter). 858 if (self.header().getSelectedIndex()) |index| { 859 const stack = self.viewStack(); 860 stack.getFocus().child_id = stack.children.keys()[index]; 861 self.session.data.current_page = (if (index == form_view_index) 862 (if (has_conflicts) switch (self.data.view) { 863 .edit => editRoute(self.data.identity, self.data.selected_id), 864 .new_comment => newCommentRoute(self.data.identity, self.data.selected_id, self.data.comment_id), 865 .edit_comment => editCommentRoute(self.data.identity, self.data.selected_id, self.data.comment_id), 866 .resolve => resolveRoute(self.data.identity, self.data.selected_id, self.data.theirs_picks), 867 else => newRoute(self.data.identity), 868 } else switch (self.data.view) { 869 .edit => editRoute(self.data.identity, self.data.selected_id), 870 .new_comment => newCommentRoute(self.data.identity, self.data.selected_id, self.data.comment_id), 871 .edit_comment => editCommentRoute(self.data.identity, self.data.selected_id, self.data.comment_id), 872 else => newRoute(self.data.identity), 873 }) 874 else if (has_conflicts and index == conflict_view_index) 875 conflictsRoute(self.data.identity, if (self.data.view == .conflicts) self.data.selected_id else "") 876 else if (index == viewIndex(self.data.view) and self.data.selected_id.len != 0) 877 (if (self.data.description_page) 878 descriptionRoute(self.data.identity, self.data.selected_id) 879 else if (self.data.comment_page) |page| 880 commentRoute(self.data.identity, self.data.selected_id, &page.selected.id, page.replies.start) 881 else 882 commentsRoute(self.data.identity, self.data.selected_id, self.data.comments_start)) 883 else if (index == tags_view_index) 884 tagsRoute(self.data.identity, self.data.tag) 885 else 886 listRoute(self.data.identity, splitStatus(index), self.data.tag, "")) orelse self.session.data.current_page; 887 } 888 889 if (self.selectedSplitIndex()) |i| { 890 // swap the detail pane to the selected issue when it changes. 891 try self.refreshDetail(allocator, i); 892 893 // mirror the focused issue into the url, but only while focus is 894 // inside the split. an issue's url is the same whether or not the 895 // list is filtered, so the mirror drops the tag. 896 if (root_focus.grandchild_id) |g| { 897 if (self.resultsBox(i).getFocus().children.contains(g)) { 898 if (self.selectedThreadIndex(i)) |sel| { 899 // selecting another issue leaves the description page 900 // behind, like it leaves the tag filter behind. 901 const entry = &self.window(i).items[sel]; 902 const mirrored = if (self.data.description_page and std.mem.eql(u8, entry.id, self.data.selected_id)) 903 descriptionRoute(self.data.identity, entry.id) 904 else if (self.data.comment_page) |page| 905 if (std.mem.eql(u8, entry.id, self.data.selected_id)) 906 commentRoute(self.data.identity, entry.id, &page.selected.id, page.replies.start) 907 else 908 commentsRoute(self.data.identity, entry.id, 0) 909 else if (std.mem.eql(u8, entry.id, self.data.selected_id)) 910 commentsRoute(self.data.identity, entry.id, entry.comments.start) 911 else 912 commentsRoute(self.data.identity, entry.id, 0); 913 if (mirrored) |route| self.session.data.current_page = route; 914 } 915 } 916 } 917 918 // the selected list row shows a border (the focused TextBox 919 // upgrades it to a double border itself); the rest stay borderless. 920 const lb = self.listBox(i); 921 for (lb.children.keys(), lb.children.values()) |id, *child| { 922 switch (child.widget) { 923 .text_box => |*tb| tb.options.border_style = if (lb.getFocus().child_id == id) .single else .hidden, 924 else => {}, 925 } 926 } 927 928 // the pane's selected child shows the selection border: the 929 // description directly, the tags via the flow's selected item. 930 // cap the list at list_max_width only while the detail pane fits 931 // beside it. the box drops the detail when the width can't hold 932 // both minimums, so when it's that narrow we lift the cap and let 933 // the list fill the whole width. 934 const both_panes_fit = if (constraint.max_size.width) |w| w >= list_max_width + detail_min_width else true; 935 self.resultsBox(i).children.values()[list_index].max_size = if (both_panes_fit) .{ .width = list_max_width, .height = null } else null; 936 937 // stretch the detail pane across the rest of the width so it fills 938 // the area rather than shrinking to its content; its scroll fills 939 // the pane. 940 if (constraint.max_size.width) |w| { 941 self.resultsBox(i).children.values()[detail_index].min_size = .{ .width = if (both_panes_fit) w - list_max_width else w, .height = null }; 942 } else { 943 self.resultsBox(i).children.values()[detail_index].min_size = .{ .width = detail_min_width, .height = null }; 944 } 945 } 946 947 // refresh the form inputs' entries in the session's focus-id -> input 948 // map with this frame's addresses, so the web/wasm form handling can 949 // find them by focus id 950 if (self.threadForm()) |form| { 951 const inputs_arena = self.session.arena.allocator(); 952 for (form.children.values()) |*child| switch (child.widget) { 953 .text_input => |*ti| try self.session.text_inputs.put(inputs_arena, ti.getFocus().id, ti), 954 else => {}, 955 }; 956 957 // on an edit page, register the issue's content as the inputs' 958 // page-constant initial values, which the web overlay renders 959 // into them. 960 if (self.data.view == .edit) { 961 if (self.data.selectedThread()) |entry| { 962 const fields = form.children.values(); 963 try self.session.input_values.put(inputs_arena, fields[title_field_index].widget.text_input.getFocus().id, entry.record.event.title); 964 try self.session.input_values.put(inputs_arena, fields[tags_field_index].widget.text_input.getFocus().id, entry.record.event.tags); 965 try self.session.input_values.put(inputs_arena, fields[description_field_index].widget.text_input.getFocus().id, entry.record.event.description); 966 } 967 } 968 969 if (self.data.view == .edit_comment) { 970 if (self.data.comment_page) |page| { 971 const body = &form.children.values()[comment_body_field_index].widget.text_input; 972 try self.session.input_values.put(inputs_arena, body.getFocus().id, page.selected.comment.event.body); 973 } 974 } 975 976 // the resolve form's inputs prefill from the picked side, which 977 // is url state and so page-constant like the edit values above 978 if (has_conflicts and self.data.view == .resolve) { 979 for (form.children.values()) |*child| switch (child.widget) { 980 .text_input => |*ti| if (self.resolvePrefill(ti.options.name)) |value| { 981 try self.session.input_values.put(inputs_arena, ti.getFocus().id, value); 982 }, 983 else => {}, 984 }; 985 } 986 } 987 988 try self.box.build(allocator, constraint, root_focus); 989 } 990 991 fn refreshDetail(self: *This, allocator: std.mem.Allocator, index: usize) !void { 992 const sel = self.selectedThreadIndex(index) orelse return; 993 if (self.detailed_index[index]) |d| if (d == sel) return; 994 try self.populateDetail(allocator, index, sel); 995 self.detailed_index[index] = sel; 996 } 997 998 fn populateDetail(self: *This, allocator: std.mem.Allocator, index: usize, sel: usize) !void { 999 const entry = self.window(index).items[sel]; 1000 const inner = self.detailInner(index); 1001 // the /description page replaces its issue's detail with a back link 1002 // and the whole description; other issues keep their normal detail. 1003 const description_page = self.data.description_page and std.mem.eql(u8, entry.id, self.data.selected_id); 1004 const comment_page = if (std.mem.eql(u8, entry.id, self.data.selected_id)) self.data.comment_page else null; 1005 1006 for (inner.children.values()) |*child| child.widget.deinit(allocator); 1007 inner.children.clearAndFree(allocator); 1008 inner.getFocus().child_id = null; 1009 self.author_id[index] = null; 1010 self.status_button_id[index] = null; 1011 self.remove_button_id[index] = null; 1012 1013 // the tool row: the open/close and edit buttons; the description page 1014 // shows none. 1015 { 1016 const row = self.toolRow(index); 1017 for (row.children.values()) |*child| child.widget.deinit(allocator); 1018 row.children.clearAndFree(allocator); 1019 row.getFocus().child_id = null; 1020 row.getFocus().kind = .container; 1021 } 1022 if (!description_page and comment_page == null) { 1023 const row = self.toolRow(index); 1024 const pa = self.session.page_arena.allocator(); 1025 1026 { 1027 var spacer = try ui.Spacer.init(allocator); 1028 errdefer spacer.deinit(allocator); 1029 try row.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 1030 } 1031 1032 // a terminal has no file picker, so attaching is web only. the 1033 // renderer covers this button with a file input, so clicking it 1034 // opens the browser's own picker and posts what it chose. 1035 if (!self.session.is_terminal and !entryConflicted(entry) and (self.session.data.is_local or self.session.data.user_id != null)) { 1036 const label = "add attachment"; 1037 var button = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1038 errdefer button.deinit(allocator); 1039 button.getFocus().focusable = true; 1040 button.getFocus().kind = .{ .custom = if (self.data.identity.len == 0) 1041 try std.fmt.allocPrint(pa, "{s}/{s}:{s}/attach", .{ ui.file_input_prefix, @tagName(kind), entry.id }) 1042 else 1043 try std.fmt.allocPrint(pa, "{s}/repo/{s}/{s}:{s}/attach", .{ ui.file_input_prefix, self.data.identity, @tagName(kind), entry.id }) }; 1044 try row.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = .{ .width = label.len + 2, .height = null } }); 1045 } 1046 1047 // a conflicted issue's only action is resolving it. the button 1048 // stays visible logged out; the resolve page shows the 1049 // unauthorized view then. 1050 if (has_conflicts and entryConflicted(entry)) { 1051 const label = "resolve conflict"; 1052 var button = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1053 errdefer button.deinit(allocator); 1054 button.getFocus().focusable = true; 1055 const route = resolveRoute(self.data.identity, entry.id, "") orelse return error.RouteTooLong; 1056 button.getFocus().kind = .{ .custom = try std.fmt.allocPrint(pa, "a:{s}", .{try route.toUrl(self.session.page_arena)}) }; 1057 try row.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = .{ .width = label.len + 2, .height = null } }); 1058 } else { 1059 // a logged-out session can't flip the status, so it gets no 1060 // open/close button 1061 if (has_status and (self.session.data.is_local or self.session.data.user_id != null)) { 1062 const action: []const u8 = switch (entryStatus(entry)) { 1063 .open => "close", 1064 .closed => "open", 1065 }; 1066 const route = commentsRoute(self.data.identity, entry.id, 0) orelse return error.RouteTooLong; 1067 row.getFocus().kind = .{ .custom = try std.fmt.allocPrint(pa, "form:{s}/{s}", .{ try route.toUrl(self.session.page_arena), action }) }; 1068 1069 var button = try wgt.TextBox(ui.Widget).init(allocator, action, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1070 errdefer button.deinit(allocator); 1071 button.getFocus().focusable = true; 1072 // the renderer distinguishes plain clickables from buttons 1073 // that should POST to a server route by this kind. 1074 button.getFocus().kind = .{ .custom = "submit" }; 1075 self.status_button_id[index] = button.getFocus().id; 1076 try row.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = .{ .width = action.len + 2, .height = null } }); 1077 } 1078 1079 // the edit button links to the issue's edit page. 1080 { 1081 var button = try wgt.TextBox(ui.Widget).init(allocator, "edit", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1082 errdefer button.deinit(allocator); 1083 button.getFocus().focusable = true; 1084 const route = editRoute(self.data.identity, entry.id) orelse return error.RouteTooLong; 1085 button.getFocus().kind = .{ .custom = try std.fmt.allocPrint(pa, "a:{s}", .{try route.toUrl(self.session.page_arena)}) }; 1086 try row.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = .{ .width = "edit".len + 2, .height = null } }); 1087 } 1088 } 1089 1090 if (self.session.data.is_local or self.session.data.user_id != null) { 1091 const thread_route = commentsRoute(self.data.identity, entry.id, 0) orelse return error.RouteTooLong; 1092 const remove_url = try std.fmt.allocPrint(pa, "{s}/remove", .{try thread_route.toUrl(self.session.page_arena)}); 1093 const action_kind: []const u8 = if (entryConflicted(entry)) blk: { 1094 row.getFocus().kind = .{ .custom = try std.fmt.allocPrint(pa, "form:{s}", .{remove_url}) }; 1095 break :blk "submit"; 1096 } else try std.fmt.allocPrint(pa, "{s}{s}", .{ ui.submit_action_prefix, remove_url }); 1097 1098 var remove = try wgt.TextBox(ui.Widget).init(allocator, "✕", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1099 errdefer remove.deinit(allocator); 1100 remove.getFocus().focusable = true; 1101 remove.getFocus().kind = .{ .custom = action_kind }; 1102 self.remove_button_id[index] = remove.getFocus().id; 1103 try row.children.put(allocator, remove.getFocus().id, .{ .widget = .{ .text_box = remove }, .rect = null, .min_size = .{ .width = 3, .height = null } }); 1104 } 1105 1106 // the leftmost button: the attachment button when the session has 1107 // one, else the resolve button on a conflicted issue, the 1108 // open/close button when present, the edit button otherwise 1109 row.getFocus().child_id = row.children.keys()[first_in_row_index]; 1110 } 1111 1112 if (comment_page) |page| { 1113 const back_label = try std.fmt.allocPrint(self.session.page_arena.allocator(), "← back to {s}", .{@tagName(kind)}); 1114 var back = try Comment.linkBox(allocator, self.session, back_label, commentsRoute(self.data.identity, entry.id, 0) orelse return error.RouteTooLong); 1115 errdefer back.deinit(allocator); 1116 try inner.children.put(allocator, back.getFocus().id, .{ .widget = .{ .text_box = back }, .rect = null, .min_size = null }); 1117 1118 try Comment.appendComment(allocator, inner, self.session, self.data.identity, kind, page.selected); 1119 try Comment.appendCount(allocator, inner, page.replies.count, "reply", "replies", self.session.page_arena.allocator()); 1120 for (page.replies.comments) |comment| try Comment.appendComment(allocator, inner, self.session, self.data.identity, kind, comment); 1121 try Comment.appendWindowNav(allocator, inner, self.session, self.data.identity, kind, &page.selected.comment.event.thread_id, &page.selected.id, page.replies); 1122 1123 var spacer = try ui.Spacer.init(allocator); 1124 errdefer spacer.deinit(allocator); 1125 try inner.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 1126 1127 inner.getFocus().child_id = inner.children.keys()[0]; 1128 const sc = self.detailScroll(index); 1129 sc.x = 0; 1130 sc.y = 0; 1131 sc.getFocus().version +%= 1; 1132 return; 1133 } 1134 1135 if (description_page) { 1136 // the back link, in the title slot so the pane's input handling 1137 // applies to it unchanged. 1138 const back_label = try std.fmt.allocPrint(self.session.page_arena.allocator(), "← back to {s}", .{@tagName(kind)}); 1139 var tb = try wgt.TextBox(ui.Widget).init(allocator, back_label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1140 errdefer tb.deinit(allocator); 1141 tb.getFocus().focusable = true; 1142 tb.getFocus().kind = .{ .custom = try listLink(self.session.page_arena, self.data.identity, entryStatus(entry), "", entry.id) }; 1143 try inner.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1144 } else { 1145 // the issue's title as a focusable word-wrapped text box. 1146 { 1147 var tb = try wgt.TextBox(ui.Widget).init(allocator, entry.record.event.title, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .word, .label = " title " }); 1148 errdefer tb.deinit(allocator); 1149 tb.getFocus().focusable = true; 1150 try inner.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1151 } 1152 1153 self.author_id[index] = blk: { 1154 var tb = try ui.authorBox(allocator, self.session.page_arena, entry.author); 1155 errdefer tb.deinit(allocator); 1156 try inner.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1157 break :blk tb.getFocus().id; 1158 }; 1159 1160 // the issue's tags, each linking to this status's list filtered to 1161 // that tag. 1162 { 1163 var items: std.ArrayList(ui.TagFlow.Item) = .empty; 1164 defer items.deinit(allocator); 1165 var tag_iter = Event.tagIterator(entry.record.event.tags); 1166 while (tag_iter.next()) |tag| { 1167 if (tag.len == 0) continue; 1168 try items.append(allocator, .{ .text = tag, .link = try tagLink(self.session.page_arena, self.data.identity, entryStatus(entry), tag) }); 1169 } 1170 if (items.items.len > 0) { 1171 var tf = try ui.TagFlow.init(allocator); 1172 errdefer tf.deinit(allocator); 1173 try tf.setItems(allocator, items.items); 1174 try inner.children.put(allocator, tf.getFocus().id, .{ .widget = .{ .tag_flow = tf }, .rect = null, .min_size = null }); 1175 } 1176 } 1177 } 1178 1179 // the description as a focusable word-wrapped text box. past the limit 1180 // it's cut at the last whole line and links to the /description page, 1181 // which shows the whole thing. 1182 self.description_id[index] = blk: { 1183 const whole = entry.record.event.description; 1184 const cut_short = !description_page and whole.len > Data.max_description_size; 1185 const shown = if (cut_short) 1186 std.mem.trimEnd(u8, whole[0 .. std.mem.lastIndexOfScalar(u8, whole[0..Data.max_description_size], '\n') orelse 0], " \t\r\n") 1187 else if (whole.len == 0) 1188 "(no description)" 1189 else 1190 whole; 1191 var tb = try wgt.TextBox(ui.Widget).init(allocator, shown, .{ 1192 .border_style = .single, 1193 .rounded_corners = true, 1194 .wrap_kind = .word, 1195 .label = " description ", 1196 .bottom_label = if (cut_short) " click or press enter to see more " else "", 1197 }); 1198 errdefer tb.deinit(allocator); 1199 tb.getFocus().focusable = true; 1200 if (cut_short) tb.getFocus().kind = .{ .custom = try descriptionLink(self.session.page_arena, self.data.identity, entry.id) }; 1201 try inner.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1202 break :blk tb.getFocus().id; 1203 }; 1204 1205 const parent_route = commentsRoute(self.data.identity, entry.id, 0) orelse return error.RouteTooLong; 1206 try Attachment.appendRows(allocator, inner, self.session, self.data.identity, try parent_route.toUrl(self.session.page_arena), entry.attachments); 1207 1208 if (!description_page) { 1209 var reply = try Comment.linkBox(allocator, self.session, "new comment", newCommentRoute(self.data.identity, entry.id, "") orelse return error.RouteTooLong); 1210 errdefer reply.deinit(allocator); 1211 try inner.children.put(allocator, reply.getFocus().id, .{ .widget = .{ .text_box = reply }, .rect = null, .min_size = null }); 1212 1213 try Comment.appendCount(allocator, inner, entry.comments.count, "comment", "comments", self.session.page_arena.allocator()); 1214 for (entry.comments.comments) |comment| try Comment.appendComment(allocator, inner, self.session, self.data.identity, kind, comment); 1215 try Comment.appendWindowNav(allocator, inner, self.session, self.data.identity, kind, entry.id, null, entry.comments); 1216 1217 var spacer = try ui.Spacer.init(allocator); 1218 errdefer spacer.deinit(allocator); 1219 try inner.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 1220 } 1221 1222 // select the title by default 1223 inner.getFocus().child_id = inner.children.keys()[title_child_index]; 1224 1225 // reset the scroll to the top for the newly-shown issue: directly on the 1226 // terminal (the wasm offset), and via a version bump on the web (so the 1227 // renderer's scroll id changes and JS drops the preserved position). 1228 const sc = self.detailScroll(index); 1229 sc.x = 0; 1230 sc.y = 0; 1231 sc.getFocus().version +%= 1; 1232 } 1233 1234 pub fn input(self: *This, allocator: std.mem.Allocator, raw_key: Key, root_focus: *Focus) !void { 1235 // wheel input moves focus like the arrows 1236 const key: Key = if (raw_key == .mouse and raw_key.mouse.action == .scroll) 1237 (if (raw_key.mouse.action.scroll == .up) .arrow_up else .arrow_down) 1238 else 1239 raw_key; 1240 1241 if (self.headerActive()) { 1242 // down from the tabs re-enters the stack if the selected view has 1243 // something to focus; other keys move the tabs. 1244 if (inp.vertDirection(key) == .down) { 1245 const enterable = if (self.viewStack().getSelected()) |selected| switch (selected.*) { 1246 .tag_flow => |*tf| tf.text_boxes.items.len > 0, 1247 // nothing focusable in the unauthorized view 1248 .unauthorized => false, 1249 else => true, 1250 } else false; 1251 if (enterable) root_focus.setFocus(self.box.children.keys()[stack_index]); 1252 } else { 1253 try self.header().input(allocator, key, root_focus); 1254 } 1255 return; 1256 } 1257 if (self.tagsViewActive()) { 1258 self.tagsViewInput(key, root_focus); 1259 return; 1260 } 1261 if (self.formViewActive()) { 1262 if (has_conflicts and self.data.view == .resolve) { 1263 try self.resolveInput(allocator, key, root_focus); 1264 } else if (self.data.view == .new_comment or self.data.view == .edit_comment) { 1265 try self.commentFormInput(allocator, key, root_focus); 1266 } else { 1267 try self.formInput(allocator, key, root_focus); 1268 } 1269 return; 1270 } 1271 const i = self.selectedSplitIndex() orelse return; 1272 if (self.detailActive(i)) { 1273 try self.detailInput(allocator, i, key, root_focus); 1274 } else { 1275 try self.listInput(i, key, root_focus); 1276 } 1277 } 1278 1279 // arrow keys move the tag selection; up from the top row crosses to the 1280 // header tabs. 1281 fn tagsViewInput(self: *This, key: Key, root_focus: *Focus) void { 1282 const tf = self.tagsView(); 1283 const cid = tf.focus.child_id orelse return; 1284 const cur = tf.indexOfFocusId(cid) orelse return; 1285 const count = tf.text_boxes.items.len; 1286 switch (key) { 1287 .arrow_left => if (cur > 0) root_focus.setFocus(tf.text_boxes.items[cur - 1].getFocus().id), 1288 .arrow_right => if (cur + 1 < count) root_focus.setFocus(tf.text_boxes.items[cur + 1].getFocus().id), 1289 .arrow_up => if (tf.rowStep(cur, false)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id) else self.focusHeader(root_focus), 1290 .arrow_down => if (tf.rowStep(cur, true)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id), 1291 .home => root_focus.setFocus(tf.text_boxes.items[0].getFocus().id), 1292 .end => root_focus.setFocus(tf.text_boxes.items[count - 1].getFocus().id), 1293 else => {}, 1294 } 1295 } 1296 1297 fn listInput(self: *This, index: usize, key: Key, root_focus: *Focus) !void { 1298 // up/down (and the scroll wheel) move the selection a row; page up/down 1299 // jump a fixed amount. right/Enter cross into the detail pane. up from 1300 // the top row crosses into the header tabs. 1301 if (inp.rowDelta(key, @intCast(self.listBox(index).children.count()))) |delta| { 1302 const lb = self.listBox(index); 1303 const at_top = if (lb.getFocus().child_id) |cid| lb.children.getIndex(cid) == 0 else true; 1304 if (delta < 0 and at_top) return self.focusHeader(root_focus); 1305 ui.moveRowFocus(lb, self.listScroll(index), root_focus, delta); 1306 return; 1307 } 1308 switch (key) { 1309 .enter, .arrow_right => try self.focusDetail(index, root_focus), 1310 else => {}, 1311 } 1312 } 1313 1314 fn detailInput(self: *This, allocator: std.mem.Allocator, index: usize, key: Key, root_focus: *Focus) !void { 1315 switch (key) { 1316 .page_up => { 1317 self.pageDetail(index, root_focus, -10); 1318 return; 1319 }, 1320 .page_down => { 1321 self.pageDetail(index, root_focus, 10); 1322 return; 1323 }, 1324 .home => { 1325 self.jumpDetail(index, root_focus, false); 1326 return; 1327 }, 1328 .end => { 1329 self.jumpDetail(index, root_focus, true); 1330 return; 1331 }, 1332 else => {}, 1333 } 1334 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)) { 1335 try self.commentInput(allocator, index, key, root_focus); 1336 } else if (self.toolRowFocused(index)) { 1337 try self.toolRowInput(allocator, index, key, root_focus); 1338 } else if (self.titleFocused(index)) { 1339 try self.titleInput(index, key, root_focus); 1340 } else if (self.authorFocused(index)) { 1341 try self.authorInput(index, key, root_focus); 1342 } else if (self.tagsFocused(index)) { 1343 try self.tagsInput(index, key, root_focus); 1344 } else if (self.descriptionFocused(index)) { 1345 try self.descriptionInput(index, key, root_focus); 1346 } else if (try self.attachmentInput(allocator, index, key, root_focus)) {} else { 1347 try self.commentInput(allocator, index, key, root_focus); 1348 } 1349 } 1350 1351 fn attachmentInput(self: *This, allocator: std.mem.Allocator, index: usize, key: Key, root_focus: *Focus) !bool { 1352 const child_index = self.focusedDetailChild(index, root_focus) orelse return false; 1353 const child = &self.detailInner(index).children.values()[child_index].widget; 1354 const row = switch (child.*) { 1355 .box => |*box| box, 1356 else => return false, 1357 }; 1358 const attachment_id = Attachment.removeId(row) orelse return false; 1359 if (row.children.count() < 2) return false; 1360 const remove_id = row.children.keys()[1]; 1361 switch (key) { 1362 .arrow_left => if (!self.moveCommentHorizontal(index, root_focus, false)) try self.focusList(index, root_focus), 1363 .arrow_right => _ = self.moveCommentHorizontal(index, root_focus, true), 1364 .arrow_up => _ = self.moveDetailVertical(index, root_focus, false), 1365 .arrow_down => _ = self.moveDetailVertical(index, root_focus, true), 1366 .enter => if (row.getFocus().child_id == remove_id) try self.removeEvent(allocator, .attach, attachment_id), 1367 .mouse => |mouse| if (inp.leftClickOn(root_focus, remove_id, mouse)) try self.removeEvent(allocator, .attach, attachment_id), 1368 else => {}, 1369 } 1370 return true; 1371 } 1372 1373 // the tool row: enter or a click on the open/close button flips the 1374 // issue's status; the edit and resolve buttons are a: links the host 1375 // follows. arrows cross between the buttons and to the neighboring 1376 // widgets. 1377 fn toolRowInput(self: *This, allocator: std.mem.Allocator, index: usize, key: Key, root_focus: *Focus) !void { 1378 const row = self.toolRow(index); 1379 const cur = if (row.getFocus().child_id) |cid| row.children.getIndex(cid) orelse return else return; 1380 const on_status = if (self.status_button_id[index]) |id| row.getFocus().child_id == id else false; 1381 const on_remove = if (self.remove_button_id[index]) |id| row.getFocus().child_id == id else false; 1382 switch (key) { 1383 .arrow_left => if (cur > first_in_row_index) root_focus.setFocus(row.children.keys()[cur - 1]) else try self.focusList(index, root_focus), 1384 .arrow_right => if (cur + 1 < row.children.count()) root_focus.setFocus(row.children.keys()[cur + 1]), 1385 .arrow_up => self.focusHeader(root_focus), 1386 .arrow_down => { 1387 self.focusDetailEdge(index, root_focus, false); 1388 self.detailScroll(index).y = 0; 1389 }, 1390 .enter => if (on_remove) try self.removeThread(allocator, index) else if (on_status) try self.toggleStatus(allocator, index), 1391 .mouse => |mouse| if (self.remove_button_id[index]) |id| { 1392 if (inp.leftClickOn(root_focus, id, mouse)) return self.removeThread(allocator, index); 1393 if (self.status_button_id[index]) |status_id| if (inp.leftClickOn(root_focus, status_id, mouse)) try self.toggleStatus(allocator, index); 1394 } else if (self.status_button_id[index]) |id| { 1395 if (inp.leftClickOn(root_focus, id, mouse)) try self.toggleStatus(allocator, index); 1396 }, 1397 else => {}, 1398 } 1399 } 1400 1401 fn titleInput(self: *This, index: usize, key: Key, root_focus: *Focus) !void { 1402 switch (key) { 1403 .arrow_left => try self.focusList(index, root_focus), 1404 .arrow_up => self.focusToolRow(index, root_focus), 1405 .arrow_down => _ = self.moveDetailVertical(index, root_focus, true), 1406 else => {}, 1407 } 1408 } 1409 1410 // the author box's a: link (when it has one) is followed by the host on 1411 // enter / a click; arrows cross to the neighboring widgets. 1412 fn authorInput(self: *This, index: usize, key: Key, root_focus: *Focus) !void { 1413 switch (key) { 1414 .arrow_left => try self.focusList(index, root_focus), 1415 .arrow_up => _ = self.moveDetailVertical(index, root_focus, false), 1416 .arrow_down => _ = self.moveDetailVertical(index, root_focus, true), 1417 else => {}, 1418 } 1419 } 1420 1421 fn descriptionInput(self: *This, index: usize, key: Key, root_focus: *Focus) !void { 1422 const sc = self.detailScroll(index); 1423 switch (key) { 1424 .arrow_left => return self.focusList(index, root_focus), 1425 // the terminal moves through the description one line at a time; 1426 // the web lets focus changes drive its native scroll. 1427 .arrow_up => { 1428 if (!self.session.is_terminal) { 1429 _ = self.moveDetailVertical(index, root_focus, false); 1430 return; 1431 } 1432 if (self.focusAdjacentVisible(index, root_focus, false)) return; 1433 const before = sc.y; 1434 sc.y -= 1; 1435 sc.clampToContent(); 1436 if (sc.y == before) _ = self.moveDetailVertical(index, root_focus, false); 1437 return; 1438 }, 1439 .arrow_down => { 1440 if (!self.session.is_terminal) { 1441 _ = self.moveDetailVertical(index, root_focus, true); 1442 return; 1443 } 1444 if (self.focusAdjacentVisible(index, root_focus, true)) return; 1445 const before = sc.y; 1446 sc.y += 1; 1447 sc.clampToContent(); 1448 if (sc.y == before) _ = self.moveDetailVertical(index, root_focus, true); 1449 return; 1450 }, 1451 else => return, 1452 } 1453 sc.clampToContent(); 1454 } 1455 1456 fn commentInput(self: *This, allocator: std.mem.Allocator, index: usize, key: Key, root_focus: *Focus) !void { 1457 const item = self.focusedComment(index, root_focus); 1458 switch (key) { 1459 .arrow_left => if (!self.moveCommentHorizontal(index, root_focus, false)) return self.focusList(index, root_focus), 1460 .arrow_right => _ = self.moveCommentHorizontal(index, root_focus, true), 1461 .arrow_up => _ = self.moveDetailVertical(index, root_focus, false), 1462 .arrow_down => _ = self.moveDetailVertical(index, root_focus, true), 1463 .enter => if (item) |comment| if (comment.removeFocused()) try self.removeComment(allocator, comment), 1464 .mouse => |mouse| if (item) |comment| if (comment.remove_button_id) |id| { 1465 if (inp.leftClickOn(root_focus, id, mouse)) try self.removeComment(allocator, comment); 1466 }, 1467 else => return, 1468 } 1469 } 1470 1471 fn focusedComment(self: *This, index: usize, root_focus: *Focus) ?*Comment.Item { 1472 const child_index = self.focusedDetailChild(index, root_focus) orelse return null; 1473 return switch (self.detailInner(index).children.values()[child_index].widget) { 1474 .repo_comment => |*comment| comment, 1475 else => null, 1476 }; 1477 } 1478 1479 // page the detail scroll, then focus the first or last visible widget. 1480 fn pageDetail(self: *This, index: usize, root_focus: *Focus, delta: isize) void { 1481 if (!self.session.is_terminal) return; 1482 const sc = self.detailScroll(index); 1483 sc.y += delta; 1484 sc.clampToContent(); 1485 self.focusVisible(index, root_focus, delta > 0); 1486 } 1487 1488 // jump to the detail's first or last focusable widget. the terminal pins 1489 // its synthetic scroll too; the browser scrolls to the new focus itself. 1490 fn jumpDetail(self: *This, index: usize, root_focus: *Focus, to_end: bool) void { 1491 if (self.session.is_terminal) { 1492 const sc = self.detailScroll(index); 1493 sc.y = if (to_end) std.math.maxInt(isize) else 0; 1494 sc.clampToContent(); 1495 self.focusVisible(index, root_focus, to_end); 1496 } else { 1497 self.focusDetailEdge(index, root_focus, to_end); 1498 } 1499 } 1500 1501 fn focusDetailEdge(self: *This, index: usize, root_focus: *Focus, last: bool) void { 1502 const focus = self.detailInner(index).getFocus(); 1503 var chosen: ?usize = null; 1504 for (focus.children.keys(), focus.children.values()) |id, child| { 1505 if (!child.focus.focusable) continue; 1506 chosen = id; 1507 if (!last) break; 1508 } 1509 if (chosen) |id| root_focus.setFocus(id); 1510 } 1511 1512 fn detailRectVisible(self: *This, index: usize, rect: layout.IRect) bool { 1513 const sc = self.detailScroll(index); 1514 const viewport = sc.grid orelse return false; 1515 const top = sc.y; 1516 const bottom = top + @as(isize, @intCast(viewport.size.height - sc.bar_h)); 1517 return rect.y + @as(isize, @intCast(rect.size.height)) > top and rect.y < bottom; 1518 } 1519 1520 // focus the top- or bottom-most focusable widget intersecting the viewport. 1521 fn focusVisible(self: *This, index: usize, root_focus: *Focus, last: bool) void { 1522 const inner = self.detailInner(index); 1523 var offset: usize = 0; 1524 while (offset < inner.children.count()) : (offset += 1) { 1525 const child_index = if (last) inner.children.count() - 1 - offset else offset; 1526 if (self.focusVisibleChild(index, child_index, root_focus, last)) return; 1527 } 1528 } 1529 1530 fn focusVisibleChild(self: *This, index: usize, child_index: usize, root_focus: *Focus, last: bool) bool { 1531 const child = &self.detailInner(index).children.values()[child_index]; 1532 const child_rect = child.rect orelse return false; 1533 var chosen: ?usize = null; 1534 if (child.widget.getFocus().focusable and self.detailRectVisible(index, child_rect)) { 1535 const id = self.detailInner(index).children.keys()[child_index]; 1536 chosen = id; 1537 if (!last) { 1538 root_focus.setFocus(id); 1539 return true; 1540 } 1541 } 1542 1543 for (child.widget.getFocus().children.keys(), child.widget.getFocus().children.values()) |id, focus_child| { 1544 if (!focus_child.focus.focusable) continue; 1545 const rect: layout.IRect = .{ 1546 .x = child_rect.x + @as(isize, @intCast(focus_child.rect.x)), 1547 .y = child_rect.y + @as(isize, @intCast(focus_child.rect.y)), 1548 .size = focus_child.rect.size, 1549 }; 1550 if (!self.detailRectVisible(index, rect)) continue; 1551 chosen = id; 1552 if (!last) break; 1553 } 1554 if (chosen) |id| { 1555 root_focus.setFocus(id); 1556 return true; 1557 } 1558 return false; 1559 } 1560 1561 fn focusAdjacentVisible(self: *This, index: usize, root_focus: *Focus, down: bool) bool { 1562 const inner = self.detailInner(index); 1563 var next = self.focusedDetailChild(index, root_focus) orelse return false; 1564 while (true) { 1565 if (down) { 1566 next += 1; 1567 if (next >= inner.children.count()) return false; 1568 } else { 1569 if (next == 0) return false; 1570 next -= 1; 1571 } 1572 if (self.focusVisibleChild(index, next, root_focus, !down)) { 1573 self.focusDetailChild(index, next, !down, root_focus); 1574 return true; 1575 } 1576 } 1577 } 1578 1579 fn focusedDetailChild(self: *This, index: usize, root_focus: *Focus) ?usize { 1580 const inner = self.detailInner(index); 1581 var id = root_focus.grandchild_id orelse return null; 1582 while (inner.getFocus().children.get(id)) |child| { 1583 if (child.parent_id == inner.getFocus().id) return inner.children.getIndex(id); 1584 id = child.parent_id; 1585 } 1586 return null; 1587 } 1588 1589 fn detailChildFocusable(self: *This, index: usize, child_index: usize) bool { 1590 const child = &self.detailInner(index).children.values()[child_index].widget; 1591 const focus = child.getFocus(); 1592 if (focus.focusable) return true; 1593 for (focus.children.values()) |item| { 1594 if (item.focus.focusable) return true; 1595 } 1596 return false; 1597 } 1598 1599 fn focusDetailChild(self: *This, index: usize, child_index: usize, last: bool, root_focus: *Focus) void { 1600 const inner = self.detailInner(index); 1601 const child = &inner.children.values()[child_index]; 1602 var target = inner.children.keys()[child_index]; 1603 var rect = child.rect orelse return; 1604 switch (child.widget) { 1605 .repo_comment => |*comment| { 1606 if (comment.rowRect(last)) |row_rect| { 1607 rect.x += row_rect.x; 1608 rect.y += row_rect.y; 1609 rect.size = row_rect.size; 1610 } 1611 if (last) comment.focusBody(root_focus) else comment.focusMetadata(root_focus); 1612 }, 1613 .box => |*box| if (box.children.count() > 0) { 1614 target = box.children.keys()[if (last) box.children.count() - 1 else 0]; 1615 root_focus.setFocus(target); 1616 }, 1617 else => root_focus.setFocus(target), 1618 } 1619 const sc = self.detailScroll(index); 1620 if (sc.grid) |viewport| { 1621 const viewport_height = viewport.size.height - sc.bar_h; 1622 if (rect.size.height > viewport_height) { 1623 const bottom_aligned = rect.y + @as(isize, @intCast(rect.size.height - viewport_height)); 1624 sc.y = std.math.clamp(sc.y, rect.y, bottom_aligned); 1625 sc.clampToContent(); 1626 return; 1627 } 1628 } 1629 sc.scrollToRect(rect); 1630 } 1631 1632 fn moveDetailVertical(self: *This, index: usize, root_focus: *Focus, down: bool) bool { 1633 const inner = self.detailInner(index); 1634 const current = self.focusedDetailChild(index, root_focus) orelse return false; 1635 if (inner.children.values()[current].widget == .repo_comment) { 1636 const comment = &inner.children.values()[current].widget.repo_comment; 1637 if (down and !comment.bodyFocused()) { 1638 self.focusDetailChild(index, current, true, root_focus); 1639 return true; 1640 } 1641 if (!down and comment.bodyFocused()) { 1642 self.focusDetailChild(index, current, false, root_focus); 1643 return true; 1644 } 1645 } 1646 1647 var next = current; 1648 while (true) { 1649 if (down) { 1650 next += 1; 1651 if (next >= inner.children.count()) return false; 1652 } else { 1653 if (next == 0) return false; 1654 next -= 1; 1655 } 1656 if (!self.detailChildFocusable(index, next)) continue; 1657 self.focusDetailChild(index, next, !down, root_focus); 1658 return true; 1659 } 1660 } 1661 1662 fn moveCommentHorizontal(self: *This, index: usize, root_focus: *Focus, right: bool) bool { 1663 const inner = self.detailInner(index); 1664 const current = self.focusedDetailChild(index, root_focus) orelse return false; 1665 const child = &inner.children.values()[current]; 1666 if (child.widget == .repo_comment) { 1667 const moved = child.widget.repo_comment.moveHorizontal(root_focus, right); 1668 if (moved) if (child.rect) |rect| self.detailScroll(index).scrollToRect(rect); 1669 return moved; 1670 } 1671 const row = switch (child.widget) { 1672 .box => |*box| box, 1673 else => return false, 1674 }; 1675 const selected = row.getFocus().child_id orelse return false; 1676 const selected_index = row.children.getIndex(selected) orelse return false; 1677 const target = if (right) selected_index + 1 else selected_index -| 1; 1678 if (target == selected_index or target >= row.children.count()) return false; 1679 root_focus.setFocus(row.children.keys()[target]); 1680 if (child.rect) |rect| self.detailScroll(index).scrollToRect(rect); 1681 return true; 1682 } 1683 1684 // up/down (and tab/shift+tab) move between a form's fields; up from the 1685 // title crosses into the header tabs. the multiline description keeps 1686 // enter and any up/down that has a row to move to. 1687 fn formInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1688 const form = self.threadForm() orelse return; 1689 const cid = form.getFocus().child_id orelse return; 1690 const cur = form.children.getIndex(cid) orelse return; 1691 1692 if (cur == description_field_index) { 1693 const child = &form.children.values()[cur]; 1694 if (try multilineKeepsKey(&child.widget.text_input, allocator, key)) 1695 return child.widget.input(allocator, key, root_focus); 1696 } 1697 1698 switch (key) { 1699 .arrow_up, .back_tab => if (cur > 0) 1700 root_focus.setFocus(form.children.keys()[cur - 1]) 1701 else 1702 self.focusHeader(root_focus), 1703 .arrow_down, .tab => if (cur < submit_field_index) { 1704 root_focus.setFocus(form.children.keys()[cur + 1]); 1705 }, 1706 .enter => if (cur == submit_field_index) try self.submitForm(allocator), 1707 .mouse => |mouse| if (cur == submit_field_index) { 1708 const submit = &form.children.values()[cur].widget.submit_button; 1709 if (inp.leftClickOn(root_focus, submit.buttonId(), mouse)) try self.submitForm(allocator); 1710 }, 1711 else => try form.children.values()[cur].widget.input(allocator, key, root_focus), 1712 } 1713 } 1714 1715 fn commentFormInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1716 const form = self.threadForm() orelse return; 1717 const cid = form.getFocus().child_id orelse return; 1718 const cur = form.children.getIndex(cid) orelse return; 1719 const child = &form.children.values()[cur]; 1720 1721 if (cur == comment_body_field_index and try multilineKeepsKey(&child.widget.text_input, allocator, key)) 1722 return child.widget.input(allocator, key, root_focus); 1723 1724 switch (key) { 1725 .arrow_up, .back_tab => if (cur > comment_author_field_index) 1726 root_focus.setFocus(form.children.keys()[cur - 1]) 1727 else 1728 self.focusHeader(root_focus), 1729 .arrow_down, .tab => if (cur < comment_submit_field_index) 1730 root_focus.setFocus(form.children.keys()[cur + 1]), 1731 .enter => if (cur == comment_submit_field_index) try self.submitComment(allocator), 1732 .mouse => |mouse| if (cur == comment_submit_field_index) { 1733 const submit = &child.widget.submit_button; 1734 if (inp.leftClickOn(root_focus, submit.buttonId(), mouse)) try self.submitComment(allocator); 1735 }, 1736 else => try child.widget.input(allocator, key, root_focus), 1737 } 1738 } 1739 1740 // the multiline hunk inputs keep enter and any up/down that has a row to 1741 // move to; enter on a "use this" link is a navigation the host follows 1742 // before this runs. 1743 fn resolveInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1744 const form = self.threadForm() orelse return; 1745 const cid = form.getFocus().child_id orelse return; 1746 const cur = form.children.getIndex(cid) orelse return; 1747 const child = &form.children.values()[cur]; 1748 1749 if (child.widget == .text_input) { 1750 const ti = &child.widget.text_input; 1751 if (ti.options.multiline and try multilineKeepsKey(ti, allocator, key)) 1752 return child.widget.input(allocator, key, root_focus); 1753 } 1754 1755 const on_submit = child.widget == .submit_button; 1756 1757 switch (key) { 1758 .arrow_up, .back_tab => if (resolveStep(form, cur, false)) |i| self.focusResolveChild(form, i, root_focus) else self.focusHeader(root_focus), 1759 .arrow_down, .tab => if (resolveStep(form, cur, true)) |i| self.focusResolveChild(form, i, root_focus), 1760 .arrow_left, .arrow_right => switch (child.widget) { 1761 .box => |*row| { 1762 const row_cid = row.getFocus().child_id orelse return; 1763 const row_index = row.children.getIndex(row_cid) orelse return; 1764 if (key == .arrow_left) { 1765 if (row_index > 0) root_focus.setFocus(row.children.keys()[row_index - 1]); 1766 } else if (row_index + 1 < row.children.count()) { 1767 root_focus.setFocus(row.children.keys()[row_index + 1]); 1768 } 1769 }, 1770 else => try child.widget.input(allocator, key, root_focus), 1771 }, 1772 .enter => if (on_submit) try self.submitResolution(allocator), 1773 .mouse => |mouse| if (on_submit) { 1774 if (inp.leftClickOn(root_focus, child.widget.submit_button.buttonId(), mouse)) try self.submitResolution(allocator); 1775 }, 1776 else => try child.widget.input(allocator, key, root_focus), 1777 } 1778 } 1779 1780 // a multiline input keeps enter and any up/down that has a row to move to 1781 fn multilineKeepsKey(text_input: *wgt.TextInput(ui.Widget), allocator: std.mem.Allocator, key: Key) !bool { 1782 return switch (key) { 1783 .enter => true, 1784 .arrow_up => !try text_input.cursorOnFirstRow(allocator), 1785 .arrow_down => !try text_input.cursorOnLastRow(allocator), 1786 else => false, 1787 }; 1788 } 1789 1790 // the neighboring focusable resolve-form child, skipping the spacer and 1791 // the blank gap rows 1792 fn resolveStep(form: *wgt.Box(ui.Widget), cur: usize, down: bool) ?usize { 1793 var i = cur; 1794 while (true) { 1795 if (down) { 1796 i += 1; 1797 if (i >= form.children.count()) return null; 1798 } else { 1799 if (i == 0) return null; 1800 i -= 1; 1801 } 1802 switch (form.children.values()[i].widget) { 1803 .spacer, .text => continue, 1804 else => return i, 1805 } 1806 } 1807 } 1808 1809 // focus the resolve-form child at `i` (a version row focuses its selected 1810 // child) and keep it visible on the terminal 1811 fn focusResolveChild(self: *This, form: *wgt.Box(ui.Widget), i: usize, root_focus: *Focus) void { 1812 const child = &form.children.values()[i]; 1813 switch (child.widget) { 1814 .box => |*row| root_focus.setFocus(row.getFocus().child_id orelse form.children.keys()[i]), 1815 else => root_focus.setFocus(form.children.keys()[i]), 1816 } 1817 if (self.session.is_terminal) { 1818 if (self.resolveScroll()) |sc| { 1819 if (child.rect) |rect| sc.scrollToRect(rect); 1820 } 1821 } 1822 } 1823 1824 // re-emit the conflicted issue's event with the resolved content; any 1825 // event on the issue settles its conflict entry. this is the terminal 1826 // path; the web posts the form to the resolve route. 1827 fn submitResolution(self: *This, allocator: std.mem.Allocator) !void { 1828 if (comptime wasm) return; 1829 const io = self.session.io orelse return; 1830 const src = self.data.repo_source orelse return; 1831 const entry = self.data.selectedThread() orelse return; 1832 const author = (try self.session.eventAuthor()) orelse return; 1833 const form = self.threadForm() orelse return; 1834 1835 // gather the inputs by name; the d<n> hunk inputs appear in chunk order 1836 var title: ?[]u8 = null; 1837 var tags: ?[]u8 = null; 1838 var hunks: std.ArrayList([]const u8) = .empty; 1839 defer { 1840 if (title) |t| allocator.free(t); 1841 if (tags) |t| allocator.free(t); 1842 for (hunks.items) |h| allocator.free(h); 1843 hunks.deinit(allocator); 1844 } 1845 for (form.children.values()) |*child| switch (child.widget) { 1846 .text_input => |*ti| { 1847 const text = try ti.text(allocator); 1848 errdefer allocator.free(text); 1849 if (std.mem.eql(u8, ti.options.name, "title")) { 1850 title = text; 1851 } else if (std.mem.eql(u8, ti.options.name, "tags")) { 1852 tags = text; 1853 } else { 1854 try hunks.append(allocator, text); 1855 } 1856 }, 1857 else => {}, 1858 }; 1859 1860 var id_bytes: [evt.event_id_size]u8 = undefined; 1861 _ = try std.fmt.hexToBytes(&id_bytes, entry.id[0 .. evt.event_id_size * 2]); 1862 1863 switch (src.repo_kind) { 1864 inline else => |repo_kind| { 1865 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 1866 defer any_repo.deinit(io, allocator); 1867 switch (any_repo) { 1868 inline else => |*repo| Event.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .resolve = .{ 1869 .title = title, 1870 .tags = tags, 1871 .hunks = hunks.items, 1872 } }, author) catch |err| switch (err) { 1873 // leave the form up for correction 1874 error.InvalidFields => return, 1875 else => |e| return e, 1876 }, 1877 } 1878 }, 1879 } 1880 1881 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 1882 try self.session.navigate(route); 1883 } 1884 1885 // commit a new or edited comment and navigate to its permalink. this is the 1886 // terminal path; the web posts the form to its comment route. 1887 fn submitComment(self: *This, allocator: std.mem.Allocator) !void { 1888 if (comptime wasm) return; 1889 const io = self.session.io orelse return; 1890 const src = self.data.repo_source orelse return; 1891 const author = (try self.session.eventAuthor()) orelse return; 1892 const form = self.threadForm() orelse return; 1893 const body_input = &form.children.values()[comment_body_field_index].widget.text_input; 1894 const body = try body_input.text(allocator); 1895 defer allocator.free(body); 1896 if (!evt.Comment.fieldsValid(body)) return; 1897 1898 var thread_id: [evt.event_id_size * 2]u8 = undefined; 1899 @memcpy(&thread_id, self.data.selected_id); 1900 const parent_id = if (self.data.comment_id.len == 0) thread_id else blk: { 1901 var parent: [evt.event_id_size * 2]u8 = undefined; 1902 @memcpy(&parent, self.data.comment_id); 1903 break :blk parent; 1904 }; 1905 var event_id_hex: [evt.event_id_size * 2]u8 = undefined; 1906 const editing = self.data.view == .edit_comment; 1907 switch (src.repo_kind) { 1908 inline else => |repo_kind| { 1909 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 1910 defer any_repo.deinit(io, allocator); 1911 switch (any_repo) { 1912 inline else => |*repo| if (editing) { 1913 var comment_id: [evt.event_id_size]u8 = undefined; 1914 _ = std.fmt.hexToBytes(&comment_id, self.data.comment_id) catch return; 1915 try evt.Comment.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &thread_id, &comment_id, body, author); 1916 event_id_hex = std.fmt.bytesToHex(comment_id, .lower); 1917 } else { 1918 event_id_hex = try evt.Comment.create(repo_kind, repo.self_repo_opts, io, allocator, repo, &thread_id, &parent_id, body, author); 1919 }, 1920 } 1921 }, 1922 } 1923 1924 body_input.clear(allocator); 1925 const route = commentRoute(self.data.identity, &thread_id, &event_id_hex, 0) orelse return; 1926 try self.session.navigate(route); 1927 } 1928 1929 // the terminal submit paths: the new form commits a new-issue event, the 1930 // edit form re-emits the selected issue's event with the form's content. 1931 // the web posts the forms to their routes instead. 1932 fn submitForm(self: *This, allocator: std.mem.Allocator) !void { 1933 if (self.data.view == .edit) { 1934 try self.submitEditedThread(allocator); 1935 } else { 1936 try self.submitNewThread(allocator); 1937 } 1938 } 1939 1940 // commit the new issue to the repo's events branch and navigate to it. 1941 // this is the terminal path; the web posts the form to the issue route, 1942 // so the wasm side never runs (or compiles) the repo access below. 1943 fn submitNewThread(self: *This, allocator: std.mem.Allocator) !void { 1944 if (comptime wasm) return; 1945 const io = self.session.io orelse return; 1946 const src = self.data.repo_source orelse return; 1947 const author = (try self.session.eventAuthor()) orelse return; 1948 1949 const form = self.threadForm() orelse return; 1950 const title_input = &form.children.values()[title_field_index].widget.text_input; 1951 const tags_input = &form.children.values()[tags_field_index].widget.text_input; 1952 const description_input = &form.children.values()[description_field_index].widget.text_input; 1953 1954 const title = try title_input.text(allocator); 1955 defer allocator.free(title); 1956 const tags = try tags_input.text(allocator); 1957 defer allocator.free(tags); 1958 const description = try description_input.text(allocator); 1959 defer allocator.free(description); 1960 1961 if (!Event.fieldsValid(title, tags)) return; 1962 1963 var id_bytes: [evt.event_id_size]u8 = undefined; 1964 io.random(&id_bytes); 1965 const event_id_hex = std.fmt.bytesToHex(id_bytes, .lower); 1966 1967 const event = evt.EventWithId{ 1968 .id = event_id_hex, 1969 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 1970 .author = author, 1971 .event = @unionInit(evt.Event, @tagName(kind), Event{ 1972 .title = title, 1973 .description = description, 1974 .tags = tags, 1975 }), 1976 }; 1977 1978 switch (src.repo_kind) { 1979 inline else => |repo_kind| { 1980 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 1981 defer any_repo.deinit(io, allocator); 1982 switch (any_repo) { 1983 inline else => |*repo| try evt.consume(repo_kind, repo.self_repo_opts, io, allocator, repo, evt.events_ref, &.{event}), 1984 } 1985 }, 1986 } 1987 1988 // wipe the form so a return visit starts fresh 1989 title_input.clear(allocator); 1990 tags_input.clear(allocator); 1991 description_input.clear(allocator); 1992 1993 const route = listRoute(self.data.identity, @enumFromInt(0), "", &event_id_hex) orelse return; 1994 try self.session.navigate(route); 1995 } 1996 1997 // re-emit the selected issue's event with the form's content (status 1998 // preserved), then reload the issue's page. this is the terminal path; 1999 // the web posts the form to the edit route. 2000 fn submitEditedThread(self: *This, allocator: std.mem.Allocator) !void { 2001 if (comptime wasm) return; 2002 const io = self.session.io orelse return; 2003 const src = self.data.repo_source orelse return; 2004 const entry = self.data.selectedThread() orelse return; 2005 const author = (try self.session.eventAuthor()) orelse return; 2006 2007 const form = self.threadForm() orelse return; 2008 const title_input = &form.children.values()[title_field_index].widget.text_input; 2009 const tags_input = &form.children.values()[tags_field_index].widget.text_input; 2010 const description_input = &form.children.values()[description_field_index].widget.text_input; 2011 2012 const title = try title_input.text(allocator); 2013 defer allocator.free(title); 2014 const tags = try tags_input.text(allocator); 2015 defer allocator.free(tags); 2016 const description = try description_input.text(allocator); 2017 defer allocator.free(description); 2018 2019 if (!Event.fieldsValid(title, tags)) return; 2020 2021 var id_bytes: [evt.event_id_size]u8 = undefined; 2022 _ = try std.fmt.hexToBytes(&id_bytes, entry.id[0 .. evt.event_id_size * 2]); 2023 2024 switch (src.repo_kind) { 2025 inline else => |repo_kind| { 2026 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2027 defer any_repo.deinit(io, allocator); 2028 switch (any_repo) { 2029 inline else => |*repo| if (has_status) 2030 try Event.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 2031 .title = title, 2032 .tags = tags, 2033 .description = description, 2034 } }, author) 2035 else 2036 try Event.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, title, tags, description, author), 2037 } 2038 }, 2039 } 2040 2041 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2042 try self.session.navigate(route); 2043 } 2044 2045 fn removeThread(self: *This, allocator: std.mem.Allocator, index: usize) !void { 2046 const sel = self.detailed_index[index] orelse return; 2047 const entry = self.window(index).items[sel]; 2048 2049 var id_bytes: [evt.event_id_size]u8 = undefined; 2050 _ = std.fmt.hexToBytes(&id_bytes, entry.id) catch return; 2051 try self.removeEvent(allocator, kind, id_bytes); 2052 } 2053 2054 fn removeComment(self: *This, allocator: std.mem.Allocator, comment: *Comment.Item) !void { 2055 try self.removeEvent(allocator, .comment, comment.id); 2056 } 2057 2058 fn removeEvent(self: *This, allocator: std.mem.Allocator, event_kind: evt.EventKind, id: [evt.event_id_size]u8) !void { 2059 if (comptime wasm) return; 2060 const io = self.session.io orelse return; 2061 const src = self.data.repo_source orelse return; 2062 const author = (try self.session.eventAuthor()) orelse return; 2063 2064 switch (src.repo_kind) { 2065 inline else => |repo_kind| { 2066 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2067 defer any_repo.deinit(io, allocator); 2068 switch (any_repo) { 2069 inline else => |*repo| try evt.remove(repo_kind, repo.self_repo_opts, io, allocator, repo, &id, event_kind, author), 2070 } 2071 }, 2072 } 2073 2074 const id_hex = std.fmt.bytesToHex(id, .lower); 2075 const route = ui.RoutablePage.repoEventsRoute(self.data.identity, .removed, event_kind, &id_hex) orelse return; 2076 try self.session.navigate(route); 2077 } 2078 2079 // flip the shown issue's status by re-emitting its event, then reload the 2080 // page rooted at the issue so the view reflects the change. this is the 2081 // terminal path; the web posts the button's form to the status route. 2082 fn toggleStatus(self: *This, allocator: std.mem.Allocator, index: usize) !void { 2083 if (comptime !has_status) return; 2084 if (comptime wasm) return; 2085 const io = self.session.io orelse return; 2086 const src = self.data.repo_source orelse return; 2087 const sel = self.detailed_index[index] orelse return; 2088 const entry = self.window(index).items[sel]; 2089 const author = (try self.session.eventAuthor()) orelse return; 2090 2091 const status: Status = switch (entryStatus(entry)) { 2092 .open => .closed, 2093 .closed => .open, 2094 }; 2095 2096 var id_bytes: [evt.event_id_size]u8 = undefined; 2097 _ = try std.fmt.hexToBytes(&id_bytes, entry.id[0 .. evt.event_id_size * 2]); 2098 2099 switch (src.repo_kind) { 2100 inline else => |repo_kind| { 2101 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2102 defer any_repo.deinit(io, allocator); 2103 switch (any_repo) { 2104 inline else => |*repo| try Event.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .status = status }, author), 2105 } 2106 }, 2107 } 2108 2109 const route = listRoute(self.data.identity, status, "", entry.id) orelse return; 2110 try self.session.navigate(route); 2111 } 2112 2113 // arrow keys move the tag selection; at the flow's edges focus crosses to 2114 // the neighboring widgets. 2115 fn tagsInput(self: *This, index: usize, key: Key, root_focus: *Focus) !void { 2116 const tf = self.tagFlow(index) orelse return; 2117 const cid = tf.focus.child_id orelse return; 2118 const cur = tf.indexOfFocusId(cid) orelse return; 2119 const count = tf.text_boxes.items.len; 2120 switch (key) { 2121 .arrow_left => if (cur > 0) self.focusTag(index, tf, root_focus, cur - 1) else try self.focusList(index, root_focus), 2122 .arrow_right => if (cur + 1 < count) self.focusTag(index, tf, root_focus, cur + 1), 2123 .arrow_up => if (tf.rowStep(cur, false)) |i| { 2124 self.focusTag(index, tf, root_focus, i); 2125 } else { 2126 _ = self.moveDetailVertical(index, root_focus, false); 2127 }, 2128 .arrow_down => if (tf.rowStep(cur, true)) |i| { 2129 self.focusTag(index, tf, root_focus, i); 2130 } else { 2131 _ = self.moveDetailVertical(index, root_focus, true); 2132 }, 2133 .home => self.focusTag(index, tf, root_focus, 0), 2134 .end => self.focusTag(index, tf, root_focus, count - 1), 2135 else => {}, 2136 } 2137 } 2138 2139 const title_child_index: usize = 0; 2140 const tags_child_index: usize = 2; 2141 2142 // the tool row's first child after the spacer that pushes it right 2143 const first_in_row_index: usize = 1; 2144 2145 fn tagFlow(self: *This, index: usize) ?*ui.TagFlow { 2146 const inner = self.detailInner(index); 2147 if (inner.children.count() <= tags_child_index) return null; 2148 return switch (inner.children.values()[tags_child_index].widget) { 2149 .tag_flow => |*tf| tf, 2150 else => null, 2151 }; 2152 } 2153 2154 fn tagsFocused(self: *This, index: usize) bool { 2155 const inner = self.detailInner(index); 2156 const cid = inner.getFocus().child_id orelse return false; 2157 return inner.children.getIndex(cid) == tags_child_index and self.tagFlow(index) != null; 2158 } 2159 2160 fn toolRowFocused(self: *This, index: usize) bool { 2161 const outer = self.detailOuter(index); 2162 const cid = outer.getFocus().child_id orelse return false; 2163 return outer.children.getIndex(cid) == tool_row_index; 2164 } 2165 2166 fn focusTag(self: *This, index: usize, tf: *ui.TagFlow, root_focus: *Focus, item: usize) void { 2167 root_focus.setFocus(tf.text_boxes.items[item].getFocus().id); 2168 // keep the tag visible on the terminal: its rect offset by the flow's 2169 // position in the pane. 2170 if (self.session.is_terminal and item < tf.rects.items.len) { 2171 if (self.detailInner(index).children.values()[tags_child_index].rect) |flow_rect| { 2172 var rect = tf.rects.items[item]; 2173 rect.x += flow_rect.x; 2174 rect.y += flow_rect.y; 2175 self.detailScroll(index).scrollToRect(rect); 2176 } 2177 } 2178 } 2179 2180 fn titleFocused(self: *This, index: usize) bool { 2181 const inner = self.detailInner(index); 2182 const cid = inner.getFocus().child_id orelse return false; 2183 return inner.children.getIndex(cid) == title_child_index; 2184 } 2185 2186 fn authorFocused(self: *This, index: usize) bool { 2187 const inner = self.detailInner(index); 2188 const cid = inner.getFocus().child_id orelse return false; 2189 return cid == self.author_id[index]; 2190 } 2191 2192 fn descriptionFocused(self: *This, index: usize) bool { 2193 const cid = self.detailInner(index).getFocus().child_id orelse return false; 2194 return cid == self.description_id[index]; 2195 } 2196 2197 // return to the tool row's last-focused button (the header when the 2198 // description page shows no row). 2199 fn focusToolRow(self: *This, index: usize, root_focus: *Focus) void { 2200 const cid = self.toolRow(index).getFocus().child_id orelse return self.focusHeader(root_focus); 2201 root_focus.setFocus(cid); 2202 } 2203 2204 // enter the detail pane. an empty pane (no issues) can't be entered. 2205 fn focusDetail(self: *This, index: usize, root_focus: *Focus) !void { 2206 if (self.detailInner(index).children.count() == 0) return; 2207 root_focus.setFocus(self.detailOuter(index).getFocus().id); 2208 } 2209 2210 // return to the list. 2211 fn focusList(self: *This, index: usize, root_focus: *Focus) !void { 2212 root_focus.setFocus(self.listScroll(index).getFocus().id); 2213 } 2214 2215 // cross to the header tabs above the stack. 2216 fn focusHeader(self: *This, root_focus: *Focus) void { 2217 root_focus.setFocus(self.box.children.keys()[header_index]); 2218 } 2219 2220 pub fn clearGrid(self: *This) void { 2221 self.box.clearGrid(); 2222 } 2223 2224 pub fn getGrid(self: This) ?Grid { 2225 return self.box.getGrid(); 2226 } 2227 2228 pub fn getFocus(self: *This) *Focus { 2229 return self.box.getFocus(); 2230 } 2231 2232 // for the parent's "scroll up at the top jumps to the header" check: at the 2233 // top only while the header tabs hold focus, so up from the split 2234 // crosses into the tabs first. 2235 pub fn getSelectedIndex(self: *This) ?usize { 2236 return if (self.headerActive()) 0 else 1; 2237 } 2238 }; 2239 } 2240