╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Issues.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
844 fn initIssueForm(allocator: std.mem.Allocator, session: *ui.Session, action: []const u8, issue: ?*const evt.Issue.Record) !wgt.Box(ui.Widget) { 845 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 846 errdefer box.deinit(allocator); 847 box.getFocus().kind = .{ .custom = action }; 848 849 { 850 var title = try wgt.TextInput(ui.Widget).init(allocator, .{ .label = " title ", .name = "title", .visible_width = null, .rounded_corners = true, .render_content = session.is_terminal }); 851 errdefer title.deinit(allocator); 852 title.getFocus().focusable = true; 853 if (issue) |i| try title.setContent(allocator, i.event.title); 854 try box.children.put(allocator, title.getFocus().id, .{ .widget = .{ .text_input = title }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 855 } 856 857 { 858 var tags = try wgt.TextInput(ui.Widget).init(allocator, .{ .label = " tags (separate with spaces) ", .name = "tags", .visible_width = null, .rounded_corners = true, .render_content = session.is_terminal }); 859 errdefer tags.deinit(allocator); 860 tags.getFocus().focusable = true; 861 if (issue) |i| try tags.setContent(allocator, i.event.tags); 862 try box.children.put(allocator, tags.getFocus().id, .{ .widget = .{ .text_input = tags }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 863 } 864 865 { 866 var description = try wgt.TextInput(ui.Widget).init(allocator, .{ .label = " description ", .name = "description", .visible_width = null, .rounded_corners = true, .render_content = session.is_terminal, .multiline = true, .scroll = .{ .fill = true } }); 867 errdefer description.deinit(allocator); 868 description.getFocus().focusable = true; 869 if (issue) |i| try description.setContent(allocator, i.event.description); 870 try box.children.put(allocator, description.getFocus().id, .{ .widget = .{ .text_input = description }, .rect = null, .min_size = null }); 871 } 872 873 try addSubmitButton(allocator, &box); 874 875 box.getFocus().child_id = box.children.keys()[title_field_index]; 876 return box; 877 } 878 879 fn initCommentForm(allocator: std.mem.Allocator, session: *ui.Session, action: []const u8, author: ui.Author, parent_route: ui.RoutablePage, initial_body: ?[]const u8) !wgt.Box(ui.Widget) { 880 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 881 errdefer box.deinit(allocator); 882 box.getFocus().kind = .{ .custom = action }; 883 884 { 885 var parent = try ui.authorBox(allocator, session.page_arena, author); 886 errdefer parent.deinit(allocator); 887 parent.options.label = " replying to "; 888 parent.getFocus().kind = .{ .custom = try std.fmt.allocPrint(session.page_arena.allocator(), "a:{s}", .{try parent_route.toUrl(session.page_arena)}) }; 889 try box.children.put(allocator, parent.getFocus().id, .{ .widget = .{ .text_box = parent }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 890 } 891 892 { 893 var body = try wgt.TextInput(ui.Widget).init(allocator, .{ 894 .label = " comment ", 895 .name = "body", 896 .visible_width = null, 897 .rounded_corners = true, 898 .render_content = session.is_terminal, 899 .multiline = true, 900 .visible_height = 5, 901 .scroll = .{ .fill = true }, 902 }); 903 errdefer body.deinit(allocator); 904 body.getFocus().focusable = true; 905 if (initial_body) |text| try body.setContent(allocator, text); 906 try box.children.put(allocator, body.getFocus().id, .{ .widget = .{ .text_input = body }, .rect = null, .min_size = null }); 907 } 908 909 try addSubmitButtonLabeled(allocator, &box, "submit"); 910 box.getFocus().child_id = box.children.keys()[comment_author_field_index]; 911 return box; 912 } 913 914 // a form's submit button, then a spacer absorbing the leftover 915 // min-height the box hands its last child, so the button keeps its 916 // natural height 917 fn addSubmitButton(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget)) !void { 918 return addSubmitButtonLabeled(allocator, box, "submit"); 919 } 920 921 fn addSubmitButtonLabeled(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), label: []const u8) !void { 922 { 923 var submit = try ui.SubmitButton.initLabeled(allocator, label); 924 errdefer submit.deinit(allocator); 925 try box.children.put(allocator, submit.getFocus().id, .{ .widget = .{ .submit_button = submit }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 926 } 927 { 928 var spacer = try ui.Spacer.init(allocator); 929 errdefer spacer.deinit(allocator); 930 try box.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 931 } 932 } 933 934 // the conflict resolve form. the "use this" links are navigations that 935 // flip the url's theirs: pick, reloading the prefills; one submit 936 // settles every conflict. the form: subtree makes the web overlay POST 937 // to the resolve route. 938 fn initResolveForm(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self) !wgt.Box(ui.Widget) { 939 const aa = session.page_arena.allocator(); 940 // init only picks the resolve view once it has read the conflict 941 const conflict = if (data.conflict) |*c| c else unreachable; 942 943 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 944 errdefer box.deinit(allocator); 945 box.getFocus().kind = .{ .custom = if (data.identity.len == 0) 946 try std.fmt.allocPrint(aa, "form:/issue:{s}/resolve", .{data.selected_id}) 947 else 948 try std.fmt.allocPrint(aa, "form:/repo/{s}/issue:{s}/resolve", .{ data.identity, data.selected_id }) }; 949 950 if (conflict.title) |*fc| { 951 try addLabel(allocator, &box, "title conflict:"); 952 try addFieldConflict(allocator, &box, session, data, "title", fc); 953 } 954 if (conflict.tags) |*fc| { 955 try addLabel(allocator, &box, "tags conflict:"); 956 try addFieldConflict(allocator, &box, session, data, "tags", fc); 957 } 958 959 if (conflict.description) |*desc| { 960 try addLabel(allocator, &box, "description conflict:"); 961 var hunk_index: usize = 0; 962 for (desc.chunks, 0..) |*chunk, chunk_index| { 963 switch (chunk.*) { 964 .same => |text| { 965 var tb = try wgt.TextBox(ui.Widget).init(allocator, text, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .word }); 966 errdefer tb.deinit(allocator); 967 tb.getFocus().focusable = true; 968 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 969 }, 970 // an auto-resolved chunk: only one side changed it, so its 971 // version stands, shown distinctly with its author 972 .auto => |auto| { 973 const author = if (auto.theirs) desc.theirs_author else desc.ours_author; 974 const verb: []const u8 = if (auto.text == null) "removed" else "edited"; 975 const label = switch (author) { 976 .user_name, .email => |name| try std.fmt.allocPrint(aa, " {s} by {s} ", .{ verb, name }), 977 .unknown => try std.fmt.allocPrint(aa, " {s} by {s} ", .{ verb, if (auto.theirs) @as([]const u8, "them") else "us" }), 978 }; 979 var tb = try wgt.TextBox(ui.Widget).init(allocator, auto.text orelse "(removed)", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .word, .label = label }); 980 errdefer tb.deinit(allocator); 981 tb.getFocus().focusable = true; 982 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 983 }, 984 .conflict => |hunk| { 985 // a blank row on each side sets the group apart from 986 // the flowing shared and auto-resolved chunks 987 if (chunk_index > 0) try addGap(allocator, &box); 988 const name = try std.fmt.allocPrint(aa, "d{d}", .{hunk_index}); 989 hunk_index += 1; 990 const picked_theirs = data.theirsPicked(name); 991 try addVersionRow(allocator, &box, try sideLabel(aa, desc.ours_author, true), hunk.ours orelse "", try useThisLink(session, data, name, false)); 992 try addVersionRow(allocator, &box, try sideLabel(aa, desc.theirs_author, false), hunk.theirs orelse "", try useThisLink(session, data, name, true)); 993 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 }); 994 errdefer resolution_input.deinit(allocator); 995 resolution_input.getFocus().focusable = true; 996 try resolution_input.setContent(allocator, (if (picked_theirs) hunk.theirs else hunk.ours) orelse ""); 997 try box.children.put(allocator, resolution_input.getFocus().id, .{ .widget = .{ .text_input = resolution_input }, .rect = null, .min_size = null }); 998 if (chunk_index + 1 < desc.chunks.len) try addGap(allocator, &box); 999 }, 1000 } 1001 } 1002 } 1003 1004 try addSubmitButton(allocator, &box); 1005 1006 box.getFocus().child_id = box.children.keys()[0]; 1007 return box; 1008 } 1009 1010 // one conflicted scalar field: both sides' version rows, then the 1011 // resolution input prefilled from the picked side. 1012 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 { 1013 const aa = session.page_arena.allocator(); 1014 try addVersionRow(allocator, box, try sideLabel(aa, fc.ours.author, true), fc.ours.text, try useThisLink(session, data, name, false)); 1015 try addVersionRow(allocator, box, try sideLabel(aa, fc.theirs.author, false), fc.theirs.text, try useThisLink(session, data, name, true)); 1016 1017 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 }); 1018 errdefer resolution_input.deinit(allocator); 1019 resolution_input.getFocus().focusable = true; 1020 try resolution_input.setContent(allocator, if (data.theirsPicked(name)) fc.theirs.text else fc.ours.text); 1021 try box.children.put(allocator, resolution_input.getFocus().id, .{ .widget = .{ .text_input = resolution_input }, .rect = null, .min_size = null }); 1022 } 1023 1024 // a blank row setting a conflict group apart from its neighbors 1025 fn addGap(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget)) !void { 1026 var text = try wgt.Text(ui.Widget).init(allocator, " "); 1027 errdefer text.deinit(allocator); 1028 try box.children.put(allocator, text.getFocus().id, .{ .widget = .{ .text = text }, .rect = null, .min_size = null }); 1029 } 1030 1031 fn addLabel(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), content: []const u8) !void { 1032 var label = try ui.SectionLabel.init(allocator, content); 1033 errdefer label.deinit(allocator); 1034 try box.children.put(allocator, label.getFocus().id, .{ .widget = .{ .section_label = label }, .rect = null, .min_size = null }); 1035 } 1036 1037 // one side of a conflict: the "use this" link on the left, the version 1038 // itself as a labeled read-only box. 1039 fn addVersionRow(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), label: []const u8, text: []const u8, link: []const u8) !void { 1040 var row = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 1041 errdefer row.deinit(allocator); 1042 1043 { 1044 const use_label = "use this"; 1045 var use = try wgt.TextBox(ui.Widget).init(allocator, use_label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1046 errdefer use.deinit(allocator); 1047 use.getFocus().focusable = true; 1048 use.getFocus().kind = .{ .custom = link }; 1049 try row.children.put(allocator, use.getFocus().id, .{ 1050 .widget = .{ .text_box = use }, 1051 .rect = null, 1052 .min_size = .{ .width = use_label.len + 2, .height = null }, 1053 .max_size = .{ .width = use_label.len + 2, .height = null }, 1054 }); 1055 } 1056 1057 { 1058 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 }); 1059 errdefer tb.deinit(allocator); 1060 tb.getFocus().focusable = true; 1061 try row.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1062 } 1063 1064 row.getFocus().child_id = row.children.keys()[0]; 1065 try box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .box = row }, .rect = null, .min_size = null }); 1066 } 1067 1068 // the label of one side's version box, naming its author 1069 fn sideLabel(aa: std.mem.Allocator, author: ui.Author, ours: bool) ![]const u8 { 1070 return switch (author) { 1071 .user_name, .email => |name| try std.fmt.allocPrint(aa, " edited by {s} ", .{name}), 1072 .unknown => if (ours) " current version " else " their version ", 1073 }; 1074 } 1075 1076 // the a: link that reloads the resolve page with `name` picked from ours 1077 // or theirs, reprefilling its resolution input 1078 fn useThisLink(session: *ui.Session, data: *const Self, name: []const u8, theirs: bool) ![]const u8 { 1079 const aa = session.page_arena.allocator(); 1080 var picks: std.ArrayList(u8) = .empty; 1081 var it = std.mem.splitScalar(u8, data.theirs_picks, ','); 1082 while (it.next()) |pick| { 1083 if (pick.len == 0 or std.mem.eql(u8, pick, name)) continue; 1084 if (picks.items.len > 0) try picks.append(aa, ','); 1085 try picks.appendSlice(aa, pick); 1086 } 1087 if (theirs) { 1088 if (picks.items.len > 0) try picks.append(aa, ','); 1089 try picks.appendSlice(aa, name); 1090 } 1091 const route = ui.RoutablePage.repoIssuesResolveRoute(data.identity, data.selected_id, picks.items) orelse return error.RouteTooLong; 1092 return std.fmt.allocPrint(aa, "a:{s}", .{try route.toUrl(session.page_arena)}); 1093 } 1094 1095 // the resolve form's page-constant initial value for the input `name`: 1096 // the picked side of its conflicted field or hunk 1097 fn resolvePrefill(self: *View, name: []const u8) ?[]const u8 { 1098 const conflict = if (self.data.conflict) |*c| c else return null; 1099 const theirs = self.data.theirsPicked(name); 1100 if (std.mem.eql(u8, name, "title")) { 1101 const fc = if (conflict.title) |*f| f else return null; 1102 return if (theirs) fc.theirs.text else fc.ours.text; 1103 } 1104 if (std.mem.eql(u8, name, "tags")) { 1105 const fc = if (conflict.tags) |*f| f else return null; 1106 return if (theirs) fc.theirs.text else fc.ours.text; 1107 } 1108 if (std.mem.startsWith(u8, name, "d")) { 1109 const desc = if (conflict.description) |*d| d else return null; 1110 const hunk_index = std.fmt.parseInt(usize, name[1..], 10) catch return null; 1111 var seen: usize = 0; 1112 for (desc.chunks) |chunk| switch (chunk) { 1113 .conflict => |hunk| { 1114 if (seen == hunk_index) return (if (theirs) hunk.theirs else hunk.ours) orelse ""; 1115 seen += 1; 1116 }, 1117 else => {}, 1118 }; 1119 } 1120 return null; 1121 } 1122 1123 fn addRow(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), text: []const u8, bottom_label: []const u8, link: []const u8) !void { 1124 var row = try wgt.TextBox(ui.Widget).init(allocator, text, .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .word, .bottom_label = bottom_label }); 1125 errdefer row.deinit(allocator); 1126 row.getFocus().focusable = true; 1127 if (link.len != 0) row.getFocus().kind = .{ .custom = link }; 1128 try box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .text_box = row }, .rect = null, .min_size = null, .max_size = .{ .width = null, .height = 5 } }); 1129 } 1130 1131 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 1132 self.box.deinit(allocator); 1133 } 1134 1135 fn header(self: *View) *Header { 1136 return &self.box.children.values()[header_index].widget.repo_issues_header; 1137 } 1138 1139 fn viewStack(self: *View) *wgt.Stack(ui.Widget) { 1140 return &self.box.children.values()[stack_index].widget.stack; 1141 } 1142 1143 // `index`'s master-detail split inside the stack. 1144 fn resultsBox(self: *View, index: usize) *wgt.Box(ui.Widget) { 1145 return &self.viewStack().children.values()[index].box; 1146 } 1147 1148 // the tags view's flow inside the stack. 1149 fn tagsView(self: *View) *ui.TagFlow { 1150 return &self.viewStack().children.values()[tags_view_index].tag_flow; 1151 } 1152 1153 // the new-issue, edit, or resolve form inside the stack, or null when the 1154 // unauthorized view stands in for it. 1155 fn issueForm(self: *View) ?*wgt.Box(ui.Widget) { 1156 return switch (self.viewStack().children.values()[form_view_index]) { 1157 .box => |*box| box, 1158 // the resolve form sits inside a scroll on the terminal 1159 .scroll => |*scroll| &scroll.child.box, 1160 else => null, 1161 }; 1162 } 1163 1164 // the resolve form's scroll on the terminal (the web page scrolls itself) 1165 fn resolveScroll(self: *View) ?*wgt.Scroll(ui.Widget) { 1166 return switch (self.viewStack().children.values()[form_view_index]) { 1167 .scroll => |*scroll| scroll, 1168 else => null, 1169 }; 1170 } 1171 1172 fn listScroll(self: *View, index: usize) *wgt.Scroll(ui.Widget) { 1173 return &self.resultsBox(index).children.values()[list_index].widget.scroll; 1174 } 1175 1176 fn listBox(self: *View, index: usize) *wgt.Box(ui.Widget) { 1177 return &self.listScroll(index).child.box; 1178 } 1179 1180 // the detail frame's children: the tool row above the scroll pane. 1181 const tool_row_index: usize = 0; 1182 const detail_scroll_index: usize = 1; 1183 1184 fn detailOuter(self: *View, index: usize) *wgt.Box(ui.Widget) { 1185 return &self.resultsBox(index).children.values()[detail_index].widget.box; 1186 } 1187 1188 fn toolRow(self: *View, index: usize) *wgt.Box(ui.Widget) { 1189 return &self.detailOuter(index).children.values()[tool_row_index].widget.box; 1190 } 1191 1192 fn detailScroll(self: *View, index: usize) *wgt.Scroll(ui.Widget) { 1193 return &self.detailOuter(index).children.values()[detail_scroll_index].widget.scroll; 1194 } 1195 1196 fn detailInner(self: *View, index: usize) *wgt.Box(ui.Widget) { 1197 return &self.detailScroll(index).child.box; 1198 } 1199 1200 fn window(self: *View, index: usize) *const Window { 1201 if (index == conflicts_view_index) return &self.data.conflicts; 1202 return self.data.window(splitStatus(index)); 1203 } 1204 1205 fn stackSelectedIndex(self: *View) ?usize { 1206 const stack = self.viewStack(); 1207 const cid = stack.getFocus().child_id orelse return null; 1208 return stack.children.getIndex(cid); 1209 } 1210 1211 // the stack's selected master-detail split (null when the tags view or the 1212 // new-issue form shows). 1213 fn selectedSplitIndex(self: *View) ?usize { 1214 const idx = self.stackSelectedIndex() orelse return null; 1215 return if (idx < split_count or idx == conflicts_view_index) idx else null; 1216 } 1217 1218 fn detailActive(self: *View, index: usize) bool { 1219 const rb = self.resultsBox(index); 1220 const cid = rb.getFocus().child_id orelse return false; 1221 return rb.children.getIndex(cid) == detail_index; 1222 } 1223 1224 fn headerActive(self: *View) bool { 1225 const cid = self.box.getFocus().child_id orelse return false; 1226 return self.box.children.getIndex(cid) == header_index; 1227 } 1228 1229 fn tagsViewActive(self: *View) bool { 1230 if (self.headerActive()) return false; 1231 return self.stackSelectedIndex() == tags_view_index; 1232 } 1233 1234 fn formViewActive(self: *View) bool { 1235 if (self.headerActive()) return false; 1236 return self.stackSelectedIndex() == form_view_index; 1237 } 1238 1239 // the selected issue's index, or null when a window-navigation row is 1240 // selected (a leading "previous" row shifts the issue rows down by one). 1241 fn selectedIssueIndex(self: *View, index: usize) ?usize { 1242 const lb = self.listBox(index); 1243 const cid = lb.getFocus().child_id orelse return null; 1244 const idx = lb.children.getIndex(cid) orelse return null; 1245 const win = self.window(index); 1246 const lead: usize = if (win.prev_id != null) 1 else 0; 1247 if (idx < lead or idx - lead >= win.issues.len) return null; 1248 return idx - lead; 1249 } 1250 1251 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1252 self.clearGrid(); 1253 1254 // each header tab maps 1:1 to a stack child by position. mirror the 1255 // selection into the url: the view the page's window is rooted at 1256 // keeps its rooted url, the others get their list route (keeping the 1257 // tag filter). 1258 if (self.header().getSelectedIndex()) |index| { 1259 const stack = self.viewStack(); 1260 stack.getFocus().child_id = stack.children.keys()[index]; 1261 self.session.data.current_page = (if (index == form_view_index) 1262 (switch (self.data.view) { 1263 .edit => ui.RoutablePage.repoIssuesEditRoute(self.data.identity, self.data.selected_id), 1264 .new_comment => ui.RoutablePage.repoCommentNewRoute(self.data.identity, self.data.selected_id, self.data.comment_id), 1265 .edit_comment => ui.RoutablePage.repoCommentEditRoute(self.data.identity, self.data.selected_id, self.data.comment_id), 1266 .resolve => ui.RoutablePage.repoIssuesResolveRoute(self.data.identity, self.data.selected_id, self.data.theirs_picks), 1267 else => ui.RoutablePage.repoIssuesNewRoute(self.data.identity), 1268 }) 1269 else if (index == conflicts_view_index) 1270 ui.RoutablePage.repoIssuesConflictsRoute(self.data.identity, if (self.data.view == .conflicts) self.data.selected_id else "") 1271 else if (index == viewIndex(self.data.view) and self.data.selected_id.len != 0) 1272 (if (self.data.description_page) 1273 ui.RoutablePage.repoIssuesDescriptionRoute(self.data.identity, self.data.selected_id) 1274 else if (self.data.comment_page) |page| 1275 ui.RoutablePage.repoCommentsRoute(self.data.identity, self.data.selected_id, &page.selected.id, page.replies.start) 1276 else 1277 ui.RoutablePage.repoIssueCommentsRoute(self.data.identity, self.data.selected_id, self.data.comments_start)) 1278 else if (index == tags_view_index) 1279 ui.RoutablePage.repoIssuesTagsRoute(self.data.identity, self.data.tag) 1280 else 1281 ui.RoutablePage.repoIssuesRoute(self.data.identity, splitStatus(index), self.data.tag, "")) orelse self.session.data.current_page; 1282 } 1283 1284 if (self.selectedSplitIndex()) |i| { 1285 // swap the detail pane to the selected issue when it changes. 1286 try self.refreshDetail(allocator, i); 1287 1288 // mirror the focused issue into the url, but only while focus is 1289 // inside the split. an issue's url is the same whether or not the 1290 // list is filtered, so the mirror drops the tag. 1291 if (root_focus.grandchild_id) |g| { 1292 if (self.resultsBox(i).getFocus().children.contains(g)) { 1293 if (self.selectedIssueIndex(i)) |sel| { 1294 // selecting another issue leaves the description page 1295 // behind, like it leaves the tag filter behind. 1296 const entry = &self.window(i).issues[sel]; 1297 const mirrored = if (self.data.description_page and std.mem.eql(u8, entry.id, self.data.selected_id)) 1298 ui.RoutablePage.repoIssuesDescriptionRoute(self.data.identity, entry.id) 1299 else if (self.data.comment_page) |page| 1300 if (std.mem.eql(u8, entry.id, self.data.selected_id)) 1301 ui.RoutablePage.repoCommentsRoute(self.data.identity, entry.id, &page.selected.id, page.replies.start) 1302 else 1303 ui.RoutablePage.repoIssueCommentsRoute(self.data.identity, entry.id, 0) 1304 else if (std.mem.eql(u8, entry.id, self.data.selected_id)) 1305 ui.RoutablePage.repoIssueCommentsRoute(self.data.identity, entry.id, entry.comments.start) 1306 else 1307 ui.RoutablePage.repoIssueCommentsRoute(self.data.identity, entry.id, 0); 1308 if (mirrored) |route| self.session.data.current_page = route; 1309 } 1310 } 1311 } 1312 1313 // the selected list row shows a border (the focused TextBox 1314 // upgrades it to a double border itself); the rest stay borderless. 1315 const lb = self.listBox(i); 1316 for (lb.children.keys(), lb.children.values()) |id, *child| { 1317 switch (child.widget) { 1318 .text_box => |*tb| tb.options.border_style = if (lb.getFocus().child_id == id) .single else .hidden, 1319 else => {}, 1320 } 1321 } 1322 1323 // the pane's selected child shows the selection border: the 1324 // description directly, the tags via the flow's selected item. 1325 // cap the list at list_max_width only while the detail pane fits 1326 // beside it. the box drops the detail when the width can't hold 1327 // both minimums, so when it's that narrow we lift the cap and let 1328 // the list fill the whole width. 1329 const both_panes_fit = if (constraint.max_size.width) |w| w >= list_max_width + detail_min_width else true; 1330 self.resultsBox(i).children.values()[list_index].max_size = if (both_panes_fit) .{ .width = list_max_width, .height = null } else null; 1331 1332 // stretch the detail pane across the rest of the width so it fills 1333 // the area rather than shrinking to its content; its scroll fills 1334 // the pane. 1335 if (constraint.max_size.width) |w| { 1336 self.resultsBox(i).children.values()[detail_index].min_size = .{ .width = if (both_panes_fit) w - list_max_width else w, .height = null }; 1337 } else { 1338 self.resultsBox(i).children.values()[detail_index].min_size = .{ .width = detail_min_width, .height = null }; 1339 } 1340 } 1341 1342 // refresh the form inputs' entries in the session's focus-id -> input 1343 // map with this frame's addresses, so the web/wasm form handling can 1344 // find them by focus id 1345 if (self.issueForm()) |form| { 1346 const inputs_arena = self.session.arena.allocator(); 1347 for (form.children.values()) |*child| switch (child.widget) { 1348 .text_input => |*ti| try self.session.text_inputs.put(inputs_arena, ti.getFocus().id, ti), 1349 else => {}, 1350 }; 1351 1352 // on an edit page, register the issue's content as the inputs' 1353 // page-constant initial values, which the web overlay renders 1354 // into them. 1355 if (self.data.view == .edit) { 1356 if (self.data.selectedIssue()) |entry| { 1357 const fields = form.children.values(); 1358 try self.session.input_values.put(inputs_arena, fields[title_field_index].widget.text_input.getFocus().id, entry.issue.event.title); 1359 try self.session.input_values.put(inputs_arena, fields[tags_field_index].widget.text_input.getFocus().id, entry.issue.event.tags); 1360 try self.session.input_values.put(inputs_arena, fields[description_field_index].widget.text_input.getFocus().id, entry.issue.event.description); 1361 } 1362 } 1363 1364 if (self.data.view == .edit_comment) { 1365 if (self.data.comment_page) |page| { 1366 const body = &form.children.values()[comment_body_field_index].widget.text_input; 1367 try self.session.input_values.put(inputs_arena, body.getFocus().id, page.selected.comment.event.body); 1368 } 1369 } 1370 1371 // the resolve form's inputs prefill from the picked side, which 1372 // is url state and so page-constant like the edit values above 1373 if (self.data.view == .resolve) { 1374 for (form.children.values()) |*child| switch (child.widget) { 1375 .text_input => |*ti| if (self.resolvePrefill(ti.options.name)) |value| { 1376 try self.session.input_values.put(inputs_arena, ti.getFocus().id, value); 1377 }, 1378 else => {}, 1379 }; 1380 } 1381 } 1382 1383 try self.box.build(allocator, constraint, root_focus); 1384 } 1385 1386 fn refreshDetail(self: *View, allocator: std.mem.Allocator, index: usize) !void { 1387 const sel = self.selectedIssueIndex(index) orelse return; 1388 if (self.detailed_index[index]) |d| if (d == sel) return; 1389 try self.populateDetail(allocator, index, sel); 1390 self.detailed_index[index] = sel; 1391 } 1392 1393 fn populateDetail(self: *View, allocator: std.mem.Allocator, index: usize, sel: usize) !void { 1394 const entry = self.window(index).issues[sel]; 1395 const inner = self.detailInner(index); 1396 // the /description page replaces its issue's detail with a back link 1397 // and the whole description; other issues keep their normal detail. 1398 const description_page = self.data.description_page and std.mem.eql(u8, entry.id, self.data.selected_id); 1399 const comment_page = if (std.mem.eql(u8, entry.id, self.data.selected_id)) self.data.comment_page else null; 1400 1401 for (inner.children.values()) |*child| child.widget.deinit(allocator); 1402 inner.children.clearAndFree(allocator); 1403 inner.getFocus().child_id = null; 1404 self.author_id[index] = null; 1405 1406 // the tool row: the open/close and edit buttons; the description page 1407 // shows none. 1408 { 1409 const row = self.toolRow(index); 1410 for (row.children.values()) |*child| child.widget.deinit(allocator); 1411 row.children.clearAndFree(allocator); 1412 row.getFocus().child_id = null; 1413 row.getFocus().kind = .container; 1414 } 1415 if (!description_page and comment_page == null) { 1416 const row = self.toolRow(index); 1417 const pa = self.session.page_arena.allocator(); 1418 1419 { 1420 var spacer = try ui.Spacer.init(allocator); 1421 errdefer spacer.deinit(allocator); 1422 try row.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 1423 } 1424 1425 // a conflicted issue's only action is resolving it. the button 1426 // stays visible logged out; the resolve page shows the 1427 // unauthorized view then. 1428 if (entry.conflicted) { 1429 const label = "resolve conflict"; 1430 var button = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1431 errdefer button.deinit(allocator); 1432 button.getFocus().focusable = true; 1433 const route = ui.RoutablePage.repoIssuesResolveRoute(self.data.identity, entry.id, "") orelse return error.RouteTooLong; 1434 button.getFocus().kind = .{ .custom = try std.fmt.allocPrint(pa, "a:{s}", .{try route.toUrl(self.session.page_arena)}) }; 1435 try row.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = .{ .width = label.len + 2, .height = null } }); 1436 } else { 1437 // a logged-out session can't flip the status, so it gets no 1438 // open/close button (and no form action on the row) 1439 if (self.session.data.is_local or self.session.data.user_id != null) { 1440 const action: []const u8 = switch (entry.issue.event.status) { 1441 .open => "close", 1442 .closed => "open", 1443 }; 1444 row.getFocus().kind = .{ .custom = if (self.data.identity.len == 0) 1445 try std.fmt.allocPrint(pa, "form:/issue:{s}/{s}", .{ entry.id, action }) 1446 else 1447 try std.fmt.allocPrint(pa, "form:/repo/{s}/issue:{s}/{s}", .{ self.data.identity, entry.id, action }) }; 1448 1449 var button = try wgt.TextBox(ui.Widget).init(allocator, action, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1450 errdefer button.deinit(allocator); 1451 button.getFocus().focusable = true; 1452 // the renderer distinguishes plain clickables from buttons that 1453 // should POST to a server route by this kind. 1454 button.getFocus().kind = .{ .custom = "submit" }; 1455 try row.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = .{ .width = action.len + 2, .height = null } }); 1456 } 1457 1458 // the edit button links to the issue's edit page. 1459 { 1460 var button = try wgt.TextBox(ui.Widget).init(allocator, "edit", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1461 errdefer button.deinit(allocator); 1462 button.getFocus().focusable = true; 1463 const route = ui.RoutablePage.repoIssuesEditRoute(self.data.identity, entry.id) orelse return error.RouteTooLong; 1464 button.getFocus().kind = .{ .custom = try std.fmt.allocPrint(pa, "a:{s}", .{try route.toUrl(self.session.page_arena)}) }; 1465 try row.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = .{ .width = "edit".len + 2, .height = null } }); 1466 } 1467 } 1468 1469 // the resolve button on a conflicted issue, else the open/close 1470 // button when present, the edit button otherwise 1471 row.getFocus().child_id = row.children.keys()[button_in_row_index]; 1472 } 1473 1474 if (comment_page) |page| { 1475 var back = try Comment.linkBox(allocator, self.session, "← back to issue", ui.RoutablePage.repoIssueCommentsRoute(self.data.identity, entry.id, 0) orelse return error.RouteTooLong); 1476 errdefer back.deinit(allocator); 1477 try inner.children.put(allocator, back.getFocus().id, .{ .widget = .{ .text_box = back }, .rect = null, .min_size = null }); 1478 1479 try Comment.appendComment(allocator, inner, self.session, self.data.identity, page.selected); 1480 try Comment.appendCount(allocator, inner, page.replies.count, "reply", "replies", self.session.page_arena.allocator()); 1481 for (page.replies.comments) |comment| try Comment.appendComment(allocator, inner, self.session, self.data.identity, comment); 1482 try Comment.appendWindowNav(allocator, inner, self.session, self.data.identity, &page.selected.comment.event.thread_id, &page.selected.id, page.replies); 1483 1484 var spacer = try ui.Spacer.init(allocator); 1485 errdefer spacer.deinit(allocator); 1486 try inner.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 1487 1488 inner.getFocus().child_id = inner.children.keys()[0]; 1489 const sc = self.detailScroll(index); 1490 sc.x = 0; 1491 sc.y = 0; 1492 sc.getFocus().version +%= 1; 1493 return; 1494 } 1495 1496 if (description_page) { 1497 // the back link, in the title slot so the pane's input handling 1498 // applies to it unchanged. 1499 var tb = try wgt.TextBox(ui.Widget).init(allocator, "← back to issue", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1500 errdefer tb.deinit(allocator); 1501 tb.getFocus().focusable = true; 1502 tb.getFocus().kind = .{ .custom = try issuesLink(self.session.page_arena, self.data.identity, entry.issue.event.status, "", entry.id) }; 1503 try inner.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1504 } else { 1505 // the issue's title as a focusable word-wrapped text box. 1506 { 1507 var tb = try wgt.TextBox(ui.Widget).init(allocator, entry.issue.event.title, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .word, .label = " title " }); 1508 errdefer tb.deinit(allocator); 1509 tb.getFocus().focusable = true; 1510 try inner.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1511 } 1512 1513 self.author_id[index] = blk: { 1514 var tb = try ui.authorBox(allocator, self.session.page_arena, entry.author); 1515 errdefer tb.deinit(allocator); 1516 try inner.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1517 break :blk tb.getFocus().id; 1518 }; 1519 1520 // the issue's tags, each linking to this status's list filtered to 1521 // that tag. 1522 { 1523 var items: std.ArrayList(ui.TagFlow.Item) = .empty; 1524 defer items.deinit(allocator); 1525 var tag_iter = evt.Issue.tagIterator(entry.issue.event.tags); 1526 while (tag_iter.next()) |tag| { 1527 if (tag.len == 0) continue; 1528 try items.append(allocator, .{ .text = tag, .link = try tagLink(self.session.page_arena, self.data.identity, entry.issue.event.status, tag) }); 1529 } 1530 if (items.items.len > 0) { 1531 var tf = try ui.TagFlow.init(allocator); 1532 errdefer tf.deinit(allocator); 1533 try tf.setItems(allocator, items.items); 1534 try inner.children.put(allocator, tf.getFocus().id, .{ .widget = .{ .tag_flow = tf }, .rect = null, .min_size = null }); 1535 } 1536 } 1537 } 1538 1539 // the description as a focusable word-wrapped text box. past the limit 1540 // it's cut at the last whole line and links to the /description page, 1541 // which shows the whole thing. 1542 self.description_id[index] = blk: { 1543 const whole = entry.issue.event.description; 1544 const cut_short = !description_page and whole.len > max_description_size; 1545 const shown = if (cut_short) 1546 std.mem.trimEnd(u8, whole[0 .. std.mem.lastIndexOfScalar(u8, whole[0..max_description_size], '\n') orelse 0], " \t\r\n") 1547 else if (whole.len == 0) 1548 "(no description)" 1549 else 1550 whole; 1551 var tb = try wgt.TextBox(ui.Widget).init(allocator, shown, .{ 1552 .border_style = .single, 1553 .rounded_corners = true, 1554 .wrap_kind = .word, 1555 .label = " description ", 1556 .bottom_label = if (cut_short) " click or press enter to see more " else "", 1557 }); 1558 errdefer tb.deinit(allocator); 1559 tb.getFocus().focusable = true; 1560 if (cut_short) tb.getFocus().kind = .{ .custom = try descriptionLink(self.session.page_arena, self.data.identity, entry.id) }; 1561 try inner.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1562 break :blk tb.getFocus().id; 1563 }; 1564 1565 if (!description_page) { 1566 var reply = try Comment.linkBox(allocator, self.session, "new comment", ui.RoutablePage.repoCommentNewRoute(self.data.identity, entry.id, "") orelse return error.RouteTooLong); 1567 errdefer reply.deinit(allocator); 1568 try inner.children.put(allocator, reply.getFocus().id, .{ .widget = .{ .text_box = reply }, .rect = null, .min_size = null }); 1569 1570 try Comment.appendCount(allocator, inner, entry.comments.count, "comment", "comments", self.session.page_arena.allocator()); 1571 for (entry.comments.comments) |comment| try Comment.appendComment(allocator, inner, self.session, self.data.identity, comment); 1572 try Comment.appendWindowNav(allocator, inner, self.session, self.data.identity, entry.id, null, entry.comments); 1573 1574 var spacer = try ui.Spacer.init(allocator); 1575 errdefer spacer.deinit(allocator); 1576 try inner.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 1577 } 1578 1579 // select the title by default 1580 inner.getFocus().child_id = inner.children.keys()[title_child_index]; 1581 1582 // reset the scroll to the top for the newly-shown issue: directly on the 1583 // terminal (the wasm offset), and via a version bump on the web (so the 1584 // renderer's scroll id changes and JS drops the preserved position). 1585 const sc = self.detailScroll(index); 1586 sc.x = 0; 1587 sc.y = 0; 1588 sc.getFocus().version +%= 1; 1589 } 1590 1591 pub fn input(self: *View, allocator: std.mem.Allocator, raw_key: Key, root_focus: *Focus) !void { 1592 // wheel input moves focus like the arrows 1593 const key: Key = if (raw_key == .mouse and raw_key.mouse.action == .scroll) 1594 (if (raw_key.mouse.action.scroll == .up) .arrow_up else .arrow_down) 1595 else 1596 raw_key; 1597 1598 if (self.headerActive()) { 1599 // down from the tabs re-enters the stack if the selected view has 1600 // something to focus; other keys move the tabs. 1601 if (inp.vertDirection(key) == .down) { 1602 const enterable = if (self.viewStack().getSelected()) |selected| switch (selected.*) { 1603 .tag_flow => |*tf| tf.text_boxes.items.len > 0, 1604 // nothing focusable in the unauthorized view 1605 .unauthorized => false, 1606 else => true, 1607 } else false; 1608 if (enterable) root_focus.setFocus(self.box.children.keys()[stack_index]); 1609 } else { 1610 try self.header().input(allocator, key, root_focus); 1611 } 1612 return; 1613 } 1614 if (self.tagsViewActive()) { 1615 self.tagsViewInput(key, root_focus); 1616 return; 1617 } 1618 if (self.formViewActive()) { 1619 if (self.data.view == .resolve) { 1620 try self.resolveInput(allocator, key, root_focus); 1621 } else if (self.data.view == .new_comment or self.data.view == .edit_comment) { 1622 try self.commentFormInput(allocator, key, root_focus); 1623 } else { 1624 try self.formInput(allocator, key, root_focus); 1625 } 1626 return; 1627 } 1628 const i = self.selectedSplitIndex() orelse return; 1629 if (self.detailActive(i)) { 1630 try self.detailInput(allocator, i, key, root_focus); 1631 } else { 1632 try self.listInput(i, key, root_focus); 1633 } 1634 } 1635 1636 // arrow keys move the tag selection; up from the top row crosses to the 1637 // header tabs. 1638 fn tagsViewInput(self: *View, key: Key, root_focus: *Focus) void { 1639 const tf = self.tagsView(); 1640 const cid = tf.focus.child_id orelse return; 1641 const cur = tf.indexOfFocusId(cid) orelse return; 1642 const count = tf.text_boxes.items.len; 1643 switch (key) { 1644 .arrow_left => if (cur > 0) root_focus.setFocus(tf.text_boxes.items[cur - 1].getFocus().id), 1645 .arrow_right => if (cur + 1 < count) root_focus.setFocus(tf.text_boxes.items[cur + 1].getFocus().id), 1646 .arrow_up => if (tf.rowStep(cur, false)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id) else self.focusHeader(root_focus), 1647 .arrow_down => if (tf.rowStep(cur, true)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id), 1648 .home => root_focus.setFocus(tf.text_boxes.items[0].getFocus().id), 1649 .end => root_focus.setFocus(tf.text_boxes.items[count - 1].getFocus().id), 1650 else => {}, 1651 } 1652 } 1653 1654 fn listInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 1655 // up/down (and the scroll wheel) move the selection a row; page up/down 1656 // jump a fixed amount. right/Enter cross into the detail pane. up from 1657 // the top row crosses into the header tabs. 1658 if (inp.rowDelta(key, @intCast(self.listBox(index).children.count()))) |delta| { 1659 const lb = self.listBox(index); 1660 const at_top = if (lb.getFocus().child_id) |cid| lb.children.getIndex(cid) == 0 else true; 1661 if (delta < 0 and at_top) return self.focusHeader(root_focus); 1662 ui.moveRowFocus(lb, self.listScroll(index), root_focus, delta); 1663 return; 1664 } 1665 switch (key) { 1666 .enter, .arrow_right => try self.focusDetail(index, root_focus), 1667 else => {}, 1668 } 1669 } 1670 1671 fn detailInput(self: *View, allocator: std.mem.Allocator, index: usize, key: Key, root_focus: *Focus) !void { 1672 switch (key) { 1673 .page_up => { 1674 self.pageDetail(index, root_focus, -10); 1675 return; 1676 }, 1677 .page_down => { 1678 self.pageDetail(index, root_focus, 10); 1679 return; 1680 }, 1681 .home => { 1682 self.jumpDetail(index, root_focus, false); 1683 return; 1684 }, 1685 .end => { 1686 self.jumpDetail(index, root_focus, true); 1687 return; 1688 }, 1689 else => {}, 1690 } 1691 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)) { 1692 try self.commentInput(index, key, root_focus); 1693 } else if (self.toolRowFocused(index)) { 1694 try self.toolRowInput(allocator, index, key, root_focus); 1695 } else if (self.titleFocused(index)) { 1696 try self.titleInput(index, key, root_focus); 1697 } else if (self.authorFocused(index)) { 1698 try self.authorInput(index, key, root_focus); 1699 } else if (self.tagsFocused(index)) { 1700 try self.tagsInput(index, key, root_focus); 1701 } else if (self.descriptionFocused(index)) { 1702 try self.descriptionInput(index, key, root_focus); 1703 } else { 1704 try self.commentInput(index, key, root_focus); 1705 } 1706 } 1707 1708 // the tool row: enter or a click on the open/close button flips the 1709 // issue's status; the edit and resolve buttons are a: links the host 1710 // follows. arrows cross between the buttons and to the neighboring 1711 // widgets. 1712 fn toolRowInput(self: *View, allocator: std.mem.Allocator, index: usize, key: Key, root_focus: *Focus) !void { 1713 const row = self.toolRow(index); 1714 const cur = if (row.getFocus().child_id) |cid| row.children.getIndex(cid) orelse return else return; 1715 const on_status = cur == button_in_row_index and self.statusButton(index) != null; 1716 switch (key) { 1717 .arrow_left => if (cur > button_in_row_index) root_focus.setFocus(row.children.keys()[cur - 1]) else try self.focusList(index, root_focus), 1718 .arrow_right => if (cur + 1 < row.children.count()) root_focus.setFocus(row.children.keys()[cur + 1]), 1719 .arrow_up => self.focusHeader(root_focus), 1720 .arrow_down => { 1721 self.focusDetailEdge(index, root_focus, false); 1722 self.detailScroll(index).y = 0; 1723 }, 1724 .enter => if (on_status) try self.toggleIssueStatus(allocator, index), 1725 .mouse => |mouse| if (self.statusButton(index)) |button| { 1726 if (inp.leftClickOn(root_focus, button.getFocus().id, mouse)) try self.toggleIssueStatus(allocator, index); 1727 }, 1728 else => {}, 1729 } 1730 } 1731 1732 fn titleInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 1733 switch (key) { 1734 .arrow_left => try self.focusList(index, root_focus), 1735 .arrow_up => self.focusToolRow(index, root_focus), 1736 .arrow_down => _ = self.moveDetailVertical(index, root_focus, true), 1737 else => {}, 1738 } 1739 } 1740 1741 // the author box's a: link (when it has one) is followed by the host on 1742 // enter / a click; arrows cross to the neighboring widgets. 1743 fn authorInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 1744 switch (key) { 1745 .arrow_left => try self.focusList(index, root_focus), 1746 .arrow_up => _ = self.moveDetailVertical(index, root_focus, false), 1747 .arrow_down => _ = self.moveDetailVertical(index, root_focus, true), 1748 else => {}, 1749 } 1750 } 1751 1752 fn descriptionInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 1753 const sc = self.detailScroll(index); 1754 switch (key) { 1755 .arrow_left => return self.focusList(index, root_focus), 1756 // the terminal moves through the description one line at a time; 1757 // the web lets focus changes drive its native scroll. 1758 .arrow_up => { 1759 if (!self.session.is_terminal) { 1760 _ = self.moveDetailVertical(index, root_focus, false); 1761 return; 1762 } 1763 if (self.focusAdjacentVisible(index, root_focus, false)) return; 1764 const before = sc.y; 1765 sc.y -= 1; 1766 sc.clampToContent(); 1767 if (sc.y == before) _ = self.moveDetailVertical(index, root_focus, false); 1768 return; 1769 }, 1770 .arrow_down => { 1771 if (!self.session.is_terminal) { 1772 _ = self.moveDetailVertical(index, root_focus, true); 1773 return; 1774 } 1775 if (self.focusAdjacentVisible(index, root_focus, true)) return; 1776 const before = sc.y; 1777 sc.y += 1; 1778 sc.clampToContent(); 1779 if (sc.y == before) _ = self.moveDetailVertical(index, root_focus, true); 1780 return; 1781 }, 1782 else => return, 1783 } 1784 sc.clampToContent(); 1785 } 1786 1787 fn commentInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 1788 switch (key) { 1789 .arrow_left => if (!self.moveCommentHorizontal(index, root_focus, false)) return self.focusList(index, root_focus), 1790 .arrow_right => _ = self.moveCommentHorizontal(index, root_focus, true), 1791 .arrow_up => _ = self.moveDetailVertical(index, root_focus, false), 1792 .arrow_down => _ = self.moveDetailVertical(index, root_focus, true), 1793 else => return, 1794 } 1795 } 1796 1797 // page the detail scroll, then focus the first or last visible widget. 1798 fn pageDetail(self: *View, index: usize, root_focus: *Focus, delta: isize) void { 1799 if (!self.session.is_terminal) return; 1800 const sc = self.detailScroll(index); 1801 sc.y += delta; 1802 sc.clampToContent(); 1803 self.focusVisible(index, root_focus, delta > 0); 1804 } 1805 1806 // jump to the detail's first or last focusable widget. the terminal pins 1807 // its synthetic scroll too; the browser scrolls to the new focus itself. 1808 fn jumpDetail(self: *View, index: usize, root_focus: *Focus, to_end: bool) void { 1809 if (self.session.is_terminal) { 1810 const sc = self.detailScroll(index); 1811 sc.y = if (to_end) std.math.maxInt(isize) else 0; 1812 sc.clampToContent(); 1813 self.focusVisible(index, root_focus, to_end); 1814 } else { 1815 self.focusDetailEdge(index, root_focus, to_end); 1816 } 1817 } 1818 1819 fn focusDetailEdge(self: *View, index: usize, root_focus: *Focus, last: bool) void { 1820 const focus = self.detailInner(index).getFocus(); 1821 var chosen: ?usize = null; 1822 for (focus.children.keys(), focus.children.values()) |id, child| { 1823 if (!child.focus.focusable) continue; 1824 chosen = id; 1825 if (!last) break; 1826 } 1827 if (chosen) |id| root_focus.setFocus(id); 1828 } 1829 1830 fn detailRectVisible(self: *View, index: usize, rect: layout.IRect) bool { 1831 const sc = self.detailScroll(index); 1832 const viewport = sc.grid orelse return false; 1833 const top = sc.y; 1834 const bottom = top + @as(isize, @intCast(viewport.size.height - sc.bar_h)); 1835 return rect.y + @as(isize, @intCast(rect.size.height)) > top and rect.y < bottom; 1836 } 1837 1838 // focus the top- or bottom-most focusable widget intersecting the viewport. 1839 fn focusVisible(self: *View, index: usize, root_focus: *Focus, last: bool) void { 1840 const inner = self.detailInner(index); 1841 var offset: usize = 0; 1842 while (offset < inner.children.count()) : (offset += 1) { 1843 const child_index = if (last) inner.children.count() - 1 - offset else offset; 1844 if (self.focusVisibleChild(index, child_index, root_focus, last)) return; 1845 } 1846 } 1847 1848 fn focusVisibleChild(self: *View, index: usize, child_index: usize, root_focus: *Focus, last: bool) bool { 1849 const child = &self.detailInner(index).children.values()[child_index]; 1850 const child_rect = child.rect orelse return false; 1851 var chosen: ?usize = null; 1852 if (child.widget.getFocus().focusable and self.detailRectVisible(index, child_rect)) { 1853 const id = self.detailInner(index).children.keys()[child_index]; 1854 chosen = id; 1855 if (!last) { 1856 root_focus.setFocus(id); 1857 return true; 1858 } 1859 } 1860 1861 for (child.widget.getFocus().children.keys(), child.widget.getFocus().children.values()) |id, focus_child| { 1862 if (!focus_child.focus.focusable) continue; 1863 const rect: layout.IRect = .{ 1864 .x = child_rect.x + @as(isize, @intCast(focus_child.rect.x)), 1865 .y = child_rect.y + @as(isize, @intCast(focus_child.rect.y)), 1866 .size = focus_child.rect.size, 1867 }; 1868 if (!self.detailRectVisible(index, rect)) continue; 1869 chosen = id; 1870 if (!last) break; 1871 } 1872 if (chosen) |id| { 1873 root_focus.setFocus(id); 1874 return true; 1875 } 1876 return false; 1877 } 1878 1879 fn focusAdjacentVisible(self: *View, index: usize, root_focus: *Focus, down: bool) bool { 1880 const inner = self.detailInner(index); 1881 var next = self.focusedDetailChild(index, root_focus) orelse return false; 1882 while (true) { 1883 if (down) { 1884 next += 1; 1885 if (next >= inner.children.count()) return false; 1886 } else { 1887 if (next == 0) return false; 1888 next -= 1; 1889 } 1890 if (self.focusVisibleChild(index, next, root_focus, !down)) { 1891 self.focusDetailChild(index, next, !down, root_focus); 1892 return true; 1893 } 1894 } 1895 } 1896 1897 fn focusedDetailChild(self: *View, index: usize, root_focus: *Focus) ?usize { 1898 const inner = self.detailInner(index); 1899 var id = root_focus.grandchild_id orelse return null; 1900 while (inner.getFocus().children.get(id)) |child| { 1901 if (child.parent_id == inner.getFocus().id) return inner.children.getIndex(id); 1902 id = child.parent_id; 1903 } 1904 return null; 1905 } 1906 1907 fn detailChildFocusable(self: *View, index: usize, child_index: usize) bool { 1908 const child = &self.detailInner(index).children.values()[child_index].widget; 1909 const focus = child.getFocus(); 1910 if (focus.focusable) return true; 1911 for (focus.children.values()) |item| { 1912 if (item.focus.focusable) return true; 1913 } 1914 return false; 1915 } 1916 1917 fn focusDetailChild(self: *View, index: usize, child_index: usize, last: bool, root_focus: *Focus) void { 1918 const inner = self.detailInner(index); 1919 const child = &inner.children.values()[child_index]; 1920 var target = inner.children.keys()[child_index]; 1921 var rect = child.rect orelse return; 1922 switch (child.widget) { 1923 .repo_comment => |*comment| { 1924 if (comment.rowRect(last)) |row_rect| { 1925 rect.x += row_rect.x; 1926 rect.y += row_rect.y; 1927 rect.size = row_rect.size; 1928 } 1929 if (last) comment.focusBody(root_focus) else comment.focusMetadata(root_focus); 1930 }, 1931 .box => |*box| if (box.children.count() > 0) { 1932 target = box.children.keys()[if (last) box.children.count() - 1 else 0]; 1933 root_focus.setFocus(target); 1934 }, 1935 else => root_focus.setFocus(target), 1936 } 1937 const sc = self.detailScroll(index); 1938 if (sc.grid) |viewport| { 1939 const viewport_height = viewport.size.height - sc.bar_h; 1940 if (rect.size.height > viewport_height) { 1941 const bottom_aligned = rect.y + @as(isize, @intCast(rect.size.height - viewport_height)); 1942 sc.y = std.math.clamp(sc.y, rect.y, bottom_aligned); 1943 sc.clampToContent(); 1944 return; 1945 } 1946 } 1947 sc.scrollToRect(rect); 1948 } 1949 1950 fn moveDetailVertical(self: *View, index: usize, root_focus: *Focus, down: bool) bool { 1951 const inner = self.detailInner(index); 1952 const current = self.focusedDetailChild(index, root_focus) orelse return false; 1953 if (inner.children.values()[current].widget == .repo_comment) { 1954 const comment = &inner.children.values()[current].widget.repo_comment; 1955 if (down and !comment.bodyFocused()) { 1956 self.focusDetailChild(index, current, true, root_focus); 1957 return true; 1958 } 1959 if (!down and comment.bodyFocused()) { 1960 self.focusDetailChild(index, current, false, root_focus); 1961 return true; 1962 } 1963 } 1964 1965 var next = current; 1966 while (true) { 1967 if (down) { 1968 next += 1; 1969 if (next >= inner.children.count()) return false; 1970 } else { 1971 if (next == 0) return false; 1972 next -= 1; 1973 } 1974 if (!self.detailChildFocusable(index, next)) continue; 1975 self.focusDetailChild(index, next, !down, root_focus); 1976 return true; 1977 } 1978 } 1979 1980 fn moveCommentHorizontal(self: *View, index: usize, root_focus: *Focus, right: bool) bool { 1981 const inner = self.detailInner(index); 1982 const current = self.focusedDetailChild(index, root_focus) orelse return false; 1983 const child = &inner.children.values()[current]; 1984 if (child.widget == .repo_comment) { 1985 const moved = child.widget.repo_comment.moveHorizontal(root_focus, right); 1986 if (moved) if (child.rect) |rect| self.detailScroll(index).scrollToRect(rect); 1987 return moved; 1988 } 1989 const row = switch (child.widget) { 1990 .box => |*box| box, 1991 else => return false, 1992 }; 1993 const selected = row.getFocus().child_id orelse return false; 1994 const selected_index = row.children.getIndex(selected) orelse return false; 1995 const target = if (right) selected_index + 1 else selected_index -| 1; 1996 if (target == selected_index or target >= row.children.count()) return false; 1997 root_focus.setFocus(row.children.keys()[target]); 1998 if (child.rect) |rect| self.detailScroll(index).scrollToRect(rect); 1999 return true; 2000 } 2001 2002 // up/down (and tab/shift+tab) move between a form's fields; up from the 2003 // title crosses into the header tabs. the multiline description keeps 2004 // enter and any up/down that has a row to move to. 2005 fn formInput(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2006 const form = self.issueForm() orelse return; 2007 const cid = form.getFocus().child_id orelse return; 2008 const cur = form.children.getIndex(cid) orelse return; 2009 2010 if (cur == description_field_index) { 2011 const child = &form.children.values()[cur]; 2012 if (try multilineKeepsKey(&child.widget.text_input, allocator, key)) 2013 return child.widget.input(allocator, key, root_focus); 2014 } 2015 2016 switch (key) { 2017 .arrow_up, .back_tab => if (cur > 0) 2018 root_focus.setFocus(form.children.keys()[cur - 1]) 2019 else 2020 self.focusHeader(root_focus), 2021 .arrow_down, .tab => if (cur < submit_field_index) { 2022 root_focus.setFocus(form.children.keys()[cur + 1]); 2023 }, 2024 .enter => if (cur == submit_field_index) try self.submitForm(allocator), 2025 .mouse => |mouse| if (cur == submit_field_index) { 2026 const submit = &form.children.values()[cur].widget.submit_button; 2027 if (inp.leftClickOn(root_focus, submit.buttonId(), mouse)) try self.submitForm(allocator); 2028 }, 2029 else => try form.children.values()[cur].widget.input(allocator, key, root_focus), 2030 } 2031 } 2032 2033 fn commentFormInput(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2034 const form = self.issueForm() orelse return; 2035 const cid = form.getFocus().child_id orelse return; 2036 const cur = form.children.getIndex(cid) orelse return; 2037 const child = &form.children.values()[cur]; 2038 2039 if (cur == comment_body_field_index and try multilineKeepsKey(&child.widget.text_input, allocator, key)) 2040 return child.widget.input(allocator, key, root_focus); 2041 2042 switch (key) { 2043 .arrow_up, .back_tab => if (cur > comment_author_field_index) 2044 root_focus.setFocus(form.children.keys()[cur - 1]) 2045 else 2046 self.focusHeader(root_focus), 2047 .arrow_down, .tab => if (cur < comment_submit_field_index) 2048 root_focus.setFocus(form.children.keys()[cur + 1]), 2049 .enter => if (cur == comment_submit_field_index) try self.submitComment(allocator), 2050 .mouse => |mouse| if (cur == comment_submit_field_index) { 2051 const submit = &child.widget.submit_button; 2052 if (inp.leftClickOn(root_focus, submit.buttonId(), mouse)) try self.submitComment(allocator); 2053 }, 2054 else => try child.widget.input(allocator, key, root_focus), 2055 } 2056 } 2057 2058 // the multiline hunk inputs keep enter and any up/down that has a row to 2059 // move to; enter on a "use this" link is a navigation the host follows 2060 // before this runs. 2061 fn resolveInput(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2062 const form = self.issueForm() orelse return; 2063 const cid = form.getFocus().child_id orelse return; 2064 const cur = form.children.getIndex(cid) orelse return; 2065 const child = &form.children.values()[cur]; 2066 2067 if (child.widget == .text_input) { 2068 const ti = &child.widget.text_input; 2069 if (ti.options.multiline and try multilineKeepsKey(ti, allocator, key)) 2070 return child.widget.input(allocator, key, root_focus); 2071 } 2072 2073 const on_submit = child.widget == .submit_button; 2074 2075 switch (key) { 2076 .arrow_up, .back_tab => if (resolveStep(form, cur, false)) |i| self.focusResolveChild(form, i, root_focus) else self.focusHeader(root_focus), 2077 .arrow_down, .tab => if (resolveStep(form, cur, true)) |i| self.focusResolveChild(form, i, root_focus), 2078 .arrow_left, .arrow_right => switch (child.widget) { 2079 .box => |*row| { 2080 const row_cid = row.getFocus().child_id orelse return; 2081 const row_index = row.children.getIndex(row_cid) orelse return; 2082 if (key == .arrow_left) { 2083 if (row_index > 0) root_focus.setFocus(row.children.keys()[row_index - 1]); 2084 } else if (row_index + 1 < row.children.count()) { 2085 root_focus.setFocus(row.children.keys()[row_index + 1]); 2086 } 2087 }, 2088 else => try child.widget.input(allocator, key, root_focus), 2089 }, 2090 .enter => if (on_submit) try self.submitResolution(allocator), 2091 .mouse => |mouse| if (on_submit) { 2092 if (inp.leftClickOn(root_focus, child.widget.submit_button.buttonId(), mouse)) try self.submitResolution(allocator); 2093 }, 2094 else => try child.widget.input(allocator, key, root_focus), 2095 } 2096 } 2097 2098 // a multiline input keeps enter and any up/down that has a row to move to 2099 fn multilineKeepsKey(text_input: *wgt.TextInput(ui.Widget), allocator: std.mem.Allocator, key: Key) !bool { 2100 return switch (key) { 2101 .enter => true, 2102 .arrow_up => !try text_input.cursorOnFirstRow(allocator), 2103 .arrow_down => !try text_input.cursorOnLastRow(allocator), 2104 else => false, 2105 }; 2106 } 2107 2108 // the neighboring focusable resolve-form child, skipping the spacer and 2109 // the blank gap rows 2110 fn resolveStep(form: *wgt.Box(ui.Widget), cur: usize, down: bool) ?usize { 2111 var i = cur; 2112 while (true) { 2113 if (down) { 2114 i += 1; 2115 if (i >= form.children.count()) return null; 2116 } else { 2117 if (i == 0) return null; 2118 i -= 1; 2119 } 2120 switch (form.children.values()[i].widget) { 2121 .spacer, .text => continue, 2122 else => return i, 2123 } 2124 } 2125 } 2126 2127 // focus the resolve-form child at `i` (a version row focuses its selected 2128 // child) and keep it visible on the terminal 2129 fn focusResolveChild(self: *View, form: *wgt.Box(ui.Widget), i: usize, root_focus: *Focus) void { 2130 const child = &form.children.values()[i]; 2131 switch (child.widget) { 2132 .box => |*row| root_focus.setFocus(row.getFocus().child_id orelse form.children.keys()[i]), 2133 else => root_focus.setFocus(form.children.keys()[i]), 2134 } 2135 if (self.session.is_terminal) { 2136 if (self.resolveScroll()) |sc| { 2137 if (child.rect) |rect| sc.scrollToRect(rect); 2138 } 2139 } 2140 } 2141 2142 // re-emit the conflicted issue's event with the resolved content; any 2143 // event on the issue settles its conflict entry. this is the terminal 2144 // path; the web posts the form to the resolve route. 2145 fn submitResolution(self: *View, allocator: std.mem.Allocator) !void { 2146 if (comptime wasm) return; 2147 const io = self.session.io orelse return; 2148 const src = self.data.repo_source orelse return; 2149 const entry = self.data.selectedIssue() orelse return; 2150 const author_email = (try self.session.eventAuthorEmail()) orelse return; 2151 const form = self.issueForm() orelse return; 2152 2153 // gather the inputs by name; the d<n> hunk inputs appear in chunk order 2154 var title: ?[]u8 = null; 2155 var tags: ?[]u8 = null; 2156 var hunks: std.ArrayList([]const u8) = .empty; 2157 defer { 2158 if (title) |t| allocator.free(t); 2159 if (tags) |t| allocator.free(t); 2160 for (hunks.items) |h| allocator.free(h); 2161 hunks.deinit(allocator); 2162 } 2163 for (form.children.values()) |*child| switch (child.widget) { 2164 .text_input => |*ti| { 2165 const text = try ti.text(allocator); 2166 errdefer allocator.free(text); 2167 if (std.mem.eql(u8, ti.options.name, "title")) { 2168 title = text; 2169 } else if (std.mem.eql(u8, ti.options.name, "tags")) { 2170 tags = text; 2171 } else { 2172 try hunks.append(allocator, text); 2173 } 2174 }, 2175 else => {}, 2176 }; 2177 2178 var id_bytes: [evt.event_id_size]u8 = undefined; 2179 _ = try std.fmt.hexToBytes(&id_bytes, entry.id[0 .. evt.event_id_size * 2]); 2180 2181 switch (src.repo_kind) { 2182 inline else => |repo_kind| { 2183 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, .{ .path = src.path }); 2184 defer any_repo.deinit(io, allocator); 2185 switch (any_repo) { 2186 inline else => |*repo| evt.Issue.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .resolve = .{ 2187 .title = title, 2188 .tags = tags, 2189 .hunks = hunks.items, 2190 } }, author_email) catch |err| switch (err) { 2191 // leave the form up for correction 2192 error.InvalidFields => return, 2193 else => |e| return e, 2194 }, 2195 } 2196 }, 2197 } 2198 2199 const route = ui.RoutablePage.repoIssuesRoute(self.data.identity, entry.issue.event.status, "", entry.id) orelse return; 2200 try self.session.navigate(route); 2201 } 2202 2203 // commit a new or edited comment and navigate to its permalink. this is the 2204 // terminal path; the web posts the form to its comment route. 2205 fn submitComment(self: *View, allocator: std.mem.Allocator) !void { 2206 if (comptime wasm) return; 2207 const io = self.session.io orelse return; 2208 const src = self.data.repo_source orelse return; 2209 const author_email = (try self.session.eventAuthorEmail()) orelse return; 2210 const form = self.issueForm() orelse return; 2211 const body_input = &form.children.values()[comment_body_field_index].widget.text_input; 2212 const body = try body_input.text(allocator); 2213 defer allocator.free(body); 2214 if (!evt.Comment.fieldsValid(body)) return; 2215 2216 var thread_id: [evt.event_id_size * 2]u8 = undefined; 2217 @memcpy(&thread_id, self.data.selected_id); 2218 const parent_id = if (self.data.comment_id.len == 0) thread_id else blk: { 2219 var parent: [evt.event_id_size * 2]u8 = undefined; 2220 @memcpy(&parent, self.data.comment_id); 2221 break :blk parent; 2222 }; 2223 var event_id_hex: [evt.event_id_size * 2]u8 = undefined; 2224 const editing = self.data.view == .edit_comment; 2225 switch (src.repo_kind) { 2226 inline else => |repo_kind| { 2227 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, .{ .path = src.path }); 2228 defer any_repo.deinit(io, allocator); 2229 switch (any_repo) { 2230 inline else => |*repo| if (editing) { 2231 var comment_id: [evt.event_id_size]u8 = undefined; 2232 _ = std.fmt.hexToBytes(&comment_id, self.data.comment_id) catch return; 2233 try evt.Comment.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &thread_id, &comment_id, body, author_email); 2234 event_id_hex = std.fmt.bytesToHex(comment_id, .lower); 2235 } else { 2236 event_id_hex = try evt.Comment.create(repo_kind, repo.self_repo_opts, io, allocator, repo, &thread_id, &parent_id, body, author_email); 2237 }, 2238 } 2239 }, 2240 } 2241 2242 body_input.clear(allocator); 2243 const route = ui.RoutablePage.repoCommentsRoute(self.data.identity, &thread_id, &event_id_hex, 0) orelse return; 2244 try self.session.navigate(route); 2245 } 2246 2247 // the terminal submit paths: the new form commits a new-issue event, the 2248 // edit form re-emits the selected issue's event with the form's content. 2249 // the web posts the forms to their routes instead. 2250 fn submitForm(self: *View, allocator: std.mem.Allocator) !void { 2251 if (self.data.view == .edit) { 2252 try self.submitEditedIssue(allocator); 2253 } else { 2254 try self.submitNewIssue(allocator); 2255 } 2256 } 2257 2258 // commit the new issue to the repo's events branch and navigate to it. 2259 // this is the terminal path; the web posts the form to the issue route, 2260 // so the wasm side never runs (or compiles) the repo access below. 2261 fn submitNewIssue(self: *View, allocator: std.mem.Allocator) !void { 2262 if (comptime wasm) return; 2263 const io = self.session.io orelse return; 2264 const src = self.data.repo_source orelse return; 2265 const author_email = (try self.session.eventAuthorEmail()) orelse return; 2266 2267 const form = self.issueForm() orelse return; 2268 const title_input = &form.children.values()[title_field_index].widget.text_input; 2269 const tags_input = &form.children.values()[tags_field_index].widget.text_input; 2270 const description_input = &form.children.values()[description_field_index].widget.text_input; 2271 2272 const title = try title_input.text(allocator); 2273 defer allocator.free(title); 2274 const tags = try tags_input.text(allocator); 2275 defer allocator.free(tags); 2276 const description = try description_input.text(allocator); 2277 defer allocator.free(description); 2278 2279 if (!evt.Issue.fieldsValid(title, tags)) return; 2280 2281 var id_bytes: [evt.event_id_size]u8 = undefined; 2282 io.random(&id_bytes); 2283 const event_id_hex = std.fmt.bytesToHex(id_bytes, .lower); 2284 2285 const event = evt.EventWithId{ 2286 .id = event_id_hex, 2287 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 2288 .author_email = author_email, 2289 .event = .{ .issue = .{ 2290 .title = title, 2291 .description = description, 2292 .tags = tags, 2293 } }, 2294 }; 2295 2296 switch (src.repo_kind) { 2297 inline else => |repo_kind| { 2298 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, .{ .path = src.path }); 2299 defer any_repo.deinit(io, allocator); 2300 switch (any_repo) { 2301 inline else => |*repo| try evt.consume(repo_kind, repo.self_repo_opts, io, allocator, repo, evt.events_ref, &.{event}), 2302 } 2303 }, 2304 } 2305 2306 // wipe the form so a return visit starts fresh 2307 title_input.clear(allocator); 2308 tags_input.clear(allocator); 2309 description_input.clear(allocator); 2310 2311 const route = ui.RoutablePage.repoIssuesRoute(self.data.identity, .open, "", &event_id_hex) orelse return; 2312 try self.session.navigate(route); 2313 } 2314 2315 // re-emit the selected issue's event with the form's content (status 2316 // preserved), then reload the issue's page. this is the terminal path; 2317 // the web posts the form to the edit route. 2318 fn submitEditedIssue(self: *View, allocator: std.mem.Allocator) !void { 2319 if (comptime wasm) return; 2320 const io = self.session.io orelse return; 2321 const src = self.data.repo_source orelse return; 2322 const entry = self.data.selectedIssue() orelse return; 2323 const author_email = (try self.session.eventAuthorEmail()) orelse return; 2324 2325 const form = self.issueForm() orelse return; 2326 const title_input = &form.children.values()[title_field_index].widget.text_input; 2327 const tags_input = &form.children.values()[tags_field_index].widget.text_input; 2328 const description_input = &form.children.values()[description_field_index].widget.text_input; 2329 2330 const title = try title_input.text(allocator); 2331 defer allocator.free(title); 2332 const tags = try tags_input.text(allocator); 2333 defer allocator.free(tags); 2334 const description = try description_input.text(allocator); 2335 defer allocator.free(description); 2336 2337 if (!evt.Issue.fieldsValid(title, tags)) return; 2338 2339 var id_bytes: [evt.event_id_size]u8 = undefined; 2340 _ = try std.fmt.hexToBytes(&id_bytes, entry.id[0 .. evt.event_id_size * 2]); 2341 2342 switch (src.repo_kind) { 2343 inline else => |repo_kind| { 2344 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, .{ .path = src.path }); 2345 defer any_repo.deinit(io, allocator); 2346 switch (any_repo) { 2347 inline else => |*repo| try evt.Issue.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 2348 .title = title, 2349 .tags = tags, 2350 .description = description, 2351 } }, author_email), 2352 } 2353 }, 2354 } 2355 2356 const route = ui.RoutablePage.repoIssuesRoute(self.data.identity, entry.issue.event.status, "", entry.id) orelse return; 2357 try self.session.navigate(route); 2358 } 2359 2360 // flip the shown issue's status by re-emitting its event, then reload the 2361 // page rooted at the issue so the view reflects the change. this is the 2362 // terminal path; the web posts the button's form to the status route. 2363 fn toggleIssueStatus(self: *View, allocator: std.mem.Allocator, index: usize) !void { 2364 if (comptime wasm) return; 2365 const io = self.session.io orelse return; 2366 const src = self.data.repo_source orelse return; 2367 const sel = self.detailed_index[index] orelse return; 2368 const entry = self.window(index).issues[sel]; 2369 const author_email = (try self.session.eventAuthorEmail()) orelse return; 2370 2371 const status: evt.Issue.Status = switch (entry.issue.event.status) { 2372 .open => .closed, 2373 .closed => .open, 2374 }; 2375 2376 var id_bytes: [evt.event_id_size]u8 = undefined; 2377 _ = try std.fmt.hexToBytes(&id_bytes, entry.id[0 .. evt.event_id_size * 2]); 2378 2379 switch (src.repo_kind) { 2380 inline else => |repo_kind| { 2381 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, .{ .path = src.path }); 2382 defer any_repo.deinit(io, allocator); 2383 switch (any_repo) { 2384 inline else => |*repo| try evt.Issue.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .status = status }, author_email), 2385 } 2386 }, 2387 } 2388 2389 const route = ui.RoutablePage.repoIssuesRoute(self.data.identity, status, "", entry.id) orelse return; 2390 try self.session.navigate(route); 2391 } 2392 2393 // arrow keys move the tag selection; at the flow's edges focus crosses to 2394 // the neighboring widgets. 2395 fn tagsInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 2396 const tf = self.tagFlow(index) orelse return; 2397 const cid = tf.focus.child_id orelse return; 2398 const cur = tf.indexOfFocusId(cid) orelse return; 2399 const count = tf.text_boxes.items.len; 2400 switch (key) { 2401 .arrow_left => if (cur > 0) self.focusTag(index, tf, root_focus, cur - 1) else try self.focusList(index, root_focus), 2402 .arrow_right => if (cur + 1 < count) self.focusTag(index, tf, root_focus, cur + 1), 2403 .arrow_up => if (tf.rowStep(cur, false)) |i| { 2404 self.focusTag(index, tf, root_focus, i); 2405 } else { 2406 _ = self.moveDetailVertical(index, root_focus, false); 2407 }, 2408 .arrow_down => if (tf.rowStep(cur, true)) |i| { 2409 self.focusTag(index, tf, root_focus, i); 2410 } else { 2411 _ = self.moveDetailVertical(index, root_focus, true); 2412 }, 2413 .home => self.focusTag(index, tf, root_focus, 0), 2414 .end => self.focusTag(index, tf, root_focus, count - 1), 2415 else => {}, 2416 } 2417 } 2418 2419 const button_in_row_index: usize = 1; 2420 const title_child_index: usize = 0; 2421 const tags_child_index: usize = 2; 2422 2423 fn tagFlow(self: *View, index: usize) ?*ui.TagFlow { 2424 const inner = self.detailInner(index); 2425 if (inner.children.count() <= tags_child_index) return null; 2426 return switch (inner.children.values()[tags_child_index].widget) { 2427 .tag_flow => |*tf| tf, 2428 else => null, 2429 }; 2430 } 2431 2432 fn tagsFocused(self: *View, index: usize) bool { 2433 const inner = self.detailInner(index); 2434 const cid = inner.getFocus().child_id orelse return false; 2435 return inner.children.getIndex(cid) == tags_child_index and self.tagFlow(index) != null; 2436 } 2437 2438 // the open/close button inside the detail frame's tool row, identified 2439 // by its posting kind; a logged-out session's or conflicted issue's row 2440 // has none. 2441 fn statusButton(self: *View, index: usize) ?*wgt.TextBox(ui.Widget) { 2442 const row = self.toolRow(index); 2443 if (row.children.count() <= button_in_row_index) return null; 2444 switch (row.children.values()[button_in_row_index].widget) { 2445 .text_box => |*tb| switch (tb.box.focus.kind) { 2446 .custom => |custom| if (std.mem.eql(u8, custom, "submit")) return tb, 2447 else => {}, 2448 }, 2449 else => {}, 2450 } 2451 return null; 2452 } 2453 2454 fn toolRowFocused(self: *View, index: usize) bool { 2455 const outer = self.detailOuter(index); 2456 const cid = outer.getFocus().child_id orelse return false; 2457 return outer.children.getIndex(cid) == tool_row_index; 2458 } 2459 2460 fn focusTag(self: *View, index: usize, tf: *ui.TagFlow, root_focus: *Focus, item: usize) void { 2461 root_focus.setFocus(tf.text_boxes.items[item].getFocus().id); 2462 // keep the tag visible on the terminal: its rect offset by the flow's 2463 // position in the pane. 2464 if (self.session.is_terminal and item < tf.rects.items.len) { 2465 if (self.detailInner(index).children.values()[tags_child_index].rect) |flow_rect| { 2466 var rect = tf.rects.items[item]; 2467 rect.x += flow_rect.x; 2468 rect.y += flow_rect.y; 2469 self.detailScroll(index).scrollToRect(rect); 2470 } 2471 } 2472 } 2473 2474 fn titleFocused(self: *View, index: usize) bool { 2475 const inner = self.detailInner(index); 2476 const cid = inner.getFocus().child_id orelse return false; 2477 return inner.children.getIndex(cid) == title_child_index; 2478 } 2479 2480 fn authorFocused(self: *View, index: usize) bool { 2481 const inner = self.detailInner(index); 2482 const cid = inner.getFocus().child_id orelse return false; 2483 return cid == self.author_id[index]; 2484 } 2485 2486 fn descriptionFocused(self: *View, index: usize) bool { 2487 const cid = self.detailInner(index).getFocus().child_id orelse return false; 2488 return cid == self.description_id[index]; 2489 } 2490 2491 // return to the tool row's last-focused button (the header when the 2492 // description page shows no row). 2493 fn focusToolRow(self: *View, index: usize, root_focus: *Focus) void { 2494 const cid = self.toolRow(index).getFocus().child_id orelse return self.focusHeader(root_focus); 2495 root_focus.setFocus(cid); 2496 } 2497 2498 // enter the detail pane. an empty pane (no issues) can't be entered. 2499 fn focusDetail(self: *View, index: usize, root_focus: *Focus) !void { 2500 if (self.detailInner(index).children.count() == 0) return; 2501 root_focus.setFocus(self.detailOuter(index).getFocus().id); 2502 } 2503 2504 // return to the list. 2505 fn focusList(self: *View, index: usize, root_focus: *Focus) !void { 2506 root_focus.setFocus(self.listScroll(index).getFocus().id); 2507 } 2508 2509 // cross to the header tabs above the stack. 2510 fn focusHeader(self: *View, root_focus: *Focus) void { 2511 root_focus.setFocus(self.box.children.keys()[header_index]); 2512 } 2513 2514 pub fn clearGrid(self: *View) void { 2515 self.box.clearGrid(); 2516 } 2517 2518 pub fn getGrid(self: View) ?Grid { 2519 return self.box.getGrid(); 2520 } 2521 2522 pub fn getFocus(self: *View) *Focus { 2523 return self.box.getFocus(); 2524 } 2525 2526 // for the parent's "scroll up at the top jumps to the header" check: at the 2527 // top only while the header tabs hold focus, so up from the split 2528 // crosses into the tabs first. 2529 pub fn getSelectedIndex(self: *View) ?usize { 2530 return if (self.headerActive()) 0 else 1; 2531 } 2532 }; 2533 2534 // the "a:" navigation link for `status`'s issues page filtered to the 2535 // url-encoded `tag` and rooted at issue `id` within `identity` ("owner/name"). 2536 // a rooted url ignores `status` (it derives its view from the issue's own); 2537 // with an empty `id` this is the bare list link. 2538 fn issuesLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, status: evt.Issue.Status, tag: []const u8, id: []const u8) ![]const u8 { 2539 const route = ui.RoutablePage.repoIssuesRoute(identity, status, tag, id) orelse return error.RouteTooLong; 2540 const url = try route.toUrl(page_arena); 2541 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2542 } 2543 2544 // the in-page "ai:" anchor for selecting issue `id` in `identity`'s list; the 2545 // href is only followed with js off. 2546 fn issueRowLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, id: []const u8) ![]const u8 { 2547 const route = ui.RoutablePage.repoIssuesRoute(identity, .open, "", id) orelse return error.RouteTooLong; 2548 const url = try route.toUrl(page_arena); 2549 return std.fmt.allocPrint(page_arena.allocator(), "ai:{s}", .{url}); 2550 } 2551 2552 // the "a:" link to the window `status_maybe` names, keeping the tag filter, 2553 // or the conflicts window when null, rooted at issue `id`. 2554 fn windowLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, status_maybe: ?evt.Issue.Status, id: []const u8) ![]const u8 { 2555 if (status_maybe) |status| return issuesLink(page_arena, data.identity, status, data.tag, id); 2556 return conflictsLink(page_arena, data.identity, id); 2557 } 2558 2559 // the "a:" link to the conflicts list rooted at issue `start` ("" = the 2560 // first window). 2561 fn conflictsLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, start: []const u8) ![]const u8 { 2562 const route = ui.RoutablePage.repoIssuesConflictsRoute(identity, start) orelse return error.RouteTooLong; 2563 const url = try route.toUrl(page_arena); 2564 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2565 } 2566 2567 // the "a:" link to the whole-description page of issue `id` within `identity`. 2568 fn descriptionLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, id: []const u8) ![]const u8 { 2569 const route = ui.RoutablePage.repoIssuesDescriptionRoute(identity, id) orelse return error.RouteTooLong; 2570 const url = try route.toUrl(page_arena); 2571 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2572 } 2573 2574 // the "a:" link to `status`'s issues list filtered to `tag` (raw; encoded here). 2575 fn tagLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, status: evt.Issue.Status, tag: []const u8) ![]const u8 { 2576 const encoded = try ui.urlEncodeRef(page_arena.allocator(), tag); 2577 const route = ui.RoutablePage.repoIssuesRoute(identity, status, encoded, "") orelse return error.RouteTooLong; 2578 const url = try route.toUrl(page_arena); 2579 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2580 } 2581 2582 // tabs switching between the issues page's views. 2583 pub const Header = struct { 2584 box: wgt.Box(ui.Widget), 2585 tab_ids: std.AutoArrayHashMapUnmanaged(usize, void), 2586 2587 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self) !Header { 2588 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 2589 errdefer box.deinit(allocator); 2590 2591 var tab_ids: std.AutoArrayHashMapUnmanaged(usize, void) = .empty; 2592 errdefer tab_ids.deinit(allocator); 2593 2594 const aa = session.page_arena.allocator(); 2595 2596 // a list tab per status, labeled with its listing's issue count 2597 for ([_]evt.Issue.Status{ .open, .closed }) |status| { 2598 const route = ui.RoutablePage.repoIssuesRoute(data.identity, status, data.tag, "") orelse return error.RouteTooLong; 2599 const link = try std.fmt.allocPrint(aa, "ai:{s}", .{try route.toUrl(session.page_arena)}); 2600 const label = try std.fmt.allocPrint(aa, "{s} ({d})", .{ @tagName(status), data.window(status).count }); 2601 try addTab(allocator, &box, &tab_ids, label, link); 2602 } 2603 2604 // tags tab, labeled with the active tag filter 2605 { 2606 const tags_route = ui.RoutablePage.repoIssuesTagsRoute(data.identity, data.tag) orelse return error.RouteTooLong; 2607 const tags_link = try std.fmt.allocPrint(aa, "ai:{s}", .{try tags_route.toUrl(session.page_arena)}); 2608 const label = if (data.tag.len == 0) "tags" else blk: { 2609 const decoded = std.Uri.percentDecodeInPlace(try aa.dupe(u8, data.tag)); 2610 break :blk try std.fmt.allocPrint(aa, "tags ({s})", .{decoded}); 2611 }; 2612 try addTab(allocator, &box, &tab_ids, label, tags_link); 2613 } 2614 2615 // new-issue tab; an edit or resolve url shows its tab in this place 2616 { 2617 const route = switch (data.view) { 2618 .edit => ui.RoutablePage.repoIssuesEditRoute(data.identity, data.selected_id) orelse return error.RouteTooLong, 2619 .new_comment => ui.RoutablePage.repoCommentNewRoute(data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong, 2620 .edit_comment => ui.RoutablePage.repoCommentEditRoute(data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong, 2621 .resolve => ui.RoutablePage.repoIssuesResolveRoute(data.identity, data.selected_id, data.theirs_picks) orelse return error.RouteTooLong, 2622 else => ui.RoutablePage.repoIssuesNewRoute(data.identity) orelse return error.RouteTooLong, 2623 }; 2624 const link = try std.fmt.allocPrint(aa, "ai:{s}", .{try route.toUrl(session.page_arena)}); 2625 const label: []const u8 = switch (data.view) { 2626 .edit => "edit", 2627 .new_comment => "reply", 2628 .edit_comment => "edit", 2629 .resolve => "resolve", 2630 else => "new", 2631 }; 2632 try addTab(allocator, &box, &tab_ids, label, link); 2633 } 2634 2635 // conflicts tab, labeled with the conflict count 2636 if (data.conflicts.count > 0) { 2637 const route = ui.RoutablePage.repoIssuesConflictsRoute(data.identity, "") orelse return error.RouteTooLong; 2638 const link = try std.fmt.allocPrint(aa, "ai:{s}", .{try route.toUrl(session.page_arena)}); 2639 const label = try std.fmt.allocPrint(aa, "conflicts ({d})", .{data.conflicts.count}); 2640 try addTab(allocator, &box, &tab_ids, label, link); 2641 } 2642 2643 var self = Header{ .box = box, .tab_ids = tab_ids }; 2644 // the tab matching the page's view is selected initially. 2645 self.getFocus().child_id = self.tab_ids.keys()[View.viewIndex(data.view)]; 2646 return self; 2647 } 2648 2649 fn addTab(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), tab_ids: *std.AutoArrayHashMapUnmanaged(usize, void), label: []const u8, link: []const u8) !void { 2650 var text_box = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 2651 errdefer text_box.deinit(allocator); 2652 text_box.getFocus().focusable = true; 2653 text_box.getFocus().kind = .{ .custom = link }; 2654 try tab_ids.put(allocator, text_box.getFocus().id, {}); 2655 try box.children.put(allocator, text_box.getFocus().id, .{ 2656 .widget = .{ .text_box = text_box }, 2657 .rect = null, 2658 .min_size = .{ .width = label.len + 2, .height = null }, 2659 }); 2660 } 2661 2662 pub fn deinit(self: *Header, allocator: std.mem.Allocator) void { 2663 self.box.deinit(allocator); 2664 self.tab_ids.deinit(allocator); 2665 } 2666 2667 pub fn build(self: *Header, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2668 self.clearGrid(); 2669 // only the selected tab shows its border 2670 for (self.box.children.keys(), self.box.children.values()) |id, *child| { 2671 switch (child.widget) { 2672 .text_box => |*tb| tb.options.border_style = if (self.getFocus().child_id == id) .single else .hidden, 2673 else => {}, 2674 } 2675 } 2676 try self.box.build(allocator, constraint, root_focus); 2677 } 2678 2679 pub fn input(self: *Header, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2680 _ = allocator; 2681 const current_tab = self.currentTabIndex() orelse return; 2682 if (inp.moveTab(key, current_tab, self.tab_ids.count())) |new_tab| { 2683 root_focus.setFocus(self.tab_ids.keys()[new_tab]); 2684 } 2685 } 2686 2687 pub fn clearGrid(self: *Header) void { 2688 self.box.clearGrid(); 2689 } 2690 2691 pub fn getGrid(self: Header) ?Grid { 2692 return self.box.getGrid(); 2693 } 2694 2695 pub fn getFocus(self: *Header) *Focus { 2696 return self.box.getFocus(); 2697 } 2698 2699 pub fn getSelectedIndex(self: Header) ?usize { 2700 return self.currentTabIndex(); 2701 } 2702 2703 fn currentTabIndex(self: Header) ?usize { 2704 const child_id = self.box.focus.child_id orelse return null; 2705 return self.tab_ids.getIndex(child_id); 2706 } 2707 }; 2708