╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Issues.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2343 const inner = self.detailInner(index); 2344 const cid = inner.getFocus().child_id orelse return false; 2345 return inner.children.getIndex(cid) == title_child_index; 2346 } 2347 2348 fn authorPresent(self: *View, index: usize) bool { 2349 return self.author_id[index] != null; 2350 } 2351 2352 fn authorFocused(self: *View, index: usize) bool { 2353 const inner = self.detailInner(index); 2354 const cid = inner.getFocus().child_id orelse return false; 2355 return cid == self.author_id[index]; 2356 } 2357 2358 fn descriptionFocused(self: *View, index: usize) bool { 2359 const cid = self.detailInner(index).getFocus().child_id orelse return false; 2360 return cid == self.description_id[index]; 2361 } 2362 2363 // the author sits right under the title at the top of the pane, so 2364 // focusing it scrolls there. 2365 fn focusAuthor(self: *View, index: usize, root_focus: *Focus) void { 2366 const id = self.author_id[index] orelse return; 2367 root_focus.setFocus(id); 2368 self.detailScroll(index).y = 0; 2369 } 2370 2371 // the title sits at the top of the pane, so focusing it scrolls there. 2372 fn focusTitle(self: *View, index: usize, root_focus: *Focus) void { 2373 const inner = self.detailInner(index); 2374 if (inner.children.count() == 0) return; 2375 root_focus.setFocus(inner.children.keys()[title_child_index]); 2376 self.detailScroll(index).y = 0; 2377 } 2378 2379 fn focusTags(self: *View, index: usize, root_focus: *Focus) !void { 2380 const tf = self.tagFlow(index) orelse return; 2381 if (tf.text_boxes.items.len == 0) return; 2382 const cid = tf.focus.child_id orelse tf.text_boxes.items[0].getFocus().id; 2383 const item = tf.indexOfFocusId(cid) orelse 0; 2384 self.focusTag(index, tf, root_focus, item); 2385 } 2386 2387 fn focusDescription(self: *View, index: usize, root_focus: *Focus) !void { 2388 if (self.description_id[index]) |id| root_focus.setFocus(id); 2389 } 2390 2391 // return to the tool row's last-focused button (the header when the 2392 // description page shows no row). 2393 fn focusToolRow(self: *View, index: usize, root_focus: *Focus) void { 2394 const cid = self.toolRow(index).getFocus().child_id orelse return self.focusHeader(root_focus); 2395 root_focus.setFocus(cid); 2396 } 2397 2398 // enter the detail pane. an empty pane (no issues) can't be entered. 2399 fn focusDetail(self: *View, index: usize, root_focus: *Focus) !void { 2400 if (self.detailInner(index).children.count() == 0) return; 2401 root_focus.setFocus(self.detailOuter(index).getFocus().id); 2402 } 2403 2404 // return to the list. 2405 fn focusList(self: *View, index: usize, root_focus: *Focus) !void { 2406 root_focus.setFocus(self.listScroll(index).getFocus().id); 2407 } 2408 2409 // cross to the header tabs above the stack. 2410 fn focusHeader(self: *View, root_focus: *Focus) void { 2411 root_focus.setFocus(self.box.children.keys()[header_index]); 2412 } 2413 2414 pub fn clearGrid(self: *View) void { 2415 self.box.clearGrid(); 2416 } 2417 2418 pub fn getGrid(self: View) ?Grid { 2419 return self.box.getGrid(); 2420 } 2421 2422 pub fn getFocus(self: *View) *Focus { 2423 return self.box.getFocus(); 2424 } 2425 2426 // for the parent's "scroll up at the top jumps to the header" check: at the 2427 // top only while the header tabs hold focus, so up from the split 2428 // crosses into the tabs first. 2429 pub fn getSelectedIndex(self: *View) ?usize { 2430 return if (self.headerActive()) 0 else 1; 2431 } 2432 }; 2433 2434 // the "a:" navigation link for `status`'s issues page filtered to the 2435 // url-encoded `tag` and rooted at issue `id` within `identity` ("owner/name"). 2436 // a rooted url ignores `status` (it derives its view from the issue's own); 2437 // with an empty `id` this is the bare list link. 2438 fn issuesLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, status: evt.Issue.Status, tag: []const u8, id: []const u8) ![]const u8 { 2439 const route = ui.RoutablePage.repoIssuesRoute(identity, status, tag, id) orelse return error.RouteTooLong; 2440 const url = try route.toUrl(page_arena); 2441 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2442 } 2443 2444 // the in-page "ai:" anchor for selecting issue `id` in `identity`'s list; the 2445 // href is only followed with js off. 2446 fn issueRowLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, id: []const u8) ![]const u8 { 2447 const route = ui.RoutablePage.repoIssuesRoute(identity, .open, "", id) orelse return error.RouteTooLong; 2448 const url = try route.toUrl(page_arena); 2449 return std.fmt.allocPrint(page_arena.allocator(), "ai:{s}", .{url}); 2450 } 2451 2452 // the "a:" link to the window `status_maybe` names, keeping the tag filter, 2453 // or the conflicts window when null, rooted at issue `id`. 2454 fn windowLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, status_maybe: ?evt.Issue.Status, id: []const u8) ![]const u8 { 2455 if (status_maybe) |status| return issuesLink(page_arena, data.identity, status, data.tag, id); 2456 return conflictsLink(page_arena, data.identity, id); 2457 } 2458 2459 // the "a:" link to the conflicts list rooted at issue `start` ("" = the 2460 // first window). 2461 fn conflictsLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, start: []const u8) ![]const u8 { 2462 const route = ui.RoutablePage.repoIssuesConflictsRoute(identity, start) orelse return error.RouteTooLong; 2463 const url = try route.toUrl(page_arena); 2464 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2465 } 2466 2467 // the "a:" link to the whole-description page of issue `id` within `identity`. 2468 fn descriptionLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, id: []const u8) ![]const u8 { 2469 const route = ui.RoutablePage.repoIssuesDescriptionRoute(identity, id) orelse return error.RouteTooLong; 2470 const url = try route.toUrl(page_arena); 2471 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2472 } 2473 2474 // the "a:" link to `status`'s issues list filtered to `tag` (raw; encoded here). 2475 fn tagLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, status: evt.Issue.Status, tag: []const u8) ![]const u8 { 2476 const encoded = try ui.urlEncodeRef(page_arena.allocator(), tag); 2477 const route = ui.RoutablePage.repoIssuesRoute(identity, status, encoded, "") orelse return error.RouteTooLong; 2478 const url = try route.toUrl(page_arena); 2479 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2480 } 2481 2482 // tabs switching between the issues page's views. 2483 pub const Header = struct { 2484 box: wgt.Box(ui.Widget), 2485 tab_ids: std.AutoArrayHashMapUnmanaged(usize, void), 2486 2487 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self) !Header { 2488 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 2489 errdefer box.deinit(allocator); 2490 2491 var tab_ids: std.AutoArrayHashMapUnmanaged(usize, void) = .empty; 2492 errdefer tab_ids.deinit(allocator); 2493 2494 const aa = session.page_arena.allocator(); 2495 2496 // a list tab per status, labeled with its listing's issue count 2497 for ([_]evt.Issue.Status{ .open, .closed }) |status| { 2498 const route = ui.RoutablePage.repoIssuesRoute(data.identity, status, data.tag, "") orelse return error.RouteTooLong; 2499 const link = try std.fmt.allocPrint(aa, "ai:{s}", .{try route.toUrl(session.page_arena)}); 2500 const label = try std.fmt.allocPrint(aa, "{s} ({d})", .{ @tagName(status), data.window(status).count }); 2501 try addTab(allocator, &box, &tab_ids, label, link); 2502 } 2503 2504 // tags tab, labeled with the active tag filter 2505 { 2506 const tags_route = ui.RoutablePage.repoIssuesTagsRoute(data.identity, data.tag) orelse return error.RouteTooLong; 2507 const tags_link = try std.fmt.allocPrint(aa, "ai:{s}", .{try tags_route.toUrl(session.page_arena)}); 2508 const label = if (data.tag.len == 0) "tags" else blk: { 2509 const decoded = std.Uri.percentDecodeInPlace(try aa.dupe(u8, data.tag)); 2510 break :blk try std.fmt.allocPrint(aa, "tags ({s})", .{decoded}); 2511 }; 2512 try addTab(allocator, &box, &tab_ids, label, tags_link); 2513 } 2514 2515 // new-issue tab; an edit or resolve url shows its tab in this place 2516 { 2517 const route = switch (data.view) { 2518 .edit => ui.RoutablePage.repoIssuesEditRoute(data.identity, data.selected_id) orelse return error.RouteTooLong, 2519 .new_comment => ui.RoutablePage.repoCommentNewRoute(data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong, 2520 .edit_comment => ui.RoutablePage.repoCommentEditRoute(data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong, 2521 .resolve => ui.RoutablePage.repoIssuesResolveRoute(data.identity, data.selected_id, data.theirs_picks) orelse return error.RouteTooLong, 2522 else => ui.RoutablePage.repoIssuesNewRoute(data.identity) orelse return error.RouteTooLong, 2523 }; 2524 const link = try std.fmt.allocPrint(aa, "ai:{s}", .{try route.toUrl(session.page_arena)}); 2525 const label: []const u8 = switch (data.view) { 2526 .edit => "edit", 2527 .new_comment => "reply", 2528 .edit_comment => "edit", 2529 .resolve => "resolve", 2530 else => "new", 2531 }; 2532 try addTab(allocator, &box, &tab_ids, label, link); 2533 } 2534 2535 // conflicts tab, labeled with the conflict count 2536 if (data.conflicts.count > 0) { 2537 const route = ui.RoutablePage.repoIssuesConflictsRoute(data.identity, "") orelse return error.RouteTooLong; 2538 const link = try std.fmt.allocPrint(aa, "ai:{s}", .{try route.toUrl(session.page_arena)}); 2539 const label = try std.fmt.allocPrint(aa, "conflicts ({d})", .{data.conflicts.count}); 2540 try addTab(allocator, &box, &tab_ids, label, link); 2541 } 2542 2543 var self = Header{ .box = box, .tab_ids = tab_ids }; 2544 // the tab matching the page's view is selected initially. 2545 self.getFocus().child_id = self.tab_ids.keys()[View.viewIndex(data.view)]; 2546 return self; 2547 } 2548 2549 fn addTab(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), tab_ids: *std.AutoArrayHashMapUnmanaged(usize, void), label: []const u8, link: []const u8) !void { 2550 var text_box = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 2551 errdefer text_box.deinit(allocator); 2552 text_box.getFocus().focusable = true; 2553 text_box.getFocus().kind = .{ .custom = link }; 2554 try tab_ids.put(allocator, text_box.getFocus().id, {}); 2555 try box.children.put(allocator, text_box.getFocus().id, .{ 2556 .widget = .{ .text_box = text_box }, 2557 .rect = null, 2558 .min_size = .{ .width = label.len + 2, .height = null }, 2559 }); 2560 } 2561 2562 pub fn deinit(self: *Header, allocator: std.mem.Allocator) void { 2563 self.box.deinit(allocator); 2564 self.tab_ids.deinit(allocator); 2565 } 2566 2567 pub fn build(self: *Header, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2568 self.clearGrid(); 2569 // only the selected tab shows its border 2570 for (self.box.children.keys(), self.box.children.values()) |id, *child| { 2571 switch (child.widget) { 2572 .text_box => |*tb| tb.options.border_style = if (self.getFocus().child_id == id) .single else .hidden, 2573 else => {}, 2574 } 2575 } 2576 try self.box.build(allocator, constraint, root_focus); 2577 } 2578 2579 pub fn input(self: *Header, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2580 _ = allocator; 2581 const current_tab = self.currentTabIndex() orelse return; 2582 if (inp.moveTab(key, current_tab, self.tab_ids.count())) |new_tab| { 2583 root_focus.setFocus(self.tab_ids.keys()[new_tab]); 2584 } 2585 } 2586 2587 pub fn clearGrid(self: *Header) void { 2588 self.box.clearGrid(); 2589 } 2590 2591 pub fn getGrid(self: Header) ?Grid { 2592 return self.box.getGrid(); 2593 } 2594 2595 pub fn getFocus(self: *Header) *Focus { 2596 return self.box.getFocus(); 2597 } 2598 2599 pub fn getSelectedIndex(self: Header) ?usize { 2600 return self.currentTabIndex(); 2601 } 2602 2603 fn currentTabIndex(self: Header) ?usize { 2604 const child_id = self.box.focus.child_id orelse return null; 2605 return self.tab_ids.getIndex(child_id); 2606 } 2607 }; 2608