╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ thread.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1183 const id = try evt.parseEventId(entry.id); 1184 1185 switch (source.repo_kind) { 1186 inline else => |repo_kind| { 1187 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, source.localInitOpts()); 1188 defer any_repo.deinit(io, allocator); 1189 switch (any_repo) { 1190 inline else => |*repo| try Event.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id, .{ .status = status }, author), 1191 } 1192 }, 1193 } 1194 1195 const route = listRoute(self.data.identity, status, "", entry.id) orelse return; 1196 try self.session.navigate(route); 1197 } 1198 }; 1199 } 1200 1201 pub fn View(comptime kind: evt.EventKind, comptime Data: type) type { 1202 const Self = Data; 1203 const HeaderType = Data.Header; 1204 const Window = Data.Window; 1205 const Entry = Data.Entry; 1206 const Event = Data.Event; 1207 const Status = Data.Status; 1208 const has_status = @hasField(Event, "status"); 1209 const supports_conflicts = Event.merge_policy == .field_conflicts; 1210 const supports_drafts = @hasField(Self, "drafts"); 1211 const FieldConflict = if (supports_conflicts) Data.FieldConflict else void; 1212 const ViewKind = Data.ViewKind; 1213 const thread_name = Data.thread_name; 1214 const DetailType = Data.Detail; 1215 1216 return struct { 1217 const This = @This(); 1218 // a vertical box: the header tabs on top, then a stack holding a 1219 // master-detail split (thread list + description pane) per status list, 1220 // plus the tags view. 1221 box: wgt.Box(Widget), // vert: [header_index] = tabs, [stack_index] = stack 1222 data: *const Self, 1223 session: *ui.Session, 1224 1225 const header_index: usize = 0; 1226 const stack_index: usize = 1; 1227 // indices within the stack, 1:1 with the header tabs. 1228 const status_count = @typeInfo(Status).@"enum".fields.len; 1229 const tags_view_index: usize = status_count; 1230 // the new-thread form, or the edit, comment, or resolve form when the page was 1231 // loaded at one of their urls. 1232 const form_view_index: usize = tags_view_index + 1; 1233 // the conflicts split; the tab and stack child exist only when the repo 1234 // has conflicted threads. 1235 const drafts_view_index: usize = form_view_index + 1; 1236 const conflict_view_index: usize = drafts_view_index + @intFromBool(supports_drafts); 1237 const view_count: usize = conflict_view_index + @intFromBool(supports_conflicts); 1238 // indices within a split (the horizontal box inside the stack). 1239 const list_index: usize = 0; 1240 const detail_index: usize = 1; 1241 const list_max_width: usize = 35; 1242 const detail_min_width: usize = 40; 1243 // indices within the thread form. 1244 const title_field_index: usize = 0; 1245 const tags_field_index: usize = 1; 1246 const description_field_index: usize = 2; 1247 const submit_field_index: usize = 3; 1248 const comment_author_field_index: usize = 0; 1249 const comment_body_field_index: usize = 1; 1250 const comment_submit_field_index: usize = 2; 1251 1252 pub fn viewIndex(view: ViewKind) usize { 1253 const name = @tagName(view); 1254 if (std.mem.eql(u8, name, "tags")) return tags_view_index; 1255 if (std.mem.eql(u8, name, "new") or std.mem.eql(u8, name, "edit") or std.mem.eql(u8, name, "new_comment") or std.mem.eql(u8, name, "edit_comment") or std.mem.eql(u8, name, "remove") or std.mem.eql(u8, name, "resolve")) return form_view_index; 1256 if (std.mem.eql(u8, name, "conflicts")) return conflict_view_index; 1257 if (std.mem.eql(u8, name, "drafts")) return drafts_view_index; 1258 if (std.mem.eql(u8, name, "description")) unreachable; 1259 inline for (@typeInfo(Status).@"enum".fields, 0..) |field, index| { 1260 if (std.mem.eql(u8, name, field.name)) return index; 1261 } 1262 unreachable; 1263 } 1264 1265 // the status a status split lists; the conflicts split has none (its 1266 // threads carry their own). 1267 fn splitStatus(index: usize) Status { 1268 return @enumFromInt(index); 1269 } 1270 1271 fn entryConflicted(entry: Entry) bool { 1272 return if (supports_conflicts) entry.conflicted else false; 1273 } 1274 1275 fn entryStatus(entry: Entry) Status { 1276 return if (has_status) entry.record.event.status else @enumFromInt(0); 1277 } 1278 1279 fn entryDraft(entry: Entry) bool { 1280 return if (supports_drafts) entry.draft else false; 1281 } 1282 1283 fn listRoute(identity: []const u8, status: Status, tag: []const u8, selected: []const u8) ?ui.RoutablePage { 1284 return Data.listRoute(identity, status, tag, selected); 1285 } 1286 1287 fn draftsRoute(identity: []const u8) ?ui.RoutablePage { 1288 return if (supports_drafts) Data.draftsRoute(identity) else null; 1289 } 1290 1291 fn conflictsRoute(identity: []const u8, selected: []const u8) ?ui.RoutablePage { 1292 return if (supports_conflicts) Data.conflictsRoute(identity, selected) else null; 1293 } 1294 1295 fn resolveRoute(identity: []const u8, selected: []const u8, picks: []const u8) ?ui.RoutablePage { 1296 return if (supports_conflicts) Data.resolveRoute(identity, selected, picks) else null; 1297 } 1298 1299 fn listLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, status: Status, tag: []const u8, id: []const u8) ![]const u8 { 1300 const route = listRoute(identity, status, tag, id) orelse return error.RouteTooLong; 1301 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{try route.toUrl(page_arena)}); 1302 } 1303 1304 fn rowLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, id: []const u8) ![]const u8 { 1305 const selected = std.mem.eql(u8, id, data.selected_id); 1306 const route = (if (selected and data.description_page) 1307 ui.RoutablePage.repoThreadDescriptionRoute(kind, data.identity, id) 1308 else if (selected) 1309 if (data.comment_page) |page| 1310 ui.RoutablePage.repoThreadCommentRoute(kind, data.identity, id, &page.selected.id, page.replies.start) 1311 else 1312 ui.RoutablePage.repoThreadCommentsRoute(kind, data.identity, id, data.comments_start) 1313 else 1314 ui.RoutablePage.repoThreadCommentsRoute(kind, data.identity, id, 0)) orelse return error.RouteTooLong; 1315 return std.fmt.allocPrint(page_arena.allocator(), "ai:{s}", .{try route.toUrl(page_arena)}); 1316 } 1317 1318 fn windowLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, status_maybe: ?Status, drafts: bool, id: []const u8) ![]const u8 { 1319 if (supports_conflicts and status_maybe == null and !drafts) { 1320 const route = conflictsRoute(data.identity, id) orelse return error.RouteTooLong; 1321 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{try route.toUrl(page_arena)}); 1322 } 1323 if (drafts and id.len == 0) { 1324 const route = draftsRoute(data.identity) orelse return error.RouteTooLong; 1325 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{try route.toUrl(page_arena)}); 1326 } 1327 return listLink(page_arena, data.identity, status_maybe orelse @enumFromInt(0), data.tag, id); 1328 } 1329 1330 fn tagLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, status: Status, tag: []const u8) ![]const u8 { 1331 const encoded = try ui.urlEncodeRef(page_arena.allocator(), tag); 1332 return listLink(page_arena, identity, status, encoded, ""); 1333 } 1334 1335 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !This { 1336 var outer = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 1337 errdefer outer.deinit(allocator); 1338 1339 // the tabs at the top. 1340 { 1341 var hdr = try Data.initHeader(allocator, session, data); 1342 errdefer hdr.deinit(allocator); 1343 try outer.children.put(allocator, hdr.getFocus().id, .{ .widget = @unionInit(Widget, Data.header_widget_name, hdr), .rect = null, .min_size = null }); 1344 } 1345 1346 // the stack enters `outer` before its children enter it, so an error 1347 // frees each child exactly once. 1348 { 1349 var stack = try wgt.Stack(Widget).init(allocator); 1350 errdefer stack.deinit(allocator); 1351 try outer.children.put(allocator, stack.getFocus().id, .{ .widget = .{ .stack = stack }, .rect = null, .min_size = null }); 1352 } 1353 const stack = &outer.children.values()[stack_index].widget.stack; 1354 1355 inline for (@typeInfo(Status).@"enum".fields) |field| { 1356 var split = try initSplit(allocator, session, data, @enumFromInt(field.value), false); 1357 errdefer split.deinit(allocator); 1358 try stack.children.put(allocator, split.getFocus().id, .{ .box = split }); 1359 } 1360 1361 // the tags view 1362 { 1363 var tf = try TagFlow.init(allocator); 1364 errdefer tf.deinit(allocator); 1365 var items: std.ArrayList(TagFlow.Item) = .empty; 1366 defer items.deinit(allocator); 1367 // when filtered, the first item clears the filter 1368 if (data.tag.len != 0) 1369 try items.append(allocator, .{ .text = "✕", .link = try listLink(session.page_arena, data.identity, @enumFromInt(0), "", "") }); 1370 for (data.tags) |tag| 1371 try items.append(allocator, .{ .text = tag, .link = try tagLink(session.page_arena, data.identity, @enumFromInt(0), tag) }); 1372 try tf.setItems(allocator, items.items); 1373 try stack.children.put(allocator, tf.getFocus().id, .{ .tag_flow = tf }); 1374 } 1375 1376 // the new-thread form, or — on an edit, comment, or resolve url — that form in 1377 // its place, prefilled with the selected thread's content. a 1378 // logged-out session can't create events, so the unauthorized view 1379 // stands in. 1380 if (session.data.is_local or session.data.user_id != null) { 1381 if (data.view == .remove) { 1382 const aa = session.page_arena.allocator(); 1383 const route = ui.RoutablePage.repoThreadRemoveRoute(kind, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong; 1384 const action = try std.fmt.allocPrint(aa, "form:{s}", .{try route.toUrl(session.page_arena)}); 1385 const event_name = if (data.comment_id.len == 0) thread_name else "comment"; 1386 var label_buf: ["remove ".len + @max(thread_name.len, "comment".len)]u8 = undefined; 1387 const label = try std.fmt.bufPrint(&label_buf, "remove {s}", .{event_name}); 1388 var center = try initRemoveForm(allocator, action, label); 1389 errdefer center.deinit(allocator); 1390 try stack.children.put(allocator, center.getFocus().id, .{ .center = center }); 1391 } else if (data.view == .new_comment or data.view == .edit_comment) { 1392 const editing = data.view == .edit_comment; 1393 const route = if (editing) 1394 ui.RoutablePage.repoThreadCommentEditRoute(kind, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong 1395 else 1396 ui.RoutablePage.repoThreadCommentNewRoute(kind, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong; 1397 const page_url = try route.toUrl(session.page_arena); 1398 const action = try std.fmt.allocPrint(session.page_arena.allocator(), "form:{s}", .{page_url}); 1399 const page = data.comment_page; 1400 const top_level = if (page) |p| std.mem.eql(u8, &p.selected.comment.event.parent_id, &p.selected.comment.event.thread_id) else false; 1401 const parent_route = if (editing) 1402 if (top_level) 1403 ui.RoutablePage.repoThreadCommentsRoute(kind, data.identity, data.selected_id, 0) 1404 else if (page) |p| 1405 ui.RoutablePage.repoThreadCommentRoute(kind, data.identity, data.selected_id, &p.selected.comment.event.parent_id, 0) 1406 else 1407 null 1408 else if (page) |p| 1409 ui.RoutablePage.repoThreadCommentRoute(kind, data.identity, data.selected_id, &p.selected.id, 0) 1410 else 1411 ui.RoutablePage.repoThreadCommentsRoute(kind, data.identity, data.selected_id, 0); 1412 const parent_author: ui.Author = if (editing) 1413 if (top_level) 1414 if (data.selectedThread()) |entry| entry.author else .unknown 1415 else if (page) |p| 1416 p.selected.parent_author orelse .unknown 1417 else 1418 .unknown 1419 else if (page) |p| 1420 p.selected.author 1421 else if (data.selectedThread()) |entry| 1422 entry.author 1423 else 1424 .unknown; 1425 const initial_body: ?[]const u8 = if (editing) if (page) |p| p.selected.comment.event.body else null else null; 1426 var form = try initCommentForm(allocator, session, action, parent_author, parent_route orelse return error.RouteTooLong, initial_body); 1427 errdefer form.deinit(allocator); 1428 try stack.children.put(allocator, form.getFocus().id, .{ .box = form }); 1429 } else if (supports_conflicts and data.view == .resolve) { 1430 // on the web the page grows to the form's height and the 1431 // browser scrolls it; the terminal scrolls the form itself 1432 var form_widget: Widget = blk: { 1433 var form = try initResolveForm(allocator, session, data); 1434 errdefer form.deinit(allocator); 1435 break :blk if (session.is_terminal) 1436 .{ .scroll = try wgt.Scroll(Widget).init(allocator, .{ .box = form }, .{ .direction = .vert }) } 1437 else 1438 .{ .box = form }; 1439 }; 1440 errdefer form_widget.deinit(allocator); 1441 try stack.children.put(allocator, form_widget.getFocus().id, form_widget); 1442 } else { 1443 const aa = session.page_arena.allocator(); 1444 const route = if (data.view == .edit) 1445 ui.RoutablePage.repoThreadEditRoute(kind, data.identity, data.selected_id) orelse return error.RouteTooLong 1446 else 1447 ui.RoutablePage.repoThreadNewRoute(kind, data.identity) orelse return error.RouteTooLong; 1448 const action = try std.fmt.allocPrint(aa, "form:{s}", .{try route.toUrl(session.page_arena)}); 1449 const record = if (data.view == .edit) 1450 (if (data.selectedThread()) |entry| &entry.record else null) 1451 else 1452 null; 1453 var form = try initThreadForm(allocator, session, action, record); 1454 errdefer form.deinit(allocator); 1455 try stack.children.put(allocator, form.getFocus().id, .{ .box = form }); 1456 } 1457 } else { 1458 var message = try ui.Unauthorized.View.init(allocator); 1459 errdefer message.deinit(allocator); 1460 try stack.children.put(allocator, message.getFocus().id, .{ .unauthorized = message }); 1461 } 1462 1463 if (supports_drafts) { 1464 var split = try initSplit(allocator, session, data, null, true); 1465 errdefer split.deinit(allocator); 1466 try stack.children.put(allocator, split.getFocus().id, .{ .box = split }); 1467 } 1468 1469 // the conflicts split; its tab exists only when the repo has 1470 // conflicts, so skip the child too to keep the stack 1:1 with the 1471 // tabs. 1472 if (supports_conflicts and data.conflicts.count > 0) { 1473 var split = try initSplit(allocator, session, data, null, false); 1474 errdefer split.deinit(allocator); 1475 try stack.children.put(allocator, split.getFocus().id, .{ .box = split }); 1476 } 1477 1478 // the stack starts on the page's view. 1479 stack.getFocus().child_id = stack.children.keys()[viewIndex(data.view)]; 1480 1481 // focus entering the view lands on the tabs first. 1482 outer.getFocus().child_id = outer.children.keys()[header_index]; 1483 1484 return .{ 1485 .box = outer, 1486 .data = data, 1487 .session = session, 1488 }; 1489 } 1490 1491 // the master-detail split showing `status`'s window, or the conflicts 1492 // window when null. 1493 fn initSplit(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self, status_maybe: ?Status, drafts: bool) !wgt.Box(Widget) { 1494 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 1495 errdefer box.deinit(allocator); 1496 1497 const win = if (status_maybe) |status| data.window(status) else if (drafts and supports_drafts) &data.drafts else if (supports_conflicts) &data.conflicts else unreachable; 1498 1499 // the thread list (one focusable row per title), plus a "next" link that 1500 // reloads the page rooted at the following thread. 1501 { 1502 var list_scroll = blk: { 1503 var list_box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .vert, .stretch = true }); 1504 errdefer list_box.deinit(allocator); 1505 if (win.prev_id) |prev| 1506 try addRow(allocator, &list_box, "← previous", "", try windowLink(session.page_arena, data, status_maybe, drafts, prev)); 1507 for (win.items) |entry| { 1508 // conflicts take precedence over the comment count 1509 const label: []const u8 = if (entryConflicted(entry) and status_maybe != null) 1510 " conflict " 1511 else if (entry.comments.count > 0) 1512 try std.fmt.allocPrint(session.page_arena.allocator(), " {d} comments ", .{entry.comments.count}) 1513 else 1514 ""; 1515 try addRow(allocator, &list_box, entry.record.event.title, label, try rowLink(session.page_arena, data, entry.id)); 1516 } 1517 if (win.next_id) |next| 1518 try addRow(allocator, &list_box, "next →", "", try windowLink(session.page_arena, data, status_maybe, drafts, next)); 1519 // select the window's first thread (past a leading "previous" 1520 // row) so its description shows on load. 1521 if (win.items.len > 0) 1522 list_box.getFocus().child_id = list_box.children.keys()[if (win.prev_id != null) 1 else 0] 1523 else if (list_box.children.count() > 0) 1524 list_box.getFocus().child_id = list_box.children.keys()[0]; 1525 break :blk try wgt.Scroll(Widget).init(allocator, .{ .box = list_box }, .{ .direction = .vert, .web_native = !session.is_terminal }); 1526 }; 1527 errdefer list_scroll.deinit(allocator); 1528 try box.children.put(allocator, list_scroll.getFocus().id, .{ .widget = .{ .scroll = list_scroll }, .rect = null, .min_size = .{ .width = list_max_width, .height = null }, .max_size = .{ .width = list_max_width, .height = null } }); 1529 } 1530 1531 // frame the detail pane inside the master-detail split 1532 { 1533 var frame = try wgt.Box(Widget).init(allocator, .{ .border_style = .hidden, .direction = .vert }); 1534 errdefer frame.deinit(allocator); 1535 var detail_view = try DetailType.init(allocator, data, session, null, .{}); 1536 errdefer detail_view.deinit(allocator); 1537 frame.getFocus().child_id = detail_view.getFocus().id; 1538 try frame.children.put(allocator, detail_view.getFocus().id, .{ .widget = @unionInit(Widget, Data.detail_widget_name, detail_view), .rect = null, .min_size = null }); 1539 try box.children.put(allocator, frame.getFocus().id, .{ .widget = .{ .box = frame }, .rect = null, .min_size = .{ .width = detail_min_width, .height = null } }); 1540 } 1541 1542 box.getFocus().child_id = box.children.keys()[list_index]; 1543 return box; 1544 } 1545 1546 // a thread form: title/tags/description inputs and a submit button, 1547 // prefilled from `record` when given. its form: subtree makes the web 1548 // renderer POST them to `action`'s route. 1549 fn initThreadForm(allocator: std.mem.Allocator, session: *ui.Session, action: []const u8, record: ?*const Event.Record) !wgt.Box(Widget) { 1550 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 1551 errdefer box.deinit(allocator); 1552 box.getFocus().kind = .{ .custom = action }; 1553 1554 { 1555 var title = try wgt.TextInput.init(allocator, .{ .label = " title ", .name = "title", .visible_width = null, .rounded_corners = true, .render_content = session.is_terminal }); 1556 errdefer title.deinit(allocator); 1557 title.getFocus().focusable = true; 1558 if (record) |r| try title.setContent(allocator, r.event.title); 1559 try box.children.put(allocator, title.getFocus().id, .{ .widget = .{ .text_input = title }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 1560 } 1561 1562 { 1563 var tags = try wgt.TextInput.init(allocator, .{ .label = " tags (separate with spaces) ", .name = "tags", .visible_width = null, .rounded_corners = true, .render_content = session.is_terminal }); 1564 errdefer tags.deinit(allocator); 1565 tags.getFocus().focusable = true; 1566 if (record) |r| try tags.setContent(allocator, r.event.tags); 1567 try box.children.put(allocator, tags.getFocus().id, .{ .widget = .{ .text_input = tags }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 1568 } 1569 1570 { 1571 var description = try wgt.TextInput.init(allocator, .{ .label = " description ", .name = "description", .visible_width = null, .rounded_corners = true, .render_content = session.is_terminal, .multiline = true, .scroll = .{ .fill = true } }); 1572 errdefer description.deinit(allocator); 1573 description.getFocus().focusable = true; 1574 if (record) |r| try description.setContent(allocator, r.event.description); 1575 try box.children.put(allocator, description.getFocus().id, .{ .widget = .{ .text_input = description }, .rect = null, .min_size = null }); 1576 } 1577 1578 try addSubmitButtonLabeled(allocator, &box, if (supports_drafts and record == null) "submit draft" else "submit"); 1579 1580 box.getFocus().child_id = box.children.keys()[title_field_index]; 1581 return box; 1582 } 1583 1584 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(Widget) { 1585 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 1586 errdefer box.deinit(allocator); 1587 box.getFocus().kind = .{ .custom = action }; 1588 1589 { 1590 var parent = try ui.authorBox(allocator, session.page_arena, author); 1591 errdefer parent.deinit(allocator); 1592 parent.options.label = " replying to "; 1593 parent.getFocus().kind = .{ .custom = try std.fmt.allocPrint(session.page_arena.allocator(), "a:{s}", .{try parent_route.toUrl(session.page_arena)}) }; 1594 try box.children.put(allocator, parent.getFocus().id, .{ .widget = .{ .text_box = parent }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 1595 } 1596 1597 { 1598 var body = try wgt.TextInput.init(allocator, .{ 1599 .label = " comment ", 1600 .name = "body", 1601 .visible_width = null, 1602 .rounded_corners = true, 1603 .render_content = session.is_terminal, 1604 .multiline = true, 1605 .visible_height = 5, 1606 .scroll = .{ .fill = true }, 1607 }); 1608 errdefer body.deinit(allocator); 1609 body.getFocus().focusable = true; 1610 if (initial_body) |text| try body.setContent(allocator, text); 1611 try box.children.put(allocator, body.getFocus().id, .{ .widget = .{ .text_input = body }, .rect = null, .min_size = null }); 1612 } 1613 1614 try addSubmitButtonLabeled(allocator, &box, "submit"); 1615 box.getFocus().child_id = box.children.keys()[comment_author_field_index]; 1616 return box; 1617 } 1618 1619 fn initRemoveForm(allocator: std.mem.Allocator, action: []const u8, label: []const u8) !Center { 1620 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .rounded_corners = true, .direction = .vert }); 1621 errdefer box.deinit(allocator); 1622 box.getFocus().kind = .{ .custom = action }; 1623 1624 var prompt = try wgt.Text.init(allocator, "are you sure?"); 1625 errdefer prompt.deinit(allocator); 1626 try box.children.put(allocator, prompt.getFocus().id, .{ .widget = .{ .text = prompt }, .rect = null, .min_size = null }); 1627 1628 var button = try wgt.TextBox.init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1629 errdefer button.deinit(allocator); 1630 button.getFocus().focusable = true; 1631 button.getFocus().kind = .{ .custom = "submit" }; 1632 try box.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = null }); 1633 box.getFocus().child_id = button.getFocus().id; 1634 1635 return Center.init(allocator, .{ .box = box }); 1636 } 1637 1638 // a form's submit button, then a spacer absorbing the leftover 1639 // min-height the box hands its last child, so the button keeps its 1640 // natural height 1641 fn addSubmitButtonLabeled(allocator: std.mem.Allocator, box: *wgt.Box(Widget), label: []const u8) !void { 1642 { 1643 var submit = try SubmitButton.initLabeled(allocator, label); 1644 errdefer submit.deinit(allocator); 1645 try box.children.put(allocator, submit.getFocus().id, .{ .widget = .{ .submit_button = submit }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 1646 } 1647 { 1648 var spacer = try Spacer.init(allocator); 1649 errdefer spacer.deinit(allocator); 1650 try box.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 1651 } 1652 } 1653 1654 // the conflict resolve form. the "use this" links are navigations that 1655 // flip the url's theirs: pick, reloading the prefills; one submit 1656 // settles every conflict. the form: subtree makes the web renderer POST 1657 // to the resolve route. 1658 fn initResolveForm(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self) !wgt.Box(Widget) { 1659 const aa = session.page_arena.allocator(); 1660 // init only picks the resolve view once it has read the conflict 1661 const conflict = if (data.conflict) |*c| c else unreachable; 1662 1663 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 1664 errdefer box.deinit(allocator); 1665 const route = resolveRoute(data.identity, data.selected_id, data.theirs_picks) orelse return error.RouteTooLong; 1666 box.getFocus().kind = .{ .custom = try std.fmt.allocPrint(aa, "form:{s}", .{try route.toUrl(session.page_arena)}) }; 1667 1668 if (conflict.title) |*fc| { 1669 try addLabel(allocator, &box, "title conflict:"); 1670 try addFieldConflict(allocator, &box, session, data, "title", fc); 1671 } 1672 if (conflict.tags) |*fc| { 1673 try addLabel(allocator, &box, "tags conflict:"); 1674 try addFieldConflict(allocator, &box, session, data, "tags", fc); 1675 } 1676 if (comptime @hasField(Data.Conflict, "status")) { 1677 if (conflict.status) |*fc| { 1678 try addLabel(allocator, &box, "status conflict:"); 1679 try addAtomicConflict(allocator, &box, session, data, "status", fc); 1680 } 1681 } 1682 if (comptime @hasField(Data.Conflict, "revision")) { 1683 if (conflict.revision) |*fc| { 1684 try addLabel(allocator, &box, "revision conflict:"); 1685 try addAtomicConflict(allocator, &box, session, data, "revision", fc); 1686 } 1687 } 1688 1689 if (conflict.description) |*desc| { 1690 try addLabel(allocator, &box, "description conflict:"); 1691 var hunk_index: usize = 0; 1692 for (desc.chunks, 0..) |*chunk, chunk_index| { 1693 switch (chunk.*) { 1694 .same => |text| { 1695 var tb = try wgt.TextBox.init(allocator, text, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .word }); 1696 errdefer tb.deinit(allocator); 1697 tb.getFocus().focusable = true; 1698 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1699 }, 1700 // an auto-resolved chunk: only one side changed it, so its 1701 // version stands, shown distinctly with its author 1702 .auto => |auto| { 1703 const author = if (auto.theirs) desc.theirs_author else desc.ours_author; 1704 const verb: []const u8 = if (auto.text == null) "removed" else "edited"; 1705 const label = switch (author) { 1706 .user_name, .email => |name| try std.fmt.allocPrint(aa, " {s} by {s} ", .{ verb, name }), 1707 .unknown => try std.fmt.allocPrint(aa, " {s} by {s} ", .{ verb, if (auto.theirs) @as([]const u8, "them") else "us" }), 1708 }; 1709 var tb = try wgt.TextBox.init(allocator, auto.text orelse "(removed)", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .word, .label = label }); 1710 errdefer tb.deinit(allocator); 1711 tb.getFocus().focusable = true; 1712 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1713 }, 1714 .conflict => |hunk| { 1715 // a blank row on each side sets the group apart from 1716 // the flowing shared and auto-resolved chunks 1717 if (chunk_index > 0) try addGap(allocator, &box); 1718 const name = try std.fmt.allocPrint(aa, "d{d}", .{hunk_index}); 1719 hunk_index += 1; 1720 const picked_theirs = data.theirsPicked(name); 1721 try addVersionRow(allocator, &box, try sideLabel(aa, desc.ours_author, true), hunk.ours orelse "", try useThisLink(session, data, name, false)); 1722 try addVersionRow(allocator, &box, try sideLabel(aa, desc.theirs_author, false), hunk.theirs orelse "", try useThisLink(session, data, name, true)); 1723 var resolution_input = try wgt.TextInput.init(allocator, .{ .label = " resolution ", .name = name, .visible_width = null, .rounded_corners = true, .render_content = session.is_terminal, .multiline = true }); 1724 errdefer resolution_input.deinit(allocator); 1725 resolution_input.getFocus().focusable = true; 1726 try resolution_input.setContent(allocator, (if (picked_theirs) hunk.theirs else hunk.ours) orelse ""); 1727 try box.children.put(allocator, resolution_input.getFocus().id, .{ .widget = .{ .text_input = resolution_input }, .rect = null, .min_size = null }); 1728 if (chunk_index + 1 < desc.chunks.len) try addGap(allocator, &box); 1729 }, 1730 } 1731 } 1732 } 1733 1734 try addSubmitButtonLabeled(allocator, &box, "submit"); 1735 1736 box.getFocus().child_id = box.children.keys()[0]; 1737 return box; 1738 } 1739 1740 // one conflicted scalar field: both sides' version rows, then the 1741 // resolution input prefilled from the picked side. 1742 fn addFieldConflict(allocator: std.mem.Allocator, box: *wgt.Box(Widget), session: *ui.Session, data: *const Self, comptime name: []const u8, fc: *const FieldConflict) !void { 1743 const aa = session.page_arena.allocator(); 1744 try addVersionRow(allocator, box, try sideLabel(aa, fc.ours.author, true), fc.ours.text, try useThisLink(session, data, name, false)); 1745 try addVersionRow(allocator, box, try sideLabel(aa, fc.theirs.author, false), fc.theirs.text, try useThisLink(session, data, name, true)); 1746 1747 var resolution_input = try wgt.TextInput.init(allocator, .{ .label = " resolution ", .name = name, .visible_width = null, .rounded_corners = true, .render_content = session.is_terminal }); 1748 errdefer resolution_input.deinit(allocator); 1749 resolution_input.getFocus().focusable = true; 1750 try resolution_input.setContent(allocator, if (data.theirsPicked(name)) fc.theirs.text else fc.ours.text); 1751 try box.children.put(allocator, resolution_input.getFocus().id, .{ .widget = .{ .text_input = resolution_input }, .rect = null, .min_size = null }); 1752 } 1753 1754 // atomic fields can only pick one complete side 1755 fn addAtomicConflict(allocator: std.mem.Allocator, box: *wgt.Box(Widget), session: *ui.Session, data: *const Self, comptime name: []const u8, fc: *const FieldConflict) !void { 1756 const aa = session.page_arena.allocator(); 1757 try addVersionRow(allocator, box, try sideLabel(aa, fc.ours.author, true), fc.ours.text, try useThisLink(session, data, name, false)); 1758 try addVersionRow(allocator, box, try sideLabel(aa, fc.theirs.author, false), fc.theirs.text, try useThisLink(session, data, name, true)); 1759 var selected = try wgt.TextBox.init(allocator, if (data.theirsPicked(name)) fc.theirs.text else fc.ours.text, .{ 1760 .border_style = .single, 1761 .rounded_corners = true, 1762 .wrap_kind = .word, 1763 .label = " resolution ", 1764 }); 1765 errdefer selected.deinit(allocator); 1766 selected.getFocus().focusable = true; 1767 try box.children.put(allocator, selected.getFocus().id, .{ .widget = .{ .text_box = selected }, .rect = null, .min_size = null }); 1768 } 1769 1770 // a blank row setting a conflict group apart from its neighbors 1771 fn addGap(allocator: std.mem.Allocator, box: *wgt.Box(Widget)) !void { 1772 var text = try wgt.Text.init(allocator, " "); 1773 errdefer text.deinit(allocator); 1774 try box.children.put(allocator, text.getFocus().id, .{ .widget = .{ .text = text }, .rect = null, .min_size = null }); 1775 } 1776 1777 fn addLabel(allocator: std.mem.Allocator, box: *wgt.Box(Widget), content: []const u8) !void { 1778 var label = try SectionLabel.init(allocator, content); 1779 errdefer label.deinit(allocator); 1780 try box.children.put(allocator, label.getFocus().id, .{ .widget = .{ .section_label = label }, .rect = null, .min_size = null }); 1781 } 1782 1783 // one side of a conflict: the "use this" link on the left, the version 1784 // itself as a labeled read-only box. 1785 fn addVersionRow(allocator: std.mem.Allocator, box: *wgt.Box(Widget), label: []const u8, text: []const u8, link: []const u8) !void { 1786 var row = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 1787 errdefer row.deinit(allocator); 1788 1789 { 1790 const use_label = "use this"; 1791 var use = try wgt.TextBox.init(allocator, use_label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1792 errdefer use.deinit(allocator); 1793 use.getFocus().focusable = true; 1794 use.getFocus().kind = .{ .custom = link }; 1795 try row.children.put(allocator, use.getFocus().id, .{ 1796 .widget = .{ .text_box = use }, 1797 .rect = null, 1798 .min_size = .{ .width = use_label.len + 2, .height = null }, 1799 .max_size = .{ .width = use_label.len + 2, .height = null }, 1800 }); 1801 } 1802 1803 { 1804 var tb = try wgt.TextBox.init(allocator, if (text.len > 0) text else "(removed)", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .word, .label = label }); 1805 errdefer tb.deinit(allocator); 1806 tb.getFocus().focusable = true; 1807 try row.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1808 } 1809 1810 row.getFocus().child_id = row.children.keys()[0]; 1811 try box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .box = row }, .rect = null, .min_size = null }); 1812 } 1813 1814 // the label of one side's version box, naming its author 1815 fn sideLabel(aa: std.mem.Allocator, author: ui.Author, ours: bool) ![]const u8 { 1816 return switch (author) { 1817 .user_name, .email => |name| try std.fmt.allocPrint(aa, " edited by {s} ", .{name}), 1818 .unknown => if (ours) " current version " else " their version ", 1819 }; 1820 } 1821 1822 // the a: link that reloads the resolve page with `name` picked from ours 1823 // or theirs, reprefilling its resolution input 1824 fn useThisLink(session: *ui.Session, data: *const Self, name: []const u8, theirs: bool) ![]const u8 { 1825 const aa = session.page_arena.allocator(); 1826 var picks: std.ArrayList(u8) = .empty; 1827 var it = std.mem.splitScalar(u8, data.theirs_picks, ','); 1828 while (it.next()) |pick| { 1829 if (pick.len == 0 or std.mem.eql(u8, pick, name)) continue; 1830 if (picks.items.len > 0) try picks.append(aa, ','); 1831 try picks.appendSlice(aa, pick); 1832 } 1833 if (theirs) { 1834 if (picks.items.len > 0) try picks.append(aa, ','); 1835 try picks.appendSlice(aa, name); 1836 } 1837 const route = resolveRoute(data.identity, data.selected_id, picks.items) orelse return error.RouteTooLong; 1838 return std.fmt.allocPrint(aa, "a:{s}", .{try route.toUrl(session.page_arena)}); 1839 } 1840 1841 fn addRow(allocator: std.mem.Allocator, box: *wgt.Box(Widget), text: []const u8, bottom_label: []const u8, link: []const u8) !void { 1842 var row = try wgt.TextBox.init(allocator, text, .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .word, .bottom_label = bottom_label }); 1843 errdefer row.deinit(allocator); 1844 row.getFocus().focusable = true; 1845 if (link.len != 0) row.getFocus().kind = .{ .custom = link }; 1846 try box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .text_box = row }, .rect = null, .min_size = null, .max_size = .{ .width = null, .height = 5 } }); 1847 } 1848 1849 pub fn deinit(self: *This, allocator: std.mem.Allocator) void { 1850 self.box.deinit(allocator); 1851 } 1852 1853 fn header(self: *This) *HeaderType { 1854 return &@field(self.box.children.values()[header_index].widget, Data.header_widget_name); 1855 } 1856 1857 fn viewStack(self: *This) *wgt.Stack(Widget) { 1858 return &self.box.children.values()[stack_index].widget.stack; 1859 } 1860 1861 // `index`'s master-detail split inside the stack. 1862 fn resultsBox(self: *This, index: usize) *wgt.Box(Widget) { 1863 return &self.viewStack().children.values()[index].box; 1864 } 1865 1866 // the tags view's flow inside the stack. 1867 fn tagsView(self: *This) *TagFlow { 1868 return &self.viewStack().children.values()[tags_view_index].tag_flow; 1869 } 1870 1871 // the new-thread, edit, or resolve form inside the stack, or null when the 1872 // unauthorized view stands in for it. 1873 fn threadForm(self: *This) ?*wgt.Box(Widget) { 1874 return switch (self.viewStack().children.values()[form_view_index]) { 1875 .box => |*box| box, 1876 // the resolve form sits inside a scroll on the terminal 1877 .scroll => |*scroll| &scroll.child.box, 1878 else => null, 1879 }; 1880 } 1881 1882 fn removeForm(self: *This) ?*wgt.Box(Widget) { 1883 return switch (self.viewStack().children.values()[form_view_index]) { 1884 .center => |*center| switch (center.child.*) { 1885 .box => |*box| box, 1886 else => null, 1887 }, 1888 else => null, 1889 }; 1890 } 1891 1892 // the resolve form's scroll on the terminal (the web page scrolls itself) 1893 fn resolveScroll(self: *This) ?*wgt.Scroll(Widget) { 1894 return switch (self.viewStack().children.values()[form_view_index]) { 1895 .scroll => |*scroll| scroll, 1896 else => null, 1897 }; 1898 } 1899 1900 fn listScroll(self: *This, index: usize) *wgt.Scroll(Widget) { 1901 return &self.resultsBox(index).children.values()[list_index].widget.scroll; 1902 } 1903 1904 fn listBox(self: *This, index: usize) *wgt.Box(Widget) { 1905 return &self.listScroll(index).child.box; 1906 } 1907 1908 fn detail(self: *This, index: usize) *DetailType { 1909 const frame = &self.resultsBox(index).children.values()[detail_index].widget.box; 1910 return &@field(frame.children.values()[0].widget, Data.detail_widget_name); 1911 } 1912 1913 fn window(self: *This, index: usize) *const Window { 1914 if (supports_conflicts and index == conflict_view_index) return &self.data.conflicts; 1915 if (supports_drafts and index == drafts_view_index) return &self.data.drafts; 1916 return self.data.window(splitStatus(index)); 1917 } 1918 1919 fn stackSelectedIndex(self: *This) ?usize { 1920 const stack = self.viewStack(); 1921 const cid = stack.getFocus().child_id orelse return null; 1922 return stack.children.getIndex(cid); 1923 } 1924 1925 // the stack's selected master-detail split (null when the tags view or the 1926 // new-thread form shows). 1927 fn selectedSplitIndex(self: *This) ?usize { 1928 const idx = self.stackSelectedIndex() orelse return null; 1929 return if (idx < status_count or (supports_drafts and idx == drafts_view_index) or idx == conflict_view_index) idx else null; 1930 } 1931 1932 fn detailActive(self: *This, index: usize) bool { 1933 const rb = self.resultsBox(index); 1934 const cid = rb.getFocus().child_id orelse return false; 1935 return rb.children.getIndex(cid) == detail_index; 1936 } 1937 1938 fn headerActive(self: *This) bool { 1939 const cid = self.box.getFocus().child_id orelse return false; 1940 return self.box.children.getIndex(cid) == header_index; 1941 } 1942 1943 fn tagsViewActive(self: *This) bool { 1944 if (self.headerActive()) return false; 1945 return self.stackSelectedIndex() == tags_view_index; 1946 } 1947 1948 fn formViewActive(self: *This) bool { 1949 if (self.headerActive()) return false; 1950 return self.stackSelectedIndex() == form_view_index; 1951 } 1952 1953 // the selected thread's index, or null when a window-navigation row is 1954 // selected (a leading "previous" row shifts the thread rows down by one). 1955 fn selectedThreadIndex(self: *This, index: usize) ?usize { 1956 const lb = self.listBox(index); 1957 const cid = lb.getFocus().child_id orelse return null; 1958 const idx = lb.children.getIndex(cid) orelse return null; 1959 const win = self.window(index); 1960 const lead: usize = if (win.prev_id != null) 1 else 0; 1961 if (idx < lead or idx - lead >= win.items.len) return null; 1962 return idx - lead; 1963 } 1964 1965 pub fn build(self: *This, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1966 self.clearGrid(); 1967 1968 // each header tab maps 1:1 to a stack child by position 1969 if (self.header().getSelectedIndex()) |index| { 1970 const stack = self.viewStack(); 1971 stack.getFocus().child_id = stack.children.keys()[index]; 1972 } 1973 1974 if (self.selectedSplitIndex()) |i| { 1975 // swap the detail pane to the selected thread when it changes. 1976 if (self.selectedThreadIndex(i)) |selected| { 1977 const entry = self.window(i).items[selected]; 1978 const changed = if (self.detail(i).entry) |current| !std.mem.eql(u8, current.id, entry.id) else true; 1979 if (changed) { 1980 try self.detail(i).setEntry(allocator, entry); 1981 } 1982 } 1983 1984 // the selected list row shows a border (the focused TextBox 1985 // upgrades it to a double border itself); the rest stay borderless. 1986 const lb = self.listBox(i); 1987 for (lb.children.keys(), lb.children.values()) |id, *child| { 1988 switch (child.widget) { 1989 .text_box => |*tb| tb.options.border_style = if (lb.getFocus().child_id == id) .single else .hidden, 1990 else => {}, 1991 } 1992 } 1993 1994 // cap the list at list_max_width only while the detail pane fits 1995 // beside it. the box drops the detail when the width can't hold 1996 // both minimums, so when it's that narrow we lift the cap and let 1997 // the list fill the whole width. 1998 const both_panes_fit = if (constraint.max_size.width) |w| w >= list_max_width + detail_min_width else true; 1999 self.resultsBox(i).children.values()[list_index].max_size = if (both_panes_fit) .{ .width = list_max_width, .height = null } else null; 2000 2001 // stretch the detail pane across the rest of the width so it fills 2002 // the area rather than shrinking to its content; its scroll fills 2003 // the pane. 2004 if (constraint.max_size.width) |w| { 2005 self.resultsBox(i).children.values()[detail_index].min_size = .{ .width = if (both_panes_fit) w - list_max_width else w, .height = null }; 2006 } else { 2007 self.resultsBox(i).children.values()[detail_index].min_size = .{ .width = detail_min_width, .height = null }; 2008 } 2009 } 2010 2011 // refresh the form inputs' entries in the session's focus-id -> input 2012 // map with this frame's addresses, so the web/wasm form handling can 2013 // find them by focus id 2014 if (self.threadForm()) |form| { 2015 const inputs_arena = self.session.arena.allocator(); 2016 for (form.children.values()) |*child| switch (child.widget) { 2017 .text_input => |*ti| try self.session.text_inputs.put(inputs_arena, ti.getFocus().id, ti), 2018 else => {}, 2019 }; 2020 } 2021 2022 try self.box.build(allocator, constraint, root_focus); 2023 } 2024 2025 pub fn input(self: *This, allocator: std.mem.Allocator, raw_key: Key, root_focus: *Focus) !void { 2026 // wheel input moves focus like the arrows 2027 const key: Key = if (raw_key == .mouse and raw_key.mouse.action == .scroll) 2028 (if (raw_key.mouse.action.scroll == .up) .arrow_up else .arrow_down) 2029 else 2030 raw_key; 2031 2032 if (self.headerActive()) { 2033 // down from the tabs re-enters the stack if the selected view has 2034 // something to focus; other keys move the tabs. 2035 if (inp.vertDirection(key) == .down) { 2036 const enterable = if (self.viewStack().getSelected()) |selected| switch (selected.*) { 2037 .tag_flow => |*tf| tf.text_boxes.items.len > 0, 2038 // nothing focusable in the unauthorized view 2039 .unauthorized => false, 2040 else => true, 2041 } else false; 2042 if (enterable) root_focus.setFocus(self.box.children.keys()[stack_index]); 2043 } else { 2044 try self.header().input(allocator, key, root_focus); 2045 } 2046 return; 2047 } 2048 if (self.tagsViewActive()) { 2049 self.tagsViewInput(key, root_focus); 2050 return; 2051 } 2052 if (self.formViewActive()) { 2053 if (self.data.view == .remove) { 2054 try self.removeFormInput(allocator, key, root_focus); 2055 } else if (supports_conflicts and self.data.view == .resolve) { 2056 try self.resolveInput(allocator, key, root_focus); 2057 } else if (self.data.view == .new_comment or self.data.view == .edit_comment) { 2058 try self.commentFormInput(allocator, key, root_focus); 2059 } else { 2060 try self.formInput(allocator, key, root_focus); 2061 } 2062 return; 2063 } 2064 const i = self.selectedSplitIndex() orelse return; 2065 if (self.detailActive(i)) { 2066 switch (try self.detail(i).inputWithExit(allocator, key, root_focus)) { 2067 .none => {}, 2068 .list => self.focusList(i, root_focus), 2069 .header => self.focusHeader(root_focus), 2070 } 2071 } else { 2072 self.listInput(i, key, root_focus); 2073 } 2074 } 2075 2076 // arrow keys move the tag selection; up from the top row crosses to the 2077 // header tabs. 2078 fn tagsViewInput(self: *This, key: Key, root_focus: *Focus) void { 2079 const tf = self.tagsView(); 2080 const cid = tf.focus.child_id orelse return; 2081 const cur = tf.indexOfFocusId(cid) orelse return; 2082 const count = tf.text_boxes.items.len; 2083 switch (key) { 2084 .arrow_left => if (cur > 0) root_focus.setFocus(tf.text_boxes.items[cur - 1].getFocus().id), 2085 .arrow_right => if (cur + 1 < count) root_focus.setFocus(tf.text_boxes.items[cur + 1].getFocus().id), 2086 .arrow_up => if (tf.rowStep(cur, false)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id) else self.focusHeader(root_focus), 2087 .arrow_down => if (tf.rowStep(cur, true)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id), 2088 .home => root_focus.setFocus(tf.text_boxes.items[0].getFocus().id), 2089 .end => root_focus.setFocus(tf.text_boxes.items[count - 1].getFocus().id), 2090 else => {}, 2091 } 2092 } 2093 2094 fn listInput(self: *This, index: usize, key: Key, root_focus: *Focus) void { 2095 // up/down (and the scroll wheel) move the selection a row; page up/down 2096 // jump a fixed amount. right/Enter cross into the detail pane. up from 2097 // the top row crosses into the header tabs. 2098 if (inp.rowDelta(key, @intCast(self.listBox(index).children.count()))) |delta| { 2099 const lb = self.listBox(index); 2100 const at_top = if (lb.getFocus().child_id) |cid| lb.children.getIndex(cid) == 0 else true; 2101 if (delta < 0 and at_top) return self.focusHeader(root_focus); 2102 moveRowFocus(lb, self.listScroll(index), root_focus, delta); 2103 return; 2104 } 2105 switch (key) { 2106 .enter, .arrow_right => _ = self.detail(index).focusFirst(root_focus), 2107 else => {}, 2108 } 2109 } 2110 2111 fn formInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2112 const form = self.threadForm() orelse return; 2113 const cid = form.getFocus().child_id orelse return; 2114 const cur = form.children.getIndex(cid) orelse return; 2115 2116 if (cur == description_field_index) { 2117 const child = &form.children.values()[cur]; 2118 if (try multilineKeepsKey(&child.widget.text_input, allocator, key)) 2119 return child.widget.input(allocator, key, root_focus); 2120 } 2121 2122 switch (key) { 2123 .arrow_up, .back_tab => if (cur > 0) 2124 root_focus.setFocus(form.children.keys()[cur - 1]) 2125 else 2126 self.focusHeader(root_focus), 2127 .arrow_down, .tab => if (cur < submit_field_index) { 2128 root_focus.setFocus(form.children.keys()[cur + 1]); 2129 }, 2130 .enter => if (cur == submit_field_index) try self.submitForm(allocator), 2131 .mouse => |mouse| if (cur == submit_field_index) { 2132 const submit = &form.children.values()[cur].widget.submit_button; 2133 if (inp.leftClickOn(root_focus, submit.buttonId(), mouse)) try self.submitForm(allocator); 2134 }, 2135 else => try form.children.values()[cur].widget.input(allocator, key, root_focus), 2136 } 2137 } 2138 2139 fn commentFormInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2140 const form = self.threadForm() orelse return; 2141 const cid = form.getFocus().child_id orelse return; 2142 const cur = form.children.getIndex(cid) orelse return; 2143 const child = &form.children.values()[cur]; 2144 2145 if (cur == comment_body_field_index and try multilineKeepsKey(&child.widget.text_input, allocator, key)) 2146 return child.widget.input(allocator, key, root_focus); 2147 2148 switch (key) { 2149 .arrow_up, .back_tab => if (cur > comment_author_field_index) 2150 root_focus.setFocus(form.children.keys()[cur - 1]) 2151 else 2152 self.focusHeader(root_focus), 2153 .arrow_down, .tab => if (cur < comment_submit_field_index) 2154 root_focus.setFocus(form.children.keys()[cur + 1]), 2155 .enter => if (cur == comment_submit_field_index) try self.submitComment(allocator), 2156 .mouse => |mouse| if (cur == comment_submit_field_index) { 2157 const submit = &child.widget.submit_button; 2158 if (inp.leftClickOn(root_focus, submit.buttonId(), mouse)) try self.submitComment(allocator); 2159 }, 2160 else => try child.widget.input(allocator, key, root_focus), 2161 } 2162 } 2163 2164 fn removeFormInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2165 const form = self.removeForm() orelse return; 2166 const button_id = form.children.keys()[1]; 2167 switch (key) { 2168 .arrow_up, .back_tab => self.focusHeader(root_focus), 2169 .enter => try self.submitRemove(allocator), 2170 .mouse => |mouse| if (inp.leftClickOn(root_focus, button_id, mouse)) try self.submitRemove(allocator), 2171 else => {}, 2172 } 2173 } 2174 2175 // the multiline hunk inputs keep enter and any up/down that has a row to 2176 // move to; enter on a "use this" link is a navigation the host follows 2177 // before this runs. 2178 fn resolveInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2179 const form = self.threadForm() orelse return; 2180 const cid = form.getFocus().child_id orelse return; 2181 const cur = form.children.getIndex(cid) orelse return; 2182 const child = &form.children.values()[cur]; 2183 2184 if (child.widget == .text_input) { 2185 const ti = &child.widget.text_input; 2186 if (ti.options.multiline and try multilineKeepsKey(ti, allocator, key)) 2187 return child.widget.input(allocator, key, root_focus); 2188 } 2189 2190 const on_submit = child.widget == .submit_button; 2191 2192 switch (key) { 2193 .arrow_up, .back_tab => if (resolveStep(form, cur, false)) |i| self.focusResolveChild(form, i, root_focus) else self.focusHeader(root_focus), 2194 .arrow_down, .tab => if (resolveStep(form, cur, true)) |i| self.focusResolveChild(form, i, root_focus), 2195 .arrow_left, .arrow_right => switch (child.widget) { 2196 .box => |*row| { 2197 const row_cid = row.getFocus().child_id orelse return; 2198 const row_index = row.children.getIndex(row_cid) orelse return; 2199 if (key == .arrow_left) { 2200 if (row_index > 0) root_focus.setFocus(row.children.keys()[row_index - 1]); 2201 } else if (row_index + 1 < row.children.count()) { 2202 root_focus.setFocus(row.children.keys()[row_index + 1]); 2203 } 2204 }, 2205 else => try child.widget.input(allocator, key, root_focus), 2206 }, 2207 .enter => if (on_submit) try self.submitResolution(allocator), 2208 .mouse => |mouse| if (on_submit) { 2209 if (inp.leftClickOn(root_focus, child.widget.submit_button.buttonId(), mouse)) try self.submitResolution(allocator); 2210 }, 2211 else => try child.widget.input(allocator, key, root_focus), 2212 } 2213 } 2214 2215 // a multiline input keeps enter and any up/down that has a row to move to 2216 fn multilineKeepsKey(text_input: *wgt.TextInput, allocator: std.mem.Allocator, key: Key) !bool { 2217 return switch (key) { 2218 .enter => true, 2219 .arrow_up => !try text_input.cursorOnFirstRow(allocator), 2220 .arrow_down => !try text_input.cursorOnLastRow(allocator), 2221 else => false, 2222 }; 2223 } 2224 2225 // the neighboring focusable resolve-form child, skipping the spacer and 2226 // the blank gap rows 2227 fn resolveStep(form: *wgt.Box(Widget), cur: usize, down: bool) ?usize { 2228 var i = cur; 2229 while (true) { 2230 if (down) { 2231 i += 1; 2232 if (i >= form.children.count()) return null; 2233 } else { 2234 if (i == 0) return null; 2235 i -= 1; 2236 } 2237 switch (form.children.values()[i].widget) { 2238 .spacer, .text => continue, 2239 else => return i, 2240 } 2241 } 2242 } 2243 2244 // focus the resolve-form child at `i` (a version row focuses its selected 2245 // child) and keep it visible on the terminal 2246 fn focusResolveChild(self: *This, form: *wgt.Box(Widget), i: usize, root_focus: *Focus) void { 2247 const child = &form.children.values()[i]; 2248 switch (child.widget) { 2249 .box => |*row| root_focus.setFocus(row.getFocus().child_id orelse form.children.keys()[i]), 2250 else => root_focus.setFocus(form.children.keys()[i]), 2251 } 2252 if (self.session.is_terminal) { 2253 if (self.resolveScroll()) |sc| { 2254 if (child.rect) |rect| sc.scrollToRect(rect); 2255 } 2256 } 2257 } 2258 2259 // re-emit the conflicted thread's event with the resolved content; any 2260 // event on the thread settles its conflict entry. this is the terminal 2261 // path; the web posts the form to the resolve route. 2262 fn submitResolution(self: *This, allocator: std.mem.Allocator) !void { 2263 if (comptime wasm) return; 2264 const io = self.session.io orelse return; 2265 const src = self.data.repo_source orelse return; 2266 const entry = self.data.selectedThread() orelse return; 2267 const author = (try self.session.eventAuthor()) orelse return; 2268 const form = self.threadForm() orelse return; 2269 2270 // gather the inputs by name; the d<n> hunk inputs appear in chunk order 2271 var title: ?[]u8 = null; 2272 var tags: ?[]u8 = null; 2273 var hunks: std.ArrayList([]const u8) = .empty; 2274 defer { 2275 if (title) |t| allocator.free(t); 2276 if (tags) |t| allocator.free(t); 2277 for (hunks.items) |h| allocator.free(h); 2278 hunks.deinit(allocator); 2279 } 2280 for (form.children.values()) |*child| switch (child.widget) { 2281 .text_input => |*ti| { 2282 const text = try ti.text(allocator); 2283 errdefer allocator.free(text); 2284 if (std.mem.eql(u8, ti.options.name, "title")) { 2285 title = text; 2286 } else if (std.mem.eql(u8, ti.options.name, "tags")) { 2287 tags = text; 2288 } else { 2289 try hunks.append(allocator, text); 2290 } 2291 }, 2292 else => {}, 2293 }; 2294 2295 const id_bytes = try evt.parseEventId(entry.id); 2296 2297 switch (src.repo_kind) { 2298 inline else => |repo_kind| { 2299 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2300 defer any_repo.deinit(io, allocator); 2301 switch (any_repo) { 2302 inline else => |*repo| { 2303 var resolution: Event.Resolve = .{ 2304 .title = title, 2305 .tags = tags, 2306 .hunks = hunks.items, 2307 }; 2308 if (comptime @hasField(Event.Resolve, "theirs")) resolution.theirs = self.data.theirs_picks; 2309 const change = Event.Update{ .resolve = resolution }; 2310 Event.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, change, author) catch |err| switch (err) { 2311 // leave the form up for correction 2312 error.InvalidFields => return, 2313 else => |e| return e, 2314 }; 2315 }, 2316 } 2317 }, 2318 } 2319 2320 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2321 try self.session.navigate(route); 2322 } 2323 2324 // commit a new or edited comment and navigate to its permalink. this is the 2325 // terminal path; the web posts the form to its comment route. 2326 fn submitComment(self: *This, allocator: std.mem.Allocator) !void { 2327 if (comptime wasm) return; 2328 const io = self.session.io orelse return; 2329 const src = self.data.repo_source orelse return; 2330 const author = (try self.session.eventAuthor()) orelse return; 2331 const form = self.threadForm() orelse return; 2332 const body_input = &form.children.values()[comment_body_field_index].widget.text_input; 2333 const body = try body_input.text(allocator); 2334 defer allocator.free(body); 2335 if (!evt.Comment.fieldsValid(body)) return; 2336 2337 var thread_id: [evt.event_id_size * 2]u8 = undefined; 2338 @memcpy(&thread_id, self.data.selected_id); 2339 const parent_id = if (self.data.comment_id.len == 0) thread_id else blk: { 2340 var parent: [evt.event_id_size * 2]u8 = undefined; 2341 @memcpy(&parent, self.data.comment_id); 2342 break :blk parent; 2343 }; 2344 var event_id_hex: [evt.event_id_size * 2]u8 = undefined; 2345 const editing = self.data.view == .edit_comment; 2346 switch (src.repo_kind) { 2347 inline else => |repo_kind| { 2348 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2349 defer any_repo.deinit(io, allocator); 2350 switch (any_repo) { 2351 inline else => |*repo| if (editing) { 2352 const comment_id = evt.parseEventId(self.data.comment_id) catch return; 2353 try evt.Comment.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &thread_id, &comment_id, body, author); 2354 event_id_hex = std.fmt.bytesToHex(comment_id, .lower); 2355 } else { 2356 event_id_hex = try evt.Comment.create(repo_kind, repo.self_repo_opts, io, allocator, repo, &thread_id, &parent_id, body, author); 2357 }, 2358 } 2359 }, 2360 } 2361 2362 body_input.clear(allocator); 2363 const route = ui.RoutablePage.repoThreadCommentRoute(kind, self.data.identity, &thread_id, &event_id_hex, 0) orelse return; 2364 try self.session.navigate(route); 2365 } 2366 2367 // the terminal submit paths: the new form commits a new thread event, the 2368 // edit form re-emits the selected thread's event with the form's content. 2369 // the web posts the forms to their routes instead. 2370 fn submitForm(self: *This, allocator: std.mem.Allocator) !void { 2371 if (self.data.view == .edit) { 2372 try self.submitEditedThread(allocator); 2373 } else { 2374 try self.submitNewThread(allocator); 2375 } 2376 } 2377 2378 // commit the new thread to the repo's events branch and navigate to it. 2379 // this is the terminal path; the web posts the form to the thread route, 2380 // so the wasm side never runs (or compiles) the repo access below. 2381 fn submitNewThread(self: *This, allocator: std.mem.Allocator) !void { 2382 if (comptime wasm) return; 2383 const io = self.session.io orelse return; 2384 const src = self.data.repo_source orelse return; 2385 const author = (try self.session.eventAuthor()) orelse return; 2386 2387 const form = self.threadForm() orelse return; 2388 const title_input = &form.children.values()[title_field_index].widget.text_input; 2389 const tags_input = &form.children.values()[tags_field_index].widget.text_input; 2390 const description_input = &form.children.values()[description_field_index].widget.text_input; 2391 2392 const title = try title_input.text(allocator); 2393 defer allocator.free(title); 2394 const tags = try tags_input.text(allocator); 2395 defer allocator.free(tags); 2396 const description = try description_input.text(allocator); 2397 defer allocator.free(description); 2398 2399 if (!Event.fieldsValid(title, tags)) return; 2400 2401 if (comptime supports_drafts) { 2402 const event_id_hex = try Data.createDraft(self.data, self.session, allocator, title, tags, description); 2403 title_input.clear(allocator); 2404 tags_input.clear(allocator); 2405 description_input.clear(allocator); 2406 const route = ui.RoutablePage.repoThreadCommentsRoute(kind, self.data.identity, &event_id_hex, 0) orelse return; 2407 try self.session.navigate(route); 2408 return; 2409 } 2410 2411 var id_bytes: [evt.event_id_size]u8 = undefined; 2412 io.random(&id_bytes); 2413 const event_id_hex = std.fmt.bytesToHex(id_bytes, .lower); 2414 2415 const event = evt.EventWithId{ 2416 .id = event_id_hex, 2417 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 2418 .author = author, 2419 .event = @unionInit(evt.Event, @tagName(kind), Event{ 2420 .title = title, 2421 .description = description, 2422 .tags = tags, 2423 }), 2424 }; 2425 2426 switch (src.repo_kind) { 2427 inline else => |repo_kind| { 2428 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2429 defer any_repo.deinit(io, allocator); 2430 switch (any_repo) { 2431 inline else => |*repo| try evt.consume(.repo, repo_kind, repo.self_repo_opts, io, allocator, repo, evt.events_ref, &.{event}), 2432 } 2433 }, 2434 } 2435 2436 // wipe the form so a return visit starts fresh 2437 title_input.clear(allocator); 2438 tags_input.clear(allocator); 2439 description_input.clear(allocator); 2440 2441 const route = listRoute(self.data.identity, @enumFromInt(0), "", &event_id_hex) orelse return; 2442 try self.session.navigate(route); 2443 } 2444 2445 // re-emit the selected thread's event with the form's content (status 2446 // preserved), then reload the thread's page. this is the terminal path; 2447 // the web posts the form to the edit route. 2448 fn submitEditedThread(self: *This, allocator: std.mem.Allocator) !void { 2449 if (comptime wasm) return; 2450 const io = self.session.io orelse return; 2451 const entry = self.data.selectedThread() orelse return; 2452 2453 const form = self.threadForm() orelse return; 2454 const title_input = &form.children.values()[title_field_index].widget.text_input; 2455 const tags_input = &form.children.values()[tags_field_index].widget.text_input; 2456 const description_input = &form.children.values()[description_field_index].widget.text_input; 2457 2458 const title = try title_input.text(allocator); 2459 defer allocator.free(title); 2460 const tags = try tags_input.text(allocator); 2461 defer allocator.free(tags); 2462 const description = try description_input.text(allocator); 2463 defer allocator.free(description); 2464 2465 if (!Event.fieldsValid(title, tags)) return; 2466 2467 if (comptime supports_drafts) { 2468 if (entryDraft(entry.*)) { 2469 try self.data.editDraft(self.session, allocator, entry.id, title, tags, description); 2470 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2471 try self.session.navigate(route); 2472 return; 2473 } 2474 } 2475 2476 const id_bytes = try evt.parseEventId(entry.id); 2477 const src = self.data.repo_source orelse return; 2478 const author = (try self.session.eventAuthor()) orelse return; 2479 2480 switch (src.repo_kind) { 2481 inline else => |repo_kind| { 2482 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2483 defer any_repo.deinit(io, allocator); 2484 switch (any_repo) { 2485 inline else => |*repo| if (has_status) 2486 try Event.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 2487 .title = title, 2488 .tags = tags, 2489 .description = description, 2490 } }, author) 2491 else 2492 try Event.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, title, tags, description, author), 2493 } 2494 }, 2495 } 2496 2497 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2498 try self.session.navigate(route); 2499 } 2500 2501 fn submitRemove(self: *This, allocator: std.mem.Allocator) !void { 2502 const comment = self.data.comment_id.len != 0; 2503 const event_id = if (comment) self.data.comment_id else self.data.selected_id; 2504 const id = evt.parseEventId(event_id) catch return; 2505 try self.removeEvent(allocator, if (comment) .comment else kind, id); 2506 } 2507 2508 fn removeEvent(self: *This, allocator: std.mem.Allocator, event_kind: evt.EventKind, id: [evt.event_id_size]u8) !void { 2509 if (comptime wasm) return; 2510 2511 if (comptime supports_drafts) { 2512 const selected = self.data.selectedThread() orelse return; 2513 if (event_kind == kind and entryDraft(selected.*)) { 2514 try Data.removeDraft(self.session, allocator, &id); 2515 const route = draftsRoute(self.data.identity) orelse return; 2516 try self.session.navigate(route); 2517 return; 2518 } 2519 } 2520 2521 const io = self.session.io orelse return; 2522 const src = self.data.repo_source orelse return; 2523 const author = (try self.session.eventAuthor()) orelse return; 2524 2525 switch (src.repo_kind) { 2526 inline else => |repo_kind| { 2527 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2528 defer any_repo.deinit(io, allocator); 2529 switch (any_repo) { 2530 inline else => |*repo| try evt.remove(.repo, repo_kind, repo.self_repo_opts, io, allocator, repo, &id, event_kind, author), 2531 } 2532 }, 2533 } 2534 2535 const id_hex = std.fmt.bytesToHex(id, .lower); 2536 const route = ui.RoutablePage.repoEventsRoute(self.data.identity, .removed, event_kind, &id_hex) orelse return; 2537 try self.session.navigate(route); 2538 } 2539 2540 // return to the list. 2541 fn focusList(self: *This, index: usize, root_focus: *Focus) void { 2542 root_focus.setFocus(self.listScroll(index).getFocus().id); 2543 } 2544 2545 // cross to the header tabs above the stack. 2546 fn focusHeader(self: *This, root_focus: *Focus) void { 2547 root_focus.setFocus(self.box.children.keys()[header_index]); 2548 } 2549 2550 pub fn clearGrid(self: *This) void { 2551 self.box.clearGrid(); 2552 } 2553 2554 pub fn getGrid(self: This) ?Grid { 2555 return self.box.getGrid(); 2556 } 2557 2558 pub fn getFocus(self: *This) *Focus { 2559 return self.box.getFocus(); 2560 } 2561 2562 // for the parent's "scroll up at the top jumps to the header" check: at the 2563 // top only while the header tabs hold focus, so up from the split 2564 // crosses into the tabs first. 2565 pub fn getSelectedIndex(self: *This) ?usize { 2566 return if (self.headerActive()) 0 else 1; 2567 } 2568 }; 2569 } 2570