╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Issues.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1398 1399 fn titleFocused(self: *View, index: usize) bool { 1400 const inner = self.detailInner(index); 1401 const cid = inner.getFocus().child_id orelse return false; 1402 return inner.children.getIndex(cid) == title_child_index; 1403 } 1404 1405 fn authorPresent(self: *View, index: usize) bool { 1406 return self.author_id[index] != null; 1407 } 1408 1409 fn authorFocused(self: *View, index: usize) bool { 1410 const inner = self.detailInner(index); 1411 const cid = inner.getFocus().child_id orelse return false; 1412 return cid == self.author_id[index]; 1413 } 1414 1415 // the author sits right under the title at the top of the pane, so 1416 // focusing it scrolls there. 1417 fn focusAuthor(self: *View, index: usize, root_focus: *Focus) void { 1418 const id = self.author_id[index] orelse return; 1419 root_focus.setFocus(id); 1420 self.detailScroll(index).y = 0; 1421 } 1422 1423 // the title sits at the top of the pane, so focusing it scrolls there. 1424 fn focusTitle(self: *View, index: usize, root_focus: *Focus) void { 1425 const inner = self.detailInner(index); 1426 if (inner.children.count() == 0) return; 1427 root_focus.setFocus(inner.children.keys()[title_child_index]); 1428 self.detailScroll(index).y = 0; 1429 } 1430 1431 fn focusTags(self: *View, index: usize, root_focus: *Focus) !void { 1432 const tf = self.tagFlow(index) orelse return; 1433 if (tf.text_boxes.items.len == 0) return; 1434 const cid = tf.focus.child_id orelse tf.text_boxes.items[0].getFocus().id; 1435 const item = tf.indexOfFocusId(cid) orelse 0; 1436 self.focusTag(index, tf, root_focus, item); 1437 } 1438 1439 fn focusDescription(self: *View, index: usize, root_focus: *Focus) !void { 1440 if (self.description_id[index]) |id| root_focus.setFocus(id); 1441 } 1442 1443 // return to the tool row's last-focused button (the header when the 1444 // description page shows no row). 1445 fn focusToolRow(self: *View, index: usize, root_focus: *Focus) void { 1446 const cid = self.toolRow(index).getFocus().child_id orelse return self.focusHeader(root_focus); 1447 root_focus.setFocus(cid); 1448 } 1449 1450 // enter the detail pane. an empty pane (no issues) can't be entered. 1451 fn focusDetail(self: *View, index: usize, root_focus: *Focus) !void { 1452 if (self.detailInner(index).children.count() == 0) return; 1453 root_focus.setFocus(self.detailOuter(index).getFocus().id); 1454 } 1455 1456 // return to the list. 1457 fn focusList(self: *View, index: usize, root_focus: *Focus) !void { 1458 root_focus.setFocus(self.listScroll(index).getFocus().id); 1459 } 1460 1461 // cross to the header tabs above the stack. 1462 fn focusHeader(self: *View, root_focus: *Focus) void { 1463 root_focus.setFocus(self.box.children.keys()[header_index]); 1464 } 1465 1466 pub fn clearGrid(self: *View) void { 1467 self.box.clearGrid(); 1468 } 1469 1470 pub fn getGrid(self: View) ?Grid { 1471 return self.box.getGrid(); 1472 } 1473 1474 pub fn getFocus(self: *View) *Focus { 1475 return self.box.getFocus(); 1476 } 1477 1478 // for the parent's "scroll up at the top jumps to the header" check: at the 1479 // top only while the header tabs hold focus, so up from the split 1480 // crosses into the tabs first. 1481 pub fn getSelectedIndex(self: *View) ?usize { 1482 return if (self.headerActive()) 0 else 1; 1483 } 1484 }; 1485 1486 // the "a:" navigation link for `status`'s issues page filtered to the 1487 // url-encoded `tag` and rooted at issue `id` within `identity` ("owner/name"). 1488 // a rooted url ignores `status` (it derives its view from the issue's own); 1489 // with an empty `id` this is the bare list link. 1490 fn issuesLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, status: evt.Issue.Status, tag: []const u8, id: []const u8) ![]const u8 { 1491 const route = ui.RoutablePage.repoIssuesRoute(identity, status, tag, id) orelse return error.RouteTooLong; 1492 const url = try route.toUrl(page_arena); 1493 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 1494 } 1495 1496 // the in-page "ai:" anchor for selecting issue `id` in `identity`'s list; the 1497 // href is only followed with js off. 1498 fn issueRowLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, id: []const u8) ![]const u8 { 1499 const route = ui.RoutablePage.repoIssuesRoute(identity, .open, "", id) orelse return error.RouteTooLong; 1500 const url = try route.toUrl(page_arena); 1501 return std.fmt.allocPrint(page_arena.allocator(), "ai:{s}", .{url}); 1502 } 1503 1504 // the "a:" link to the whole-description page of issue `id` within `identity`. 1505 fn descriptionLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, id: []const u8) ![]const u8 { 1506 const route = ui.RoutablePage.repoIssuesDescriptionRoute(identity, id) orelse return error.RouteTooLong; 1507 const url = try route.toUrl(page_arena); 1508 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 1509 } 1510 1511 // the "a:" link to `status`'s issues list filtered to `tag` (raw; encoded here). 1512 fn tagLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, status: evt.Issue.Status, tag: []const u8) ![]const u8 { 1513 const encoded = try ui.urlEncodeRef(page_arena.allocator(), tag); 1514 const route = ui.RoutablePage.repoIssuesRoute(identity, status, encoded, "") orelse return error.RouteTooLong; 1515 const url = try route.toUrl(page_arena); 1516 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 1517 } 1518 1519 // tabs switching between the issues page's views. 1520 pub const Header = struct { 1521 box: wgt.Box(ui.Widget), 1522 tab_ids: std.AutoArrayHashMapUnmanaged(usize, void), 1523 1524 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self) !Header { 1525 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 1526 errdefer box.deinit(allocator); 1527 1528 var tab_ids: std.AutoArrayHashMapUnmanaged(usize, void) = .empty; 1529 errdefer tab_ids.deinit(allocator); 1530 1531 const aa = session.page_arena.allocator(); 1532 1533 // a list tab per status, labeled with its listing's issue count 1534 for ([_]evt.Issue.Status{ .open, .closed }) |status| { 1535 const route = ui.RoutablePage.repoIssuesRoute(data.identity, status, data.tag, "") orelse return error.RouteTooLong; 1536 const link = try std.fmt.allocPrint(aa, "ai:{s}", .{try route.toUrl(session.page_arena)}); 1537 const label = try std.fmt.allocPrint(aa, "{s} ({d})", .{ @tagName(status), data.window(status).count }); 1538 var text_box = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1539 errdefer text_box.deinit(allocator); 1540 text_box.getFocus().focusable = true; 1541 text_box.getFocus().kind = .{ .custom = link }; 1542 try tab_ids.put(allocator, text_box.getFocus().id, {}); 1543 try box.children.put(allocator, text_box.getFocus().id, .{ 1544 .widget = .{ .text_box = text_box }, 1545 .rect = null, 1546 .min_size = .{ .width = label.len + 2, .height = null }, 1547 }); 1548 } 1549 1550 // tags tab, labeled with the active tag filter 1551 { 1552 const tags_route = ui.RoutablePage.repoIssuesTagsRoute(data.identity, data.tag) orelse return error.RouteTooLong; 1553 const tags_link = try std.fmt.allocPrint(aa, "ai:{s}", .{try tags_route.toUrl(session.page_arena)}); 1554 const label = if (data.tag.len == 0) "tags" else blk: { 1555 const decoded = std.Uri.percentDecodeInPlace(try aa.dupe(u8, data.tag)); 1556 break :blk try std.fmt.allocPrint(aa, "tags ({s})", .{decoded}); 1557 }; 1558 var text_box = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1559 errdefer text_box.deinit(allocator); 1560 text_box.getFocus().focusable = true; 1561 text_box.getFocus().kind = .{ .custom = tags_link }; 1562 try tab_ids.put(allocator, text_box.getFocus().id, {}); 1563 try box.children.put(allocator, text_box.getFocus().id, .{ 1564 .widget = .{ .text_box = text_box }, 1565 .rect = null, 1566 .min_size = .{ .width = label.len + 2, .height = null }, 1567 }); 1568 } 1569 1570 // new-issue tab; an edit url shows the edit tab in its place 1571 { 1572 const route = if (data.view == .edit) 1573 ui.RoutablePage.repoIssuesEditRoute(data.identity, data.selected_id) orelse return error.RouteTooLong 1574 else 1575 ui.RoutablePage.repoIssuesNewRoute(data.identity) orelse return error.RouteTooLong; 1576 const link = try std.fmt.allocPrint(aa, "ai:{s}", .{try route.toUrl(session.page_arena)}); 1577 const label: []const u8 = if (data.view == .edit) "edit" else "new"; 1578 var text_box = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1579 errdefer text_box.deinit(allocator); 1580 text_box.getFocus().focusable = true; 1581 text_box.getFocus().kind = .{ .custom = link }; 1582 try tab_ids.put(allocator, text_box.getFocus().id, {}); 1583 try box.children.put(allocator, text_box.getFocus().id, .{ 1584 .widget = .{ .text_box = text_box }, 1585 .rect = null, 1586 .min_size = .{ .width = label.len + 2, .height = null }, 1587 }); 1588 } 1589 1590 var self = Header{ .box = box, .tab_ids = tab_ids }; 1591 // the tab matching the page's view is selected initially. 1592 self.getFocus().child_id = self.tab_ids.keys()[View.viewIndex(data.view)]; 1593 return self; 1594 } 1595 1596 pub fn deinit(self: *Header, allocator: std.mem.Allocator) void { 1597 self.box.deinit(allocator); 1598 self.tab_ids.deinit(allocator); 1599 } 1600 1601 pub fn build(self: *Header, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1602 self.clearGrid(); 1603 // only the selected tab shows its border 1604 for (self.box.children.keys(), self.box.children.values()) |id, *child| { 1605 switch (child.widget) { 1606 .text_box => |*tb| tb.options.border_style = if (self.getFocus().child_id == id) .single else .hidden, 1607 else => {}, 1608 } 1609 } 1610 try self.box.build(allocator, constraint, root_focus); 1611 } 1612 1613 pub fn input(self: *Header, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1614 _ = allocator; 1615 const current_tab = self.currentTabIndex() orelse return; 1616 if (inp.moveTab(key, current_tab, self.tab_ids.count())) |new_tab| { 1617 root_focus.setFocus(self.tab_ids.keys()[new_tab]); 1618 } 1619 } 1620 1621 pub fn clearGrid(self: *Header) void { 1622 self.box.clearGrid(); 1623 } 1624 1625 pub fn getGrid(self: Header) ?Grid { 1626 return self.box.getGrid(); 1627 } 1628 1629 pub fn getFocus(self: *Header) *Focus { 1630 return self.box.getFocus(); 1631 } 1632 1633 pub fn getSelectedIndex(self: Header) ?usize { 1634 return self.currentTabIndex(); 1635 } 1636 1637 fn currentTabIndex(self: Header) ?usize { 1638 const child_id = self.box.focus.child_id orelse return null; 1639 return self.tab_ids.getIndex(child_id); 1640 } 1641 }; 1642