╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Issues.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
520 return rb.children.getIndex(cid) == detail_index; 521 } 522 523 fn headerActive(self: *View) bool { 524 const cid = self.box.getFocus().child_id orelse return false; 525 return self.box.children.getIndex(cid) == header_index; 526 } 527 528 fn tagsViewActive(self: *View) bool { 529 if (self.headerActive()) return false; 530 return self.selectedSplitIndex() == null; 531 } 532 533 // the selected issue's index, or null when a window-navigation row is 534 // selected (a leading "previous" row shifts the issue rows down by one). 535 fn selectedIssueIndex(self: *View, index: usize) ?usize { 536 const lb = self.listBox(index); 537 const cid = lb.getFocus().child_id orelse return null; 538 const idx = lb.children.getIndex(cid) orelse return null; 539 const win = self.window(index); 540 const lead: usize = if (win.prev_id != null) 1 else 0; 541 if (idx < lead or idx - lead >= win.issues.len) return null; 542 return idx - lead; 543 } 544 545 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 546 self.clearGrid(); 547 548 // each header tab maps 1:1 to a stack child by position. mirror the 549 // selection into the url: the view the page's window is rooted at 550 // keeps its rooted url, the others get their list route (keeping the 551 // tag filter). 552 if (self.header().getSelectedIndex()) |index| { 553 const stack = self.viewStack(); 554 stack.getFocus().child_id = stack.children.keys()[index]; 555 self.session.data.current_page = (if (index == viewIndex(self.data.view) and self.data.selected_id.len != 0) 556 ui.RoutablePage.repoIssuesRoute(self.data.identity, .open, self.data.tag, self.data.selected_id) 557 else if (index == tags_view_index) 558 ui.RoutablePage.repoIssuesTagsRoute(self.data.identity, self.data.tag) 559 else 560 ui.RoutablePage.repoIssuesRoute(self.data.identity, splitStatus(index), self.data.tag, "")) orelse self.session.data.current_page; 561 } 562 563 if (self.selectedSplitIndex()) |i| { 564 // swap the detail pane to the selected issue when it changes. 565 try self.refreshDetail(allocator, i); 566 567 // mirror the focused issue into the url, but only while focus is 568 // inside the split. an issue's url is the same whether or not the 569 // list is filtered, so the mirror drops the tag. 570 if (root_focus.grandchild_id) |g| { 571 if (self.resultsBox(i).getFocus().children.contains(g)) { 572 if (self.selectedIssueIndex(i)) |sel| { 573 if (ui.RoutablePage.repoIssuesRoute(self.data.identity, splitStatus(i), "", self.window(i).issues[sel].id)) |route| 574 self.session.data.current_page = route; 575 } 576 } 577 } 578 579 // the selected list row shows a border (the focused TextBox 580 // upgrades it to a double border itself); the rest stay borderless. 581 const lb = self.listBox(i); 582 for (lb.children.keys(), lb.children.values()) |id, *child| { 583 switch (child.widget) { 584 .text_box => |*tb| tb.options.border_style = if (lb.getFocus().child_id == id) .single else .hidden, 585 else => {}, 586 } 587 } 588 589 // the pane's selected child shows the selection border: the 590 // description directly, the tags via the flow's selected item. 591 const inner = self.detailInner(i); 592 for (inner.children.keys(), inner.children.values()) |id, *child| { 593 switch (child.widget) { 594 .text_box => |*tb| tb.options.border_style = if (inner.getFocus().child_id == id) .single else .hidden, 595 else => {}, 596 } 597 } 598 if (self.tagFlow(i)) |tf| tf.selected = self.tagsFocused(i); 599 600 // cap the list at list_max_width only while the detail pane fits 601 // beside it. the box drops the detail when the width can't hold 602 // both minimums, so when it's that narrow we lift the cap and let 603 // the list fill the whole width. 604 const both_panes_fit = if (constraint.max_size.width) |w| w >= list_max_width + detail_min_width else true; 605 self.resultsBox(i).children.values()[list_index].max_size = if (both_panes_fit) .{ .width = list_max_width, .height = null } else null; 606 607 // stretch the detail pane across the rest of the width so it fills 608 // the area rather than shrinking to its content; its scroll fills 609 // the pane. 610 if (constraint.max_size.width) |w| { 611 self.resultsBox(i).children.values()[detail_index].min_size = .{ .width = if (both_panes_fit) w - list_max_width else w, .height = null }; 612 } else { 613 self.resultsBox(i).children.values()[detail_index].min_size = .{ .width = detail_min_width, .height = null }; 614 } 615 } 616 617 self.tagsView().selected = self.tagsViewActive(); 618 619 try self.box.build(allocator, constraint, root_focus); 620 } 621 622 fn refreshDetail(self: *View, allocator: std.mem.Allocator, index: usize) !void { 623 const sel = self.selectedIssueIndex(index) orelse return; 624 if (self.detailed_index[index]) |d| if (d == sel) return; 625 try self.populateDetail(allocator, index, sel); 626 self.detailed_index[index] = sel; 627 } 628 629 fn populateDetail(self: *View, allocator: std.mem.Allocator, index: usize, sel: usize) !void { 630 const entry = self.window(index).issues[sel]; 631 const inner = self.detailInner(index); 632 633 for (inner.children.values()) |*child| child.widget.deinit(allocator); 634 inner.children.clearAndFree(allocator); 635 inner.getFocus().child_id = null; 636 637 // the issue's tags, each linking to this status's list filtered to 638 // that tag. 639 { 640 var items: std.ArrayList(ui.TagFlow.Item) = .empty; 641 defer items.deinit(allocator); 642 var tag_iter = evt.Issue.tagIterator(entry.issue.tags); 643 while (tag_iter.next()) |tag| { 644 if (tag.len == 0) continue; 645 try items.append(allocator, .{ .text = tag, .link = try tagLink(self.session.page_arena, self.data.identity, splitStatus(index), tag) }); 646 } 647 if (items.items.len > 0) { 648 var tf = try ui.TagFlow.init(allocator); 649 errdefer tf.deinit(allocator); 650 try tf.setItems(allocator, items.items); 651 try inner.children.put(allocator, tf.getFocus().id, .{ .widget = .{ .tag_flow = tf }, .rect = null, .min_size = null }); 652 } 653 } 654 655 // the description as a focusable word-wrapped text box. its hidden 656 // border reserves the space the border occupies when focused, so 657 // focusing doesn't shift layout. 658 self.description_id[index] = blk: { 659 var tb = try wgt.TextBox(ui.Widget).init(allocator, entry.issue.description, .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .word }); 660 errdefer tb.deinit(allocator); 661 tb.getFocus().focusable = true; 662 try inner.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 663 break :blk tb.getFocus().id; 664 }; 665 666 // select the description by default 667 inner.getFocus().child_id = self.description_id[index]; 668 669 // reset the scroll to the top for the newly-shown issue: directly on the 670 // terminal (the wasm offset), and via a version bump on the web (so the 671 // renderer's scroll id changes and JS drops the preserved position). 672 const sc = self.detailScroll(index); 673 sc.x = 0; 674 sc.y = 0; 675 sc.getFocus().version +%= 1; 676 } 677 678 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 679 if (self.headerActive()) { 680 // down from the tabs re-enters the stack if the selected view has 681 // something to focus; other keys move the tabs. 682 if (inp.vertDirection(key) == .down) { 683 const enterable = if (self.viewStack().getSelected()) |selected| switch (selected.*) { 684 .tag_flow => |*tf| tf.text_boxes.items.len > 0, 685 else => true, 686 } else false; 687 if (enterable) root_focus.setFocus(self.box.children.keys()[stack_index]); 688 } else { 689 try self.header().input(allocator, key, root_focus); 690 } 691 return; 692 } 693 if (self.tagsViewActive()) { 694 self.tagsViewInput(key, root_focus); 695 return; 696 } 697 const i = self.selectedSplitIndex() orelse return; 698 if (self.detailActive(i)) { 699 try self.detailInput(i, key, root_focus); 700 } else { 701 try self.listInput(i, key, root_focus); 702 } 703 } 704 705 // arrow keys move the tag selection; up from the top row crosses to the 706 // header tabs. 707 fn tagsViewInput(self: *View, key: Key, root_focus: *Focus) void { 708 const tf = self.tagsView(); 709 const cid = tf.focus.child_id orelse return; 710 const cur = tf.indexOfFocusId(cid) orelse return; 711 const count = tf.text_boxes.items.len; 712 switch (key) { 713 .arrow_left => if (cur > 0) root_focus.setFocus(tf.text_boxes.items[cur - 1].getFocus().id), 714 .arrow_right => if (cur + 1 < count) root_focus.setFocus(tf.text_boxes.items[cur + 1].getFocus().id), 715 .arrow_up => if (tf.rowStep(cur, false)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id) else self.focusHeader(root_focus), 716 .arrow_down => if (tf.rowStep(cur, true)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id), 717 .home => root_focus.setFocus(tf.text_boxes.items[0].getFocus().id), 718 .end => root_focus.setFocus(tf.text_boxes.items[count - 1].getFocus().id), 719 else => {}, 720 } 721 } 722 723 fn listInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 724 // up/down (and the scroll wheel) move the selection a row; page up/down 725 // jump a fixed amount. right/Enter cross into the detail pane. up from 726 // the top row crosses into the header tabs. 727 if (inp.rowDelta(key, @intCast(self.listBox(index).children.count()))) |delta| { 728 const lb = self.listBox(index); 729 const at_top = if (lb.getFocus().child_id) |cid| lb.children.getIndex(cid) == 0 else true; 730 if (delta < 0 and at_top) return self.focusHeader(root_focus); 731 ui.moveRowFocus(lb, self.listScroll(index), root_focus, delta); 732 return; 733 } 734 switch (key) { 735 .enter, .arrow_right => try self.focusDetail(index, root_focus), 736 else => {}, 737 } 738 } 739 740 fn detailInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 741 if (self.tagsFocused(index)) { 742 try self.tagsInput(index, key, root_focus); 743 } else { 744 try self.descriptionInput(index, key, root_focus); 745 } 746 } 747 748 fn descriptionInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 749 const sc = self.detailScroll(index); 750 switch (key) { 751 .arrow_left => return self.focusList(index, root_focus), 752 // once the scroll can't move further, cross into the tags (or the 753 // header tabs when the issue has none). 754 .arrow_up => { 755 const before = sc.y; 756 sc.y -= 1; 757 sc.clampToContent(); 758 if (sc.y == before) { 759 if (self.tagFlow(index) != null) try self.focusTags(index, root_focus) else self.focusHeader(root_focus); 760 } 761 return; 762 }, 763 .arrow_down => sc.y += 1, 764 .page_up => sc.y -= 10, 765 .page_down => sc.y += 10, 766 .home => sc.y = 0, 767 .end => sc.y = std.math.maxInt(isize), 768 .mouse => |mouse| switch (mouse.action) { 769 .scroll => |dir| sc.y += if (dir == .up) @as(isize, -1) else 1, 770 else => {}, 771 }, 772 else => return, 773 } 774 sc.clampToContent(); 775 } 776 777 // arrow keys move the tag selection; at the flow's edges focus crosses to 778 // the neighboring widgets. 779 fn tagsInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 780 const tf = self.tagFlow(index) orelse return; 781 const cid = tf.focus.child_id orelse return; 782 const cur = tf.indexOfFocusId(cid) orelse return; 783 const count = tf.text_boxes.items.len; 784 const sc = self.detailScroll(index); 785 switch (key) { 786 .arrow_left => if (cur > 0) self.focusTag(index, tf, root_focus, cur - 1) else try self.focusList(index, root_focus), 787 .arrow_right => if (cur + 1 < count) self.focusTag(index, tf, root_focus, cur + 1), 788 .arrow_up => if (tf.rowStep(cur, false)) |i| self.focusTag(index, tf, root_focus, i) else self.focusHeader(root_focus), 789 .arrow_down => if (tf.rowStep(cur, true)) |i| self.focusTag(index, tf, root_focus, i) else try self.focusDescription(index, root_focus), 790 .home => self.focusTag(index, tf, root_focus, 0), 791 .end => self.focusTag(index, tf, root_focus, count - 1), 792 .mouse => |mouse| switch (mouse.action) { 793 .scroll => |dir| { 794 sc.y += if (dir == .up) @as(isize, -1) else 1; 795 sc.clampToContent(); 796 }, 797 else => {}, 798 }, 799 else => {}, 800 } 801 } 802 803 const tags_child_index: usize = 0; 804 805 fn tagFlow(self: *View, index: usize) ?*ui.TagFlow { 806 const inner = self.detailInner(index); 807 if (inner.children.count() == 0) return null; 808 return switch (inner.children.values()[tags_child_index].widget) { 809 .tag_flow => |*tf| tf, 810 else => null, 811 }; 812 } 813 814 fn tagsFocused(self: *View, index: usize) bool { 815 const inner = self.detailInner(index); 816 const cid = inner.getFocus().child_id orelse return false; 817 return inner.children.getIndex(cid) == tags_child_index and self.tagFlow(index) != null; 818 } 819 820 fn focusTag(self: *View, index: usize, tf: *ui.TagFlow, root_focus: *Focus, item: usize) void { 821 root_focus.setFocus(tf.text_boxes.items[item].getFocus().id); 822 // keep the tag visible on the terminal: its rect offset by the flow's 823 // position in the pane. 824 if (self.session.is_terminal and item < tf.rects.items.len) { 825 if (self.detailInner(index).children.values()[tags_child_index].rect) |flow_rect| { 826 var rect = tf.rects.items[item]; 827 rect.x += flow_rect.x; 828 rect.y += flow_rect.y; 829 self.detailScroll(index).scrollToRect(rect); 830 } 831 } 832 } 833 834 fn focusTags(self: *View, index: usize, root_focus: *Focus) !void { 835 const tf = self.tagFlow(index) orelse return; 836 if (tf.text_boxes.items.len == 0) return; 837 const cid = tf.focus.child_id orelse tf.text_boxes.items[0].getFocus().id; 838 const item = tf.indexOfFocusId(cid) orelse 0; 839 self.focusTag(index, tf, root_focus, item); 840 } 841 842 fn focusDescription(self: *View, index: usize, root_focus: *Focus) !void { 843 if (self.description_id[index]) |id| root_focus.setFocus(id); 844 } 845 846 // enter the detail pane. an empty pane (no issues) can't be entered. 847 fn focusDetail(self: *View, index: usize, root_focus: *Focus) !void { 848 if (self.detailInner(index).children.count() == 0) return; 849 root_focus.setFocus(self.detailOuter(index).getFocus().id); 850 } 851 852 // return to the list. 853 fn focusList(self: *View, index: usize, root_focus: *Focus) !void { 854 root_focus.setFocus(self.listScroll(index).getFocus().id); 855 } 856 857 // cross to the header tabs above the stack. 858 fn focusHeader(self: *View, root_focus: *Focus) void { 859 root_focus.setFocus(self.box.children.keys()[header_index]); 860 } 861 862 pub fn clearGrid(self: *View) void { 863 self.box.clearGrid(); 864 } 865 866 pub fn getGrid(self: View) ?Grid { 867 return self.box.getGrid(); 868 } 869 870 pub fn getFocus(self: *View) *Focus { 871 return self.box.getFocus(); 872 } 873 874 // for the parent's "scroll up at the top jumps to the header" check: at the 875 // top only while the header tabs hold focus, so up from the split 876 // crosses into the tabs first. 877 pub fn getSelectedIndex(self: *View) ?usize { 878 return if (self.headerActive()) 0 else 1; 879 } 880 }; 881 882 // the "a:" navigation link for `status`'s issues page filtered to the 883 // url-encoded `tag` and rooted at issue `id` within `identity` ("owner/name"). 884 // a rooted url ignores `status` (it derives its view from the issue's own); 885 // with an empty `id` this is the bare list link. 886 fn issuesLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, status: evt.Issue.Status, tag: []const u8, id: []const u8) ![]const u8 { 887 const route = ui.RoutablePage.repoIssuesRoute(identity, status, tag, id) orelse return error.RouteTooLong; 888 const url = try route.toUrl(page_arena); 889 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 890 } 891 892 // the in-page "ai:" anchor for selecting issue `id` in `identity`'s list; the 893 // href is only followed with js off. 894 fn issueRowLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, id: []const u8) ![]const u8 { 895 const route = ui.RoutablePage.repoIssuesRoute(identity, .open, "", id) orelse return error.RouteTooLong; 896 const url = try route.toUrl(page_arena); 897 return std.fmt.allocPrint(page_arena.allocator(), "ai:{s}", .{url}); 898 } 899 900 // the "a:" link to `status`'s issues list filtered to `tag` (raw; encoded here). 901 fn tagLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, status: evt.Issue.Status, tag: []const u8) ![]const u8 { 902 const encoded = try ui.urlEncodeRef(page_arena.allocator(), tag); 903 const route = ui.RoutablePage.repoIssuesRoute(identity, status, encoded, "") orelse return error.RouteTooLong; 904 const url = try route.toUrl(page_arena); 905 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 906 } 907 908 // tabs switching between the issues page's views. 909 pub const Header = struct { 910 box: wgt.Box(ui.Widget), 911 tab_ids: std.AutoArrayHashMapUnmanaged(usize, void), 912 913 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self) !Header { 914 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 915 errdefer box.deinit(allocator); 916 917 var tab_ids: std.AutoArrayHashMapUnmanaged(usize, void) = .empty; 918 errdefer tab_ids.deinit(allocator); 919 920 const aa = session.page_arena.allocator(); 921 922 // a list tab per status, labeled with its listing's issue count 923 for ([_]evt.Issue.Status{ .open, .closed }) |status| { 924 const route = ui.RoutablePage.repoIssuesRoute(data.identity, status, data.tag, "") orelse return error.RouteTooLong; 925 const link = try std.fmt.allocPrint(aa, "ai:{s}", .{try route.toUrl(session.page_arena)}); 926 const label = try std.fmt.allocPrint(aa, "{s} ({d})", .{ @tagName(status), data.window(status).count }); 927 var text_box = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 928 errdefer text_box.deinit(allocator); 929 text_box.getFocus().focusable = true; 930 text_box.getFocus().kind = .{ .custom = link }; 931 try tab_ids.put(allocator, text_box.getFocus().id, {}); 932 try box.children.put(allocator, text_box.getFocus().id, .{ 933 .widget = .{ .text_box = text_box }, 934 .rect = null, 935 .min_size = .{ .width = label.len + 2, .height = null }, 936 }); 937 } 938 939 // tags tab, labeled with the active tag filter 940 { 941 const tags_route = ui.RoutablePage.repoIssuesTagsRoute(data.identity, data.tag) orelse return error.RouteTooLong; 942 const tags_link = try std.fmt.allocPrint(aa, "ai:{s}", .{try tags_route.toUrl(session.page_arena)}); 943 const label = if (data.tag.len == 0) "tags" else blk: { 944 const decoded = std.Uri.percentDecodeInPlace(try aa.dupe(u8, data.tag)); 945 break :blk try std.fmt.allocPrint(aa, "tags ({s})", .{decoded}); 946 }; 947 var text_box = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 948 errdefer text_box.deinit(allocator); 949 text_box.getFocus().focusable = true; 950 text_box.getFocus().kind = .{ .custom = tags_link }; 951 try tab_ids.put(allocator, text_box.getFocus().id, {}); 952 try box.children.put(allocator, text_box.getFocus().id, .{ 953 .widget = .{ .text_box = text_box }, 954 .rect = null, 955 .min_size = .{ .width = label.len + 2, .height = null }, 956 }); 957 } 958 959 var self = Header{ .box = box, .tab_ids = tab_ids }; 960 // the tab matching the page's view is selected initially. 961 self.getFocus().child_id = self.tab_ids.keys()[View.viewIndex(data.view)]; 962 return self; 963 } 964 965 pub fn deinit(self: *Header, allocator: std.mem.Allocator) void { 966 self.box.deinit(allocator); 967 self.tab_ids.deinit(allocator); 968 } 969 970 pub fn build(self: *Header, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 971 self.clearGrid(); 972 // only the selected tab shows its border 973 for (self.box.children.keys(), self.box.children.values()) |id, *child| { 974 switch (child.widget) { 975 .text_box => |*tb| tb.options.border_style = if (self.getFocus().child_id == id) .single else .hidden, 976 else => {}, 977 } 978 } 979 try self.box.build(allocator, constraint, root_focus); 980 } 981 982 pub fn input(self: *Header, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 983 _ = allocator; 984 const current_tab = self.currentTabIndex() orelse return; 985 if (inp.moveTab(key, current_tab, self.tab_ids.count())) |new_tab| { 986 root_focus.setFocus(self.tab_ids.keys()[new_tab]); 987 } 988 } 989 990 pub fn clearGrid(self: *Header) void { 991 self.box.clearGrid(); 992 } 993 994 pub fn getGrid(self: Header) ?Grid { 995 return self.box.getGrid(); 996 } 997 998 pub fn getFocus(self: *Header) *Focus { 999 return self.box.getFocus(); 1000 } 1001 1002 pub fn getSelectedIndex(self: Header) ?usize { 1003 return self.currentTabIndex(); 1004 } 1005 1006 fn currentTabIndex(self: Header) ?usize { 1007 const child_id = self.box.focus.child_id orelse return null; 1008 return self.tab_ids.getIndex(child_id); 1009 } 1010 }; 1011