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