╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ thread.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1068 .box => |*box| box, 1069 else => return false, 1070 }; 1071 const selected = row.getFocus().child_id orelse return false; 1072 const selected_index = row.children.getIndex(selected) orelse return false; 1073 const target = if (right) selected_index + 1 else selected_index -| 1; 1074 if (target == selected_index or target >= row.children.count()) return false; 1075 root_focus.setFocus(row.children.keys()[target]); 1076 if (child.rect) |rect| self.scroll.scrollToRect(rect); 1077 return true; 1078 } 1079 1080 fn tagsInput(self: *This, child_index: usize, tags: *TagFlow, key: Key, root_focus: *Focus) void { 1081 const child_id = tags.focus.child_id orelse return; 1082 const cur = tags.indexOfFocusId(child_id) orelse return; 1083 const count = tags.text_boxes.items.len; 1084 switch (key) { 1085 .arrow_left => if (cur > 0) self.focusTag(child_index, tags, root_focus, cur - 1) else { 1086 self.exit = .list; 1087 }, 1088 .arrow_right => if (cur + 1 < count) self.focusTag(child_index, tags, root_focus, cur + 1), 1089 .arrow_up => if (tags.rowStep(cur, false)) |index| { 1090 self.focusTag(child_index, tags, root_focus, index); 1091 } else { 1092 _ = self.moveVertical(root_focus, false); 1093 }, 1094 .arrow_down => if (tags.rowStep(cur, true)) |index| { 1095 self.focusTag(child_index, tags, root_focus, index); 1096 } else { 1097 _ = self.moveVertical(root_focus, true); 1098 }, 1099 .home => self.focusTag(child_index, tags, root_focus, 0), 1100 .end => self.focusTag(child_index, tags, root_focus, count - 1), 1101 else => {}, 1102 } 1103 } 1104 1105 fn focusTag(self: *This, child_index: usize, tags: *TagFlow, root_focus: *Focus, item: usize) void { 1106 root_focus.setFocus(tags.text_boxes.items[item].getFocus().id); 1107 if (self.session.is_terminal and item < tags.rects.items.len) { 1108 if (self.inner().children.values()[child_index].rect) |flow_rect| { 1109 var rect = tags.rects.items[item]; 1110 rect.x += flow_rect.x; 1111 rect.y += flow_rect.y; 1112 self.scroll.scrollToRect(rect); 1113 } 1114 } 1115 } 1116 1117 fn toolRowFocused(self: *This, root_focus: *Focus) bool { 1118 return self.focusedChild(root_focus) == tool_row_index; 1119 } 1120 1121 fn focusToolRow(self: *This, root_focus: *Focus) void { 1122 const child_id = self.toolRow().getFocus().child_id orelse { 1123 self.exit = .header; 1124 return; 1125 }; 1126 root_focus.setFocus(child_id); 1127 if (self.inner().children.values()[tool_row_index].rect) |rect| self.scroll.scrollToRect(rect); 1128 } 1129 1130 pub fn focusFirst(self: *This, root_focus: ?*Focus) bool { 1131 const inner_box = self.inner(); 1132 for (tool_row_index + 1..inner_box.children.count()) |child_index| { 1133 if (!self.childFocusable(child_index)) continue; 1134 if (root_focus) |focus| 1135 self.focusChild(child_index, false, focus) 1136 else 1137 inner_box.getFocus().child_id = inner_box.children.keys()[child_index]; 1138 return true; 1139 } 1140 return false; 1141 } 1142 1143 pub fn atTop(self: *This, root_focus: *Focus) bool { 1144 const focused = self.focusedChild(root_focus) orelse return false; 1145 for (0..self.inner().children.count()) |child_index| { 1146 if (!self.childFocusable(child_index)) continue; 1147 return focused == child_index; 1148 } 1149 return false; 1150 } 1151 1152 fn removeEvent(self: *This, allocator: std.mem.Allocator, event_kind: evt.EventKind, id: [evt.event_id_size]u8) !void { 1153 if (comptime wasm) return; 1154 1155 if (comptime supports_drafts) { 1156 const entry = self.entry orelse return; 1157 if (event_kind == kind and entryDraft(entry)) { 1158 try Data.removeDraft(self.session, allocator, &id); 1159 const route = draftsRoute(self.data.identity) orelse return; 1160 try self.session.navigate(route); 1161 return; 1162 } 1163 } 1164 1165 const io = self.session.io orelse return; 1166 const source = self.data.repo_source orelse return; 1167 const author = (try self.session.eventAuthor()) orelse return; 1168 1169 switch (source.repo_kind) { 1170 inline else => |repo_kind| { 1171 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, source.localInitOpts()); 1172 defer any_repo.deinit(io, allocator); 1173 switch (any_repo) { 1174 inline else => |*repo| try evt.remove(.repo, repo_kind, repo.self_repo_opts, io, allocator, repo, &id, event_kind, author), 1175 } 1176 }, 1177 } 1178 1179 const id_hex = std.fmt.bytesToHex(id, .lower); 1180 const route = ui.RoutablePage.repoEventsRoute(self.data.identity, .removed, event_kind, &id_hex) orelse return; 1181 try self.session.navigate(route); 1182 } 1183 1184 fn toggleStatus(self: *This, allocator: std.mem.Allocator) !void { 1185 if (comptime (!has_status or wasm)) return; 1186 const entry = self.entry orelse return; 1187 const io = self.session.io orelse return; 1188 const source = self.data.repo_source orelse return; 1189 const author = (try self.session.eventAuthor()) orelse return; 1190 const status = (statusChange(entryStatus(entry)) orelse return).status; 1191 const id = try evt.parseEventId(entry.id); 1192 1193 switch (source.repo_kind) { 1194 inline else => |repo_kind| { 1195 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, source.localInitOpts()); 1196 defer any_repo.deinit(io, allocator); 1197 switch (any_repo) { 1198 inline else => |*repo| try Event.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id, .{ .status = status }, author), 1199 } 1200 }, 1201 } 1202 1203 const route = listRoute(self.data.identity, status, "", entry.id) orelse return; 1204 try self.session.navigate(route); 1205 } 1206 }; 1207 } 1208 1209 pub fn View(comptime kind: evt.EventKind, comptime Data: type) type { 1210 const Self = Data; 1211 const HeaderType = Data.Header; 1212 const Window = Data.Window; 1213 const Entry = Data.Entry; 1214 const Event = Data.Event; 1215 const feedback_tag: std.meta.Tag(ui.Session.FormFeedback) = switch (kind) { 1216 .issue => .issue, 1217 .patch => .patch, 1218 .discuss => .discussion, 1219 else => unreachable, 1220 }; 1221 const FeedbackFailure = if (kind == .patch) ui.Session.FormFeedback.PatchFailure else ui.Session.FormFeedback.ThreadFailure; 1222 const Status = Data.Status; 1223 const has_status = @hasField(Event, "status"); 1224 const supports_conflicts = Event.merge_policy == .field_conflicts; 1225 const supports_drafts = @hasField(Self, "drafts"); 1226 const supports_merge = @hasDecl(Data, "mergeRoute"); 1227 const FieldConflict = if (supports_conflicts) Data.FieldConflict else void; 1228 const ViewKind = Data.ViewKind; 1229 const thread_name = Data.thread_name; 1230 const DetailType = Data.Detail; 1231 1232 return struct { 1233 const This = @This(); 1234 // a vertical box: the header tabs on top, then a stack holding a 1235 // master-detail split (thread list + description pane) per status list, 1236 // plus the tags view. 1237 box: wgt.Box(Widget), // vert: [header_index] = tabs, [stack_index] = stack 1238 data: *const Self, 1239 session: *ui.Session, 1240 1241 const header_index: usize = 0; 1242 const stack_index: usize = 1; 1243 // indices within the stack, 1:1 with the header tabs. 1244 const status_count = @typeInfo(Status).@"enum".fields.len; 1245 const tags_view_index: usize = status_count; 1246 // the new-thread form, or the edit, comment, or resolve form when the page was 1247 // loaded at one of their urls. 1248 const form_view_index: usize = tags_view_index + 1; 1249 // the conflicts split; the tab and stack child exist only when the repo 1250 // has conflicted threads. 1251 const drafts_view_index: usize = form_view_index + 1; 1252 const conflict_view_index: usize = drafts_view_index + @intFromBool(supports_drafts); 1253 const view_count: usize = conflict_view_index + @intFromBool(supports_conflicts); 1254 // indices within a split (the horizontal box inside the stack). 1255 const list_index: usize = 0; 1256 const detail_index: usize = 1; 1257 const list_max_width: usize = 35; 1258 const detail_min_width: usize = 40; 1259 // indices within the thread form. 1260 const title_field_index: usize = 0; 1261 const tags_field_index: usize = 1; 1262 const description_field_index: usize = 2; 1263 const target_branch_field_index: usize = 3; 1264 const submit_field_index: usize = 3 + @as(usize, @intFromBool(supports_drafts)); 1265 const comment_author_field_index: usize = 0; 1266 const comment_body_field_index: usize = 1; 1267 const comment_submit_field_index: usize = 2; 1268 1269 pub fn viewIndex(view: ViewKind) usize { 1270 const name = @tagName(view); 1271 if (std.mem.eql(u8, name, "tags")) return tags_view_index; 1272 if (std.mem.eql(u8, name, "new") or std.mem.eql(u8, name, "edit") or std.mem.eql(u8, name, "publish") or std.mem.eql(u8, name, "merge") or std.mem.eql(u8, name, "new_comment") or std.mem.eql(u8, name, "edit_comment") or std.mem.eql(u8, name, "remove") or std.mem.eql(u8, name, "resolve")) return form_view_index; 1273 if (std.mem.eql(u8, name, "conflicts")) return conflict_view_index; 1274 if (std.mem.eql(u8, name, "drafts")) return drafts_view_index; 1275 if (std.mem.eql(u8, name, "description")) unreachable; 1276 inline for (@typeInfo(Status).@"enum".fields, 0..) |field, index| { 1277 if (std.mem.eql(u8, name, field.name)) return index; 1278 } 1279 unreachable; 1280 } 1281 1282 const Confirmation = enum { remove, publish, merge }; 1283 1284 fn confirmation(view: ViewKind) ?Confirmation { 1285 if (view == .remove) return .remove; 1286 if (supports_drafts and std.mem.eql(u8, @tagName(view), "publish")) return .publish; 1287 if (supports_merge and std.mem.eql(u8, @tagName(view), "merge")) return .merge; 1288 return null; 1289 } 1290 1291 // the status a status split lists; the conflicts split has none (its 1292 // threads carry their own). 1293 fn splitStatus(index: usize) Status { 1294 return @enumFromInt(index); 1295 } 1296 1297 fn entryConflicted(entry: Entry) bool { 1298 return if (supports_conflicts) entry.conflicted else false; 1299 } 1300 1301 fn entryStatus(entry: Entry) Status { 1302 if (comptime has_status) return Data.statusKind(entry.record.event); 1303 return @enumFromInt(0); 1304 } 1305 1306 fn entryDraft(entry: Entry) bool { 1307 return if (supports_drafts) entry.draft else false; 1308 } 1309 1310 fn listRoute(identity: []const u8, status: Status, tag: []const u8, selected: []const u8) ?ui.RoutablePage { 1311 return Data.listRoute(identity, status, tag, selected); 1312 } 1313 1314 fn draftsRoute(identity: []const u8) ?ui.RoutablePage { 1315 return if (supports_drafts) Data.draftsRoute(identity) else null; 1316 } 1317 1318 fn publishRoute(identity: []const u8, selected: []const u8) ?ui.RoutablePage { 1319 return if (supports_drafts) Data.publishRoute(identity, selected) else null; 1320 } 1321 1322 fn mergeRoute(identity: []const u8, selected: []const u8) ?ui.RoutablePage { 1323 return if (supports_merge) Data.mergeRoute(identity, selected) else null; 1324 } 1325 1326 fn conflictsRoute(identity: []const u8, selected: []const u8) ?ui.RoutablePage { 1327 return if (supports_conflicts) Data.conflictsRoute(identity, selected) else null; 1328 } 1329 1330 fn resolveRoute(identity: []const u8, selected: []const u8, picks: []const u8) ?ui.RoutablePage { 1331 return if (supports_conflicts) Data.resolveRoute(identity, selected, picks) else null; 1332 } 1333 1334 fn listLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, status: Status, tag: []const u8, id: []const u8) ![]const u8 { 1335 const route = listRoute(identity, status, tag, id) orelse return error.RouteTooLong; 1336 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{try route.toUrl(page_arena)}); 1337 } 1338 1339 fn rowLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, id: []const u8) ![]const u8 { 1340 const selected = std.mem.eql(u8, id, data.selected_id); 1341 const route = (if (selected and data.description_page) 1342 ui.RoutablePage.repoThreadDescriptionRoute(kind, data.identity, id) 1343 else if (selected) 1344 if (data.comment_page) |page| 1345 ui.RoutablePage.repoThreadCommentRoute(kind, data.identity, id, &page.selected.id, page.replies.start) 1346 else 1347 ui.RoutablePage.repoThreadCommentsRoute(kind, data.identity, id, data.comments_start) 1348 else 1349 ui.RoutablePage.repoThreadCommentsRoute(kind, data.identity, id, 0)) orelse return error.RouteTooLong; 1350 return std.fmt.allocPrint(page_arena.allocator(), "ai:{s}", .{try route.toUrl(page_arena)}); 1351 } 1352 1353 fn windowLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, status_maybe: ?Status, drafts: bool, id: []const u8) ![]const u8 { 1354 if (supports_conflicts and status_maybe == null and !drafts) { 1355 const route = conflictsRoute(data.identity, id) orelse return error.RouteTooLong; 1356 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{try route.toUrl(page_arena)}); 1357 } 1358 if (drafts and id.len == 0) { 1359 const route = draftsRoute(data.identity) orelse return error.RouteTooLong; 1360 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{try route.toUrl(page_arena)}); 1361 } 1362 return listLink(page_arena, data.identity, status_maybe orelse @enumFromInt(0), data.tag, id); 1363 } 1364 1365 fn tagLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, status: Status, tag: []const u8) ![]const u8 { 1366 const encoded = try ui.urlEncodeRef(page_arena.allocator(), tag); 1367 return listLink(page_arena, identity, status, encoded, ""); 1368 } 1369 1370 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !This { 1371 var outer = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 1372 errdefer outer.deinit(allocator); 1373 1374 // the tabs at the top. 1375 { 1376 var hdr = try Data.initHeader(allocator, session, data); 1377 errdefer hdr.deinit(allocator); 1378 try outer.children.put(allocator, hdr.getFocus().id, .{ .widget = @unionInit(Widget, Data.header_widget_name, hdr), .rect = null, .min_size = null }); 1379 } 1380 1381 // the stack enters `outer` before its children enter it, so an error 1382 // frees each child exactly once. 1383 { 1384 var stack = try wgt.Stack(Widget).init(allocator); 1385 errdefer stack.deinit(allocator); 1386 try outer.children.put(allocator, stack.getFocus().id, .{ .widget = .{ .stack = stack }, .rect = null, .min_size = null }); 1387 } 1388 const stack = &outer.children.values()[stack_index].widget.stack; 1389 1390 inline for (@typeInfo(Status).@"enum".fields) |field| { 1391 var split = try initSplit(allocator, session, data, @enumFromInt(field.value), false); 1392 errdefer split.deinit(allocator); 1393 try stack.children.put(allocator, split.getFocus().id, .{ .box = split }); 1394 } 1395 1396 // the tags view 1397 { 1398 var tf = try TagFlow.init(allocator); 1399 errdefer tf.deinit(allocator); 1400 var items: std.ArrayList(TagFlow.Item) = .empty; 1401 defer items.deinit(allocator); 1402 // when filtered, the first item clears the filter 1403 if (data.tag.len != 0) 1404 try items.append(allocator, .{ .text = "✕", .link = try listLink(session.page_arena, data.identity, @enumFromInt(0), "", "") }); 1405 for (data.tags) |tag| 1406 try items.append(allocator, .{ .text = tag, .link = try tagLink(session.page_arena, data.identity, @enumFromInt(0), tag) }); 1407 try tf.setItems(allocator, items.items); 1408 try stack.children.put(allocator, tf.getFocus().id, .{ .tag_flow = tf }); 1409 } 1410 1411 // the new-thread form, or — on an edit, comment, or resolve url — that form in 1412 // its place, prefilled with the selected thread's content. a 1413 // logged-out session can't create events, so the unauthorized view 1414 // stands in. 1415 if (session.data.is_local or session.data.user_id != null) { 1416 if (confirmation(data.view)) |confirmation_kind| { 1417 const aa = session.page_arena.allocator(); 1418 const route = switch (confirmation_kind) { 1419 .remove => ui.RoutablePage.repoThreadRemoveRoute(kind, data.identity, data.selected_id, data.comment_id), 1420 .publish => publishRoute(data.identity, data.selected_id), 1421 .merge => mergeRoute(data.identity, data.selected_id), 1422 } orelse return error.RouteTooLong; 1423 const page_url = try route.toUrl(session.page_arena); 1424 const action = try std.fmt.allocPrint(aa, "form:{s}", .{page_url}); 1425 const event_name = if (data.comment_id.len == 0) thread_name else "comment"; 1426 var label_buf: ["remove ".len + @max(thread_name.len, "comment".len)]u8 = undefined; 1427 var buttons: [2]ConfirmationButton = undefined; 1428 buttons[0] = .{ .label = switch (confirmation_kind) { 1429 .remove => try std.fmt.bufPrint(&label_buf, "remove {s}", .{event_name}), 1430 .publish => "publish patch", 1431 .merge => "merge patch", 1432 } }; 1433 var button_count: usize = 1; 1434 if (confirmation_kind == .merge) { 1435 if (comptime supports_merge) { 1436 buttons[1] = .{ 1437 .label = "squash and merge patch", 1438 .action = try std.fmt.allocPrint(aa, "{s}{s}/squash", .{ ui.submit_action_prefix, page_url }), 1439 }; 1440 button_count += 1; 1441 } else unreachable; 1442 } 1443 var center = try initConfirmationForm(allocator, action, buttons[0..button_count]); 1444 errdefer center.deinit(allocator); 1445 try stack.children.put(allocator, center.getFocus().id, .{ .center = center }); 1446 } else if (data.view == .new_comment or data.view == .edit_comment) { 1447 const editing = data.view == .edit_comment; 1448 const route = if (editing) 1449 ui.RoutablePage.repoThreadCommentEditRoute(kind, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong 1450 else 1451 ui.RoutablePage.repoThreadCommentNewRoute(kind, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong; 1452 const page_url = try route.toUrl(session.page_arena); 1453 const action = try std.fmt.allocPrint(session.page_arena.allocator(), "form:{s}", .{page_url}); 1454 const page = data.comment_page; 1455 const top_level = if (page) |p| std.mem.eql(u8, &p.selected.comment.event.parent_id, &p.selected.comment.event.thread_id) else false; 1456 const parent_route = if (editing) 1457 if (top_level) 1458 ui.RoutablePage.repoThreadCommentsRoute(kind, data.identity, data.selected_id, 0) 1459 else if (page) |p| 1460 ui.RoutablePage.repoThreadCommentRoute(kind, data.identity, data.selected_id, &p.selected.comment.event.parent_id, 0) 1461 else 1462 null 1463 else if (page) |p| 1464 ui.RoutablePage.repoThreadCommentRoute(kind, data.identity, data.selected_id, &p.selected.id, 0) 1465 else 1466 ui.RoutablePage.repoThreadCommentsRoute(kind, data.identity, data.selected_id, 0); 1467 const parent_author: ui.Author = if (editing) 1468 if (top_level) 1469 if (data.selectedThread()) |entry| entry.author else .unknown 1470 else if (page) |p| 1471 p.selected.parent_author orelse .unknown 1472 else 1473 .unknown 1474 else if (page) |p| 1475 p.selected.author 1476 else if (data.selectedThread()) |entry| 1477 entry.author 1478 else 1479 .unknown; 1480 const initial_body: ?[]const u8 = if (editing) if (page) |p| p.selected.comment.event.body else null else null; 1481 var form = try initCommentForm(allocator, session, action, parent_author, parent_route orelse return error.RouteTooLong, initial_body); 1482 errdefer form.deinit(allocator); 1483 try stack.children.put(allocator, form.getFocus().id, .{ .box = form }); 1484 } else if (supports_conflicts and data.view == .resolve) { 1485 // on the web the page grows to the form's height and the 1486 // browser scrolls it; the terminal scrolls the form itself 1487 var form_widget: Widget = blk: { 1488 var form = try initResolveForm(allocator, session, data); 1489 errdefer form.deinit(allocator); 1490 break :blk if (session.is_terminal) 1491 .{ .scroll = try wgt.Scroll(Widget).init(allocator, .{ .box = form }, .{ .direction = .vert }) } 1492 else 1493 .{ .box = form }; 1494 }; 1495 errdefer form_widget.deinit(allocator); 1496 try stack.children.put(allocator, form_widget.getFocus().id, form_widget); 1497 } else { 1498 const aa = session.page_arena.allocator(); 1499 const route = if (data.view == .edit) 1500 ui.RoutablePage.repoThreadEditRoute(kind, data.identity, data.selected_id) orelse return error.RouteTooLong 1501 else 1502 ui.RoutablePage.repoThreadNewRoute(kind, data.identity) orelse return error.RouteTooLong; 1503 const action = try std.fmt.allocPrint(aa, "form:{s}", .{try route.toUrl(session.page_arena)}); 1504 const record = if (data.view == .edit) 1505 (if (data.selectedThread()) |entry| &entry.record else null) 1506 else 1507 null; 1508 var form = try initThreadForm(allocator, session, data, action, record); 1509 errdefer form.deinit(allocator); 1510 try stack.children.put(allocator, form.getFocus().id, .{ .box = form }); 1511 } 1512 } else { 1513 var message = try ui.Unauthorized.View.init(allocator); 1514 errdefer message.deinit(allocator); 1515 try stack.children.put(allocator, message.getFocus().id, .{ .unauthorized = message }); 1516 } 1517 1518 if (supports_drafts) { 1519 var split = try initSplit(allocator, session, data, null, true); 1520 errdefer split.deinit(allocator); 1521 try stack.children.put(allocator, split.getFocus().id, .{ .box = split }); 1522 } 1523 1524 // the conflicts split; its tab exists only when the repo has 1525 // conflicts, so skip the child too to keep the stack 1:1 with the 1526 // tabs. 1527 if (supports_conflicts and data.conflicts.count > 0) { 1528 var split = try initSplit(allocator, session, data, null, false); 1529 errdefer split.deinit(allocator); 1530 try stack.children.put(allocator, split.getFocus().id, .{ .box = split }); 1531 } 1532 1533 // the stack starts on the page's view. 1534 stack.getFocus().child_id = stack.children.keys()[viewIndex(data.view)]; 1535 1536 // focus entering the view lands on the tabs first. 1537 outer.getFocus().child_id = outer.children.keys()[header_index]; 1538 1539 return .{ 1540 .box = outer, 1541 .data = data, 1542 .session = session, 1543 }; 1544 } 1545 1546 // the master-detail split showing `status`'s window, or the conflicts 1547 // window when null. 1548 fn initSplit(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self, status_maybe: ?Status, drafts: bool) !wgt.Box(Widget) { 1549 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 1550 errdefer box.deinit(allocator); 1551 1552 const win = if (status_maybe) |status| data.window(status) else if (drafts and supports_drafts) &data.drafts else if (supports_conflicts) &data.conflicts else unreachable; 1553 1554 // the thread list (one focusable row per title), plus a "next" link that 1555 // reloads the page rooted at the following thread. 1556 { 1557 var list_scroll = blk: { 1558 var list_box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .vert, .stretch = true }); 1559 errdefer list_box.deinit(allocator); 1560 if (win.prev_id) |prev| 1561 try addRow(allocator, &list_box, "← previous", "", try windowLink(session.page_arena, data, status_maybe, drafts, prev)); 1562 for (win.items) |entry| { 1563 // conflicts take precedence over the comment count 1564 const label: []const u8 = if (entryConflicted(entry) and status_maybe != null) 1565 " conflict " 1566 else if (entry.comments.count > 0) 1567 try std.fmt.allocPrint(session.page_arena.allocator(), " {d} comments ", .{entry.comments.count}) 1568 else 1569 ""; 1570 try addRow(allocator, &list_box, entry.record.event.title, label, try rowLink(session.page_arena, data, entry.id)); 1571 } 1572 if (win.next_id) |next| 1573 try addRow(allocator, &list_box, "next →", "", try windowLink(session.page_arena, data, status_maybe, drafts, next)); 1574 // select the window's first thread (past a leading "previous" 1575 // row) so its description shows on load. 1576 if (win.items.len > 0) 1577 list_box.getFocus().child_id = list_box.children.keys()[if (win.prev_id != null) 1 else 0] 1578 else if (list_box.children.count() > 0) 1579 list_box.getFocus().child_id = list_box.children.keys()[0]; 1580 break :blk try wgt.Scroll(Widget).init(allocator, .{ .box = list_box }, .{ .direction = .vert, .web_native = !session.is_terminal }); 1581 }; 1582 errdefer list_scroll.deinit(allocator); 1583 try box.children.put(allocator, list_scroll.getFocus().id, .{ .widget = .{ .scroll = list_scroll }, .rect = null, .min_size = .{ .width = list_max_width, .height = null }, .max_size = .{ .width = list_max_width, .height = null } }); 1584 } 1585 1586 // frame the detail pane inside the master-detail split 1587 { 1588 var frame = try wgt.Box(Widget).init(allocator, .{ .border_style = .hidden, .direction = .vert }); 1589 errdefer frame.deinit(allocator); 1590 frame.getFocus().mode = .mouse; 1591 var detail_view = try DetailType.init(allocator, data, session, null, .{}); 1592 errdefer detail_view.deinit(allocator); 1593 frame.getFocus().child_id = detail_view.getFocus().id; 1594 try frame.children.put(allocator, detail_view.getFocus().id, .{ .widget = @unionInit(Widget, Data.detail_widget_name, detail_view), .rect = null, .min_size = null }); 1595 try box.children.put(allocator, frame.getFocus().id, .{ .widget = .{ .box = frame }, .rect = null, .min_size = .{ .width = detail_min_width, .height = null } }); 1596 } 1597 1598 box.getFocus().child_id = box.children.keys()[list_index]; 1599 return box; 1600 } 1601 1602 // a thread form, plus the target branch for patches, prefilled from 1603 // `record` when given. its form: subtree makes the web renderer POST 1604 // the inputs to `action`'s route. 1605 fn initThreadForm(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self, action: []const u8, record: ?*const Event.Record) !wgt.Box(Widget) { 1606 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 1607 errdefer box.deinit(allocator); 1608 box.getFocus().kind = .{ .custom = action }; 1609 1610 const saved_form = session.formFeedback(feedback_tag); 1611 const saved_fields = if (saved_form) |saved| saved.fields else null; 1612 1613 { 1614 var title = try wgt.TextInput.init(allocator, .{ .label = " title ", .name = "title", .visible_width = null, .rounded_corners = true, .render_content = session.is_terminal }); 1615 errdefer title.deinit(allocator); 1616 title.getFocus().mode = .all; 1617 if (saved_fields) |saved| 1618 try title.setContent(allocator, saved.title) 1619 else if (record) |r| 1620 try title.setContent(allocator, r.event.title); 1621 try box.children.put(allocator, title.getFocus().id, .{ .widget = .{ .text_input = title }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 1622 } 1623 1624 { 1625 var tags = try wgt.TextInput.init(allocator, .{ .label = " tags (separate with spaces) ", .name = "tags", .visible_width = null, .rounded_corners = true, .render_content = session.is_terminal }); 1626 errdefer tags.deinit(allocator); 1627 tags.getFocus().mode = .all; 1628 if (saved_fields) |saved| 1629 try tags.setContent(allocator, saved.tags) 1630 else if (record) |r| 1631 try tags.setContent(allocator, r.event.tags); 1632 try box.children.put(allocator, tags.getFocus().id, .{ .widget = .{ .text_input = tags }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 1633 } 1634 1635 { 1636 var description = try wgt.TextInput.init(allocator, .{ .label = " description ", .name = "description", .visible_width = null, .rounded_corners = true, .render_content = session.is_terminal, .multiline = true, .scroll = .{ .fill = true } }); 1637 errdefer description.deinit(allocator); 1638 description.getFocus().mode = .all; 1639 if (saved_fields) |saved| 1640 try description.setContent(allocator, saved.description) 1641 else if (record) |r| 1642 try description.setContent(allocator, r.event.description); 1643 try box.children.put(allocator, description.getFocus().id, .{ .widget = .{ .text_input = description }, .rect = null, .min_size = null }); 1644 } 1645 1646 if (supports_drafts) { 1647 var target = try wgt.TextInput.init(allocator, .{ .label = " target branch ", .name = "target_branch", .visible_width = null, .rounded_corners = true, .render_content = session.is_terminal }); 1648 errdefer target.deinit(allocator); 1649 target.getFocus().mode = .all; 1650 const content = if (saved_fields) |saved| saved.target_branch else if (record) |r| r.event.target_branch else data.default_target_branch; 1651 try target.setContent(allocator, content); 1652 try box.children.put(allocator, target.getFocus().id, .{ .widget = .{ .text_input = target }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 1653 } 1654 1655 try addSubmitButtonLabeled(allocator, &box, if (supports_drafts and record == null) "submit draft" else "submit"); 1656 1657 box.getFocus().child_id = box.children.keys()[title_field_index]; 1658 return box; 1659 } 1660 1661 fn initCommentForm(allocator: std.mem.Allocator, session: *ui.Session, action: []const u8, author: ui.Author, parent_route: ui.RoutablePage, initial_body: ?[]const u8) !wgt.Box(Widget) { 1662 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 1663 errdefer box.deinit(allocator); 1664 box.getFocus().kind = .{ .custom = action }; 1665 1666 { 1667 var parent = try ui.authorBox(allocator, session.page_arena, author); 1668 errdefer parent.deinit(allocator); 1669 parent.options.label = " replying to "; 1670 parent.getFocus().kind = .{ .custom = try std.fmt.allocPrint(session.page_arena.allocator(), "a:{s}", .{try parent_route.toUrl(session.page_arena)}) }; 1671 try box.children.put(allocator, parent.getFocus().id, .{ .widget = .{ .text_box = parent }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 1672 } 1673 1674 { 1675 var body = try wgt.TextInput.init(allocator, .{ 1676 .label = " comment ", 1677 .name = "body", 1678 .visible_width = null, 1679 .rounded_corners = true, 1680 .render_content = session.is_terminal, 1681 .multiline = true, 1682 .visible_height = 5, 1683 .scroll = .{ .fill = true }, 1684 }); 1685 errdefer body.deinit(allocator); 1686 body.getFocus().mode = .all; 1687 if (initial_body) |text| try body.setContent(allocator, text); 1688 try box.children.put(allocator, body.getFocus().id, .{ .widget = .{ .text_input = body }, .rect = null, .min_size = null }); 1689 } 1690 1691 try addSubmitButtonLabeled(allocator, &box, "submit"); 1692 box.getFocus().child_id = box.children.keys()[comment_author_field_index]; 1693 return box; 1694 } 1695 1696 const ConfirmationButton = struct { 1697 label: []const u8, 1698 action: []const u8 = "submit", 1699 }; 1700 1701 fn initConfirmationForm(allocator: std.mem.Allocator, action: []const u8, buttons: []const ConfirmationButton) !Center { 1702 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .rounded_corners = true, .direction = .vert }); 1703 errdefer box.deinit(allocator); 1704 box.getFocus().kind = .{ .custom = action }; 1705 1706 var prompt = try wgt.Text.init(allocator, "are you sure?"); 1707 errdefer prompt.deinit(allocator); 1708 try box.children.put(allocator, prompt.getFocus().id, .{ .widget = .{ .text = prompt }, .rect = null, .min_size = null }); 1709 1710 for (buttons, 0..) |options, index| { 1711 var button = try wgt.TextBox.init(allocator, options.label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1712 errdefer button.deinit(allocator); 1713 button.getFocus().mode = .all; 1714 button.getFocus().kind = .{ .custom = options.action }; 1715 try box.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = null }); 1716 if (index == 0) box.getFocus().child_id = button.getFocus().id; 1717 } 1718 1719 return Center.init(allocator, .{ .box = box }); 1720 } 1721 1722 // a form's submit button, then a spacer absorbing the leftover 1723 // min-height the box hands its last child, so the button keeps its 1724 // natural height 1725 fn addSubmitButtonLabeled(allocator: std.mem.Allocator, box: *wgt.Box(Widget), label: []const u8) !void { 1726 { 1727 var submit = try SubmitButton.initLabeled(allocator, label); 1728 errdefer submit.deinit(allocator); 1729 try box.children.put(allocator, submit.getFocus().id, .{ .widget = .{ .submit_button = submit }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 1730 } 1731 { 1732 var spacer = try Spacer.init(allocator); 1733 errdefer spacer.deinit(allocator); 1734 try box.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 1735 } 1736 } 1737 1738 // the conflict resolve form. the "use this" links are navigations that 1739 // flip the url's theirs: pick, reloading the prefills; one submit 1740 // settles every conflict. the form: subtree makes the web renderer POST 1741 // to the resolve route. 1742 fn initResolveForm(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self) !wgt.Box(Widget) { 1743 const aa = session.page_arena.allocator(); 1744 // init only picks the resolve view once it has read the conflict 1745 const conflict = if (data.conflict) |*c| c else unreachable; 1746 1747 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 1748 errdefer box.deinit(allocator); 1749 const route = resolveRoute(data.identity, data.selected_id, data.theirs_picks) orelse return error.RouteTooLong; 1750 box.getFocus().kind = .{ .custom = try std.fmt.allocPrint(aa, "form:{s}", .{try route.toUrl(session.page_arena)}) }; 1751 1752 if (conflict.title) |*fc| { 1753 try addLabel(allocator, &box, "title conflict:"); 1754 try addFieldConflict(allocator, &box, session, data, "title", fc); 1755 } 1756 if (conflict.tags) |*fc| { 1757 try addLabel(allocator, &box, "tags conflict:"); 1758 try addFieldConflict(allocator, &box, session, data, "tags", fc); 1759 } 1760 if (comptime @hasField(Data.Conflict, "status")) { 1761 if (conflict.status) |*fc| { 1762 try addLabel(allocator, &box, "status conflict:"); 1763 try addAtomicConflict(allocator, &box, session, data, "status", fc); 1764 } 1765 } 1766 if (comptime @hasField(Data.Conflict, "revision")) { 1767 if (conflict.revision) |*fc| { 1768 try addLabel(allocator, &box, "revision conflict:"); 1769 try addAtomicConflict(allocator, &box, session, data, "revision", fc); 1770 } 1771 if (conflict.target_branch) |*fc| { 1772 try addLabel(allocator, &box, "target branch conflict:"); 1773 try addAtomicConflict(allocator, &box, session, data, "target_branch", fc); 1774 } 1775 } 1776 1777 if (conflict.description) |*desc| { 1778 try addLabel(allocator, &box, "description conflict:"); 1779 var hunk_index: usize = 0; 1780 for (desc.chunks, 0..) |*chunk, chunk_index| { 1781 switch (chunk.*) { 1782 .same => |text| { 1783 var tb = try wgt.TextBox.init(allocator, text, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .word }); 1784 errdefer tb.deinit(allocator); 1785 tb.getFocus().mode = .all; 1786 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1787 }, 1788 // an auto-resolved chunk: only one side changed it, so its 1789 // version stands, shown distinctly with its author 1790 .auto => |auto| { 1791 const author = if (auto.theirs) desc.theirs_author else desc.ours_author; 1792 const verb: []const u8 = if (auto.text == null) "removed" else "edited"; 1793 const label = switch (author) { 1794 .user_name, .email => |name| try std.fmt.allocPrint(aa, " {s} by {s} ", .{ verb, name }), 1795 .unknown => try std.fmt.allocPrint(aa, " {s} by {s} ", .{ verb, if (auto.theirs) @as([]const u8, "them") else "us" }), 1796 }; 1797 var tb = try wgt.TextBox.init(allocator, auto.text orelse "(removed)", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .word, .label = label }); 1798 errdefer tb.deinit(allocator); 1799 tb.getFocus().mode = .all; 1800 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1801 }, 1802 .conflict => |hunk| { 1803 // a blank row on each side sets the group apart from 1804 // the flowing shared and auto-resolved chunks 1805 if (chunk_index > 0) try addGap(allocator, &box); 1806 const name = try std.fmt.allocPrint(aa, "d{d}", .{hunk_index}); 1807 hunk_index += 1; 1808 const picked_theirs = data.theirsPicked(name); 1809 try addVersionRow(allocator, &box, try sideLabel(aa, desc.ours_author, true), hunk.ours orelse "", try useThisLink(session, data, name, false)); 1810 try addVersionRow(allocator, &box, try sideLabel(aa, desc.theirs_author, false), hunk.theirs orelse "", try useThisLink(session, data, name, true)); 1811 var resolution_input = try wgt.TextInput.init(allocator, .{ .label = " resolution ", .name = name, .visible_width = null, .rounded_corners = true, .render_content = session.is_terminal, .multiline = true }); 1812 errdefer resolution_input.deinit(allocator); 1813 resolution_input.getFocus().mode = .all; 1814 try resolution_input.setContent(allocator, (if (picked_theirs) hunk.theirs else hunk.ours) orelse ""); 1815 try box.children.put(allocator, resolution_input.getFocus().id, .{ .widget = .{ .text_input = resolution_input }, .rect = null, .min_size = null }); 1816 if (chunk_index + 1 < desc.chunks.len) try addGap(allocator, &box); 1817 }, 1818 } 1819 } 1820 } 1821 1822 try addSubmitButtonLabeled(allocator, &box, "submit"); 1823 1824 box.getFocus().child_id = box.children.keys()[0]; 1825 return box; 1826 } 1827 1828 // one conflicted scalar field: both sides' version rows, then the 1829 // resolution input prefilled from the picked side. 1830 fn addFieldConflict(allocator: std.mem.Allocator, box: *wgt.Box(Widget), session: *ui.Session, data: *const Self, comptime name: []const u8, fc: *const FieldConflict) !void { 1831 const aa = session.page_arena.allocator(); 1832 try addVersionRow(allocator, box, try sideLabel(aa, fc.ours.author, true), fc.ours.text, try useThisLink(session, data, name, false)); 1833 try addVersionRow(allocator, box, try sideLabel(aa, fc.theirs.author, false), fc.theirs.text, try useThisLink(session, data, name, true)); 1834 1835 var resolution_input = try wgt.TextInput.init(allocator, .{ .label = " resolution ", .name = name, .visible_width = null, .rounded_corners = true, .render_content = session.is_terminal }); 1836 errdefer resolution_input.deinit(allocator); 1837 resolution_input.getFocus().mode = .all; 1838 try resolution_input.setContent(allocator, if (data.theirsPicked(name)) fc.theirs.text else fc.ours.text); 1839 try box.children.put(allocator, resolution_input.getFocus().id, .{ .widget = .{ .text_input = resolution_input }, .rect = null, .min_size = null }); 1840 } 1841 1842 // atomic fields can only pick one complete side 1843 fn addAtomicConflict(allocator: std.mem.Allocator, box: *wgt.Box(Widget), session: *ui.Session, data: *const Self, comptime name: []const u8, fc: *const FieldConflict) !void { 1844 const aa = session.page_arena.allocator(); 1845 try addVersionRow(allocator, box, try sideLabel(aa, fc.ours.author, true), fc.ours.text, try useThisLink(session, data, name, false)); 1846 try addVersionRow(allocator, box, try sideLabel(aa, fc.theirs.author, false), fc.theirs.text, try useThisLink(session, data, name, true)); 1847 var selected = try wgt.TextBox.init(allocator, if (data.theirsPicked(name)) fc.theirs.text else fc.ours.text, .{ 1848 .border_style = .single, 1849 .rounded_corners = true, 1850 .wrap_kind = .word, 1851 .label = " resolution ", 1852 }); 1853 errdefer selected.deinit(allocator); 1854 selected.getFocus().mode = .all; 1855 try box.children.put(allocator, selected.getFocus().id, .{ .widget = .{ .text_box = selected }, .rect = null, .min_size = null }); 1856 } 1857 1858 // a blank row setting a conflict group apart from its neighbors 1859 fn addGap(allocator: std.mem.Allocator, box: *wgt.Box(Widget)) !void { 1860 var text = try wgt.Text.init(allocator, " "); 1861 errdefer text.deinit(allocator); 1862 try box.children.put(allocator, text.getFocus().id, .{ .widget = .{ .text = text }, .rect = null, .min_size = null }); 1863 } 1864 1865 fn addLabel(allocator: std.mem.Allocator, box: *wgt.Box(Widget), content: []const u8) !void { 1866 var label = try SectionLabel.init(allocator, content); 1867 errdefer label.deinit(allocator); 1868 try box.children.put(allocator, label.getFocus().id, .{ .widget = .{ .section_label = label }, .rect = null, .min_size = null }); 1869 } 1870 1871 // one side of a conflict: the "use this" link on the left, the version 1872 // itself as a labeled read-only box. 1873 fn addVersionRow(allocator: std.mem.Allocator, box: *wgt.Box(Widget), label: []const u8, text: []const u8, link: []const u8) !void { 1874 var row = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 1875 errdefer row.deinit(allocator); 1876 1877 { 1878 const use_label = "use this"; 1879 var use = try wgt.TextBox.init(allocator, use_label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1880 errdefer use.deinit(allocator); 1881 use.getFocus().mode = .all; 1882 use.getFocus().kind = .{ .custom = link }; 1883 try row.children.put(allocator, use.getFocus().id, .{ 1884 .widget = .{ .text_box = use }, 1885 .rect = null, 1886 .min_size = .{ .width = use_label.len + 2, .height = null }, 1887 .max_size = .{ .width = use_label.len + 2, .height = null }, 1888 }); 1889 } 1890 1891 { 1892 var tb = try wgt.TextBox.init(allocator, if (text.len > 0) text else "(removed)", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .word, .label = label }); 1893 errdefer tb.deinit(allocator); 1894 tb.getFocus().mode = .all; 1895 try row.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1896 } 1897 1898 row.getFocus().child_id = row.children.keys()[0]; 1899 try box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .box = row }, .rect = null, .min_size = null }); 1900 } 1901 1902 // the label of one side's version box, naming its author 1903 fn sideLabel(aa: std.mem.Allocator, author: ui.Author, ours: bool) ![]const u8 { 1904 return switch (author) { 1905 .user_name, .email => |name| try std.fmt.allocPrint(aa, " edited by {s} ", .{name}), 1906 .unknown => if (ours) " current version " else " their version ", 1907 }; 1908 } 1909 1910 // the a: link that reloads the resolve page with `name` picked from ours 1911 // or theirs, reprefilling its resolution input 1912 fn useThisLink(session: *ui.Session, data: *const Self, name: []const u8, theirs: bool) ![]const u8 { 1913 const aa = session.page_arena.allocator(); 1914 var picks: std.ArrayList(u8) = .empty; 1915 var it = std.mem.splitScalar(u8, data.theirs_picks, ','); 1916 while (it.next()) |pick| { 1917 if (pick.len == 0 or std.mem.eql(u8, pick, name)) continue; 1918 if (picks.items.len > 0) try picks.append(aa, ','); 1919 try picks.appendSlice(aa, pick); 1920 } 1921 if (theirs) { 1922 if (picks.items.len > 0) try picks.append(aa, ','); 1923 try picks.appendSlice(aa, name); 1924 } 1925 const route = resolveRoute(data.identity, data.selected_id, picks.items) orelse return error.RouteTooLong; 1926 return std.fmt.allocPrint(aa, "a:{s}", .{try route.toUrl(session.page_arena)}); 1927 } 1928 1929 fn addRow(allocator: std.mem.Allocator, box: *wgt.Box(Widget), text: []const u8, bottom_label: []const u8, link: []const u8) !void { 1930 var row = try wgt.TextBox.init(allocator, text, .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .word, .bottom_label = bottom_label }); 1931 errdefer row.deinit(allocator); 1932 row.getFocus().mode = .all; 1933 if (link.len != 0) row.getFocus().kind = .{ .custom = link }; 1934 try box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .text_box = row }, .rect = null, .min_size = null, .max_size = .{ .width = null, .height = 5 } }); 1935 } 1936 1937 pub fn deinit(self: *This, allocator: std.mem.Allocator) void { 1938 self.box.deinit(allocator); 1939 } 1940 1941 fn header(self: *This) *HeaderType { 1942 return &@field(self.box.children.values()[header_index].widget, Data.header_widget_name); 1943 } 1944 1945 fn viewStack(self: *This) *wgt.Stack(Widget) { 1946 return &self.box.children.values()[stack_index].widget.stack; 1947 } 1948 1949 // `index`'s master-detail split inside the stack. 1950 fn resultsBox(self: *This, index: usize) *wgt.Box(Widget) { 1951 return &self.viewStack().children.values()[index].box; 1952 } 1953 1954 // the tags view's flow inside the stack. 1955 fn tagsView(self: *This) *TagFlow { 1956 return &self.viewStack().children.values()[tags_view_index].tag_flow; 1957 } 1958 1959 // the non-confirmation form inside the stack 1960 fn formBox(self: *This) ?*wgt.Box(Widget) { 1961 return switch (self.viewStack().children.values()[form_view_index]) { 1962 .box => |*box| box, 1963 // the resolve form sits inside a scroll on the terminal 1964 .scroll => |*scroll| &scroll.child.box, 1965 else => null, 1966 }; 1967 } 1968 1969 fn threadForm(self: *This) ?*wgt.Box(Widget) { 1970 return switch (self.data.view) { 1971 .new, .edit => self.formBox(), 1972 else => null, 1973 }; 1974 } 1975 1976 fn confirmationForm(self: *This) ?*wgt.Box(Widget) { 1977 return switch (self.viewStack().children.values()[form_view_index]) { 1978 .center => |*center| switch (center.child.*) { 1979 .box => |*box| box, 1980 else => null, 1981 }, 1982 else => null, 1983 }; 1984 } 1985 1986 // the resolve form's scroll on the terminal (the web page scrolls itself) 1987 fn resolveScroll(self: *This) ?*wgt.Scroll(Widget) { 1988 return switch (self.viewStack().children.values()[form_view_index]) { 1989 .scroll => |*scroll| scroll, 1990 else => null, 1991 }; 1992 } 1993 1994 fn listScroll(self: *This, index: usize) *wgt.Scroll(Widget) { 1995 return &self.resultsBox(index).children.values()[list_index].widget.scroll; 1996 } 1997 1998 fn listBox(self: *This, index: usize) *wgt.Box(Widget) { 1999 return &self.listScroll(index).child.box; 2000 } 2001 2002 fn detail(self: *This, index: usize) *DetailType { 2003 const frame = &self.resultsBox(index).children.values()[detail_index].widget.box; 2004 return &@field(frame.children.values()[0].widget, Data.detail_widget_name); 2005 } 2006 2007 fn window(self: *This, index: usize) *const Window { 2008 if (supports_conflicts and index == conflict_view_index) return &self.data.conflicts; 2009 if (supports_drafts and index == drafts_view_index) return &self.data.drafts; 2010 return self.data.window(splitStatus(index)); 2011 } 2012 2013 fn stackSelectedIndex(self: *This) ?usize { 2014 const stack = self.viewStack(); 2015 const cid = stack.getFocus().child_id orelse return null; 2016 return stack.children.getIndex(cid); 2017 } 2018 2019 // the stack's selected master-detail split (null when the tags view or the 2020 // new-thread form shows). 2021 fn selectedSplitIndex(self: *This) ?usize { 2022 const idx = self.stackSelectedIndex() orelse return null; 2023 return if (idx < status_count or (supports_drafts and idx == drafts_view_index) or idx == conflict_view_index) idx else null; 2024 } 2025 2026 fn detailActive(self: *This, index: usize) bool { 2027 const rb = self.resultsBox(index); 2028 const cid = rb.getFocus().child_id orelse return false; 2029 return rb.children.getIndex(cid) == detail_index; 2030 } 2031 2032 fn headerActive(self: *This) bool { 2033 const cid = self.box.getFocus().child_id orelse return false; 2034 return self.box.children.getIndex(cid) == header_index; 2035 } 2036 2037 fn tagsViewActive(self: *This) bool { 2038 if (self.headerActive()) return false; 2039 return self.stackSelectedIndex() == tags_view_index; 2040 } 2041 2042 fn formViewActive(self: *This) bool { 2043 if (self.headerActive()) return false; 2044 return self.stackSelectedIndex() == form_view_index; 2045 } 2046 2047 // the selected thread's index, or null when a window-navigation row is 2048 // selected (a leading "previous" row shifts the thread rows down by one). 2049 fn selectedThreadIndex(self: *This, index: usize) ?usize { 2050 const lb = self.listBox(index); 2051 const cid = lb.getFocus().child_id orelse return null; 2052 const idx = lb.children.getIndex(cid) orelse return null; 2053 const win = self.window(index); 2054 const lead: usize = if (win.prev_id != null) 1 else 0; 2055 if (idx < lead or idx - lead >= win.items.len) return null; 2056 return idx - lead; 2057 } 2058 2059 pub fn build(self: *This, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2060 self.clearGrid(); 2061 2062 // each header tab maps 1:1 to a stack child by position 2063 if (self.header().getSelectedIndex()) |index| { 2064 const stack = self.viewStack(); 2065 stack.getFocus().child_id = stack.children.keys()[index]; 2066 } 2067 2068 if (self.selectedSplitIndex()) |i| { 2069 // swap the detail pane to the selected thread when it changes. 2070 if (self.selectedThreadIndex(i)) |selected| { 2071 const entry = self.window(i).items[selected]; 2072 const changed = if (self.detail(i).entry) |current| !std.mem.eql(u8, current.id, entry.id) else true; 2073 if (changed) { 2074 try self.detail(i).setEntry(allocator, entry); 2075 } 2076 } 2077 2078 // the selected list row shows a border (the focused TextBox 2079 // upgrades it to a double border itself); the rest stay borderless. 2080 const lb = self.listBox(i); 2081 for (lb.children.keys(), lb.children.values()) |id, *child| { 2082 switch (child.widget) { 2083 .text_box => |*tb| tb.options.border_style = if (lb.getFocus().child_id == id) .single else .hidden, 2084 else => {}, 2085 } 2086 } 2087 2088 // cap the list at list_max_width only while the detail pane fits 2089 // beside it. the box drops the detail when the width can't hold 2090 // both minimums, so when it's that narrow we lift the cap and let 2091 // the list fill the whole width. 2092 const both_panes_fit = if (constraint.max_size.width) |w| w >= list_max_width + detail_min_width else true; 2093 self.resultsBox(i).children.values()[list_index].max_size = if (both_panes_fit) .{ .width = list_max_width, .height = null } else null; 2094 2095 // stretch the detail pane across the rest of the width so it fills 2096 // the area rather than shrinking to its content; its scroll fills 2097 // the pane. 2098 if (constraint.max_size.width) |w| { 2099 self.resultsBox(i).children.values()[detail_index].min_size = .{ .width = if (both_panes_fit) w - list_max_width else w, .height = null }; 2100 } else { 2101 self.resultsBox(i).children.values()[detail_index].min_size = .{ .width = detail_min_width, .height = null }; 2102 } 2103 } 2104 2105 // refresh the form inputs' entries in the session's focus-id -> input 2106 // map with this frame's addresses, so the web/wasm form handling can 2107 // find them by focus id 2108 if (self.threadForm()) |form| { 2109 const failure = if (self.session.formFeedback(feedback_tag)) |saved| saved.failure else null; 2110 form.children.values()[title_field_index].widget.text_input.options.label = if (failure == .required_title) " title (required) " else " title "; 2111 if (comptime supports_drafts) { 2112 const invalid_target = failure == .invalid_target_branch; 2113 form.children.values()[target_branch_field_index].widget.text_input.options.label = if (invalid_target) " target branch (not found) " else " target branch "; 2114 } 2115 } 2116 if (self.formBox()) |form| { 2117 const inputs_arena = self.session.arena.allocator(); 2118 for (form.children.values()) |*child| switch (child.widget) { 2119 .text_input => |*ti| try self.session.text_inputs.put(inputs_arena, ti.getFocus().id, ti), 2120 else => {}, 2121 }; 2122 } 2123 2124 try self.box.build(allocator, constraint, root_focus); 2125 } 2126 2127 pub fn input(self: *This, allocator: std.mem.Allocator, raw_key: Key, root_focus: *Focus) !void { 2128 // wheel input moves focus like the arrows 2129 const key: Key = if (raw_key == .mouse and raw_key.mouse.action == .scroll) 2130 (if (raw_key.mouse.action.scroll == .up) .arrow_up else .arrow_down) 2131 else 2132 raw_key; 2133 2134 if (self.headerActive()) { 2135 // down from the tabs re-enters the stack if the selected view has 2136 // something to focus; other keys move the tabs. 2137 if (inp.vertDirection(key) == .down) { 2138 const enterable = if (self.viewStack().getSelected()) |selected| switch (selected.*) { 2139 .tag_flow => |*tf| tf.text_boxes.items.len > 0, 2140 // nothing focusable in the unauthorized view 2141 .unauthorized => false, 2142 else => true, 2143 } else false; 2144 if (enterable) root_focus.setFocus(self.box.children.keys()[stack_index]); 2145 } else { 2146 try self.header().input(allocator, key, root_focus); 2147 } 2148 return; 2149 } 2150 if (self.tagsViewActive()) { 2151 self.tagsViewInput(key, root_focus); 2152 return; 2153 } 2154 if (self.formViewActive()) { 2155 if (confirmation(self.data.view) != null) { 2156 try self.confirmationFormInput(allocator, key, root_focus); 2157 } else if (supports_conflicts and self.data.view == .resolve) { 2158 try self.resolveInput(allocator, key, root_focus); 2159 } else if (self.data.view == .new_comment or self.data.view == .edit_comment) { 2160 try self.commentFormInput(allocator, key, root_focus); 2161 } else { 2162 try self.formInput(allocator, key, root_focus); 2163 } 2164 return; 2165 } 2166 const i = self.selectedSplitIndex() orelse return; 2167 if (self.detailActive(i)) { 2168 switch (try self.detail(i).inputWithExit(allocator, key, root_focus)) { 2169 .none => {}, 2170 .list => self.focusList(i, root_focus), 2171 .header => self.focusHeader(root_focus), 2172 } 2173 } else { 2174 self.listInput(i, key, root_focus); 2175 } 2176 } 2177 2178 // arrow keys move the tag selection; up from the top row crosses to the 2179 // header tabs. 2180 fn tagsViewInput(self: *This, key: Key, root_focus: *Focus) void { 2181 const tf = self.tagsView(); 2182 const cid = tf.focus.child_id orelse return; 2183 const cur = tf.indexOfFocusId(cid) orelse return; 2184 const count = tf.text_boxes.items.len; 2185 switch (key) { 2186 .arrow_left => if (cur > 0) root_focus.setFocus(tf.text_boxes.items[cur - 1].getFocus().id), 2187 .arrow_right => if (cur + 1 < count) root_focus.setFocus(tf.text_boxes.items[cur + 1].getFocus().id), 2188 .arrow_up => if (tf.rowStep(cur, false)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id) else self.focusHeader(root_focus), 2189 .arrow_down => if (tf.rowStep(cur, true)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id), 2190 .home => root_focus.setFocus(tf.text_boxes.items[0].getFocus().id), 2191 .end => root_focus.setFocus(tf.text_boxes.items[count - 1].getFocus().id), 2192 else => {}, 2193 } 2194 } 2195 2196 fn listInput(self: *This, index: usize, key: Key, root_focus: *Focus) void { 2197 // up/down (and the scroll wheel) move the selection a row; page up/down 2198 // jump a fixed amount. right/Enter cross into the detail pane. up from 2199 // the top row crosses into the header tabs. 2200 if (inp.rowDelta(key, @intCast(self.listBox(index).children.count()))) |delta| { 2201 const lb = self.listBox(index); 2202 const at_top = if (lb.getFocus().child_id) |cid| lb.children.getIndex(cid) == 0 else true; 2203 if (delta < 0 and at_top) return self.focusHeader(root_focus); 2204 moveRowFocus(lb, self.listScroll(index), root_focus, delta); 2205 return; 2206 } 2207 switch (key) { 2208 .enter, .arrow_right => _ = self.detail(index).focusFirst(root_focus), 2209 else => {}, 2210 } 2211 } 2212 2213 fn formInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2214 const form = self.threadForm() orelse return; 2215 const cid = form.getFocus().child_id orelse return; 2216 const cur = form.children.getIndex(cid) orelse return; 2217 2218 if (cur == description_field_index) { 2219 const child = &form.children.values()[cur]; 2220 if (try multilineKeepsKey(&child.widget.text_input, allocator, key)) 2221 return child.widget.input(allocator, key, root_focus); 2222 } 2223 2224 switch (key) { 2225 .arrow_up, .back_tab => if (cur > 0) 2226 root_focus.setFocus(form.children.keys()[cur - 1]) 2227 else 2228 self.focusHeader(root_focus), 2229 .arrow_down, .tab => if (cur < submit_field_index) { 2230 root_focus.setFocus(form.children.keys()[cur + 1]); 2231 }, 2232 .enter => if (cur == submit_field_index) try self.submitForm(allocator), 2233 .mouse => |mouse| if (cur == submit_field_index) { 2234 const submit = &form.children.values()[cur].widget.submit_button; 2235 if (inp.leftClickOn(root_focus, submit.buttonId(), mouse)) try self.submitForm(allocator); 2236 }, 2237 else => try form.children.values()[cur].widget.input(allocator, key, root_focus), 2238 } 2239 } 2240 2241 fn commentFormInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2242 const form = self.formBox() orelse return; 2243 const cid = form.getFocus().child_id orelse return; 2244 const cur = form.children.getIndex(cid) orelse return; 2245 const child = &form.children.values()[cur]; 2246 2247 if (cur == comment_body_field_index and try multilineKeepsKey(&child.widget.text_input, allocator, key)) 2248 return child.widget.input(allocator, key, root_focus); 2249 2250 switch (key) { 2251 .arrow_up, .back_tab => if (cur > comment_author_field_index) 2252 root_focus.setFocus(form.children.keys()[cur - 1]) 2253 else 2254 self.focusHeader(root_focus), 2255 .arrow_down, .tab => if (cur < comment_submit_field_index) 2256 root_focus.setFocus(form.children.keys()[cur + 1]), 2257 .enter => if (cur == comment_submit_field_index) try self.submitComment(allocator), 2258 .mouse => |mouse| if (cur == comment_submit_field_index) { 2259 const submit = &child.widget.submit_button; 2260 if (inp.leftClickOn(root_focus, submit.buttonId(), mouse)) try self.submitComment(allocator); 2261 }, 2262 else => try child.widget.input(allocator, key, root_focus), 2263 } 2264 } 2265 2266 fn confirmationFormInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2267 const form = self.confirmationForm() orelse return; 2268 const child_id = form.getFocus().child_id orelse return; 2269 const index = form.children.getIndex(child_id) orelse return; 2270 switch (key) { 2271 .arrow_up, .back_tab => if (index > 1) 2272 root_focus.setFocus(form.children.keys()[index - 1]) 2273 else 2274 self.focusHeader(root_focus), 2275 .arrow_down, .tab => if (index + 1 < form.children.count()) root_focus.setFocus(form.children.keys()[index + 1]), 2276 .enter => try self.submitConfirmation(allocator, index), 2277 .mouse => |mouse| for (form.children.keys()[1..], 1..) |button_id, button_index| { 2278 if (inp.leftClickOn(root_focus, button_id, mouse)) { 2279 try self.submitConfirmation(allocator, button_index); 2280 break; 2281 } 2282 }, 2283 else => {}, 2284 } 2285 } 2286 2287 // the multiline hunk inputs keep enter and any up/down that has a row to 2288 // move to; enter on a "use this" link is a navigation the host follows 2289 // before this runs. 2290 fn resolveInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2291 const form = self.formBox() orelse return; 2292 const cid = form.getFocus().child_id orelse return; 2293 const cur = form.children.getIndex(cid) orelse return; 2294 const child = &form.children.values()[cur]; 2295 2296 if (child.widget == .text_input) { 2297 const ti = &child.widget.text_input; 2298 if (ti.options.multiline and try multilineKeepsKey(ti, allocator, key)) 2299 return child.widget.input(allocator, key, root_focus); 2300 } 2301 2302 const on_submit = child.widget == .submit_button; 2303 2304 switch (key) { 2305 .arrow_up, .back_tab => if (resolveStep(form, cur, false)) |i| self.focusResolveChild(form, i, root_focus) else self.focusHeader(root_focus), 2306 .arrow_down, .tab => if (resolveStep(form, cur, true)) |i| self.focusResolveChild(form, i, root_focus), 2307 .arrow_left, .arrow_right => switch (child.widget) { 2308 .box => |*row| { 2309 const row_cid = row.getFocus().child_id orelse return; 2310 const row_index = row.children.getIndex(row_cid) orelse return; 2311 if (key == .arrow_left) { 2312 if (row_index > 0) root_focus.setFocus(row.children.keys()[row_index - 1]); 2313 } else if (row_index + 1 < row.children.count()) { 2314 root_focus.setFocus(row.children.keys()[row_index + 1]); 2315 } 2316 }, 2317 else => try child.widget.input(allocator, key, root_focus), 2318 }, 2319 .enter => if (on_submit) try self.submitResolution(allocator), 2320 .mouse => |mouse| if (on_submit) { 2321 if (inp.leftClickOn(root_focus, child.widget.submit_button.buttonId(), mouse)) try self.submitResolution(allocator); 2322 }, 2323 else => try child.widget.input(allocator, key, root_focus), 2324 } 2325 } 2326 2327 // a multiline input keeps enter and any up/down that has a row to move to 2328 fn multilineKeepsKey(text_input: *wgt.TextInput, allocator: std.mem.Allocator, key: Key) !bool { 2329 return switch (key) { 2330 .enter => true, 2331 .arrow_up => !try text_input.cursorOnFirstRow(allocator), 2332 .arrow_down => !try text_input.cursorOnLastRow(allocator), 2333 else => false, 2334 }; 2335 } 2336 2337 // the neighboring focusable resolve-form child, skipping the spacer and 2338 // the blank gap rows 2339 fn resolveStep(form: *wgt.Box(Widget), cur: usize, down: bool) ?usize { 2340 var i = cur; 2341 while (true) { 2342 if (down) { 2343 i += 1; 2344 if (i >= form.children.count()) return null; 2345 } else { 2346 if (i == 0) return null; 2347 i -= 1; 2348 } 2349 switch (form.children.values()[i].widget) { 2350 .spacer, .text => continue, 2351 else => return i, 2352 } 2353 } 2354 } 2355 2356 // focus the resolve-form child at `i` (a version row focuses its selected 2357 // child) and keep it visible on the terminal 2358 fn focusResolveChild(self: *This, form: *wgt.Box(Widget), i: usize, root_focus: *Focus) void { 2359 const child = &form.children.values()[i]; 2360 switch (child.widget) { 2361 .box => |*row| root_focus.setFocus(row.getFocus().child_id orelse form.children.keys()[i]), 2362 else => root_focus.setFocus(form.children.keys()[i]), 2363 } 2364 if (self.session.is_terminal) { 2365 if (self.resolveScroll()) |sc| { 2366 if (child.rect) |rect| sc.scrollToRect(rect); 2367 } 2368 } 2369 } 2370 2371 // re-emit the conflicted thread's event with the resolved content; any 2372 // event on the thread settles its conflict entry. this is the terminal 2373 // path; the web posts the form to the resolve route. 2374 fn submitResolution(self: *This, allocator: std.mem.Allocator) !void { 2375 if (comptime wasm) return; 2376 const io = self.session.io orelse return; 2377 const src = self.data.repo_source orelse return; 2378 const entry = self.data.selectedThread() orelse return; 2379 const author = (try self.session.eventAuthor()) orelse return; 2380 const form = self.formBox() orelse return; 2381 2382 // gather the inputs by name; the d<n> hunk inputs appear in chunk order 2383 var title: ?[]u8 = null; 2384 var tags: ?[]u8 = null; 2385 var hunks: std.ArrayList([]const u8) = .empty; 2386 defer { 2387 if (title) |t| allocator.free(t); 2388 if (tags) |t| allocator.free(t); 2389 for (hunks.items) |h| allocator.free(h); 2390 hunks.deinit(allocator); 2391 } 2392 for (form.children.values()) |*child| switch (child.widget) { 2393 .text_input => |*ti| { 2394 const text = try ti.text(allocator); 2395 errdefer allocator.free(text); 2396 if (std.mem.eql(u8, ti.options.name, "title")) { 2397 title = text; 2398 } else if (std.mem.eql(u8, ti.options.name, "tags")) { 2399 tags = text; 2400 } else { 2401 try hunks.append(allocator, text); 2402 } 2403 }, 2404 else => {}, 2405 }; 2406 2407 const id_bytes = try evt.parseEventId(entry.id); 2408 2409 switch (src.repo_kind) { 2410 inline else => |repo_kind| { 2411 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2412 defer any_repo.deinit(io, allocator); 2413 switch (any_repo) { 2414 inline else => |*repo| { 2415 var resolution: Event.Resolve = .{ 2416 .title = title, 2417 .tags = tags, 2418 .hunks = hunks.items, 2419 }; 2420 if (comptime @hasField(Event.Resolve, "theirs")) resolution.theirs = self.data.theirs_picks; 2421 const change = Event.Update{ .resolve = resolution }; 2422 Event.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, change, author) catch |err| switch (err) { 2423 // leave the form up for correction 2424 error.InvalidFields => return, 2425 else => |e| return e, 2426 }; 2427 }, 2428 } 2429 }, 2430 } 2431 2432 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2433 try self.session.navigate(route); 2434 } 2435 2436 // commit a new or edited comment and navigate to its permalink. this is the 2437 // terminal path; the web posts the form to its comment route. 2438 fn submitComment(self: *This, allocator: std.mem.Allocator) !void { 2439 if (comptime wasm) return; 2440 const io = self.session.io orelse return; 2441 const src = self.data.repo_source orelse return; 2442 const author = (try self.session.eventAuthor()) orelse return; 2443 const form = self.formBox() orelse return; 2444 const body_input = &form.children.values()[comment_body_field_index].widget.text_input; 2445 const body = try body_input.text(allocator); 2446 defer allocator.free(body); 2447 if (!evt.Comment.fieldsValid(body)) return; 2448 2449 var thread_id: [evt.event_id_size * 2]u8 = undefined; 2450 @memcpy(&thread_id, self.data.selected_id); 2451 const parent_id = if (self.data.comment_id.len == 0) thread_id else blk: { 2452 var parent: [evt.event_id_size * 2]u8 = undefined; 2453 @memcpy(&parent, self.data.comment_id); 2454 break :blk parent; 2455 }; 2456 var event_id_hex: [evt.event_id_size * 2]u8 = undefined; 2457 const editing = self.data.view == .edit_comment; 2458 switch (src.repo_kind) { 2459 inline else => |repo_kind| { 2460 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2461 defer any_repo.deinit(io, allocator); 2462 switch (any_repo) { 2463 inline else => |*repo| if (editing) { 2464 const comment_id = evt.parseEventId(self.data.comment_id) catch return; 2465 try evt.Comment.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &thread_id, &comment_id, body, author); 2466 event_id_hex = std.fmt.bytesToHex(comment_id, .lower); 2467 } else { 2468 event_id_hex = try evt.Comment.create(repo_kind, repo.self_repo_opts, io, allocator, repo, &thread_id, &parent_id, body, author); 2469 }, 2470 } 2471 }, 2472 } 2473 2474 body_input.clear(allocator); 2475 const route = ui.RoutablePage.repoThreadCommentRoute(kind, self.data.identity, &thread_id, &event_id_hex, 0) orelse return; 2476 try self.session.navigate(route); 2477 } 2478 2479 // the terminal submit paths: the new form commits a new thread event, the 2480 // edit form re-emits the selected thread's event with the form's content. 2481 // the web posts the forms to their routes instead. 2482 fn submitForm(self: *This, allocator: std.mem.Allocator) !void { 2483 if (self.data.view == .edit) { 2484 try self.submitEditedThread(allocator); 2485 } else { 2486 try self.submitNewThread(allocator); 2487 } 2488 } 2489 2490 // commit the new thread to the repo's events branch and navigate to it. 2491 // this is the terminal path; the web posts the form to the thread route, 2492 // so the wasm side never runs (or compiles) the repo access below. 2493 fn submitNewThread(self: *This, allocator: std.mem.Allocator) !void { 2494 if (comptime wasm) return; 2495 const io = self.session.io orelse return; 2496 const src = self.data.repo_source orelse return; 2497 const author = (try self.session.eventAuthor()) orelse return; 2498 2499 const form = self.threadForm() orelse return; 2500 const title_input = &form.children.values()[title_field_index].widget.text_input; 2501 const tags_input = &form.children.values()[tags_field_index].widget.text_input; 2502 const description_input = &form.children.values()[description_field_index].widget.text_input; 2503 2504 const title = try title_input.text(allocator); 2505 defer allocator.free(title); 2506 const tags = try tags_input.text(allocator); 2507 defer allocator.free(tags); 2508 const description = try description_input.text(allocator); 2509 defer allocator.free(description); 2510 const target_branch = if (comptime supports_drafts) try form.children.values()[target_branch_field_index].widget.text_input.text(allocator) else ""; 2511 defer if (comptime supports_drafts) allocator.free(target_branch); 2512 2513 if (!Event.fieldsValid(title, tags)) { 2514 if (!evt.titleValid(title)) try self.rememberThreadFeedback(.required_title, title, tags, description, target_branch); 2515 return; 2516 } 2517 2518 if (comptime supports_drafts) { 2519 const event_id_hex = Data.createDraft(self.data, self.session, allocator, title, tags, description, target_branch) catch |err| switch (err) { 2520 error.InvalidFields => { 2521 try self.rememberThreadFeedback(.invalid_target_branch, title, tags, description, target_branch); 2522 return; 2523 }, 2524 else => |other| return other, 2525 }; 2526 title_input.clear(allocator); 2527 tags_input.clear(allocator); 2528 description_input.clear(allocator); 2529 form.children.values()[target_branch_field_index].widget.text_input.clear(allocator); 2530 const route = ui.RoutablePage.repoThreadCommentsRoute(kind, self.data.identity, &event_id_hex, 0) orelse return; 2531 try self.session.navigate(route); 2532 return; 2533 } 2534 2535 var id_bytes: [evt.event_id_size]u8 = undefined; 2536 io.random(&id_bytes); 2537 const event_id_hex = std.fmt.bytesToHex(id_bytes, .lower); 2538 2539 const event = evt.EventWithId{ 2540 .id = event_id_hex, 2541 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 2542 .author = author, 2543 .event = @unionInit(evt.Event, @tagName(kind), Event{ 2544 .title = title, 2545 .description = description, 2546 .tags = tags, 2547 }), 2548 }; 2549 2550 switch (src.repo_kind) { 2551 inline else => |repo_kind| { 2552 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2553 defer any_repo.deinit(io, allocator); 2554 switch (any_repo) { 2555 inline else => |*repo| try evt.consume(.repo, repo_kind, repo.self_repo_opts, io, allocator, repo, evt.events_ref, &.{event}), 2556 } 2557 }, 2558 } 2559 2560 // wipe the form so a return visit starts fresh 2561 title_input.clear(allocator); 2562 tags_input.clear(allocator); 2563 description_input.clear(allocator); 2564 2565 const route = listRoute(self.data.identity, @enumFromInt(0), "", &event_id_hex) orelse return; 2566 try self.session.navigate(route); 2567 } 2568 2569 // re-emit the selected thread's event with the form's content (status 2570 // preserved), then reload the thread's page. this is the terminal path; 2571 // the web posts the form to the edit route. 2572 fn submitEditedThread(self: *This, allocator: std.mem.Allocator) !void { 2573 if (comptime wasm) return; 2574 const io = self.session.io orelse return; 2575 const entry = self.data.selectedThread() orelse return; 2576 2577 const form = self.threadForm() orelse return; 2578 const title_input = &form.children.values()[title_field_index].widget.text_input; 2579 const tags_input = &form.children.values()[tags_field_index].widget.text_input; 2580 const description_input = &form.children.values()[description_field_index].widget.text_input; 2581 2582 const title = try title_input.text(allocator); 2583 defer allocator.free(title); 2584 const tags = try tags_input.text(allocator); 2585 defer allocator.free(tags); 2586 const description = try description_input.text(allocator); 2587 defer allocator.free(description); 2588 const target_branch = if (comptime supports_drafts) try form.children.values()[target_branch_field_index].widget.text_input.text(allocator) else ""; 2589 defer if (comptime supports_drafts) allocator.free(target_branch); 2590 2591 if (!Event.fieldsValid(title, tags)) { 2592 if (!evt.titleValid(title)) try self.rememberThreadFeedback(.required_title, title, tags, description, target_branch); 2593 return; 2594 } 2595 const src = self.data.repo_source orelse return; 2596 2597 if (comptime supports_drafts) { 2598 if (entryDraft(entry.*)) { 2599 self.data.editDraft(self.session, allocator, entry.id, title, tags, description, target_branch) catch |err| switch (err) { 2600 error.InvalidFields => { 2601 try self.rememberThreadFeedback(.invalid_target_branch, title, tags, description, target_branch); 2602 return; 2603 }, 2604 else => |other| return other, 2605 }; 2606 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2607 try self.session.navigate(route); 2608 return; 2609 } 2610 } 2611 2612 const id_bytes = try evt.parseEventId(entry.id); 2613 const author = (try self.session.eventAuthor()) orelse return; 2614 2615 switch (src.repo_kind) { 2616 inline else => |repo_kind| { 2617 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2618 defer any_repo.deinit(io, allocator); 2619 switch (any_repo) { 2620 inline else => |*repo| if (supports_drafts) { 2621 Event.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 2622 .title = title, 2623 .tags = tags, 2624 .description = description, 2625 .target_branch = target_branch, 2626 } }, author) catch |err| switch (err) { 2627 error.InvalidFields => { 2628 try self.rememberThreadFeedback(.invalid_target_branch, title, tags, description, target_branch); 2629 return; 2630 }, 2631 else => |other| return other, 2632 }; 2633 } else if (has_status) 2634 try Event.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 2635 .title = title, 2636 .tags = tags, 2637 .description = description, 2638 } }, author) 2639 else 2640 try Event.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, title, tags, description, author), 2641 } 2642 }, 2643 } 2644 2645 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2646 try self.session.navigate(route); 2647 } 2648 2649 fn rememberThreadFeedback(self: *This, failure: FeedbackFailure, title: []const u8, tags: []const u8, description: []const u8, target_branch: []const u8) !void { 2650 const aa = self.session.arena.allocator(); 2651 self.session.data.form_feedback = @unionInit(ui.Session.FormFeedback, @tagName(feedback_tag), .{ 2652 .failure = failure, 2653 .fields = if (comptime supports_drafts) .{ 2654 .title = try aa.dupe(u8, title), 2655 .tags = try aa.dupe(u8, tags), 2656 .description = try aa.dupe(u8, description), 2657 .target_branch = try aa.dupe(u8, target_branch), 2658 } else .{ 2659 .title = try aa.dupe(u8, title), 2660 .tags = try aa.dupe(u8, tags), 2661 .description = try aa.dupe(u8, description), 2662 }, 2663 }); 2664 } 2665 2666 fn submitConfirmation(self: *This, allocator: std.mem.Allocator, button_index: usize) !void { 2667 switch (confirmation(self.data.view) orelse return) { 2668 .publish => if (comptime supports_drafts) { 2669 if (comptime wasm) return; 2670 const entry = self.data.selectedThread() orelse return; 2671 if (!entryDraft(entry.*)) return; 2672 try self.data.publishDraft(self.session, allocator, entry.id); 2673 const route = listRoute(self.data.identity, @enumFromInt(0), "", entry.id) orelse return; 2674 try self.session.navigate(route); 2675 } else unreachable, 2676 .merge => if (comptime supports_merge) { 2677 if (comptime wasm) return; 2678 const entry = self.data.selectedThread() orelse return; 2679 try self.data.mergePatch(self.session, allocator, entry.id, button_index > 1); 2680 const merged = std.meta.stringToEnum(Status, "merged") orelse return; 2681 const route = listRoute(self.data.identity, merged, "", entry.id) orelse return; 2682 try self.session.navigate(route); 2683 } else unreachable, 2684 .remove => try self.submitRemove(allocator), 2685 } 2686 } 2687 2688 fn submitRemove(self: *This, allocator: std.mem.Allocator) !void { 2689 const comment = self.data.comment_id.len != 0; 2690 const event_id = if (comment) self.data.comment_id else self.data.selected_id; 2691 const id = evt.parseEventId(event_id) catch return; 2692 try self.removeEvent(allocator, if (comment) .comment else kind, id); 2693 } 2694 2695 fn removeEvent(self: *This, allocator: std.mem.Allocator, event_kind: evt.EventKind, id: [evt.event_id_size]u8) !void { 2696 if (comptime wasm) return; 2697 2698 if (comptime supports_drafts) { 2699 const selected = self.data.selectedThread() orelse return; 2700 if (event_kind == kind and entryDraft(selected.*)) { 2701 try Data.removeDraft(self.session, allocator, &id); 2702 const route = draftsRoute(self.data.identity) orelse return; 2703 try self.session.navigate(route); 2704 return; 2705 } 2706 } 2707 2708 const io = self.session.io orelse return; 2709 const src = self.data.repo_source orelse return; 2710 const author = (try self.session.eventAuthor()) orelse return; 2711 2712 switch (src.repo_kind) { 2713 inline else => |repo_kind| { 2714 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2715 defer any_repo.deinit(io, allocator); 2716 switch (any_repo) { 2717 inline else => |*repo| try evt.remove(.repo, repo_kind, repo.self_repo_opts, io, allocator, repo, &id, event_kind, author), 2718 } 2719 }, 2720 } 2721 2722 const id_hex = std.fmt.bytesToHex(id, .lower); 2723 const route = ui.RoutablePage.repoEventsRoute(self.data.identity, .removed, event_kind, &id_hex) orelse return; 2724 try self.session.navigate(route); 2725 } 2726 2727 // return to the list. 2728 fn focusList(self: *This, index: usize, root_focus: *Focus) void { 2729 root_focus.setFocus(self.listScroll(index).getFocus().id); 2730 } 2731 2732 // cross to the header tabs above the stack. 2733 fn focusHeader(self: *This, root_focus: *Focus) void { 2734 root_focus.setFocus(self.box.children.keys()[header_index]); 2735 } 2736 2737 pub fn clearGrid(self: *This) void { 2738 self.box.clearGrid(); 2739 } 2740 2741 pub fn getGrid(self: This) ?Grid { 2742 return self.box.getGrid(); 2743 } 2744 2745 pub fn getFocus(self: *This) *Focus { 2746 return self.box.getFocus(); 2747 } 2748 2749 // for the parent's "scroll up at the top jumps to the header" check: at the 2750 // top only while the header tabs hold focus, so up from the split 2751 // crosses into the tabs first. 2752 pub fn getSelectedIndex(self: *This) ?usize { 2753 return if (self.headerActive()) 0 else 1; 2754 } 2755 }; 2756 } 2757