╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ thread.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2521 } 2522 2523 fn threadForm(self: *This) ?*wgt.Box(Widget) { 2524 if (supports_conflicts and self.data.view == .resolve) return null; 2525 return switch (self.data.view) { 2526 .new_comment, .edit_comment => null, 2527 else => self.formBox(), 2528 }; 2529 } 2530 2531 fn formField(form: *wgt.Box(Widget), name: []const u8) !*wgt.TextInput { 2532 for (form.children.values()) |*child| switch (child.widget) { 2533 .text_input => |*text_input| if (std.mem.eql(u8, text_input.options.name, name)) return text_input, 2534 else => {}, 2535 }; 2536 return error.MissingFormField; 2537 } 2538 2539 fn formWidget(form: *wgt.Box(Widget), comptime widget_kind: std.meta.Tag(Widget)) ?*@FieldType(Widget, @tagName(widget_kind)) { 2540 for (form.children.values()) |*child| { 2541 if (child.widget == widget_kind) return &@field(child.widget, @tagName(widget_kind)); 2542 } 2543 return null; 2544 } 2545 2546 fn confirmationForm(self: *This) ?*wgt.Box(Widget) { 2547 return switch (self.viewStack().children.values()[form_view_index]) { 2548 .center => |*center| switch (center.child.*) { 2549 .box => |*box| box, 2550 else => null, 2551 }, 2552 else => null, 2553 }; 2554 } 2555 2556 // the resolve form's scroll on the terminal (the web page scrolls itself) 2557 fn resolveScroll(self: *This) ?*wgt.Scroll(Widget) { 2558 return switch (self.viewStack().children.values()[form_view_index]) { 2559 .scroll => |*scroll| scroll, 2560 else => null, 2561 }; 2562 } 2563 2564 fn listScroll(self: *This, index: usize) *wgt.Scroll(Widget) { 2565 return &self.resultsBox(index).children.values()[list_index].widget.scroll; 2566 } 2567 2568 fn listBox(self: *This, index: usize) *wgt.Box(Widget) { 2569 return &self.listScroll(index).child.box; 2570 } 2571 2572 fn detailFrame(self: *This, index: usize) *wgt.Box(Widget) { 2573 return &self.resultsBox(index).children.values()[detail_index].widget.box; 2574 } 2575 2576 fn detail(self: *This, index: usize) *DetailType { 2577 return &@field(self.detailFrame(index).children.values()[widget.list_pane_index].widget, Data.detail_widget_name); 2578 } 2579 2580 fn window(self: *This, index: usize) *const Window { 2581 if (supports_conflicts and index == conflict_view_index) return &self.data.conflicts; 2582 if (supports_drafts and index == drafts_view_index) return &self.data.drafts; 2583 return self.data.window(splitStatus(index)); 2584 } 2585 2586 // an empty first window, which shows the empty message 2587 fn windowEmpty(win: *const Window) bool { 2588 return win.items.len == 0 and win.prev_id == null; 2589 } 2590 2591 fn stackSelectedIndex(self: *This) ?usize { 2592 const stack = self.viewStack(); 2593 const cid = stack.getFocus().child_id orelse return null; 2594 return stack.children.getIndex(cid); 2595 } 2596 2597 // the stack's selected master-detail split (null when the labels view, the 2598 // new-thread form, or the empty message shows). 2599 fn selectedSplitIndex(self: *This) ?usize { 2600 const idx = self.stackSelectedIndex() orelse return null; 2601 const split = idx < status_count or (supports_drafts and idx == drafts_view_index) or idx == conflict_view_index; 2602 return if (split and !windowEmpty(self.window(idx))) idx else null; 2603 } 2604 2605 fn detailActive(self: *This, index: usize) bool { 2606 const rb = self.resultsBox(index); 2607 const cid = rb.getFocus().child_id orelse return false; 2608 return rb.children.getIndex(cid) == detail_index; 2609 } 2610 2611 fn headerActive(self: *This) bool { 2612 const cid = self.box.getFocus().child_id orelse return false; 2613 return self.box.children.getIndex(cid) == header_index; 2614 } 2615 2616 fn labelsViewActive(self: *This) bool { 2617 if (self.headerActive()) return false; 2618 return self.stackSelectedIndex() == labels_view_index; 2619 } 2620 2621 fn formViewActive(self: *This) bool { 2622 if (self.headerActive()) return false; 2623 return self.stackSelectedIndex() == form_view_index; 2624 } 2625 2626 // the selected thread's index, or null when a window-navigation row is 2627 // selected (a leading "previous" row shifts the thread rows down by one). 2628 fn selectedThreadIndex(self: *This, index: usize) ?usize { 2629 const lb = self.listBox(index); 2630 const cid = lb.getFocus().child_id orelse return null; 2631 const idx = lb.children.getIndex(cid) orelse return null; 2632 const win = self.window(index); 2633 const lead: usize = if (win.prev_id != null) 1 else 0; 2634 if (idx < lead or idx - lead >= win.items.len) return null; 2635 return idx - lead; 2636 } 2637 2638 // swap the detail pane to the selected thread when it changes 2639 fn refreshDetail(self: *This, allocator: std.mem.Allocator, index: usize) !void { 2640 const selected = self.selectedThreadIndex(index) orelse return; 2641 const entry = self.window(index).items[selected]; 2642 const changed = if (self.detail(index).entry) |current| !std.mem.eql(u8, current.id, entry.id) else true; 2643 if (!changed) return; 2644 try self.detail(index).setEntry(allocator, entry); 2645 widget.setListBackLink(self.detailFrame(index), try rowLink(self.session.page_arena, self.data, entry.id)); 2646 } 2647 2648 pub fn build(self: *This, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2649 self.clearGrid(); 2650 2651 // each header tab maps 1:1 to a stack child by position 2652 if (self.header().getSelectedIndex()) |index| { 2653 const stack = self.viewStack(); 2654 stack.getFocus().child_id = stack.children.keys()[index]; 2655 } 2656 2657 if (self.selectedSplitIndex()) |i| { 2658 // whether the detail pane fits beside the list, which decides 2659 // the list cap below and whether the detail carries a way back 2660 const both_panes_fit = if (constraint.max_size.width) |w| w >= list_max_width + detail_min_width else true; 2661 try self.refreshDetail(allocator, i); 2662 widget.showListBack(self.detailFrame(i), !both_panes_fit); 2663 2664 // the selected list row shows a border (the focused TextBox 2665 // upgrades it to a double border itself); the rest stay borderless. 2666 const lb = self.listBox(i); 2667 for (lb.children.keys(), lb.children.values()) |id, *child| { 2668 switch (child.widget) { 2669 .text_box => |*tb| widget.markSelected(tb, lb.getFocus().child_id == id), 2670 else => {}, 2671 } 2672 } 2673 2674 // cap the list at list_max_width only while the detail pane fits 2675 // beside it. the box drops the detail when the width can't hold 2676 // both minimums, so when it's that narrow we lift the cap and let 2677 // the list fill the whole width. 2678 self.resultsBox(i).children.values()[list_index].max_size = if (both_panes_fit) .{ .width = list_max_width, .height = null } else null; 2679 2680 // stretch the detail pane across the rest of the width so it fills 2681 // the area rather than shrinking to its content; its scroll fills 2682 // the pane. 2683 if (constraint.max_size.width) |w| { 2684 self.resultsBox(i).children.values()[detail_index].min_size = .{ .width = if (both_panes_fit) w - list_max_width else w, .height = null }; 2685 } else { 2686 self.resultsBox(i).children.values()[detail_index].min_size = .{ .width = detail_min_width, .height = null }; 2687 } 2688 } 2689 2690 // refresh the form inputs' entries in the session's focus-id -> input 2691 // map with this frame's addresses, so the web/wasm form handling can 2692 // find them by focus id 2693 if (self.threadForm()) |form| { 2694 const failure = if (self.session.formFeedback(feedback_tag)) |saved| saved.failure else null; 2695 (try formField(form, "title")).options.top_label.text = if (failure == .required_title) " title (required) " else " title "; 2696 if (comptime supports_drafts) { 2697 if (formWidget(form, .patch_source)) |source| { 2698 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 "; 2699 const submit = formWidget(form, .submit_button) orelse return error.MissingFormField; 2700 try submit.setLabel(allocator, switch (source.selectedKind()) { 2701 .fork => "submit draft", 2702 .branch => "submit patch", 2703 }); 2704 } 2705 (try formField(form, "target_branch")).options.top_label.text = 2706 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 "; 2707 } 2708 } 2709 if (self.formBox()) |form| { 2710 const inputs_arena = self.session.arena.allocator(); 2711 for (form.children.values()) |*child| switch (child.widget) { 2712 .text_input => |*ti| try self.session.text_inputs.put(inputs_arena, ti.getFocus().id, ti), 2713 else => {}, 2714 }; 2715 } 2716 2717 try self.box.build(allocator, constraint, root_focus); 2718 } 2719 2720 pub fn input(self: *This, allocator: std.mem.Allocator, raw_key: Key, root_focus: *Focus) !void { 2721 // wheel input moves focus like the arrows 2722 const key: Key = switch (raw_key) { 2723 .scroll_up => .arrow_up, 2724 .scroll_down => .arrow_down, 2725 else => raw_key, 2726 }; 2727 2728 if (self.headerActive()) { 2729 // down from the sub-header re-enters the stack if the selected 2730 // view has something to focus; enter in the search box submits 2731 // the query; other keys move within the sub-header. 2732 if (inp.vertDirection(key) == .down) { 2733 const enterable = if (self.stackSelectedIndex()) |idx| 2734 if (idx == labels_view_index) 2735 !labelsEmpty(self.data) 2736 else if (idx == form_view_index) 2737 // nothing focusable in the unauthorized view 2738 self.viewStack().children.values()[idx] != .unauthorized 2739 else 2740 !windowEmpty(self.window(idx)) 2741 else 2742 false; 2743 if (enterable) root_focus.setFocus(self.box.children.keys()[stack_index]); 2744 } else if (key == .enter) { 2745 if (try self.header().submittedText(allocator)) |text| { 2746 defer allocator.free(text); 2747 try self.submitSearch(text); 2748 } 2749 } else { 2750 try self.header().input(allocator, key, root_focus); 2751 } 2752 return; 2753 } 2754 if (self.labelsViewActive()) { 2755 self.labelsViewInput(key, root_focus); 2756 return; 2757 } 2758 if (self.formViewActive()) { 2759 if (confirmation(self.data.view) != null) { 2760 try self.confirmationFormInput(allocator, key, root_focus); 2761 } else if (supports_conflicts and self.data.view == .resolve) { 2762 try self.resolveInput(allocator, key, root_focus); 2763 } else { 2764 try self.formInput(allocator, key, root_focus); 2765 } 2766 return; 2767 } 2768 const i = self.selectedSplitIndex() orelse return; 2769 if (self.detailActive(i)) { 2770 // the way back to the list, by enter or a click 2771 if (widget.listBackId(self.detailFrame(i))) |id| { 2772 if (inp.activated(root_focus, id, key)) return self.focusList(i, root_focus); 2773 if (root_focus.grandchild_id == id) return switch (key) { 2774 .arrow_left => self.focusList(i, root_focus), 2775 .arrow_up => self.focusHeader(root_focus), 2776 .arrow_down => _ = self.detail(i).focusFirst(root_focus), 2777 else => {}, 2778 }; 2779 } 2780 switch (try self.detail(i).inputWithExit(allocator, key, root_focus)) { 2781 .none => {}, 2782 .list => self.focusList(i, root_focus), 2783 .header => if (widget.listBackId(self.detailFrame(i))) |id| root_focus.setFocus(id) else self.focusHeader(root_focus), 2784 } 2785 } else { 2786 try self.listInput(allocator, i, key, root_focus); 2787 } 2788 } 2789 2790 // navigate to the typed query's results, keeping the status list the 2791 // page shows. an empty box clears a filtered page and does nothing on 2792 // any other one. 2793 fn submitSearch(self: *This, text: []const u8) !void { 2794 if (text.len == 0 and self.data.search == null) return; 2795 const selected = self.header().getSelectedIndex() orelse 0; 2796 const status: Status = if (selected < status_count) splitStatus(selected) else @fromBackingInt(@intCast(0)); 2797 const route = listRoute(self.data.identity, status, "") orelse return; 2798 try self.session.navigate(route.withSearch(text) orelse return); 2799 } 2800 2801 // arrow keys move the label selection; up from the top row crosses to the 2802 // header tabs. 2803 fn labelsViewInput(self: *This, key: Key, root_focus: *Focus) void { 2804 const tf = self.labelsView(); 2805 const cid = tf.focus.child_id orelse return; 2806 const cur = tf.indexOfFocusId(cid) orelse return; 2807 const count = tf.text_boxes.items.len; 2808 switch (key) { 2809 .arrow_left => if (cur > 0) root_focus.setFocus(tf.text_boxes.items[cur - 1].getFocus().id), 2810 .arrow_right => if (cur + 1 < count) root_focus.setFocus(tf.text_boxes.items[cur + 1].getFocus().id), 2811 .arrow_up => if (tf.rowStep(cur, false)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id) else self.focusHeader(root_focus), 2812 .arrow_down => if (tf.rowStep(cur, true)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id), 2813 .home => root_focus.setFocus(tf.text_boxes.items[0].getFocus().id), 2814 .end => root_focus.setFocus(tf.text_boxes.items[count - 1].getFocus().id), 2815 else => {}, 2816 } 2817 } 2818 2819 fn listInput(self: *This, allocator: std.mem.Allocator, index: usize, key: Key, root_focus: *Focus) !void { 2820 // up/down (and the scroll wheel) move the selection a row; page up/down 2821 // jump a fixed amount. right/Enter cross into the detail pane. up from 2822 // the top row crosses into the header tabs. 2823 if (inp.rowDelta(key, @intCast(self.listBox(index).children.count()))) |delta| { 2824 const lb = self.listBox(index); 2825 const at_top = if (lb.getFocus().child_id) |cid| lb.children.getIndex(cid) == 0 else true; 2826 if (delta < 0 and at_top) return self.focusHeader(root_focus); 2827 moveRowFocus(lb, self.listScroll(index), root_focus, delta); 2828 return; 2829 } 2830 switch (key) { 2831 .enter, .arrow_right => self.focusDetail(index, root_focus), 2832 // when the window is too narrow to lay out the detail pane beside 2833 // the list, a click on a row opens it like enter. the row was just 2834 // selected, so the detail is swapped to it ahead of the build. 2835 .mouse => |mouse| if (self.resultsBox(index).children.values()[detail_index].rect == null and widget.clickOnSelectedRow(self.listBox(index), root_focus, mouse)) { 2836 try self.refreshDetail(allocator, index); 2837 self.focusDetail(index, root_focus); 2838 }, 2839 else => {}, 2840 } 2841 } 2842 2843 fn formInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2844 const form = self.formBox() orelse return; 2845 const cid = form.getFocus().child_id orelse return; 2846 const cur = form.children.getIndex(cid) orelse return; 2847 const child = &form.children.values()[cur]; 2848 2849 if (child.widget == .text_input) { 2850 const text_input = &child.widget.text_input; 2851 if (text_input.options.multiline and try multilineKeepsKey(text_input, allocator, key)) 2852 return child.widget.input(allocator, key, root_focus); 2853 } 2854 2855 const on_submit = child.widget == .submit_button; 2856 switch (key) { 2857 .arrow_up, .back_tab => return if (formStep(form, cur, false)) |i| 2858 root_focus.setFocus(form.children.keys()[i]) 2859 else 2860 self.focusHeader(root_focus), 2861 .arrow_down, .tab => return if (formStep(form, cur, true)) |i| root_focus.setFocus(form.children.keys()[i]), 2862 .enter => if (on_submit) return self.submitForm(allocator), 2863 .mouse => |mouse| if (on_submit) { 2864 if (inp.leftClickOn(root_focus, child.widget.submit_button.buttonId(), mouse)) return self.submitForm(allocator); 2865 }, 2866 else => {}, 2867 } 2868 try child.widget.input(allocator, key, root_focus); 2869 } 2870 2871 fn confirmationFormInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2872 const form = self.confirmationForm() orelse return; 2873 const child_id = form.getFocus().child_id orelse return; 2874 const index = form.children.getIndex(child_id) orelse return; 2875 switch (key) { 2876 .arrow_up, .back_tab => if (index > 1) 2877 root_focus.setFocus(form.children.keys()[index - 1]) 2878 else 2879 self.focusHeader(root_focus), 2880 .arrow_down, .tab => if (index + 1 < form.children.count()) root_focus.setFocus(form.children.keys()[index + 1]), 2881 .enter => try self.submitConfirmation(allocator, index), 2882 .mouse => |mouse| for (form.children.keys()[1..], 1..) |button_id, button_index| { 2883 if (inp.leftClickOn(root_focus, button_id, mouse)) { 2884 try self.submitConfirmation(allocator, button_index); 2885 break; 2886 } 2887 }, 2888 else => {}, 2889 } 2890 } 2891 2892 // the multiline hunk inputs keep enter and any up/down that has a row to 2893 // move to; enter on a "use this" link is a navigation the host follows 2894 // before this runs. 2895 fn resolveInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2896 const form = self.formBox() orelse return; 2897 const cid = form.getFocus().child_id orelse return; 2898 const cur = form.children.getIndex(cid) orelse return; 2899 const child = &form.children.values()[cur]; 2900 2901 if (child.widget == .text_input) { 2902 const ti = &child.widget.text_input; 2903 if (ti.options.multiline and try multilineKeepsKey(ti, allocator, key)) 2904 return child.widget.input(allocator, key, root_focus); 2905 } 2906 2907 const on_submit = child.widget == .submit_button; 2908 2909 switch (key) { 2910 .arrow_up, .back_tab => if (formStep(form, cur, false)) |i| self.focusResolveChild(form, i, root_focus) else self.focusHeader(root_focus), 2911 .arrow_down, .tab => if (formStep(form, cur, true)) |i| self.focusResolveChild(form, i, root_focus), 2912 .arrow_left, .arrow_right => switch (child.widget) { 2913 .box => |*row| _ = widget.moveInRow(row, root_focus, key == .arrow_right), 2914 else => try child.widget.input(allocator, key, root_focus), 2915 }, 2916 .enter => if (on_submit) try self.submitResolution(allocator), 2917 .mouse => |mouse| if (on_submit) { 2918 if (inp.leftClickOn(root_focus, child.widget.submit_button.buttonId(), mouse)) try self.submitResolution(allocator); 2919 }, 2920 else => try child.widget.input(allocator, key, root_focus), 2921 } 2922 } 2923 2924 // a multiline input keeps enter and any up/down that has a row to move to 2925 fn multilineKeepsKey(text_input: *wgt.TextInput, allocator: std.mem.Allocator, key: Key) !bool { 2926 return switch (key) { 2927 .enter => true, 2928 .arrow_up => !try text_input.cursorOnFirstRow(allocator), 2929 .arrow_down => !try text_input.cursorOnLastRow(allocator), 2930 else => false, 2931 }; 2932 } 2933 2934 // the neighboring form control, skipping spacers and blank rows 2935 fn formStep(form: *wgt.Box(Widget), cur: usize, down: bool) ?usize { 2936 var i = cur; 2937 while (true) { 2938 if (down) { 2939 i += 1; 2940 if (i >= form.children.count()) return null; 2941 } else { 2942 if (i == 0) return null; 2943 i -= 1; 2944 } 2945 const child = &form.children.values()[i]; 2946 if (child.hidden) continue; 2947 switch (child.widget) { 2948 .spacer, .text => continue, 2949 else => return i, 2950 } 2951 } 2952 } 2953 2954 // focus the resolve-form child at `i` (a version row focuses its selected 2955 // child) and keep it visible on the terminal 2956 fn focusResolveChild(self: *This, form: *wgt.Box(Widget), i: usize, root_focus: *Focus) void { 2957 const child = &form.children.values()[i]; 2958 switch (child.widget) { 2959 .box => |*row| root_focus.setFocus(row.getFocus().child_id orelse form.children.keys()[i]), 2960 else => root_focus.setFocus(form.children.keys()[i]), 2961 } 2962 if (self.session.is_terminal) { 2963 if (self.resolveScroll()) |sc| { 2964 if (child.rect) |rect| sc.scrollToRect(rect); 2965 } 2966 } 2967 } 2968 2969 // re-emit the conflicted thread's event with the resolved content; any 2970 // event on the thread settles its conflict entry. this is the terminal 2971 // path; the web posts the form to the resolve route. 2972 fn submitResolution(self: *This, allocator: std.mem.Allocator) !void { 2973 if (comptime wasm) return; 2974 const io = self.session.io orelse return; 2975 const src = self.data.repo_source orelse return; 2976 const entry = self.data.selectedThread() orelse return; 2977 const author = ((try self.session.authorize(self.data.identity, .write, kind)) orelse return).author; 2978 const form = self.formBox() orelse return; 2979 2980 // gather the inputs by name; the d<n> hunk inputs appear in chunk order 2981 var title: ?[]u8 = null; 2982 var labels: ?[]u8 = null; 2983 var hunks: std.ArrayList([]const u8) = .empty; 2984 defer { 2985 if (title) |t| allocator.free(t); 2986 if (labels) |t| allocator.free(t); 2987 for (hunks.items) |h| allocator.free(h); 2988 hunks.deinit(allocator); 2989 } 2990 for (form.children.values()) |*child| switch (child.widget) { 2991 .text_input => |*ti| { 2992 const text = try ti.text(allocator); 2993 errdefer allocator.free(text); 2994 if (std.mem.eql(u8, ti.options.name, "title")) { 2995 title = text; 2996 } else if (std.mem.eql(u8, ti.options.name, "labels")) { 2997 labels = text; 2998 } else { 2999 try hunks.append(allocator, text); 3000 } 3001 }, 3002 else => {}, 3003 }; 3004 3005 const id_bytes = try evt.parseEventId(entry.id); 3006 3007 switch (src.repo_kind) { 3008 inline else => |repo_kind| { 3009 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 3010 defer any_repo.deinit(io, allocator); 3011 switch (any_repo) { 3012 inline else => |*repo| { 3013 var resolution: Event.Resolve = .{ 3014 .title = title, 3015 .labels = labels, 3016 .hunks = hunks.items, 3017 }; 3018 if (comptime @hasField(Event.Resolve, "theirs")) resolution.theirs = self.data.theirs_picks; 3019 const change = Event.Update{ .resolve = resolution }; 3020 Event.update(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, change, author) catch |err| switch (err) { 3021 // leave the form up for correction 3022 error.InvalidFields => return, 3023 else => |e| return e, 3024 }; 3025 }, 3026 } 3027 }, 3028 } 3029 3030 const route = listRoute(self.data.identity, entryStatus(entry.*), entry.id) orelse return; 3031 try self.session.navigate(route); 3032 } 3033 3034 // commit a new or edited comment and navigate to its permalink. this is the 3035 // terminal path; the web posts the form to its comment route. 3036 fn submitComment(self: *This, allocator: std.mem.Allocator) !void { 3037 if (comptime wasm) return; 3038 const io = self.session.io orelse return; 3039 const src = self.data.repo_source orelse return; 3040 const actor = (try self.session.authorize(self.data.identity, .read, kind)) orelse return; 3041 const author = actor.author; 3042 const form = self.formBox() orelse return; 3043 const body_input = try formField(form, "body"); 3044 const body = try body_input.text(allocator); 3045 defer allocator.free(body); 3046 if (!evt.Comment.fieldsValid(body)) return; 3047 3048 var thread_id: [evt.event_id_size * 2]u8 = undefined; 3049 @memcpy(&thread_id, self.data.selected_id); 3050 const parent_id = if (self.data.comment_id.len == 0) thread_id else blk: { 3051 var parent: [evt.event_id_size * 2]u8 = undefined; 3052 @memcpy(&parent, self.data.comment_id); 3053 break :blk parent; 3054 }; 3055 var event_id_hex: [evt.event_id_size * 2]u8 = undefined; 3056 const editing = self.data.view == .edit_comment; 3057 if (editing) { 3058 const comment_id = evt.parseEventId(self.data.comment_id) catch return; 3059 if (!try src.canModify(io, allocator, actor, .comment, &comment_id)) return; 3060 } 3061 switch (src.repo_kind) { 3062 inline else => |repo_kind| { 3063 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 3064 defer any_repo.deinit(io, allocator); 3065 switch (any_repo) { 3066 inline else => |*repo| if (editing) { 3067 const comment_id = evt.parseEventId(self.data.comment_id) catch return; 3068 try evt.Comment.update(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, kind, &thread_id, &comment_id, body, author); 3069 event_id_hex = std.fmt.bytesToHex(comment_id, .lower); 3070 } else { 3071 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); 3072 }, 3073 } 3074 }, 3075 } 3076 3077 body_input.clear(allocator); 3078 const route = ui.RoutablePage.repoThreadCommentRoute(kind, self.data.identity, &thread_id, &event_id_hex, 0) orelse return; 3079 try self.session.navigate(route); 3080 } 3081 3082 // terminal form submissions; the web posts to the forms' routes instead 3083 fn submitForm(self: *This, allocator: std.mem.Allocator) !void { 3084 switch (self.data.view) { 3085 .new_comment, .edit_comment => try self.submitComment(allocator), 3086 .edit => try self.submitEditedThread(allocator), 3087 else => try self.submitNewThread(allocator), 3088 } 3089 } 3090 3091 // commit the new thread to the repo's events branch and navigate to it. 3092 // this is the terminal path; the web posts the form to the thread route, 3093 // so the wasm side never runs (or compiles) the repo access below. 3094 fn submitNewThread(self: *This, allocator: std.mem.Allocator) !void { 3095 if (comptime wasm) return; 3096 const io = self.session.io orelse return; 3097 const src = self.data.repo_source orelse return; 3098 const actor = (try self.session.authorize(self.data.identity, .read, kind)) orelse return; 3099 3100 const form = self.threadForm() orelse return; 3101 const title_input = try formField(form, "title"); 3102 const labels_input = try formField(form, "labels"); 3103 const description_input = try formField(form, "description"); 3104 3105 const title = try title_input.text(allocator); 3106 defer allocator.free(title); 3107 const labels = try labels_input.text(allocator); 3108 defer allocator.free(labels); 3109 const description = try description_input.text(allocator); 3110 defer allocator.free(description); 3111 const target_branch = if (comptime supports_drafts) try (try formField(form, "target_branch")).text(allocator) else ""; 3112 defer if (comptime supports_drafts) allocator.free(target_branch); 3113 3114 if (!Event.fieldsValid(title, labels)) { 3115 if (!evt.titleValid(title)) try self.rememberThreadFeedback(.required_title, title, labels, description, target_branch); 3116 return; 3117 } 3118 3119 if (comptime supports_drafts) { 3120 const source = formWidget(form, .patch_source) orelse return error.MissingFormField; 3121 const source_branch = switch (source.selectedKind()) { 3122 .fork => null, 3123 .branch => try source.field().text(allocator), 3124 }; 3125 defer if (source_branch) |branch| allocator.free(branch); 3126 const event_id_hex = Data.create(self.data, self.session, allocator, actor, title, labels, description, target_branch, source_branch) catch |err| { 3127 const failure = FeedbackFailure.fromError(err) orelse return err; 3128 try self.rememberThreadFeedback(failure, title, labels, description, target_branch); 3129 return; 3130 }; 3131 title_input.clear(allocator); 3132 labels_input.clear(allocator); 3133 description_input.clear(allocator); 3134 (try formField(form, "target_branch")).clear(allocator); 3135 const route = ui.RoutablePage.repoThreadCommentsRoute(kind, self.data.identity, &event_id_hex, 0) orelse return; 3136 try self.session.navigate(route); 3137 return; 3138 } 3139 3140 var id_bytes: [evt.event_id_size]u8 = undefined; 3141 io.random(&id_bytes); 3142 const event_id_hex = std.fmt.bytesToHex(id_bytes, .lower); 3143 3144 const event = evt.EventWithId{ 3145 .id = event_id_hex, 3146 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 3147 .author = actor.author, 3148 .event = @unionInit(evt.Event, @tagName(kind), Event{ 3149 .title = title, 3150 .description = description, 3151 .labels = labels, 3152 }), 3153 }; 3154 3155 switch (src.repo_kind) { 3156 inline else => |repo_kind| { 3157 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 3158 defer any_repo.deinit(io, allocator); 3159 switch (any_repo) { 3160 inline else => |*repo| try evt.consume(self.session.eventHost(), .repo, repo_kind, repo.self_repo_opts, io, allocator, repo, evt.events_ref, &.{event}), 3161 } 3162 }, 3163 } 3164 3165 // wipe the form so a return visit starts fresh 3166 title_input.clear(allocator); 3167 labels_input.clear(allocator); 3168 description_input.clear(allocator); 3169 3170 const route = listRoute(self.data.identity, @fromBackingInt(@intCast(0)), &event_id_hex) orelse return; 3171 try self.session.navigate(route); 3172 } 3173 3174 // re-emit the selected thread's event with the form's content (status 3175 // preserved), then reload the thread's page. this is the terminal path; 3176 // the web posts the form to the edit route. 3177 fn submitEditedThread(self: *This, allocator: std.mem.Allocator) !void { 3178 if (comptime wasm) return; 3179 const io = self.session.io orelse return; 3180 const entry = self.data.selectedThread() orelse return; 3181 const actor = (try self.session.authorize(self.data.identity, .read, kind)) orelse return; 3182 const author = actor.author; 3183 3184 const form = self.threadForm() orelse return; 3185 const title_input = try formField(form, "title"); 3186 const labels_input = try formField(form, "labels"); 3187 const description_input = try formField(form, "description"); 3188 3189 const title = try title_input.text(allocator); 3190 defer allocator.free(title); 3191 const labels = try labels_input.text(allocator); 3192 defer allocator.free(labels); 3193 const description = try description_input.text(allocator); 3194 defer allocator.free(description); 3195 const target_branch = if (comptime supports_drafts) try (try formField(form, "target_branch")).text(allocator) else ""; 3196 defer if (comptime supports_drafts) allocator.free(target_branch); 3197 3198 if (!Event.fieldsValid(title, labels)) { 3199 if (!evt.titleValid(title)) try self.rememberThreadFeedback(.required_title, title, labels, description, target_branch); 3200 return; 3201 } 3202 const src = self.data.repo_source orelse return; 3203 3204 if (comptime supports_drafts) { 3205 if (entryDraft(entry.*)) { 3206 self.data.editDraft(self.session, allocator, author, entry.id, title, labels, description, target_branch) catch |err| { 3207 const failure = FeedbackFailure.fromError(err) orelse return err; 3208 try self.rememberThreadFeedback(failure, title, labels, description, target_branch); 3209 return; 3210 }; 3211 const route = listRoute(self.data.identity, entryStatus(entry.*), entry.id) orelse return; 3212 try self.session.navigate(route); 3213 return; 3214 } 3215 } 3216 3217 const id_bytes = try evt.parseEventId(entry.id); 3218 if (!try src.canModify(io, allocator, actor, kind, &id_bytes)) return; 3219 3220 switch (src.repo_kind) { 3221 inline else => |repo_kind| { 3222 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 3223 defer any_repo.deinit(io, allocator); 3224 switch (any_repo) { 3225 inline else => |*repo| if (supports_drafts) { 3226 Event.update(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 3227 .title = title, 3228 .labels = labels, 3229 .description = description, 3230 .target_branch = target_branch, 3231 } }, author) catch |err| { 3232 const failure = FeedbackFailure.fromError(err) orelse return err; 3233 try self.rememberThreadFeedback(failure, title, labels, description, target_branch); 3234 return; 3235 }; 3236 } else if (has_status) 3237 try Event.update(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 3238 .title = title, 3239 .labels = labels, 3240 .description = description, 3241 } }, author) 3242 else 3243 try Event.update(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, title, labels, description, author), 3244 } 3245 }, 3246 } 3247 3248 const route = listRoute(self.data.identity, entryStatus(entry.*), entry.id) orelse return; 3249 try self.session.navigate(route); 3250 } 3251 3252 fn rememberThreadFeedback(self: *This, failure: FeedbackFailure, title: []const u8, labels: []const u8, description: []const u8, target_branch: []const u8) !void { 3253 const aa = self.session.arena.allocator(); 3254 const source = if (supports_drafts) blk: { 3255 const form = self.threadForm() orelse break :blk null; 3256 break :blk formWidget(form, .patch_source); 3257 } else null; 3258 self.session.data.form_feedback = @unionInit(ui.Session.FormFeedback, @tagName(feedback_tag), .{ 3259 .failure = failure, 3260 .fields = if (comptime supports_drafts) .{ 3261 .title = try aa.dupe(u8, title), 3262 .labels = try aa.dupe(u8, labels), 3263 .description = try aa.dupe(u8, description), 3264 .target_branch = try aa.dupe(u8, target_branch), 3265 .source_branch = if (source) |value| try value.field().text(aa) else "", 3266 .source_kind = if (source) |value| value.selectedKind() else .fork, 3267 } else .{ 3268 .title = try aa.dupe(u8, title), 3269 .labels = try aa.dupe(u8, labels), 3270 .description = try aa.dupe(u8, description), 3271 }, 3272 }); 3273 } 3274 3275 fn submitConfirmation(self: *This, allocator: std.mem.Allocator, button_index: usize) !void { 3276 switch (confirmation(self.data.view) orelse return) { 3277 .publish => if (comptime supports_drafts) { 3278 if (comptime wasm) return; 3279 const entry = self.data.selectedThread() orelse return; 3280 if (!entryDraft(entry.*)) return; 3281 const author = ((try self.session.authorize(self.data.identity, .read, kind)) orelse return).author; 3282 try self.data.publishDraft(self.session, allocator, author, entry.id); 3283 const route = listRoute(self.data.identity, @fromBackingInt(@intCast(0)), entry.id) orelse return; 3284 try self.session.navigate(route); 3285 } else unreachable, 3286 .merge => if (comptime supports_merge) { 3287 if (comptime wasm) return; 3288 const entry = self.data.selectedThread() orelse return; 3289 const revision: evt.Patch.MergeRevision = if (button_index > 1) .squash else .source; 3290 if (!Data.canMerge(entry.*, self.session) or entry.mergeability.get(revision) != .clean) return; 3291 const author = ((try self.session.authorize(self.data.identity, .write, kind)) orelse return).author; 3292 self.data.mergePatch(self.session, allocator, author, entry.id, revision) catch |err| switch (err) { 3293 error.MergeConflict, error.MergeCheckUnavailable, error.PatchOutOfDate, error.PatchDataUnavailable => { 3294 const route = listRoute(self.data.identity, entryStatus(entry.*), entry.id) orelse return; 3295 try self.session.navigate(route); 3296 return; 3297 }, 3298 else => return err, 3299 }; 3300 const merged = std.meta.stringToEnum(Status, "merged") orelse return; 3301 const route = listRoute(self.data.identity, merged, entry.id) orelse return; 3302 try self.session.navigate(route); 3303 } else unreachable, 3304 .remove => try self.submitRemove(allocator), 3305 } 3306 } 3307 3308 fn submitRemove(self: *This, allocator: std.mem.Allocator) !void { 3309 const comment = self.data.comment_id.len != 0; 3310 const event_id = if (comment) self.data.comment_id else self.data.selected_id; 3311 const id = evt.parseEventId(event_id) catch return; 3312 try self.removeEvent(allocator, if (comment) .comment else kind, id); 3313 } 3314 3315 fn removeEvent(self: *This, allocator: std.mem.Allocator, event_kind: evt.EventKind, id: [evt.event_id_size]u8) !void { 3316 if (comptime wasm) return; 3317 const actor = (try self.session.authorize(self.data.identity, .read, kind)) orelse return; 3318 const author = actor.author; 3319 3320 if (comptime supports_drafts) { 3321 const selected = self.data.selectedThread() orelse return; 3322 if (event_kind == kind and entryDraft(selected.*)) { 3323 try Data.removeDraft(self.session, allocator, author, &id); 3324 const route = draftsRoute(self.data.identity) orelse return; 3325 try self.session.navigate(route); 3326 return; 3327 } 3328 } 3329 3330 const io = self.session.io orelse return; 3331 const src = self.data.repo_source orelse return; 3332 if (!try src.canModify(io, allocator, actor, event_kind, &id)) return; 3333 const thread_id = evt.parseEventId(self.data.selected_id) catch return; 3334 3335 switch (src.repo_kind) { 3336 inline else => |repo_kind| { 3337 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 3338 defer any_repo.deinit(io, allocator); 3339 switch (any_repo) { 3340 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), 3341 } 3342 }, 3343 } 3344 3345 const id_hex = std.fmt.bytesToHex(id, .lower); 3346 const route = ui.RoutablePage.repoEventsRoute(self.data.identity, .removed, event_kind, &id_hex, null) orelse return; 3347 try self.session.navigate(route); 3348 } 3349 3350 // enter the detail pane, landing on the way back to the list when it shows 3351 fn focusDetail(self: *This, index: usize, root_focus: *Focus) void { 3352 if (widget.listBackId(self.detailFrame(index)) != null) return widget.focusListBack(self.detailFrame(index), root_focus); 3353 _ = self.detail(index).focusFirst(root_focus); 3354 } 3355 3356 // return to the list. 3357 fn focusList(self: *This, index: usize, root_focus: *Focus) void { 3358 root_focus.setFocus(self.listScroll(index).getFocus().id); 3359 } 3360 3361 // cross to the header tabs above the stack. 3362 fn focusHeader(self: *This, root_focus: *Focus) void { 3363 root_focus.setFocus(self.box.children.keys()[header_index]); 3364 } 3365 3366 pub fn clearGrid(self: *This) void { 3367 self.box.clearGrid(); 3368 } 3369 3370 pub fn getGrid(self: This) ?Grid { 3371 return self.box.getGrid(); 3372 } 3373 3374 pub fn getFocus(self: *This) *Focus { 3375 return self.box.getFocus(); 3376 } 3377 3378 // for the parent's "scroll up at the top jumps to the header" check: at the 3379 // top only while the header tabs hold focus, so up from the split 3380 // crosses into the tabs first. 3381 pub fn atTop(self: *This) bool { 3382 return self.headerActive(); 3383 } 3384 }; 3385 } 3386