╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Issues.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2059 2060 pub fn getFocus(self: *View) *Focus { 2061 return self.box.getFocus(); 2062 } 2063 2064 // for the parent's "scroll up at the top jumps to the header" check: at the 2065 // top only while the header tabs hold focus, so up from the split 2066 // crosses into the tabs first. 2067 pub fn getSelectedIndex(self: *View) ?usize { 2068 return if (self.headerActive()) 0 else 1; 2069 } 2070 }; 2071 2072 // the "a:" navigation link for `status`'s issues page filtered to the 2073 // url-encoded `tag` and rooted at issue `id` within `identity` ("owner/name"). 2074 // a rooted url ignores `status` (it derives its view from the issue's own); 2075 // with an empty `id` this is the bare list link. 2076 fn issuesLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, status: evt.Issue.Status, tag: []const u8, id: []const u8) ![]const u8 { 2077 const route = ui.RoutablePage.repoIssuesRoute(identity, status, tag, id) orelse return error.RouteTooLong; 2078 const url = try route.toUrl(page_arena); 2079 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2080 } 2081 2082 // the in-page "ai:" anchor for selecting issue `id` in `identity`'s list; the 2083 // href is only followed with js off. 2084 fn issueRowLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, id: []const u8) ![]const u8 { 2085 const route = ui.RoutablePage.repoIssuesRoute(identity, .open, "", id) orelse return error.RouteTooLong; 2086 const url = try route.toUrl(page_arena); 2087 return std.fmt.allocPrint(page_arena.allocator(), "ai:{s}", .{url}); 2088 } 2089 2090 // the "a:" link to the window `status_maybe` names, keeping the tag filter, 2091 // or the conflicts window when null, rooted at issue `id`. 2092 fn windowLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, status_maybe: ?evt.Issue.Status, id: []const u8) ![]const u8 { 2093 if (status_maybe) |status| return issuesLink(page_arena, data.identity, status, data.tag, id); 2094 return conflictsLink(page_arena, data.identity, id); 2095 } 2096 2097 // the "a:" link to the conflicts list rooted at issue `start` ("" = the 2098 // first window). 2099 fn conflictsLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, start: []const u8) ![]const u8 { 2100 const route = ui.RoutablePage.repoIssuesConflictsRoute(identity, start) orelse return error.RouteTooLong; 2101 const url = try route.toUrl(page_arena); 2102 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2103 } 2104 2105 // the "a:" link to the whole-description page of issue `id` within `identity`. 2106 fn descriptionLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, id: []const u8) ![]const u8 { 2107 const route = ui.RoutablePage.repoIssuesDescriptionRoute(identity, id) orelse return error.RouteTooLong; 2108 const url = try route.toUrl(page_arena); 2109 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2110 } 2111 2112 // the "a:" link to `status`'s issues list filtered to `tag` (raw; encoded here). 2113 fn tagLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, status: evt.Issue.Status, tag: []const u8) ![]const u8 { 2114 const encoded = try ui.urlEncodeRef(page_arena.allocator(), tag); 2115 const route = ui.RoutablePage.repoIssuesRoute(identity, status, encoded, "") orelse return error.RouteTooLong; 2116 const url = try route.toUrl(page_arena); 2117 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2118 } 2119 2120 // tabs switching between the issues page's views. 2121 pub const Header = struct { 2122 box: wgt.Box(ui.Widget), 2123 tab_ids: std.AutoArrayHashMapUnmanaged(usize, void), 2124 2125 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self) !Header { 2126 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 2127 errdefer box.deinit(allocator); 2128 2129 var tab_ids: std.AutoArrayHashMapUnmanaged(usize, void) = .empty; 2130 errdefer tab_ids.deinit(allocator); 2131 2132 const aa = session.page_arena.allocator(); 2133 2134 // a list tab per status, labeled with its listing's issue count 2135 for ([_]evt.Issue.Status{ .open, .closed }) |status| { 2136 const route = ui.RoutablePage.repoIssuesRoute(data.identity, status, data.tag, "") orelse return error.RouteTooLong; 2137 const link = try std.fmt.allocPrint(aa, "ai:{s}", .{try route.toUrl(session.page_arena)}); 2138 const label = try std.fmt.allocPrint(aa, "{s} ({d})", .{ @tagName(status), data.window(status).count }); 2139 try addTab(allocator, &box, &tab_ids, label, link); 2140 } 2141 2142 // tags tab, labeled with the active tag filter 2143 { 2144 const tags_route = ui.RoutablePage.repoIssuesTagsRoute(data.identity, data.tag) orelse return error.RouteTooLong; 2145 const tags_link = try std.fmt.allocPrint(aa, "ai:{s}", .{try tags_route.toUrl(session.page_arena)}); 2146 const label = if (data.tag.len == 0) "tags" else blk: { 2147 const decoded = std.Uri.percentDecodeInPlace(try aa.dupe(u8, data.tag)); 2148 break :blk try std.fmt.allocPrint(aa, "tags ({s})", .{decoded}); 2149 }; 2150 try addTab(allocator, &box, &tab_ids, label, tags_link); 2151 } 2152 2153 // new-issue tab; an edit or resolve url shows its tab in this place 2154 { 2155 const route = switch (data.view) { 2156 .edit => ui.RoutablePage.repoIssuesEditRoute(data.identity, data.selected_id) orelse return error.RouteTooLong, 2157 .resolve => ui.RoutablePage.repoIssuesResolveRoute(data.identity, data.selected_id, data.theirs_picks) orelse return error.RouteTooLong, 2158 else => ui.RoutablePage.repoIssuesNewRoute(data.identity) orelse return error.RouteTooLong, 2159 }; 2160 const link = try std.fmt.allocPrint(aa, "ai:{s}", .{try route.toUrl(session.page_arena)}); 2161 const label: []const u8 = switch (data.view) { 2162 .edit => "edit", 2163 .resolve => "resolve", 2164 else => "new", 2165 }; 2166 try addTab(allocator, &box, &tab_ids, label, link); 2167 } 2168 2169 // conflicts tab, labeled with the conflict count 2170 if (data.conflicts.count > 0) { 2171 const route = ui.RoutablePage.repoIssuesConflictsRoute(data.identity, "") orelse return error.RouteTooLong; 2172 const link = try std.fmt.allocPrint(aa, "ai:{s}", .{try route.toUrl(session.page_arena)}); 2173 const label = try std.fmt.allocPrint(aa, "conflicts ({d})", .{data.conflicts.count}); 2174 try addTab(allocator, &box, &tab_ids, label, link); 2175 } 2176 2177 var self = Header{ .box = box, .tab_ids = tab_ids }; 2178 // the tab matching the page's view is selected initially. 2179 self.getFocus().child_id = self.tab_ids.keys()[View.viewIndex(data.view)]; 2180 return self; 2181 } 2182 2183 fn addTab(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), tab_ids: *std.AutoArrayHashMapUnmanaged(usize, void), label: []const u8, link: []const u8) !void { 2184 var text_box = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 2185 errdefer text_box.deinit(allocator); 2186 text_box.getFocus().focusable = true; 2187 text_box.getFocus().kind = .{ .custom = link }; 2188 try tab_ids.put(allocator, text_box.getFocus().id, {}); 2189 try box.children.put(allocator, text_box.getFocus().id, .{ 2190 .widget = .{ .text_box = text_box }, 2191 .rect = null, 2192 .min_size = .{ .width = label.len + 2, .height = null }, 2193 }); 2194 } 2195 2196 pub fn deinit(self: *Header, allocator: std.mem.Allocator) void { 2197 self.box.deinit(allocator); 2198 self.tab_ids.deinit(allocator); 2199 } 2200 2201 pub fn build(self: *Header, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2202 self.clearGrid(); 2203 // only the selected tab shows its border 2204 for (self.box.children.keys(), self.box.children.values()) |id, *child| { 2205 switch (child.widget) { 2206 .text_box => |*tb| tb.options.border_style = if (self.getFocus().child_id == id) .single else .hidden, 2207 else => {}, 2208 } 2209 } 2210 try self.box.build(allocator, constraint, root_focus); 2211 } 2212 2213 pub fn input(self: *Header, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2214 _ = allocator; 2215 const current_tab = self.currentTabIndex() orelse return; 2216 if (inp.moveTab(key, current_tab, self.tab_ids.count())) |new_tab| { 2217 root_focus.setFocus(self.tab_ids.keys()[new_tab]); 2218 } 2219 } 2220 2221 pub fn clearGrid(self: *Header) void { 2222 self.box.clearGrid(); 2223 } 2224 2225 pub fn getGrid(self: Header) ?Grid { 2226 return self.box.getGrid(); 2227 } 2228 2229 pub fn getFocus(self: *Header) *Focus { 2230 return self.box.getFocus(); 2231 } 2232 2233 pub fn getSelectedIndex(self: Header) ?usize { 2234 return self.currentTabIndex(); 2235 } 2236 2237 fn currentTabIndex(self: Header) ?usize { 2238 const child_id = self.box.focus.child_id orelse return null; 2239 return self.tab_ids.getIndex(child_id); 2240 } 2241 }; 2242