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