╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Issues.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
876 // the "a:" navigation link for the issues page filtered to the url-encoded 877 // `tag` and rooted at issue `id` within `identity` ("owner/name"). a rooted 878 // url derives its view from the issue's status; the bare list is the open view. 879 fn issuesLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, tag: []const u8, id: []const u8) ![]const u8 { 880 const route = ui.RoutablePage.repoIssuesRoute(identity, .open, tag, id) orelse return error.RouteTooLong; 881 const url = try route.urlAlloc(page_arena); 882 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 883 } 884 885 // the in-page "ai:" anchor for selecting issue `id` in `identity`'s list; the 886 // href is only followed with js off. 887 fn issueRowLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, id: []const u8) ![]const u8 { 888 const route = ui.RoutablePage.repoIssuesRoute(identity, .open, "", id) orelse return error.RouteTooLong; 889 const url = try route.urlAlloc(page_arena); 890 return std.fmt.allocPrint(page_arena.allocator(), "ai:{s}", .{url}); 891 } 892 893 // the "a:" link to `status`'s issues list filtered to `tag` (raw; encoded here). 894 fn tagLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, status: evt.Issue.Status, tag: []const u8) ![]const u8 { 895 const encoded = try ui.urlEncodeRef(page_arena.allocator(), tag); 896 const route = ui.RoutablePage.repoIssuesRoute(identity, status, encoded, "") orelse return error.RouteTooLong; 897 const url = try route.urlAlloc(page_arena); 898 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 899 } 900 901 // tabs switching between the issues page's views. 902 pub const Header = struct { 903 box: wgt.Box(ui.Widget), 904 tab_ids: std.AutoArrayHashMapUnmanaged(usize, void), 905 906 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self) !Header { 907 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = .hidden, .rounded_corners = true, .direction = .horiz }); 908 errdefer box.deinit(allocator); 909 910 var tab_ids: std.AutoArrayHashMapUnmanaged(usize, void) = .empty; 911 errdefer tab_ids.deinit(allocator); 912 913 const aa = session.page_arena.allocator(); 914 915 // a list tab per status, labeled with its listing's issue count 916 for ([_]evt.Issue.Status{ .open, .closed }) |status| { 917 const route = ui.RoutablePage.repoIssuesRoute(data.identity, status, data.tag, "") orelse return error.RouteTooLong; 918 const link = try std.fmt.allocPrint(aa, "ai:{s}", .{try route.urlAlloc(session.page_arena)}); 919 const label = try std.fmt.allocPrint(aa, "{s} ({d})", .{ @tagName(status), data.window(status).count }); 920 var text_box = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 921 errdefer text_box.deinit(allocator); 922 text_box.getFocus().focusable = true; 923 text_box.getFocus().kind = .{ .custom = link }; 924 try tab_ids.put(allocator, text_box.getFocus().id, {}); 925 try box.children.put(allocator, text_box.getFocus().id, .{ 926 .widget = .{ .text_box = text_box }, 927 .rect = null, 928 .min_size = .{ .width = label.len + 2, .height = null }, 929 }); 930 } 931 932 // tags tab, labeled with the active tag filter 933 { 934 const tags_route = ui.RoutablePage.repoIssuesTagsRoute(data.identity, data.tag) orelse return error.RouteTooLong; 935 const tags_link = try std.fmt.allocPrint(aa, "ai:{s}", .{try tags_route.urlAlloc(session.page_arena)}); 936 const label = if (data.tag.len == 0) "tags" else blk: { 937 const decoded = std.Uri.percentDecodeInPlace(try aa.dupe(u8, data.tag)); 938 break :blk try std.fmt.allocPrint(aa, "tags ({s})", .{decoded}); 939 }; 940 var text_box = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 941 errdefer text_box.deinit(allocator); 942 text_box.getFocus().focusable = true; 943 text_box.getFocus().kind = .{ .custom = tags_link }; 944 try tab_ids.put(allocator, text_box.getFocus().id, {}); 945 try box.children.put(allocator, text_box.getFocus().id, .{ 946 .widget = .{ .text_box = text_box }, 947 .rect = null, 948 .min_size = .{ .width = label.len + 2, .height = null }, 949 }); 950 } 951 952 var self = Header{ .box = box, .tab_ids = tab_ids }; 953 // the tab matching the page's view is selected initially. 954 self.getFocus().child_id = self.tab_ids.keys()[View.viewIndex(data.view)]; 955 return self; 956 } 957 958 pub fn deinit(self: *Header, allocator: std.mem.Allocator) void { 959 self.box.deinit(allocator); 960 self.tab_ids.deinit(allocator); 961 } 962 963 pub fn build(self: *Header, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 964 self.clearGrid(); 965 // only the selected tab shows its border 966 for (self.box.children.keys(), self.box.children.values()) |id, *child| { 967 switch (child.widget) { 968 .text_box => |*tb| tb.options.border_style = if (self.getFocus().child_id == id) .single else .hidden, 969 else => {}, 970 } 971 } 972 try self.box.build(allocator, constraint, root_focus); 973 } 974 975 pub fn input(self: *Header, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 976 _ = allocator; 977 const current_tab = self.currentTabIndex() orelse return; 978 if (inp.moveTab(key, current_tab, self.tab_ids.count())) |new_tab| { 979 root_focus.setFocus(self.tab_ids.keys()[new_tab]); 980 } 981 } 982 983 pub fn clearGrid(self: *Header) void { 984 self.box.clearGrid(); 985 } 986 987 pub fn getGrid(self: Header) ?Grid { 988 return self.box.getGrid(); 989 } 990 991 pub fn getFocus(self: *Header) *Focus { 992 return self.box.getFocus(); 993 } 994 995 pub fn getSelectedIndex(self: Header) ?usize { 996 return self.currentTabIndex(); 997 } 998 999 fn currentTabIndex(self: Header) ?usize { 1000 const child_id = self.box.focus.child_id orelse return null; 1001 return self.tab_ids.getIndex(child_id); 1002 } 1003 }; 1004