╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ thread.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1380 try Comment.appendWindowNav(allocator, inner, self.session, self.data.identity, kind, &page.selected.comment.event.thread_id, &page.selected.id, page.replies); 1381 1382 var spacer = try Spacer.init(allocator); 1383 errdefer spacer.deinit(allocator); 1384 try inner.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 1385 1386 inner.getFocus().child_id = inner.children.keys()[tool_row_index + 1]; 1387 const sc = self.detailScroll(index); 1388 sc.x = 0; 1389 sc.y = 0; 1390 sc.getFocus().version +%= 1; 1391 return; 1392 } 1393 1394 if (description_page) { 1395 // the back link, in the title slot so the pane's input handling 1396 // applies to it unchanged. 1397 var back_label_buf: ["← back to ".len + thread_name.len]u8 = undefined; 1398 const back_label = try std.fmt.bufPrint(&back_label_buf, "← back to {s}", .{thread_name}); 1399 var tb = try wgt.TextBox.init(allocator, back_label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1400 errdefer tb.deinit(allocator); 1401 tb.getFocus().focusable = true; 1402 tb.getFocus().kind = .{ .custom = try listLink(self.session.page_arena, self.data.identity, entryStatus(entry), "", entry.id) }; 1403 try inner.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1404 self.title_id[index] = tb.getFocus().id; 1405 } else { 1406 // the thread's title as a focusable word-wrapped text box. 1407 self.title_id[index] = blk: { 1408 var tb = try wgt.TextBox.init(allocator, entry.record.event.title, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .word, .label = " title " }); 1409 errdefer tb.deinit(allocator); 1410 tb.getFocus().focusable = true; 1411 try inner.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1412 break :blk tb.getFocus().id; 1413 }; 1414 1415 self.author_id[index] = blk: { 1416 var tb = try ui.authorBox(allocator, self.session.page_arena, entry.author); 1417 errdefer tb.deinit(allocator); 1418 try inner.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1419 break :blk tb.getFocus().id; 1420 }; 1421 1422 // the thread's tags, each linking to this status's list filtered to 1423 // that tag. 1424 { 1425 var items: std.ArrayList(TagFlow.Item) = .empty; 1426 defer items.deinit(allocator); 1427 var tag_iter = Event.tagIterator(entry.record.event.tags); 1428 while (tag_iter.next()) |tag| { 1429 if (tag.len == 0) continue; 1430 try items.append(allocator, .{ .text = tag, .link = try tagLink(self.session.page_arena, self.data.identity, entryStatus(entry), tag) }); 1431 } 1432 if (items.items.len > 0) { 1433 var tf = try TagFlow.init(allocator); 1434 errdefer tf.deinit(allocator); 1435 try tf.setItems(allocator, items.items); 1436 try inner.children.put(allocator, tf.getFocus().id, .{ .widget = .{ .tag_flow = tf }, .rect = null, .min_size = null }); 1437 } 1438 } 1439 } 1440 1441 // the description as a focusable word-wrapped text box. past the limit 1442 // it's cut at the last whole line and links to the /description page, 1443 // which shows the whole thing. 1444 self.description_id[index] = blk: { 1445 const whole = entry.record.event.description; 1446 const cut_short = !description_page and whole.len > Data.max_description_size; 1447 const shown = if (cut_short) 1448 std.mem.trimEnd(u8, whole[0 .. std.mem.lastIndexOfScalar(u8, whole[0..Data.max_description_size], '\n') orelse 0], " \t\r\n") 1449 else if (whole.len == 0) 1450 "(no description)" 1451 else 1452 whole; 1453 var tb = try wgt.TextBox.init(allocator, shown, .{ 1454 .border_style = .single, 1455 .rounded_corners = true, 1456 .wrap_kind = .word, 1457 .label = " description ", 1458 .bottom_label = if (cut_short) " click or press enter to see more " else "", 1459 }); 1460 errdefer tb.deinit(allocator); 1461 tb.getFocus().focusable = true; 1462 if (cut_short) { 1463 const route = ui.RoutablePage.repoThreadDescriptionRoute(kind, self.data.identity, entry.id) orelse return error.RouteTooLong; 1464 tb.getFocus().kind = .{ .custom = try std.fmt.allocPrint(self.session.page_arena.allocator(), "a:{s}", .{try route.toUrl(self.session.page_arena)}) }; 1465 } 1466 try inner.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1467 break :blk tb.getFocus().id; 1468 }; 1469 1470 if (supports_drafts and entryDraft(entry)) { 1471 inner.getFocus().child_id = inner.children.keys()[tool_row_index + 1]; 1472 const sc = self.detailScroll(index); 1473 sc.x = 0; 1474 sc.y = 0; 1475 sc.getFocus().version +%= 1; 1476 return; 1477 } 1478 1479 const parent_route = ui.RoutablePage.repoThreadCommentsRoute(kind, self.data.identity, entry.id, 0) orelse return error.RouteTooLong; 1480 try Attachment.appendRows(allocator, inner, self.session, self.data.identity, try parent_route.toUrl(self.session.page_arena), entry.attachments); 1481 1482 if (!description_page) { 1483 var reply = try Comment.linkBox(allocator, self.session, "new comment", ui.RoutablePage.repoThreadCommentNewRoute(kind, self.data.identity, entry.id, "") orelse return error.RouteTooLong); 1484 errdefer reply.deinit(allocator); 1485 try inner.children.put(allocator, reply.getFocus().id, .{ .widget = .{ .text_box = reply }, .rect = null, .min_size = null }); 1486 1487 try Comment.appendCount(allocator, inner, entry.comments.count, "comment", "comments"); 1488 for (entry.comments.comments) |comment| try Comment.appendComment(allocator, inner, self.session, self.data.identity, kind, comment); 1489 try Comment.appendWindowNav(allocator, inner, self.session, self.data.identity, kind, entry.id, null, entry.comments); 1490 1491 var spacer = try Spacer.init(allocator); 1492 errdefer spacer.deinit(allocator); 1493 try inner.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 1494 } 1495 1496 // select the title by default 1497 if (self.title_id[index]) |id| inner.getFocus().child_id = id; 1498 1499 // reset the scroll to the top for the newly-shown thread: directly on the 1500 // terminal (the wasm offset), and via a version bump on the web (so the 1501 // renderer's scroll id changes and JS drops the preserved position). 1502 const sc = self.detailScroll(index); 1503 sc.x = 0; 1504 sc.y = 0; 1505 sc.getFocus().version +%= 1; 1506 } 1507 1508 pub fn input(self: *This, allocator: std.mem.Allocator, raw_key: Key, root_focus: *Focus) !void { 1509 // wheel input moves focus like the arrows 1510 const key: Key = if (raw_key == .mouse and raw_key.mouse.action == .scroll) 1511 (if (raw_key.mouse.action.scroll == .up) .arrow_up else .arrow_down) 1512 else 1513 raw_key; 1514 1515 if (self.headerActive()) { 1516 // down from the tabs re-enters the stack if the selected view has 1517 // something to focus; other keys move the tabs. 1518 if (inp.vertDirection(key) == .down) { 1519 const enterable = if (self.viewStack().getSelected()) |selected| switch (selected.*) { 1520 .tag_flow => |*tf| tf.text_boxes.items.len > 0, 1521 // nothing focusable in the unauthorized view 1522 .unauthorized => false, 1523 else => true, 1524 } else false; 1525 if (enterable) root_focus.setFocus(self.box.children.keys()[stack_index]); 1526 } else { 1527 try self.header().input(allocator, key, root_focus); 1528 } 1529 return; 1530 } 1531 if (self.tagsViewActive()) { 1532 self.tagsViewInput(key, root_focus); 1533 return; 1534 } 1535 if (self.formViewActive()) { 1536 if (self.data.view == .remove) { 1537 try self.removeFormInput(allocator, key, root_focus); 1538 } else if (supports_conflicts and self.data.view == .resolve) { 1539 try self.resolveInput(allocator, key, root_focus); 1540 } else if (self.data.view == .new_comment or self.data.view == .edit_comment) { 1541 try self.commentFormInput(allocator, key, root_focus); 1542 } else { 1543 try self.formInput(allocator, key, root_focus); 1544 } 1545 return; 1546 } 1547 const i = self.selectedSplitIndex() orelse return; 1548 if (self.detailActive(i)) { 1549 try self.detailInput(allocator, i, key, root_focus); 1550 } else { 1551 self.listInput(i, key, root_focus); 1552 } 1553 } 1554 1555 // arrow keys move the tag selection; up from the top row crosses to the 1556 // header tabs. 1557 fn tagsViewInput(self: *This, key: Key, root_focus: *Focus) void { 1558 const tf = self.tagsView(); 1559 const cid = tf.focus.child_id orelse return; 1560 const cur = tf.indexOfFocusId(cid) orelse return; 1561 const count = tf.text_boxes.items.len; 1562 switch (key) { 1563 .arrow_left => if (cur > 0) root_focus.setFocus(tf.text_boxes.items[cur - 1].getFocus().id), 1564 .arrow_right => if (cur + 1 < count) root_focus.setFocus(tf.text_boxes.items[cur + 1].getFocus().id), 1565 .arrow_up => if (tf.rowStep(cur, false)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id) else self.focusHeader(root_focus), 1566 .arrow_down => if (tf.rowStep(cur, true)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id), 1567 .home => root_focus.setFocus(tf.text_boxes.items[0].getFocus().id), 1568 .end => root_focus.setFocus(tf.text_boxes.items[count - 1].getFocus().id), 1569 else => {}, 1570 } 1571 } 1572 1573 fn listInput(self: *This, index: usize, key: Key, root_focus: *Focus) void { 1574 // up/down (and the scroll wheel) move the selection a row; page up/down 1575 // jump a fixed amount. right/Enter cross into the detail pane. up from 1576 // the top row crosses into the header tabs. 1577 if (inp.rowDelta(key, @intCast(self.listBox(index).children.count()))) |delta| { 1578 const lb = self.listBox(index); 1579 const at_top = if (lb.getFocus().child_id) |cid| lb.children.getIndex(cid) == 0 else true; 1580 if (delta < 0 and at_top) return self.focusHeader(root_focus); 1581 moveRowFocus(lb, self.listScroll(index), root_focus, delta); 1582 return; 1583 } 1584 switch (key) { 1585 .enter, .arrow_right => self.focusDetail(index, root_focus), 1586 else => {}, 1587 } 1588 } 1589 1590 fn detailInput(self: *This, allocator: std.mem.Allocator, index: usize, key: Key, root_focus: *Focus) !void { 1591 switch (key) { 1592 .page_up => { 1593 self.pageDetail(index, root_focus, -10); 1594 return; 1595 }, 1596 .page_down => { 1597 self.pageDetail(index, root_focus, 10); 1598 return; 1599 }, 1600 .home => { 1601 self.jumpDetail(index, root_focus, false); 1602 return; 1603 }, 1604 .end => { 1605 self.jumpDetail(index, root_focus, true); 1606 return; 1607 }, 1608 else => {}, 1609 } 1610 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)) { 1611 self.commentInput(index, key, root_focus); 1612 return; 1613 } 1614 if (self.toolRowFocused(index, root_focus)) { 1615 try self.toolRowInput(allocator, index, key, root_focus); 1616 return; 1617 } 1618 1619 const child_index = self.focusedDetailChild(index, root_focus) orelse return; 1620 const inner = self.detailInner(index); 1621 const child_id = inner.children.keys()[child_index]; 1622 const child = &inner.children.values()[child_index].widget; 1623 1624 if (child_id == self.title_id[index]) { 1625 switch (key) { 1626 .arrow_left => self.focusList(index, root_focus), 1627 .arrow_up => if (!self.moveDetailVertical(index, root_focus, false)) self.focusToolRow(index, root_focus), 1628 .arrow_down => _ = self.moveDetailVertical(index, root_focus, true), 1629 else => {}, 1630 } 1631 return; 1632 } 1633 if (child_id == self.author_id[index]) { 1634 // the author's a: link is followed by the host; arrows cross to 1635 // neighboring widgets. 1636 switch (key) { 1637 .arrow_left => self.focusList(index, root_focus), 1638 .arrow_up => _ = self.moveDetailVertical(index, root_focus, false), 1639 .arrow_down => _ = self.moveDetailVertical(index, root_focus, true), 1640 else => {}, 1641 } 1642 return; 1643 } 1644 if (child_id == self.description_id[index]) { 1645 self.descriptionInput(index, key, root_focus); 1646 return; 1647 } 1648 1649 switch (child.*) { 1650 .tag_flow => |*tf| { 1651 self.tagsInput(index, child_index, tf, key, root_focus); 1652 return; 1653 }, 1654 .copyable_text => |*copyable| { 1655 switch (key) { 1656 .arrow_up => if (!self.moveDetailVertical(index, root_focus, false)) self.focusToolRow(index, root_focus), 1657 .arrow_down => _ = self.moveDetailVertical(index, root_focus, true), 1658 .arrow_left => if (copyable.selected == 0) 1659 self.focusList(index, root_focus) 1660 else 1661 try copyable.input(allocator, key, root_focus), 1662 else => try copyable.input(allocator, key, root_focus), 1663 } 1664 return; 1665 }, 1666 .box => |*row| if (Attachment.removeId(row)) |attachment_id| { 1667 const remove_id = row.children.keys()[1]; 1668 switch (key) { 1669 .arrow_left => if (!self.moveCommentHorizontal(index, root_focus, false)) self.focusList(index, root_focus), 1670 .arrow_right => _ = self.moveCommentHorizontal(index, root_focus, true), 1671 .arrow_up => _ = self.moveDetailVertical(index, root_focus, false), 1672 .arrow_down => _ = self.moveDetailVertical(index, root_focus, true), 1673 .enter => if (row.getFocus().child_id == remove_id) try self.removeEvent(allocator, .attach, attachment_id), 1674 .mouse => |mouse| if (inp.leftClickOn(root_focus, remove_id, mouse)) try self.removeEvent(allocator, .attach, attachment_id), 1675 else => {}, 1676 } 1677 return; 1678 }, 1679 else => {}, 1680 } 1681 self.commentInput(index, key, root_focus); 1682 } 1683 1684 // enter or a click runs the primary action; links are handled by the host 1685 fn toolRowInput(self: *This, allocator: std.mem.Allocator, index: usize, key: Key, root_focus: *Focus) !void { 1686 const row = self.toolRow(index); 1687 const cur = if (row.getFocus().child_id) |cid| row.children.getIndex(cid) orelse return else return; 1688 const child_id = row.children.keys()[cur]; 1689 const on_primary = switch (row.children.values()[cur].widget.getFocus().kind) { 1690 .custom => |action| std.mem.eql(u8, action, "submit"), 1691 else => false, 1692 }; 1693 switch (key) { 1694 .arrow_left => { 1695 var previous = cur; 1696 while (previous > 0) { 1697 previous -= 1; 1698 if (!row.children.values()[previous].widget.getFocus().focusable) continue; 1699 root_focus.setFocus(row.children.keys()[previous]); 1700 return; 1701 } 1702 self.focusList(index, root_focus); 1703 }, 1704 .arrow_right => { 1705 var next = cur + 1; 1706 while (next < row.children.count()) : (next += 1) { 1707 if (!row.children.values()[next].widget.getFocus().focusable) continue; 1708 root_focus.setFocus(row.children.keys()[next]); 1709 return; 1710 } 1711 }, 1712 .arrow_up => self.focusHeader(root_focus), 1713 .arrow_down => _ = self.moveDetailVertical(index, root_focus, true), 1714 .enter => if (on_primary) try self.primaryAction(allocator, index), 1715 .mouse => |mouse| if (on_primary and inp.leftClickOn(root_focus, child_id, mouse)) try self.primaryAction(allocator, index), 1716 else => {}, 1717 } 1718 } 1719 1720 fn primaryAction(self: *This, allocator: std.mem.Allocator, index: usize) !void { 1721 const selected = self.detailed_index[index] orelse return; 1722 const entry = self.window(index).items[selected]; 1723 if (supports_drafts and entryDraft(entry)) 1724 try self.publishDraft(allocator, index) 1725 else 1726 try self.toggleStatus(allocator, index); 1727 } 1728 1729 fn publishDraft(self: *This, allocator: std.mem.Allocator, index: usize) !void { 1730 if (!supports_drafts or comptime wasm) return; 1731 const selected = self.detailed_index[index] orelse return; 1732 const entry = self.window(index).items[selected]; 1733 if (!entryDraft(entry) or !entry.revision_ready) return; 1734 try self.data.publishDraft(self.session, allocator, entry.id); 1735 const route = listRoute(self.data.identity, @enumFromInt(0), "", entry.id) orelse return; 1736 try self.session.navigate(route); 1737 } 1738 1739 fn descriptionInput(self: *This, index: usize, key: Key, root_focus: *Focus) void { 1740 const sc = self.detailScroll(index); 1741 switch (key) { 1742 .arrow_left => return self.focusList(index, root_focus), 1743 // the terminal moves through the description one line at a time; 1744 // the web lets focus changes drive its native scroll. 1745 .arrow_up => { 1746 if (!self.session.is_terminal) { 1747 _ = self.moveDetailVertical(index, root_focus, false); 1748 return; 1749 } 1750 if (self.focusAdjacentVisible(index, root_focus, false)) return; 1751 const before = sc.y; 1752 sc.y -= 1; 1753 sc.clampToContent(); 1754 if (sc.y == before) _ = self.moveDetailVertical(index, root_focus, false); 1755 return; 1756 }, 1757 .arrow_down => { 1758 if (!self.session.is_terminal) { 1759 _ = self.moveDetailVertical(index, root_focus, true); 1760 return; 1761 } 1762 if (self.focusAdjacentVisible(index, root_focus, true)) return; 1763 const before = sc.y; 1764 sc.y += 1; 1765 sc.clampToContent(); 1766 if (sc.y == before) _ = self.moveDetailVertical(index, root_focus, true); 1767 return; 1768 }, 1769 else => return, 1770 } 1771 } 1772 1773 fn commentInput(self: *This, index: usize, key: Key, root_focus: *Focus) void { 1774 switch (key) { 1775 .arrow_left => if (!self.moveCommentHorizontal(index, root_focus, false)) return self.focusList(index, root_focus), 1776 .arrow_right => _ = self.moveCommentHorizontal(index, root_focus, true), 1777 .arrow_up => _ = self.moveDetailVertical(index, root_focus, false), 1778 .arrow_down => _ = self.moveDetailVertical(index, root_focus, true), 1779 else => return, 1780 } 1781 } 1782 1783 // page the detail scroll, then focus the first or last visible widget. 1784 fn pageDetail(self: *This, index: usize, root_focus: *Focus, delta: isize) void { 1785 if (!self.session.is_terminal) return; 1786 const sc = self.detailScroll(index); 1787 sc.y += delta; 1788 sc.clampToContent(); 1789 self.focusVisible(index, root_focus, delta > 0); 1790 } 1791 1792 // jump to the detail's first or last focusable widget. the terminal pins 1793 // its synthetic scroll too; the browser scrolls to the new focus itself. 1794 fn jumpDetail(self: *This, index: usize, root_focus: *Focus, to_end: bool) void { 1795 if (self.session.is_terminal) { 1796 const sc = self.detailScroll(index); 1797 sc.y = if (to_end) std.math.maxInt(isize) else 0; 1798 sc.clampToContent(); 1799 self.focusVisible(index, root_focus, to_end); 1800 } else { 1801 self.focusDetailEdge(index, root_focus, to_end); 1802 } 1803 } 1804 1805 fn focusDetailEdge(self: *This, index: usize, root_focus: *Focus, last: bool) void { 1806 const focus = self.detailInner(index).getFocus(); 1807 var chosen: ?usize = null; 1808 for (focus.children.keys(), focus.children.values()) |id, child| { 1809 if (!child.focus.focusable) continue; 1810 chosen = id; 1811 if (!last) break; 1812 } 1813 if (chosen) |id| root_focus.setFocus(id); 1814 } 1815 1816 fn detailRectVisible(self: *This, index: usize, rect: layout.IRect) bool { 1817 const sc = self.detailScroll(index); 1818 const viewport = sc.grid orelse return false; 1819 const top = sc.y; 1820 const bottom = top + @as(isize, @intCast(viewport.size.height - sc.bar_h)); 1821 return rect.y + @as(isize, @intCast(rect.size.height)) > top and rect.y < bottom; 1822 } 1823 1824 // focus the top- or bottom-most focusable widget intersecting the viewport. 1825 fn focusVisible(self: *This, index: usize, root_focus: *Focus, last: bool) void { 1826 const inner = self.detailInner(index); 1827 var offset: usize = 0; 1828 while (offset < inner.children.count()) : (offset += 1) { 1829 const child_index = if (last) inner.children.count() - 1 - offset else offset; 1830 if (self.focusVisibleChild(index, child_index, root_focus, last)) return; 1831 } 1832 } 1833 1834 fn focusVisibleChild(self: *This, index: usize, child_index: usize, root_focus: *Focus, last: bool) bool { 1835 const child = &self.detailInner(index).children.values()[child_index]; 1836 const child_rect = child.rect orelse return false; 1837 var chosen: ?usize = null; 1838 if (child.widget.getFocus().focusable and self.detailRectVisible(index, child_rect)) { 1839 const id = self.detailInner(index).children.keys()[child_index]; 1840 chosen = id; 1841 if (!last) { 1842 root_focus.setFocus(id); 1843 return true; 1844 } 1845 } 1846 1847 for (child.widget.getFocus().children.keys(), child.widget.getFocus().children.values()) |id, focus_child| { 1848 if (!focus_child.focus.focusable) continue; 1849 const rect: layout.IRect = .{ 1850 .x = child_rect.x + @as(isize, @intCast(focus_child.rect.x)), 1851 .y = child_rect.y + @as(isize, @intCast(focus_child.rect.y)), 1852 .size = focus_child.rect.size, 1853 }; 1854 if (!self.detailRectVisible(index, rect)) continue; 1855 chosen = id; 1856 if (!last) break; 1857 } 1858 if (chosen) |id| { 1859 root_focus.setFocus(id); 1860 return true; 1861 } 1862 return false; 1863 } 1864 1865 fn focusAdjacentVisible(self: *This, index: usize, root_focus: *Focus, down: bool) bool { 1866 const inner = self.detailInner(index); 1867 var next = self.focusedDetailChild(index, root_focus) orelse return false; 1868 while (true) { 1869 if (down) { 1870 next += 1; 1871 if (next >= inner.children.count()) return false; 1872 } else { 1873 if (next == 0) return false; 1874 next -= 1; 1875 } 1876 if (self.focusVisibleChild(index, next, root_focus, !down)) { 1877 self.focusDetailChild(index, next, !down, root_focus); 1878 return true; 1879 } 1880 } 1881 } 1882 1883 fn focusedDetailChild(self: *This, index: usize, root_focus: *Focus) ?usize { 1884 const inner = self.detailInner(index); 1885 var id = root_focus.grandchild_id orelse return null; 1886 while (inner.getFocus().children.get(id)) |child| { 1887 if (child.parent_id == inner.getFocus().id) return inner.children.getIndex(id); 1888 id = child.parent_id; 1889 } 1890 return null; 1891 } 1892 1893 fn detailChildFocusable(self: *This, index: usize, child_index: usize) bool { 1894 const child = &self.detailInner(index).children.values()[child_index].widget; 1895 const focus = child.getFocus(); 1896 if (focus.focusable) return true; 1897 for (focus.children.values()) |item| { 1898 if (item.focus.focusable) return true; 1899 } 1900 return false; 1901 } 1902 1903 fn focusDetailChild(self: *This, index: usize, child_index: usize, last: bool, root_focus: *Focus) void { 1904 const inner = self.detailInner(index); 1905 const child = &inner.children.values()[child_index]; 1906 var target = inner.children.keys()[child_index]; 1907 var rect = child.rect orelse return; 1908 switch (child.widget) { 1909 .repo_comment => |*comment| { 1910 if (comment.rowRect(last)) |row_rect| { 1911 rect.x += row_rect.x; 1912 rect.y += row_rect.y; 1913 rect.size = row_rect.size; 1914 } 1915 if (last) comment.focusBody(root_focus) else comment.focusMetadata(root_focus); 1916 }, 1917 .box => |*box| if (box.children.count() > 0) { 1918 target = if (child_index == tool_row_index) 1919 box.getFocus().child_id orelse return 1920 else 1921 box.children.keys()[if (last) box.children.count() - 1 else 0]; 1922 root_focus.setFocus(target); 1923 }, 1924 else => root_focus.setFocus(target), 1925 } 1926 const sc = self.detailScroll(index); 1927 if (sc.grid) |viewport| { 1928 const viewport_height = viewport.size.height - sc.bar_h; 1929 if (rect.size.height > viewport_height) { 1930 const bottom_aligned = rect.y + @as(isize, @intCast(rect.size.height - viewport_height)); 1931 sc.y = std.math.clamp(sc.y, rect.y, bottom_aligned); 1932 sc.clampToContent(); 1933 return; 1934 } 1935 } 1936 sc.scrollToRect(rect); 1937 } 1938 1939 fn moveDetailVertical(self: *This, index: usize, root_focus: *Focus, down: bool) bool { 1940 const inner = self.detailInner(index); 1941 const current = self.focusedDetailChild(index, root_focus) orelse return false; 1942 if (inner.children.values()[current].widget == .repo_comment) { 1943 const comment = &inner.children.values()[current].widget.repo_comment; 1944 if (down and !comment.bodyFocused()) { 1945 self.focusDetailChild(index, current, true, root_focus); 1946 return true; 1947 } 1948 if (!down and comment.bodyFocused()) { 1949 self.focusDetailChild(index, current, false, root_focus); 1950 return true; 1951 } 1952 } 1953 1954 var next = current; 1955 while (true) { 1956 if (down) { 1957 next += 1; 1958 if (next >= inner.children.count()) return false; 1959 } else { 1960 if (next == 0) return false; 1961 next -= 1; 1962 } 1963 if (!self.detailChildFocusable(index, next)) continue; 1964 self.focusDetailChild(index, next, !down, root_focus); 1965 return true; 1966 } 1967 } 1968 1969 fn moveCommentHorizontal(self: *This, index: usize, root_focus: *Focus, right: bool) bool { 1970 const inner = self.detailInner(index); 1971 const current = self.focusedDetailChild(index, root_focus) orelse return false; 1972 const child = &inner.children.values()[current]; 1973 if (child.widget == .repo_comment) { 1974 const moved = child.widget.repo_comment.moveHorizontal(root_focus, right); 1975 if (moved) if (child.rect) |rect| self.detailScroll(index).scrollToRect(rect); 1976 return moved; 1977 } 1978 const row = switch (child.widget) { 1979 .box => |*box| box, 1980 else => return false, 1981 }; 1982 const selected = row.getFocus().child_id orelse return false; 1983 const selected_index = row.children.getIndex(selected) orelse return false; 1984 const target = if (right) selected_index + 1 else selected_index -| 1; 1985 if (target == selected_index or target >= row.children.count()) return false; 1986 root_focus.setFocus(row.children.keys()[target]); 1987 if (child.rect) |rect| self.detailScroll(index).scrollToRect(rect); 1988 return true; 1989 } 1990 1991 // up/down (and tab/shift+tab) move between a form's fields; up from the 1992 // title crosses into the header tabs. the multiline description keeps 1993 // enter and any up/down that has a row to move to. 1994 fn formInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1995 const form = self.threadForm() orelse return; 1996 const cid = form.getFocus().child_id orelse return; 1997 const cur = form.children.getIndex(cid) orelse return; 1998 1999 if (cur == description_field_index) { 2000 const child = &form.children.values()[cur]; 2001 if (try multilineKeepsKey(&child.widget.text_input, allocator, key)) 2002 return child.widget.input(allocator, key, root_focus); 2003 } 2004 2005 switch (key) { 2006 .arrow_up, .back_tab => if (cur > 0) 2007 root_focus.setFocus(form.children.keys()[cur - 1]) 2008 else 2009 self.focusHeader(root_focus), 2010 .arrow_down, .tab => if (cur < submit_field_index) { 2011 root_focus.setFocus(form.children.keys()[cur + 1]); 2012 }, 2013 .enter => if (cur == submit_field_index) try self.submitForm(allocator), 2014 .mouse => |mouse| if (cur == submit_field_index) { 2015 const submit = &form.children.values()[cur].widget.submit_button; 2016 if (inp.leftClickOn(root_focus, submit.buttonId(), mouse)) try self.submitForm(allocator); 2017 }, 2018 else => try form.children.values()[cur].widget.input(allocator, key, root_focus), 2019 } 2020 } 2021 2022 fn commentFormInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2023 const form = self.threadForm() orelse return; 2024 const cid = form.getFocus().child_id orelse return; 2025 const cur = form.children.getIndex(cid) orelse return; 2026 const child = &form.children.values()[cur]; 2027 2028 if (cur == comment_body_field_index and try multilineKeepsKey(&child.widget.text_input, allocator, key)) 2029 return child.widget.input(allocator, key, root_focus); 2030 2031 switch (key) { 2032 .arrow_up, .back_tab => if (cur > comment_author_field_index) 2033 root_focus.setFocus(form.children.keys()[cur - 1]) 2034 else 2035 self.focusHeader(root_focus), 2036 .arrow_down, .tab => if (cur < comment_submit_field_index) 2037 root_focus.setFocus(form.children.keys()[cur + 1]), 2038 .enter => if (cur == comment_submit_field_index) try self.submitComment(allocator), 2039 .mouse => |mouse| if (cur == comment_submit_field_index) { 2040 const submit = &child.widget.submit_button; 2041 if (inp.leftClickOn(root_focus, submit.buttonId(), mouse)) try self.submitComment(allocator); 2042 }, 2043 else => try child.widget.input(allocator, key, root_focus), 2044 } 2045 } 2046 2047 fn removeFormInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2048 const form = self.removeForm() orelse return; 2049 const button_id = form.children.keys()[1]; 2050 switch (key) { 2051 .arrow_up, .back_tab => self.focusHeader(root_focus), 2052 .enter => try self.submitRemove(allocator), 2053 .mouse => |mouse| if (inp.leftClickOn(root_focus, button_id, mouse)) try self.submitRemove(allocator), 2054 else => {}, 2055 } 2056 } 2057 2058 // the multiline hunk inputs keep enter and any up/down that has a row to 2059 // move to; enter on a "use this" link is a navigation the host follows 2060 // before this runs. 2061 fn resolveInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2062 const form = self.threadForm() orelse return; 2063 const cid = form.getFocus().child_id orelse return; 2064 const cur = form.children.getIndex(cid) orelse return; 2065 const child = &form.children.values()[cur]; 2066 2067 if (child.widget == .text_input) { 2068 const ti = &child.widget.text_input; 2069 if (ti.options.multiline and try multilineKeepsKey(ti, allocator, key)) 2070 return child.widget.input(allocator, key, root_focus); 2071 } 2072 2073 const on_submit = child.widget == .submit_button; 2074 2075 switch (key) { 2076 .arrow_up, .back_tab => if (resolveStep(form, cur, false)) |i| self.focusResolveChild(form, i, root_focus) else self.focusHeader(root_focus), 2077 .arrow_down, .tab => if (resolveStep(form, cur, true)) |i| self.focusResolveChild(form, i, root_focus), 2078 .arrow_left, .arrow_right => switch (child.widget) { 2079 .box => |*row| { 2080 const row_cid = row.getFocus().child_id orelse return; 2081 const row_index = row.children.getIndex(row_cid) orelse return; 2082 if (key == .arrow_left) { 2083 if (row_index > 0) root_focus.setFocus(row.children.keys()[row_index - 1]); 2084 } else if (row_index + 1 < row.children.count()) { 2085 root_focus.setFocus(row.children.keys()[row_index + 1]); 2086 } 2087 }, 2088 else => try child.widget.input(allocator, key, root_focus), 2089 }, 2090 .enter => if (on_submit) try self.submitResolution(allocator), 2091 .mouse => |mouse| if (on_submit) { 2092 if (inp.leftClickOn(root_focus, child.widget.submit_button.buttonId(), mouse)) try self.submitResolution(allocator); 2093 }, 2094 else => try child.widget.input(allocator, key, root_focus), 2095 } 2096 } 2097 2098 // a multiline input keeps enter and any up/down that has a row to move to 2099 fn multilineKeepsKey(text_input: *wgt.TextInput, allocator: std.mem.Allocator, key: Key) !bool { 2100 return switch (key) { 2101 .enter => true, 2102 .arrow_up => !try text_input.cursorOnFirstRow(allocator), 2103 .arrow_down => !try text_input.cursorOnLastRow(allocator), 2104 else => false, 2105 }; 2106 } 2107 2108 // the neighboring focusable resolve-form child, skipping the spacer and 2109 // the blank gap rows 2110 fn resolveStep(form: *wgt.Box(Widget), cur: usize, down: bool) ?usize { 2111 var i = cur; 2112 while (true) { 2113 if (down) { 2114 i += 1; 2115 if (i >= form.children.count()) return null; 2116 } else { 2117 if (i == 0) return null; 2118 i -= 1; 2119 } 2120 switch (form.children.values()[i].widget) { 2121 .spacer, .text => continue, 2122 else => return i, 2123 } 2124 } 2125 } 2126 2127 // focus the resolve-form child at `i` (a version row focuses its selected 2128 // child) and keep it visible on the terminal 2129 fn focusResolveChild(self: *This, form: *wgt.Box(Widget), i: usize, root_focus: *Focus) void { 2130 const child = &form.children.values()[i]; 2131 switch (child.widget) { 2132 .box => |*row| root_focus.setFocus(row.getFocus().child_id orelse form.children.keys()[i]), 2133 else => root_focus.setFocus(form.children.keys()[i]), 2134 } 2135 if (self.session.is_terminal) { 2136 if (self.resolveScroll()) |sc| { 2137 if (child.rect) |rect| sc.scrollToRect(rect); 2138 } 2139 } 2140 } 2141 2142 // re-emit the conflicted thread's event with the resolved content; any 2143 // event on the thread settles its conflict entry. this is the terminal 2144 // path; the web posts the form to the resolve route. 2145 fn submitResolution(self: *This, allocator: std.mem.Allocator) !void { 2146 if (comptime wasm) return; 2147 const io = self.session.io orelse return; 2148 const src = self.data.repo_source orelse return; 2149 const entry = self.data.selectedThread() orelse return; 2150 const author = (try self.session.eventAuthor()) orelse return; 2151 const form = self.threadForm() orelse return; 2152 2153 // gather the inputs by name; the d<n> hunk inputs appear in chunk order 2154 var title: ?[]u8 = null; 2155 var tags: ?[]u8 = null; 2156 var hunks: std.ArrayList([]const u8) = .empty; 2157 defer { 2158 if (title) |t| allocator.free(t); 2159 if (tags) |t| allocator.free(t); 2160 for (hunks.items) |h| allocator.free(h); 2161 hunks.deinit(allocator); 2162 } 2163 for (form.children.values()) |*child| switch (child.widget) { 2164 .text_input => |*ti| { 2165 const text = try ti.text(allocator); 2166 errdefer allocator.free(text); 2167 if (std.mem.eql(u8, ti.options.name, "title")) { 2168 title = text; 2169 } else if (std.mem.eql(u8, ti.options.name, "tags")) { 2170 tags = text; 2171 } else { 2172 try hunks.append(allocator, text); 2173 } 2174 }, 2175 else => {}, 2176 }; 2177 2178 const id_bytes = try evt.parseEventId(entry.id); 2179 2180 switch (src.repo_kind) { 2181 inline else => |repo_kind| { 2182 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2183 defer any_repo.deinit(io, allocator); 2184 switch (any_repo) { 2185 inline else => |*repo| { 2186 var resolution: Event.Resolve = .{ 2187 .title = title, 2188 .tags = tags, 2189 .hunks = hunks.items, 2190 }; 2191 if (comptime @hasField(Event.Resolve, "theirs")) resolution.theirs = self.data.theirs_picks; 2192 const change = Event.Update{ .resolve = resolution }; 2193 Event.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, change, author) catch |err| switch (err) { 2194 // leave the form up for correction 2195 error.InvalidFields => return, 2196 else => |e| return e, 2197 }; 2198 }, 2199 } 2200 }, 2201 } 2202 2203 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2204 try self.session.navigate(route); 2205 } 2206 2207 // commit a new or edited comment and navigate to its permalink. this is the 2208 // terminal path; the web posts the form to its comment route. 2209 fn submitComment(self: *This, allocator: std.mem.Allocator) !void { 2210 if (comptime wasm) return; 2211 const io = self.session.io orelse return; 2212 const src = self.data.repo_source orelse return; 2213 const author = (try self.session.eventAuthor()) orelse return; 2214 const form = self.threadForm() orelse return; 2215 const body_input = &form.children.values()[comment_body_field_index].widget.text_input; 2216 const body = try body_input.text(allocator); 2217 defer allocator.free(body); 2218 if (!evt.Comment.fieldsValid(body)) return; 2219 2220 var thread_id: [evt.event_id_size * 2]u8 = undefined; 2221 @memcpy(&thread_id, self.data.selected_id); 2222 const parent_id = if (self.data.comment_id.len == 0) thread_id else blk: { 2223 var parent: [evt.event_id_size * 2]u8 = undefined; 2224 @memcpy(&parent, self.data.comment_id); 2225 break :blk parent; 2226 }; 2227 var event_id_hex: [evt.event_id_size * 2]u8 = undefined; 2228 const editing = self.data.view == .edit_comment; 2229 switch (src.repo_kind) { 2230 inline else => |repo_kind| { 2231 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2232 defer any_repo.deinit(io, allocator); 2233 switch (any_repo) { 2234 inline else => |*repo| if (editing) { 2235 const comment_id = evt.parseEventId(self.data.comment_id) catch return; 2236 try evt.Comment.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &thread_id, &comment_id, body, author); 2237 event_id_hex = std.fmt.bytesToHex(comment_id, .lower); 2238 } else { 2239 event_id_hex = try evt.Comment.create(repo_kind, repo.self_repo_opts, io, allocator, repo, &thread_id, &parent_id, body, author); 2240 }, 2241 } 2242 }, 2243 } 2244 2245 body_input.clear(allocator); 2246 const route = ui.RoutablePage.repoThreadCommentRoute(kind, self.data.identity, &thread_id, &event_id_hex, 0) orelse return; 2247 try self.session.navigate(route); 2248 } 2249 2250 // the terminal submit paths: the new form commits a new thread event, the 2251 // edit form re-emits the selected thread's event with the form's content. 2252 // the web posts the forms to their routes instead. 2253 fn submitForm(self: *This, allocator: std.mem.Allocator) !void { 2254 if (self.data.view == .edit) { 2255 try self.submitEditedThread(allocator); 2256 } else { 2257 try self.submitNewThread(allocator); 2258 } 2259 } 2260 2261 // commit the new thread to the repo's events branch and navigate to it. 2262 // this is the terminal path; the web posts the form to the thread route, 2263 // so the wasm side never runs (or compiles) the repo access below. 2264 fn submitNewThread(self: *This, allocator: std.mem.Allocator) !void { 2265 if (comptime wasm) return; 2266 const io = self.session.io orelse return; 2267 const src = self.data.repo_source orelse return; 2268 const author = (try self.session.eventAuthor()) orelse return; 2269 2270 const form = self.threadForm() orelse return; 2271 const title_input = &form.children.values()[title_field_index].widget.text_input; 2272 const tags_input = &form.children.values()[tags_field_index].widget.text_input; 2273 const description_input = &form.children.values()[description_field_index].widget.text_input; 2274 2275 const title = try title_input.text(allocator); 2276 defer allocator.free(title); 2277 const tags = try tags_input.text(allocator); 2278 defer allocator.free(tags); 2279 const description = try description_input.text(allocator); 2280 defer allocator.free(description); 2281 2282 if (!Event.fieldsValid(title, tags)) return; 2283 2284 if (comptime supports_drafts) { 2285 const event_id_hex = try Data.createDraft(self.data, self.session, allocator, title, tags, description); 2286 title_input.clear(allocator); 2287 tags_input.clear(allocator); 2288 description_input.clear(allocator); 2289 const route = ui.RoutablePage.repoThreadCommentsRoute(kind, self.data.identity, &event_id_hex, 0) orelse return; 2290 try self.session.navigate(route); 2291 return; 2292 } 2293 2294 var id_bytes: [evt.event_id_size]u8 = undefined; 2295 io.random(&id_bytes); 2296 const event_id_hex = std.fmt.bytesToHex(id_bytes, .lower); 2297 2298 const event = evt.EventWithId{ 2299 .id = event_id_hex, 2300 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 2301 .author = author, 2302 .event = @unionInit(evt.Event, @tagName(kind), Event{ 2303 .title = title, 2304 .description = description, 2305 .tags = tags, 2306 }), 2307 }; 2308 2309 switch (src.repo_kind) { 2310 inline else => |repo_kind| { 2311 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2312 defer any_repo.deinit(io, allocator); 2313 switch (any_repo) { 2314 inline else => |*repo| try evt.consume(.repo, repo_kind, repo.self_repo_opts, io, allocator, repo, evt.events_ref, &.{event}), 2315 } 2316 }, 2317 } 2318 2319 // wipe the form so a return visit starts fresh 2320 title_input.clear(allocator); 2321 tags_input.clear(allocator); 2322 description_input.clear(allocator); 2323 2324 const route = listRoute(self.data.identity, @enumFromInt(0), "", &event_id_hex) orelse return; 2325 try self.session.navigate(route); 2326 } 2327 2328 // re-emit the selected thread's event with the form's content (status 2329 // preserved), then reload the thread's page. this is the terminal path; 2330 // the web posts the form to the edit route. 2331 fn submitEditedThread(self: *This, allocator: std.mem.Allocator) !void { 2332 if (comptime wasm) return; 2333 const io = self.session.io orelse return; 2334 const entry = self.data.selectedThread() orelse return; 2335 2336 const form = self.threadForm() orelse return; 2337 const title_input = &form.children.values()[title_field_index].widget.text_input; 2338 const tags_input = &form.children.values()[tags_field_index].widget.text_input; 2339 const description_input = &form.children.values()[description_field_index].widget.text_input; 2340 2341 const title = try title_input.text(allocator); 2342 defer allocator.free(title); 2343 const tags = try tags_input.text(allocator); 2344 defer allocator.free(tags); 2345 const description = try description_input.text(allocator); 2346 defer allocator.free(description); 2347 2348 if (!Event.fieldsValid(title, tags)) return; 2349 2350 if (comptime supports_drafts) { 2351 if (entryDraft(entry.*)) { 2352 try self.data.editDraft(self.session, allocator, entry.id, title, tags, description); 2353 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2354 try self.session.navigate(route); 2355 return; 2356 } 2357 } 2358 2359 const id_bytes = try evt.parseEventId(entry.id); 2360 const src = self.data.repo_source orelse return; 2361 const author = (try self.session.eventAuthor()) orelse return; 2362 2363 switch (src.repo_kind) { 2364 inline else => |repo_kind| { 2365 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2366 defer any_repo.deinit(io, allocator); 2367 switch (any_repo) { 2368 inline else => |*repo| if (has_status) 2369 try Event.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 2370 .title = title, 2371 .tags = tags, 2372 .description = description, 2373 } }, author) 2374 else 2375 try Event.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, title, tags, description, author), 2376 } 2377 }, 2378 } 2379 2380 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2381 try self.session.navigate(route); 2382 } 2383 2384 fn submitRemove(self: *This, allocator: std.mem.Allocator) !void { 2385 const comment = self.data.comment_id.len != 0; 2386 const event_id = if (comment) self.data.comment_id else self.data.selected_id; 2387 const id = evt.parseEventId(event_id) catch return; 2388 try self.removeEvent(allocator, if (comment) .comment else kind, id); 2389 } 2390 2391 fn removeEvent(self: *This, allocator: std.mem.Allocator, event_kind: evt.EventKind, id: [evt.event_id_size]u8) !void { 2392 if (comptime wasm) return; 2393 2394 if (comptime supports_drafts) { 2395 const selected = self.data.selectedThread() orelse return; 2396 if (event_kind == kind and entryDraft(selected.*)) { 2397 try Data.removeDraft(self.session, allocator, &id); 2398 const route = draftsRoute(self.data.identity) orelse return; 2399 try self.session.navigate(route); 2400 return; 2401 } 2402 } 2403 2404 const io = self.session.io orelse return; 2405 const src = self.data.repo_source orelse return; 2406 const author = (try self.session.eventAuthor()) orelse return; 2407 2408 switch (src.repo_kind) { 2409 inline else => |repo_kind| { 2410 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2411 defer any_repo.deinit(io, allocator); 2412 switch (any_repo) { 2413 inline else => |*repo| try evt.remove(.repo, repo_kind, repo.self_repo_opts, io, allocator, repo, &id, event_kind, author), 2414 } 2415 }, 2416 } 2417 2418 const id_hex = std.fmt.bytesToHex(id, .lower); 2419 const route = ui.RoutablePage.repoEventsRoute(self.data.identity, .removed, event_kind, &id_hex) orelse return; 2420 try self.session.navigate(route); 2421 } 2422 2423 // flip the shown thread's status by re-emitting its event, then reload the 2424 // page rooted at the thread so the view reflects the change. this is the 2425 // terminal path; the web posts the button's form to the status route. 2426 fn toggleStatus(self: *This, allocator: std.mem.Allocator, index: usize) !void { 2427 if (comptime !has_status) return; 2428 if (comptime wasm) return; 2429 const io = self.session.io orelse return; 2430 const src = self.data.repo_source orelse return; 2431 const sel = self.detailed_index[index] orelse return; 2432 const entry = self.window(index).items[sel]; 2433 const author = (try self.session.eventAuthor()) orelse return; 2434 2435 const status = (statusChange(entryStatus(entry)) orelse return).status; 2436 2437 const id_bytes = try evt.parseEventId(entry.id); 2438 2439 switch (src.repo_kind) { 2440 inline else => |repo_kind| { 2441 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2442 defer any_repo.deinit(io, allocator); 2443 switch (any_repo) { 2444 inline else => |*repo| try Event.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .status = status }, author), 2445 } 2446 }, 2447 } 2448 2449 const route = listRoute(self.data.identity, status, "", entry.id) orelse return; 2450 try self.session.navigate(route); 2451 } 2452 2453 // arrow keys move the tag selection; at the flow's edges focus crosses to 2454 // the neighboring widgets. 2455 fn tagsInput(self: *This, index: usize, child_index: usize, tf: *TagFlow, key: Key, root_focus: *Focus) void { 2456 const cid = tf.focus.child_id orelse return; 2457 const cur = tf.indexOfFocusId(cid) orelse return; 2458 const count = tf.text_boxes.items.len; 2459 switch (key) { 2460 .arrow_left => if (cur > 0) self.focusTag(index, child_index, tf, root_focus, cur - 1) else self.focusList(index, root_focus), 2461 .arrow_right => if (cur + 1 < count) self.focusTag(index, child_index, tf, root_focus, cur + 1), 2462 .arrow_up => if (tf.rowStep(cur, false)) |i| { 2463 self.focusTag(index, child_index, tf, root_focus, i); 2464 } else { 2465 _ = self.moveDetailVertical(index, root_focus, false); 2466 }, 2467 .arrow_down => if (tf.rowStep(cur, true)) |i| { 2468 self.focusTag(index, child_index, tf, root_focus, i); 2469 } else { 2470 _ = self.moveDetailVertical(index, root_focus, true); 2471 }, 2472 .home => self.focusTag(index, child_index, tf, root_focus, 0), 2473 .end => self.focusTag(index, child_index, tf, root_focus, count - 1), 2474 else => {}, 2475 } 2476 } 2477 2478 fn toolRowFocused(self: *This, index: usize, root_focus: *Focus) bool { 2479 return self.focusedDetailChild(index, root_focus) == tool_row_index; 2480 } 2481 2482 fn focusTag(self: *This, index: usize, child_index: usize, tf: *TagFlow, root_focus: *Focus, item: usize) void { 2483 root_focus.setFocus(tf.text_boxes.items[item].getFocus().id); 2484 // keep the tag visible on the terminal: its rect offset by the flow's 2485 // position in the pane. 2486 if (self.session.is_terminal and item < tf.rects.items.len) { 2487 if (self.detailInner(index).children.values()[child_index].rect) |flow_rect| { 2488 var rect = tf.rects.items[item]; 2489 rect.x += flow_rect.x; 2490 rect.y += flow_rect.y; 2491 self.detailScroll(index).scrollToRect(rect); 2492 } 2493 } 2494 } 2495 2496 // return to the tool row's last-focused button (the header when the 2497 // description page shows no row). 2498 fn focusToolRow(self: *This, index: usize, root_focus: *Focus) void { 2499 const cid = self.toolRow(index).getFocus().child_id orelse return self.focusHeader(root_focus); 2500 root_focus.setFocus(cid); 2501 if (self.detailInner(index).children.values()[tool_row_index].rect) |rect| 2502 self.detailScroll(index).scrollToRect(rect); 2503 } 2504 2505 // enter the detail pane. an empty pane can't be entered. 2506 fn focusDetail(self: *This, index: usize, root_focus: *Focus) void { 2507 if (self.detailInner(index).children.count() == 0) return; 2508 root_focus.setFocus(self.detailOuter(index).getFocus().id); 2509 } 2510 2511 // return to the list. 2512 fn focusList(self: *This, index: usize, root_focus: *Focus) void { 2513 if (self.detail_only) return; 2514 root_focus.setFocus(self.listScroll(index).getFocus().id); 2515 } 2516 2517 // cross to the header tabs above the stack. 2518 fn focusHeader(self: *This, root_focus: *Focus) void { 2519 if (self.detail_only) return; 2520 root_focus.setFocus(self.box.children.keys()[header_index]); 2521 } 2522 2523 pub fn clearGrid(self: *This) void { 2524 self.box.clearGrid(); 2525 } 2526 2527 pub fn getGrid(self: This) ?Grid { 2528 return self.box.getGrid(); 2529 } 2530 2531 pub fn getFocus(self: *This) *Focus { 2532 return self.box.getFocus(); 2533 } 2534 2535 pub fn atTop(self: *This, root_focus: *Focus) bool { 2536 if (!self.detail_only) return self.getSelectedIndex() == 0; 2537 const index = self.selectedSplitIndex() orelse return false; 2538 const focused = self.focusedDetailChild(index, root_focus) orelse return false; 2539 for (0..self.detailInner(index).children.count()) |child_index| { 2540 if (!self.detailChildFocusable(index, child_index)) continue; 2541 return focused == child_index; 2542 } 2543 return false; 2544 } 2545 2546 // for the parent's "scroll up at the top jumps to the header" check: at the 2547 // top only while the header tabs hold focus, so up from the split 2548 // crosses into the tabs first. 2549 pub fn getSelectedIndex(self: *This) ?usize { 2550 return if (self.headerActive()) 0 else 1; 2551 } 2552 }; 2553 } 2554