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