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