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