╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Issues.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
854 } 855 856 fn initCommentForm(allocator: std.mem.Allocator, session: *ui.Session, action: []const u8, author: ui.Author, parent_route: ui.RoutablePage) !wgt.Box(ui.Widget) { 857 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 858 errdefer box.deinit(allocator); 859 box.getFocus().kind = .{ .custom = action }; 860 861 { 862 var parent = try ui.authorBox(allocator, session.page_arena, author); 863 errdefer parent.deinit(allocator); 864 parent.options.label = " replying to "; 865 parent.getFocus().kind = .{ .custom = try std.fmt.allocPrint(session.page_arena.allocator(), "a:{s}", .{try parent_route.toUrl(session.page_arena)}) }; 866 try box.children.put(allocator, parent.getFocus().id, .{ .widget = .{ .text_box = parent }, .rect = null, .min_size = null }); 867 } 868 869 { 870 var body = try wgt.TextInput(ui.Widget).init(allocator, .{ 871 .label = " comment ", 872 .name = "body", 873 .visible_width = null, 874 .rounded_corners = true, 875 .render_content = session.is_terminal, 876 .multiline = true, 877 .visible_height = 5, 878 }); 879 errdefer body.deinit(allocator); 880 body.getFocus().focusable = true; 881 try box.children.put(allocator, body.getFocus().id, .{ .widget = .{ .text_input = body }, .rect = null, .min_size = null }); 882 } 883 884 try addSubmitButtonLabeled(allocator, &box, "submit reply"); 885 box.getFocus().child_id = box.children.keys()[comment_author_field_index]; 886 return box; 887 } 888 889 // a form's submit button, then a spacer absorbing the leftover 890 // min-height the box hands its last child, so the button keeps its 891 // natural height 892 fn addSubmitButton(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget)) !void { 893 return addSubmitButtonLabeled(allocator, box, "submit"); 894 } 895 896 fn addSubmitButtonLabeled(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), label: []const u8) !void { 897 { 898 var submit = try ui.SubmitButton.initLabeled(allocator, label); 899 errdefer submit.deinit(allocator); 900 try box.children.put(allocator, submit.getFocus().id, .{ .widget = .{ .submit_button = submit }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 901 } 902 { 903 var spacer = try ui.Spacer.init(allocator); 904 errdefer spacer.deinit(allocator); 905 try box.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 906 } 907 } 908 909 // the conflict resolve form. the "use this" links are navigations that 910 // flip the url's theirs: pick, reloading the prefills; one submit 911 // settles every conflict. the form: subtree makes the web overlay POST 912 // to the resolve route. 913 fn initResolveForm(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self) !wgt.Box(ui.Widget) { 914 const aa = session.page_arena.allocator(); 915 // init only picks the resolve view once it has read the conflict 916 const conflict = if (data.conflict) |*c| c else unreachable; 917 918 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 919 errdefer box.deinit(allocator); 920 box.getFocus().kind = .{ .custom = if (data.identity.len == 0) 921 try std.fmt.allocPrint(aa, "form:/issue:{s}/resolve", .{data.selected_id}) 922 else 923 try std.fmt.allocPrint(aa, "form:/repo/{s}/issue:{s}/resolve", .{ data.identity, data.selected_id }) }; 924 925 if (conflict.title) |*fc| { 926 try addLabel(allocator, &box, "title conflict:"); 927 try addFieldConflict(allocator, &box, session, data, "title", fc); 928 } 929 if (conflict.tags) |*fc| { 930 try addLabel(allocator, &box, "tags conflict:"); 931 try addFieldConflict(allocator, &box, session, data, "tags", fc); 932 } 933 934 if (conflict.description) |*desc| { 935 try addLabel(allocator, &box, "description conflict:"); 936 var hunk_index: usize = 0; 937 for (desc.chunks, 0..) |*chunk, chunk_index| { 938 switch (chunk.*) { 939 .same => |text| { 940 var tb = try wgt.TextBox(ui.Widget).init(allocator, text, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .word }); 941 errdefer tb.deinit(allocator); 942 tb.getFocus().focusable = true; 943 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 944 }, 945 // an auto-resolved chunk: only one side changed it, so its 946 // version stands, shown distinctly with its author 947 .auto => |auto| { 948 const author = if (auto.theirs) desc.theirs_author else desc.ours_author; 949 const verb: []const u8 = if (auto.text == null) "removed" else "edited"; 950 const label = switch (author) { 951 .user_name, .email => |name| try std.fmt.allocPrint(aa, " {s} by {s} ", .{ verb, name }), 952 .unknown => try std.fmt.allocPrint(aa, " {s} by {s} ", .{ verb, if (auto.theirs) @as([]const u8, "them") else "us" }), 953 }; 954 var tb = try wgt.TextBox(ui.Widget).init(allocator, auto.text orelse "(removed)", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .word, .label = label }); 955 errdefer tb.deinit(allocator); 956 tb.getFocus().focusable = true; 957 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 958 }, 959 .conflict => |hunk| { 960 // a blank row on each side sets the group apart from 961 // the flowing shared and auto-resolved chunks 962 if (chunk_index > 0) try addGap(allocator, &box); 963 const name = try std.fmt.allocPrint(aa, "d{d}", .{hunk_index}); 964 hunk_index += 1; 965 const picked_theirs = data.theirsPicked(name); 966 try addVersionRow(allocator, &box, try sideLabel(aa, desc.ours_author, true), hunk.ours orelse "", try useThisLink(session, data, name, false)); 967 try addVersionRow(allocator, &box, try sideLabel(aa, desc.theirs_author, false), hunk.theirs orelse "", try useThisLink(session, data, name, true)); 968 var resolution_input = try wgt.TextInput(ui.Widget).init(allocator, .{ .label = " resolution ", .name = name, .visible_width = null, .rounded_corners = true, .render_content = session.is_terminal, .multiline = true }); 969 errdefer resolution_input.deinit(allocator); 970 resolution_input.getFocus().focusable = true; 971 try resolution_input.setContent(allocator, (if (picked_theirs) hunk.theirs else hunk.ours) orelse ""); 972 try box.children.put(allocator, resolution_input.getFocus().id, .{ .widget = .{ .text_input = resolution_input }, .rect = null, .min_size = null }); 973 if (chunk_index + 1 < desc.chunks.len) try addGap(allocator, &box); 974 }, 975 } 976 } 977 } 978 979 try addSubmitButton(allocator, &box); 980 981 box.getFocus().child_id = box.children.keys()[0]; 982 return box; 983 } 984 985 // one conflicted scalar field: both sides' version rows, then the 986 // resolution input prefilled from the picked side. 987 fn addFieldConflict(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), session: *ui.Session, data: *const Self, comptime name: []const u8, fc: *const FieldConflict) !void { 988 const aa = session.page_arena.allocator(); 989 try addVersionRow(allocator, box, try sideLabel(aa, fc.ours.author, true), fc.ours.text, try useThisLink(session, data, name, false)); 990 try addVersionRow(allocator, box, try sideLabel(aa, fc.theirs.author, false), fc.theirs.text, try useThisLink(session, data, name, true)); 991 992 var resolution_input = try wgt.TextInput(ui.Widget).init(allocator, .{ .label = " resolution ", .name = name, .visible_width = null, .rounded_corners = true, .render_content = session.is_terminal }); 993 errdefer resolution_input.deinit(allocator); 994 resolution_input.getFocus().focusable = true; 995 try resolution_input.setContent(allocator, if (data.theirsPicked(name)) fc.theirs.text else fc.ours.text); 996 try box.children.put(allocator, resolution_input.getFocus().id, .{ .widget = .{ .text_input = resolution_input }, .rect = null, .min_size = null }); 997 } 998 999 // a blank row setting a conflict group apart from its neighbors 1000 fn addGap(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget)) !void { 1001 var text = try wgt.Text(ui.Widget).init(allocator, " "); 1002 errdefer text.deinit(allocator); 1003 try box.children.put(allocator, text.getFocus().id, .{ .widget = .{ .text = text }, .rect = null, .min_size = null }); 1004 } 1005 1006 fn addLabel(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), content: []const u8) !void { 1007 var label = try ui.SectionLabel.init(allocator, content); 1008 errdefer label.deinit(allocator); 1009 try box.children.put(allocator, label.getFocus().id, .{ .widget = .{ .section_label = label }, .rect = null, .min_size = null }); 1010 } 1011 1012 // one side of a conflict: the "use this" link on the left, the version 1013 // itself as a labeled read-only box. 1014 fn addVersionRow(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), label: []const u8, text: []const u8, link: []const u8) !void { 1015 var row = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 1016 errdefer row.deinit(allocator); 1017 1018 { 1019 const use_label = "use this"; 1020 var use = try wgt.TextBox(ui.Widget).init(allocator, use_label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1021 errdefer use.deinit(allocator); 1022 use.getFocus().focusable = true; 1023 use.getFocus().kind = .{ .custom = link }; 1024 try row.children.put(allocator, use.getFocus().id, .{ 1025 .widget = .{ .text_box = use }, 1026 .rect = null, 1027 .min_size = .{ .width = use_label.len + 2, .height = null }, 1028 .max_size = .{ .width = use_label.len + 2, .height = null }, 1029 }); 1030 } 1031 1032 { 1033 var tb = try wgt.TextBox(ui.Widget).init(allocator, if (text.len > 0) text else "(removed)", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .word, .label = label }); 1034 errdefer tb.deinit(allocator); 1035 tb.getFocus().focusable = true; 1036 try row.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1037 } 1038 1039 row.getFocus().child_id = row.children.keys()[0]; 1040 try box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .box = row }, .rect = null, .min_size = null }); 1041 } 1042 1043 // the label of one side's version box, naming its author 1044 fn sideLabel(aa: std.mem.Allocator, author: ui.Author, ours: bool) ![]const u8 { 1045 return switch (author) { 1046 .user_name, .email => |name| try std.fmt.allocPrint(aa, " edited by {s} ", .{name}), 1047 .unknown => if (ours) " current version " else " their version ", 1048 }; 1049 } 1050 1051 // the a: link that reloads the resolve page with `name` picked from ours 1052 // or theirs, reprefilling its resolution input 1053 fn useThisLink(session: *ui.Session, data: *const Self, name: []const u8, theirs: bool) ![]const u8 { 1054 const aa = session.page_arena.allocator(); 1055 var picks: std.ArrayList(u8) = .empty; 1056 var it = std.mem.splitScalar(u8, data.theirs_picks, ','); 1057 while (it.next()) |pick| { 1058 if (pick.len == 0 or std.mem.eql(u8, pick, name)) continue; 1059 if (picks.items.len > 0) try picks.append(aa, ','); 1060 try picks.appendSlice(aa, pick); 1061 } 1062 if (theirs) { 1063 if (picks.items.len > 0) try picks.append(aa, ','); 1064 try picks.appendSlice(aa, name); 1065 } 1066 const route = ui.RoutablePage.repoIssuesResolveRoute(data.identity, data.selected_id, picks.items) orelse return error.RouteTooLong; 1067 return std.fmt.allocPrint(aa, "a:{s}", .{try route.toUrl(session.page_arena)}); 1068 } 1069 1070 // the resolve form's page-constant initial value for the input `name`: 1071 // the picked side of its conflicted field or hunk 1072 fn resolvePrefill(self: *View, name: []const u8) ?[]const u8 { 1073 const conflict = if (self.data.conflict) |*c| c else return null; 1074 const theirs = self.data.theirsPicked(name); 1075 if (std.mem.eql(u8, name, "title")) { 1076 const fc = if (conflict.title) |*f| f else return null; 1077 return if (theirs) fc.theirs.text else fc.ours.text; 1078 } 1079 if (std.mem.eql(u8, name, "tags")) { 1080 const fc = if (conflict.tags) |*f| f else return null; 1081 return if (theirs) fc.theirs.text else fc.ours.text; 1082 } 1083 if (std.mem.startsWith(u8, name, "d")) { 1084 const desc = if (conflict.description) |*d| d else return null; 1085 const hunk_index = std.fmt.parseInt(usize, name[1..], 10) catch return null; 1086 var seen: usize = 0; 1087 for (desc.chunks) |chunk| switch (chunk) { 1088 .conflict => |hunk| { 1089 if (seen == hunk_index) return (if (theirs) hunk.theirs else hunk.ours) orelse ""; 1090 seen += 1; 1091 }, 1092 else => {}, 1093 }; 1094 } 1095 return null; 1096 } 1097 1098 fn addRow(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), text: []const u8, bottom_label: []const u8, link: []const u8) !void { 1099 var row = try wgt.TextBox(ui.Widget).init(allocator, text, .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .word, .bottom_label = bottom_label }); 1100 errdefer row.deinit(allocator); 1101 row.getFocus().focusable = true; 1102 if (link.len != 0) row.getFocus().kind = .{ .custom = link }; 1103 try box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .text_box = row }, .rect = null, .min_size = null, .max_size = .{ .width = null, .height = 5 } }); 1104 } 1105 1106 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 1107 self.box.deinit(allocator); 1108 } 1109 1110 fn header(self: *View) *Header { 1111 return &self.box.children.values()[header_index].widget.repo_issues_header; 1112 } 1113 1114 fn viewStack(self: *View) *wgt.Stack(ui.Widget) { 1115 return &self.box.children.values()[stack_index].widget.stack; 1116 } 1117 1118 // `index`'s master-detail split inside the stack. 1119 fn resultsBox(self: *View, index: usize) *wgt.Box(ui.Widget) { 1120 return &self.viewStack().children.values()[index].box; 1121 } 1122 1123 // the tags view's flow inside the stack. 1124 fn tagsView(self: *View) *ui.TagFlow { 1125 return &self.viewStack().children.values()[tags_view_index].tag_flow; 1126 } 1127 1128 // the new-issue, edit, or resolve form inside the stack, or null when the 1129 // unauthorized view stands in for it. 1130 fn issueForm(self: *View) ?*wgt.Box(ui.Widget) { 1131 return switch (self.viewStack().children.values()[form_view_index]) { 1132 .box => |*box| box, 1133 // the resolve form sits inside a scroll on the terminal 1134 .scroll => |*scroll| &scroll.child.box, 1135 else => null, 1136 }; 1137 } 1138 1139 // the resolve form's scroll on the terminal (the web page scrolls itself) 1140 fn resolveScroll(self: *View) ?*wgt.Scroll(ui.Widget) { 1141 return switch (self.viewStack().children.values()[form_view_index]) { 1142 .scroll => |*scroll| scroll, 1143 else => null, 1144 }; 1145 } 1146 1147 fn listScroll(self: *View, index: usize) *wgt.Scroll(ui.Widget) { 1148 return &self.resultsBox(index).children.values()[list_index].widget.scroll; 1149 } 1150 1151 fn listBox(self: *View, index: usize) *wgt.Box(ui.Widget) { 1152 return &self.listScroll(index).child.box; 1153 } 1154 1155 // the detail frame's children: the tool row above the scroll pane. 1156 const tool_row_index: usize = 0; 1157 const detail_scroll_index: usize = 1; 1158 1159 fn detailOuter(self: *View, index: usize) *wgt.Box(ui.Widget) { 1160 return &self.resultsBox(index).children.values()[detail_index].widget.box; 1161 } 1162 1163 fn toolRow(self: *View, index: usize) *wgt.Box(ui.Widget) { 1164 return &self.detailOuter(index).children.values()[tool_row_index].widget.box; 1165 } 1166 1167 fn detailScroll(self: *View, index: usize) *wgt.Scroll(ui.Widget) { 1168 return &self.detailOuter(index).children.values()[detail_scroll_index].widget.scroll; 1169 } 1170 1171 fn detailInner(self: *View, index: usize) *wgt.Box(ui.Widget) { 1172 return &self.detailScroll(index).child.box; 1173 } 1174 1175 fn window(self: *View, index: usize) *const Window { 1176 if (index == conflicts_view_index) return &self.data.conflicts; 1177 return self.data.window(splitStatus(index)); 1178 } 1179 1180 fn stackSelectedIndex(self: *View) ?usize { 1181 const stack = self.viewStack(); 1182 const cid = stack.getFocus().child_id orelse return null; 1183 return stack.children.getIndex(cid); 1184 } 1185 1186 // the stack's selected master-detail split (null when the tags view or the 1187 // new-issue form shows). 1188 fn selectedSplitIndex(self: *View) ?usize { 1189 const idx = self.stackSelectedIndex() orelse return null; 1190 return if (idx < split_count or idx == conflicts_view_index) idx else null; 1191 } 1192 1193 fn detailActive(self: *View, index: usize) bool { 1194 const rb = self.resultsBox(index); 1195 const cid = rb.getFocus().child_id orelse return false; 1196 return rb.children.getIndex(cid) == detail_index; 1197 } 1198 1199 fn headerActive(self: *View) bool { 1200 const cid = self.box.getFocus().child_id orelse return false; 1201 return self.box.children.getIndex(cid) == header_index; 1202 } 1203 1204 fn tagsViewActive(self: *View) bool { 1205 if (self.headerActive()) return false; 1206 return self.stackSelectedIndex() == tags_view_index; 1207 } 1208 1209 fn formViewActive(self: *View) bool { 1210 if (self.headerActive()) return false; 1211 return self.stackSelectedIndex() == form_view_index; 1212 } 1213 1214 // the selected issue's index, or null when a window-navigation row is 1215 // selected (a leading "previous" row shifts the issue rows down by one). 1216 fn selectedIssueIndex(self: *View, index: usize) ?usize { 1217 const lb = self.listBox(index); 1218 const cid = lb.getFocus().child_id orelse return null; 1219 const idx = lb.children.getIndex(cid) orelse return null; 1220 const win = self.window(index); 1221 const lead: usize = if (win.prev_id != null) 1 else 0; 1222 if (idx < lead or idx - lead >= win.issues.len) return null; 1223 return idx - lead; 1224 } 1225 1226 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1227 self.clearGrid(); 1228 1229 // each header tab maps 1:1 to a stack child by position. mirror the 1230 // selection into the url: the view the page's window is rooted at 1231 // keeps its rooted url, the others get their list route (keeping the 1232 // tag filter). 1233 if (self.header().getSelectedIndex()) |index| { 1234 const stack = self.viewStack(); 1235 stack.getFocus().child_id = stack.children.keys()[index]; 1236 self.session.data.current_page = (if (index == form_view_index) 1237 (switch (self.data.view) { 1238 .edit => ui.RoutablePage.repoIssuesEditRoute(self.data.identity, self.data.selected_id), 1239 .new_comment => ui.RoutablePage.repoCommentNewRoute(self.data.identity, self.data.selected_id, self.data.comment_id), 1240 .resolve => ui.RoutablePage.repoIssuesResolveRoute(self.data.identity, self.data.selected_id, self.data.theirs_picks), 1241 else => ui.RoutablePage.repoIssuesNewRoute(self.data.identity), 1242 }) 1243 else if (index == conflicts_view_index) 1244 ui.RoutablePage.repoIssuesConflictsRoute(self.data.identity, if (self.data.view == .conflicts) self.data.selected_id else "") 1245 else if (index == viewIndex(self.data.view) and self.data.selected_id.len != 0) 1246 (if (self.data.description_page) 1247 ui.RoutablePage.repoIssuesDescriptionRoute(self.data.identity, self.data.selected_id) 1248 else if (self.data.comment_page) |page| 1249 ui.RoutablePage.repoCommentsRoute(self.data.identity, self.data.selected_id, &page.selected.id, page.replies.start) 1250 else 1251 ui.RoutablePage.repoIssueCommentsRoute(self.data.identity, self.data.selected_id, self.data.comments_start)) 1252 else if (index == tags_view_index) 1253 ui.RoutablePage.repoIssuesTagsRoute(self.data.identity, self.data.tag) 1254 else 1255 ui.RoutablePage.repoIssuesRoute(self.data.identity, splitStatus(index), self.data.tag, "")) orelse self.session.data.current_page; 1256 } 1257 1258 if (self.selectedSplitIndex()) |i| { 1259 // swap the detail pane to the selected issue when it changes. 1260 try self.refreshDetail(allocator, i); 1261 1262 // mirror the focused issue into the url, but only while focus is 1263 // inside the split. an issue's url is the same whether or not the 1264 // list is filtered, so the mirror drops the tag. 1265 if (root_focus.grandchild_id) |g| { 1266 if (self.resultsBox(i).getFocus().children.contains(g)) { 1267 if (self.selectedIssueIndex(i)) |sel| { 1268 // selecting another issue leaves the description page 1269 // behind, like it leaves the tag filter behind. 1270 const entry = &self.window(i).issues[sel]; 1271 const mirrored = if (self.data.description_page and std.mem.eql(u8, entry.id, self.data.selected_id)) 1272 ui.RoutablePage.repoIssuesDescriptionRoute(self.data.identity, entry.id) 1273 else if (self.data.comment_page) |page| 1274 if (std.mem.eql(u8, entry.id, self.data.selected_id)) 1275 ui.RoutablePage.repoCommentsRoute(self.data.identity, entry.id, &page.selected.id, page.replies.start) 1276 else 1277 ui.RoutablePage.repoIssueCommentsRoute(self.data.identity, entry.id, 0) 1278 else if (std.mem.eql(u8, entry.id, self.data.selected_id)) 1279 ui.RoutablePage.repoIssueCommentsRoute(self.data.identity, entry.id, entry.comments.start) 1280 else 1281 ui.RoutablePage.repoIssueCommentsRoute(self.data.identity, entry.id, 0); 1282 if (mirrored) |route| self.session.data.current_page = route; 1283 } 1284 } 1285 } 1286 1287 // the selected list row shows a border (the focused TextBox 1288 // upgrades it to a double border itself); the rest stay borderless. 1289 const lb = self.listBox(i); 1290 for (lb.children.keys(), lb.children.values()) |id, *child| { 1291 switch (child.widget) { 1292 .text_box => |*tb| tb.options.border_style = if (lb.getFocus().child_id == id) .single else .hidden, 1293 else => {}, 1294 } 1295 } 1296 1297 // the pane's selected child shows the selection border: the 1298 // description directly, the tags via the flow's selected item. 1299 // cap the list at list_max_width only while the detail pane fits 1300 // beside it. the box drops the detail when the width can't hold 1301 // both minimums, so when it's that narrow we lift the cap and let 1302 // the list fill the whole width. 1303 const both_panes_fit = if (constraint.max_size.width) |w| w >= list_max_width + detail_min_width else true; 1304 self.resultsBox(i).children.values()[list_index].max_size = if (both_panes_fit) .{ .width = list_max_width, .height = null } else null; 1305 1306 // stretch the detail pane across the rest of the width so it fills 1307 // the area rather than shrinking to its content; its scroll fills 1308 // the pane. 1309 if (constraint.max_size.width) |w| { 1310 self.resultsBox(i).children.values()[detail_index].min_size = .{ .width = if (both_panes_fit) w - list_max_width else w, .height = null }; 1311 } else { 1312 self.resultsBox(i).children.values()[detail_index].min_size = .{ .width = detail_min_width, .height = null }; 1313 } 1314 } 1315 1316 // refresh the form inputs' entries in the session's focus-id -> input 1317 // map with this frame's addresses, so the web/wasm form handling can 1318 // find them by focus id 1319 if (self.issueForm()) |form| { 1320 const inputs_arena = self.session.arena.allocator(); 1321 for (form.children.values()) |*child| switch (child.widget) { 1322 .text_input => |*ti| try self.session.text_inputs.put(inputs_arena, ti.getFocus().id, ti), 1323 else => {}, 1324 }; 1325 1326 // on an edit page, register the issue's content as the inputs' 1327 // page-constant initial values, which the web overlay renders 1328 // into them. 1329 if (self.data.view == .edit) { 1330 if (self.data.selectedIssue()) |entry| { 1331 const fields = form.children.values(); 1332 try self.session.input_values.put(inputs_arena, fields[title_field_index].widget.text_input.getFocus().id, entry.issue.event.title); 1333 try self.session.input_values.put(inputs_arena, fields[tags_field_index].widget.text_input.getFocus().id, entry.issue.event.tags); 1334 try self.session.input_values.put(inputs_arena, fields[description_field_index].widget.text_input.getFocus().id, entry.issue.event.description); 1335 } 1336 } 1337 1338 // the resolve form's inputs prefill from the picked side, which 1339 // is url state and so page-constant like the edit values above 1340 if (self.data.view == .resolve) { 1341 for (form.children.values()) |*child| switch (child.widget) { 1342 .text_input => |*ti| if (self.resolvePrefill(ti.options.name)) |value| { 1343 try self.session.input_values.put(inputs_arena, ti.getFocus().id, value); 1344 }, 1345 else => {}, 1346 }; 1347 } 1348 } 1349 1350 try self.box.build(allocator, constraint, root_focus); 1351 } 1352 1353 fn refreshDetail(self: *View, allocator: std.mem.Allocator, index: usize) !void { 1354 const sel = self.selectedIssueIndex(index) orelse return; 1355 if (self.detailed_index[index]) |d| if (d == sel) return; 1356 try self.populateDetail(allocator, index, sel); 1357 self.detailed_index[index] = sel; 1358 } 1359 1360 fn populateDetail(self: *View, allocator: std.mem.Allocator, index: usize, sel: usize) !void { 1361 const entry = self.window(index).issues[sel]; 1362 const inner = self.detailInner(index); 1363 // the /description page replaces its issue's detail with a back link 1364 // and the whole description; other issues keep their normal detail. 1365 const description_page = self.data.description_page and std.mem.eql(u8, entry.id, self.data.selected_id); 1366 const comment_page = if (std.mem.eql(u8, entry.id, self.data.selected_id)) self.data.comment_page else null; 1367 1368 for (inner.children.values()) |*child| child.widget.deinit(allocator); 1369 inner.children.clearAndFree(allocator); 1370 inner.getFocus().child_id = null; 1371 self.author_id[index] = null; 1372 1373 // the tool row: the open/close and edit buttons; the description page 1374 // shows none. 1375 { 1376 const row = self.toolRow(index); 1377 for (row.children.values()) |*child| child.widget.deinit(allocator); 1378 row.children.clearAndFree(allocator); 1379 row.getFocus().child_id = null; 1380 row.getFocus().kind = .container; 1381 } 1382 if (!description_page and comment_page == null) { 1383 const row = self.toolRow(index); 1384 const pa = self.session.page_arena.allocator(); 1385 1386 { 1387 var spacer = try ui.Spacer.init(allocator); 1388 errdefer spacer.deinit(allocator); 1389 try row.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 1390 } 1391 1392 // a conflicted issue's only action is resolving it. the button 1393 // stays visible logged out; the resolve page shows the 1394 // unauthorized view then. 1395 if (entry.conflicted) { 1396 const label = "resolve conflict"; 1397 var button = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1398 errdefer button.deinit(allocator); 1399 button.getFocus().focusable = true; 1400 const route = ui.RoutablePage.repoIssuesResolveRoute(self.data.identity, entry.id, "") orelse return error.RouteTooLong; 1401 button.getFocus().kind = .{ .custom = try std.fmt.allocPrint(pa, "a:{s}", .{try route.toUrl(self.session.page_arena)}) }; 1402 try row.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = .{ .width = label.len + 2, .height = null } }); 1403 } else { 1404 // a logged-out session can't flip the status, so it gets no 1405 // open/close button (and no form action on the row) 1406 if (self.session.data.is_local or self.session.data.user_id != null) { 1407 const action: []const u8 = switch (entry.issue.event.status) { 1408 .open => "close", 1409 .closed => "open", 1410 }; 1411 row.getFocus().kind = .{ .custom = if (self.data.identity.len == 0) 1412 try std.fmt.allocPrint(pa, "form:/issue:{s}/{s}", .{ entry.id, action }) 1413 else 1414 try std.fmt.allocPrint(pa, "form:/repo/{s}/issue:{s}/{s}", .{ self.data.identity, entry.id, action }) }; 1415 1416 var button = try wgt.TextBox(ui.Widget).init(allocator, action, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1417 errdefer button.deinit(allocator); 1418 button.getFocus().focusable = true; 1419 // the renderer distinguishes plain clickables from buttons that 1420 // should POST to a server route by this kind. 1421 button.getFocus().kind = .{ .custom = "submit" }; 1422 try row.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = .{ .width = action.len + 2, .height = null } }); 1423 } 1424 1425 // the edit button links to the issue's edit page. 1426 { 1427 var button = try wgt.TextBox(ui.Widget).init(allocator, "edit", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1428 errdefer button.deinit(allocator); 1429 button.getFocus().focusable = true; 1430 const route = ui.RoutablePage.repoIssuesEditRoute(self.data.identity, entry.id) orelse return error.RouteTooLong; 1431 button.getFocus().kind = .{ .custom = try std.fmt.allocPrint(pa, "a:{s}", .{try route.toUrl(self.session.page_arena)}) }; 1432 try row.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = .{ .width = "edit".len + 2, .height = null } }); 1433 } 1434 } 1435 1436 // the resolve button on a conflicted issue, else the open/close 1437 // button when present, the edit button otherwise 1438 row.getFocus().child_id = row.children.keys()[button_in_row_index]; 1439 } 1440 1441 if (comment_page) |page| { 1442 var back = try Comment.linkBox(allocator, self.session, "← back to issue", ui.RoutablePage.repoIssueCommentsRoute(self.data.identity, entry.id, 0) orelse return error.RouteTooLong); 1443 errdefer back.deinit(allocator); 1444 try inner.children.put(allocator, back.getFocus().id, .{ .widget = .{ .text_box = back }, .rect = null, .min_size = null }); 1445 1446 try Comment.appendComment(allocator, inner, self.session, self.data.identity, page.selected); 1447 try Comment.appendCount(allocator, inner, page.replies.count, "reply", "replies", self.session.page_arena.allocator()); 1448 for (page.replies.comments) |comment| try Comment.appendComment(allocator, inner, self.session, self.data.identity, comment); 1449 try Comment.appendWindowNav(allocator, inner, self.session, self.data.identity, &page.selected.comment.event.thread_id, &page.selected.id, page.replies); 1450 1451 var spacer = try ui.Spacer.init(allocator); 1452 errdefer spacer.deinit(allocator); 1453 try inner.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 1454 1455 inner.getFocus().child_id = inner.children.keys()[0]; 1456 const sc = self.detailScroll(index); 1457 sc.x = 0; 1458 sc.y = 0; 1459 sc.getFocus().version +%= 1; 1460 return; 1461 } 1462 1463 if (description_page) { 1464 // the back link, in the title slot so the pane's input handling 1465 // applies to it unchanged. 1466 var tb = try wgt.TextBox(ui.Widget).init(allocator, "← back to issue", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1467 errdefer tb.deinit(allocator); 1468 tb.getFocus().focusable = true; 1469 tb.getFocus().kind = .{ .custom = try issuesLink(self.session.page_arena, self.data.identity, entry.issue.event.status, "", entry.id) }; 1470 try inner.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1471 } else { 1472 // the issue's title as a focusable word-wrapped text box. 1473 { 1474 var tb = try wgt.TextBox(ui.Widget).init(allocator, entry.issue.event.title, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .word, .label = " title " }); 1475 errdefer tb.deinit(allocator); 1476 tb.getFocus().focusable = true; 1477 try inner.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1478 } 1479 1480 self.author_id[index] = blk: { 1481 var tb = try ui.authorBox(allocator, self.session.page_arena, entry.author); 1482 errdefer tb.deinit(allocator); 1483 try inner.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1484 break :blk tb.getFocus().id; 1485 }; 1486 1487 // the issue's tags, each linking to this status's list filtered to 1488 // that tag. 1489 { 1490 var items: std.ArrayList(ui.TagFlow.Item) = .empty; 1491 defer items.deinit(allocator); 1492 var tag_iter = evt.Issue.tagIterator(entry.issue.event.tags); 1493 while (tag_iter.next()) |tag| { 1494 if (tag.len == 0) continue; 1495 try items.append(allocator, .{ .text = tag, .link = try tagLink(self.session.page_arena, self.data.identity, entry.issue.event.status, tag) }); 1496 } 1497 if (items.items.len > 0) { 1498 var tf = try ui.TagFlow.init(allocator); 1499 errdefer tf.deinit(allocator); 1500 try tf.setItems(allocator, items.items); 1501 try inner.children.put(allocator, tf.getFocus().id, .{ .widget = .{ .tag_flow = tf }, .rect = null, .min_size = null }); 1502 } 1503 } 1504 } 1505 1506 // the description as a focusable word-wrapped text box. past the limit 1507 // it's cut at the last whole line and links to the /description page, 1508 // which shows the whole thing. 1509 self.description_id[index] = blk: { 1510 const whole = entry.issue.event.description; 1511 const cut_short = !description_page and whole.len > max_description_size; 1512 const shown = if (cut_short) 1513 std.mem.trimEnd(u8, whole[0 .. std.mem.lastIndexOfScalar(u8, whole[0..max_description_size], '\n') orelse 0], " \t\r\n") 1514 else if (whole.len == 0) 1515 "(no description)" 1516 else 1517 whole; 1518 var tb = try wgt.TextBox(ui.Widget).init(allocator, shown, .{ 1519 .border_style = .single, 1520 .rounded_corners = true, 1521 .wrap_kind = .word, 1522 .label = " description ", 1523 .bottom_label = if (cut_short) " click or press enter to see more " else "", 1524 }); 1525 errdefer tb.deinit(allocator); 1526 tb.getFocus().focusable = true; 1527 if (cut_short) tb.getFocus().kind = .{ .custom = try descriptionLink(self.session.page_arena, self.data.identity, entry.id) }; 1528 try inner.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1529 break :blk tb.getFocus().id; 1530 }; 1531 1532 if (!description_page) { 1533 var reply = try Comment.linkBox(allocator, self.session, "new comment", ui.RoutablePage.repoCommentNewRoute(self.data.identity, entry.id, "") orelse return error.RouteTooLong); 1534 errdefer reply.deinit(allocator); 1535 try inner.children.put(allocator, reply.getFocus().id, .{ .widget = .{ .text_box = reply }, .rect = null, .min_size = null }); 1536 1537 try Comment.appendCount(allocator, inner, entry.comments.count, "comment", "comments", self.session.page_arena.allocator()); 1538 for (entry.comments.comments) |comment| try Comment.appendComment(allocator, inner, self.session, self.data.identity, comment); 1539 try Comment.appendWindowNav(allocator, inner, self.session, self.data.identity, entry.id, null, entry.comments); 1540 1541 var spacer = try ui.Spacer.init(allocator); 1542 errdefer spacer.deinit(allocator); 1543 try inner.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 1544 } 1545 1546 // select the title by default 1547 inner.getFocus().child_id = inner.children.keys()[title_child_index]; 1548 1549 // reset the scroll to the top for the newly-shown issue: directly on the 1550 // terminal (the wasm offset), and via a version bump on the web (so the 1551 // renderer's scroll id changes and JS drops the preserved position). 1552 const sc = self.detailScroll(index); 1553 sc.x = 0; 1554 sc.y = 0; 1555 sc.getFocus().version +%= 1; 1556 } 1557 1558 pub fn input(self: *View, allocator: std.mem.Allocator, raw_key: Key, root_focus: *Focus) !void { 1559 // wheel input moves focus like the arrows 1560 const key: Key = if (raw_key == .mouse and raw_key.mouse.action == .scroll) 1561 (if (raw_key.mouse.action.scroll == .up) .arrow_up else .arrow_down) 1562 else 1563 raw_key; 1564 1565 if (self.headerActive()) { 1566 // down from the tabs re-enters the stack if the selected view has 1567 // something to focus; other keys move the tabs. 1568 if (inp.vertDirection(key) == .down) { 1569 const enterable = if (self.viewStack().getSelected()) |selected| switch (selected.*) { 1570 .tag_flow => |*tf| tf.text_boxes.items.len > 0, 1571 // nothing focusable in the unauthorized view 1572 .unauthorized => false, 1573 else => true, 1574 } else false; 1575 if (enterable) root_focus.setFocus(self.box.children.keys()[stack_index]); 1576 } else { 1577 try self.header().input(allocator, key, root_focus); 1578 } 1579 return; 1580 } 1581 if (self.tagsViewActive()) { 1582 self.tagsViewInput(key, root_focus); 1583 return; 1584 } 1585 if (self.formViewActive()) { 1586 if (self.data.view == .resolve) { 1587 try self.resolveInput(allocator, key, root_focus); 1588 } else if (self.data.view == .new_comment) { 1589 try self.commentFormInput(allocator, key, root_focus); 1590 } else { 1591 try self.formInput(allocator, key, root_focus); 1592 } 1593 return; 1594 } 1595 const i = self.selectedSplitIndex() orelse return; 1596 if (self.detailActive(i)) { 1597 try self.detailInput(allocator, i, key, root_focus); 1598 } else { 1599 try self.listInput(i, key, root_focus); 1600 } 1601 } 1602 1603 // arrow keys move the tag selection; up from the top row crosses to the 1604 // header tabs. 1605 fn tagsViewInput(self: *View, key: Key, root_focus: *Focus) void { 1606 const tf = self.tagsView(); 1607 const cid = tf.focus.child_id orelse return; 1608 const cur = tf.indexOfFocusId(cid) orelse return; 1609 const count = tf.text_boxes.items.len; 1610 switch (key) { 1611 .arrow_left => if (cur > 0) root_focus.setFocus(tf.text_boxes.items[cur - 1].getFocus().id), 1612 .arrow_right => if (cur + 1 < count) root_focus.setFocus(tf.text_boxes.items[cur + 1].getFocus().id), 1613 .arrow_up => if (tf.rowStep(cur, false)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id) else self.focusHeader(root_focus), 1614 .arrow_down => if (tf.rowStep(cur, true)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id), 1615 .home => root_focus.setFocus(tf.text_boxes.items[0].getFocus().id), 1616 .end => root_focus.setFocus(tf.text_boxes.items[count - 1].getFocus().id), 1617 else => {}, 1618 } 1619 } 1620 1621 fn listInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 1622 // up/down (and the scroll wheel) move the selection a row; page up/down 1623 // jump a fixed amount. right/Enter cross into the detail pane. up from 1624 // the top row crosses into the header tabs. 1625 if (inp.rowDelta(key, @intCast(self.listBox(index).children.count()))) |delta| { 1626 const lb = self.listBox(index); 1627 const at_top = if (lb.getFocus().child_id) |cid| lb.children.getIndex(cid) == 0 else true; 1628 if (delta < 0 and at_top) return self.focusHeader(root_focus); 1629 ui.moveRowFocus(lb, self.listScroll(index), root_focus, delta); 1630 return; 1631 } 1632 switch (key) { 1633 .enter, .arrow_right => try self.focusDetail(index, root_focus), 1634 else => {}, 1635 } 1636 } 1637 1638 fn detailInput(self: *View, allocator: std.mem.Allocator, index: usize, key: Key, root_focus: *Focus) !void { 1639 if (self.data.comment_page != null and std.mem.eql(u8, self.window(index).issues[self.detailed_index[index] orelse return].id, self.data.selected_id)) { 1640 try self.commentInput(index, key, root_focus); 1641 } else if (self.toolRowFocused(index)) { 1642 try self.toolRowInput(allocator, index, key, root_focus); 1643 } else if (self.titleFocused(index)) { 1644 try self.titleInput(index, key, root_focus); 1645 } else if (self.authorFocused(index)) { 1646 try self.authorInput(index, key, root_focus); 1647 } else if (self.tagsFocused(index)) { 1648 try self.tagsInput(index, key, root_focus); 1649 } else if (self.descriptionFocused(index)) { 1650 try self.descriptionInput(index, key, root_focus); 1651 } else { 1652 try self.commentInput(index, key, root_focus); 1653 } 1654 } 1655 1656 // the tool row: enter or a click on the open/close button flips the 1657 // issue's status; the edit and resolve buttons are a: links the host 1658 // follows. arrows cross between the buttons and to the neighboring 1659 // widgets. 1660 fn toolRowInput(self: *View, allocator: std.mem.Allocator, index: usize, key: Key, root_focus: *Focus) !void { 1661 const row = self.toolRow(index); 1662 const cur = if (row.getFocus().child_id) |cid| row.children.getIndex(cid) orelse return else return; 1663 const on_status = cur == button_in_row_index and self.statusButton(index) != null; 1664 switch (key) { 1665 .arrow_left => if (cur > button_in_row_index) root_focus.setFocus(row.children.keys()[cur - 1]) else try self.focusList(index, root_focus), 1666 .arrow_right => if (cur + 1 < row.children.count()) root_focus.setFocus(row.children.keys()[cur + 1]), 1667 .arrow_up => self.focusHeader(root_focus), 1668 .arrow_down => self.focusTitle(index, root_focus), 1669 .enter => if (on_status) try self.toggleIssueStatus(allocator, index), 1670 .mouse => |mouse| if (self.statusButton(index)) |button| { 1671 if (inp.leftClickOn(root_focus, button.getFocus().id, mouse)) try self.toggleIssueStatus(allocator, index); 1672 }, 1673 else => {}, 1674 } 1675 } 1676 1677 fn titleInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 1678 switch (key) { 1679 .arrow_left => try self.focusList(index, root_focus), 1680 .arrow_up => self.focusToolRow(index, root_focus), 1681 .arrow_down => if (self.authorPresent(index)) self.focusAuthor(index, root_focus) else try self.focusDescription(index, root_focus), 1682 else => {}, 1683 } 1684 } 1685 1686 // the author box's a: link (when it has one) is followed by the host on 1687 // enter / a click; arrows cross to the neighboring widgets. 1688 fn authorInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 1689 switch (key) { 1690 .arrow_left => try self.focusList(index, root_focus), 1691 .arrow_up => self.focusTitle(index, root_focus), 1692 .arrow_down => if (self.tagFlow(index) != null) try self.focusTags(index, root_focus) else try self.focusDescription(index, root_focus), 1693 else => {}, 1694 } 1695 } 1696 1697 fn descriptionInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 1698 const sc = self.detailScroll(index); 1699 switch (key) { 1700 .arrow_left => return self.focusList(index, root_focus), 1701 // once the scroll can't move further, cross into the tags (or the 1702 // open/close button when the issue has none). 1703 .arrow_up => { 1704 const before = sc.y; 1705 sc.y -= 1; 1706 sc.clampToContent(); 1707 if (sc.y == before) { 1708 if (self.tagFlow(index) != null) 1709 try self.focusTags(index, root_focus) 1710 else if (self.authorPresent(index)) 1711 self.focusAuthor(index, root_focus) 1712 else 1713 self.focusTitle(index, root_focus); 1714 } 1715 return; 1716 }, 1717 .arrow_down => { 1718 if (self.moveCommentVertical(index, root_focus, true)) return; 1719 const before = sc.y; 1720 sc.y += 1; 1721 sc.clampToContent(); 1722 if (sc.y == before) _ = self.moveCommentVertical(index, root_focus, true); 1723 return; 1724 }, 1725 .page_up => sc.y -= 10, 1726 .page_down => sc.y += 10, 1727 .home => sc.y = 0, 1728 .end => sc.y = std.math.maxInt(isize), 1729 else => return, 1730 } 1731 sc.clampToContent(); 1732 } 1733 1734 fn commentInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 1735 switch (key) { 1736 .arrow_left => if (!self.moveCommentHorizontal(index, root_focus, false)) return self.focusList(index, root_focus), 1737 .arrow_right => _ = self.moveCommentHorizontal(index, root_focus, true), 1738 .arrow_up => _ = self.moveCommentVertical(index, root_focus, false), 1739 .arrow_down => _ = self.moveCommentVertical(index, root_focus, true), 1740 .page_up => self.detailScroll(index).y -= 10, 1741 .page_down => self.detailScroll(index).y += 10, 1742 .home => self.detailScroll(index).y = 0, 1743 .end => self.detailScroll(index).y = std.math.maxInt(isize), 1744 else => return, 1745 } 1746 self.detailScroll(index).clampToContent(); 1747 } 1748 1749 fn focusedDetailChild(self: *View, index: usize, root_focus: *Focus) ?usize { 1750 const inner = self.detailInner(index); 1751 var id = root_focus.grandchild_id orelse return null; 1752 while (inner.getFocus().children.get(id)) |child| { 1753 if (child.parent_id == inner.getFocus().id) return inner.children.getIndex(id); 1754 id = child.parent_id; 1755 } 1756 return null; 1757 } 1758 1759 fn detailChildFocusable(self: *View, index: usize, child_index: usize) bool { 1760 const child = &self.detailInner(index).children.values()[child_index].widget; 1761 return switch (child.*) { 1762 .repo_comment => true, 1763 .box => |*box| for (box.children.values()) |*item| { 1764 if (item.widget.getFocus().focusable) break true; 1765 } else false, 1766 else => child.getFocus().focusable, 1767 }; 1768 } 1769 1770 fn focusDetailChild(self: *View, index: usize, child_index: usize, last: bool, root_focus: *Focus) void { 1771 const inner = self.detailInner(index); 1772 const child = &inner.children.values()[child_index]; 1773 var target = inner.children.keys()[child_index]; 1774 var rect = child.rect orelse return; 1775 switch (child.widget) { 1776 .repo_comment => |*comment| { 1777 if (comment.rowRect(last)) |row_rect| { 1778 rect.x += row_rect.x; 1779 rect.y += row_rect.y; 1780 rect.size = row_rect.size; 1781 } 1782 if (last) comment.focusBody(root_focus) else comment.focusMetadata(root_focus); 1783 }, 1784 .box => |*box| if (box.children.count() > 0) { 1785 target = box.children.keys()[if (last) box.children.count() - 1 else 0]; 1786 root_focus.setFocus(target); 1787 }, 1788 else => root_focus.setFocus(target), 1789 } 1790 self.detailScroll(index).scrollToRect(rect); 1791 } 1792 1793 fn moveCommentVertical(self: *View, index: usize, root_focus: *Focus, down: bool) bool { 1794 const inner = self.detailInner(index); 1795 const current = self.focusedDetailChild(index, root_focus) orelse return false; 1796 if (inner.children.values()[current].widget == .repo_comment) { 1797 const comment = &inner.children.values()[current].widget.repo_comment; 1798 if (down and !comment.bodyFocused()) { 1799 self.focusDetailChild(index, current, true, root_focus); 1800 return true; 1801 } 1802 if (!down and comment.bodyFocused()) { 1803 self.focusDetailChild(index, current, false, root_focus); 1804 return true; 1805 } 1806 } 1807 1808 var next = current; 1809 while (true) { 1810 if (down) { 1811 next += 1; 1812 if (next >= inner.children.count()) return false; 1813 } else { 1814 if (next == 0) return false; 1815 next -= 1; 1816 } 1817 if (!self.detailChildFocusable(index, next)) continue; 1818 self.focusDetailChild(index, next, !down, root_focus); 1819 return true; 1820 } 1821 } 1822 1823 fn moveCommentHorizontal(self: *View, index: usize, root_focus: *Focus, right: bool) bool { 1824 const inner = self.detailInner(index); 1825 const current = self.focusedDetailChild(index, root_focus) orelse return false; 1826 const child = &inner.children.values()[current]; 1827 if (child.widget == .repo_comment) { 1828 const moved = child.widget.repo_comment.moveHorizontal(root_focus, right); 1829 if (moved) if (child.rect) |rect| self.detailScroll(index).scrollToRect(rect); 1830 return moved; 1831 } 1832 const row = switch (child.widget) { 1833 .box => |*box| box, 1834 else => return false, 1835 }; 1836 const selected = row.getFocus().child_id orelse return false; 1837 const selected_index = row.children.getIndex(selected) orelse return false; 1838 const target = if (right) selected_index + 1 else selected_index -| 1; 1839 if (target == selected_index or target >= row.children.count()) return false; 1840 root_focus.setFocus(row.children.keys()[target]); 1841 if (child.rect) |rect| self.detailScroll(index).scrollToRect(rect); 1842 return true; 1843 } 1844 1845 // up/down (and tab/shift+tab) move between a form's fields; up from the 1846 // title crosses into the header tabs. the multiline description keeps 1847 // enter and any up/down that has a row to move to. 1848 fn formInput(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1849 const form = self.issueForm() orelse return; 1850 const cid = form.getFocus().child_id orelse return; 1851 const cur = form.children.getIndex(cid) orelse return; 1852 1853 if (cur == description_field_index) { 1854 const child = &form.children.values()[cur]; 1855 if (try multilineKeepsKey(&child.widget.text_input, allocator, key)) 1856 return child.widget.input(allocator, key, root_focus); 1857 } 1858 1859 switch (key) { 1860 .arrow_up, .back_tab => if (cur > 0) 1861 root_focus.setFocus(form.children.keys()[cur - 1]) 1862 else 1863 self.focusHeader(root_focus), 1864 .arrow_down, .tab => if (cur < submit_field_index) { 1865 root_focus.setFocus(form.children.keys()[cur + 1]); 1866 }, 1867 .enter => if (cur == submit_field_index) try self.submitForm(allocator), 1868 .mouse => |mouse| if (cur == submit_field_index) { 1869 const submit = &form.children.values()[cur].widget.submit_button; 1870 if (inp.leftClickOn(root_focus, submit.buttonId(), mouse)) try self.submitForm(allocator); 1871 }, 1872 else => try form.children.values()[cur].widget.input(allocator, key, root_focus), 1873 } 1874 } 1875 1876 fn commentFormInput(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1877 const form = self.issueForm() orelse return; 1878 const cid = form.getFocus().child_id orelse return; 1879 const cur = form.children.getIndex(cid) orelse return; 1880 const child = &form.children.values()[cur]; 1881 1882 if (cur == comment_body_field_index and try multilineKeepsKey(&child.widget.text_input, allocator, key)) 1883 return child.widget.input(allocator, key, root_focus); 1884 1885 switch (key) { 1886 .arrow_up, .back_tab => if (cur > comment_author_field_index) 1887 root_focus.setFocus(form.children.keys()[cur - 1]) 1888 else 1889 self.focusHeader(root_focus), 1890 .arrow_down, .tab => if (cur < comment_submit_field_index) 1891 root_focus.setFocus(form.children.keys()[cur + 1]), 1892 .enter => if (cur == comment_submit_field_index) try self.submitComment(allocator), 1893 .mouse => |mouse| if (cur == comment_submit_field_index) { 1894 const submit = &child.widget.submit_button; 1895 if (inp.leftClickOn(root_focus, submit.buttonId(), mouse)) try self.submitComment(allocator); 1896 }, 1897 else => try child.widget.input(allocator, key, root_focus), 1898 } 1899 } 1900 1901 // the multiline hunk inputs keep enter and any up/down that has a row to 1902 // move to; enter on a "use this" link is a navigation the host follows 1903 // before this runs. 1904 fn resolveInput(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1905 const form = self.issueForm() orelse return; 1906 const cid = form.getFocus().child_id orelse return; 1907 const cur = form.children.getIndex(cid) orelse return; 1908 const child = &form.children.values()[cur]; 1909 1910 if (child.widget == .text_input) { 1911 const ti = &child.widget.text_input; 1912 if (ti.options.multiline and try multilineKeepsKey(ti, allocator, key)) 1913 return child.widget.input(allocator, key, root_focus); 1914 } 1915 1916 const on_submit = child.widget == .submit_button; 1917 1918 switch (key) { 1919 .arrow_up, .back_tab => if (resolveStep(form, cur, false)) |i| self.focusResolveChild(form, i, root_focus) else self.focusHeader(root_focus), 1920 .arrow_down, .tab => if (resolveStep(form, cur, true)) |i| self.focusResolveChild(form, i, root_focus), 1921 .arrow_left, .arrow_right => switch (child.widget) { 1922 .box => |*row| { 1923 const row_cid = row.getFocus().child_id orelse return; 1924 const row_index = row.children.getIndex(row_cid) orelse return; 1925 if (key == .arrow_left) { 1926 if (row_index > 0) root_focus.setFocus(row.children.keys()[row_index - 1]); 1927 } else if (row_index + 1 < row.children.count()) { 1928 root_focus.setFocus(row.children.keys()[row_index + 1]); 1929 } 1930 }, 1931 else => try child.widget.input(allocator, key, root_focus), 1932 }, 1933 .enter => if (on_submit) try self.submitResolution(allocator), 1934 .mouse => |mouse| if (on_submit) { 1935 if (inp.leftClickOn(root_focus, child.widget.submit_button.buttonId(), mouse)) try self.submitResolution(allocator); 1936 }, 1937 else => try child.widget.input(allocator, key, root_focus), 1938 } 1939 } 1940 1941 // a multiline input keeps enter and any up/down that has a row to move to 1942 fn multilineKeepsKey(text_input: *wgt.TextInput(ui.Widget), allocator: std.mem.Allocator, key: Key) !bool { 1943 return switch (key) { 1944 .enter => true, 1945 .arrow_up => !try text_input.cursorOnFirstRow(allocator), 1946 .arrow_down => !try text_input.cursorOnLastRow(allocator), 1947 else => false, 1948 }; 1949 } 1950 1951 // the neighboring focusable resolve-form child, skipping the spacer and 1952 // the blank gap rows 1953 fn resolveStep(form: *wgt.Box(ui.Widget), cur: usize, down: bool) ?usize { 1954 var i = cur; 1955 while (true) { 1956 if (down) { 1957 i += 1; 1958 if (i >= form.children.count()) return null; 1959 } else { 1960 if (i == 0) return null; 1961 i -= 1; 1962 } 1963 switch (form.children.values()[i].widget) { 1964 .spacer, .text => continue, 1965 else => return i, 1966 } 1967 } 1968 } 1969 1970 // focus the resolve-form child at `i` (a version row focuses its selected 1971 // child) and keep it visible on the terminal 1972 fn focusResolveChild(self: *View, form: *wgt.Box(ui.Widget), i: usize, root_focus: *Focus) void { 1973 const child = &form.children.values()[i]; 1974 switch (child.widget) { 1975 .box => |*row| root_focus.setFocus(row.getFocus().child_id orelse form.children.keys()[i]), 1976 else => root_focus.setFocus(form.children.keys()[i]), 1977 } 1978 if (self.session.is_terminal) { 1979 if (self.resolveScroll()) |sc| { 1980 if (child.rect) |rect| sc.scrollToRect(rect); 1981 } 1982 } 1983 } 1984 1985 // re-emit the conflicted issue's event with the resolved content; any 1986 // event on the issue settles its conflict entry. this is the terminal 1987 // path; the web posts the form to the resolve route. 1988 fn submitResolution(self: *View, allocator: std.mem.Allocator) !void { 1989 if (comptime wasm) return; 1990 const io = self.session.io orelse return; 1991 const src = self.data.repo_source orelse return; 1992 const entry = self.data.selectedIssue() orelse return; 1993 const author_email = (try self.session.eventAuthorEmail()) orelse return; 1994 const form = self.issueForm() orelse return; 1995 1996 // gather the inputs by name; the d<n> hunk inputs appear in chunk order 1997 var title: ?[]u8 = null; 1998 var tags: ?[]u8 = null; 1999 var hunks: std.ArrayList([]const u8) = .empty; 2000 defer { 2001 if (title) |t| allocator.free(t); 2002 if (tags) |t| allocator.free(t); 2003 for (hunks.items) |h| allocator.free(h); 2004 hunks.deinit(allocator); 2005 } 2006 for (form.children.values()) |*child| switch (child.widget) { 2007 .text_input => |*ti| { 2008 const text = try ti.text(allocator); 2009 errdefer allocator.free(text); 2010 if (std.mem.eql(u8, ti.options.name, "title")) { 2011 title = text; 2012 } else if (std.mem.eql(u8, ti.options.name, "tags")) { 2013 tags = text; 2014 } else { 2015 try hunks.append(allocator, text); 2016 } 2017 }, 2018 else => {}, 2019 }; 2020 2021 var id_bytes: [evt.event_id_size]u8 = undefined; 2022 _ = try std.fmt.hexToBytes(&id_bytes, entry.id[0 .. evt.event_id_size * 2]); 2023 2024 switch (src.repo_kind) { 2025 inline else => |repo_kind| { 2026 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, .{ .path = src.path }); 2027 defer any_repo.deinit(io, allocator); 2028 switch (any_repo) { 2029 inline else => |*repo| evt.Issue.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .resolve = .{ 2030 .title = title, 2031 .tags = tags, 2032 .hunks = hunks.items, 2033 } }, author_email) catch |err| switch (err) { 2034 // leave the form up for correction 2035 error.InvalidFields => return, 2036 else => |e| return e, 2037 }, 2038 } 2039 }, 2040 } 2041 2042 const route = ui.RoutablePage.repoIssuesRoute(self.data.identity, entry.issue.event.status, "", entry.id) orelse return; 2043 try self.session.navigate(route); 2044 } 2045 2046 // commit a reply and navigate to its permalink. this is the terminal path; 2047 // the web posts the form to its /comment route. 2048 fn submitComment(self: *View, allocator: std.mem.Allocator) !void { 2049 if (comptime wasm) return; 2050 const io = self.session.io orelse return; 2051 const src = self.data.repo_source orelse return; 2052 const author_email = (try self.session.eventAuthorEmail()) orelse return; 2053 const form = self.issueForm() orelse return; 2054 const body_input = &form.children.values()[comment_body_field_index].widget.text_input; 2055 const body = try body_input.text(allocator); 2056 defer allocator.free(body); 2057 if (!evt.Comment.fieldsValid(body)) return; 2058 2059 var thread_id: [evt.event_id_size * 2]u8 = undefined; 2060 @memcpy(&thread_id, self.data.selected_id); 2061 const parent_id = if (self.data.comment_id.len == 0) thread_id else blk: { 2062 var parent: [evt.event_id_size * 2]u8 = undefined; 2063 @memcpy(&parent, self.data.comment_id); 2064 break :blk parent; 2065 }; 2066 var event_id_hex: [evt.event_id_size * 2]u8 = undefined; 2067 switch (src.repo_kind) { 2068 inline else => |repo_kind| { 2069 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, .{ .path = src.path }); 2070 defer any_repo.deinit(io, allocator); 2071 switch (any_repo) { 2072 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), 2073 } 2074 }, 2075 } 2076 2077 body_input.clear(allocator); 2078 const route = ui.RoutablePage.repoCommentsRoute(self.data.identity, &thread_id, &event_id_hex, 0) orelse return; 2079 try self.session.navigate(route); 2080 } 2081 2082 // the terminal submit paths: the new form commits a new-issue event, the 2083 // edit form re-emits the selected issue's event with the form's content. 2084 // the web posts the forms to their routes instead. 2085 fn submitForm(self: *View, allocator: std.mem.Allocator) !void { 2086 if (self.data.view == .edit) { 2087 try self.submitEditedIssue(allocator); 2088 } else { 2089 try self.submitNewIssue(allocator); 2090 } 2091 } 2092 2093 // commit the new issue to the repo's events branch and navigate to it. 2094 // this is the terminal path; the web posts the form to the issue route, 2095 // so the wasm side never runs (or compiles) the repo access below. 2096 fn submitNewIssue(self: *View, allocator: std.mem.Allocator) !void { 2097 if (comptime wasm) return; 2098 const io = self.session.io orelse return; 2099 const src = self.data.repo_source orelse return; 2100 const author_email = (try self.session.eventAuthorEmail()) orelse return; 2101 2102 const form = self.issueForm() orelse return; 2103 const title_input = &form.children.values()[title_field_index].widget.text_input; 2104 const tags_input = &form.children.values()[tags_field_index].widget.text_input; 2105 const description_input = &form.children.values()[description_field_index].widget.text_input; 2106 2107 const title = try title_input.text(allocator); 2108 defer allocator.free(title); 2109 const tags = try tags_input.text(allocator); 2110 defer allocator.free(tags); 2111 const description = try description_input.text(allocator); 2112 defer allocator.free(description); 2113 2114 if (!evt.Issue.fieldsValid(title, tags)) return; 2115 2116 var id_bytes: [evt.event_id_size]u8 = undefined; 2117 io.random(&id_bytes); 2118 const event_id_hex = std.fmt.bytesToHex(id_bytes, .lower); 2119 2120 const event = evt.EventWithId{ 2121 .id = event_id_hex, 2122 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 2123 .author_email = author_email, 2124 .event = .{ .issue = .{ 2125 .title = title, 2126 .description = description, 2127 .tags = tags, 2128 } }, 2129 }; 2130 2131 switch (src.repo_kind) { 2132 inline else => |repo_kind| { 2133 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, .{ .path = src.path }); 2134 defer any_repo.deinit(io, allocator); 2135 switch (any_repo) { 2136 inline else => |*repo| try evt.consume(repo_kind, repo.self_repo_opts, io, allocator, repo, evt.events_ref, &.{event}), 2137 } 2138 }, 2139 } 2140 2141 // wipe the form so a return visit starts fresh 2142 title_input.clear(allocator); 2143 tags_input.clear(allocator); 2144 description_input.clear(allocator); 2145 2146 const route = ui.RoutablePage.repoIssuesRoute(self.data.identity, .open, "", &event_id_hex) orelse return; 2147 try self.session.navigate(route); 2148 } 2149 2150 // re-emit the selected issue's event with the form's content (status 2151 // preserved), then reload the issue's page. this is the terminal path; 2152 // the web posts the form to the edit route. 2153 fn submitEditedIssue(self: *View, allocator: std.mem.Allocator) !void { 2154 if (comptime wasm) return; 2155 const io = self.session.io orelse return; 2156 const src = self.data.repo_source orelse return; 2157 const entry = self.data.selectedIssue() orelse return; 2158 const author_email = (try self.session.eventAuthorEmail()) orelse return; 2159 2160 const form = self.issueForm() orelse return; 2161 const title_input = &form.children.values()[title_field_index].widget.text_input; 2162 const tags_input = &form.children.values()[tags_field_index].widget.text_input; 2163 const description_input = &form.children.values()[description_field_index].widget.text_input; 2164 2165 const title = try title_input.text(allocator); 2166 defer allocator.free(title); 2167 const tags = try tags_input.text(allocator); 2168 defer allocator.free(tags); 2169 const description = try description_input.text(allocator); 2170 defer allocator.free(description); 2171 2172 if (!evt.Issue.fieldsValid(title, tags)) return; 2173 2174 var id_bytes: [evt.event_id_size]u8 = undefined; 2175 _ = try std.fmt.hexToBytes(&id_bytes, entry.id[0 .. evt.event_id_size * 2]); 2176 2177 switch (src.repo_kind) { 2178 inline else => |repo_kind| { 2179 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, .{ .path = src.path }); 2180 defer any_repo.deinit(io, allocator); 2181 switch (any_repo) { 2182 inline else => |*repo| try evt.Issue.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 2183 .title = title, 2184 .tags = tags, 2185 .description = description, 2186 } }, author_email), 2187 } 2188 }, 2189 } 2190 2191 const route = ui.RoutablePage.repoIssuesRoute(self.data.identity, entry.issue.event.status, "", entry.id) orelse return; 2192 try self.session.navigate(route); 2193 } 2194 2195 // flip the shown issue's status by re-emitting its event, then reload the 2196 // page rooted at the issue so the view reflects the change. this is the 2197 // terminal path; the web posts the button's form to the status route. 2198 fn toggleIssueStatus(self: *View, allocator: std.mem.Allocator, index: usize) !void { 2199 if (comptime wasm) return; 2200 const io = self.session.io orelse return; 2201 const src = self.data.repo_source orelse return; 2202 const sel = self.detailed_index[index] orelse return; 2203 const entry = self.window(index).issues[sel]; 2204 const author_email = (try self.session.eventAuthorEmail()) orelse return; 2205 2206 const status: evt.Issue.Status = switch (entry.issue.event.status) { 2207 .open => .closed, 2208 .closed => .open, 2209 }; 2210 2211 var id_bytes: [evt.event_id_size]u8 = undefined; 2212 _ = try std.fmt.hexToBytes(&id_bytes, entry.id[0 .. evt.event_id_size * 2]); 2213 2214 switch (src.repo_kind) { 2215 inline else => |repo_kind| { 2216 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, .{ .path = src.path }); 2217 defer any_repo.deinit(io, allocator); 2218 switch (any_repo) { 2219 inline else => |*repo| try evt.Issue.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .status = status }, author_email), 2220 } 2221 }, 2222 } 2223 2224 const route = ui.RoutablePage.repoIssuesRoute(self.data.identity, status, "", entry.id) orelse return; 2225 try self.session.navigate(route); 2226 } 2227 2228 // arrow keys move the tag selection; at the flow's edges focus crosses to 2229 // the neighboring widgets. 2230 fn tagsInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 2231 const tf = self.tagFlow(index) orelse return; 2232 const cid = tf.focus.child_id orelse return; 2233 const cur = tf.indexOfFocusId(cid) orelse return; 2234 const count = tf.text_boxes.items.len; 2235 switch (key) { 2236 .arrow_left => if (cur > 0) self.focusTag(index, tf, root_focus, cur - 1) else try self.focusList(index, root_focus), 2237 .arrow_right => if (cur + 1 < count) self.focusTag(index, tf, root_focus, cur + 1), 2238 .arrow_up => if (tf.rowStep(cur, false)) |i| self.focusTag(index, tf, root_focus, i) else self.focusAuthor(index, root_focus), 2239 .arrow_down => if (tf.rowStep(cur, true)) |i| self.focusTag(index, tf, root_focus, i) else try self.focusDescription(index, root_focus), 2240 .home => self.focusTag(index, tf, root_focus, 0), 2241 .end => self.focusTag(index, tf, root_focus, count - 1), 2242 else => {}, 2243 } 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