╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Issues.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1092 const stack = self.viewStack(); 1093 const cid = stack.getFocus().child_id orelse return null; 1094 return stack.children.getIndex(cid); 1095 } 1096 1097 // the stack's selected master-detail split (null when the tags view or the 1098 // new-issue form shows). 1099 fn selectedSplitIndex(self: *View) ?usize { 1100 const idx = self.stackSelectedIndex() orelse return null; 1101 return if (idx < split_count or idx == conflicts_view_index) idx else null; 1102 } 1103 1104 fn detailActive(self: *View, index: usize) bool { 1105 const rb = self.resultsBox(index); 1106 const cid = rb.getFocus().child_id orelse return false; 1107 return rb.children.getIndex(cid) == detail_index; 1108 } 1109 1110 fn headerActive(self: *View) bool { 1111 const cid = self.box.getFocus().child_id orelse return false; 1112 return self.box.children.getIndex(cid) == header_index; 1113 } 1114 1115 fn tagsViewActive(self: *View) bool { 1116 if (self.headerActive()) return false; 1117 return self.stackSelectedIndex() == tags_view_index; 1118 } 1119 1120 fn formViewActive(self: *View) bool { 1121 if (self.headerActive()) return false; 1122 return self.stackSelectedIndex() == form_view_index; 1123 } 1124 1125 // the selected issue's index, or null when a window-navigation row is 1126 // selected (a leading "previous" row shifts the issue rows down by one). 1127 fn selectedIssueIndex(self: *View, index: usize) ?usize { 1128 const lb = self.listBox(index); 1129 const cid = lb.getFocus().child_id orelse return null; 1130 const idx = lb.children.getIndex(cid) orelse return null; 1131 const win = self.window(index); 1132 const lead: usize = if (win.prev_id != null) 1 else 0; 1133 if (idx < lead or idx - lead >= win.issues.len) return null; 1134 return idx - lead; 1135 } 1136 1137 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1138 self.clearGrid(); 1139 1140 // each header tab maps 1:1 to a stack child by position. mirror the 1141 // selection into the url: the view the page's window is rooted at 1142 // keeps its rooted url, the others get their list route (keeping the 1143 // tag filter). 1144 if (self.header().getSelectedIndex()) |index| { 1145 const stack = self.viewStack(); 1146 stack.getFocus().child_id = stack.children.keys()[index]; 1147 self.session.data.current_page = (if (index == form_view_index) 1148 (switch (self.data.view) { 1149 .edit => ui.RoutablePage.repoIssuesEditRoute(self.data.identity, self.data.selected_id), 1150 .resolve => ui.RoutablePage.repoIssuesResolveRoute(self.data.identity, self.data.selected_id, self.data.theirs_picks), 1151 else => ui.RoutablePage.repoIssuesNewRoute(self.data.identity), 1152 }) 1153 else if (index == conflicts_view_index) 1154 ui.RoutablePage.repoIssuesConflictsRoute(self.data.identity, if (self.data.view == .conflicts) self.data.selected_id else "") 1155 else if (index == viewIndex(self.data.view) and self.data.selected_id.len != 0) 1156 (if (self.data.description_page) 1157 ui.RoutablePage.repoIssuesDescriptionRoute(self.data.identity, self.data.selected_id) 1158 else 1159 ui.RoutablePage.repoIssuesRoute(self.data.identity, .open, self.data.tag, self.data.selected_id)) 1160 else if (index == tags_view_index) 1161 ui.RoutablePage.repoIssuesTagsRoute(self.data.identity, self.data.tag) 1162 else 1163 ui.RoutablePage.repoIssuesRoute(self.data.identity, splitStatus(index), self.data.tag, "")) orelse self.session.data.current_page; 1164 } 1165 1166 if (self.selectedSplitIndex()) |i| { 1167 // swap the detail pane to the selected issue when it changes. 1168 try self.refreshDetail(allocator, i); 1169 1170 // mirror the focused issue into the url, but only while focus is 1171 // inside the split. an issue's url is the same whether or not the 1172 // list is filtered, so the mirror drops the tag. 1173 if (root_focus.grandchild_id) |g| { 1174 if (self.resultsBox(i).getFocus().children.contains(g)) { 1175 if (self.selectedIssueIndex(i)) |sel| { 1176 // selecting another issue leaves the description page 1177 // behind, like it leaves the tag filter behind. 1178 const entry = &self.window(i).issues[sel]; 1179 const mirrored = if (self.data.description_page and std.mem.eql(u8, entry.id, self.data.selected_id)) 1180 ui.RoutablePage.repoIssuesDescriptionRoute(self.data.identity, entry.id) 1181 else 1182 ui.RoutablePage.repoIssuesRoute(self.data.identity, entry.issue.event.status, "", entry.id); 1183 if (mirrored) |route| self.session.data.current_page = route; 1184 } 1185 } 1186 } 1187 1188 // the selected list row shows a border (the focused TextBox 1189 // upgrades it to a double border itself); the rest stay borderless. 1190 const lb = self.listBox(i); 1191 for (lb.children.keys(), lb.children.values()) |id, *child| { 1192 switch (child.widget) { 1193 .text_box => |*tb| tb.options.border_style = if (lb.getFocus().child_id == id) .single else .hidden, 1194 else => {}, 1195 } 1196 } 1197 1198 // the pane's selected child shows the selection border: the 1199 // description directly, the tags via the flow's selected item. 1200 // cap the list at list_max_width only while the detail pane fits 1201 // beside it. the box drops the detail when the width can't hold 1202 // both minimums, so when it's that narrow we lift the cap and let 1203 // the list fill the whole width. 1204 const both_panes_fit = if (constraint.max_size.width) |w| w >= list_max_width + detail_min_width else true; 1205 self.resultsBox(i).children.values()[list_index].max_size = if (both_panes_fit) .{ .width = list_max_width, .height = null } else null; 1206 1207 // stretch the detail pane across the rest of the width so it fills 1208 // the area rather than shrinking to its content; its scroll fills 1209 // the pane. 1210 if (constraint.max_size.width) |w| { 1211 self.resultsBox(i).children.values()[detail_index].min_size = .{ .width = if (both_panes_fit) w - list_max_width else w, .height = null }; 1212 } else { 1213 self.resultsBox(i).children.values()[detail_index].min_size = .{ .width = detail_min_width, .height = null }; 1214 } 1215 } 1216 1217 // refresh the form inputs' entries in the session's focus-id -> input 1218 // map with this frame's addresses, so the web/wasm form handling can 1219 // find them by focus id 1220 if (self.issueForm()) |form| { 1221 const inputs_arena = self.session.arena.allocator(); 1222 for (form.children.values()) |*child| switch (child.widget) { 1223 .text_input => |*ti| try self.session.text_inputs.put(inputs_arena, ti.getFocus().id, ti), 1224 else => {}, 1225 }; 1226 1227 // on an edit page, register the issue's content as the inputs' 1228 // page-constant initial values, which the web overlay renders 1229 // into them. 1230 if (self.data.view == .edit) { 1231 if (self.data.selectedIssue()) |entry| { 1232 const fields = form.children.values(); 1233 try self.session.input_values.put(inputs_arena, fields[title_field_index].widget.text_input.getFocus().id, entry.issue.event.title); 1234 try self.session.input_values.put(inputs_arena, fields[tags_field_index].widget.text_input.getFocus().id, entry.issue.event.tags); 1235 try self.session.input_values.put(inputs_arena, fields[description_field_index].widget.text_input.getFocus().id, entry.issue.event.description); 1236 } 1237 } 1238 1239 // the resolve form's inputs prefill from the picked side, which 1240 // is url state and so page-constant like the edit values above 1241 if (self.data.view == .resolve) { 1242 for (form.children.values()) |*child| switch (child.widget) { 1243 .text_input => |*ti| if (self.resolvePrefill(ti.options.name)) |value| { 1244 try self.session.input_values.put(inputs_arena, ti.getFocus().id, value); 1245 }, 1246 else => {}, 1247 }; 1248 } 1249 } 1250 1251 try self.box.build(allocator, constraint, root_focus); 1252 } 1253 1254 fn refreshDetail(self: *View, allocator: std.mem.Allocator, index: usize) !void { 1255 const sel = self.selectedIssueIndex(index) orelse return; 1256 if (self.detailed_index[index]) |d| if (d == sel) return; 1257 try self.populateDetail(allocator, index, sel); 1258 self.detailed_index[index] = sel; 1259 } 1260 1261 fn populateDetail(self: *View, allocator: std.mem.Allocator, index: usize, sel: usize) !void { 1262 const entry = self.window(index).issues[sel]; 1263 const inner = self.detailInner(index); 1264 // the /description page replaces its issue's detail with a back link 1265 // and the whole description; other issues keep their normal detail. 1266 const description_page = self.data.description_page and std.mem.eql(u8, entry.id, self.data.selected_id); 1267 1268 for (inner.children.values()) |*child| child.widget.deinit(allocator); 1269 inner.children.clearAndFree(allocator); 1270 inner.getFocus().child_id = null; 1271 self.author_id[index] = null; 1272 1273 // the tool row: the open/close and edit buttons; the description page 1274 // shows none. 1275 { 1276 const row = self.toolRow(index); 1277 for (row.children.values()) |*child| child.widget.deinit(allocator); 1278 row.children.clearAndFree(allocator); 1279 row.getFocus().child_id = null; 1280 row.getFocus().kind = .container; 1281 } 1282 if (!description_page) { 1283 const row = self.toolRow(index); 1284 const pa = self.session.page_arena.allocator(); 1285 1286 { 1287 var spacer = try ui.Spacer.init(allocator); 1288 errdefer spacer.deinit(allocator); 1289 try row.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 1290 } 1291 1292 // a conflicted issue's only action is resolving it. the button 1293 // stays visible logged out; the resolve page shows the 1294 // unauthorized view then. 1295 if (entry.conflicted) { 1296 const label = "resolve conflict"; 1297 var button = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1298 errdefer button.deinit(allocator); 1299 button.getFocus().focusable = true; 1300 const route = ui.RoutablePage.repoIssuesResolveRoute(self.data.identity, entry.id, "") orelse return error.RouteTooLong; 1301 button.getFocus().kind = .{ .custom = try std.fmt.allocPrint(pa, "a:{s}", .{try route.toUrl(self.session.page_arena)}) }; 1302 try row.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = .{ .width = label.len + 2, .height = null } }); 1303 } else { 1304 // a logged-out session can't flip the status, so it gets no 1305 // open/close button (and no form action on the row) 1306 if (self.session.data.is_local or self.session.data.user_id != null) { 1307 const action: []const u8 = switch (entry.issue.event.status) { 1308 .open => "close", 1309 .closed => "open", 1310 }; 1311 row.getFocus().kind = .{ .custom = if (self.data.identity.len == 0) 1312 try std.fmt.allocPrint(pa, "form:/issues/{s}/{s}", .{ entry.id, action }) 1313 else 1314 try std.fmt.allocPrint(pa, "form:/repo/{s}/issues/{s}/{s}", .{ self.data.identity, entry.id, action }) }; 1315 1316 var button = try wgt.TextBox(ui.Widget).init(allocator, action, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1317 errdefer button.deinit(allocator); 1318 button.getFocus().focusable = true; 1319 // the renderer distinguishes plain clickables from buttons that 1320 // should POST to a server route by this kind. 1321 button.getFocus().kind = .{ .custom = "submit" }; 1322 try row.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = .{ .width = action.len + 2, .height = null } }); 1323 } 1324 1325 // the edit button links to the issue's edit page. 1326 { 1327 var button = try wgt.TextBox(ui.Widget).init(allocator, "edit", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1328 errdefer button.deinit(allocator); 1329 button.getFocus().focusable = true; 1330 const route = ui.RoutablePage.repoIssuesEditRoute(self.data.identity, entry.id) orelse return error.RouteTooLong; 1331 button.getFocus().kind = .{ .custom = try std.fmt.allocPrint(pa, "a:{s}", .{try route.toUrl(self.session.page_arena)}) }; 1332 try row.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = .{ .width = "edit".len + 2, .height = null } }); 1333 } 1334 } 1335 1336 // the resolve button on a conflicted issue, else the open/close 1337 // button when present, the edit button otherwise 1338 row.getFocus().child_id = row.children.keys()[button_in_row_index]; 1339 } 1340 1341 if (description_page) { 1342 // the back link, in the title slot so the pane's input handling 1343 // applies to it unchanged. 1344 var tb = try wgt.TextBox(ui.Widget).init(allocator, "← back to issue", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1345 errdefer tb.deinit(allocator); 1346 tb.getFocus().focusable = true; 1347 tb.getFocus().kind = .{ .custom = try issuesLink(self.session.page_arena, self.data.identity, entry.issue.event.status, "", entry.id) }; 1348 try inner.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1349 } else { 1350 // the issue's title as a focusable word-wrapped text box. 1351 { 1352 var tb = try wgt.TextBox(ui.Widget).init(allocator, entry.issue.event.title, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .word, .label = " title " }); 1353 errdefer tb.deinit(allocator); 1354 tb.getFocus().focusable = true; 1355 try inner.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1356 } 1357 1358 self.author_id[index] = blk: { 1359 var tb = try ui.authorBox(allocator, self.session.page_arena, entry.author); 1360 errdefer tb.deinit(allocator); 1361 try inner.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1362 break :blk tb.getFocus().id; 1363 }; 1364 1365 // the issue's tags, each linking to this status's list filtered to 1366 // that tag. 1367 { 1368 var items: std.ArrayList(ui.TagFlow.Item) = .empty; 1369 defer items.deinit(allocator); 1370 var tag_iter = evt.Issue.tagIterator(entry.issue.event.tags); 1371 while (tag_iter.next()) |tag| { 1372 if (tag.len == 0) continue; 1373 try items.append(allocator, .{ .text = tag, .link = try tagLink(self.session.page_arena, self.data.identity, entry.issue.event.status, tag) }); 1374 } 1375 if (items.items.len > 0) { 1376 var tf = try ui.TagFlow.init(allocator); 1377 errdefer tf.deinit(allocator); 1378 try tf.setItems(allocator, items.items); 1379 try inner.children.put(allocator, tf.getFocus().id, .{ .widget = .{ .tag_flow = tf }, .rect = null, .min_size = null }); 1380 } 1381 } 1382 } 1383 1384 // the description as a focusable word-wrapped text box. past the limit 1385 // it's cut at the last whole line and links to the /description page, 1386 // which shows the whole thing. 1387 self.description_id[index] = blk: { 1388 const whole = entry.issue.event.description; 1389 const cut_short = !description_page and whole.len > max_description_size; 1390 const shown = if (cut_short) 1391 std.mem.trimEnd(u8, whole[0 .. std.mem.lastIndexOfScalar(u8, whole[0..max_description_size], '\n') orelse 0], " \t\r\n") 1392 else if (whole.len == 0) 1393 "(no description)" 1394 else 1395 whole; 1396 var tb = try wgt.TextBox(ui.Widget).init(allocator, shown, .{ 1397 .border_style = .single, 1398 .rounded_corners = true, 1399 .wrap_kind = .word, 1400 .label = " description ", 1401 .bottom_label = if (cut_short) " click or press enter to see more " else "", 1402 }); 1403 errdefer tb.deinit(allocator); 1404 tb.getFocus().focusable = true; 1405 if (cut_short) tb.getFocus().kind = .{ .custom = try descriptionLink(self.session.page_arena, self.data.identity, entry.id) }; 1406 try inner.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1407 break :blk tb.getFocus().id; 1408 }; 1409 1410 // select the title by default 1411 inner.getFocus().child_id = inner.children.keys()[title_child_index]; 1412 1413 // reset the scroll to the top for the newly-shown issue: directly on the 1414 // terminal (the wasm offset), and via a version bump on the web (so the 1415 // renderer's scroll id changes and JS drops the preserved position). 1416 const sc = self.detailScroll(index); 1417 sc.x = 0; 1418 sc.y = 0; 1419 sc.getFocus().version +%= 1; 1420 } 1421 1422 pub fn input(self: *View, allocator: std.mem.Allocator, raw_key: Key, root_focus: *Focus) !void { 1423 // wheel input moves focus like the arrows 1424 const key: Key = if (raw_key == .mouse and raw_key.mouse.action == .scroll) 1425 (if (raw_key.mouse.action.scroll == .up) .arrow_up else .arrow_down) 1426 else 1427 raw_key; 1428 1429 if (self.headerActive()) { 1430 // down from the tabs re-enters the stack if the selected view has 1431 // something to focus; other keys move the tabs. 1432 if (inp.vertDirection(key) == .down) { 1433 const enterable = if (self.viewStack().getSelected()) |selected| switch (selected.*) { 1434 .tag_flow => |*tf| tf.text_boxes.items.len > 0, 1435 // nothing focusable in the unauthorized view 1436 .unauthorized => false, 1437 else => true, 1438 } else false; 1439 if (enterable) root_focus.setFocus(self.box.children.keys()[stack_index]); 1440 } else { 1441 try self.header().input(allocator, key, root_focus); 1442 } 1443 return; 1444 } 1445 if (self.tagsViewActive()) { 1446 self.tagsViewInput(key, root_focus); 1447 return; 1448 } 1449 if (self.formViewActive()) { 1450 if (self.data.view == .resolve) { 1451 try self.resolveInput(allocator, key, root_focus); 1452 } else { 1453 try self.formInput(allocator, key, root_focus); 1454 } 1455 return; 1456 } 1457 const i = self.selectedSplitIndex() orelse return; 1458 if (self.detailActive(i)) { 1459 try self.detailInput(allocator, i, key, root_focus); 1460 } else { 1461 try self.listInput(i, key, root_focus); 1462 } 1463 } 1464 1465 // arrow keys move the tag selection; up from the top row crosses to the 1466 // header tabs. 1467 fn tagsViewInput(self: *View, key: Key, root_focus: *Focus) void { 1468 const tf = self.tagsView(); 1469 const cid = tf.focus.child_id orelse return; 1470 const cur = tf.indexOfFocusId(cid) orelse return; 1471 const count = tf.text_boxes.items.len; 1472 switch (key) { 1473 .arrow_left => if (cur > 0) root_focus.setFocus(tf.text_boxes.items[cur - 1].getFocus().id), 1474 .arrow_right => if (cur + 1 < count) root_focus.setFocus(tf.text_boxes.items[cur + 1].getFocus().id), 1475 .arrow_up => if (tf.rowStep(cur, false)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id) else self.focusHeader(root_focus), 1476 .arrow_down => if (tf.rowStep(cur, true)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id), 1477 .home => root_focus.setFocus(tf.text_boxes.items[0].getFocus().id), 1478 .end => root_focus.setFocus(tf.text_boxes.items[count - 1].getFocus().id), 1479 else => {}, 1480 } 1481 } 1482 1483 fn listInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 1484 // up/down (and the scroll wheel) move the selection a row; page up/down 1485 // jump a fixed amount. right/Enter cross into the detail pane. up from 1486 // the top row crosses into the header tabs. 1487 if (inp.rowDelta(key, @intCast(self.listBox(index).children.count()))) |delta| { 1488 const lb = self.listBox(index); 1489 const at_top = if (lb.getFocus().child_id) |cid| lb.children.getIndex(cid) == 0 else true; 1490 if (delta < 0 and at_top) return self.focusHeader(root_focus); 1491 ui.moveRowFocus(lb, self.listScroll(index), root_focus, delta); 1492 return; 1493 } 1494 switch (key) { 1495 .enter, .arrow_right => try self.focusDetail(index, root_focus), 1496 else => {}, 1497 } 1498 } 1499 1500 fn detailInput(self: *View, allocator: std.mem.Allocator, index: usize, key: Key, root_focus: *Focus) !void { 1501 if (self.toolRowFocused(index)) { 1502 try self.toolRowInput(allocator, index, key, root_focus); 1503 } else if (self.titleFocused(index)) { 1504 try self.titleInput(index, key, root_focus); 1505 } else if (self.authorFocused(index)) { 1506 try self.authorInput(index, key, root_focus); 1507 } else if (self.tagsFocused(index)) { 1508 try self.tagsInput(index, key, root_focus); 1509 } else { 1510 try self.descriptionInput(index, key, root_focus); 1511 } 1512 } 1513 1514 // the tool row: enter or a click on the open/close button flips the 1515 // issue's status; the edit and resolve buttons are a: links the host 1516 // follows. arrows cross between the buttons and to the neighboring 1517 // widgets. 1518 fn toolRowInput(self: *View, allocator: std.mem.Allocator, index: usize, key: Key, root_focus: *Focus) !void { 1519 const row = self.toolRow(index); 1520 const cur = if (row.getFocus().child_id) |cid| row.children.getIndex(cid) orelse return else return; 1521 const on_status = cur == button_in_row_index and self.statusButton(index) != null; 1522 switch (key) { 1523 .arrow_left => if (cur > button_in_row_index) root_focus.setFocus(row.children.keys()[cur - 1]) else try self.focusList(index, root_focus), 1524 .arrow_right => if (cur + 1 < row.children.count()) root_focus.setFocus(row.children.keys()[cur + 1]), 1525 .arrow_up => self.focusHeader(root_focus), 1526 .arrow_down => self.focusTitle(index, root_focus), 1527 .enter => if (on_status) try self.toggleIssueStatus(allocator, index), 1528 .mouse => |mouse| if (self.statusButton(index)) |button| { 1529 if (inp.leftClickOn(root_focus, button.getFocus().id, mouse)) try self.toggleIssueStatus(allocator, index); 1530 }, 1531 else => {}, 1532 } 1533 } 1534 1535 fn titleInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 1536 switch (key) { 1537 .arrow_left => try self.focusList(index, root_focus), 1538 .arrow_up => self.focusToolRow(index, root_focus), 1539 .arrow_down => if (self.authorPresent(index)) self.focusAuthor(index, root_focus) else try self.focusDescription(index, root_focus), 1540 else => {}, 1541 } 1542 } 1543 1544 // the author box's a: link (when it has one) is followed by the host on 1545 // enter / a click; arrows cross to the neighboring widgets. 1546 fn authorInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 1547 switch (key) { 1548 .arrow_left => try self.focusList(index, root_focus), 1549 .arrow_up => self.focusTitle(index, root_focus), 1550 .arrow_down => if (self.tagFlow(index) != null) try self.focusTags(index, root_focus) else try self.focusDescription(index, root_focus), 1551 else => {}, 1552 } 1553 } 1554 1555 fn descriptionInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 1556 const sc = self.detailScroll(index); 1557 switch (key) { 1558 .arrow_left => return self.focusList(index, root_focus), 1559 // once the scroll can't move further, cross into the tags (or the 1560 // open/close button when the issue has none). 1561 .arrow_up => { 1562 const before = sc.y; 1563 sc.y -= 1; 1564 sc.clampToContent(); 1565 if (sc.y == before) { 1566 if (self.tagFlow(index) != null) 1567 try self.focusTags(index, root_focus) 1568 else if (self.authorPresent(index)) 1569 self.focusAuthor(index, root_focus) 1570 else 1571 self.focusTitle(index, root_focus); 1572 } 1573 return; 1574 }, 1575 .arrow_down => sc.y += 1, 1576 .page_up => sc.y -= 10, 1577 .page_down => sc.y += 10, 1578 .home => sc.y = 0, 1579 .end => sc.y = std.math.maxInt(isize), 1580 else => return, 1581 } 1582 sc.clampToContent(); 1583 } 1584 1585 // up/down (and tab/shift+tab) move between a form's fields; up from the 1586 // title crosses into the header tabs. the multiline description keeps 1587 // enter and any up/down that has a row to move to. 1588 fn formInput(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1589 const form = self.issueForm() orelse return; 1590 const cid = form.getFocus().child_id orelse return; 1591 const cur = form.children.getIndex(cid) orelse return; 1592 1593 if (cur == description_field_index) { 1594 const child = &form.children.values()[cur]; 1595 if (try multilineKeepsKey(&child.widget.text_input, allocator, key)) 1596 return child.widget.input(allocator, key, root_focus); 1597 } 1598 1599 switch (key) { 1600 .arrow_up, .back_tab => if (cur > 0) 1601 root_focus.setFocus(form.children.keys()[cur - 1]) 1602 else 1603 self.focusHeader(root_focus), 1604 .arrow_down, .tab => if (cur < submit_field_index) { 1605 root_focus.setFocus(form.children.keys()[cur + 1]); 1606 }, 1607 .enter => if (cur == submit_field_index) try self.submitForm(allocator), 1608 .mouse => |mouse| if (cur == submit_field_index) { 1609 const submit = &form.children.values()[cur].widget.submit_button; 1610 if (inp.leftClickOn(root_focus, submit.buttonId(), mouse)) try self.submitForm(allocator); 1611 }, 1612 else => try form.children.values()[cur].widget.input(allocator, key, root_focus), 1613 } 1614 } 1615 1616 // the multiline hunk inputs keep enter and any up/down that has a row to 1617 // move to; enter on a "use this" link is a navigation the host follows 1618 // before this runs. 1619 fn resolveInput(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1620 const form = self.issueForm() orelse return; 1621 const cid = form.getFocus().child_id orelse return; 1622 const cur = form.children.getIndex(cid) orelse return; 1623 const child = &form.children.values()[cur]; 1624 1625 if (child.widget == .text_input) { 1626 const ti = &child.widget.text_input; 1627 if (ti.options.multiline and try multilineKeepsKey(ti, allocator, key)) 1628 return child.widget.input(allocator, key, root_focus); 1629 } 1630 1631 const on_submit = child.widget == .submit_button; 1632 1633 switch (key) { 1634 .arrow_up, .back_tab => if (resolveStep(form, cur, false)) |i| self.focusResolveChild(form, i, root_focus) else self.focusHeader(root_focus), 1635 .arrow_down, .tab => if (resolveStep(form, cur, true)) |i| self.focusResolveChild(form, i, root_focus), 1636 .arrow_left, .arrow_right => switch (child.widget) { 1637 .box => |*row| { 1638 const row_cid = row.getFocus().child_id orelse return; 1639 const row_index = row.children.getIndex(row_cid) orelse return; 1640 if (key == .arrow_left) { 1641 if (row_index > 0) root_focus.setFocus(row.children.keys()[row_index - 1]); 1642 } else if (row_index + 1 < row.children.count()) { 1643 root_focus.setFocus(row.children.keys()[row_index + 1]); 1644 } 1645 }, 1646 else => try child.widget.input(allocator, key, root_focus), 1647 }, 1648 .enter => if (on_submit) try self.submitResolution(allocator), 1649 .mouse => |mouse| if (on_submit) { 1650 if (inp.leftClickOn(root_focus, child.widget.submit_button.buttonId(), mouse)) try self.submitResolution(allocator); 1651 }, 1652 else => try child.widget.input(allocator, key, root_focus), 1653 } 1654 } 1655 1656 // a multiline input keeps enter and any up/down that has a row to move to 1657 fn multilineKeepsKey(text_input: *wgt.TextInput(ui.Widget), allocator: std.mem.Allocator, key: Key) !bool { 1658 return switch (key) { 1659 .enter => true, 1660 .arrow_up => !try text_input.cursorOnFirstRow(allocator), 1661 .arrow_down => !try text_input.cursorOnLastRow(allocator), 1662 else => false, 1663 }; 1664 } 1665 1666 // the neighboring focusable resolve-form child, skipping the spacer and 1667 // the blank gap rows 1668 fn resolveStep(form: *wgt.Box(ui.Widget), cur: usize, down: bool) ?usize { 1669 var i = cur; 1670 while (true) { 1671 if (down) { 1672 i += 1; 1673 if (i >= form.children.count()) return null; 1674 } else { 1675 if (i == 0) return null; 1676 i -= 1; 1677 } 1678 switch (form.children.values()[i].widget) { 1679 .spacer, .text => continue, 1680 else => return i, 1681 } 1682 } 1683 } 1684 1685 // focus the resolve-form child at `i` (a version row focuses its selected 1686 // child) and keep it visible on the terminal 1687 fn focusResolveChild(self: *View, form: *wgt.Box(ui.Widget), i: usize, root_focus: *Focus) void { 1688 const child = &form.children.values()[i]; 1689 switch (child.widget) { 1690 .box => |*row| root_focus.setFocus(row.getFocus().child_id orelse form.children.keys()[i]), 1691 else => root_focus.setFocus(form.children.keys()[i]), 1692 } 1693 if (self.session.is_terminal) { 1694 if (self.resolveScroll()) |sc| { 1695 if (child.rect) |rect| sc.scrollToRect(rect); 1696 } 1697 } 1698 } 1699 1700 // re-emit the conflicted issue's event with the resolved content; any 1701 // event on the issue settles its conflict entry. this is the terminal 1702 // path; the web posts the form to the resolve route. 1703 fn submitResolution(self: *View, allocator: std.mem.Allocator) !void { 1704 if (comptime wasm) return; 1705 const io = self.session.io orelse return; 1706 const src = self.data.repo_source orelse return; 1707 const entry = self.data.selectedIssue() orelse return; 1708 const author_email = (try self.session.eventAuthorEmail()) orelse return; 1709 const form = self.issueForm() orelse return; 1710 1711 // gather the inputs by name; the d<n> hunk inputs appear in chunk order 1712 var title: ?[]u8 = null; 1713 var tags: ?[]u8 = null; 1714 var hunks: std.ArrayList([]const u8) = .empty; 1715 defer { 1716 if (title) |t| allocator.free(t); 1717 if (tags) |t| allocator.free(t); 1718 for (hunks.items) |h| allocator.free(h); 1719 hunks.deinit(allocator); 1720 } 1721 for (form.children.values()) |*child| switch (child.widget) { 1722 .text_input => |*ti| { 1723 const text = try ti.text(allocator); 1724 errdefer allocator.free(text); 1725 if (std.mem.eql(u8, ti.options.name, "title")) { 1726 title = text; 1727 } else if (std.mem.eql(u8, ti.options.name, "tags")) { 1728 tags = text; 1729 } else { 1730 try hunks.append(allocator, text); 1731 } 1732 }, 1733 else => {}, 1734 }; 1735 1736 var id_bytes: [evt.event_id_size]u8 = undefined; 1737 _ = try std.fmt.hexToBytes(&id_bytes, entry.id[0 .. evt.event_id_size * 2]); 1738 1739 switch (src.repo_kind) { 1740 inline else => |repo_kind| { 1741 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, .{ .path = src.path }); 1742 defer any_repo.deinit(io, allocator); 1743 switch (any_repo) { 1744 inline else => |*repo| evt.Issue.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .resolve = .{ 1745 .title = title, 1746 .tags = tags, 1747 .hunks = hunks.items, 1748 } }, author_email) catch |err| switch (err) { 1749 // leave the form up for correction 1750 error.InvalidFields => return, 1751 else => |e| return e, 1752 }, 1753 } 1754 }, 1755 } 1756 1757 const route = ui.RoutablePage.repoIssuesRoute(self.data.identity, entry.issue.event.status, "", entry.id) orelse return; 1758 try self.session.navigate(route); 1759 } 1760 1761 // the terminal submit paths: the new form commits a new-issue event, the 1762 // edit form re-emits the selected issue's event with the form's content. 1763 // the web posts the forms to their routes instead. 1764 fn submitForm(self: *View, allocator: std.mem.Allocator) !void { 1765 if (self.data.view == .edit) { 1766 try self.submitEditedIssue(allocator); 1767 } else { 1768 try self.submitNewIssue(allocator); 1769 } 1770 } 1771 1772 // commit the new issue to the repo's events branch and navigate to it. 1773 // this is the terminal path; the web posts the form to the issue route, 1774 // so the wasm side never runs (or compiles) the repo access below. 1775 fn submitNewIssue(self: *View, allocator: std.mem.Allocator) !void { 1776 if (comptime wasm) return; 1777 const io = self.session.io orelse return; 1778 const src = self.data.repo_source orelse return; 1779 const author_email = (try self.session.eventAuthorEmail()) orelse return; 1780 1781 const form = self.issueForm() orelse return; 1782 const title_input = &form.children.values()[title_field_index].widget.text_input; 1783 const tags_input = &form.children.values()[tags_field_index].widget.text_input; 1784 const description_input = &form.children.values()[description_field_index].widget.text_input; 1785 1786 const title = try title_input.text(allocator); 1787 defer allocator.free(title); 1788 const tags = try tags_input.text(allocator); 1789 defer allocator.free(tags); 1790 const description = try description_input.text(allocator); 1791 defer allocator.free(description); 1792 1793 if (!evt.Issue.fieldsValid(title, tags)) return; 1794 1795 var id_bytes: [evt.event_id_size]u8 = undefined; 1796 io.random(&id_bytes); 1797 const event_id_hex = std.fmt.bytesToHex(id_bytes, .lower); 1798 1799 const event = evt.EventWithId{ 1800 .id = event_id_hex, 1801 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 1802 .author_email = author_email, 1803 .event = .{ .issue = .{ 1804 .title = title, 1805 .description = description, 1806 .tags = tags, 1807 } }, 1808 }; 1809 1810 switch (src.repo_kind) { 1811 inline else => |repo_kind| { 1812 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, .{ .path = src.path }); 1813 defer any_repo.deinit(io, allocator); 1814 switch (any_repo) { 1815 inline else => |*repo| try evt.consume(repo_kind, repo.self_repo_opts, io, allocator, repo, evt.events_ref, &.{event}), 1816 } 1817 }, 1818 } 1819 1820 // wipe the form so a return visit starts fresh 1821 title_input.clear(allocator); 1822 tags_input.clear(allocator); 1823 description_input.clear(allocator); 1824 1825 const route = ui.RoutablePage.repoIssuesRoute(self.data.identity, .open, "", &event_id_hex) orelse return; 1826 try self.session.navigate(route); 1827 } 1828 1829 // re-emit the selected issue's event with the form's content (status 1830 // preserved), then reload the issue's page. this is the terminal path; 1831 // the web posts the form to the edit route. 1832 fn submitEditedIssue(self: *View, allocator: std.mem.Allocator) !void { 1833 if (comptime wasm) return; 1834 const io = self.session.io orelse return; 1835 const src = self.data.repo_source orelse return; 1836 const entry = self.data.selectedIssue() orelse return; 1837 const author_email = (try self.session.eventAuthorEmail()) orelse return; 1838 1839 const form = self.issueForm() orelse return; 1840 const title_input = &form.children.values()[title_field_index].widget.text_input; 1841 const tags_input = &form.children.values()[tags_field_index].widget.text_input; 1842 const description_input = &form.children.values()[description_field_index].widget.text_input; 1843 1844 const title = try title_input.text(allocator); 1845 defer allocator.free(title); 1846 const tags = try tags_input.text(allocator); 1847 defer allocator.free(tags); 1848 const description = try description_input.text(allocator); 1849 defer allocator.free(description); 1850 1851 if (!evt.Issue.fieldsValid(title, tags)) return; 1852 1853 var id_bytes: [evt.event_id_size]u8 = undefined; 1854 _ = try std.fmt.hexToBytes(&id_bytes, entry.id[0 .. evt.event_id_size * 2]); 1855 1856 switch (src.repo_kind) { 1857 inline else => |repo_kind| { 1858 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, .{ .path = src.path }); 1859 defer any_repo.deinit(io, allocator); 1860 switch (any_repo) { 1861 inline else => |*repo| try evt.Issue.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 1862 .title = title, 1863 .tags = tags, 1864 .description = description, 1865 } }, author_email), 1866 } 1867 }, 1868 } 1869 1870 const route = ui.RoutablePage.repoIssuesRoute(self.data.identity, entry.issue.event.status, "", entry.id) orelse return; 1871 try self.session.navigate(route); 1872 } 1873 1874 // flip the shown issue's status by re-emitting its event, then reload the 1875 // page rooted at the issue so the view reflects the change. this is the 1876 // terminal path; the web posts the button's form to the status route. 1877 fn toggleIssueStatus(self: *View, allocator: std.mem.Allocator, index: usize) !void { 1878 if (comptime wasm) return; 1879 const io = self.session.io orelse return; 1880 const src = self.data.repo_source orelse return; 1881 const sel = self.detailed_index[index] orelse return; 1882 const entry = self.window(index).issues[sel]; 1883 const author_email = (try self.session.eventAuthorEmail()) orelse return; 1884 1885 const status: evt.Issue.Status = switch (entry.issue.event.status) { 1886 .open => .closed, 1887 .closed => .open, 1888 }; 1889 1890 var id_bytes: [evt.event_id_size]u8 = undefined; 1891 _ = try std.fmt.hexToBytes(&id_bytes, entry.id[0 .. evt.event_id_size * 2]); 1892 1893 switch (src.repo_kind) { 1894 inline else => |repo_kind| { 1895 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, .{ .path = src.path }); 1896 defer any_repo.deinit(io, allocator); 1897 switch (any_repo) { 1898 inline else => |*repo| try evt.Issue.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .status = status }, author_email), 1899 } 1900 }, 1901 } 1902 1903 const route = ui.RoutablePage.repoIssuesRoute(self.data.identity, status, "", entry.id) orelse return; 1904 try self.session.navigate(route); 1905 } 1906 1907 // arrow keys move the tag selection; at the flow's edges focus crosses to 1908 // the neighboring widgets. 1909 fn tagsInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 1910 const tf = self.tagFlow(index) orelse return; 1911 const cid = tf.focus.child_id orelse return; 1912 const cur = tf.indexOfFocusId(cid) orelse return; 1913 const count = tf.text_boxes.items.len; 1914 switch (key) { 1915 .arrow_left => if (cur > 0) self.focusTag(index, tf, root_focus, cur - 1) else try self.focusList(index, root_focus), 1916 .arrow_right => if (cur + 1 < count) self.focusTag(index, tf, root_focus, cur + 1), 1917 .arrow_up => if (tf.rowStep(cur, false)) |i| self.focusTag(index, tf, root_focus, i) else self.focusAuthor(index, root_focus), 1918 .arrow_down => if (tf.rowStep(cur, true)) |i| self.focusTag(index, tf, root_focus, i) else try self.focusDescription(index, root_focus), 1919 .home => self.focusTag(index, tf, root_focus, 0), 1920 .end => self.focusTag(index, tf, root_focus, count - 1), 1921 else => {}, 1922 } 1923 } 1924 1925 const button_in_row_index: usize = 1; 1926 const title_child_index: usize = 0; 1927 const tags_child_index: usize = 2; 1928 1929 fn tagFlow(self: *View, index: usize) ?*ui.TagFlow { 1930 const inner = self.detailInner(index); 1931 if (inner.children.count() <= tags_child_index) return null; 1932 return switch (inner.children.values()[tags_child_index].widget) { 1933 .tag_flow => |*tf| tf, 1934 else => null, 1935 }; 1936 } 1937 1938 fn tagsFocused(self: *View, index: usize) bool { 1939 const inner = self.detailInner(index); 1940 const cid = inner.getFocus().child_id orelse return false; 1941 return inner.children.getIndex(cid) == tags_child_index and self.tagFlow(index) != null; 1942 } 1943 1944 // the open/close button inside the detail frame's tool row, identified 1945 // by its posting kind; a logged-out session's or conflicted issue's row 1946 // has none. 1947 fn statusButton(self: *View, index: usize) ?*wgt.TextBox(ui.Widget) { 1948 const row = self.toolRow(index); 1949 if (row.children.count() <= button_in_row_index) return null; 1950 switch (row.children.values()[button_in_row_index].widget) { 1951 .text_box => |*tb| switch (tb.box.focus.kind) { 1952 .custom => |custom| if (std.mem.eql(u8, custom, "submit")) return tb, 1953 else => {}, 1954 }, 1955 else => {}, 1956 } 1957 return null; 1958 } 1959 1960 fn toolRowFocused(self: *View, index: usize) bool { 1961 const outer = self.detailOuter(index); 1962 const cid = outer.getFocus().child_id orelse return false; 1963 return outer.children.getIndex(cid) == tool_row_index; 1964 } 1965 1966 fn focusTag(self: *View, index: usize, tf: *ui.TagFlow, root_focus: *Focus, item: usize) void { 1967 root_focus.setFocus(tf.text_boxes.items[item].getFocus().id); 1968 // keep the tag visible on the terminal: its rect offset by the flow's 1969 // position in the pane. 1970 if (self.session.is_terminal and item < tf.rects.items.len) { 1971 if (self.detailInner(index).children.values()[tags_child_index].rect) |flow_rect| { 1972 var rect = tf.rects.items[item]; 1973 rect.x += flow_rect.x; 1974 rect.y += flow_rect.y; 1975 self.detailScroll(index).scrollToRect(rect); 1976 } 1977 } 1978 } 1979 1980 fn titleFocused(self: *View, index: usize) bool { 1981 const inner = self.detailInner(index); 1982 const cid = inner.getFocus().child_id orelse return false; 1983 return inner.children.getIndex(cid) == title_child_index; 1984 } 1985 1986 fn authorPresent(self: *View, index: usize) bool { 1987 return self.author_id[index] != null; 1988 } 1989 1990 fn authorFocused(self: *View, index: usize) bool { 1991 const inner = self.detailInner(index); 1992 const cid = inner.getFocus().child_id orelse return false; 1993 return cid == self.author_id[index]; 1994 } 1995 1996 // the author sits right under the title at the top of the pane, so 1997 // focusing it scrolls there. 1998 fn focusAuthor(self: *View, index: usize, root_focus: *Focus) void { 1999 const id = self.author_id[index] orelse return; 2000 root_focus.setFocus(id); 2001 self.detailScroll(index).y = 0; 2002 } 2003 2004 // the title sits at the top of the pane, so focusing it scrolls there. 2005 fn focusTitle(self: *View, index: usize, root_focus: *Focus) void { 2006 const inner = self.detailInner(index); 2007 if (inner.children.count() == 0) return; 2008 root_focus.setFocus(inner.children.keys()[title_child_index]); 2009 self.detailScroll(index).y = 0; 2010 } 2011 2012 fn focusTags(self: *View, index: usize, root_focus: *Focus) !void { 2013 const tf = self.tagFlow(index) orelse return; 2014 if (tf.text_boxes.items.len == 0) return; 2015 const cid = tf.focus.child_id orelse tf.text_boxes.items[0].getFocus().id; 2016 const item = tf.indexOfFocusId(cid) orelse 0; 2017 self.focusTag(index, tf, root_focus, item); 2018 } 2019 2020 fn focusDescription(self: *View, index: usize, root_focus: *Focus) !void { 2021 if (self.description_id[index]) |id| root_focus.setFocus(id); 2022 } 2023 2024 // return to the tool row's last-focused button (the header when the 2025 // description page shows no row). 2026 fn focusToolRow(self: *View, index: usize, root_focus: *Focus) void { 2027 const cid = self.toolRow(index).getFocus().child_id orelse return self.focusHeader(root_focus); 2028 root_focus.setFocus(cid); 2029 } 2030 2031 // enter the detail pane. an empty pane (no issues) can't be entered. 2032 fn focusDetail(self: *View, index: usize, root_focus: *Focus) !void { 2033 if (self.detailInner(index).children.count() == 0) return; 2034 root_focus.setFocus(self.detailOuter(index).getFocus().id); 2035 } 2036 2037 // return to the list. 2038 fn focusList(self: *View, index: usize, root_focus: *Focus) !void { 2039 root_focus.setFocus(self.listScroll(index).getFocus().id); 2040 } 2041 2042 // cross to the header tabs above the stack. 2043 fn focusHeader(self: *View, root_focus: *Focus) void { 2044 root_focus.setFocus(self.box.children.keys()[header_index]); 2045 } 2046 2047 pub fn clearGrid(self: *View) void { 2048 self.box.clearGrid(); 2049 } 2050 2051 pub fn getGrid(self: View) ?Grid { 2052 return self.box.getGrid(); 2053 } 2054 2055 pub fn getFocus(self: *View) *Focus { 2056 return self.box.getFocus(); 2057 } 2058 2059 // for the parent's "scroll up at the top jumps to the header" check: at the 2060 // top only while the header tabs hold focus, so up from the split 2061 // crosses into the tabs first. 2062 pub fn getSelectedIndex(self: *View) ?usize { 2063 return if (self.headerActive()) 0 else 1; 2064 } 2065 }; 2066 2067 // the "a:" navigation link for `status`'s issues page filtered to the 2068 // url-encoded `tag` and rooted at issue `id` within `identity` ("owner/name"). 2069 // a rooted url ignores `status` (it derives its view from the issue's own); 2070 // with an empty `id` this is the bare list link. 2071 fn issuesLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, status: evt.Issue.Status, tag: []const u8, id: []const u8) ![]const u8 { 2072 const route = ui.RoutablePage.repoIssuesRoute(identity, status, tag, id) orelse return error.RouteTooLong; 2073 const url = try route.toUrl(page_arena); 2074 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2075 } 2076 2077 // the in-page "ai:" anchor for selecting issue `id` in `identity`'s list; the 2078 // href is only followed with js off. 2079 fn issueRowLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, id: []const u8) ![]const u8 { 2080 const route = ui.RoutablePage.repoIssuesRoute(identity, .open, "", id) orelse return error.RouteTooLong; 2081 const url = try route.toUrl(page_arena); 2082 return std.fmt.allocPrint(page_arena.allocator(), "ai:{s}", .{url}); 2083 } 2084 2085 // the "a:" link to the window `status_maybe` names, keeping the tag filter, 2086 // or the conflicts window when null, rooted at issue `id`. 2087 fn windowLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, status_maybe: ?evt.Issue.Status, id: []const u8) ![]const u8 { 2088 if (status_maybe) |status| return issuesLink(page_arena, data.identity, status, data.tag, id); 2089 return conflictsLink(page_arena, data.identity, id); 2090 } 2091 2092 // the "a:" link to the conflicts list rooted at issue `start` ("" = the 2093 // first window). 2094 fn conflictsLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, start: []const u8) ![]const u8 { 2095 const route = ui.RoutablePage.repoIssuesConflictsRoute(identity, start) orelse return error.RouteTooLong; 2096 const url = try route.toUrl(page_arena); 2097 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2098 } 2099 2100 // the "a:" link to the whole-description page of issue `id` within `identity`. 2101 fn descriptionLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, id: []const u8) ![]const u8 { 2102 const route = ui.RoutablePage.repoIssuesDescriptionRoute(identity, id) orelse return error.RouteTooLong; 2103 const url = try route.toUrl(page_arena); 2104 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2105 } 2106 2107 // the "a:" link to `status`'s issues list filtered to `tag` (raw; encoded here). 2108 fn tagLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, status: evt.Issue.Status, tag: []const u8) ![]const u8 { 2109 const encoded = try ui.urlEncodeRef(page_arena.allocator(), tag); 2110 const route = ui.RoutablePage.repoIssuesRoute(identity, status, encoded, "") orelse return error.RouteTooLong; 2111 const url = try route.toUrl(page_arena); 2112 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2113 } 2114 2115 // tabs switching between the issues page's views. 2116 pub const Header = struct { 2117 box: wgt.Box(ui.Widget), 2118 tab_ids: std.AutoArrayHashMapUnmanaged(usize, void), 2119 2120 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self) !Header { 2121 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 2122 errdefer box.deinit(allocator); 2123 2124 var tab_ids: std.AutoArrayHashMapUnmanaged(usize, void) = .empty; 2125 errdefer tab_ids.deinit(allocator); 2126 2127 const aa = session.page_arena.allocator(); 2128 2129 // a list tab per status, labeled with its listing's issue count 2130 for ([_]evt.Issue.Status{ .open, .closed }) |status| { 2131 const route = ui.RoutablePage.repoIssuesRoute(data.identity, status, data.tag, "") orelse return error.RouteTooLong; 2132 const link = try std.fmt.allocPrint(aa, "ai:{s}", .{try route.toUrl(session.page_arena)}); 2133 const label = try std.fmt.allocPrint(aa, "{s} ({d})", .{ @tagName(status), data.window(status).count }); 2134 try addTab(allocator, &box, &tab_ids, label, link); 2135 } 2136 2137 // tags tab, labeled with the active tag filter 2138 { 2139 const tags_route = ui.RoutablePage.repoIssuesTagsRoute(data.identity, data.tag) orelse return error.RouteTooLong; 2140 const tags_link = try std.fmt.allocPrint(aa, "ai:{s}", .{try tags_route.toUrl(session.page_arena)}); 2141 const label = if (data.tag.len == 0) "tags" else blk: { 2142 const decoded = std.Uri.percentDecodeInPlace(try aa.dupe(u8, data.tag)); 2143 break :blk try std.fmt.allocPrint(aa, "tags ({s})", .{decoded}); 2144 }; 2145 try addTab(allocator, &box, &tab_ids, label, tags_link); 2146 } 2147 2148 // new-issue tab; an edit or resolve url shows its tab in this place 2149 { 2150 const route = switch (data.view) { 2151 .edit => ui.RoutablePage.repoIssuesEditRoute(data.identity, data.selected_id) orelse return error.RouteTooLong, 2152 .resolve => ui.RoutablePage.repoIssuesResolveRoute(data.identity, data.selected_id, data.theirs_picks) orelse return error.RouteTooLong, 2153 else => ui.RoutablePage.repoIssuesNewRoute(data.identity) orelse return error.RouteTooLong, 2154 }; 2155 const link = try std.fmt.allocPrint(aa, "ai:{s}", .{try route.toUrl(session.page_arena)}); 2156 const label: []const u8 = switch (data.view) { 2157 .edit => "edit", 2158 .resolve => "resolve", 2159 else => "new", 2160 }; 2161 try addTab(allocator, &box, &tab_ids, label, link); 2162 } 2163 2164 // conflicts tab, labeled with the conflict count 2165 if (data.conflicts.count > 0) { 2166 const route = ui.RoutablePage.repoIssuesConflictsRoute(data.identity, "") orelse return error.RouteTooLong; 2167 const link = try std.fmt.allocPrint(aa, "ai:{s}", .{try route.toUrl(session.page_arena)}); 2168 const label = try std.fmt.allocPrint(aa, "conflicts ({d})", .{data.conflicts.count}); 2169 try addTab(allocator, &box, &tab_ids, label, link); 2170 } 2171 2172 var self = Header{ .box = box, .tab_ids = tab_ids }; 2173 // the tab matching the page's view is selected initially. 2174 self.getFocus().child_id = self.tab_ids.keys()[View.viewIndex(data.view)]; 2175 return self; 2176 } 2177 2178 fn addTab(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), tab_ids: *std.AutoArrayHashMapUnmanaged(usize, void), label: []const u8, link: []const u8) !void { 2179 var text_box = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 2180 errdefer text_box.deinit(allocator); 2181 text_box.getFocus().focusable = true; 2182 text_box.getFocus().kind = .{ .custom = link }; 2183 try tab_ids.put(allocator, text_box.getFocus().id, {}); 2184 try box.children.put(allocator, text_box.getFocus().id, .{ 2185 .widget = .{ .text_box = text_box }, 2186 .rect = null, 2187 .min_size = .{ .width = label.len + 2, .height = null }, 2188 }); 2189 } 2190 2191 pub fn deinit(self: *Header, allocator: std.mem.Allocator) void { 2192 self.box.deinit(allocator); 2193 self.tab_ids.deinit(allocator); 2194 } 2195 2196 pub fn build(self: *Header, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2197 self.clearGrid(); 2198 // only the selected tab shows its border 2199 for (self.box.children.keys(), self.box.children.values()) |id, *child| { 2200 switch (child.widget) { 2201 .text_box => |*tb| tb.options.border_style = if (self.getFocus().child_id == id) .single else .hidden, 2202 else => {}, 2203 } 2204 } 2205 try self.box.build(allocator, constraint, root_focus); 2206 } 2207 2208 pub fn input(self: *Header, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2209 _ = allocator; 2210 const current_tab = self.currentTabIndex() orelse return; 2211 if (inp.moveTab(key, current_tab, self.tab_ids.count())) |new_tab| { 2212 root_focus.setFocus(self.tab_ids.keys()[new_tab]); 2213 } 2214 } 2215 2216 pub fn clearGrid(self: *Header) void { 2217 self.box.clearGrid(); 2218 } 2219 2220 pub fn getGrid(self: Header) ?Grid { 2221 return self.box.getGrid(); 2222 } 2223 2224 pub fn getFocus(self: *Header) *Focus { 2225 return self.box.getFocus(); 2226 } 2227 2228 pub fn getSelectedIndex(self: Header) ?usize { 2229 return self.currentTabIndex(); 2230 } 2231 2232 fn currentTabIndex(self: Header) ?usize { 2233 const child_id = self.box.focus.child_id orelse return null; 2234 return self.tab_ids.getIndex(child_id); 2235 } 2236 }; 2237