╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Issues.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
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