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