╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Issues.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2505 } 2506 }, 2507 } 2508 2509 const route = ui.RoutablePage.repoIssuesRoute(self.data.identity, status, "", entry.id) orelse return; 2510 try self.session.navigate(route); 2511 } 2512 2513 // arrow keys move the tag selection; at the flow's edges focus crosses to 2514 // the neighboring widgets. 2515 fn tagsInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 2516 const tf = self.tagFlow(index) orelse return; 2517 const cid = tf.focus.child_id orelse return; 2518 const cur = tf.indexOfFocusId(cid) orelse return; 2519 const count = tf.text_boxes.items.len; 2520 switch (key) { 2521 .arrow_left => if (cur > 0) self.focusTag(index, tf, root_focus, cur - 1) else try self.focusList(index, root_focus), 2522 .arrow_right => if (cur + 1 < count) self.focusTag(index, tf, root_focus, cur + 1), 2523 .arrow_up => if (tf.rowStep(cur, false)) |i| { 2524 self.focusTag(index, tf, root_focus, i); 2525 } else { 2526 _ = self.moveDetailVertical(index, root_focus, false); 2527 }, 2528 .arrow_down => if (tf.rowStep(cur, true)) |i| { 2529 self.focusTag(index, tf, root_focus, i); 2530 } else { 2531 _ = self.moveDetailVertical(index, root_focus, true); 2532 }, 2533 .home => self.focusTag(index, tf, root_focus, 0), 2534 .end => self.focusTag(index, tf, root_focus, count - 1), 2535 else => {}, 2536 } 2537 } 2538 2539 const title_child_index: usize = 0; 2540 const tags_child_index: usize = 2; 2541 2542 // the tool row's first child after the spacer that pushes it right 2543 const first_in_row_index: usize = 1; 2544 2545 fn tagFlow(self: *View, index: usize) ?*ui.TagFlow { 2546 const inner = self.detailInner(index); 2547 if (inner.children.count() <= tags_child_index) return null; 2548 return switch (inner.children.values()[tags_child_index].widget) { 2549 .tag_flow => |*tf| tf, 2550 else => null, 2551 }; 2552 } 2553 2554 fn tagsFocused(self: *View, index: usize) bool { 2555 const inner = self.detailInner(index); 2556 const cid = inner.getFocus().child_id orelse return false; 2557 return inner.children.getIndex(cid) == tags_child_index and self.tagFlow(index) != null; 2558 } 2559 2560 fn toolRowFocused(self: *View, index: usize) bool { 2561 const outer = self.detailOuter(index); 2562 const cid = outer.getFocus().child_id orelse return false; 2563 return outer.children.getIndex(cid) == tool_row_index; 2564 } 2565 2566 fn focusTag(self: *View, index: usize, tf: *ui.TagFlow, root_focus: *Focus, item: usize) void { 2567 root_focus.setFocus(tf.text_boxes.items[item].getFocus().id); 2568 // keep the tag visible on the terminal: its rect offset by the flow's 2569 // position in the pane. 2570 if (self.session.is_terminal and item < tf.rects.items.len) { 2571 if (self.detailInner(index).children.values()[tags_child_index].rect) |flow_rect| { 2572 var rect = tf.rects.items[item]; 2573 rect.x += flow_rect.x; 2574 rect.y += flow_rect.y; 2575 self.detailScroll(index).scrollToRect(rect); 2576 } 2577 } 2578 } 2579 2580 fn titleFocused(self: *View, index: usize) bool { 2581 const inner = self.detailInner(index); 2582 const cid = inner.getFocus().child_id orelse return false; 2583 return inner.children.getIndex(cid) == title_child_index; 2584 } 2585 2586 fn authorFocused(self: *View, index: usize) bool { 2587 const inner = self.detailInner(index); 2588 const cid = inner.getFocus().child_id orelse return false; 2589 return cid == self.author_id[index]; 2590 } 2591 2592 fn descriptionFocused(self: *View, index: usize) bool { 2593 const cid = self.detailInner(index).getFocus().child_id orelse return false; 2594 return cid == self.description_id[index]; 2595 } 2596 2597 // return to the tool row's last-focused button (the header when the 2598 // description page shows no row). 2599 fn focusToolRow(self: *View, index: usize, root_focus: *Focus) void { 2600 const cid = self.toolRow(index).getFocus().child_id orelse return self.focusHeader(root_focus); 2601 root_focus.setFocus(cid); 2602 } 2603 2604 // enter the detail pane. an empty pane (no issues) can't be entered. 2605 fn focusDetail(self: *View, index: usize, root_focus: *Focus) !void { 2606 if (self.detailInner(index).children.count() == 0) return; 2607 root_focus.setFocus(self.detailOuter(index).getFocus().id); 2608 } 2609 2610 // return to the list. 2611 fn focusList(self: *View, index: usize, root_focus: *Focus) !void { 2612 root_focus.setFocus(self.listScroll(index).getFocus().id); 2613 } 2614 2615 // cross to the header tabs above the stack. 2616 fn focusHeader(self: *View, root_focus: *Focus) void { 2617 root_focus.setFocus(self.box.children.keys()[header_index]); 2618 } 2619 2620 pub fn clearGrid(self: *View) void { 2621 self.box.clearGrid(); 2622 } 2623 2624 pub fn getGrid(self: View) ?Grid { 2625 return self.box.getGrid(); 2626 } 2627 2628 pub fn getFocus(self: *View) *Focus { 2629 return self.box.getFocus(); 2630 } 2631 2632 // for the parent's "scroll up at the top jumps to the header" check: at the 2633 // top only while the header tabs hold focus, so up from the split 2634 // crosses into the tabs first. 2635 pub fn getSelectedIndex(self: *View) ?usize { 2636 return if (self.headerActive()) 0 else 1; 2637 } 2638 }; 2639 2640 // the "a:" navigation link for `status`'s issues page filtered to the 2641 // url-encoded `tag` and rooted at issue `id` within `identity` ("owner/name"). 2642 // a rooted url ignores `status` (it derives its view from the issue's own); 2643 // with an empty `id` this is the bare list link. 2644 fn issuesLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, status: evt.Issue.Status, tag: []const u8, id: []const u8) ![]const u8 { 2645 const route = ui.RoutablePage.repoIssuesRoute(identity, status, tag, id) orelse return error.RouteTooLong; 2646 const url = try route.toUrl(page_arena); 2647 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2648 } 2649 2650 // the in-page "ai:" anchor for selecting issue `id` in `identity`'s list; the 2651 // href is only followed with js off. 2652 fn issueRowLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, id: []const u8) ![]const u8 { 2653 const route = ui.RoutablePage.repoIssuesRoute(identity, .open, "", id) orelse return error.RouteTooLong; 2654 const url = try route.toUrl(page_arena); 2655 return std.fmt.allocPrint(page_arena.allocator(), "ai:{s}", .{url}); 2656 } 2657 2658 // the "a:" link to the window `status_maybe` names, keeping the tag filter, 2659 // or the conflicts window when null, rooted at issue `id`. 2660 fn windowLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, status_maybe: ?evt.Issue.Status, id: []const u8) ![]const u8 { 2661 if (status_maybe) |status| return issuesLink(page_arena, data.identity, status, data.tag, id); 2662 return conflictsLink(page_arena, data.identity, id); 2663 } 2664 2665 // the "a:" link to the conflicts list rooted at issue `start` ("" = the 2666 // first window). 2667 fn conflictsLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, start: []const u8) ![]const u8 { 2668 const route = ui.RoutablePage.repoIssuesConflictsRoute(identity, start) orelse return error.RouteTooLong; 2669 const url = try route.toUrl(page_arena); 2670 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2671 } 2672 2673 // the "a:" link to the whole-description page of issue `id` within `identity`. 2674 fn descriptionLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, id: []const u8) ![]const u8 { 2675 const route = ui.RoutablePage.repoIssuesDescriptionRoute(identity, id) orelse return error.RouteTooLong; 2676 const url = try route.toUrl(page_arena); 2677 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2678 } 2679 2680 // the "a:" link to `status`'s issues list filtered to `tag` (raw; encoded here). 2681 fn tagLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, status: evt.Issue.Status, tag: []const u8) ![]const u8 { 2682 const encoded = try ui.urlEncodeRef(page_arena.allocator(), tag); 2683 const route = ui.RoutablePage.repoIssuesRoute(identity, status, encoded, "") orelse return error.RouteTooLong; 2684 const url = try route.toUrl(page_arena); 2685 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2686 } 2687 2688 // tabs switching between the issues page's views. 2689 pub const Header = struct { 2690 box: wgt.Box(ui.Widget), 2691 tab_ids: std.AutoArrayHashMapUnmanaged(usize, void), 2692 2693 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self) !Header { 2694 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 2695 errdefer box.deinit(allocator); 2696 2697 var tab_ids: std.AutoArrayHashMapUnmanaged(usize, void) = .empty; 2698 errdefer tab_ids.deinit(allocator); 2699 2700 const aa = session.page_arena.allocator(); 2701 2702 // a list tab per status, labeled with its listing's issue count 2703 for ([_]evt.Issue.Status{ .open, .closed }) |status| { 2704 const route = ui.RoutablePage.repoIssuesRoute(data.identity, status, data.tag, "") orelse return error.RouteTooLong; 2705 const link = try std.fmt.allocPrint(aa, "ai:{s}", .{try route.toUrl(session.page_arena)}); 2706 const label = try std.fmt.allocPrint(aa, "{s} ({d})", .{ @tagName(status), data.window(status).count }); 2707 try addTab(allocator, &box, &tab_ids, label, link); 2708 } 2709 2710 // tags tab, labeled with the active tag filter 2711 { 2712 const tags_route = ui.RoutablePage.repoIssuesTagsRoute(data.identity, data.tag) orelse return error.RouteTooLong; 2713 const tags_link = try std.fmt.allocPrint(aa, "ai:{s}", .{try tags_route.toUrl(session.page_arena)}); 2714 const label = if (data.tag.len == 0) "tags" else blk: { 2715 const decoded = std.Uri.percentDecodeInPlace(try aa.dupe(u8, data.tag)); 2716 break :blk try std.fmt.allocPrint(aa, "tags ({s})", .{decoded}); 2717 }; 2718 try addTab(allocator, &box, &tab_ids, label, tags_link); 2719 } 2720 2721 // new-issue tab; an edit or resolve url shows its tab in this place 2722 { 2723 const route = switch (data.view) { 2724 .edit => ui.RoutablePage.repoIssuesEditRoute(data.identity, data.selected_id) orelse return error.RouteTooLong, 2725 .new_comment => ui.RoutablePage.repoCommentNewRoute(data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong, 2726 .edit_comment => ui.RoutablePage.repoCommentEditRoute(data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong, 2727 .resolve => ui.RoutablePage.repoIssuesResolveRoute(data.identity, data.selected_id, data.theirs_picks) orelse return error.RouteTooLong, 2728 else => ui.RoutablePage.repoIssuesNewRoute(data.identity) orelse return error.RouteTooLong, 2729 }; 2730 const link = try std.fmt.allocPrint(aa, "ai:{s}", .{try route.toUrl(session.page_arena)}); 2731 const label: []const u8 = switch (data.view) { 2732 .edit => "edit", 2733 .new_comment => "reply", 2734 .edit_comment => "edit", 2735 .resolve => "resolve", 2736 else => "new", 2737 }; 2738 try addTab(allocator, &box, &tab_ids, label, link); 2739 } 2740 2741 // conflicts tab, labeled with the conflict count 2742 if (data.conflicts.count > 0) { 2743 const route = ui.RoutablePage.repoIssuesConflictsRoute(data.identity, "") orelse return error.RouteTooLong; 2744 const link = try std.fmt.allocPrint(aa, "ai:{s}", .{try route.toUrl(session.page_arena)}); 2745 const label = try std.fmt.allocPrint(aa, "conflicts ({d})", .{data.conflicts.count}); 2746 try addTab(allocator, &box, &tab_ids, label, link); 2747 } 2748 2749 var self = Header{ .box = box, .tab_ids = tab_ids }; 2750 // the tab matching the page's view is selected initially. 2751 self.getFocus().child_id = self.tab_ids.keys()[View.viewIndex(data.view)]; 2752 return self; 2753 } 2754 2755 fn addTab(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), tab_ids: *std.AutoArrayHashMapUnmanaged(usize, void), label: []const u8, link: []const u8) !void { 2756 var text_box = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 2757 errdefer text_box.deinit(allocator); 2758 text_box.getFocus().focusable = true; 2759 text_box.getFocus().kind = .{ .custom = link }; 2760 try tab_ids.put(allocator, text_box.getFocus().id, {}); 2761 try box.children.put(allocator, text_box.getFocus().id, .{ 2762 .widget = .{ .text_box = text_box }, 2763 .rect = null, 2764 .min_size = .{ .width = label.len + 2, .height = null }, 2765 }); 2766 } 2767 2768 pub fn deinit(self: *Header, allocator: std.mem.Allocator) void { 2769 self.box.deinit(allocator); 2770 self.tab_ids.deinit(allocator); 2771 } 2772 2773 pub fn build(self: *Header, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2774 self.clearGrid(); 2775 // only the selected tab shows its border 2776 for (self.box.children.keys(), self.box.children.values()) |id, *child| { 2777 switch (child.widget) { 2778 .text_box => |*tb| tb.options.border_style = if (self.getFocus().child_id == id) .single else .hidden, 2779 else => {}, 2780 } 2781 } 2782 try self.box.build(allocator, constraint, root_focus); 2783 } 2784 2785 pub fn input(self: *Header, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2786 _ = allocator; 2787 const current_tab = self.currentTabIndex() orelse return; 2788 if (inp.moveTab(key, current_tab, self.tab_ids.count())) |new_tab| { 2789 root_focus.setFocus(self.tab_ids.keys()[new_tab]); 2790 } 2791 } 2792 2793 pub fn clearGrid(self: *Header) void { 2794 self.box.clearGrid(); 2795 } 2796 2797 pub fn getGrid(self: Header) ?Grid { 2798 return self.box.getGrid(); 2799 } 2800 2801 pub fn getFocus(self: *Header) *Focus { 2802 return self.box.getFocus(); 2803 } 2804 2805 pub fn getSelectedIndex(self: Header) ?usize { 2806 return self.currentTabIndex(); 2807 } 2808 2809 fn currentTabIndex(self: Header) ?usize { 2810 const child_id = self.box.focus.child_id orelse return null; 2811 return self.tab_ids.getIndex(child_id); 2812 } 2813 }; 2814