╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Issues.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2549 return std.fmt.allocPrint(page_arena.allocator(), "ai:{s}", .{url}); 2550 } 2551 2552 // the "a:" link to the window `status_maybe` names, keeping the tag filter, 2553 // or the conflicts window when null, rooted at issue `id`. 2554 fn windowLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, status_maybe: ?evt.Issue.Status, id: []const u8) ![]const u8 { 2555 if (status_maybe) |status| return issuesLink(page_arena, data.identity, status, data.tag, id); 2556 return conflictsLink(page_arena, data.identity, id); 2557 } 2558 2559 // the "a:" link to the conflicts list rooted at issue `start` ("" = the 2560 // first window). 2561 fn conflictsLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, start: []const u8) ![]const u8 { 2562 const route = ui.RoutablePage.repoIssuesConflictsRoute(identity, start) orelse return error.RouteTooLong; 2563 const url = try route.toUrl(page_arena); 2564 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2565 } 2566 2567 // the "a:" link to the whole-description page of issue `id` within `identity`. 2568 fn descriptionLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, id: []const u8) ![]const u8 { 2569 const route = ui.RoutablePage.repoIssuesDescriptionRoute(identity, id) orelse return error.RouteTooLong; 2570 const url = try route.toUrl(page_arena); 2571 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2572 } 2573 2574 // the "a:" link to `status`'s issues list filtered to `tag` (raw; encoded here). 2575 fn tagLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, status: evt.Issue.Status, tag: []const u8) ![]const u8 { 2576 const encoded = try ui.urlEncodeRef(page_arena.allocator(), tag); 2577 const route = ui.RoutablePage.repoIssuesRoute(identity, status, encoded, "") orelse return error.RouteTooLong; 2578 const url = try route.toUrl(page_arena); 2579 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2580 } 2581 2582 // tabs switching between the issues page's views. 2583 pub const Header = struct { 2584 box: wgt.Box(ui.Widget), 2585 tab_ids: std.AutoArrayHashMapUnmanaged(usize, void), 2586 2587 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self) !Header { 2588 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 2589 errdefer box.deinit(allocator); 2590 2591 var tab_ids: std.AutoArrayHashMapUnmanaged(usize, void) = .empty; 2592 errdefer tab_ids.deinit(allocator); 2593 2594 const aa = session.page_arena.allocator(); 2595 2596 // a list tab per status, labeled with its listing's issue count 2597 for ([_]evt.Issue.Status{ .open, .closed }) |status| { 2598 const route = ui.RoutablePage.repoIssuesRoute(data.identity, status, data.tag, "") orelse return error.RouteTooLong; 2599 const link = try std.fmt.allocPrint(aa, "ai:{s}", .{try route.toUrl(session.page_arena)}); 2600 const label = try std.fmt.allocPrint(aa, "{s} ({d})", .{ @tagName(status), data.window(status).count }); 2601 try addTab(allocator, &box, &tab_ids, label, link); 2602 } 2603 2604 // tags tab, labeled with the active tag filter 2605 { 2606 const tags_route = ui.RoutablePage.repoIssuesTagsRoute(data.identity, data.tag) orelse return error.RouteTooLong; 2607 const tags_link = try std.fmt.allocPrint(aa, "ai:{s}", .{try tags_route.toUrl(session.page_arena)}); 2608 const label = if (data.tag.len == 0) "tags" else blk: { 2609 const decoded = std.Uri.percentDecodeInPlace(try aa.dupe(u8, data.tag)); 2610 break :blk try std.fmt.allocPrint(aa, "tags ({s})", .{decoded}); 2611 }; 2612 try addTab(allocator, &box, &tab_ids, label, tags_link); 2613 } 2614 2615 // new-issue tab; an edit or resolve url shows its tab in this place 2616 { 2617 const route = switch (data.view) { 2618 .edit => ui.RoutablePage.repoIssuesEditRoute(data.identity, data.selected_id) orelse return error.RouteTooLong, 2619 .new_comment => ui.RoutablePage.repoCommentNewRoute(data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong, 2620 .edit_comment => ui.RoutablePage.repoCommentEditRoute(data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong, 2621 .resolve => ui.RoutablePage.repoIssuesResolveRoute(data.identity, data.selected_id, data.theirs_picks) orelse return error.RouteTooLong, 2622 else => ui.RoutablePage.repoIssuesNewRoute(data.identity) orelse return error.RouteTooLong, 2623 }; 2624 const link = try std.fmt.allocPrint(aa, "ai:{s}", .{try route.toUrl(session.page_arena)}); 2625 const label: []const u8 = switch (data.view) { 2626 .edit => "edit", 2627 .new_comment => "reply", 2628 .edit_comment => "edit", 2629 .resolve => "resolve", 2630 else => "new", 2631 }; 2632 try addTab(allocator, &box, &tab_ids, label, link); 2633 } 2634 2635 // conflicts tab, labeled with the conflict count 2636 if (data.conflicts.count > 0) { 2637 const route = ui.RoutablePage.repoIssuesConflictsRoute(data.identity, "") orelse return error.RouteTooLong; 2638 const link = try std.fmt.allocPrint(aa, "ai:{s}", .{try route.toUrl(session.page_arena)}); 2639 const label = try std.fmt.allocPrint(aa, "conflicts ({d})", .{data.conflicts.count}); 2640 try addTab(allocator, &box, &tab_ids, label, link); 2641 } 2642 2643 var self = Header{ .box = box, .tab_ids = tab_ids }; 2644 // the tab matching the page's view is selected initially. 2645 self.getFocus().child_id = self.tab_ids.keys()[View.viewIndex(data.view)]; 2646 return self; 2647 } 2648 2649 fn addTab(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), tab_ids: *std.AutoArrayHashMapUnmanaged(usize, void), label: []const u8, link: []const u8) !void { 2650 var text_box = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 2651 errdefer text_box.deinit(allocator); 2652 text_box.getFocus().focusable = true; 2653 text_box.getFocus().kind = .{ .custom = link }; 2654 try tab_ids.put(allocator, text_box.getFocus().id, {}); 2655 try box.children.put(allocator, text_box.getFocus().id, .{ 2656 .widget = .{ .text_box = text_box }, 2657 .rect = null, 2658 .min_size = .{ .width = label.len + 2, .height = null }, 2659 }); 2660 } 2661 2662 pub fn deinit(self: *Header, allocator: std.mem.Allocator) void { 2663 self.box.deinit(allocator); 2664 self.tab_ids.deinit(allocator); 2665 } 2666 2667 pub fn build(self: *Header, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2668 self.clearGrid(); 2669 // only the selected tab shows its border 2670 for (self.box.children.keys(), self.box.children.values()) |id, *child| { 2671 switch (child.widget) { 2672 .text_box => |*tb| tb.options.border_style = if (self.getFocus().child_id == id) .single else .hidden, 2673 else => {}, 2674 } 2675 } 2676 try self.box.build(allocator, constraint, root_focus); 2677 } 2678 2679 pub fn input(self: *Header, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2680 _ = allocator; 2681 const current_tab = self.currentTabIndex() orelse return; 2682 if (inp.moveTab(key, current_tab, self.tab_ids.count())) |new_tab| { 2683 root_focus.setFocus(self.tab_ids.keys()[new_tab]); 2684 } 2685 } 2686 2687 pub fn clearGrid(self: *Header) void { 2688 self.box.clearGrid(); 2689 } 2690 2691 pub fn getGrid(self: Header) ?Grid { 2692 return self.box.getGrid(); 2693 } 2694 2695 pub fn getFocus(self: *Header) *Focus { 2696 return self.box.getFocus(); 2697 } 2698 2699 pub fn getSelectedIndex(self: Header) ?usize { 2700 return self.currentTabIndex(); 2701 } 2702 2703 fn currentTabIndex(self: Header) ?usize { 2704 const child_id = self.box.focus.child_id orelse return null; 2705 return self.tab_ids.getIndex(child_id); 2706 } 2707 }; 2708