╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Issues.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1467 .border_style = .single, 1468 .rounded_corners = true, 1469 .wrap_kind = .word, 1470 .label = " description ", 1471 .bottom_label = if (cut_short) " click or press enter to see more " else "", 1472 }); 1473 errdefer tb.deinit(allocator); 1474 tb.getFocus().focusable = true; 1475 if (cut_short) tb.getFocus().kind = .{ .custom = try descriptionLink(self.session.page_arena, self.data.identity, entry.id) }; 1476 try inner.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1477 break :blk tb.getFocus().id; 1478 }; 1479 1480 if (!description_page) { 1481 try Comment.appendCount(allocator, inner, entry.comments.count, "comment", "comments", self.session.page_arena.allocator()); 1482 for (entry.comments.comments) |comment| try Comment.appendComment(allocator, inner, self.session, self.data.identity, comment); 1483 try Comment.appendWindowNav(allocator, inner, self.session, self.data.identity, entry.id, null, entry.comments); 1484 1485 var spacer = try ui.Spacer.init(allocator); 1486 errdefer spacer.deinit(allocator); 1487 try inner.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 1488 } 1489 1490 // select the title by default 1491 inner.getFocus().child_id = inner.children.keys()[title_child_index]; 1492 1493 // reset the scroll to the top for the newly-shown issue: directly on the 1494 // terminal (the wasm offset), and via a version bump on the web (so the 1495 // renderer's scroll id changes and JS drops the preserved position). 1496 const sc = self.detailScroll(index); 1497 sc.x = 0; 1498 sc.y = 0; 1499 sc.getFocus().version +%= 1; 1500 } 1501 1502 pub fn input(self: *View, allocator: std.mem.Allocator, raw_key: Key, root_focus: *Focus) !void { 1503 // wheel input moves focus like the arrows 1504 const key: Key = if (raw_key == .mouse and raw_key.mouse.action == .scroll) 1505 (if (raw_key.mouse.action.scroll == .up) .arrow_up else .arrow_down) 1506 else 1507 raw_key; 1508 1509 if (self.headerActive()) { 1510 // down from the tabs re-enters the stack if the selected view has 1511 // something to focus; other keys move the tabs. 1512 if (inp.vertDirection(key) == .down) { 1513 const enterable = if (self.viewStack().getSelected()) |selected| switch (selected.*) { 1514 .tag_flow => |*tf| tf.text_boxes.items.len > 0, 1515 // nothing focusable in the unauthorized view 1516 .unauthorized => false, 1517 else => true, 1518 } else false; 1519 if (enterable) root_focus.setFocus(self.box.children.keys()[stack_index]); 1520 } else { 1521 try self.header().input(allocator, key, root_focus); 1522 } 1523 return; 1524 } 1525 if (self.tagsViewActive()) { 1526 self.tagsViewInput(key, root_focus); 1527 return; 1528 } 1529 if (self.formViewActive()) { 1530 if (self.data.view == .resolve) { 1531 try self.resolveInput(allocator, key, root_focus); 1532 } else { 1533 try self.formInput(allocator, key, root_focus); 1534 } 1535 return; 1536 } 1537 const i = self.selectedSplitIndex() orelse return; 1538 if (self.detailActive(i)) { 1539 try self.detailInput(allocator, i, key, root_focus); 1540 } else { 1541 try self.listInput(i, key, root_focus); 1542 } 1543 } 1544 1545 // arrow keys move the tag selection; up from the top row crosses to the 1546 // header tabs. 1547 fn tagsViewInput(self: *View, key: Key, root_focus: *Focus) void { 1548 const tf = self.tagsView(); 1549 const cid = tf.focus.child_id orelse return; 1550 const cur = tf.indexOfFocusId(cid) orelse return; 1551 const count = tf.text_boxes.items.len; 1552 switch (key) { 1553 .arrow_left => if (cur > 0) root_focus.setFocus(tf.text_boxes.items[cur - 1].getFocus().id), 1554 .arrow_right => if (cur + 1 < count) root_focus.setFocus(tf.text_boxes.items[cur + 1].getFocus().id), 1555 .arrow_up => if (tf.rowStep(cur, false)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id) else self.focusHeader(root_focus), 1556 .arrow_down => if (tf.rowStep(cur, true)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id), 1557 .home => root_focus.setFocus(tf.text_boxes.items[0].getFocus().id), 1558 .end => root_focus.setFocus(tf.text_boxes.items[count - 1].getFocus().id), 1559 else => {}, 1560 } 1561 } 1562 1563 fn listInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 1564 // up/down (and the scroll wheel) move the selection a row; page up/down 1565 // jump a fixed amount. right/Enter cross into the detail pane. up from 1566 // the top row crosses into the header tabs. 1567 if (inp.rowDelta(key, @intCast(self.listBox(index).children.count()))) |delta| { 1568 const lb = self.listBox(index); 1569 const at_top = if (lb.getFocus().child_id) |cid| lb.children.getIndex(cid) == 0 else true; 1570 if (delta < 0 and at_top) return self.focusHeader(root_focus); 1571 ui.moveRowFocus(lb, self.listScroll(index), root_focus, delta); 1572 return; 1573 } 1574 switch (key) { 1575 .enter, .arrow_right => try self.focusDetail(index, root_focus), 1576 else => {}, 1577 } 1578 } 1579 1580 fn detailInput(self: *View, allocator: std.mem.Allocator, index: usize, key: Key, root_focus: *Focus) !void { 1581 if (self.data.comment_page != null and std.mem.eql(u8, self.window(index).issues[self.detailed_index[index] orelse return].id, self.data.selected_id)) { 1582 try self.commentInput(index, key, root_focus); 1583 } else if (self.toolRowFocused(index)) { 1584 try self.toolRowInput(allocator, index, key, root_focus); 1585 } else if (self.titleFocused(index)) { 1586 try self.titleInput(index, key, root_focus); 1587 } else if (self.authorFocused(index)) { 1588 try self.authorInput(index, key, root_focus); 1589 } else if (self.tagsFocused(index)) { 1590 try self.tagsInput(index, key, root_focus); 1591 } else if (self.descriptionFocused(index)) { 1592 try self.descriptionInput(index, key, root_focus); 1593 } else { 1594 try self.commentInput(index, key, root_focus); 1595 } 1596 } 1597 1598 // the tool row: enter or a click on the open/close button flips the 1599 // issue's status; the edit and resolve buttons are a: links the host 1600 // follows. arrows cross between the buttons and to the neighboring 1601 // widgets. 1602 fn toolRowInput(self: *View, allocator: std.mem.Allocator, index: usize, key: Key, root_focus: *Focus) !void { 1603 const row = self.toolRow(index); 1604 const cur = if (row.getFocus().child_id) |cid| row.children.getIndex(cid) orelse return else return; 1605 const on_status = cur == button_in_row_index and self.statusButton(index) != null; 1606 switch (key) { 1607 .arrow_left => if (cur > button_in_row_index) root_focus.setFocus(row.children.keys()[cur - 1]) else try self.focusList(index, root_focus), 1608 .arrow_right => if (cur + 1 < row.children.count()) root_focus.setFocus(row.children.keys()[cur + 1]), 1609 .arrow_up => self.focusHeader(root_focus), 1610 .arrow_down => self.focusTitle(index, root_focus), 1611 .enter => if (on_status) try self.toggleIssueStatus(allocator, index), 1612 .mouse => |mouse| if (self.statusButton(index)) |button| { 1613 if (inp.leftClickOn(root_focus, button.getFocus().id, mouse)) try self.toggleIssueStatus(allocator, index); 1614 }, 1615 else => {}, 1616 } 1617 } 1618 1619 fn titleInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 1620 switch (key) { 1621 .arrow_left => try self.focusList(index, root_focus), 1622 .arrow_up => self.focusToolRow(index, root_focus), 1623 .arrow_down => if (self.authorPresent(index)) self.focusAuthor(index, root_focus) else try self.focusDescription(index, root_focus), 1624 else => {}, 1625 } 1626 } 1627 1628 // the author box's a: link (when it has one) is followed by the host on 1629 // enter / a click; arrows cross to the neighboring widgets. 1630 fn authorInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 1631 switch (key) { 1632 .arrow_left => try self.focusList(index, root_focus), 1633 .arrow_up => self.focusTitle(index, root_focus), 1634 .arrow_down => if (self.tagFlow(index) != null) try self.focusTags(index, root_focus) else try self.focusDescription(index, root_focus), 1635 else => {}, 1636 } 1637 } 1638 1639 fn descriptionInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 1640 const sc = self.detailScroll(index); 1641 switch (key) { 1642 .arrow_left => return self.focusList(index, root_focus), 1643 // once the scroll can't move further, cross into the tags (or the 1644 // open/close button when the issue has none). 1645 .arrow_up => { 1646 const before = sc.y; 1647 sc.y -= 1; 1648 sc.clampToContent(); 1649 if (sc.y == before) { 1650 if (self.tagFlow(index) != null) 1651 try self.focusTags(index, root_focus) 1652 else if (self.authorPresent(index)) 1653 self.focusAuthor(index, root_focus) 1654 else 1655 self.focusTitle(index, root_focus); 1656 } 1657 return; 1658 }, 1659 .arrow_down => { 1660 if (self.moveDetailFocus(index, root_focus, true)) return; 1661 const before = sc.y; 1662 sc.y += 1; 1663 sc.clampToContent(); 1664 if (sc.y == before) _ = self.moveDetailFocus(index, root_focus, true); 1665 return; 1666 }, 1667 .page_up => sc.y -= 10, 1668 .page_down => sc.y += 10, 1669 .home => sc.y = 0, 1670 .end => sc.y = std.math.maxInt(isize), 1671 else => return, 1672 } 1673 sc.clampToContent(); 1674 } 1675 1676 fn commentInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 1677 switch (key) { 1678 .arrow_left => if (!self.moveCommentHorizontal(index, root_focus, false)) return self.focusList(index, root_focus), 1679 .arrow_right => _ = self.moveCommentHorizontal(index, root_focus, true), 1680 .arrow_up => _ = self.moveCommentVertical(index, root_focus, false), 1681 .arrow_down => _ = self.moveCommentVertical(index, root_focus, true), 1682 .page_up => self.detailScroll(index).y -= 10, 1683 .page_down => self.detailScroll(index).y += 10, 1684 .home => self.detailScroll(index).y = 0, 1685 .end => self.detailScroll(index).y = std.math.maxInt(isize), 1686 else => return, 1687 } 1688 self.detailScroll(index).clampToContent(); 1689 } 1690 1691 fn focusedDetailChild(self: *View, index: usize, root_focus: *Focus) ?usize { 1692 const inner = self.detailInner(index); 1693 var id = root_focus.grandchild_id orelse return null; 1694 while (inner.getFocus().children.get(id)) |child| { 1695 if (child.parent_id == inner.getFocus().id) return inner.children.getIndex(id); 1696 id = child.parent_id; 1697 } 1698 return null; 1699 } 1700 1701 fn detailChildFocusable(self: *View, index: usize, child_index: usize) bool { 1702 const child = &self.detailInner(index).children.values()[child_index].widget; 1703 return switch (child.*) { 1704 .repo_comment => true, 1705 .box => |*box| for (box.children.values()) |*item| { 1706 if (item.widget.getFocus().focusable) break true; 1707 } else false, 1708 else => child.getFocus().focusable, 1709 }; 1710 } 1711 1712 fn focusDetailChild(self: *View, index: usize, child_index: usize, last: bool, root_focus: *Focus) void { 1713 const inner = self.detailInner(index); 1714 const child = &inner.children.values()[child_index]; 1715 var target = inner.children.keys()[child_index]; 1716 var rect = child.rect orelse return; 1717 switch (child.widget) { 1718 .repo_comment => |*comment| { 1719 if (comment.rowRect(last)) |row_rect| { 1720 rect.x += row_rect.x; 1721 rect.y += row_rect.y; 1722 rect.size = row_rect.size; 1723 } 1724 if (last) comment.focusBody(root_focus) else comment.focusMetadata(root_focus); 1725 }, 1726 .box => |*box| if (box.children.count() > 0) { 1727 target = box.children.keys()[if (last) box.children.count() - 1 else 0]; 1728 root_focus.setFocus(target); 1729 }, 1730 else => root_focus.setFocus(target), 1731 } 1732 self.detailScroll(index).scrollToRect(rect); 1733 } 1734 1735 fn moveCommentVertical(self: *View, index: usize, root_focus: *Focus, down: bool) bool { 1736 const inner = self.detailInner(index); 1737 const current = self.focusedDetailChild(index, root_focus) orelse return false; 1738 if (inner.children.values()[current].widget == .repo_comment) { 1739 const comment = &inner.children.values()[current].widget.repo_comment; 1740 if (down and !comment.bodyFocused()) { 1741 self.focusDetailChild(index, current, true, root_focus); 1742 return true; 1743 } 1744 if (!down and comment.bodyFocused()) { 1745 self.focusDetailChild(index, current, false, root_focus); 1746 return true; 1747 } 1748 } 1749 1750 var next = current; 1751 while (true) { 1752 if (down) { 1753 next += 1; 1754 if (next >= inner.children.count()) return false; 1755 } else { 1756 if (next == 0) return false; 1757 next -= 1; 1758 } 1759 if (!self.detailChildFocusable(index, next)) continue; 1760 self.focusDetailChild(index, next, !down, root_focus); 1761 return true; 1762 } 1763 } 1764 1765 fn moveCommentHorizontal(self: *View, index: usize, root_focus: *Focus, right: bool) bool { 1766 const inner = self.detailInner(index); 1767 const current = self.focusedDetailChild(index, root_focus) orelse return false; 1768 const child = &inner.children.values()[current]; 1769 if (child.widget == .repo_comment) { 1770 const moved = child.widget.repo_comment.moveHorizontal(root_focus, right); 1771 if (moved) if (child.rect) |rect| self.detailScroll(index).scrollToRect(rect); 1772 return moved; 1773 } 1774 const row = switch (child.widget) { 1775 .box => |*box| box, 1776 else => return false, 1777 }; 1778 const selected = row.getFocus().child_id orelse return false; 1779 const selected_index = row.children.getIndex(selected) orelse return false; 1780 const target = if (right) selected_index + 1 else selected_index -| 1; 1781 if (target == selected_index or target >= row.children.count()) return false; 1782 root_focus.setFocus(row.children.keys()[target]); 1783 if (child.rect) |rect| self.detailScroll(index).scrollToRect(rect); 1784 return true; 1785 } 1786 1787 fn moveDetailFocus(self: *View, index: usize, root_focus: *Focus, down: bool) bool { 1788 return self.moveCommentVertical(index, root_focus, down); 1789 } 1790 1791 // up/down (and tab/shift+tab) move between a form's fields; up from the 1792 // title crosses into the header tabs. the multiline description keeps 1793 // enter and any up/down that has a row to move to. 1794 fn formInput(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1795 const form = self.issueForm() orelse return; 1796 const cid = form.getFocus().child_id orelse return; 1797 const cur = form.children.getIndex(cid) orelse return; 1798 1799 if (cur == description_field_index) { 1800 const child = &form.children.values()[cur]; 1801 if (try multilineKeepsKey(&child.widget.text_input, allocator, key)) 1802 return child.widget.input(allocator, key, root_focus); 1803 } 1804 1805 switch (key) { 1806 .arrow_up, .back_tab => if (cur > 0) 1807 root_focus.setFocus(form.children.keys()[cur - 1]) 1808 else 1809 self.focusHeader(root_focus), 1810 .arrow_down, .tab => if (cur < submit_field_index) { 1811 root_focus.setFocus(form.children.keys()[cur + 1]); 1812 }, 1813 .enter => if (cur == submit_field_index) try self.submitForm(allocator), 1814 .mouse => |mouse| if (cur == submit_field_index) { 1815 const submit = &form.children.values()[cur].widget.submit_button; 1816 if (inp.leftClickOn(root_focus, submit.buttonId(), mouse)) try self.submitForm(allocator); 1817 }, 1818 else => try form.children.values()[cur].widget.input(allocator, key, root_focus), 1819 } 1820 } 1821 1822 // the multiline hunk inputs keep enter and any up/down that has a row to 1823 // move to; enter on a "use this" link is a navigation the host follows 1824 // before this runs. 1825 fn resolveInput(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1826 const form = self.issueForm() orelse return; 1827 const cid = form.getFocus().child_id orelse return; 1828 const cur = form.children.getIndex(cid) orelse return; 1829 const child = &form.children.values()[cur]; 1830 1831 if (child.widget == .text_input) { 1832 const ti = &child.widget.text_input; 1833 if (ti.options.multiline and try multilineKeepsKey(ti, allocator, key)) 1834 return child.widget.input(allocator, key, root_focus); 1835 } 1836 1837 const on_submit = child.widget == .submit_button; 1838 1839 switch (key) { 1840 .arrow_up, .back_tab => if (resolveStep(form, cur, false)) |i| self.focusResolveChild(form, i, root_focus) else self.focusHeader(root_focus), 1841 .arrow_down, .tab => if (resolveStep(form, cur, true)) |i| self.focusResolveChild(form, i, root_focus), 1842 .arrow_left, .arrow_right => switch (child.widget) { 1843 .box => |*row| { 1844 const row_cid = row.getFocus().child_id orelse return; 1845 const row_index = row.children.getIndex(row_cid) orelse return; 1846 if (key == .arrow_left) { 1847 if (row_index > 0) root_focus.setFocus(row.children.keys()[row_index - 1]); 1848 } else if (row_index + 1 < row.children.count()) { 1849 root_focus.setFocus(row.children.keys()[row_index + 1]); 1850 } 1851 }, 1852 else => try child.widget.input(allocator, key, root_focus), 1853 }, 1854 .enter => if (on_submit) try self.submitResolution(allocator), 1855 .mouse => |mouse| if (on_submit) { 1856 if (inp.leftClickOn(root_focus, child.widget.submit_button.buttonId(), mouse)) try self.submitResolution(allocator); 1857 }, 1858 else => try child.widget.input(allocator, key, root_focus), 1859 } 1860 } 1861 1862 // a multiline input keeps enter and any up/down that has a row to move to 1863 fn multilineKeepsKey(text_input: *wgt.TextInput(ui.Widget), allocator: std.mem.Allocator, key: Key) !bool { 1864 return switch (key) { 1865 .enter => true, 1866 .arrow_up => !try text_input.cursorOnFirstRow(allocator), 1867 .arrow_down => !try text_input.cursorOnLastRow(allocator), 1868 else => false, 1869 }; 1870 } 1871 1872 // the neighboring focusable resolve-form child, skipping the spacer and 1873 // the blank gap rows 1874 fn resolveStep(form: *wgt.Box(ui.Widget), cur: usize, down: bool) ?usize { 1875 var i = cur; 1876 while (true) { 1877 if (down) { 1878 i += 1; 1879 if (i >= form.children.count()) return null; 1880 } else { 1881 if (i == 0) return null; 1882 i -= 1; 1883 } 1884 switch (form.children.values()[i].widget) { 1885 .spacer, .text => continue, 1886 else => return i, 1887 } 1888 } 1889 } 1890 1891 // focus the resolve-form child at `i` (a version row focuses its selected 1892 // child) and keep it visible on the terminal 1893 fn focusResolveChild(self: *View, form: *wgt.Box(ui.Widget), i: usize, root_focus: *Focus) void { 1894 const child = &form.children.values()[i]; 1895 switch (child.widget) { 1896 .box => |*row| root_focus.setFocus(row.getFocus().child_id orelse form.children.keys()[i]), 1897 else => root_focus.setFocus(form.children.keys()[i]), 1898 } 1899 if (self.session.is_terminal) { 1900 if (self.resolveScroll()) |sc| { 1901 if (child.rect) |rect| sc.scrollToRect(rect); 1902 } 1903 } 1904 } 1905 1906 // re-emit the conflicted issue's event with the resolved content; any 1907 // event on the issue settles its conflict entry. this is the terminal 1908 // path; the web posts the form to the resolve route. 1909 fn submitResolution(self: *View, allocator: std.mem.Allocator) !void { 1910 if (comptime wasm) return; 1911 const io = self.session.io orelse return; 1912 const src = self.data.repo_source orelse return; 1913 const entry = self.data.selectedIssue() orelse return; 1914 const author_email = (try self.session.eventAuthorEmail()) orelse return; 1915 const form = self.issueForm() orelse return; 1916 1917 // gather the inputs by name; the d<n> hunk inputs appear in chunk order 1918 var title: ?[]u8 = null; 1919 var tags: ?[]u8 = null; 1920 var hunks: std.ArrayList([]const u8) = .empty; 1921 defer { 1922 if (title) |t| allocator.free(t); 1923 if (tags) |t| allocator.free(t); 1924 for (hunks.items) |h| allocator.free(h); 1925 hunks.deinit(allocator); 1926 } 1927 for (form.children.values()) |*child| switch (child.widget) { 1928 .text_input => |*ti| { 1929 const text = try ti.text(allocator); 1930 errdefer allocator.free(text); 1931 if (std.mem.eql(u8, ti.options.name, "title")) { 1932 title = text; 1933 } else if (std.mem.eql(u8, ti.options.name, "tags")) { 1934 tags = text; 1935 } else { 1936 try hunks.append(allocator, text); 1937 } 1938 }, 1939 else => {}, 1940 }; 1941 1942 var id_bytes: [evt.event_id_size]u8 = undefined; 1943 _ = try std.fmt.hexToBytes(&id_bytes, entry.id[0 .. evt.event_id_size * 2]); 1944 1945 switch (src.repo_kind) { 1946 inline else => |repo_kind| { 1947 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, .{ .path = src.path }); 1948 defer any_repo.deinit(io, allocator); 1949 switch (any_repo) { 1950 inline else => |*repo| evt.Issue.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .resolve = .{ 1951 .title = title, 1952 .tags = tags, 1953 .hunks = hunks.items, 1954 } }, author_email) catch |err| switch (err) { 1955 // leave the form up for correction 1956 error.InvalidFields => return, 1957 else => |e| return e, 1958 }, 1959 } 1960 }, 1961 } 1962 1963 const route = ui.RoutablePage.repoIssuesRoute(self.data.identity, entry.issue.event.status, "", entry.id) orelse return; 1964 try self.session.navigate(route); 1965 } 1966 1967 // the terminal submit paths: the new form commits a new-issue event, the 1968 // edit form re-emits the selected issue's event with the form's content. 1969 // the web posts the forms to their routes instead. 1970 fn submitForm(self: *View, allocator: std.mem.Allocator) !void { 1971 if (self.data.view == .edit) { 1972 try self.submitEditedIssue(allocator); 1973 } else { 1974 try self.submitNewIssue(allocator); 1975 } 1976 } 1977 1978 // commit the new issue to the repo's events branch and navigate to it. 1979 // this is the terminal path; the web posts the form to the issue route, 1980 // so the wasm side never runs (or compiles) the repo access below. 1981 fn submitNewIssue(self: *View, allocator: std.mem.Allocator) !void { 1982 if (comptime wasm) return; 1983 const io = self.session.io orelse return; 1984 const src = self.data.repo_source orelse return; 1985 const author_email = (try self.session.eventAuthorEmail()) orelse return; 1986 1987 const form = self.issueForm() orelse return; 1988 const title_input = &form.children.values()[title_field_index].widget.text_input; 1989 const tags_input = &form.children.values()[tags_field_index].widget.text_input; 1990 const description_input = &form.children.values()[description_field_index].widget.text_input; 1991 1992 const title = try title_input.text(allocator); 1993 defer allocator.free(title); 1994 const tags = try tags_input.text(allocator); 1995 defer allocator.free(tags); 1996 const description = try description_input.text(allocator); 1997 defer allocator.free(description); 1998 1999 if (!evt.Issue.fieldsValid(title, tags)) return; 2000 2001 var id_bytes: [evt.event_id_size]u8 = undefined; 2002 io.random(&id_bytes); 2003 const event_id_hex = std.fmt.bytesToHex(id_bytes, .lower); 2004 2005 const event = evt.EventWithId{ 2006 .id = event_id_hex, 2007 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 2008 .author_email = author_email, 2009 .event = .{ .issue = .{ 2010 .title = title, 2011 .description = description, 2012 .tags = tags, 2013 } }, 2014 }; 2015 2016 switch (src.repo_kind) { 2017 inline else => |repo_kind| { 2018 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, .{ .path = src.path }); 2019 defer any_repo.deinit(io, allocator); 2020 switch (any_repo) { 2021 inline else => |*repo| try evt.consume(repo_kind, repo.self_repo_opts, io, allocator, repo, evt.events_ref, &.{event}), 2022 } 2023 }, 2024 } 2025 2026 // wipe the form so a return visit starts fresh 2027 title_input.clear(allocator); 2028 tags_input.clear(allocator); 2029 description_input.clear(allocator); 2030 2031 const route = ui.RoutablePage.repoIssuesRoute(self.data.identity, .open, "", &event_id_hex) orelse return; 2032 try self.session.navigate(route); 2033 } 2034 2035 // re-emit the selected issue's event with the form's content (status 2036 // preserved), then reload the issue's page. this is the terminal path; 2037 // the web posts the form to the edit route. 2038 fn submitEditedIssue(self: *View, allocator: std.mem.Allocator) !void { 2039 if (comptime wasm) return; 2040 const io = self.session.io orelse return; 2041 const src = self.data.repo_source orelse return; 2042 const entry = self.data.selectedIssue() orelse return; 2043 const author_email = (try self.session.eventAuthorEmail()) orelse return; 2044 2045 const form = self.issueForm() orelse return; 2046 const title_input = &form.children.values()[title_field_index].widget.text_input; 2047 const tags_input = &form.children.values()[tags_field_index].widget.text_input; 2048 const description_input = &form.children.values()[description_field_index].widget.text_input; 2049 2050 const title = try title_input.text(allocator); 2051 defer allocator.free(title); 2052 const tags = try tags_input.text(allocator); 2053 defer allocator.free(tags); 2054 const description = try description_input.text(allocator); 2055 defer allocator.free(description); 2056 2057 if (!evt.Issue.fieldsValid(title, tags)) return; 2058 2059 var id_bytes: [evt.event_id_size]u8 = undefined; 2060 _ = try std.fmt.hexToBytes(&id_bytes, entry.id[0 .. evt.event_id_size * 2]); 2061 2062 switch (src.repo_kind) { 2063 inline else => |repo_kind| { 2064 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, .{ .path = src.path }); 2065 defer any_repo.deinit(io, allocator); 2066 switch (any_repo) { 2067 inline else => |*repo| try evt.Issue.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 2068 .title = title, 2069 .tags = tags, 2070 .description = description, 2071 } }, author_email), 2072 } 2073 }, 2074 } 2075 2076 const route = ui.RoutablePage.repoIssuesRoute(self.data.identity, entry.issue.event.status, "", entry.id) orelse return; 2077 try self.session.navigate(route); 2078 } 2079 2080 // flip the shown issue's status by re-emitting its event, then reload the 2081 // page rooted at the issue so the view reflects the change. this is the 2082 // terminal path; the web posts the button's form to the status route. 2083 fn toggleIssueStatus(self: *View, allocator: std.mem.Allocator, index: usize) !void { 2084 if (comptime wasm) return; 2085 const io = self.session.io orelse return; 2086 const src = self.data.repo_source orelse return; 2087 const sel = self.detailed_index[index] orelse return; 2088 const entry = self.window(index).issues[sel]; 2089 const author_email = (try self.session.eventAuthorEmail()) orelse return; 2090 2091 const status: evt.Issue.Status = switch (entry.issue.event.status) { 2092 .open => .closed, 2093 .closed => .open, 2094 }; 2095 2096 var id_bytes: [evt.event_id_size]u8 = undefined; 2097 _ = try std.fmt.hexToBytes(&id_bytes, entry.id[0 .. evt.event_id_size * 2]); 2098 2099 switch (src.repo_kind) { 2100 inline else => |repo_kind| { 2101 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, .{ .path = src.path }); 2102 defer any_repo.deinit(io, allocator); 2103 switch (any_repo) { 2104 inline else => |*repo| try evt.Issue.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .status = status }, author_email), 2105 } 2106 }, 2107 } 2108 2109 const route = ui.RoutablePage.repoIssuesRoute(self.data.identity, status, "", entry.id) orelse return; 2110 try self.session.navigate(route); 2111 } 2112 2113 // arrow keys move the tag selection; at the flow's edges focus crosses to 2114 // the neighboring widgets. 2115 fn tagsInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 2116 const tf = self.tagFlow(index) orelse return; 2117 const cid = tf.focus.child_id orelse return; 2118 const cur = tf.indexOfFocusId(cid) orelse return; 2119 const count = tf.text_boxes.items.len; 2120 switch (key) { 2121 .arrow_left => if (cur > 0) self.focusTag(index, tf, root_focus, cur - 1) else try self.focusList(index, root_focus), 2122 .arrow_right => if (cur + 1 < count) self.focusTag(index, tf, root_focus, cur + 1), 2123 .arrow_up => if (tf.rowStep(cur, false)) |i| self.focusTag(index, tf, root_focus, i) else self.focusAuthor(index, root_focus), 2124 .arrow_down => if (tf.rowStep(cur, true)) |i| self.focusTag(index, tf, root_focus, i) else try self.focusDescription(index, root_focus), 2125 .home => self.focusTag(index, tf, root_focus, 0), 2126 .end => self.focusTag(index, tf, root_focus, count - 1), 2127 else => {}, 2128 } 2129 } 2130 2131 const button_in_row_index: usize = 1; 2132 const title_child_index: usize = 0; 2133 const tags_child_index: usize = 2; 2134 2135 fn tagFlow(self: *View, index: usize) ?*ui.TagFlow { 2136 const inner = self.detailInner(index); 2137 if (inner.children.count() <= tags_child_index) return null; 2138 return switch (inner.children.values()[tags_child_index].widget) { 2139 .tag_flow => |*tf| tf, 2140 else => null, 2141 }; 2142 } 2143 2144 fn tagsFocused(self: *View, index: usize) bool { 2145 const inner = self.detailInner(index); 2146 const cid = inner.getFocus().child_id orelse return false; 2147 return inner.children.getIndex(cid) == tags_child_index and self.tagFlow(index) != null; 2148 } 2149 2150 // the open/close button inside the detail frame's tool row, identified 2151 // by its posting kind; a logged-out session's or conflicted issue's row 2152 // has none. 2153 fn statusButton(self: *View, index: usize) ?*wgt.TextBox(ui.Widget) { 2154 const row = self.toolRow(index); 2155 if (row.children.count() <= button_in_row_index) return null; 2156 switch (row.children.values()[button_in_row_index].widget) { 2157 .text_box => |*tb| switch (tb.box.focus.kind) { 2158 .custom => |custom| if (std.mem.eql(u8, custom, "submit")) return tb, 2159 else => {}, 2160 }, 2161 else => {}, 2162 } 2163 return null; 2164 } 2165 2166 fn toolRowFocused(self: *View, index: usize) bool { 2167 const outer = self.detailOuter(index); 2168 const cid = outer.getFocus().child_id orelse return false; 2169 return outer.children.getIndex(cid) == tool_row_index; 2170 } 2171 2172 fn focusTag(self: *View, index: usize, tf: *ui.TagFlow, root_focus: *Focus, item: usize) void { 2173 root_focus.setFocus(tf.text_boxes.items[item].getFocus().id); 2174 // keep the tag visible on the terminal: its rect offset by the flow's 2175 // position in the pane. 2176 if (self.session.is_terminal and item < tf.rects.items.len) { 2177 if (self.detailInner(index).children.values()[tags_child_index].rect) |flow_rect| { 2178 var rect = tf.rects.items[item]; 2179 rect.x += flow_rect.x; 2180 rect.y += flow_rect.y; 2181 self.detailScroll(index).scrollToRect(rect); 2182 } 2183 } 2184 } 2185 2186 fn titleFocused(self: *View, index: usize) bool { 2187 const inner = self.detailInner(index); 2188 const cid = inner.getFocus().child_id orelse return false; 2189 return inner.children.getIndex(cid) == title_child_index; 2190 } 2191 2192 fn authorPresent(self: *View, index: usize) bool { 2193 return self.author_id[index] != null; 2194 } 2195 2196 fn authorFocused(self: *View, index: usize) bool { 2197 const inner = self.detailInner(index); 2198 const cid = inner.getFocus().child_id orelse return false; 2199 return cid == self.author_id[index]; 2200 } 2201 2202 fn descriptionFocused(self: *View, index: usize) bool { 2203 const cid = self.detailInner(index).getFocus().child_id orelse return false; 2204 return cid == self.description_id[index]; 2205 } 2206 2207 // the author sits right under the title at the top of the pane, so 2208 // focusing it scrolls there. 2209 fn focusAuthor(self: *View, index: usize, root_focus: *Focus) void { 2210 const id = self.author_id[index] orelse return; 2211 root_focus.setFocus(id); 2212 self.detailScroll(index).y = 0; 2213 } 2214 2215 // the title sits at the top of the pane, so focusing it scrolls there. 2216 fn focusTitle(self: *View, index: usize, root_focus: *Focus) void { 2217 const inner = self.detailInner(index); 2218 if (inner.children.count() == 0) return; 2219 root_focus.setFocus(inner.children.keys()[title_child_index]); 2220 self.detailScroll(index).y = 0; 2221 } 2222 2223 fn focusTags(self: *View, index: usize, root_focus: *Focus) !void { 2224 const tf = self.tagFlow(index) orelse return; 2225 if (tf.text_boxes.items.len == 0) return; 2226 const cid = tf.focus.child_id orelse tf.text_boxes.items[0].getFocus().id; 2227 const item = tf.indexOfFocusId(cid) orelse 0; 2228 self.focusTag(index, tf, root_focus, item); 2229 } 2230 2231 fn focusDescription(self: *View, index: usize, root_focus: *Focus) !void { 2232 if (self.description_id[index]) |id| root_focus.setFocus(id); 2233 } 2234 2235 // return to the tool row's last-focused button (the header when the 2236 // description page shows no row). 2237 fn focusToolRow(self: *View, index: usize, root_focus: *Focus) void { 2238 const cid = self.toolRow(index).getFocus().child_id orelse return self.focusHeader(root_focus); 2239 root_focus.setFocus(cid); 2240 } 2241 2242 // enter the detail pane. an empty pane (no issues) can't be entered. 2243 fn focusDetail(self: *View, index: usize, root_focus: *Focus) !void { 2244 if (self.detailInner(index).children.count() == 0) return; 2245 root_focus.setFocus(self.detailOuter(index).getFocus().id); 2246 } 2247 2248 // return to the list. 2249 fn focusList(self: *View, index: usize, root_focus: *Focus) !void { 2250 root_focus.setFocus(self.listScroll(index).getFocus().id); 2251 } 2252 2253 // cross to the header tabs above the stack. 2254 fn focusHeader(self: *View, root_focus: *Focus) void { 2255 root_focus.setFocus(self.box.children.keys()[header_index]); 2256 } 2257 2258 pub fn clearGrid(self: *View) void { 2259 self.box.clearGrid(); 2260 } 2261 2262 pub fn getGrid(self: View) ?Grid { 2263 return self.box.getGrid(); 2264 } 2265 2266 pub fn getFocus(self: *View) *Focus { 2267 return self.box.getFocus(); 2268 } 2269 2270 // for the parent's "scroll up at the top jumps to the header" check: at the 2271 // top only while the header tabs hold focus, so up from the split 2272 // crosses into the tabs first. 2273 pub fn getSelectedIndex(self: *View) ?usize { 2274 return if (self.headerActive()) 0 else 1; 2275 } 2276 }; 2277 2278 // the "a:" navigation link for `status`'s issues page filtered to the 2279 // url-encoded `tag` and rooted at issue `id` within `identity` ("owner/name"). 2280 // a rooted url ignores `status` (it derives its view from the issue's own); 2281 // with an empty `id` this is the bare list link. 2282 fn issuesLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, status: evt.Issue.Status, tag: []const u8, id: []const u8) ![]const u8 { 2283 const route = ui.RoutablePage.repoIssuesRoute(identity, status, tag, id) orelse return error.RouteTooLong; 2284 const url = try route.toUrl(page_arena); 2285 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2286 } 2287 2288 // the in-page "ai:" anchor for selecting issue `id` in `identity`'s list; the 2289 // href is only followed with js off. 2290 fn issueRowLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, id: []const u8) ![]const u8 { 2291 const route = ui.RoutablePage.repoIssuesRoute(identity, .open, "", id) orelse return error.RouteTooLong; 2292 const url = try route.toUrl(page_arena); 2293 return std.fmt.allocPrint(page_arena.allocator(), "ai:{s}", .{url}); 2294 } 2295 2296 // the "a:" link to the window `status_maybe` names, keeping the tag filter, 2297 // or the conflicts window when null, rooted at issue `id`. 2298 fn windowLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, status_maybe: ?evt.Issue.Status, id: []const u8) ![]const u8 { 2299 if (status_maybe) |status| return issuesLink(page_arena, data.identity, status, data.tag, id); 2300 return conflictsLink(page_arena, data.identity, id); 2301 } 2302 2303 // the "a:" link to the conflicts list rooted at issue `start` ("" = the 2304 // first window). 2305 fn conflictsLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, start: []const u8) ![]const u8 { 2306 const route = ui.RoutablePage.repoIssuesConflictsRoute(identity, start) orelse return error.RouteTooLong; 2307 const url = try route.toUrl(page_arena); 2308 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2309 } 2310 2311 // the "a:" link to the whole-description page of issue `id` within `identity`. 2312 fn descriptionLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, id: []const u8) ![]const u8 { 2313 const route = ui.RoutablePage.repoIssuesDescriptionRoute(identity, id) orelse return error.RouteTooLong; 2314 const url = try route.toUrl(page_arena); 2315 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2316 } 2317 2318 // the "a:" link to `status`'s issues list filtered to `tag` (raw; encoded here). 2319 fn tagLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, status: evt.Issue.Status, tag: []const u8) ![]const u8 { 2320 const encoded = try ui.urlEncodeRef(page_arena.allocator(), tag); 2321 const route = ui.RoutablePage.repoIssuesRoute(identity, status, encoded, "") orelse return error.RouteTooLong; 2322 const url = try route.toUrl(page_arena); 2323 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2324 } 2325 2326 // tabs switching between the issues page's views. 2327 pub const Header = struct { 2328 box: wgt.Box(ui.Widget), 2329 tab_ids: std.AutoArrayHashMapUnmanaged(usize, void), 2330 2331 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self) !Header { 2332 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 2333 errdefer box.deinit(allocator); 2334 2335 var tab_ids: std.AutoArrayHashMapUnmanaged(usize, void) = .empty; 2336 errdefer tab_ids.deinit(allocator); 2337 2338 const aa = session.page_arena.allocator(); 2339 2340 // a list tab per status, labeled with its listing's issue count 2341 for ([_]evt.Issue.Status{ .open, .closed }) |status| { 2342 const route = ui.RoutablePage.repoIssuesRoute(data.identity, status, data.tag, "") orelse return error.RouteTooLong; 2343 const link = try std.fmt.allocPrint(aa, "ai:{s}", .{try route.toUrl(session.page_arena)}); 2344 const label = try std.fmt.allocPrint(aa, "{s} ({d})", .{ @tagName(status), data.window(status).count }); 2345 try addTab(allocator, &box, &tab_ids, label, link); 2346 } 2347 2348 // tags tab, labeled with the active tag filter 2349 { 2350 const tags_route = ui.RoutablePage.repoIssuesTagsRoute(data.identity, data.tag) orelse return error.RouteTooLong; 2351 const tags_link = try std.fmt.allocPrint(aa, "ai:{s}", .{try tags_route.toUrl(session.page_arena)}); 2352 const label = if (data.tag.len == 0) "tags" else blk: { 2353 const decoded = std.Uri.percentDecodeInPlace(try aa.dupe(u8, data.tag)); 2354 break :blk try std.fmt.allocPrint(aa, "tags ({s})", .{decoded}); 2355 }; 2356 try addTab(allocator, &box, &tab_ids, label, tags_link); 2357 } 2358 2359 // new-issue tab; an edit or resolve url shows its tab in this place 2360 { 2361 const route = switch (data.view) { 2362 .edit => ui.RoutablePage.repoIssuesEditRoute(data.identity, data.selected_id) orelse return error.RouteTooLong, 2363 .resolve => ui.RoutablePage.repoIssuesResolveRoute(data.identity, data.selected_id, data.theirs_picks) orelse return error.RouteTooLong, 2364 else => ui.RoutablePage.repoIssuesNewRoute(data.identity) orelse return error.RouteTooLong, 2365 }; 2366 const link = try std.fmt.allocPrint(aa, "ai:{s}", .{try route.toUrl(session.page_arena)}); 2367 const label: []const u8 = switch (data.view) { 2368 .edit => "edit", 2369 .resolve => "resolve", 2370 else => "new", 2371 }; 2372 try addTab(allocator, &box, &tab_ids, label, link); 2373 } 2374 2375 // conflicts tab, labeled with the conflict count 2376 if (data.conflicts.count > 0) { 2377 const route = ui.RoutablePage.repoIssuesConflictsRoute(data.identity, "") orelse return error.RouteTooLong; 2378 const link = try std.fmt.allocPrint(aa, "ai:{s}", .{try route.toUrl(session.page_arena)}); 2379 const label = try std.fmt.allocPrint(aa, "conflicts ({d})", .{data.conflicts.count}); 2380 try addTab(allocator, &box, &tab_ids, label, link); 2381 } 2382 2383 var self = Header{ .box = box, .tab_ids = tab_ids }; 2384 // the tab matching the page's view is selected initially. 2385 self.getFocus().child_id = self.tab_ids.keys()[View.viewIndex(data.view)]; 2386 return self; 2387 } 2388 2389 fn addTab(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), tab_ids: *std.AutoArrayHashMapUnmanaged(usize, void), label: []const u8, link: []const u8) !void { 2390 var text_box = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 2391 errdefer text_box.deinit(allocator); 2392 text_box.getFocus().focusable = true; 2393 text_box.getFocus().kind = .{ .custom = link }; 2394 try tab_ids.put(allocator, text_box.getFocus().id, {}); 2395 try box.children.put(allocator, text_box.getFocus().id, .{ 2396 .widget = .{ .text_box = text_box }, 2397 .rect = null, 2398 .min_size = .{ .width = label.len + 2, .height = null }, 2399 }); 2400 } 2401 2402 pub fn deinit(self: *Header, allocator: std.mem.Allocator) void { 2403 self.box.deinit(allocator); 2404 self.tab_ids.deinit(allocator); 2405 } 2406 2407 pub fn build(self: *Header, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2408 self.clearGrid(); 2409 // only the selected tab shows its border 2410 for (self.box.children.keys(), self.box.children.values()) |id, *child| { 2411 switch (child.widget) { 2412 .text_box => |*tb| tb.options.border_style = if (self.getFocus().child_id == id) .single else .hidden, 2413 else => {}, 2414 } 2415 } 2416 try self.box.build(allocator, constraint, root_focus); 2417 } 2418 2419 pub fn input(self: *Header, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2420 _ = allocator; 2421 const current_tab = self.currentTabIndex() orelse return; 2422 if (inp.moveTab(key, current_tab, self.tab_ids.count())) |new_tab| { 2423 root_focus.setFocus(self.tab_ids.keys()[new_tab]); 2424 } 2425 } 2426 2427 pub fn clearGrid(self: *Header) void { 2428 self.box.clearGrid(); 2429 } 2430 2431 pub fn getGrid(self: Header) ?Grid { 2432 return self.box.getGrid(); 2433 } 2434 2435 pub fn getFocus(self: *Header) *Focus { 2436 return self.box.getFocus(); 2437 } 2438 2439 pub fn getSelectedIndex(self: Header) ?usize { 2440 return self.currentTabIndex(); 2441 } 2442 2443 fn currentTabIndex(self: Header) ?usize { 2444 const child_id = self.box.focus.child_id orelse return null; 2445 return self.tab_ids.getIndex(child_id); 2446 } 2447 }; 2448