╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Issues.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2244 } 2245 2246 const button_in_row_index: usize = 1; 2247 const title_child_index: usize = 0; 2248 const tags_child_index: usize = 2; 2249 2250 fn tagFlow(self: *View, index: usize) ?*ui.TagFlow { 2251 const inner = self.detailInner(index); 2252 if (inner.children.count() <= tags_child_index) return null; 2253 return switch (inner.children.values()[tags_child_index].widget) { 2254 .tag_flow => |*tf| tf, 2255 else => null, 2256 }; 2257 } 2258 2259 fn tagsFocused(self: *View, index: usize) bool { 2260 const inner = self.detailInner(index); 2261 const cid = inner.getFocus().child_id orelse return false; 2262 return inner.children.getIndex(cid) == tags_child_index and self.tagFlow(index) != null; 2263 } 2264 2265 // the open/close button inside the detail frame's tool row, identified 2266 // by its posting kind; a logged-out session's or conflicted issue's row 2267 // has none. 2268 fn statusButton(self: *View, index: usize) ?*wgt.TextBox(ui.Widget) { 2269 const row = self.toolRow(index); 2270 if (row.children.count() <= button_in_row_index) return null; 2271 switch (row.children.values()[button_in_row_index].widget) { 2272 .text_box => |*tb| switch (tb.box.focus.kind) { 2273 .custom => |custom| if (std.mem.eql(u8, custom, "submit")) return tb, 2274 else => {}, 2275 }, 2276 else => {}, 2277 } 2278 return null; 2279 } 2280 2281 fn toolRowFocused(self: *View, index: usize) bool { 2282 const outer = self.detailOuter(index); 2283 const cid = outer.getFocus().child_id orelse return false; 2284 return outer.children.getIndex(cid) == tool_row_index; 2285 } 2286 2287 fn focusTag(self: *View, index: usize, tf: *ui.TagFlow, root_focus: *Focus, item: usize) void { 2288 root_focus.setFocus(tf.text_boxes.items[item].getFocus().id); 2289 // keep the tag visible on the terminal: its rect offset by the flow's 2290 // position in the pane. 2291 if (self.session.is_terminal and item < tf.rects.items.len) { 2292 if (self.detailInner(index).children.values()[tags_child_index].rect) |flow_rect| { 2293 var rect = tf.rects.items[item]; 2294 rect.x += flow_rect.x; 2295 rect.y += flow_rect.y; 2296 self.detailScroll(index).scrollToRect(rect); 2297 } 2298 } 2299 } 2300 2301 fn titleFocused(self: *View, index: usize) bool { 2302 const inner = self.detailInner(index); 2303 const cid = inner.getFocus().child_id orelse return false; 2304 return inner.children.getIndex(cid) == title_child_index; 2305 } 2306 2307 fn authorPresent(self: *View, index: usize) bool { 2308 return self.author_id[index] != null; 2309 } 2310 2311 fn authorFocused(self: *View, index: usize) bool { 2312 const inner = self.detailInner(index); 2313 const cid = inner.getFocus().child_id orelse return false; 2314 return cid == self.author_id[index]; 2315 } 2316 2317 fn descriptionFocused(self: *View, index: usize) bool { 2318 const cid = self.detailInner(index).getFocus().child_id orelse return false; 2319 return cid == self.description_id[index]; 2320 } 2321 2322 // the author sits right under the title at the top of the pane, so 2323 // focusing it scrolls there. 2324 fn focusAuthor(self: *View, index: usize, root_focus: *Focus) void { 2325 const id = self.author_id[index] orelse return; 2326 root_focus.setFocus(id); 2327 self.detailScroll(index).y = 0; 2328 } 2329 2330 // the title sits at the top of the pane, so focusing it scrolls there. 2331 fn focusTitle(self: *View, index: usize, root_focus: *Focus) void { 2332 const inner = self.detailInner(index); 2333 if (inner.children.count() == 0) return; 2334 root_focus.setFocus(inner.children.keys()[title_child_index]); 2335 self.detailScroll(index).y = 0; 2336 } 2337 2338 fn focusTags(self: *View, index: usize, root_focus: *Focus) !void { 2339 const tf = self.tagFlow(index) orelse return; 2340 if (tf.text_boxes.items.len == 0) return; 2341 const cid = tf.focus.child_id orelse tf.text_boxes.items[0].getFocus().id; 2342 const item = tf.indexOfFocusId(cid) orelse 0; 2343 self.focusTag(index, tf, root_focus, item); 2344 } 2345 2346 fn focusDescription(self: *View, index: usize, root_focus: *Focus) !void { 2347 if (self.description_id[index]) |id| root_focus.setFocus(id); 2348 } 2349 2350 // return to the tool row's last-focused button (the header when the 2351 // description page shows no row). 2352 fn focusToolRow(self: *View, index: usize, root_focus: *Focus) void { 2353 const cid = self.toolRow(index).getFocus().child_id orelse return self.focusHeader(root_focus); 2354 root_focus.setFocus(cid); 2355 } 2356 2357 // enter the detail pane. an empty pane (no issues) can't be entered. 2358 fn focusDetail(self: *View, index: usize, root_focus: *Focus) !void { 2359 if (self.detailInner(index).children.count() == 0) return; 2360 root_focus.setFocus(self.detailOuter(index).getFocus().id); 2361 } 2362 2363 // return to the list. 2364 fn focusList(self: *View, index: usize, root_focus: *Focus) !void { 2365 root_focus.setFocus(self.listScroll(index).getFocus().id); 2366 } 2367 2368 // cross to the header tabs above the stack. 2369 fn focusHeader(self: *View, root_focus: *Focus) void { 2370 root_focus.setFocus(self.box.children.keys()[header_index]); 2371 } 2372 2373 pub fn clearGrid(self: *View) void { 2374 self.box.clearGrid(); 2375 } 2376 2377 pub fn getGrid(self: View) ?Grid { 2378 return self.box.getGrid(); 2379 } 2380 2381 pub fn getFocus(self: *View) *Focus { 2382 return self.box.getFocus(); 2383 } 2384 2385 // for the parent's "scroll up at the top jumps to the header" check: at the 2386 // top only while the header tabs hold focus, so up from the split 2387 // crosses into the tabs first. 2388 pub fn getSelectedIndex(self: *View) ?usize { 2389 return if (self.headerActive()) 0 else 1; 2390 } 2391 }; 2392 2393 // the "a:" navigation link for `status`'s issues page filtered to the 2394 // url-encoded `tag` and rooted at issue `id` within `identity` ("owner/name"). 2395 // a rooted url ignores `status` (it derives its view from the issue's own); 2396 // with an empty `id` this is the bare list link. 2397 fn issuesLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, status: evt.Issue.Status, tag: []const u8, id: []const u8) ![]const u8 { 2398 const route = ui.RoutablePage.repoIssuesRoute(identity, status, tag, id) orelse return error.RouteTooLong; 2399 const url = try route.toUrl(page_arena); 2400 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2401 } 2402 2403 // the in-page "ai:" anchor for selecting issue `id` in `identity`'s list; the 2404 // href is only followed with js off. 2405 fn issueRowLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, id: []const u8) ![]const u8 { 2406 const route = ui.RoutablePage.repoIssuesRoute(identity, .open, "", id) orelse return error.RouteTooLong; 2407 const url = try route.toUrl(page_arena); 2408 return std.fmt.allocPrint(page_arena.allocator(), "ai:{s}", .{url}); 2409 } 2410 2411 // the "a:" link to the window `status_maybe` names, keeping the tag filter, 2412 // or the conflicts window when null, rooted at issue `id`. 2413 fn windowLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, status_maybe: ?evt.Issue.Status, id: []const u8) ![]const u8 { 2414 if (status_maybe) |status| return issuesLink(page_arena, data.identity, status, data.tag, id); 2415 return conflictsLink(page_arena, data.identity, id); 2416 } 2417 2418 // the "a:" link to the conflicts list rooted at issue `start` ("" = the 2419 // first window). 2420 fn conflictsLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, start: []const u8) ![]const u8 { 2421 const route = ui.RoutablePage.repoIssuesConflictsRoute(identity, start) orelse return error.RouteTooLong; 2422 const url = try route.toUrl(page_arena); 2423 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2424 } 2425 2426 // the "a:" link to the whole-description page of issue `id` within `identity`. 2427 fn descriptionLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, id: []const u8) ![]const u8 { 2428 const route = ui.RoutablePage.repoIssuesDescriptionRoute(identity, id) orelse return error.RouteTooLong; 2429 const url = try route.toUrl(page_arena); 2430 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2431 } 2432 2433 // the "a:" link to `status`'s issues list filtered to `tag` (raw; encoded here). 2434 fn tagLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, status: evt.Issue.Status, tag: []const u8) ![]const u8 { 2435 const encoded = try ui.urlEncodeRef(page_arena.allocator(), tag); 2436 const route = ui.RoutablePage.repoIssuesRoute(identity, status, encoded, "") orelse return error.RouteTooLong; 2437 const url = try route.toUrl(page_arena); 2438 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2439 } 2440 2441 // tabs switching between the issues page's views. 2442 pub const Header = struct { 2443 box: wgt.Box(ui.Widget), 2444 tab_ids: std.AutoArrayHashMapUnmanaged(usize, void), 2445 2446 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self) !Header { 2447 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 2448 errdefer box.deinit(allocator); 2449 2450 var tab_ids: std.AutoArrayHashMapUnmanaged(usize, void) = .empty; 2451 errdefer tab_ids.deinit(allocator); 2452 2453 const aa = session.page_arena.allocator(); 2454 2455 // a list tab per status, labeled with its listing's issue count 2456 for ([_]evt.Issue.Status{ .open, .closed }) |status| { 2457 const route = ui.RoutablePage.repoIssuesRoute(data.identity, status, data.tag, "") orelse return error.RouteTooLong; 2458 const link = try std.fmt.allocPrint(aa, "ai:{s}", .{try route.toUrl(session.page_arena)}); 2459 const label = try std.fmt.allocPrint(aa, "{s} ({d})", .{ @tagName(status), data.window(status).count }); 2460 try addTab(allocator, &box, &tab_ids, label, link); 2461 } 2462 2463 // tags tab, labeled with the active tag filter 2464 { 2465 const tags_route = ui.RoutablePage.repoIssuesTagsRoute(data.identity, data.tag) orelse return error.RouteTooLong; 2466 const tags_link = try std.fmt.allocPrint(aa, "ai:{s}", .{try tags_route.toUrl(session.page_arena)}); 2467 const label = if (data.tag.len == 0) "tags" else blk: { 2468 const decoded = std.Uri.percentDecodeInPlace(try aa.dupe(u8, data.tag)); 2469 break :blk try std.fmt.allocPrint(aa, "tags ({s})", .{decoded}); 2470 }; 2471 try addTab(allocator, &box, &tab_ids, label, tags_link); 2472 } 2473 2474 // new-issue tab; an edit or resolve url shows its tab in this place 2475 { 2476 const route = switch (data.view) { 2477 .edit => ui.RoutablePage.repoIssuesEditRoute(data.identity, data.selected_id) orelse return error.RouteTooLong, 2478 .new_comment => ui.RoutablePage.repoCommentNewRoute(data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong, 2479 .resolve => ui.RoutablePage.repoIssuesResolveRoute(data.identity, data.selected_id, data.theirs_picks) orelse return error.RouteTooLong, 2480 else => ui.RoutablePage.repoIssuesNewRoute(data.identity) orelse return error.RouteTooLong, 2481 }; 2482 const link = try std.fmt.allocPrint(aa, "ai:{s}", .{try route.toUrl(session.page_arena)}); 2483 const label: []const u8 = switch (data.view) { 2484 .edit => "edit", 2485 .new_comment => "reply", 2486 .resolve => "resolve", 2487 else => "new", 2488 }; 2489 try addTab(allocator, &box, &tab_ids, label, link); 2490 } 2491 2492 // conflicts tab, labeled with the conflict count 2493 if (data.conflicts.count > 0) { 2494 const route = ui.RoutablePage.repoIssuesConflictsRoute(data.identity, "") orelse return error.RouteTooLong; 2495 const link = try std.fmt.allocPrint(aa, "ai:{s}", .{try route.toUrl(session.page_arena)}); 2496 const label = try std.fmt.allocPrint(aa, "conflicts ({d})", .{data.conflicts.count}); 2497 try addTab(allocator, &box, &tab_ids, label, link); 2498 } 2499 2500 var self = Header{ .box = box, .tab_ids = tab_ids }; 2501 // the tab matching the page's view is selected initially. 2502 self.getFocus().child_id = self.tab_ids.keys()[View.viewIndex(data.view)]; 2503 return self; 2504 } 2505 2506 fn addTab(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), tab_ids: *std.AutoArrayHashMapUnmanaged(usize, void), label: []const u8, link: []const u8) !void { 2507 var text_box = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 2508 errdefer text_box.deinit(allocator); 2509 text_box.getFocus().focusable = true; 2510 text_box.getFocus().kind = .{ .custom = link }; 2511 try tab_ids.put(allocator, text_box.getFocus().id, {}); 2512 try box.children.put(allocator, text_box.getFocus().id, .{ 2513 .widget = .{ .text_box = text_box }, 2514 .rect = null, 2515 .min_size = .{ .width = label.len + 2, .height = null }, 2516 }); 2517 } 2518 2519 pub fn deinit(self: *Header, allocator: std.mem.Allocator) void { 2520 self.box.deinit(allocator); 2521 self.tab_ids.deinit(allocator); 2522 } 2523 2524 pub fn build(self: *Header, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2525 self.clearGrid(); 2526 // only the selected tab shows its border 2527 for (self.box.children.keys(), self.box.children.values()) |id, *child| { 2528 switch (child.widget) { 2529 .text_box => |*tb| tb.options.border_style = if (self.getFocus().child_id == id) .single else .hidden, 2530 else => {}, 2531 } 2532 } 2533 try self.box.build(allocator, constraint, root_focus); 2534 } 2535 2536 pub fn input(self: *Header, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2537 _ = allocator; 2538 const current_tab = self.currentTabIndex() orelse return; 2539 if (inp.moveTab(key, current_tab, self.tab_ids.count())) |new_tab| { 2540 root_focus.setFocus(self.tab_ids.keys()[new_tab]); 2541 } 2542 } 2543 2544 pub fn clearGrid(self: *Header) void { 2545 self.box.clearGrid(); 2546 } 2547 2548 pub fn getGrid(self: Header) ?Grid { 2549 return self.box.getGrid(); 2550 } 2551 2552 pub fn getFocus(self: *Header) *Focus { 2553 return self.box.getFocus(); 2554 } 2555 2556 pub fn getSelectedIndex(self: Header) ?usize { 2557 return self.currentTabIndex(); 2558 } 2559 2560 fn currentTabIndex(self: Header) ?usize { 2561 const child_id = self.box.focus.child_id orelse return null; 2562 return self.tab_ids.getIndex(child_id); 2563 } 2564 }; 2565