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