╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ thread.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2221 box.getFocus().child_id = parent.getFocus().id; 2222 } 2223 2224 { 2225 var body = try wgt.TextInput.init(allocator, .{ 2226 .top_label = .{ .text = " comment (markdown supported) " }, 2227 .name = "body", 2228 .visible_width = null, 2229 .round_corners = true, 2230 .render_content = session.is_terminal, 2231 .multiline = true, 2232 .visible_height = 5, 2233 .scroll = .{ .fill = true }, 2234 }); 2235 errdefer body.deinit(allocator); 2236 body.getFocus().mode = .all; 2237 if (initial_body) |text| try body.setContent(allocator, text); 2238 try box.children.put(allocator, body.getFocus().id, .{ .widget = .{ .text_input = body }, .rect = null, .min_size = null }); 2239 } 2240 2241 try addSubmitButtonLabeled(allocator, &box, "submit"); 2242 return box; 2243 } 2244 2245 // a form's submit button, then a spacer absorbing the leftover 2246 // min-height the box hands its last child, so the button keeps its 2247 // natural height 2248 fn addSubmitButtonLabeled(allocator: std.mem.Allocator, box: *wgt.Box(Widget), label: []const u8) !void { 2249 { 2250 var submit = try SubmitButton.initLabeled(allocator, label); 2251 errdefer submit.deinit(allocator); 2252 try box.children.put(allocator, submit.getFocus().id, .{ .widget = .{ .submit_button = submit }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 2253 } 2254 { 2255 var spacer = try Spacer.init(allocator); 2256 errdefer spacer.deinit(allocator); 2257 try box.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 2258 } 2259 } 2260 2261 // the conflict resolve form. the "use this" links are navigations that 2262 // flip the url's theirs: pick, reloading the prefills; one submit 2263 // settles every conflict. the form: subtree makes the web renderer POST 2264 // to the resolve route. 2265 fn initResolveForm(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self) !wgt.Box(Widget) { 2266 const aa = session.page_arena.allocator(); 2267 // init only picks the resolve view once it has read the conflict 2268 const conflict = if (data.conflict) |*c| c else unreachable; 2269 2270 var box = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .vert }); 2271 errdefer box.deinit(allocator); 2272 const route = resolveRoute(data.identity, data.selected_id, data.theirs_picks) orelse return error.RouteTooLong; 2273 box.getFocus().kind = .{ .custom = try aa.print("form:{s}", .{try route.toUrl(session.page_arena)}) }; 2274 2275 if (conflict.title) |*fc| { 2276 try addLabel(allocator, &box, "title conflict:"); 2277 try addFieldConflict(allocator, &box, session, data, "title", fc); 2278 } 2279 if (conflict.labels) |*fc| { 2280 try addLabel(allocator, &box, "labels conflict:"); 2281 try addFieldConflict(allocator, &box, session, data, "labels", fc); 2282 } 2283 if (comptime @hasField(Data.Conflict, "status")) { 2284 if (conflict.status) |*fc| { 2285 try addLabel(allocator, &box, "status conflict:"); 2286 try addAtomicConflict(allocator, &box, session, data, "status", fc); 2287 } 2288 } 2289 if (comptime @hasField(Data.Conflict, "revision")) { 2290 if (conflict.revision) |*fc| { 2291 try addLabel(allocator, &box, "revision conflict:"); 2292 try addAtomicConflict(allocator, &box, session, data, "revision", fc); 2293 } 2294 if (conflict.target_branch) |*fc| { 2295 try addLabel(allocator, &box, "target branch conflict:"); 2296 try addAtomicConflict(allocator, &box, session, data, "target_branch", fc); 2297 } 2298 } 2299 2300 if (conflict.description) |*desc| { 2301 try addLabel(allocator, &box, "description conflict:"); 2302 var hunk_index: usize = 0; 2303 for (desc.chunks, 0..) |*chunk, chunk_index| { 2304 switch (chunk.*) { 2305 .same => |text| { 2306 var tb = try wgt.TextBox.init(allocator, text, .{ .border = .single, .round_corners = true, .wrap_kind = .word }); 2307 errdefer tb.deinit(allocator); 2308 tb.getFocus().mode = .all; 2309 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 2310 }, 2311 // an auto-resolved chunk: only one side changed it, so its 2312 // version stands, shown distinctly with its author 2313 .auto => |auto| { 2314 const author = if (auto.theirs) desc.theirs_author else desc.ours_author; 2315 const verb: []const u8 = if (auto.text == null) "removed" else "edited"; 2316 const label = switch (author) { 2317 .user_name, .email => |name| try aa.print(" {s} by {s} ", .{ verb, name }), 2318 .unknown => try aa.print(" {s} by {s} ", .{ verb, if (auto.theirs) @as([]const u8, "them") else "us" }), 2319 }; 2320 var tb = try wgt.TextBox.init(allocator, auto.text orelse "(removed)", .{ .border = .single, .round_corners = true, .wrap_kind = .word, .top_label = .{ .text = label } }); 2321 errdefer tb.deinit(allocator); 2322 tb.getFocus().mode = .all; 2323 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 2324 }, 2325 .conflict => |hunk| { 2326 // a blank row on each side sets the group apart from 2327 // the flowing shared and auto-resolved chunks 2328 if (chunk_index > 0) try addGap(allocator, &box); 2329 const name = try aa.print("d{d}", .{hunk_index}); 2330 hunk_index += 1; 2331 const picked_theirs = data.theirsPicked(name); 2332 try addVersionRow(allocator, &box, try sideLabel(aa, desc.ours_author, true), hunk.ours orelse "", try useThisLink(session, data, name, false)); 2333 try addVersionRow(allocator, &box, try sideLabel(aa, desc.theirs_author, false), hunk.theirs orelse "", try useThisLink(session, data, name, true)); 2334 var resolution_input = try wgt.TextInput.init(allocator, .{ .top_label = .{ .text = " resolution " }, .name = name, .visible_width = null, .round_corners = true, .render_content = session.is_terminal, .multiline = true }); 2335 errdefer resolution_input.deinit(allocator); 2336 resolution_input.getFocus().mode = .all; 2337 try resolution_input.setContent(allocator, (if (picked_theirs) hunk.theirs else hunk.ours) orelse ""); 2338 try box.children.put(allocator, resolution_input.getFocus().id, .{ .widget = .{ .text_input = resolution_input }, .rect = null, .min_size = null }); 2339 if (chunk_index + 1 < desc.chunks.len) try addGap(allocator, &box); 2340 }, 2341 } 2342 } 2343 } 2344 2345 try addSubmitButtonLabeled(allocator, &box, "submit"); 2346 2347 box.getFocus().child_id = box.children.keys()[0]; 2348 return box; 2349 } 2350 2351 // one conflicted scalar field: both sides' version rows, then the 2352 // resolution input prefilled from the picked side. 2353 fn addFieldConflict(allocator: std.mem.Allocator, box: *wgt.Box(Widget), session: *ui.Session, data: *const Self, comptime name: []const u8, fc: *const FieldConflict) !void { 2354 const aa = session.page_arena.allocator(); 2355 try addVersionRow(allocator, box, try sideLabel(aa, fc.ours.author, true), fc.ours.text, try useThisLink(session, data, name, false)); 2356 try addVersionRow(allocator, box, try sideLabel(aa, fc.theirs.author, false), fc.theirs.text, try useThisLink(session, data, name, true)); 2357 2358 var resolution_input = try wgt.TextInput.init(allocator, .{ .top_label = .{ .text = " resolution " }, .name = name, .visible_width = null, .round_corners = true, .render_content = session.is_terminal }); 2359 errdefer resolution_input.deinit(allocator); 2360 resolution_input.getFocus().mode = .all; 2361 try resolution_input.setContent(allocator, if (data.theirsPicked(name)) fc.theirs.text else fc.ours.text); 2362 try box.children.put(allocator, resolution_input.getFocus().id, .{ .widget = .{ .text_input = resolution_input }, .rect = null, .min_size = null }); 2363 } 2364 2365 // atomic fields can only pick one complete side 2366 fn addAtomicConflict(allocator: std.mem.Allocator, box: *wgt.Box(Widget), session: *ui.Session, data: *const Self, comptime name: []const u8, fc: *const FieldConflict) !void { 2367 const aa = session.page_arena.allocator(); 2368 try addVersionRow(allocator, box, try sideLabel(aa, fc.ours.author, true), fc.ours.text, try useThisLink(session, data, name, false)); 2369 try addVersionRow(allocator, box, try sideLabel(aa, fc.theirs.author, false), fc.theirs.text, try useThisLink(session, data, name, true)); 2370 var selected = try wgt.TextBox.init(allocator, if (data.theirsPicked(name)) fc.theirs.text else fc.ours.text, .{ 2371 .border = .single, 2372 .round_corners = true, 2373 .wrap_kind = .word, 2374 .top_label = .{ .text = " resolution " }, 2375 }); 2376 errdefer selected.deinit(allocator); 2377 selected.getFocus().mode = .all; 2378 try box.children.put(allocator, selected.getFocus().id, .{ .widget = .{ .text_box = selected }, .rect = null, .min_size = null }); 2379 } 2380 2381 // a blank row setting a conflict group apart from its neighbors 2382 fn addGap(allocator: std.mem.Allocator, box: *wgt.Box(Widget)) !void { 2383 var text = try wgt.Text.init(allocator, " "); 2384 errdefer text.deinit(allocator); 2385 try box.children.put(allocator, text.getFocus().id, .{ .widget = .{ .text = text }, .rect = null, .min_size = null }); 2386 } 2387 2388 fn addLabel(allocator: std.mem.Allocator, box: *wgt.Box(Widget), content: []const u8) !void { 2389 var label = try SectionLabel.init(allocator, content); 2390 errdefer label.deinit(allocator); 2391 try box.children.put(allocator, label.getFocus().id, .{ .widget = .{ .section_label = label }, .rect = null, .min_size = null }); 2392 } 2393 2394 // one side of a conflict: the "use this" link on the left, the version 2395 // itself as a labeled read-only box. 2396 fn addVersionRow(allocator: std.mem.Allocator, box: *wgt.Box(Widget), label: []const u8, text: []const u8, link: []const u8) !void { 2397 var row = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .horiz }); 2398 errdefer row.deinit(allocator); 2399 2400 { 2401 const use_label = "use this"; 2402 var use = try wgt.TextBox.init(allocator, use_label, .{ .border = .single, .round_corners = true, .wrap_kind = .none }); 2403 errdefer use.deinit(allocator); 2404 use.getFocus().mode = .all; 2405 use.getFocus().kind = .{ .custom = link }; 2406 try row.children.put(allocator, use.getFocus().id, .{ 2407 .widget = .{ .text_box = use }, 2408 .rect = null, 2409 .min_size = .{ .width = use_label.len + 2, .height = null }, 2410 .max_size = .{ .width = use_label.len + 2, .height = null }, 2411 }); 2412 } 2413 2414 { 2415 var tb = try wgt.TextBox.init(allocator, if (text.len > 0) text else "(removed)", .{ .border = .single, .round_corners = true, .wrap_kind = .word, .top_label = .{ .text = label } }); 2416 errdefer tb.deinit(allocator); 2417 tb.getFocus().mode = .all; 2418 try row.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 2419 } 2420 2421 row.getFocus().child_id = row.children.keys()[0]; 2422 try box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .box = row }, .rect = null, .min_size = null }); 2423 } 2424 2425 // the label of one side's version box, naming its author 2426 fn sideLabel(aa: std.mem.Allocator, author: ui.Author, ours: bool) ![]const u8 { 2427 return switch (author) { 2428 .user_name, .email => |name| try aa.print(" edited by {s} ", .{name}), 2429 .unknown => if (ours) " current version " else " their version ", 2430 }; 2431 } 2432 2433 // the a: link that reloads the resolve page with `name` picked from ours 2434 // or theirs, reprefilling its resolution input 2435 fn useThisLink(session: *ui.Session, data: *const Self, name: []const u8, theirs: bool) ![]const u8 { 2436 const aa = session.page_arena.allocator(); 2437 var picks: std.ArrayList(u8) = .empty; 2438 var it = std.mem.splitScalar(u8, data.theirs_picks, ','); 2439 while (it.next()) |pick| { 2440 if (pick.len == 0 or std.mem.eql(u8, pick, name)) continue; 2441 if (picks.items.len > 0) try picks.append(aa, ','); 2442 try picks.appendSlice(aa, pick); 2443 } 2444 if (theirs) { 2445 if (picks.items.len > 0) try picks.append(aa, ','); 2446 try picks.appendSlice(aa, name); 2447 } 2448 const route = resolveRoute(data.identity, data.selected_id, picks.items) orelse return error.RouteTooLong; 2449 return aa.print("a:{s}", .{try route.toUrl(session.page_arena)}); 2450 } 2451 2452 fn addRow(allocator: std.mem.Allocator, box: *wgt.Box(Widget), text: []const u8, bottom_label: []const u8, link: []const u8) !void { 2453 var row = try wgt.TextBox.init(allocator, text, .{ .border = .hidden, .round_corners = true, .wrap_kind = .word, .bottom_label = .{ .text = bottom_label } }); 2454 errdefer row.deinit(allocator); 2455 row.getFocus().mode = .all; 2456 if (link.len != 0) row.getFocus().kind = .{ .custom = link }; 2457 try box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .text_box = row }, .rect = null, .min_size = null, .max_size = .{ .width = null, .height = 5 } }); 2458 } 2459 2460 pub fn deinit(self: *This, allocator: std.mem.Allocator) void { 2461 self.box.deinit(allocator); 2462 } 2463 2464 fn header(self: *This) *HeaderType { 2465 return &@field(self.box.children.values()[header_index].widget, Data.header_widget_name); 2466 } 2467 2468 fn viewStack(self: *This) *wgt.Stack(Widget) { 2469 return &self.box.children.values()[stack_index].widget.stack; 2470 } 2471 2472 // `index`'s master-detail split inside the stack. 2473 fn resultsBox(self: *This, index: usize) *wgt.Box(Widget) { 2474 return &self.viewStack().children.values()[index].box; 2475 } 2476 2477 // the labels view's flow inside the stack. 2478 fn labelsView(self: *This) *WordFlow { 2479 return &self.viewStack().children.values()[labels_view_index].word_flow; 2480 } 2481 2482 // no labels, which shows the empty message 2483 fn labelsEmpty(data: *const Self) bool { 2484 return data.labels.len == 0; 2485 } 2486 2487 // the non-confirmation form inside the stack 2488 fn formBox(self: *This) ?*wgt.Box(Widget) { 2489 return switch (self.viewStack().children.values()[form_view_index]) { 2490 .box => |*box| box, 2491 // the resolve form sits inside a scroll on the terminal 2492 .scroll => |*scroll| &scroll.child.box, 2493 else => null, 2494 }; 2495 } 2496 2497 fn threadForm(self: *This) ?*wgt.Box(Widget) { 2498 if (supports_conflicts and self.data.view == .resolve) return null; 2499 return switch (self.data.view) { 2500 .new_comment, .edit_comment => null, 2501 else => self.formBox(), 2502 }; 2503 } 2504 2505 fn formField(form: *wgt.Box(Widget), name: []const u8) !*wgt.TextInput { 2506 for (form.children.values()) |*child| switch (child.widget) { 2507 .text_input => |*text_input| if (std.mem.eql(u8, text_input.options.name, name)) return text_input, 2508 else => {}, 2509 }; 2510 return error.MissingFormField; 2511 } 2512 2513 fn formWidget(form: *wgt.Box(Widget), comptime widget_kind: std.meta.Tag(Widget)) ?*@FieldType(Widget, @tagName(widget_kind)) { 2514 for (form.children.values()) |*child| { 2515 if (child.widget == widget_kind) return &@field(child.widget, @tagName(widget_kind)); 2516 } 2517 return null; 2518 } 2519 2520 fn confirmationForm(self: *This) ?*wgt.Box(Widget) { 2521 return switch (self.viewStack().children.values()[form_view_index]) { 2522 .center => |*center| switch (center.child.*) { 2523 .box => |*box| box, 2524 else => null, 2525 }, 2526 else => null, 2527 }; 2528 } 2529 2530 // the resolve form's scroll on the terminal (the web page scrolls itself) 2531 fn resolveScroll(self: *This) ?*wgt.Scroll(Widget) { 2532 return switch (self.viewStack().children.values()[form_view_index]) { 2533 .scroll => |*scroll| scroll, 2534 else => null, 2535 }; 2536 } 2537 2538 fn listScroll(self: *This, index: usize) *wgt.Scroll(Widget) { 2539 return &self.resultsBox(index).children.values()[list_index].widget.scroll; 2540 } 2541 2542 fn listBox(self: *This, index: usize) *wgt.Box(Widget) { 2543 return &self.listScroll(index).child.box; 2544 } 2545 2546 fn detailFrame(self: *This, index: usize) *wgt.Box(Widget) { 2547 return &self.resultsBox(index).children.values()[detail_index].widget.box; 2548 } 2549 2550 fn detail(self: *This, index: usize) *DetailType { 2551 return &@field(self.detailFrame(index).children.values()[widget.list_pane_index].widget, Data.detail_widget_name); 2552 } 2553 2554 fn window(self: *This, index: usize) *const Window { 2555 if (supports_conflicts and index == conflict_view_index) return &self.data.conflicts; 2556 if (supports_drafts and index == drafts_view_index) return &self.data.drafts; 2557 return self.data.window(splitStatus(index)); 2558 } 2559 2560 // an empty first window, which shows the empty message 2561 fn windowEmpty(win: *const Window) bool { 2562 return win.items.len == 0 and win.prev_id == null; 2563 } 2564 2565 fn stackSelectedIndex(self: *This) ?usize { 2566 const stack = self.viewStack(); 2567 const cid = stack.getFocus().child_id orelse return null; 2568 return stack.children.getIndex(cid); 2569 } 2570 2571 // the stack's selected master-detail split (null when the labels view, the 2572 // new-thread form, or the empty message shows). 2573 fn selectedSplitIndex(self: *This) ?usize { 2574 const idx = self.stackSelectedIndex() orelse return null; 2575 const split = idx < status_count or (supports_drafts and idx == drafts_view_index) or idx == conflict_view_index; 2576 return if (split and !windowEmpty(self.window(idx))) idx else null; 2577 } 2578 2579 fn detailActive(self: *This, index: usize) bool { 2580 const rb = self.resultsBox(index); 2581 const cid = rb.getFocus().child_id orelse return false; 2582 return rb.children.getIndex(cid) == detail_index; 2583 } 2584 2585 fn headerActive(self: *This) bool { 2586 const cid = self.box.getFocus().child_id orelse return false; 2587 return self.box.children.getIndex(cid) == header_index; 2588 } 2589 2590 fn labelsViewActive(self: *This) bool { 2591 if (self.headerActive()) return false; 2592 return self.stackSelectedIndex() == labels_view_index; 2593 } 2594 2595 fn formViewActive(self: *This) bool { 2596 if (self.headerActive()) return false; 2597 return self.stackSelectedIndex() == form_view_index; 2598 } 2599 2600 // the selected thread's index, or null when a window-navigation row is 2601 // selected (a leading "previous" row shifts the thread rows down by one). 2602 fn selectedThreadIndex(self: *This, index: usize) ?usize { 2603 const lb = self.listBox(index); 2604 const cid = lb.getFocus().child_id orelse return null; 2605 const idx = lb.children.getIndex(cid) orelse return null; 2606 const win = self.window(index); 2607 const lead: usize = if (win.prev_id != null) 1 else 0; 2608 if (idx < lead or idx - lead >= win.items.len) return null; 2609 return idx - lead; 2610 } 2611 2612 // swap the detail pane to the selected thread when it changes 2613 fn refreshDetail(self: *This, allocator: std.mem.Allocator, index: usize) !void { 2614 const selected = self.selectedThreadIndex(index) orelse return; 2615 const entry = self.window(index).items[selected]; 2616 const changed = if (self.detail(index).entry) |current| !std.mem.eql(u8, current.id, entry.id) else true; 2617 if (!changed) return; 2618 try self.detail(index).setEntry(allocator, entry); 2619 widget.setListBackLink(self.detailFrame(index), try rowLink(self.session.page_arena, self.data, entry.id)); 2620 } 2621 2622 pub fn build(self: *This, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2623 self.clearGrid(); 2624 2625 // each header tab maps 1:1 to a stack child by position 2626 if (self.header().getSelectedIndex()) |index| { 2627 const stack = self.viewStack(); 2628 stack.getFocus().child_id = stack.children.keys()[index]; 2629 } 2630 2631 if (self.selectedSplitIndex()) |i| { 2632 // whether the detail pane fits beside the list, which decides 2633 // the list cap below and whether the detail carries a way back 2634 const both_panes_fit = if (constraint.max_size.width) |w| w >= list_max_width + detail_min_width else true; 2635 try self.refreshDetail(allocator, i); 2636 widget.showListBack(self.detailFrame(i), !both_panes_fit); 2637 2638 // the selected list row shows a border (the focused TextBox 2639 // upgrades it to a double border itself); the rest stay borderless. 2640 const lb = self.listBox(i); 2641 for (lb.children.keys(), lb.children.values()) |id, *child| { 2642 switch (child.widget) { 2643 .text_box => |*tb| widget.markSelected(tb, lb.getFocus().child_id == id), 2644 else => {}, 2645 } 2646 } 2647 2648 // cap the list at list_max_width only while the detail pane fits 2649 // beside it. the box drops the detail when the width can't hold 2650 // both minimums, so when it's that narrow we lift the cap and let 2651 // the list fill the whole width. 2652 self.resultsBox(i).children.values()[list_index].max_size = if (both_panes_fit) .{ .width = list_max_width, .height = null } else null; 2653 2654 // stretch the detail pane across the rest of the width so it fills 2655 // the area rather than shrinking to its content; its scroll fills 2656 // the pane. 2657 if (constraint.max_size.width) |w| { 2658 self.resultsBox(i).children.values()[detail_index].min_size = .{ .width = if (both_panes_fit) w - list_max_width else w, .height = null }; 2659 } else { 2660 self.resultsBox(i).children.values()[detail_index].min_size = .{ .width = detail_min_width, .height = null }; 2661 } 2662 } 2663 2664 // refresh the form inputs' entries in the session's focus-id -> input 2665 // map with this frame's addresses, so the web/wasm form handling can 2666 // find them by focus id 2667 if (self.threadForm()) |form| { 2668 const failure = if (self.session.formFeedback(feedback_tag)) |saved| saved.failure else null; 2669 (try formField(form, "title")).options.top_label.text = if (failure == .required_title) " title (required) " else " title "; 2670 if (comptime supports_drafts) { 2671 if (formWidget(form, .patch_source)) |source| { 2672 source.field().options.top_label.text = if (failure == .invalid_source_branch) " source branch (not found) " else if (failure == .same_branch) " source branch (same as target) " else if (failure == .unrelated_branches) " source branch (unrelated) " else " source branch "; 2673 const submit = formWidget(form, .submit_button) orelse return error.MissingFormField; 2674 try submit.setLabel(allocator, switch (source.selectedKind()) { 2675 .fork => "submit draft", 2676 .branch => "submit patch", 2677 }); 2678 } 2679 (try formField(form, "target_branch")).options.top_label.text = 2680 if (failure == .invalid_target_branch) " target branch (invalid) " else if (self.data.view == .edit and failure == .same_branch) " target branch (same as source) " else if (self.data.view == .edit and failure == .unrelated_branches) " target branch (unrelated to source) " else if (self.data.view == .edit and failure == .invalid_source_branch) " target branch (source branch not found) " else " target branch "; 2681 } 2682 } 2683 if (self.formBox()) |form| { 2684 const inputs_arena = self.session.arena.allocator(); 2685 for (form.children.values()) |*child| switch (child.widget) { 2686 .text_input => |*ti| try self.session.text_inputs.put(inputs_arena, ti.getFocus().id, ti), 2687 else => {}, 2688 }; 2689 } 2690 2691 try self.box.build(allocator, constraint, root_focus); 2692 } 2693 2694 pub fn input(self: *This, allocator: std.mem.Allocator, raw_key: Key, root_focus: *Focus) !void { 2695 // wheel input moves focus like the arrows 2696 const key: Key = switch (raw_key) { 2697 .scroll_up => .arrow_up, 2698 .scroll_down => .arrow_down, 2699 else => raw_key, 2700 }; 2701 2702 if (self.headerActive()) { 2703 // down from the sub-header re-enters the stack if the selected 2704 // view has something to focus; enter in the search box submits 2705 // the query; other keys move within the sub-header. 2706 if (inp.vertDirection(key) == .down) { 2707 const enterable = if (self.stackSelectedIndex()) |idx| 2708 if (idx == labels_view_index) 2709 !labelsEmpty(self.data) 2710 else if (idx == form_view_index) 2711 // nothing focusable in the unauthorized view 2712 self.viewStack().children.values()[idx] != .unauthorized 2713 else 2714 !windowEmpty(self.window(idx)) 2715 else 2716 false; 2717 if (enterable) root_focus.setFocus(self.box.children.keys()[stack_index]); 2718 } else if (key == .enter) { 2719 if (try self.header().submittedText(allocator)) |text| { 2720 defer allocator.free(text); 2721 try self.submitSearch(text); 2722 } 2723 } else { 2724 try self.header().input(allocator, key, root_focus); 2725 } 2726 return; 2727 } 2728 if (self.labelsViewActive()) { 2729 self.labelsViewInput(key, root_focus); 2730 return; 2731 } 2732 if (self.formViewActive()) { 2733 if (confirmation(self.data.view) != null) { 2734 try self.confirmationFormInput(allocator, key, root_focus); 2735 } else if (supports_conflicts and self.data.view == .resolve) { 2736 try self.resolveInput(allocator, key, root_focus); 2737 } else { 2738 try self.formInput(allocator, key, root_focus); 2739 } 2740 return; 2741 } 2742 const i = self.selectedSplitIndex() orelse return; 2743 if (self.detailActive(i)) { 2744 // the way back to the list, by enter or a click 2745 if (widget.listBackId(self.detailFrame(i))) |id| { 2746 if (inp.activated(root_focus, id, key)) return self.focusList(i, root_focus); 2747 if (root_focus.grandchild_id == id) return switch (key) { 2748 .arrow_left => self.focusList(i, root_focus), 2749 .arrow_up => self.focusHeader(root_focus), 2750 .arrow_down => _ = self.detail(i).focusFirst(root_focus), 2751 else => {}, 2752 }; 2753 } 2754 switch (try self.detail(i).inputWithExit(allocator, key, root_focus)) { 2755 .none => {}, 2756 .list => self.focusList(i, root_focus), 2757 .header => if (widget.listBackId(self.detailFrame(i))) |id| root_focus.setFocus(id) else self.focusHeader(root_focus), 2758 } 2759 } else { 2760 try self.listInput(allocator, i, key, root_focus); 2761 } 2762 } 2763 2764 // navigate to the typed query's results, keeping the status list the 2765 // page shows. an empty box clears a filtered page and does nothing on 2766 // any other one. 2767 fn submitSearch(self: *This, text: []const u8) !void { 2768 if (text.len == 0 and self.data.search == null) return; 2769 const selected = self.header().getSelectedIndex() orelse 0; 2770 const status: Status = if (selected < status_count) splitStatus(selected) else @fromBackingInt(@intCast(0)); 2771 const route = listRoute(self.data.identity, status, "") orelse return; 2772 try self.session.navigate(route.withSearch(text) orelse return); 2773 } 2774 2775 // arrow keys move the label selection; up from the top row crosses to the 2776 // header tabs. 2777 fn labelsViewInput(self: *This, key: Key, root_focus: *Focus) void { 2778 const tf = self.labelsView(); 2779 const cid = tf.focus.child_id orelse return; 2780 const cur = tf.indexOfFocusId(cid) orelse return; 2781 const count = tf.text_boxes.items.len; 2782 switch (key) { 2783 .arrow_left => if (cur > 0) root_focus.setFocus(tf.text_boxes.items[cur - 1].getFocus().id), 2784 .arrow_right => if (cur + 1 < count) root_focus.setFocus(tf.text_boxes.items[cur + 1].getFocus().id), 2785 .arrow_up => if (tf.rowStep(cur, false)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id) else self.focusHeader(root_focus), 2786 .arrow_down => if (tf.rowStep(cur, true)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id), 2787 .home => root_focus.setFocus(tf.text_boxes.items[0].getFocus().id), 2788 .end => root_focus.setFocus(tf.text_boxes.items[count - 1].getFocus().id), 2789 else => {}, 2790 } 2791 } 2792 2793 fn listInput(self: *This, allocator: std.mem.Allocator, index: usize, key: Key, root_focus: *Focus) !void { 2794 // up/down (and the scroll wheel) move the selection a row; page up/down 2795 // jump a fixed amount. right/Enter cross into the detail pane. up from 2796 // the top row crosses into the header tabs. 2797 if (inp.rowDelta(key, @intCast(self.listBox(index).children.count()))) |delta| { 2798 const lb = self.listBox(index); 2799 const at_top = if (lb.getFocus().child_id) |cid| lb.children.getIndex(cid) == 0 else true; 2800 if (delta < 0 and at_top) return self.focusHeader(root_focus); 2801 moveRowFocus(lb, self.listScroll(index), root_focus, delta); 2802 return; 2803 } 2804 switch (key) { 2805 .enter, .arrow_right => self.focusDetail(index, root_focus), 2806 // when the window is too narrow to lay out the detail pane beside 2807 // the list, a click on a row opens it like enter. the row was just 2808 // selected, so the detail is swapped to it ahead of the build. 2809 .mouse => |mouse| if (self.resultsBox(index).children.values()[detail_index].rect == null and widget.clickOnSelectedRow(self.listBox(index), root_focus, mouse)) { 2810 try self.refreshDetail(allocator, index); 2811 self.focusDetail(index, root_focus); 2812 }, 2813 else => {}, 2814 } 2815 } 2816 2817 fn formInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2818 const form = self.formBox() orelse return; 2819 const cid = form.getFocus().child_id orelse return; 2820 const cur = form.children.getIndex(cid) orelse return; 2821 const child = &form.children.values()[cur]; 2822 2823 if (child.widget == .text_input) { 2824 const text_input = &child.widget.text_input; 2825 if (text_input.options.multiline and try multilineKeepsKey(text_input, allocator, key)) 2826 return child.widget.input(allocator, key, root_focus); 2827 } 2828 2829 const on_submit = child.widget == .submit_button; 2830 switch (key) { 2831 .arrow_up, .back_tab => return if (formStep(form, cur, false)) |i| 2832 root_focus.setFocus(form.children.keys()[i]) 2833 else 2834 self.focusHeader(root_focus), 2835 .arrow_down, .tab => return if (formStep(form, cur, true)) |i| root_focus.setFocus(form.children.keys()[i]), 2836 .enter => if (on_submit) return self.submitForm(allocator), 2837 .mouse => |mouse| if (on_submit) { 2838 if (inp.leftClickOn(root_focus, child.widget.submit_button.buttonId(), mouse)) return self.submitForm(allocator); 2839 }, 2840 else => {}, 2841 } 2842 try child.widget.input(allocator, key, root_focus); 2843 } 2844 2845 fn confirmationFormInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2846 const form = self.confirmationForm() orelse return; 2847 const child_id = form.getFocus().child_id orelse return; 2848 const index = form.children.getIndex(child_id) orelse return; 2849 switch (key) { 2850 .arrow_up, .back_tab => if (index > 1) 2851 root_focus.setFocus(form.children.keys()[index - 1]) 2852 else 2853 self.focusHeader(root_focus), 2854 .arrow_down, .tab => if (index + 1 < form.children.count()) root_focus.setFocus(form.children.keys()[index + 1]), 2855 .enter => try self.submitConfirmation(allocator, index), 2856 .mouse => |mouse| for (form.children.keys()[1..], 1..) |button_id, button_index| { 2857 if (inp.leftClickOn(root_focus, button_id, mouse)) { 2858 try self.submitConfirmation(allocator, button_index); 2859 break; 2860 } 2861 }, 2862 else => {}, 2863 } 2864 } 2865 2866 // the multiline hunk inputs keep enter and any up/down that has a row to 2867 // move to; enter on a "use this" link is a navigation the host follows 2868 // before this runs. 2869 fn resolveInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2870 const form = self.formBox() orelse return; 2871 const cid = form.getFocus().child_id orelse return; 2872 const cur = form.children.getIndex(cid) orelse return; 2873 const child = &form.children.values()[cur]; 2874 2875 if (child.widget == .text_input) { 2876 const ti = &child.widget.text_input; 2877 if (ti.options.multiline and try multilineKeepsKey(ti, allocator, key)) 2878 return child.widget.input(allocator, key, root_focus); 2879 } 2880 2881 const on_submit = child.widget == .submit_button; 2882 2883 switch (key) { 2884 .arrow_up, .back_tab => if (formStep(form, cur, false)) |i| self.focusResolveChild(form, i, root_focus) else self.focusHeader(root_focus), 2885 .arrow_down, .tab => if (formStep(form, cur, true)) |i| self.focusResolveChild(form, i, root_focus), 2886 .arrow_left, .arrow_right => switch (child.widget) { 2887 .box => |*row| _ = widget.moveInRow(row, root_focus, key == .arrow_right), 2888 else => try child.widget.input(allocator, key, root_focus), 2889 }, 2890 .enter => if (on_submit) try self.submitResolution(allocator), 2891 .mouse => |mouse| if (on_submit) { 2892 if (inp.leftClickOn(root_focus, child.widget.submit_button.buttonId(), mouse)) try self.submitResolution(allocator); 2893 }, 2894 else => try child.widget.input(allocator, key, root_focus), 2895 } 2896 } 2897 2898 // a multiline input keeps enter and any up/down that has a row to move to 2899 fn multilineKeepsKey(text_input: *wgt.TextInput, allocator: std.mem.Allocator, key: Key) !bool { 2900 return switch (key) { 2901 .enter => true, 2902 .arrow_up => !try text_input.cursorOnFirstRow(allocator), 2903 .arrow_down => !try text_input.cursorOnLastRow(allocator), 2904 else => false, 2905 }; 2906 } 2907 2908 // the neighboring form control, skipping spacers and blank rows 2909 fn formStep(form: *wgt.Box(Widget), cur: usize, down: bool) ?usize { 2910 var i = cur; 2911 while (true) { 2912 if (down) { 2913 i += 1; 2914 if (i >= form.children.count()) return null; 2915 } else { 2916 if (i == 0) return null; 2917 i -= 1; 2918 } 2919 const child = &form.children.values()[i]; 2920 if (child.hidden) continue; 2921 switch (child.widget) { 2922 .spacer, .text => continue, 2923 else => return i, 2924 } 2925 } 2926 } 2927 2928 // focus the resolve-form child at `i` (a version row focuses its selected 2929 // child) and keep it visible on the terminal 2930 fn focusResolveChild(self: *This, form: *wgt.Box(Widget), i: usize, root_focus: *Focus) void { 2931 const child = &form.children.values()[i]; 2932 switch (child.widget) { 2933 .box => |*row| root_focus.setFocus(row.getFocus().child_id orelse form.children.keys()[i]), 2934 else => root_focus.setFocus(form.children.keys()[i]), 2935 } 2936 if (self.session.is_terminal) { 2937 if (self.resolveScroll()) |sc| { 2938 if (child.rect) |rect| sc.scrollToRect(rect); 2939 } 2940 } 2941 } 2942 2943 // re-emit the conflicted thread's event with the resolved content; any 2944 // event on the thread settles its conflict entry. this is the terminal 2945 // path; the web posts the form to the resolve route. 2946 fn submitResolution(self: *This, allocator: std.mem.Allocator) !void { 2947 if (comptime wasm) return; 2948 const io = self.session.io orelse return; 2949 const src = self.data.repo_source orelse return; 2950 const entry = self.data.selectedThread() orelse return; 2951 const author = ((try self.session.authorize(self.data.identity, .write, kind)) orelse return).author; 2952 const form = self.formBox() orelse return; 2953 2954 // gather the inputs by name; the d<n> hunk inputs appear in chunk order 2955 var title: ?[]u8 = null; 2956 var labels: ?[]u8 = null; 2957 var hunks: std.ArrayList([]const u8) = .empty; 2958 defer { 2959 if (title) |t| allocator.free(t); 2960 if (labels) |t| allocator.free(t); 2961 for (hunks.items) |h| allocator.free(h); 2962 hunks.deinit(allocator); 2963 } 2964 for (form.children.values()) |*child| switch (child.widget) { 2965 .text_input => |*ti| { 2966 const text = try ti.text(allocator); 2967 errdefer allocator.free(text); 2968 if (std.mem.eql(u8, ti.options.name, "title")) { 2969 title = text; 2970 } else if (std.mem.eql(u8, ti.options.name, "labels")) { 2971 labels = text; 2972 } else { 2973 try hunks.append(allocator, text); 2974 } 2975 }, 2976 else => {}, 2977 }; 2978 2979 const id_bytes = try evt.parseEventId(entry.id); 2980 2981 switch (src.repo_kind) { 2982 inline else => |repo_kind| { 2983 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2984 defer any_repo.deinit(io, allocator); 2985 switch (any_repo) { 2986 inline else => |*repo| { 2987 var resolution: Event.Resolve = .{ 2988 .title = title, 2989 .labels = labels, 2990 .hunks = hunks.items, 2991 }; 2992 if (comptime @hasField(Event.Resolve, "theirs")) resolution.theirs = self.data.theirs_picks; 2993 const change = Event.Update{ .resolve = resolution }; 2994 Event.update(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, change, author) catch |err| switch (err) { 2995 // leave the form up for correction 2996 error.InvalidFields => return, 2997 else => |e| return e, 2998 }; 2999 }, 3000 } 3001 }, 3002 } 3003 3004 const route = listRoute(self.data.identity, entryStatus(entry.*), entry.id) orelse return; 3005 try self.session.navigate(route); 3006 } 3007 3008 // commit a new or edited comment and navigate to its permalink. this is the 3009 // terminal path; the web posts the form to its comment route. 3010 fn submitComment(self: *This, allocator: std.mem.Allocator) !void { 3011 if (comptime wasm) return; 3012 const io = self.session.io orelse return; 3013 const src = self.data.repo_source orelse return; 3014 const actor = (try self.session.authorize(self.data.identity, .read, kind)) orelse return; 3015 const author = actor.author; 3016 const form = self.formBox() orelse return; 3017 const body_input = try formField(form, "body"); 3018 const body = try body_input.text(allocator); 3019 defer allocator.free(body); 3020 if (!evt.Comment.fieldsValid(body)) return; 3021 3022 var thread_id: [evt.event_id_size * 2]u8 = undefined; 3023 @memcpy(&thread_id, self.data.selected_id); 3024 const parent_id = if (self.data.comment_id.len == 0) thread_id else blk: { 3025 var parent: [evt.event_id_size * 2]u8 = undefined; 3026 @memcpy(&parent, self.data.comment_id); 3027 break :blk parent; 3028 }; 3029 var event_id_hex: [evt.event_id_size * 2]u8 = undefined; 3030 const editing = self.data.view == .edit_comment; 3031 if (editing) { 3032 const comment_id = evt.parseEventId(self.data.comment_id) catch return; 3033 if (!try src.canModify(io, allocator, actor, .comment, &comment_id)) return; 3034 } 3035 switch (src.repo_kind) { 3036 inline else => |repo_kind| { 3037 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 3038 defer any_repo.deinit(io, allocator); 3039 switch (any_repo) { 3040 inline else => |*repo| if (editing) { 3041 const comment_id = evt.parseEventId(self.data.comment_id) catch return; 3042 try evt.Comment.update(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, kind, &thread_id, &comment_id, body, author); 3043 event_id_hex = std.fmt.bytesToHex(comment_id, .lower); 3044 } else { 3045 event_id_hex = try evt.Comment.create(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, kind, &thread_id, &parent_id, body, author); 3046 }, 3047 } 3048 }, 3049 } 3050 3051 body_input.clear(allocator); 3052 const route = ui.RoutablePage.repoThreadCommentRoute(kind, self.data.identity, &thread_id, &event_id_hex, 0) orelse return; 3053 try self.session.navigate(route); 3054 } 3055 3056 // terminal form submissions; the web posts to the forms' routes instead 3057 fn submitForm(self: *This, allocator: std.mem.Allocator) !void { 3058 switch (self.data.view) { 3059 .new_comment, .edit_comment => try self.submitComment(allocator), 3060 .edit => try self.submitEditedThread(allocator), 3061 else => try self.submitNewThread(allocator), 3062 } 3063 } 3064 3065 // commit the new thread to the repo's events branch and navigate to it. 3066 // this is the terminal path; the web posts the form to the thread route, 3067 // so the wasm side never runs (or compiles) the repo access below. 3068 fn submitNewThread(self: *This, allocator: std.mem.Allocator) !void { 3069 if (comptime wasm) return; 3070 const io = self.session.io orelse return; 3071 const src = self.data.repo_source orelse return; 3072 const actor = (try self.session.authorize(self.data.identity, .read, kind)) orelse return; 3073 3074 const form = self.threadForm() orelse return; 3075 const title_input = try formField(form, "title"); 3076 const labels_input = try formField(form, "labels"); 3077 const description_input = try formField(form, "description"); 3078 3079 const title = try title_input.text(allocator); 3080 defer allocator.free(title); 3081 const labels = try labels_input.text(allocator); 3082 defer allocator.free(labels); 3083 const description = try description_input.text(allocator); 3084 defer allocator.free(description); 3085 const target_branch = if (comptime supports_drafts) try (try formField(form, "target_branch")).text(allocator) else ""; 3086 defer if (comptime supports_drafts) allocator.free(target_branch); 3087 3088 if (!Event.fieldsValid(title, labels)) { 3089 if (!evt.titleValid(title)) try self.rememberThreadFeedback(.required_title, title, labels, description, target_branch); 3090 return; 3091 } 3092 3093 if (comptime supports_drafts) { 3094 const source = formWidget(form, .patch_source) orelse return error.MissingFormField; 3095 const source_branch = switch (source.selectedKind()) { 3096 .fork => null, 3097 .branch => try source.field().text(allocator), 3098 }; 3099 defer if (source_branch) |branch| allocator.free(branch); 3100 const event_id_hex = Data.create(self.data, self.session, allocator, actor, title, labels, description, target_branch, source_branch) catch |err| { 3101 const failure = FeedbackFailure.fromError(err) orelse return err; 3102 try self.rememberThreadFeedback(failure, title, labels, description, target_branch); 3103 return; 3104 }; 3105 title_input.clear(allocator); 3106 labels_input.clear(allocator); 3107 description_input.clear(allocator); 3108 (try formField(form, "target_branch")).clear(allocator); 3109 const route = ui.RoutablePage.repoThreadCommentsRoute(kind, self.data.identity, &event_id_hex, 0) orelse return; 3110 try self.session.navigate(route); 3111 return; 3112 } 3113 3114 var id_bytes: [evt.event_id_size]u8 = undefined; 3115 io.random(&id_bytes); 3116 const event_id_hex = std.fmt.bytesToHex(id_bytes, .lower); 3117 3118 const event = evt.EventWithId{ 3119 .id = event_id_hex, 3120 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 3121 .author = actor.author, 3122 .event = @unionInit(evt.Event, @tagName(kind), Event{ 3123 .title = title, 3124 .description = description, 3125 .labels = labels, 3126 }), 3127 }; 3128 3129 switch (src.repo_kind) { 3130 inline else => |repo_kind| { 3131 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 3132 defer any_repo.deinit(io, allocator); 3133 switch (any_repo) { 3134 inline else => |*repo| try evt.consume(self.session.eventHost(), .repo, repo_kind, repo.self_repo_opts, io, allocator, repo, evt.events_ref, &.{event}), 3135 } 3136 }, 3137 } 3138 3139 // wipe the form so a return visit starts fresh 3140 title_input.clear(allocator); 3141 labels_input.clear(allocator); 3142 description_input.clear(allocator); 3143 3144 const route = listRoute(self.data.identity, @fromBackingInt(@intCast(0)), &event_id_hex) orelse return; 3145 try self.session.navigate(route); 3146 } 3147 3148 // re-emit the selected thread's event with the form's content (status 3149 // preserved), then reload the thread's page. this is the terminal path; 3150 // the web posts the form to the edit route. 3151 fn submitEditedThread(self: *This, allocator: std.mem.Allocator) !void { 3152 if (comptime wasm) return; 3153 const io = self.session.io orelse return; 3154 const entry = self.data.selectedThread() orelse return; 3155 const actor = (try self.session.authorize(self.data.identity, .read, kind)) orelse return; 3156 const author = actor.author; 3157 3158 const form = self.threadForm() orelse return; 3159 const title_input = try formField(form, "title"); 3160 const labels_input = try formField(form, "labels"); 3161 const description_input = try formField(form, "description"); 3162 3163 const title = try title_input.text(allocator); 3164 defer allocator.free(title); 3165 const labels = try labels_input.text(allocator); 3166 defer allocator.free(labels); 3167 const description = try description_input.text(allocator); 3168 defer allocator.free(description); 3169 const target_branch = if (comptime supports_drafts) try (try formField(form, "target_branch")).text(allocator) else ""; 3170 defer if (comptime supports_drafts) allocator.free(target_branch); 3171 3172 if (!Event.fieldsValid(title, labels)) { 3173 if (!evt.titleValid(title)) try self.rememberThreadFeedback(.required_title, title, labels, description, target_branch); 3174 return; 3175 } 3176 const src = self.data.repo_source orelse return; 3177 3178 if (comptime supports_drafts) { 3179 if (entryDraft(entry.*)) { 3180 self.data.editDraft(self.session, allocator, author, entry.id, title, labels, description, target_branch) catch |err| { 3181 const failure = FeedbackFailure.fromError(err) orelse return err; 3182 try self.rememberThreadFeedback(failure, title, labels, description, target_branch); 3183 return; 3184 }; 3185 const route = listRoute(self.data.identity, entryStatus(entry.*), entry.id) orelse return; 3186 try self.session.navigate(route); 3187 return; 3188 } 3189 } 3190 3191 const id_bytes = try evt.parseEventId(entry.id); 3192 if (!try src.canModify(io, allocator, actor, kind, &id_bytes)) return; 3193 3194 switch (src.repo_kind) { 3195 inline else => |repo_kind| { 3196 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 3197 defer any_repo.deinit(io, allocator); 3198 switch (any_repo) { 3199 inline else => |*repo| if (supports_drafts) { 3200 Event.update(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 3201 .title = title, 3202 .labels = labels, 3203 .description = description, 3204 .target_branch = target_branch, 3205 } }, author) catch |err| { 3206 const failure = FeedbackFailure.fromError(err) orelse return err; 3207 try self.rememberThreadFeedback(failure, title, labels, description, target_branch); 3208 return; 3209 }; 3210 } else if (has_status) 3211 try Event.update(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 3212 .title = title, 3213 .labels = labels, 3214 .description = description, 3215 } }, author) 3216 else 3217 try Event.update(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, title, labels, description, author), 3218 } 3219 }, 3220 } 3221 3222 const route = listRoute(self.data.identity, entryStatus(entry.*), entry.id) orelse return; 3223 try self.session.navigate(route); 3224 } 3225 3226 fn rememberThreadFeedback(self: *This, failure: FeedbackFailure, title: []const u8, labels: []const u8, description: []const u8, target_branch: []const u8) !void { 3227 const aa = self.session.arena.allocator(); 3228 const source = if (supports_drafts) blk: { 3229 const form = self.threadForm() orelse break :blk null; 3230 break :blk formWidget(form, .patch_source); 3231 } else null; 3232 self.session.data.form_feedback = @unionInit(ui.Session.FormFeedback, @tagName(feedback_tag), .{ 3233 .failure = failure, 3234 .fields = if (comptime supports_drafts) .{ 3235 .title = try aa.dupe(u8, title), 3236 .labels = try aa.dupe(u8, labels), 3237 .description = try aa.dupe(u8, description), 3238 .target_branch = try aa.dupe(u8, target_branch), 3239 .source_branch = if (source) |value| try value.field().text(aa) else "", 3240 .source_kind = if (source) |value| value.selectedKind() else .fork, 3241 } else .{ 3242 .title = try aa.dupe(u8, title), 3243 .labels = try aa.dupe(u8, labels), 3244 .description = try aa.dupe(u8, description), 3245 }, 3246 }); 3247 } 3248 3249 fn submitConfirmation(self: *This, allocator: std.mem.Allocator, button_index: usize) !void { 3250 switch (confirmation(self.data.view) orelse return) { 3251 .publish => if (comptime supports_drafts) { 3252 if (comptime wasm) return; 3253 const entry = self.data.selectedThread() orelse return; 3254 if (!entryDraft(entry.*)) return; 3255 const author = ((try self.session.authorize(self.data.identity, .read, kind)) orelse return).author; 3256 try self.data.publishDraft(self.session, allocator, author, entry.id); 3257 const route = listRoute(self.data.identity, @fromBackingInt(@intCast(0)), entry.id) orelse return; 3258 try self.session.navigate(route); 3259 } else unreachable, 3260 .merge => if (comptime supports_merge) { 3261 if (comptime wasm) return; 3262 const entry = self.data.selectedThread() orelse return; 3263 const revision: evt.Patch.MergeRevision = if (button_index > 1) .squash else .source; 3264 if (!Data.canMerge(entry.*, self.session) or entry.mergeability.get(revision) != .clean) return; 3265 const author = ((try self.session.authorize(self.data.identity, .write, kind)) orelse return).author; 3266 self.data.mergePatch(self.session, allocator, author, entry.id, revision) catch |err| switch (err) { 3267 error.MergeConflict, error.MergeCheckUnavailable, error.PatchOutOfDate, error.PatchDataUnavailable => { 3268 const route = listRoute(self.data.identity, entryStatus(entry.*), entry.id) orelse return; 3269 try self.session.navigate(route); 3270 return; 3271 }, 3272 else => return err, 3273 }; 3274 const merged = std.meta.stringToEnum(Status, "merged") orelse return; 3275 const route = listRoute(self.data.identity, merged, entry.id) orelse return; 3276 try self.session.navigate(route); 3277 } else unreachable, 3278 .remove => try self.submitRemove(allocator), 3279 } 3280 } 3281 3282 fn submitRemove(self: *This, allocator: std.mem.Allocator) !void { 3283 const comment = self.data.comment_id.len != 0; 3284 const event_id = if (comment) self.data.comment_id else self.data.selected_id; 3285 const id = evt.parseEventId(event_id) catch return; 3286 try self.removeEvent(allocator, if (comment) .comment else kind, id); 3287 } 3288 3289 fn removeEvent(self: *This, allocator: std.mem.Allocator, event_kind: evt.EventKind, id: [evt.event_id_size]u8) !void { 3290 if (comptime wasm) return; 3291 const actor = (try self.session.authorize(self.data.identity, .read, kind)) orelse return; 3292 const author = actor.author; 3293 3294 if (comptime supports_drafts) { 3295 const selected = self.data.selectedThread() orelse return; 3296 if (event_kind == kind and entryDraft(selected.*)) { 3297 try Data.removeDraft(self.session, allocator, author, &id); 3298 const route = draftsRoute(self.data.identity) orelse return; 3299 try self.session.navigate(route); 3300 return; 3301 } 3302 } 3303 3304 const io = self.session.io orelse return; 3305 const src = self.data.repo_source orelse return; 3306 if (!try src.canModify(io, allocator, actor, event_kind, &id)) return; 3307 const thread_id = evt.parseEventId(self.data.selected_id) catch return; 3308 3309 switch (src.repo_kind) { 3310 inline else => |repo_kind| { 3311 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 3312 defer any_repo.deinit(io, allocator); 3313 switch (any_repo) { 3314 inline else => |*repo| try evt.removeInThread(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, kind, &thread_id, event_kind, &id, author), 3315 } 3316 }, 3317 } 3318 3319 const id_hex = std.fmt.bytesToHex(id, .lower); 3320 const route = ui.RoutablePage.repoEventsRoute(self.data.identity, .removed, event_kind, &id_hex, null) orelse return; 3321 try self.session.navigate(route); 3322 } 3323 3324 // enter the detail pane, landing on the way back to the list when it shows 3325 fn focusDetail(self: *This, index: usize, root_focus: *Focus) void { 3326 if (widget.listBackId(self.detailFrame(index)) != null) return widget.focusListBack(self.detailFrame(index), root_focus); 3327 _ = self.detail(index).focusFirst(root_focus); 3328 } 3329 3330 // return to the list. 3331 fn focusList(self: *This, index: usize, root_focus: *Focus) void { 3332 root_focus.setFocus(self.listScroll(index).getFocus().id); 3333 } 3334 3335 // cross to the header tabs above the stack. 3336 fn focusHeader(self: *This, root_focus: *Focus) void { 3337 root_focus.setFocus(self.box.children.keys()[header_index]); 3338 } 3339 3340 pub fn clearGrid(self: *This) void { 3341 self.box.clearGrid(); 3342 } 3343 3344 pub fn getGrid(self: This) ?Grid { 3345 return self.box.getGrid(); 3346 } 3347 3348 pub fn getFocus(self: *This) *Focus { 3349 return self.box.getFocus(); 3350 } 3351 3352 // for the parent's "scroll up at the top jumps to the header" check: at the 3353 // top only while the header tabs hold focus, so up from the split 3354 // crosses into the tabs first. 3355 pub fn atTop(self: *This) bool { 3356 return self.headerActive(); 3357 } 3358 }; 3359 } 3360