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