╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Threads.zig ──────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1193 .rounded_corners = true, 1194 .wrap_kind = .word, 1195 .label = " description ", 1196 .bottom_label = if (cut_short) " click or press enter to see more " else "", 1197 }); 1198 errdefer tb.deinit(allocator); 1199 tb.getFocus().focusable = true; 1200 if (cut_short) tb.getFocus().kind = .{ .custom = try descriptionLink(self.session.page_arena, self.data.identity, entry.id) }; 1201 try inner.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1202 break :blk tb.getFocus().id; 1203 }; 1204 1205 const parent_route = commentsRoute(self.data.identity, entry.id, 0) orelse return error.RouteTooLong; 1206 try Attachment.appendRows(allocator, inner, self.session, self.data.identity, try parent_route.toUrl(self.session.page_arena), entry.attachments); 1207 1208 if (!description_page) { 1209 var reply = try Comment.linkBox(allocator, self.session, "new comment", newCommentRoute(self.data.identity, entry.id, "") orelse return error.RouteTooLong); 1210 errdefer reply.deinit(allocator); 1211 try inner.children.put(allocator, reply.getFocus().id, .{ .widget = .{ .text_box = reply }, .rect = null, .min_size = null }); 1212 1213 try Comment.appendCount(allocator, inner, entry.comments.count, "comment", "comments", self.session.page_arena.allocator()); 1214 for (entry.comments.comments) |comment| try Comment.appendComment(allocator, inner, self.session, self.data.identity, kind, comment); 1215 try Comment.appendWindowNav(allocator, inner, self.session, self.data.identity, kind, entry.id, null, entry.comments); 1216 1217 var spacer = try ui.Spacer.init(allocator); 1218 errdefer spacer.deinit(allocator); 1219 try inner.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 1220 } 1221 1222 // select the title by default 1223 inner.getFocus().child_id = inner.children.keys()[title_child_index]; 1224 1225 // reset the scroll to the top for the newly-shown issue: directly on the 1226 // terminal (the wasm offset), and via a version bump on the web (so the 1227 // renderer's scroll id changes and JS drops the preserved position). 1228 const sc = self.detailScroll(index); 1229 sc.x = 0; 1230 sc.y = 0; 1231 sc.getFocus().version +%= 1; 1232 } 1233 1234 pub fn input(self: *This, allocator: std.mem.Allocator, raw_key: Key, root_focus: *Focus) !void { 1235 // wheel input moves focus like the arrows 1236 const key: Key = if (raw_key == .mouse and raw_key.mouse.action == .scroll) 1237 (if (raw_key.mouse.action.scroll == .up) .arrow_up else .arrow_down) 1238 else 1239 raw_key; 1240 1241 if (self.headerActive()) { 1242 // down from the tabs re-enters the stack if the selected view has 1243 // something to focus; other keys move the tabs. 1244 if (inp.vertDirection(key) == .down) { 1245 const enterable = if (self.viewStack().getSelected()) |selected| switch (selected.*) { 1246 .tag_flow => |*tf| tf.text_boxes.items.len > 0, 1247 // nothing focusable in the unauthorized view 1248 .unauthorized => false, 1249 else => true, 1250 } else false; 1251 if (enterable) root_focus.setFocus(self.box.children.keys()[stack_index]); 1252 } else { 1253 try self.header().input(allocator, key, root_focus); 1254 } 1255 return; 1256 } 1257 if (self.tagsViewActive()) { 1258 self.tagsViewInput(key, root_focus); 1259 return; 1260 } 1261 if (self.formViewActive()) { 1262 if (has_conflicts and self.data.view == .resolve) { 1263 try self.resolveInput(allocator, key, root_focus); 1264 } else if (self.data.view == .new_comment or self.data.view == .edit_comment) { 1265 try self.commentFormInput(allocator, key, root_focus); 1266 } else { 1267 try self.formInput(allocator, key, root_focus); 1268 } 1269 return; 1270 } 1271 const i = self.selectedSplitIndex() orelse return; 1272 if (self.detailActive(i)) { 1273 try self.detailInput(allocator, i, key, root_focus); 1274 } else { 1275 try self.listInput(i, key, root_focus); 1276 } 1277 } 1278 1279 // arrow keys move the tag selection; up from the top row crosses to the 1280 // header tabs. 1281 fn tagsViewInput(self: *This, key: Key, root_focus: *Focus) void { 1282 const tf = self.tagsView(); 1283 const cid = tf.focus.child_id orelse return; 1284 const cur = tf.indexOfFocusId(cid) orelse return; 1285 const count = tf.text_boxes.items.len; 1286 switch (key) { 1287 .arrow_left => if (cur > 0) root_focus.setFocus(tf.text_boxes.items[cur - 1].getFocus().id), 1288 .arrow_right => if (cur + 1 < count) root_focus.setFocus(tf.text_boxes.items[cur + 1].getFocus().id), 1289 .arrow_up => if (tf.rowStep(cur, false)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id) else self.focusHeader(root_focus), 1290 .arrow_down => if (tf.rowStep(cur, true)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id), 1291 .home => root_focus.setFocus(tf.text_boxes.items[0].getFocus().id), 1292 .end => root_focus.setFocus(tf.text_boxes.items[count - 1].getFocus().id), 1293 else => {}, 1294 } 1295 } 1296 1297 fn listInput(self: *This, index: usize, key: Key, root_focus: *Focus) !void { 1298 // up/down (and the scroll wheel) move the selection a row; page up/down 1299 // jump a fixed amount. right/Enter cross into the detail pane. up from 1300 // the top row crosses into the header tabs. 1301 if (inp.rowDelta(key, @intCast(self.listBox(index).children.count()))) |delta| { 1302 const lb = self.listBox(index); 1303 const at_top = if (lb.getFocus().child_id) |cid| lb.children.getIndex(cid) == 0 else true; 1304 if (delta < 0 and at_top) return self.focusHeader(root_focus); 1305 ui.moveRowFocus(lb, self.listScroll(index), root_focus, delta); 1306 return; 1307 } 1308 switch (key) { 1309 .enter, .arrow_right => try self.focusDetail(index, root_focus), 1310 else => {}, 1311 } 1312 } 1313 1314 fn detailInput(self: *This, allocator: std.mem.Allocator, index: usize, key: Key, root_focus: *Focus) !void { 1315 switch (key) { 1316 .page_up => { 1317 self.pageDetail(index, root_focus, -10); 1318 return; 1319 }, 1320 .page_down => { 1321 self.pageDetail(index, root_focus, 10); 1322 return; 1323 }, 1324 .home => { 1325 self.jumpDetail(index, root_focus, false); 1326 return; 1327 }, 1328 .end => { 1329 self.jumpDetail(index, root_focus, true); 1330 return; 1331 }, 1332 else => {}, 1333 } 1334 if (self.data.comment_page != null and std.mem.eql(u8, self.window(index).items[self.detailed_index[index] orelse return].id, self.data.selected_id)) { 1335 try self.commentInput(allocator, index, key, root_focus); 1336 } else if (self.toolRowFocused(index)) { 1337 try self.toolRowInput(allocator, index, key, root_focus); 1338 } else if (self.titleFocused(index)) { 1339 try self.titleInput(index, key, root_focus); 1340 } else if (self.authorFocused(index)) { 1341 try self.authorInput(index, key, root_focus); 1342 } else if (self.tagsFocused(index)) { 1343 try self.tagsInput(index, key, root_focus); 1344 } else if (self.descriptionFocused(index)) { 1345 try self.descriptionInput(index, key, root_focus); 1346 } else if (try self.attachmentInput(allocator, index, key, root_focus)) {} else { 1347 try self.commentInput(allocator, index, key, root_focus); 1348 } 1349 } 1350 1351 fn attachmentInput(self: *This, allocator: std.mem.Allocator, index: usize, key: Key, root_focus: *Focus) !bool { 1352 const child_index = self.focusedDetailChild(index, root_focus) orelse return false; 1353 const child = &self.detailInner(index).children.values()[child_index].widget; 1354 const row = switch (child.*) { 1355 .box => |*box| box, 1356 else => return false, 1357 }; 1358 const attachment_id = Attachment.removeId(row) orelse return false; 1359 if (row.children.count() < 2) return false; 1360 const remove_id = row.children.keys()[1]; 1361 switch (key) { 1362 .arrow_left => if (!self.moveCommentHorizontal(index, root_focus, false)) try self.focusList(index, root_focus), 1363 .arrow_right => _ = self.moveCommentHorizontal(index, root_focus, true), 1364 .arrow_up => _ = self.moveDetailVertical(index, root_focus, false), 1365 .arrow_down => _ = self.moveDetailVertical(index, root_focus, true), 1366 .enter => if (row.getFocus().child_id == remove_id) try self.removeEvent(allocator, .attach, attachment_id), 1367 .mouse => |mouse| if (inp.leftClickOn(root_focus, remove_id, mouse)) try self.removeEvent(allocator, .attach, attachment_id), 1368 else => {}, 1369 } 1370 return true; 1371 } 1372 1373 // the tool row: enter or a click on the open/close button flips the 1374 // issue's status; the edit and resolve buttons are a: links the host 1375 // follows. arrows cross between the buttons and to the neighboring 1376 // widgets. 1377 fn toolRowInput(self: *This, allocator: std.mem.Allocator, index: usize, key: Key, root_focus: *Focus) !void { 1378 const row = self.toolRow(index); 1379 const cur = if (row.getFocus().child_id) |cid| row.children.getIndex(cid) orelse return else return; 1380 const on_status = if (self.status_button_id[index]) |id| row.getFocus().child_id == id else false; 1381 const on_remove = if (self.remove_button_id[index]) |id| row.getFocus().child_id == id else false; 1382 switch (key) { 1383 .arrow_left => if (cur > first_in_row_index) root_focus.setFocus(row.children.keys()[cur - 1]) else try self.focusList(index, root_focus), 1384 .arrow_right => if (cur + 1 < row.children.count()) root_focus.setFocus(row.children.keys()[cur + 1]), 1385 .arrow_up => self.focusHeader(root_focus), 1386 .arrow_down => { 1387 self.focusDetailEdge(index, root_focus, false); 1388 self.detailScroll(index).y = 0; 1389 }, 1390 .enter => if (on_remove) try self.removeThread(allocator, index) else if (on_status) try self.toggleStatus(allocator, index), 1391 .mouse => |mouse| if (self.remove_button_id[index]) |id| { 1392 if (inp.leftClickOn(root_focus, id, mouse)) return self.removeThread(allocator, index); 1393 if (self.status_button_id[index]) |status_id| if (inp.leftClickOn(root_focus, status_id, mouse)) try self.toggleStatus(allocator, index); 1394 } else if (self.status_button_id[index]) |id| { 1395 if (inp.leftClickOn(root_focus, id, mouse)) try self.toggleStatus(allocator, index); 1396 }, 1397 else => {}, 1398 } 1399 } 1400 1401 fn titleInput(self: *This, index: usize, key: Key, root_focus: *Focus) !void { 1402 switch (key) { 1403 .arrow_left => try self.focusList(index, root_focus), 1404 .arrow_up => self.focusToolRow(index, root_focus), 1405 .arrow_down => _ = self.moveDetailVertical(index, root_focus, true), 1406 else => {}, 1407 } 1408 } 1409 1410 // the author box's a: link (when it has one) is followed by the host on 1411 // enter / a click; arrows cross to the neighboring widgets. 1412 fn authorInput(self: *This, index: usize, key: Key, root_focus: *Focus) !void { 1413 switch (key) { 1414 .arrow_left => try self.focusList(index, root_focus), 1415 .arrow_up => _ = self.moveDetailVertical(index, root_focus, false), 1416 .arrow_down => _ = self.moveDetailVertical(index, root_focus, true), 1417 else => {}, 1418 } 1419 } 1420 1421 fn descriptionInput(self: *This, index: usize, key: Key, root_focus: *Focus) !void { 1422 const sc = self.detailScroll(index); 1423 switch (key) { 1424 .arrow_left => return self.focusList(index, root_focus), 1425 // the terminal moves through the description one line at a time; 1426 // the web lets focus changes drive its native scroll. 1427 .arrow_up => { 1428 if (!self.session.is_terminal) { 1429 _ = self.moveDetailVertical(index, root_focus, false); 1430 return; 1431 } 1432 if (self.focusAdjacentVisible(index, root_focus, false)) return; 1433 const before = sc.y; 1434 sc.y -= 1; 1435 sc.clampToContent(); 1436 if (sc.y == before) _ = self.moveDetailVertical(index, root_focus, false); 1437 return; 1438 }, 1439 .arrow_down => { 1440 if (!self.session.is_terminal) { 1441 _ = self.moveDetailVertical(index, root_focus, true); 1442 return; 1443 } 1444 if (self.focusAdjacentVisible(index, root_focus, true)) return; 1445 const before = sc.y; 1446 sc.y += 1; 1447 sc.clampToContent(); 1448 if (sc.y == before) _ = self.moveDetailVertical(index, root_focus, true); 1449 return; 1450 }, 1451 else => return, 1452 } 1453 sc.clampToContent(); 1454 } 1455 1456 fn commentInput(self: *This, allocator: std.mem.Allocator, index: usize, key: Key, root_focus: *Focus) !void { 1457 const item = self.focusedComment(index, root_focus); 1458 switch (key) { 1459 .arrow_left => if (!self.moveCommentHorizontal(index, root_focus, false)) return self.focusList(index, root_focus), 1460 .arrow_right => _ = self.moveCommentHorizontal(index, root_focus, true), 1461 .arrow_up => _ = self.moveDetailVertical(index, root_focus, false), 1462 .arrow_down => _ = self.moveDetailVertical(index, root_focus, true), 1463 .enter => if (item) |comment| if (comment.removeFocused()) try self.removeComment(allocator, comment), 1464 .mouse => |mouse| if (item) |comment| if (comment.remove_button_id) |id| { 1465 if (inp.leftClickOn(root_focus, id, mouse)) try self.removeComment(allocator, comment); 1466 }, 1467 else => return, 1468 } 1469 } 1470 1471 fn focusedComment(self: *This, index: usize, root_focus: *Focus) ?*Comment.Item { 1472 const child_index = self.focusedDetailChild(index, root_focus) orelse return null; 1473 return switch (self.detailInner(index).children.values()[child_index].widget) { 1474 .repo_comment => |*comment| comment, 1475 else => null, 1476 }; 1477 } 1478 1479 // page the detail scroll, then focus the first or last visible widget. 1480 fn pageDetail(self: *This, index: usize, root_focus: *Focus, delta: isize) void { 1481 if (!self.session.is_terminal) return; 1482 const sc = self.detailScroll(index); 1483 sc.y += delta; 1484 sc.clampToContent(); 1485 self.focusVisible(index, root_focus, delta > 0); 1486 } 1487 1488 // jump to the detail's first or last focusable widget. the terminal pins 1489 // its synthetic scroll too; the browser scrolls to the new focus itself. 1490 fn jumpDetail(self: *This, index: usize, root_focus: *Focus, to_end: bool) void { 1491 if (self.session.is_terminal) { 1492 const sc = self.detailScroll(index); 1493 sc.y = if (to_end) std.math.maxInt(isize) else 0; 1494 sc.clampToContent(); 1495 self.focusVisible(index, root_focus, to_end); 1496 } else { 1497 self.focusDetailEdge(index, root_focus, to_end); 1498 } 1499 } 1500 1501 fn focusDetailEdge(self: *This, index: usize, root_focus: *Focus, last: bool) void { 1502 const focus = self.detailInner(index).getFocus(); 1503 var chosen: ?usize = null; 1504 for (focus.children.keys(), focus.children.values()) |id, child| { 1505 if (!child.focus.focusable) continue; 1506 chosen = id; 1507 if (!last) break; 1508 } 1509 if (chosen) |id| root_focus.setFocus(id); 1510 } 1511 1512 fn detailRectVisible(self: *This, index: usize, rect: layout.IRect) bool { 1513 const sc = self.detailScroll(index); 1514 const viewport = sc.grid orelse return false; 1515 const top = sc.y; 1516 const bottom = top + @as(isize, @intCast(viewport.size.height - sc.bar_h)); 1517 return rect.y + @as(isize, @intCast(rect.size.height)) > top and rect.y < bottom; 1518 } 1519 1520 // focus the top- or bottom-most focusable widget intersecting the viewport. 1521 fn focusVisible(self: *This, index: usize, root_focus: *Focus, last: bool) void { 1522 const inner = self.detailInner(index); 1523 var offset: usize = 0; 1524 while (offset < inner.children.count()) : (offset += 1) { 1525 const child_index = if (last) inner.children.count() - 1 - offset else offset; 1526 if (self.focusVisibleChild(index, child_index, root_focus, last)) return; 1527 } 1528 } 1529 1530 fn focusVisibleChild(self: *This, index: usize, child_index: usize, root_focus: *Focus, last: bool) bool { 1531 const child = &self.detailInner(index).children.values()[child_index]; 1532 const child_rect = child.rect orelse return false; 1533 var chosen: ?usize = null; 1534 if (child.widget.getFocus().focusable and self.detailRectVisible(index, child_rect)) { 1535 const id = self.detailInner(index).children.keys()[child_index]; 1536 chosen = id; 1537 if (!last) { 1538 root_focus.setFocus(id); 1539 return true; 1540 } 1541 } 1542 1543 for (child.widget.getFocus().children.keys(), child.widget.getFocus().children.values()) |id, focus_child| { 1544 if (!focus_child.focus.focusable) continue; 1545 const rect: layout.IRect = .{ 1546 .x = child_rect.x + @as(isize, @intCast(focus_child.rect.x)), 1547 .y = child_rect.y + @as(isize, @intCast(focus_child.rect.y)), 1548 .size = focus_child.rect.size, 1549 }; 1550 if (!self.detailRectVisible(index, rect)) continue; 1551 chosen = id; 1552 if (!last) break; 1553 } 1554 if (chosen) |id| { 1555 root_focus.setFocus(id); 1556 return true; 1557 } 1558 return false; 1559 } 1560 1561 fn focusAdjacentVisible(self: *This, index: usize, root_focus: *Focus, down: bool) bool { 1562 const inner = self.detailInner(index); 1563 var next = self.focusedDetailChild(index, root_focus) orelse return false; 1564 while (true) { 1565 if (down) { 1566 next += 1; 1567 if (next >= inner.children.count()) return false; 1568 } else { 1569 if (next == 0) return false; 1570 next -= 1; 1571 } 1572 if (self.focusVisibleChild(index, next, root_focus, !down)) { 1573 self.focusDetailChild(index, next, !down, root_focus); 1574 return true; 1575 } 1576 } 1577 } 1578 1579 fn focusedDetailChild(self: *This, index: usize, root_focus: *Focus) ?usize { 1580 const inner = self.detailInner(index); 1581 var id = root_focus.grandchild_id orelse return null; 1582 while (inner.getFocus().children.get(id)) |child| { 1583 if (child.parent_id == inner.getFocus().id) return inner.children.getIndex(id); 1584 id = child.parent_id; 1585 } 1586 return null; 1587 } 1588 1589 fn detailChildFocusable(self: *This, index: usize, child_index: usize) bool { 1590 const child = &self.detailInner(index).children.values()[child_index].widget; 1591 const focus = child.getFocus(); 1592 if (focus.focusable) return true; 1593 for (focus.children.values()) |item| { 1594 if (item.focus.focusable) return true; 1595 } 1596 return false; 1597 } 1598 1599 fn focusDetailChild(self: *This, index: usize, child_index: usize, last: bool, root_focus: *Focus) void { 1600 const inner = self.detailInner(index); 1601 const child = &inner.children.values()[child_index]; 1602 var target = inner.children.keys()[child_index]; 1603 var rect = child.rect orelse return; 1604 switch (child.widget) { 1605 .repo_comment => |*comment| { 1606 if (comment.rowRect(last)) |row_rect| { 1607 rect.x += row_rect.x; 1608 rect.y += row_rect.y; 1609 rect.size = row_rect.size; 1610 } 1611 if (last) comment.focusBody(root_focus) else comment.focusMetadata(root_focus); 1612 }, 1613 .box => |*box| if (box.children.count() > 0) { 1614 target = box.children.keys()[if (last) box.children.count() - 1 else 0]; 1615 root_focus.setFocus(target); 1616 }, 1617 else => root_focus.setFocus(target), 1618 } 1619 const sc = self.detailScroll(index); 1620 if (sc.grid) |viewport| { 1621 const viewport_height = viewport.size.height - sc.bar_h; 1622 if (rect.size.height > viewport_height) { 1623 const bottom_aligned = rect.y + @as(isize, @intCast(rect.size.height - viewport_height)); 1624 sc.y = std.math.clamp(sc.y, rect.y, bottom_aligned); 1625 sc.clampToContent(); 1626 return; 1627 } 1628 } 1629 sc.scrollToRect(rect); 1630 } 1631 1632 fn moveDetailVertical(self: *This, index: usize, root_focus: *Focus, down: bool) bool { 1633 const inner = self.detailInner(index); 1634 const current = self.focusedDetailChild(index, root_focus) orelse return false; 1635 if (inner.children.values()[current].widget == .repo_comment) { 1636 const comment = &inner.children.values()[current].widget.repo_comment; 1637 if (down and !comment.bodyFocused()) { 1638 self.focusDetailChild(index, current, true, root_focus); 1639 return true; 1640 } 1641 if (!down and comment.bodyFocused()) { 1642 self.focusDetailChild(index, current, false, root_focus); 1643 return true; 1644 } 1645 } 1646 1647 var next = current; 1648 while (true) { 1649 if (down) { 1650 next += 1; 1651 if (next >= inner.children.count()) return false; 1652 } else { 1653 if (next == 0) return false; 1654 next -= 1; 1655 } 1656 if (!self.detailChildFocusable(index, next)) continue; 1657 self.focusDetailChild(index, next, !down, root_focus); 1658 return true; 1659 } 1660 } 1661 1662 fn moveCommentHorizontal(self: *This, index: usize, root_focus: *Focus, right: bool) bool { 1663 const inner = self.detailInner(index); 1664 const current = self.focusedDetailChild(index, root_focus) orelse return false; 1665 const child = &inner.children.values()[current]; 1666 if (child.widget == .repo_comment) { 1667 const moved = child.widget.repo_comment.moveHorizontal(root_focus, right); 1668 if (moved) if (child.rect) |rect| self.detailScroll(index).scrollToRect(rect); 1669 return moved; 1670 } 1671 const row = switch (child.widget) { 1672 .box => |*box| box, 1673 else => return false, 1674 }; 1675 const selected = row.getFocus().child_id orelse return false; 1676 const selected_index = row.children.getIndex(selected) orelse return false; 1677 const target = if (right) selected_index + 1 else selected_index -| 1; 1678 if (target == selected_index or target >= row.children.count()) return false; 1679 root_focus.setFocus(row.children.keys()[target]); 1680 if (child.rect) |rect| self.detailScroll(index).scrollToRect(rect); 1681 return true; 1682 } 1683 1684 // up/down (and tab/shift+tab) move between a form's fields; up from the 1685 // title crosses into the header tabs. the multiline description keeps 1686 // enter and any up/down that has a row to move to. 1687 fn formInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1688 const form = self.threadForm() orelse return; 1689 const cid = form.getFocus().child_id orelse return; 1690 const cur = form.children.getIndex(cid) orelse return; 1691 1692 if (cur == description_field_index) { 1693 const child = &form.children.values()[cur]; 1694 if (try multilineKeepsKey(&child.widget.text_input, allocator, key)) 1695 return child.widget.input(allocator, key, root_focus); 1696 } 1697 1698 switch (key) { 1699 .arrow_up, .back_tab => if (cur > 0) 1700 root_focus.setFocus(form.children.keys()[cur - 1]) 1701 else 1702 self.focusHeader(root_focus), 1703 .arrow_down, .tab => if (cur < submit_field_index) { 1704 root_focus.setFocus(form.children.keys()[cur + 1]); 1705 }, 1706 .enter => if (cur == submit_field_index) try self.submitForm(allocator), 1707 .mouse => |mouse| if (cur == submit_field_index) { 1708 const submit = &form.children.values()[cur].widget.submit_button; 1709 if (inp.leftClickOn(root_focus, submit.buttonId(), mouse)) try self.submitForm(allocator); 1710 }, 1711 else => try form.children.values()[cur].widget.input(allocator, key, root_focus), 1712 } 1713 } 1714 1715 fn commentFormInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1716 const form = self.threadForm() orelse return; 1717 const cid = form.getFocus().child_id orelse return; 1718 const cur = form.children.getIndex(cid) orelse return; 1719 const child = &form.children.values()[cur]; 1720 1721 if (cur == comment_body_field_index and try multilineKeepsKey(&child.widget.text_input, allocator, key)) 1722 return child.widget.input(allocator, key, root_focus); 1723 1724 switch (key) { 1725 .arrow_up, .back_tab => if (cur > comment_author_field_index) 1726 root_focus.setFocus(form.children.keys()[cur - 1]) 1727 else 1728 self.focusHeader(root_focus), 1729 .arrow_down, .tab => if (cur < comment_submit_field_index) 1730 root_focus.setFocus(form.children.keys()[cur + 1]), 1731 .enter => if (cur == comment_submit_field_index) try self.submitComment(allocator), 1732 .mouse => |mouse| if (cur == comment_submit_field_index) { 1733 const submit = &child.widget.submit_button; 1734 if (inp.leftClickOn(root_focus, submit.buttonId(), mouse)) try self.submitComment(allocator); 1735 }, 1736 else => try child.widget.input(allocator, key, root_focus), 1737 } 1738 } 1739 1740 // the multiline hunk inputs keep enter and any up/down that has a row to 1741 // move to; enter on a "use this" link is a navigation the host follows 1742 // before this runs. 1743 fn resolveInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1744 const form = self.threadForm() orelse return; 1745 const cid = form.getFocus().child_id orelse return; 1746 const cur = form.children.getIndex(cid) orelse return; 1747 const child = &form.children.values()[cur]; 1748 1749 if (child.widget == .text_input) { 1750 const ti = &child.widget.text_input; 1751 if (ti.options.multiline and try multilineKeepsKey(ti, allocator, key)) 1752 return child.widget.input(allocator, key, root_focus); 1753 } 1754 1755 const on_submit = child.widget == .submit_button; 1756 1757 switch (key) { 1758 .arrow_up, .back_tab => if (resolveStep(form, cur, false)) |i| self.focusResolveChild(form, i, root_focus) else self.focusHeader(root_focus), 1759 .arrow_down, .tab => if (resolveStep(form, cur, true)) |i| self.focusResolveChild(form, i, root_focus), 1760 .arrow_left, .arrow_right => switch (child.widget) { 1761 .box => |*row| { 1762 const row_cid = row.getFocus().child_id orelse return; 1763 const row_index = row.children.getIndex(row_cid) orelse return; 1764 if (key == .arrow_left) { 1765 if (row_index > 0) root_focus.setFocus(row.children.keys()[row_index - 1]); 1766 } else if (row_index + 1 < row.children.count()) { 1767 root_focus.setFocus(row.children.keys()[row_index + 1]); 1768 } 1769 }, 1770 else => try child.widget.input(allocator, key, root_focus), 1771 }, 1772 .enter => if (on_submit) try self.submitResolution(allocator), 1773 .mouse => |mouse| if (on_submit) { 1774 if (inp.leftClickOn(root_focus, child.widget.submit_button.buttonId(), mouse)) try self.submitResolution(allocator); 1775 }, 1776 else => try child.widget.input(allocator, key, root_focus), 1777 } 1778 } 1779 1780 // a multiline input keeps enter and any up/down that has a row to move to 1781 fn multilineKeepsKey(text_input: *wgt.TextInput(ui.Widget), allocator: std.mem.Allocator, key: Key) !bool { 1782 return switch (key) { 1783 .enter => true, 1784 .arrow_up => !try text_input.cursorOnFirstRow(allocator), 1785 .arrow_down => !try text_input.cursorOnLastRow(allocator), 1786 else => false, 1787 }; 1788 } 1789 1790 // the neighboring focusable resolve-form child, skipping the spacer and 1791 // the blank gap rows 1792 fn resolveStep(form: *wgt.Box(ui.Widget), cur: usize, down: bool) ?usize { 1793 var i = cur; 1794 while (true) { 1795 if (down) { 1796 i += 1; 1797 if (i >= form.children.count()) return null; 1798 } else { 1799 if (i == 0) return null; 1800 i -= 1; 1801 } 1802 switch (form.children.values()[i].widget) { 1803 .spacer, .text => continue, 1804 else => return i, 1805 } 1806 } 1807 } 1808 1809 // focus the resolve-form child at `i` (a version row focuses its selected 1810 // child) and keep it visible on the terminal 1811 fn focusResolveChild(self: *This, form: *wgt.Box(ui.Widget), i: usize, root_focus: *Focus) void { 1812 const child = &form.children.values()[i]; 1813 switch (child.widget) { 1814 .box => |*row| root_focus.setFocus(row.getFocus().child_id orelse form.children.keys()[i]), 1815 else => root_focus.setFocus(form.children.keys()[i]), 1816 } 1817 if (self.session.is_terminal) { 1818 if (self.resolveScroll()) |sc| { 1819 if (child.rect) |rect| sc.scrollToRect(rect); 1820 } 1821 } 1822 } 1823 1824 // re-emit the conflicted issue's event with the resolved content; any 1825 // event on the issue settles its conflict entry. this is the terminal 1826 // path; the web posts the form to the resolve route. 1827 fn submitResolution(self: *This, allocator: std.mem.Allocator) !void { 1828 if (comptime wasm) return; 1829 const io = self.session.io orelse return; 1830 const src = self.data.repo_source orelse return; 1831 const entry = self.data.selectedThread() orelse return; 1832 const author = (try self.session.eventAuthor()) orelse return; 1833 const form = self.threadForm() orelse return; 1834 1835 // gather the inputs by name; the d<n> hunk inputs appear in chunk order 1836 var title: ?[]u8 = null; 1837 var tags: ?[]u8 = null; 1838 var hunks: std.ArrayList([]const u8) = .empty; 1839 defer { 1840 if (title) |t| allocator.free(t); 1841 if (tags) |t| allocator.free(t); 1842 for (hunks.items) |h| allocator.free(h); 1843 hunks.deinit(allocator); 1844 } 1845 for (form.children.values()) |*child| switch (child.widget) { 1846 .text_input => |*ti| { 1847 const text = try ti.text(allocator); 1848 errdefer allocator.free(text); 1849 if (std.mem.eql(u8, ti.options.name, "title")) { 1850 title = text; 1851 } else if (std.mem.eql(u8, ti.options.name, "tags")) { 1852 tags = text; 1853 } else { 1854 try hunks.append(allocator, text); 1855 } 1856 }, 1857 else => {}, 1858 }; 1859 1860 var id_bytes: [evt.event_id_size]u8 = undefined; 1861 _ = try std.fmt.hexToBytes(&id_bytes, entry.id[0 .. evt.event_id_size * 2]); 1862 1863 switch (src.repo_kind) { 1864 inline else => |repo_kind| { 1865 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 1866 defer any_repo.deinit(io, allocator); 1867 switch (any_repo) { 1868 inline else => |*repo| Event.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .resolve = .{ 1869 .title = title, 1870 .tags = tags, 1871 .hunks = hunks.items, 1872 } }, author) catch |err| switch (err) { 1873 // leave the form up for correction 1874 error.InvalidFields => return, 1875 else => |e| return e, 1876 }, 1877 } 1878 }, 1879 } 1880 1881 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 1882 try self.session.navigate(route); 1883 } 1884 1885 // commit a new or edited comment and navigate to its permalink. this is the 1886 // terminal path; the web posts the form to its comment route. 1887 fn submitComment(self: *This, allocator: std.mem.Allocator) !void { 1888 if (comptime wasm) return; 1889 const io = self.session.io orelse return; 1890 const src = self.data.repo_source orelse return; 1891 const author = (try self.session.eventAuthor()) orelse return; 1892 const form = self.threadForm() orelse return; 1893 const body_input = &form.children.values()[comment_body_field_index].widget.text_input; 1894 const body = try body_input.text(allocator); 1895 defer allocator.free(body); 1896 if (!evt.Comment.fieldsValid(body)) return; 1897 1898 var thread_id: [evt.event_id_size * 2]u8 = undefined; 1899 @memcpy(&thread_id, self.data.selected_id); 1900 const parent_id = if (self.data.comment_id.len == 0) thread_id else blk: { 1901 var parent: [evt.event_id_size * 2]u8 = undefined; 1902 @memcpy(&parent, self.data.comment_id); 1903 break :blk parent; 1904 }; 1905 var event_id_hex: [evt.event_id_size * 2]u8 = undefined; 1906 const editing = self.data.view == .edit_comment; 1907 switch (src.repo_kind) { 1908 inline else => |repo_kind| { 1909 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 1910 defer any_repo.deinit(io, allocator); 1911 switch (any_repo) { 1912 inline else => |*repo| if (editing) { 1913 var comment_id: [evt.event_id_size]u8 = undefined; 1914 _ = std.fmt.hexToBytes(&comment_id, self.data.comment_id) catch return; 1915 try evt.Comment.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &thread_id, &comment_id, body, author); 1916 event_id_hex = std.fmt.bytesToHex(comment_id, .lower); 1917 } else { 1918 event_id_hex = try evt.Comment.create(repo_kind, repo.self_repo_opts, io, allocator, repo, &thread_id, &parent_id, body, author); 1919 }, 1920 } 1921 }, 1922 } 1923 1924 body_input.clear(allocator); 1925 const route = commentRoute(self.data.identity, &thread_id, &event_id_hex, 0) orelse return; 1926 try self.session.navigate(route); 1927 } 1928 1929 // the terminal submit paths: the new form commits a new-issue event, the 1930 // edit form re-emits the selected issue's event with the form's content. 1931 // the web posts the forms to their routes instead. 1932 fn submitForm(self: *This, allocator: std.mem.Allocator) !void { 1933 if (self.data.view == .edit) { 1934 try self.submitEditedThread(allocator); 1935 } else { 1936 try self.submitNewThread(allocator); 1937 } 1938 } 1939 1940 // commit the new issue to the repo's events branch and navigate to it. 1941 // this is the terminal path; the web posts the form to the issue route, 1942 // so the wasm side never runs (or compiles) the repo access below. 1943 fn submitNewThread(self: *This, allocator: std.mem.Allocator) !void { 1944 if (comptime wasm) return; 1945 const io = self.session.io orelse return; 1946 const src = self.data.repo_source orelse return; 1947 const author = (try self.session.eventAuthor()) orelse return; 1948 1949 const form = self.threadForm() orelse return; 1950 const title_input = &form.children.values()[title_field_index].widget.text_input; 1951 const tags_input = &form.children.values()[tags_field_index].widget.text_input; 1952 const description_input = &form.children.values()[description_field_index].widget.text_input; 1953 1954 const title = try title_input.text(allocator); 1955 defer allocator.free(title); 1956 const tags = try tags_input.text(allocator); 1957 defer allocator.free(tags); 1958 const description = try description_input.text(allocator); 1959 defer allocator.free(description); 1960 1961 if (!Event.fieldsValid(title, tags)) return; 1962 1963 var id_bytes: [evt.event_id_size]u8 = undefined; 1964 io.random(&id_bytes); 1965 const event_id_hex = std.fmt.bytesToHex(id_bytes, .lower); 1966 1967 const event = evt.EventWithId{ 1968 .id = event_id_hex, 1969 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 1970 .author = author, 1971 .event = @unionInit(evt.Event, @tagName(kind), Event{ 1972 .title = title, 1973 .description = description, 1974 .tags = tags, 1975 }), 1976 }; 1977 1978 switch (src.repo_kind) { 1979 inline else => |repo_kind| { 1980 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 1981 defer any_repo.deinit(io, allocator); 1982 switch (any_repo) { 1983 inline else => |*repo| try evt.consume(repo_kind, repo.self_repo_opts, io, allocator, repo, evt.events_ref, &.{event}), 1984 } 1985 }, 1986 } 1987 1988 // wipe the form so a return visit starts fresh 1989 title_input.clear(allocator); 1990 tags_input.clear(allocator); 1991 description_input.clear(allocator); 1992 1993 const route = listRoute(self.data.identity, @enumFromInt(0), "", &event_id_hex) orelse return; 1994 try self.session.navigate(route); 1995 } 1996 1997 // re-emit the selected issue's event with the form's content (status 1998 // preserved), then reload the issue's page. this is the terminal path; 1999 // the web posts the form to the edit route. 2000 fn submitEditedThread(self: *This, allocator: std.mem.Allocator) !void { 2001 if (comptime wasm) return; 2002 const io = self.session.io orelse return; 2003 const src = self.data.repo_source orelse return; 2004 const entry = self.data.selectedThread() orelse return; 2005 const author = (try self.session.eventAuthor()) orelse return; 2006 2007 const form = self.threadForm() orelse return; 2008 const title_input = &form.children.values()[title_field_index].widget.text_input; 2009 const tags_input = &form.children.values()[tags_field_index].widget.text_input; 2010 const description_input = &form.children.values()[description_field_index].widget.text_input; 2011 2012 const title = try title_input.text(allocator); 2013 defer allocator.free(title); 2014 const tags = try tags_input.text(allocator); 2015 defer allocator.free(tags); 2016 const description = try description_input.text(allocator); 2017 defer allocator.free(description); 2018 2019 if (!Event.fieldsValid(title, tags)) return; 2020 2021 var id_bytes: [evt.event_id_size]u8 = undefined; 2022 _ = try std.fmt.hexToBytes(&id_bytes, entry.id[0 .. evt.event_id_size * 2]); 2023 2024 switch (src.repo_kind) { 2025 inline else => |repo_kind| { 2026 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2027 defer any_repo.deinit(io, allocator); 2028 switch (any_repo) { 2029 inline else => |*repo| if (has_status) 2030 try Event.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 2031 .title = title, 2032 .tags = tags, 2033 .description = description, 2034 } }, author) 2035 else 2036 try Event.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, title, tags, description, author), 2037 } 2038 }, 2039 } 2040 2041 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2042 try self.session.navigate(route); 2043 } 2044 2045 fn removeThread(self: *This, allocator: std.mem.Allocator, index: usize) !void { 2046 const sel = self.detailed_index[index] orelse return; 2047 const entry = self.window(index).items[sel]; 2048 2049 var id_bytes: [evt.event_id_size]u8 = undefined; 2050 _ = std.fmt.hexToBytes(&id_bytes, entry.id) catch return; 2051 try self.removeEvent(allocator, kind, id_bytes); 2052 } 2053 2054 fn removeComment(self: *This, allocator: std.mem.Allocator, comment: *Comment.Item) !void { 2055 try self.removeEvent(allocator, .comment, comment.id); 2056 } 2057 2058 fn removeEvent(self: *This, allocator: std.mem.Allocator, event_kind: evt.EventKind, id: [evt.event_id_size]u8) !void { 2059 if (comptime wasm) return; 2060 const io = self.session.io orelse return; 2061 const src = self.data.repo_source orelse return; 2062 const author = (try self.session.eventAuthor()) orelse return; 2063 2064 switch (src.repo_kind) { 2065 inline else => |repo_kind| { 2066 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2067 defer any_repo.deinit(io, allocator); 2068 switch (any_repo) { 2069 inline else => |*repo| try evt.remove(repo_kind, repo.self_repo_opts, io, allocator, repo, &id, event_kind, author), 2070 } 2071 }, 2072 } 2073 2074 const id_hex = std.fmt.bytesToHex(id, .lower); 2075 const route = ui.RoutablePage.repoEventsRoute(self.data.identity, .removed, event_kind, &id_hex) orelse return; 2076 try self.session.navigate(route); 2077 } 2078 2079 // flip the shown issue's status by re-emitting its event, then reload the 2080 // page rooted at the issue so the view reflects the change. this is the 2081 // terminal path; the web posts the button's form to the status route. 2082 fn toggleStatus(self: *This, allocator: std.mem.Allocator, index: usize) !void { 2083 if (comptime !has_status) return; 2084 if (comptime wasm) return; 2085 const io = self.session.io orelse return; 2086 const src = self.data.repo_source orelse return; 2087 const sel = self.detailed_index[index] orelse return; 2088 const entry = self.window(index).items[sel]; 2089 const author = (try self.session.eventAuthor()) orelse return; 2090 2091 const status: Status = switch (entryStatus(entry)) { 2092 .open => .closed, 2093 .closed => .open, 2094 }; 2095 2096 var id_bytes: [evt.event_id_size]u8 = undefined; 2097 _ = try std.fmt.hexToBytes(&id_bytes, entry.id[0 .. evt.event_id_size * 2]); 2098 2099 switch (src.repo_kind) { 2100 inline else => |repo_kind| { 2101 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2102 defer any_repo.deinit(io, allocator); 2103 switch (any_repo) { 2104 inline else => |*repo| try Event.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .status = status }, author), 2105 } 2106 }, 2107 } 2108 2109 const route = listRoute(self.data.identity, status, "", entry.id) orelse return; 2110 try self.session.navigate(route); 2111 } 2112 2113 // arrow keys move the tag selection; at the flow's edges focus crosses to 2114 // the neighboring widgets. 2115 fn tagsInput(self: *This, index: usize, key: Key, root_focus: *Focus) !void { 2116 const tf = self.tagFlow(index) orelse return; 2117 const cid = tf.focus.child_id orelse return; 2118 const cur = tf.indexOfFocusId(cid) orelse return; 2119 const count = tf.text_boxes.items.len; 2120 switch (key) { 2121 .arrow_left => if (cur > 0) self.focusTag(index, tf, root_focus, cur - 1) else try self.focusList(index, root_focus), 2122 .arrow_right => if (cur + 1 < count) self.focusTag(index, tf, root_focus, cur + 1), 2123 .arrow_up => if (tf.rowStep(cur, false)) |i| { 2124 self.focusTag(index, tf, root_focus, i); 2125 } else { 2126 _ = self.moveDetailVertical(index, root_focus, false); 2127 }, 2128 .arrow_down => if (tf.rowStep(cur, true)) |i| { 2129 self.focusTag(index, tf, root_focus, i); 2130 } else { 2131 _ = self.moveDetailVertical(index, root_focus, true); 2132 }, 2133 .home => self.focusTag(index, tf, root_focus, 0), 2134 .end => self.focusTag(index, tf, root_focus, count - 1), 2135 else => {}, 2136 } 2137 } 2138 2139 const title_child_index: usize = 0; 2140 const tags_child_index: usize = 2; 2141 2142 // the tool row's first child after the spacer that pushes it right 2143 const first_in_row_index: usize = 1; 2144 2145 fn tagFlow(self: *This, index: usize) ?*ui.TagFlow { 2146 const inner = self.detailInner(index); 2147 if (inner.children.count() <= tags_child_index) return null; 2148 return switch (inner.children.values()[tags_child_index].widget) { 2149 .tag_flow => |*tf| tf, 2150 else => null, 2151 }; 2152 } 2153 2154 fn tagsFocused(self: *This, index: usize) bool { 2155 const inner = self.detailInner(index); 2156 const cid = inner.getFocus().child_id orelse return false; 2157 return inner.children.getIndex(cid) == tags_child_index and self.tagFlow(index) != null; 2158 } 2159 2160 fn toolRowFocused(self: *This, index: usize) bool { 2161 const outer = self.detailOuter(index); 2162 const cid = outer.getFocus().child_id orelse return false; 2163 return outer.children.getIndex(cid) == tool_row_index; 2164 } 2165 2166 fn focusTag(self: *This, index: usize, tf: *ui.TagFlow, root_focus: *Focus, item: usize) void { 2167 root_focus.setFocus(tf.text_boxes.items[item].getFocus().id); 2168 // keep the tag visible on the terminal: its rect offset by the flow's 2169 // position in the pane. 2170 if (self.session.is_terminal and item < tf.rects.items.len) { 2171 if (self.detailInner(index).children.values()[tags_child_index].rect) |flow_rect| { 2172 var rect = tf.rects.items[item]; 2173 rect.x += flow_rect.x; 2174 rect.y += flow_rect.y; 2175 self.detailScroll(index).scrollToRect(rect); 2176 } 2177 } 2178 } 2179 2180 fn titleFocused(self: *This, index: usize) bool { 2181 const inner = self.detailInner(index); 2182 const cid = inner.getFocus().child_id orelse return false; 2183 return inner.children.getIndex(cid) == title_child_index; 2184 } 2185 2186 fn authorFocused(self: *This, index: usize) bool { 2187 const inner = self.detailInner(index); 2188 const cid = inner.getFocus().child_id orelse return false; 2189 return cid == self.author_id[index]; 2190 } 2191 2192 fn descriptionFocused(self: *This, index: usize) bool { 2193 const cid = self.detailInner(index).getFocus().child_id orelse return false; 2194 return cid == self.description_id[index]; 2195 } 2196 2197 // return to the tool row's last-focused button (the header when the 2198 // description page shows no row). 2199 fn focusToolRow(self: *This, index: usize, root_focus: *Focus) void { 2200 const cid = self.toolRow(index).getFocus().child_id orelse return self.focusHeader(root_focus); 2201 root_focus.setFocus(cid); 2202 } 2203 2204 // enter the detail pane. an empty pane (no issues) can't be entered. 2205 fn focusDetail(self: *This, index: usize, root_focus: *Focus) !void { 2206 if (self.detailInner(index).children.count() == 0) return; 2207 root_focus.setFocus(self.detailOuter(index).getFocus().id); 2208 } 2209 2210 // return to the list. 2211 fn focusList(self: *This, index: usize, root_focus: *Focus) !void { 2212 root_focus.setFocus(self.listScroll(index).getFocus().id); 2213 } 2214 2215 // cross to the header tabs above the stack. 2216 fn focusHeader(self: *This, root_focus: *Focus) void { 2217 root_focus.setFocus(self.box.children.keys()[header_index]); 2218 } 2219 2220 pub fn clearGrid(self: *This) void { 2221 self.box.clearGrid(); 2222 } 2223 2224 pub fn getGrid(self: This) ?Grid { 2225 return self.box.getGrid(); 2226 } 2227 2228 pub fn getFocus(self: *This) *Focus { 2229 return self.box.getFocus(); 2230 } 2231 2232 // for the parent's "scroll up at the top jumps to the header" check: at the 2233 // top only while the header tabs hold focus, so up from the split 2234 // crosses into the tabs first. 2235 pub fn getSelectedIndex(self: *This) ?usize { 2236 return if (self.headerActive()) 0 else 1; 2237 } 2238 }; 2239 } 2240