╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ thread.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2275 if (idx < lead or idx - lead >= win.items.len) return null; 2276 return idx - lead; 2277 } 2278 2279 pub fn build(self: *This, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2280 self.clearGrid(); 2281 2282 // each header tab maps 1:1 to a stack child by position 2283 if (self.header().getSelectedIndex()) |index| { 2284 const stack = self.viewStack(); 2285 stack.getFocus().child_id = stack.children.keys()[index]; 2286 } 2287 2288 if (self.selectedSplitIndex()) |i| { 2289 // swap the detail pane to the selected thread when it changes. 2290 if (self.selectedThreadIndex(i)) |selected| { 2291 const entry = self.window(i).items[selected]; 2292 const changed = if (self.detail(i).entry) |current| !std.mem.eql(u8, current.id, entry.id) else true; 2293 if (changed) { 2294 try self.detail(i).setEntry(allocator, entry); 2295 } 2296 } 2297 2298 // the selected list row shows a border (the focused TextBox 2299 // upgrades it to a double border itself); the rest stay borderless. 2300 const lb = self.listBox(i); 2301 for (lb.children.keys(), lb.children.values()) |id, *child| { 2302 switch (child.widget) { 2303 .text_box => |*tb| widget.markSelected(tb, lb.getFocus().child_id == id), 2304 else => {}, 2305 } 2306 } 2307 2308 // cap the list at list_max_width only while the detail pane fits 2309 // beside it. the box drops the detail when the width can't hold 2310 // both minimums, so when it's that narrow we lift the cap and let 2311 // the list fill the whole width. 2312 const both_panes_fit = if (constraint.max_size.width) |w| w >= list_max_width + detail_min_width else true; 2313 self.resultsBox(i).children.values()[list_index].max_size = if (both_panes_fit) .{ .width = list_max_width, .height = null } else null; 2314 2315 // stretch the detail pane across the rest of the width so it fills 2316 // the area rather than shrinking to its content; its scroll fills 2317 // the pane. 2318 if (constraint.max_size.width) |w| { 2319 self.resultsBox(i).children.values()[detail_index].min_size = .{ .width = if (both_panes_fit) w - list_max_width else w, .height = null }; 2320 } else { 2321 self.resultsBox(i).children.values()[detail_index].min_size = .{ .width = detail_min_width, .height = null }; 2322 } 2323 } 2324 2325 // refresh the form inputs' entries in the session's focus-id -> input 2326 // map with this frame's addresses, so the web/wasm form handling can 2327 // find them by focus id 2328 if (self.threadForm()) |form| { 2329 const failure = if (self.session.formFeedback(feedback_tag)) |saved| saved.failure else null; 2330 (try formField(form, "title")).options.label = if (failure == .required_title) " title (required) " else " title "; 2331 if (comptime supports_drafts) { 2332 if (formWidget(form, .patch_source)) |source| { 2333 source.field().options.label = 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 "; 2334 const submit = formWidget(form, .submit_button) orelse return error.MissingFormField; 2335 try submit.setLabel(allocator, switch (source.selectedKind()) { 2336 .fork => "submit draft", 2337 .branch => "submit patch", 2338 }); 2339 } 2340 (try formField(form, "target_branch")).options.label = 2341 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 "; 2342 } 2343 } 2344 if (self.formBox()) |form| { 2345 const inputs_arena = self.session.arena.allocator(); 2346 for (form.children.values()) |*child| switch (child.widget) { 2347 .text_input => |*ti| try self.session.text_inputs.put(inputs_arena, ti.getFocus().id, ti), 2348 else => {}, 2349 }; 2350 } 2351 2352 try self.box.build(allocator, constraint, root_focus); 2353 } 2354 2355 pub fn input(self: *This, allocator: std.mem.Allocator, raw_key: Key, root_focus: *Focus) !void { 2356 // wheel input moves focus like the arrows 2357 const key: Key = if (raw_key == .mouse and raw_key.mouse.action == .scroll) 2358 (if (raw_key.mouse.action.scroll == .up) .arrow_up else .arrow_down) 2359 else 2360 raw_key; 2361 2362 if (self.headerActive()) { 2363 // down from the sub-header re-enters the stack if the selected 2364 // view has something to focus; enter in the search box submits 2365 // the query; other keys move within the sub-header. 2366 if (inp.vertDirection(key) == .down) { 2367 const enterable = if (self.viewStack().getSelected()) |selected| switch (selected.*) { 2368 .word_flow => |*tf| tf.text_boxes.items.len > 0, 2369 // nothing focusable in the unauthorized view 2370 .unauthorized => false, 2371 else => true, 2372 } else false; 2373 if (enterable) root_focus.setFocus(self.box.children.keys()[stack_index]); 2374 } else if (key == .enter) { 2375 if (try self.header().submittedText(allocator)) |text| { 2376 defer allocator.free(text); 2377 try self.submitSearch(text); 2378 } 2379 } else { 2380 try self.header().input(allocator, key, root_focus); 2381 } 2382 return; 2383 } 2384 if (self.labelsViewActive()) { 2385 self.labelsViewInput(key, root_focus); 2386 return; 2387 } 2388 if (self.formViewActive()) { 2389 if (confirmation(self.data.view) != null) { 2390 try self.confirmationFormInput(allocator, key, root_focus); 2391 } else if (supports_conflicts and self.data.view == .resolve) { 2392 try self.resolveInput(allocator, key, root_focus); 2393 } else { 2394 try self.formInput(allocator, key, root_focus); 2395 } 2396 return; 2397 } 2398 const i = self.selectedSplitIndex() orelse return; 2399 if (self.detailActive(i)) { 2400 switch (try self.detail(i).inputWithExit(allocator, key, root_focus)) { 2401 .none => {}, 2402 .list => self.focusList(i, root_focus), 2403 .header => self.focusHeader(root_focus), 2404 } 2405 } else { 2406 self.listInput(i, key, root_focus); 2407 } 2408 } 2409 2410 // navigate to the typed query's results, keeping the label filter and the 2411 // status list the page shows. an empty box clears a filtered page and 2412 // does nothing on any other one. 2413 fn submitSearch(self: *This, text: []const u8) !void { 2414 if (text.len == 0 and self.data.search == null) return; 2415 const selected = self.header().getSelectedIndex() orelse 0; 2416 const status: Status = if (selected < status_count) splitStatus(selected) else @enumFromInt(0); 2417 const route = listRoute(self.data.identity, status, self.data.label, "") orelse return; 2418 try self.session.navigate(route.withSearch(text) orelse return); 2419 } 2420 2421 // arrow keys move the label selection; up from the top row crosses to the 2422 // header tabs. 2423 fn labelsViewInput(self: *This, key: Key, root_focus: *Focus) void { 2424 const tf = self.labelsView(); 2425 const cid = tf.focus.child_id orelse return; 2426 const cur = tf.indexOfFocusId(cid) orelse return; 2427 const count = tf.text_boxes.items.len; 2428 switch (key) { 2429 .arrow_left => if (cur > 0) root_focus.setFocus(tf.text_boxes.items[cur - 1].getFocus().id), 2430 .arrow_right => if (cur + 1 < count) root_focus.setFocus(tf.text_boxes.items[cur + 1].getFocus().id), 2431 .arrow_up => if (tf.rowStep(cur, false)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id) else self.focusHeader(root_focus), 2432 .arrow_down => if (tf.rowStep(cur, true)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id), 2433 .home => root_focus.setFocus(tf.text_boxes.items[0].getFocus().id), 2434 .end => root_focus.setFocus(tf.text_boxes.items[count - 1].getFocus().id), 2435 else => {}, 2436 } 2437 } 2438 2439 fn listInput(self: *This, index: usize, key: Key, root_focus: *Focus) void { 2440 // up/down (and the scroll wheel) move the selection a row; page up/down 2441 // jump a fixed amount. right/Enter cross into the detail pane. up from 2442 // the top row crosses into the header tabs. 2443 if (inp.rowDelta(key, @intCast(self.listBox(index).children.count()))) |delta| { 2444 const lb = self.listBox(index); 2445 const at_top = if (lb.getFocus().child_id) |cid| lb.children.getIndex(cid) == 0 else true; 2446 if (delta < 0 and at_top) return self.focusHeader(root_focus); 2447 moveRowFocus(lb, self.listScroll(index), root_focus, delta); 2448 return; 2449 } 2450 switch (key) { 2451 .enter, .arrow_right => _ = self.detail(index).focusFirst(root_focus), 2452 else => {}, 2453 } 2454 } 2455 2456 fn formInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2457 const form = self.formBox() orelse return; 2458 const cid = form.getFocus().child_id orelse return; 2459 const cur = form.children.getIndex(cid) orelse return; 2460 const child = &form.children.values()[cur]; 2461 2462 if (child.widget == .text_input) { 2463 const text_input = &child.widget.text_input; 2464 if (text_input.options.multiline and try multilineKeepsKey(text_input, allocator, key)) 2465 return child.widget.input(allocator, key, root_focus); 2466 } 2467 2468 const on_submit = child.widget == .submit_button; 2469 switch (key) { 2470 .arrow_up, .back_tab => return if (formStep(form, cur, false)) |i| 2471 root_focus.setFocus(form.children.keys()[i]) 2472 else 2473 self.focusHeader(root_focus), 2474 .arrow_down, .tab => return if (formStep(form, cur, true)) |i| root_focus.setFocus(form.children.keys()[i]), 2475 .enter => if (on_submit) return self.submitForm(allocator), 2476 .mouse => |mouse| if (on_submit) { 2477 if (inp.leftClickOn(root_focus, child.widget.submit_button.buttonId(), mouse)) return self.submitForm(allocator); 2478 }, 2479 else => {}, 2480 } 2481 try child.widget.input(allocator, key, root_focus); 2482 } 2483 2484 fn confirmationFormInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2485 const form = self.confirmationForm() orelse return; 2486 const child_id = form.getFocus().child_id orelse return; 2487 const index = form.children.getIndex(child_id) orelse return; 2488 switch (key) { 2489 .arrow_up, .back_tab => if (index > 1) 2490 root_focus.setFocus(form.children.keys()[index - 1]) 2491 else 2492 self.focusHeader(root_focus), 2493 .arrow_down, .tab => if (index + 1 < form.children.count()) root_focus.setFocus(form.children.keys()[index + 1]), 2494 .enter => try self.submitConfirmation(allocator, index), 2495 .mouse => |mouse| for (form.children.keys()[1..], 1..) |button_id, button_index| { 2496 if (inp.leftClickOn(root_focus, button_id, mouse)) { 2497 try self.submitConfirmation(allocator, button_index); 2498 break; 2499 } 2500 }, 2501 else => {}, 2502 } 2503 } 2504 2505 // the multiline hunk inputs keep enter and any up/down that has a row to 2506 // move to; enter on a "use this" link is a navigation the host follows 2507 // before this runs. 2508 fn resolveInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2509 const form = self.formBox() orelse return; 2510 const cid = form.getFocus().child_id orelse return; 2511 const cur = form.children.getIndex(cid) orelse return; 2512 const child = &form.children.values()[cur]; 2513 2514 if (child.widget == .text_input) { 2515 const ti = &child.widget.text_input; 2516 if (ti.options.multiline and try multilineKeepsKey(ti, allocator, key)) 2517 return child.widget.input(allocator, key, root_focus); 2518 } 2519 2520 const on_submit = child.widget == .submit_button; 2521 2522 switch (key) { 2523 .arrow_up, .back_tab => if (formStep(form, cur, false)) |i| self.focusResolveChild(form, i, root_focus) else self.focusHeader(root_focus), 2524 .arrow_down, .tab => if (formStep(form, cur, true)) |i| self.focusResolveChild(form, i, root_focus), 2525 .arrow_left, .arrow_right => switch (child.widget) { 2526 .box => |*row| _ = widget.moveInRow(row, root_focus, key == .arrow_right), 2527 else => try child.widget.input(allocator, key, root_focus), 2528 }, 2529 .enter => if (on_submit) try self.submitResolution(allocator), 2530 .mouse => |mouse| if (on_submit) { 2531 if (inp.leftClickOn(root_focus, child.widget.submit_button.buttonId(), mouse)) try self.submitResolution(allocator); 2532 }, 2533 else => try child.widget.input(allocator, key, root_focus), 2534 } 2535 } 2536 2537 // a multiline input keeps enter and any up/down that has a row to move to 2538 fn multilineKeepsKey(text_input: *wgt.TextInput, allocator: std.mem.Allocator, key: Key) !bool { 2539 return switch (key) { 2540 .enter => true, 2541 .arrow_up => !try text_input.cursorOnFirstRow(allocator), 2542 .arrow_down => !try text_input.cursorOnLastRow(allocator), 2543 else => false, 2544 }; 2545 } 2546 2547 // the neighboring form control, skipping spacers and blank rows 2548 fn formStep(form: *wgt.Box(Widget), cur: usize, down: bool) ?usize { 2549 var i = cur; 2550 while (true) { 2551 if (down) { 2552 i += 1; 2553 if (i >= form.children.count()) return null; 2554 } else { 2555 if (i == 0) return null; 2556 i -= 1; 2557 } 2558 const child = &form.children.values()[i]; 2559 if (child.hidden) continue; 2560 switch (child.widget) { 2561 .spacer, .text => continue, 2562 else => return i, 2563 } 2564 } 2565 } 2566 2567 // focus the resolve-form child at `i` (a version row focuses its selected 2568 // child) and keep it visible on the terminal 2569 fn focusResolveChild(self: *This, form: *wgt.Box(Widget), i: usize, root_focus: *Focus) void { 2570 const child = &form.children.values()[i]; 2571 switch (child.widget) { 2572 .box => |*row| root_focus.setFocus(row.getFocus().child_id orelse form.children.keys()[i]), 2573 else => root_focus.setFocus(form.children.keys()[i]), 2574 } 2575 if (self.session.is_terminal) { 2576 if (self.resolveScroll()) |sc| { 2577 if (child.rect) |rect| sc.scrollToRect(rect); 2578 } 2579 } 2580 } 2581 2582 // re-emit the conflicted thread's event with the resolved content; any 2583 // event on the thread settles its conflict entry. this is the terminal 2584 // path; the web posts the form to the resolve route. 2585 fn submitResolution(self: *This, allocator: std.mem.Allocator) !void { 2586 if (comptime wasm) return; 2587 const io = self.session.io orelse return; 2588 const src = self.data.repo_source orelse return; 2589 const entry = self.data.selectedThread() orelse return; 2590 const author = ((try self.session.authorize(self.data.identity, .write)) orelse return).author; 2591 const form = self.formBox() orelse return; 2592 2593 // gather the inputs by name; the d<n> hunk inputs appear in chunk order 2594 var title: ?[]u8 = null; 2595 var labels: ?[]u8 = null; 2596 var hunks: std.ArrayList([]const u8) = .empty; 2597 defer { 2598 if (title) |t| allocator.free(t); 2599 if (labels) |t| allocator.free(t); 2600 for (hunks.items) |h| allocator.free(h); 2601 hunks.deinit(allocator); 2602 } 2603 for (form.children.values()) |*child| switch (child.widget) { 2604 .text_input => |*ti| { 2605 const text = try ti.text(allocator); 2606 errdefer allocator.free(text); 2607 if (std.mem.eql(u8, ti.options.name, "title")) { 2608 title = text; 2609 } else if (std.mem.eql(u8, ti.options.name, "labels")) { 2610 labels = text; 2611 } else { 2612 try hunks.append(allocator, text); 2613 } 2614 }, 2615 else => {}, 2616 }; 2617 2618 const id_bytes = try evt.parseEventId(entry.id); 2619 2620 switch (src.repo_kind) { 2621 inline else => |repo_kind| { 2622 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2623 defer any_repo.deinit(io, allocator); 2624 switch (any_repo) { 2625 inline else => |*repo| { 2626 var resolution: Event.Resolve = .{ 2627 .title = title, 2628 .labels = labels, 2629 .hunks = hunks.items, 2630 }; 2631 if (comptime @hasField(Event.Resolve, "theirs")) resolution.theirs = self.data.theirs_picks; 2632 const change = Event.Update{ .resolve = resolution }; 2633 Event.update(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, change, author) catch |err| switch (err) { 2634 // leave the form up for correction 2635 error.InvalidFields => return, 2636 else => |e| return e, 2637 }; 2638 }, 2639 } 2640 }, 2641 } 2642 2643 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2644 try self.session.navigate(route); 2645 } 2646 2647 // commit a new or edited comment and navigate to its permalink. this is the 2648 // terminal path; the web posts the form to its comment route. 2649 fn submitComment(self: *This, allocator: std.mem.Allocator) !void { 2650 if (comptime wasm) return; 2651 const io = self.session.io orelse return; 2652 const src = self.data.repo_source orelse return; 2653 const actor = (try self.session.authorize(self.data.identity, .read)) orelse return; 2654 const author = actor.author; 2655 const form = self.formBox() orelse return; 2656 const body_input = try formField(form, "body"); 2657 const body = try body_input.text(allocator); 2658 defer allocator.free(body); 2659 if (!evt.Comment.fieldsValid(body)) return; 2660 2661 var thread_id: [evt.event_id_size * 2]u8 = undefined; 2662 @memcpy(&thread_id, self.data.selected_id); 2663 const parent_id = if (self.data.comment_id.len == 0) thread_id else blk: { 2664 var parent: [evt.event_id_size * 2]u8 = undefined; 2665 @memcpy(&parent, self.data.comment_id); 2666 break :blk parent; 2667 }; 2668 var event_id_hex: [evt.event_id_size * 2]u8 = undefined; 2669 const editing = self.data.view == .edit_comment; 2670 if (editing) { 2671 const comment_id = evt.parseEventId(self.data.comment_id) catch return; 2672 if (!try src.canModify(io, allocator, actor, .comment, &comment_id)) return; 2673 } 2674 switch (src.repo_kind) { 2675 inline else => |repo_kind| { 2676 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2677 defer any_repo.deinit(io, allocator); 2678 switch (any_repo) { 2679 inline else => |*repo| if (editing) { 2680 const comment_id = evt.parseEventId(self.data.comment_id) catch return; 2681 try evt.Comment.update(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, &thread_id, &comment_id, body, author); 2682 event_id_hex = std.fmt.bytesToHex(comment_id, .lower); 2683 } else { 2684 event_id_hex = try evt.Comment.create(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, &thread_id, &parent_id, body, author); 2685 }, 2686 } 2687 }, 2688 } 2689 2690 body_input.clear(allocator); 2691 const route = ui.RoutablePage.repoThreadCommentRoute(kind, self.data.identity, &thread_id, &event_id_hex, 0) orelse return; 2692 try self.session.navigate(route); 2693 } 2694 2695 // terminal form submissions; the web posts to the forms' routes instead 2696 fn submitForm(self: *This, allocator: std.mem.Allocator) !void { 2697 switch (self.data.view) { 2698 .new_comment, .edit_comment => try self.submitComment(allocator), 2699 .edit => try self.submitEditedThread(allocator), 2700 else => try self.submitNewThread(allocator), 2701 } 2702 } 2703 2704 // commit the new thread to the repo's events branch and navigate to it. 2705 // this is the terminal path; the web posts the form to the thread route, 2706 // so the wasm side never runs (or compiles) the repo access below. 2707 fn submitNewThread(self: *This, allocator: std.mem.Allocator) !void { 2708 if (comptime wasm) return; 2709 const io = self.session.io orelse return; 2710 const src = self.data.repo_source orelse return; 2711 const actor = (try self.session.authorize(self.data.identity, .read)) orelse return; 2712 2713 const form = self.threadForm() orelse return; 2714 const title_input = try formField(form, "title"); 2715 const labels_input = try formField(form, "labels"); 2716 const description_input = try formField(form, "description"); 2717 2718 const title = try title_input.text(allocator); 2719 defer allocator.free(title); 2720 const labels = try labels_input.text(allocator); 2721 defer allocator.free(labels); 2722 const description = try description_input.text(allocator); 2723 defer allocator.free(description); 2724 const target_branch = if (comptime supports_drafts) try (try formField(form, "target_branch")).text(allocator) else ""; 2725 defer if (comptime supports_drafts) allocator.free(target_branch); 2726 2727 if (!Event.fieldsValid(title, labels)) { 2728 if (!evt.titleValid(title)) try self.rememberThreadFeedback(.required_title, title, labels, description, target_branch); 2729 return; 2730 } 2731 2732 if (comptime supports_drafts) { 2733 const source = formWidget(form, .patch_source) orelse return error.MissingFormField; 2734 const source_branch = switch (source.selectedKind()) { 2735 .fork => null, 2736 .branch => try source.field().text(allocator), 2737 }; 2738 defer if (source_branch) |branch| allocator.free(branch); 2739 const event_id_hex = Data.create(self.data, self.session, allocator, actor, title, labels, description, target_branch, source_branch) catch |err| { 2740 const failure = FeedbackFailure.fromError(err) orelse return err; 2741 try self.rememberThreadFeedback(failure, title, labels, description, target_branch); 2742 return; 2743 }; 2744 title_input.clear(allocator); 2745 labels_input.clear(allocator); 2746 description_input.clear(allocator); 2747 (try formField(form, "target_branch")).clear(allocator); 2748 const route = ui.RoutablePage.repoThreadCommentsRoute(kind, self.data.identity, &event_id_hex, 0) orelse return; 2749 try self.session.navigate(route); 2750 return; 2751 } 2752 2753 var id_bytes: [evt.event_id_size]u8 = undefined; 2754 io.random(&id_bytes); 2755 const event_id_hex = std.fmt.bytesToHex(id_bytes, .lower); 2756 2757 const event = evt.EventWithId{ 2758 .id = event_id_hex, 2759 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 2760 .author = actor.author, 2761 .event = @unionInit(evt.Event, @tagName(kind), Event{ 2762 .title = title, 2763 .description = description, 2764 .labels = labels, 2765 }), 2766 }; 2767 2768 switch (src.repo_kind) { 2769 inline else => |repo_kind| { 2770 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2771 defer any_repo.deinit(io, allocator); 2772 switch (any_repo) { 2773 inline else => |*repo| try evt.consume(self.session.eventHost(), .repo, repo_kind, repo.self_repo_opts, io, allocator, repo, evt.events_ref, &.{event}), 2774 } 2775 }, 2776 } 2777 2778 // wipe the form so a return visit starts fresh 2779 title_input.clear(allocator); 2780 labels_input.clear(allocator); 2781 description_input.clear(allocator); 2782 2783 const route = listRoute(self.data.identity, @enumFromInt(0), "", &event_id_hex) orelse return; 2784 try self.session.navigate(route); 2785 } 2786 2787 // re-emit the selected thread's event with the form's content (status 2788 // preserved), then reload the thread's page. this is the terminal path; 2789 // the web posts the form to the edit route. 2790 fn submitEditedThread(self: *This, allocator: std.mem.Allocator) !void { 2791 if (comptime wasm) return; 2792 const io = self.session.io orelse return; 2793 const entry = self.data.selectedThread() orelse return; 2794 const actor = (try self.session.authorize(self.data.identity, .read)) orelse return; 2795 const author = actor.author; 2796 2797 const form = self.threadForm() orelse return; 2798 const title_input = try formField(form, "title"); 2799 const labels_input = try formField(form, "labels"); 2800 const description_input = try formField(form, "description"); 2801 2802 const title = try title_input.text(allocator); 2803 defer allocator.free(title); 2804 const labels = try labels_input.text(allocator); 2805 defer allocator.free(labels); 2806 const description = try description_input.text(allocator); 2807 defer allocator.free(description); 2808 const target_branch = if (comptime supports_drafts) try (try formField(form, "target_branch")).text(allocator) else ""; 2809 defer if (comptime supports_drafts) allocator.free(target_branch); 2810 2811 if (!Event.fieldsValid(title, labels)) { 2812 if (!evt.titleValid(title)) try self.rememberThreadFeedback(.required_title, title, labels, description, target_branch); 2813 return; 2814 } 2815 const src = self.data.repo_source orelse return; 2816 2817 if (comptime supports_drafts) { 2818 if (entryDraft(entry.*)) { 2819 self.data.editDraft(self.session, allocator, author, entry.id, title, labels, description, target_branch) catch |err| { 2820 const failure = FeedbackFailure.fromError(err) orelse return err; 2821 try self.rememberThreadFeedback(failure, title, labels, description, target_branch); 2822 return; 2823 }; 2824 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2825 try self.session.navigate(route); 2826 return; 2827 } 2828 } 2829 2830 const id_bytes = try evt.parseEventId(entry.id); 2831 if (!try src.canModify(io, allocator, actor, kind, &id_bytes)) return; 2832 2833 switch (src.repo_kind) { 2834 inline else => |repo_kind| { 2835 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2836 defer any_repo.deinit(io, allocator); 2837 switch (any_repo) { 2838 inline else => |*repo| if (supports_drafts) { 2839 Event.update(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 2840 .title = title, 2841 .labels = labels, 2842 .description = description, 2843 .target_branch = target_branch, 2844 } }, author) catch |err| { 2845 const failure = FeedbackFailure.fromError(err) orelse return err; 2846 try self.rememberThreadFeedback(failure, title, labels, description, target_branch); 2847 return; 2848 }; 2849 } else if (has_status) 2850 try Event.update(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 2851 .title = title, 2852 .labels = labels, 2853 .description = description, 2854 } }, author) 2855 else 2856 try Event.update(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, title, labels, description, author), 2857 } 2858 }, 2859 } 2860 2861 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2862 try self.session.navigate(route); 2863 } 2864 2865 fn rememberThreadFeedback(self: *This, failure: FeedbackFailure, title: []const u8, labels: []const u8, description: []const u8, target_branch: []const u8) !void { 2866 const aa = self.session.arena.allocator(); 2867 const source = if (supports_drafts) blk: { 2868 const form = self.threadForm() orelse break :blk null; 2869 break :blk formWidget(form, .patch_source); 2870 } else null; 2871 self.session.data.form_feedback = @unionInit(ui.Session.FormFeedback, @tagName(feedback_tag), .{ 2872 .failure = failure, 2873 .fields = if (comptime supports_drafts) .{ 2874 .title = try aa.dupe(u8, title), 2875 .labels = try aa.dupe(u8, labels), 2876 .description = try aa.dupe(u8, description), 2877 .target_branch = try aa.dupe(u8, target_branch), 2878 .source_branch = if (source) |value| try value.field().text(aa) else "", 2879 .source_kind = if (source) |value| value.selectedKind() else .fork, 2880 } else .{ 2881 .title = try aa.dupe(u8, title), 2882 .labels = try aa.dupe(u8, labels), 2883 .description = try aa.dupe(u8, description), 2884 }, 2885 }); 2886 } 2887 2888 fn submitConfirmation(self: *This, allocator: std.mem.Allocator, button_index: usize) !void { 2889 switch (confirmation(self.data.view) orelse return) { 2890 .publish => if (comptime supports_drafts) { 2891 if (comptime wasm) return; 2892 const entry = self.data.selectedThread() orelse return; 2893 if (!entryDraft(entry.*)) return; 2894 const author = ((try self.session.authorize(self.data.identity, .read)) orelse return).author; 2895 try self.data.publishDraft(self.session, allocator, author, entry.id); 2896 const route = listRoute(self.data.identity, @enumFromInt(0), "", entry.id) orelse return; 2897 try self.session.navigate(route); 2898 } else unreachable, 2899 .merge => if (comptime supports_merge) { 2900 if (comptime wasm) return; 2901 const entry = self.data.selectedThread() orelse return; 2902 const revision: evt.Patch.MergeRevision = if (button_index > 1) .squash else .source; 2903 if (!Data.canMerge(entry.*, self.session) or entry.mergeability.get(revision) != .clean) return; 2904 const author = ((try self.session.authorize(self.data.identity, .write)) orelse return).author; 2905 self.data.mergePatch(self.session, allocator, author, entry.id, revision) catch |err| switch (err) { 2906 error.MergeConflict, error.MergeCheckUnavailable, error.PatchOutOfDate, error.PatchDataUnavailable => { 2907 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2908 try self.session.navigate(route); 2909 return; 2910 }, 2911 else => return err, 2912 }; 2913 const merged = std.meta.stringToEnum(Status, "merged") orelse return; 2914 const route = listRoute(self.data.identity, merged, "", entry.id) orelse return; 2915 try self.session.navigate(route); 2916 } else unreachable, 2917 .remove => try self.submitRemove(allocator), 2918 } 2919 } 2920 2921 fn submitRemove(self: *This, allocator: std.mem.Allocator) !void { 2922 const comment = self.data.comment_id.len != 0; 2923 const event_id = if (comment) self.data.comment_id else self.data.selected_id; 2924 const id = evt.parseEventId(event_id) catch return; 2925 try self.removeEvent(allocator, if (comment) .comment else kind, id); 2926 } 2927 2928 fn removeEvent(self: *This, allocator: std.mem.Allocator, event_kind: evt.EventKind, id: [evt.event_id_size]u8) !void { 2929 if (comptime wasm) return; 2930 const actor = (try self.session.authorize(self.data.identity, .read)) orelse return; 2931 const author = actor.author; 2932 2933 if (comptime supports_drafts) { 2934 const selected = self.data.selectedThread() orelse return; 2935 if (event_kind == kind and entryDraft(selected.*)) { 2936 try Data.removeDraft(self.session, allocator, author, &id); 2937 const route = draftsRoute(self.data.identity) orelse return; 2938 try self.session.navigate(route); 2939 return; 2940 } 2941 } 2942 2943 const io = self.session.io orelse return; 2944 const src = self.data.repo_source orelse return; 2945 if (!try src.canModify(io, allocator, actor, event_kind, &id)) return; 2946 2947 switch (src.repo_kind) { 2948 inline else => |repo_kind| { 2949 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2950 defer any_repo.deinit(io, allocator); 2951 switch (any_repo) { 2952 inline else => |*repo| try evt.remove(self.session.eventHost(), .repo, repo_kind, repo.self_repo_opts, io, allocator, repo, &id, event_kind, author), 2953 } 2954 }, 2955 } 2956 2957 const id_hex = std.fmt.bytesToHex(id, .lower); 2958 const route = ui.RoutablePage.repoEventsRoute(self.data.identity, .removed, event_kind, &id_hex, null) orelse return; 2959 try self.session.navigate(route); 2960 } 2961 2962 // return to the list. 2963 fn focusList(self: *This, index: usize, root_focus: *Focus) void { 2964 root_focus.setFocus(self.listScroll(index).getFocus().id); 2965 } 2966 2967 // cross to the header tabs above the stack. 2968 fn focusHeader(self: *This, root_focus: *Focus) void { 2969 root_focus.setFocus(self.box.children.keys()[header_index]); 2970 } 2971 2972 pub fn clearGrid(self: *This) void { 2973 self.box.clearGrid(); 2974 } 2975 2976 pub fn getGrid(self: This) ?Grid { 2977 return self.box.getGrid(); 2978 } 2979 2980 pub fn getFocus(self: *This) *Focus { 2981 return self.box.getFocus(); 2982 } 2983 2984 // for the parent's "scroll up at the top jumps to the header" check: at the 2985 // top only while the header tabs hold focus, so up from the split 2986 // crosses into the tabs first. 2987 pub fn atTop(self: *This) bool { 2988 return self.headerActive(); 2989 } 2990 }; 2991 } 2992