╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Issues.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2060 var thread_id: [evt.event_id_size * 2]u8 = undefined; 2061 @memcpy(&thread_id, self.data.selected_id); 2062 const parent_id = if (self.data.comment_id.len == 0) thread_id else blk: { 2063 var parent: [evt.event_id_size * 2]u8 = undefined; 2064 @memcpy(&parent, self.data.comment_id); 2065 break :blk parent; 2066 }; 2067 var event_id_hex: [evt.event_id_size * 2]u8 = undefined; 2068 switch (src.repo_kind) { 2069 inline else => |repo_kind| { 2070 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, .{ .path = src.path }); 2071 defer any_repo.deinit(io, allocator); 2072 switch (any_repo) { 2073 inline else => |*repo| event_id_hex = try evt.Comment.create(repo_kind, repo.self_repo_opts, io, allocator, repo, &thread_id, &parent_id, body, author_email), 2074 } 2075 }, 2076 } 2077 2078 body_input.clear(allocator); 2079 const route = ui.RoutablePage.repoCommentsRoute(self.data.identity, &thread_id, &event_id_hex, 0) orelse return; 2080 try self.session.navigate(route); 2081 } 2082 2083 // the terminal submit paths: the new form commits a new-issue event, the 2084 // edit form re-emits the selected issue's event with the form's content. 2085 // the web posts the forms to their routes instead. 2086 fn submitForm(self: *View, allocator: std.mem.Allocator) !void { 2087 if (self.data.view == .edit) { 2088 try self.submitEditedIssue(allocator); 2089 } else { 2090 try self.submitNewIssue(allocator); 2091 } 2092 } 2093 2094 // commit the new issue to the repo's events branch and navigate to it. 2095 // this is the terminal path; the web posts the form to the issue route, 2096 // so the wasm side never runs (or compiles) the repo access below. 2097 fn submitNewIssue(self: *View, allocator: std.mem.Allocator) !void { 2098 if (comptime wasm) return; 2099 const io = self.session.io orelse return; 2100 const src = self.data.repo_source orelse return; 2101 const author_email = (try self.session.eventAuthorEmail()) orelse return; 2102 2103 const form = self.issueForm() orelse return; 2104 const title_input = &form.children.values()[title_field_index].widget.text_input; 2105 const tags_input = &form.children.values()[tags_field_index].widget.text_input; 2106 const description_input = &form.children.values()[description_field_index].widget.text_input; 2107 2108 const title = try title_input.text(allocator); 2109 defer allocator.free(title); 2110 const tags = try tags_input.text(allocator); 2111 defer allocator.free(tags); 2112 const description = try description_input.text(allocator); 2113 defer allocator.free(description); 2114 2115 if (!evt.Issue.fieldsValid(title, tags)) return; 2116 2117 var id_bytes: [evt.event_id_size]u8 = undefined; 2118 io.random(&id_bytes); 2119 const event_id_hex = std.fmt.bytesToHex(id_bytes, .lower); 2120 2121 const event = evt.EventWithId{ 2122 .id = event_id_hex, 2123 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 2124 .author_email = author_email, 2125 .event = .{ .issue = .{ 2126 .title = title, 2127 .description = description, 2128 .tags = tags, 2129 } }, 2130 }; 2131 2132 switch (src.repo_kind) { 2133 inline else => |repo_kind| { 2134 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, .{ .path = src.path }); 2135 defer any_repo.deinit(io, allocator); 2136 switch (any_repo) { 2137 inline else => |*repo| try evt.consume(repo_kind, repo.self_repo_opts, io, allocator, repo, evt.events_ref, &.{event}), 2138 } 2139 }, 2140 } 2141 2142 // wipe the form so a return visit starts fresh 2143 title_input.clear(allocator); 2144 tags_input.clear(allocator); 2145 description_input.clear(allocator); 2146 2147 const route = ui.RoutablePage.repoIssuesRoute(self.data.identity, .open, "", &event_id_hex) orelse return; 2148 try self.session.navigate(route); 2149 } 2150 2151 // re-emit the selected issue's event with the form's content (status 2152 // preserved), then reload the issue's page. this is the terminal path; 2153 // the web posts the form to the edit route. 2154 fn submitEditedIssue(self: *View, allocator: std.mem.Allocator) !void { 2155 if (comptime wasm) return; 2156 const io = self.session.io orelse return; 2157 const src = self.data.repo_source orelse return; 2158 const entry = self.data.selectedIssue() orelse return; 2159 const author_email = (try self.session.eventAuthorEmail()) orelse return; 2160 2161 const form = self.issueForm() orelse return; 2162 const title_input = &form.children.values()[title_field_index].widget.text_input; 2163 const tags_input = &form.children.values()[tags_field_index].widget.text_input; 2164 const description_input = &form.children.values()[description_field_index].widget.text_input; 2165 2166 const title = try title_input.text(allocator); 2167 defer allocator.free(title); 2168 const tags = try tags_input.text(allocator); 2169 defer allocator.free(tags); 2170 const description = try description_input.text(allocator); 2171 defer allocator.free(description); 2172 2173 if (!evt.Issue.fieldsValid(title, tags)) return; 2174 2175 var id_bytes: [evt.event_id_size]u8 = undefined; 2176 _ = try std.fmt.hexToBytes(&id_bytes, entry.id[0 .. evt.event_id_size * 2]); 2177 2178 switch (src.repo_kind) { 2179 inline else => |repo_kind| { 2180 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, .{ .path = src.path }); 2181 defer any_repo.deinit(io, allocator); 2182 switch (any_repo) { 2183 inline else => |*repo| try evt.Issue.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 2184 .title = title, 2185 .tags = tags, 2186 .description = description, 2187 } }, author_email), 2188 } 2189 }, 2190 } 2191 2192 const route = ui.RoutablePage.repoIssuesRoute(self.data.identity, entry.issue.event.status, "", entry.id) orelse return; 2193 try self.session.navigate(route); 2194 } 2195 2196 // flip the shown issue's status by re-emitting its event, then reload the 2197 // page rooted at the issue so the view reflects the change. this is the 2198 // terminal path; the web posts the button's form to the status route. 2199 fn toggleIssueStatus(self: *View, allocator: std.mem.Allocator, index: usize) !void { 2200 if (comptime wasm) return; 2201 const io = self.session.io orelse return; 2202 const src = self.data.repo_source orelse return; 2203 const sel = self.detailed_index[index] orelse return; 2204 const entry = self.window(index).issues[sel]; 2205 const author_email = (try self.session.eventAuthorEmail()) orelse return; 2206 2207 const status: evt.Issue.Status = switch (entry.issue.event.status) { 2208 .open => .closed, 2209 .closed => .open, 2210 }; 2211 2212 var id_bytes: [evt.event_id_size]u8 = undefined; 2213 _ = try std.fmt.hexToBytes(&id_bytes, entry.id[0 .. evt.event_id_size * 2]); 2214 2215 switch (src.repo_kind) { 2216 inline else => |repo_kind| { 2217 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, .{ .path = src.path }); 2218 defer any_repo.deinit(io, allocator); 2219 switch (any_repo) { 2220 inline else => |*repo| try evt.Issue.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .status = status }, author_email), 2221 } 2222 }, 2223 } 2224 2225 const route = ui.RoutablePage.repoIssuesRoute(self.data.identity, status, "", entry.id) orelse return; 2226 try self.session.navigate(route); 2227 } 2228 2229 // arrow keys move the tag selection; at the flow's edges focus crosses to 2230 // the neighboring widgets. 2231 fn tagsInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 2232 const tf = self.tagFlow(index) orelse return; 2233 const cid = tf.focus.child_id orelse return; 2234 const cur = tf.indexOfFocusId(cid) orelse return; 2235 const count = tf.text_boxes.items.len; 2236 switch (key) { 2237 .arrow_left => if (cur > 0) self.focusTag(index, tf, root_focus, cur - 1) else try self.focusList(index, root_focus), 2238 .arrow_right => if (cur + 1 < count) self.focusTag(index, tf, root_focus, cur + 1), 2239 .arrow_up => if (tf.rowStep(cur, false)) |i| self.focusTag(index, tf, root_focus, i) else self.focusAuthor(index, root_focus), 2240 .arrow_down => if (tf.rowStep(cur, true)) |i| self.focusTag(index, tf, root_focus, i) else try self.focusDescription(index, root_focus), 2241 .home => self.focusTag(index, tf, root_focus, 0), 2242 .end => self.focusTag(index, tf, root_focus, count - 1), 2243 else => {}, 2244 } 2245 } 2246 2247 const button_in_row_index: usize = 1; 2248 const title_child_index: usize = 0; 2249 const tags_child_index: usize = 2; 2250 2251 fn tagFlow(self: *View, index: usize) ?*ui.TagFlow { 2252 const inner = self.detailInner(index); 2253 if (inner.children.count() <= tags_child_index) return null; 2254 return switch (inner.children.values()[tags_child_index].widget) { 2255 .tag_flow => |*tf| tf, 2256 else => null, 2257 }; 2258 } 2259 2260 fn tagsFocused(self: *View, index: usize) bool { 2261 const inner = self.detailInner(index); 2262 const cid = inner.getFocus().child_id orelse return false; 2263 return inner.children.getIndex(cid) == tags_child_index and self.tagFlow(index) != null; 2264 } 2265 2266 // the open/close button inside the detail frame's tool row, identified 2267 // by its posting kind; a logged-out session's or conflicted issue's row 2268 // has none. 2269 fn statusButton(self: *View, index: usize) ?*wgt.TextBox(ui.Widget) { 2270 const row = self.toolRow(index); 2271 if (row.children.count() <= button_in_row_index) return null; 2272 switch (row.children.values()[button_in_row_index].widget) { 2273 .text_box => |*tb| switch (tb.box.focus.kind) { 2274 .custom => |custom| if (std.mem.eql(u8, custom, "submit")) return tb, 2275 else => {}, 2276 }, 2277 else => {}, 2278 } 2279 return null; 2280 } 2281 2282 fn toolRowFocused(self: *View, index: usize) bool { 2283 const outer = self.detailOuter(index); 2284 const cid = outer.getFocus().child_id orelse return false; 2285 return outer.children.getIndex(cid) == tool_row_index; 2286 } 2287 2288 fn focusTag(self: *View, index: usize, tf: *ui.TagFlow, root_focus: *Focus, item: usize) void { 2289 root_focus.setFocus(tf.text_boxes.items[item].getFocus().id); 2290 // keep the tag visible on the terminal: its rect offset by the flow's 2291 // position in the pane. 2292 if (self.session.is_terminal and item < tf.rects.items.len) { 2293 if (self.detailInner(index).children.values()[tags_child_index].rect) |flow_rect| { 2294 var rect = tf.rects.items[item]; 2295 rect.x += flow_rect.x; 2296 rect.y += flow_rect.y; 2297 self.detailScroll(index).scrollToRect(rect); 2298 } 2299 } 2300 } 2301 2302 fn titleFocused(self: *View, index: usize) bool { 2303 const inner = self.detailInner(index); 2304 const cid = inner.getFocus().child_id orelse return false; 2305 return inner.children.getIndex(cid) == title_child_index; 2306 } 2307 2308 fn authorPresent(self: *View, index: usize) bool { 2309 return self.author_id[index] != null; 2310 } 2311 2312 fn authorFocused(self: *View, index: usize) bool { 2313 const inner = self.detailInner(index); 2314 const cid = inner.getFocus().child_id orelse return false; 2315 return cid == self.author_id[index]; 2316 } 2317 2318 fn descriptionFocused(self: *View, index: usize) bool { 2319 const cid = self.detailInner(index).getFocus().child_id orelse return false; 2320 return cid == self.description_id[index]; 2321 } 2322 2323 // the author sits right under the title at the top of the pane, so 2324 // focusing it scrolls there. 2325 fn focusAuthor(self: *View, index: usize, root_focus: *Focus) void { 2326 const id = self.author_id[index] orelse return; 2327 root_focus.setFocus(id); 2328 self.detailScroll(index).y = 0; 2329 } 2330 2331 // the title sits at the top of the pane, so focusing it scrolls there. 2332 fn focusTitle(self: *View, index: usize, root_focus: *Focus) void { 2333 const inner = self.detailInner(index); 2334 if (inner.children.count() == 0) return; 2335 root_focus.setFocus(inner.children.keys()[title_child_index]); 2336 self.detailScroll(index).y = 0; 2337 } 2338 2339 fn focusTags(self: *View, index: usize, root_focus: *Focus) !void { 2340 const tf = self.tagFlow(index) orelse return; 2341 if (tf.text_boxes.items.len == 0) return; 2342 const cid = tf.focus.child_id orelse tf.text_boxes.items[0].getFocus().id; 2343 const item = tf.indexOfFocusId(cid) orelse 0; 2344 self.focusTag(index, tf, root_focus, item); 2345 } 2346 2347 fn focusDescription(self: *View, index: usize, root_focus: *Focus) !void { 2348 if (self.description_id[index]) |id| root_focus.setFocus(id); 2349 } 2350 2351 // return to the tool row's last-focused button (the header when the 2352 // description page shows no row). 2353 fn focusToolRow(self: *View, index: usize, root_focus: *Focus) void { 2354 const cid = self.toolRow(index).getFocus().child_id orelse return self.focusHeader(root_focus); 2355 root_focus.setFocus(cid); 2356 } 2357 2358 // enter the detail pane. an empty pane (no issues) can't be entered. 2359 fn focusDetail(self: *View, index: usize, root_focus: *Focus) !void { 2360 if (self.detailInner(index).children.count() == 0) return; 2361 root_focus.setFocus(self.detailOuter(index).getFocus().id); 2362 } 2363 2364 // return to the list. 2365 fn focusList(self: *View, index: usize, root_focus: *Focus) !void { 2366 root_focus.setFocus(self.listScroll(index).getFocus().id); 2367 } 2368 2369 // cross to the header tabs above the stack. 2370 fn focusHeader(self: *View, root_focus: *Focus) void { 2371 root_focus.setFocus(self.box.children.keys()[header_index]); 2372 } 2373 2374 pub fn clearGrid(self: *View) void { 2375 self.box.clearGrid(); 2376 } 2377 2378 pub fn getGrid(self: View) ?Grid { 2379 return self.box.getGrid(); 2380 } 2381 2382 pub fn getFocus(self: *View) *Focus { 2383 return self.box.getFocus(); 2384 } 2385 2386 // for the parent's "scroll up at the top jumps to the header" check: at the 2387 // top only while the header tabs hold focus, so up from the split 2388 // crosses into the tabs first. 2389 pub fn getSelectedIndex(self: *View) ?usize { 2390 return if (self.headerActive()) 0 else 1; 2391 } 2392 }; 2393 2394 // the "a:" navigation link for `status`'s issues page filtered to the 2395 // url-encoded `tag` and rooted at issue `id` within `identity` ("owner/name"). 2396 // a rooted url ignores `status` (it derives its view from the issue's own); 2397 // with an empty `id` this is the bare list link. 2398 fn issuesLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, status: evt.Issue.Status, tag: []const u8, id: []const u8) ![]const u8 { 2399 const route = ui.RoutablePage.repoIssuesRoute(identity, status, tag, id) orelse return error.RouteTooLong; 2400 const url = try route.toUrl(page_arena); 2401 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2402 } 2403 2404 // the in-page "ai:" anchor for selecting issue `id` in `identity`'s list; the 2405 // href is only followed with js off. 2406 fn issueRowLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, id: []const u8) ![]const u8 { 2407 const route = ui.RoutablePage.repoIssuesRoute(identity, .open, "", id) orelse return error.RouteTooLong; 2408 const url = try route.toUrl(page_arena); 2409 return std.fmt.allocPrint(page_arena.allocator(), "ai:{s}", .{url}); 2410 } 2411 2412 // the "a:" link to the window `status_maybe` names, keeping the tag filter, 2413 // or the conflicts window when null, rooted at issue `id`. 2414 fn windowLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, status_maybe: ?evt.Issue.Status, id: []const u8) ![]const u8 { 2415 if (status_maybe) |status| return issuesLink(page_arena, data.identity, status, data.tag, id); 2416 return conflictsLink(page_arena, data.identity, id); 2417 } 2418 2419 // the "a:" link to the conflicts list rooted at issue `start` ("" = the 2420 // first window). 2421 fn conflictsLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, start: []const u8) ![]const u8 { 2422 const route = ui.RoutablePage.repoIssuesConflictsRoute(identity, start) orelse return error.RouteTooLong; 2423 const url = try route.toUrl(page_arena); 2424 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2425 } 2426 2427 // the "a:" link to the whole-description page of issue `id` within `identity`. 2428 fn descriptionLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, id: []const u8) ![]const u8 { 2429 const route = ui.RoutablePage.repoIssuesDescriptionRoute(identity, id) orelse return error.RouteTooLong; 2430 const url = try route.toUrl(page_arena); 2431 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2432 } 2433 2434 // the "a:" link to `status`'s issues list filtered to `tag` (raw; encoded here). 2435 fn tagLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, status: evt.Issue.Status, tag: []const u8) ![]const u8 { 2436 const encoded = try ui.urlEncodeRef(page_arena.allocator(), tag); 2437 const route = ui.RoutablePage.repoIssuesRoute(identity, status, encoded, "") orelse return error.RouteTooLong; 2438 const url = try route.toUrl(page_arena); 2439 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2440 } 2441 2442 // tabs switching between the issues page's views. 2443 pub const Header = struct { 2444 box: wgt.Box(ui.Widget), 2445 tab_ids: std.AutoArrayHashMapUnmanaged(usize, void), 2446 2447 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self) !Header { 2448 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 2449 errdefer box.deinit(allocator); 2450 2451 var tab_ids: std.AutoArrayHashMapUnmanaged(usize, void) = .empty; 2452 errdefer tab_ids.deinit(allocator); 2453 2454 const aa = session.page_arena.allocator(); 2455 2456 // a list tab per status, labeled with its listing's issue count 2457 for ([_]evt.Issue.Status{ .open, .closed }) |status| { 2458 const route = ui.RoutablePage.repoIssuesRoute(data.identity, status, data.tag, "") orelse return error.RouteTooLong; 2459 const link = try std.fmt.allocPrint(aa, "ai:{s}", .{try route.toUrl(session.page_arena)}); 2460 const label = try std.fmt.allocPrint(aa, "{s} ({d})", .{ @tagName(status), data.window(status).count }); 2461 try addTab(allocator, &box, &tab_ids, label, link); 2462 } 2463 2464 // tags tab, labeled with the active tag filter 2465 { 2466 const tags_route = ui.RoutablePage.repoIssuesTagsRoute(data.identity, data.tag) orelse return error.RouteTooLong; 2467 const tags_link = try std.fmt.allocPrint(aa, "ai:{s}", .{try tags_route.toUrl(session.page_arena)}); 2468 const label = if (data.tag.len == 0) "tags" else blk: { 2469 const decoded = std.Uri.percentDecodeInPlace(try aa.dupe(u8, data.tag)); 2470 break :blk try std.fmt.allocPrint(aa, "tags ({s})", .{decoded}); 2471 }; 2472 try addTab(allocator, &box, &tab_ids, label, tags_link); 2473 } 2474 2475 // new-issue tab; an edit or resolve url shows its tab in this place 2476 { 2477 const route = switch (data.view) { 2478 .edit => ui.RoutablePage.repoIssuesEditRoute(data.identity, data.selected_id) orelse return error.RouteTooLong, 2479 .new_comment => ui.RoutablePage.repoCommentNewRoute(data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong, 2480 .resolve => ui.RoutablePage.repoIssuesResolveRoute(data.identity, data.selected_id, data.theirs_picks) orelse return error.RouteTooLong, 2481 else => ui.RoutablePage.repoIssuesNewRoute(data.identity) orelse return error.RouteTooLong, 2482 }; 2483 const link = try std.fmt.allocPrint(aa, "ai:{s}", .{try route.toUrl(session.page_arena)}); 2484 const label: []const u8 = switch (data.view) { 2485 .edit => "edit", 2486 .new_comment => "reply", 2487 .resolve => "resolve", 2488 else => "new", 2489 }; 2490 try addTab(allocator, &box, &tab_ids, label, link); 2491 } 2492 2493 // conflicts tab, labeled with the conflict count 2494 if (data.conflicts.count > 0) { 2495 const route = ui.RoutablePage.repoIssuesConflictsRoute(data.identity, "") orelse return error.RouteTooLong; 2496 const link = try std.fmt.allocPrint(aa, "ai:{s}", .{try route.toUrl(session.page_arena)}); 2497 const label = try std.fmt.allocPrint(aa, "conflicts ({d})", .{data.conflicts.count}); 2498 try addTab(allocator, &box, &tab_ids, label, link); 2499 } 2500 2501 var self = Header{ .box = box, .tab_ids = tab_ids }; 2502 // the tab matching the page's view is selected initially. 2503 self.getFocus().child_id = self.tab_ids.keys()[View.viewIndex(data.view)]; 2504 return self; 2505 } 2506 2507 fn addTab(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), tab_ids: *std.AutoArrayHashMapUnmanaged(usize, void), label: []const u8, link: []const u8) !void { 2508 var text_box = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 2509 errdefer text_box.deinit(allocator); 2510 text_box.getFocus().focusable = true; 2511 text_box.getFocus().kind = .{ .custom = link }; 2512 try tab_ids.put(allocator, text_box.getFocus().id, {}); 2513 try box.children.put(allocator, text_box.getFocus().id, .{ 2514 .widget = .{ .text_box = text_box }, 2515 .rect = null, 2516 .min_size = .{ .width = label.len + 2, .height = null }, 2517 }); 2518 } 2519 2520 pub fn deinit(self: *Header, allocator: std.mem.Allocator) void { 2521 self.box.deinit(allocator); 2522 self.tab_ids.deinit(allocator); 2523 } 2524 2525 pub fn build(self: *Header, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2526 self.clearGrid(); 2527 // only the selected tab shows its border 2528 for (self.box.children.keys(), self.box.children.values()) |id, *child| { 2529 switch (child.widget) { 2530 .text_box => |*tb| tb.options.border_style = if (self.getFocus().child_id == id) .single else .hidden, 2531 else => {}, 2532 } 2533 } 2534 try self.box.build(allocator, constraint, root_focus); 2535 } 2536 2537 pub fn input(self: *Header, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2538 _ = allocator; 2539 const current_tab = self.currentTabIndex() orelse return; 2540 if (inp.moveTab(key, current_tab, self.tab_ids.count())) |new_tab| { 2541 root_focus.setFocus(self.tab_ids.keys()[new_tab]); 2542 } 2543 } 2544 2545 pub fn clearGrid(self: *Header) void { 2546 self.box.clearGrid(); 2547 } 2548 2549 pub fn getGrid(self: Header) ?Grid { 2550 return self.box.getGrid(); 2551 } 2552 2553 pub fn getFocus(self: *Header) *Focus { 2554 return self.box.getFocus(); 2555 } 2556 2557 pub fn getSelectedIndex(self: Header) ?usize { 2558 return self.currentTabIndex(); 2559 } 2560 2561 fn currentTabIndex(self: Header) ?usize { 2562 const child_id = self.box.focus.child_id orelse return null; 2563 return self.tab_ids.getIndex(child_id); 2564 } 2565 }; 2566