╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ thread.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2001 .box => |*box| box, 2002 else => null, 2003 }, 2004 else => null, 2005 }; 2006 } 2007 2008 // the resolve form's scroll on the terminal (the web page scrolls itself) 2009 fn resolveScroll(self: *This) ?*wgt.Scroll(Widget) { 2010 return switch (self.viewStack().children.values()[form_view_index]) { 2011 .scroll => |*scroll| scroll, 2012 else => null, 2013 }; 2014 } 2015 2016 fn listScroll(self: *This, index: usize) *wgt.Scroll(Widget) { 2017 return &self.resultsBox(index).children.values()[list_index].widget.scroll; 2018 } 2019 2020 fn listBox(self: *This, index: usize) *wgt.Box(Widget) { 2021 return &self.listScroll(index).child.box; 2022 } 2023 2024 fn detail(self: *This, index: usize) *DetailType { 2025 const frame = &self.resultsBox(index).children.values()[detail_index].widget.box; 2026 return &@field(frame.children.values()[0].widget, Data.detail_widget_name); 2027 } 2028 2029 fn window(self: *This, index: usize) *const Window { 2030 if (supports_conflicts and index == conflict_view_index) return &self.data.conflicts; 2031 if (supports_drafts and index == drafts_view_index) return &self.data.drafts; 2032 return self.data.window(splitStatus(index)); 2033 } 2034 2035 fn stackSelectedIndex(self: *This) ?usize { 2036 const stack = self.viewStack(); 2037 const cid = stack.getFocus().child_id orelse return null; 2038 return stack.children.getIndex(cid); 2039 } 2040 2041 // the stack's selected master-detail split (null when the tags view or the 2042 // new-thread form shows). 2043 fn selectedSplitIndex(self: *This) ?usize { 2044 const idx = self.stackSelectedIndex() orelse return null; 2045 return if (idx < status_count or (supports_drafts and idx == drafts_view_index) or idx == conflict_view_index) idx else null; 2046 } 2047 2048 fn detailActive(self: *This, index: usize) bool { 2049 const rb = self.resultsBox(index); 2050 const cid = rb.getFocus().child_id orelse return false; 2051 return rb.children.getIndex(cid) == detail_index; 2052 } 2053 2054 fn headerActive(self: *This) bool { 2055 const cid = self.box.getFocus().child_id orelse return false; 2056 return self.box.children.getIndex(cid) == header_index; 2057 } 2058 2059 fn tagsViewActive(self: *This) bool { 2060 if (self.headerActive()) return false; 2061 return self.stackSelectedIndex() == tags_view_index; 2062 } 2063 2064 fn formViewActive(self: *This) bool { 2065 if (self.headerActive()) return false; 2066 return self.stackSelectedIndex() == form_view_index; 2067 } 2068 2069 // the selected thread's index, or null when a window-navigation row is 2070 // selected (a leading "previous" row shifts the thread rows down by one). 2071 fn selectedThreadIndex(self: *This, index: usize) ?usize { 2072 const lb = self.listBox(index); 2073 const cid = lb.getFocus().child_id orelse return null; 2074 const idx = lb.children.getIndex(cid) orelse return null; 2075 const win = self.window(index); 2076 const lead: usize = if (win.prev_id != null) 1 else 0; 2077 if (idx < lead or idx - lead >= win.items.len) return null; 2078 return idx - lead; 2079 } 2080 2081 pub fn build(self: *This, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2082 self.clearGrid(); 2083 2084 // each header tab maps 1:1 to a stack child by position 2085 if (self.header().getSelectedIndex()) |index| { 2086 const stack = self.viewStack(); 2087 stack.getFocus().child_id = stack.children.keys()[index]; 2088 } 2089 2090 if (self.selectedSplitIndex()) |i| { 2091 // swap the detail pane to the selected thread when it changes. 2092 if (self.selectedThreadIndex(i)) |selected| { 2093 const entry = self.window(i).items[selected]; 2094 const changed = if (self.detail(i).entry) |current| !std.mem.eql(u8, current.id, entry.id) else true; 2095 if (changed) { 2096 try self.detail(i).setEntry(allocator, entry); 2097 } 2098 } 2099 2100 // the selected list row shows a border (the focused TextBox 2101 // upgrades it to a double border itself); the rest stay borderless. 2102 const lb = self.listBox(i); 2103 for (lb.children.keys(), lb.children.values()) |id, *child| { 2104 switch (child.widget) { 2105 .text_box => |*tb| tb.options.border_style = if (lb.getFocus().child_id == id) .single else .hidden, 2106 else => {}, 2107 } 2108 } 2109 2110 // cap the list at list_max_width only while the detail pane fits 2111 // beside it. the box drops the detail when the width can't hold 2112 // both minimums, so when it's that narrow we lift the cap and let 2113 // the list fill the whole width. 2114 const both_panes_fit = if (constraint.max_size.width) |w| w >= list_max_width + detail_min_width else true; 2115 self.resultsBox(i).children.values()[list_index].max_size = if (both_panes_fit) .{ .width = list_max_width, .height = null } else null; 2116 2117 // stretch the detail pane across the rest of the width so it fills 2118 // the area rather than shrinking to its content; its scroll fills 2119 // the pane. 2120 if (constraint.max_size.width) |w| { 2121 self.resultsBox(i).children.values()[detail_index].min_size = .{ .width = if (both_panes_fit) w - list_max_width else w, .height = null }; 2122 } else { 2123 self.resultsBox(i).children.values()[detail_index].min_size = .{ .width = detail_min_width, .height = null }; 2124 } 2125 } 2126 2127 // refresh the form inputs' entries in the session's focus-id -> input 2128 // map with this frame's addresses, so the web/wasm form handling can 2129 // find them by focus id 2130 if (self.threadForm()) |form| { 2131 const failure = if (self.session.formFeedback(feedback_tag)) |saved| saved.failure else null; 2132 (try formField(form, "title")).options.label = if (failure == .required_title) " title (required) " else " title "; 2133 if (comptime supports_drafts) { 2134 if (formWidget(form, .patch_source)) |source| { 2135 source.field().options.label = if (failure == .invalid_source_branch) " source branch (not found) " else if (failure == .same_branch) " source branch (same as target) " else if (failure == .unrelated_branches) " source branch (unrelated) " else " source branch "; 2136 const submit = formWidget(form, .submit_button) orelse return error.MissingFormField; 2137 try submit.setLabel(allocator, switch (source.selected) { 2138 .fork => "submit draft", 2139 .branch => "submit patch", 2140 }); 2141 } 2142 (try formField(form, "target_branch")).options.label = 2143 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 "; 2144 } 2145 } 2146 if (self.formBox()) |form| { 2147 const inputs_arena = self.session.arena.allocator(); 2148 for (form.children.values()) |*child| switch (child.widget) { 2149 .text_input => |*ti| try self.session.text_inputs.put(inputs_arena, ti.getFocus().id, ti), 2150 else => {}, 2151 }; 2152 } 2153 2154 try self.box.build(allocator, constraint, root_focus); 2155 } 2156 2157 pub fn input(self: *This, allocator: std.mem.Allocator, raw_key: Key, root_focus: *Focus) !void { 2158 // wheel input moves focus like the arrows 2159 const key: Key = if (raw_key == .mouse and raw_key.mouse.action == .scroll) 2160 (if (raw_key.mouse.action.scroll == .up) .arrow_up else .arrow_down) 2161 else 2162 raw_key; 2163 2164 if (self.headerActive()) { 2165 // down from the tabs re-enters the stack if the selected view has 2166 // something to focus; other keys move the tabs. 2167 if (inp.vertDirection(key) == .down) { 2168 const enterable = if (self.viewStack().getSelected()) |selected| switch (selected.*) { 2169 .tag_flow => |*tf| tf.text_boxes.items.len > 0, 2170 // nothing focusable in the unauthorized view 2171 .unauthorized => false, 2172 else => true, 2173 } else false; 2174 if (enterable) root_focus.setFocus(self.box.children.keys()[stack_index]); 2175 } else { 2176 try self.header().input(allocator, key, root_focus); 2177 } 2178 return; 2179 } 2180 if (self.tagsViewActive()) { 2181 self.tagsViewInput(key, root_focus); 2182 return; 2183 } 2184 if (self.formViewActive()) { 2185 if (confirmation(self.data.view) != null) { 2186 try self.confirmationFormInput(allocator, key, root_focus); 2187 } else if (supports_conflicts and self.data.view == .resolve) { 2188 try self.resolveInput(allocator, key, root_focus); 2189 } else { 2190 try self.formInput(allocator, key, root_focus); 2191 } 2192 return; 2193 } 2194 const i = self.selectedSplitIndex() orelse return; 2195 if (self.detailActive(i)) { 2196 switch (try self.detail(i).inputWithExit(allocator, key, root_focus)) { 2197 .none => {}, 2198 .list => self.focusList(i, root_focus), 2199 .header => self.focusHeader(root_focus), 2200 } 2201 } else { 2202 self.listInput(i, key, root_focus); 2203 } 2204 } 2205 2206 // arrow keys move the tag selection; up from the top row crosses to the 2207 // header tabs. 2208 fn tagsViewInput(self: *This, key: Key, root_focus: *Focus) void { 2209 const tf = self.tagsView(); 2210 const cid = tf.focus.child_id orelse return; 2211 const cur = tf.indexOfFocusId(cid) orelse return; 2212 const count = tf.text_boxes.items.len; 2213 switch (key) { 2214 .arrow_left => if (cur > 0) root_focus.setFocus(tf.text_boxes.items[cur - 1].getFocus().id), 2215 .arrow_right => if (cur + 1 < count) root_focus.setFocus(tf.text_boxes.items[cur + 1].getFocus().id), 2216 .arrow_up => if (tf.rowStep(cur, false)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id) else self.focusHeader(root_focus), 2217 .arrow_down => if (tf.rowStep(cur, true)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id), 2218 .home => root_focus.setFocus(tf.text_boxes.items[0].getFocus().id), 2219 .end => root_focus.setFocus(tf.text_boxes.items[count - 1].getFocus().id), 2220 else => {}, 2221 } 2222 } 2223 2224 fn listInput(self: *This, index: usize, key: Key, root_focus: *Focus) void { 2225 // up/down (and the scroll wheel) move the selection a row; page up/down 2226 // jump a fixed amount. right/Enter cross into the detail pane. up from 2227 // the top row crosses into the header tabs. 2228 if (inp.rowDelta(key, @intCast(self.listBox(index).children.count()))) |delta| { 2229 const lb = self.listBox(index); 2230 const at_top = if (lb.getFocus().child_id) |cid| lb.children.getIndex(cid) == 0 else true; 2231 if (delta < 0 and at_top) return self.focusHeader(root_focus); 2232 moveRowFocus(lb, self.listScroll(index), root_focus, delta); 2233 return; 2234 } 2235 switch (key) { 2236 .enter, .arrow_right => _ = self.detail(index).focusFirst(root_focus), 2237 else => {}, 2238 } 2239 } 2240 2241 fn formInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2242 const form = self.formBox() orelse return; 2243 const cid = form.getFocus().child_id orelse return; 2244 const cur = form.children.getIndex(cid) orelse return; 2245 const child = &form.children.values()[cur]; 2246 2247 if (child.widget == .text_input) { 2248 const text_input = &child.widget.text_input; 2249 if (text_input.options.multiline and try multilineKeepsKey(text_input, allocator, key)) 2250 return child.widget.input(allocator, key, root_focus); 2251 } 2252 2253 const on_submit = child.widget == .submit_button; 2254 switch (key) { 2255 .arrow_up, .back_tab => return if (formStep(form, cur, false)) |i| 2256 root_focus.setFocus(form.children.keys()[i]) 2257 else 2258 self.focusHeader(root_focus), 2259 .arrow_down, .tab => return if (formStep(form, cur, true)) |i| root_focus.setFocus(form.children.keys()[i]), 2260 .enter => if (on_submit) return self.submitForm(allocator), 2261 .mouse => |mouse| if (on_submit) { 2262 if (inp.leftClickOn(root_focus, child.widget.submit_button.buttonId(), mouse)) return self.submitForm(allocator); 2263 }, 2264 else => {}, 2265 } 2266 try child.widget.input(allocator, key, root_focus); 2267 } 2268 2269 fn confirmationFormInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2270 const form = self.confirmationForm() orelse return; 2271 const child_id = form.getFocus().child_id orelse return; 2272 const index = form.children.getIndex(child_id) orelse return; 2273 switch (key) { 2274 .arrow_up, .back_tab => if (index > 1) 2275 root_focus.setFocus(form.children.keys()[index - 1]) 2276 else 2277 self.focusHeader(root_focus), 2278 .arrow_down, .tab => if (index + 1 < form.children.count()) root_focus.setFocus(form.children.keys()[index + 1]), 2279 .enter => try self.submitConfirmation(allocator, index), 2280 .mouse => |mouse| for (form.children.keys()[1..], 1..) |button_id, button_index| { 2281 if (inp.leftClickOn(root_focus, button_id, mouse)) { 2282 try self.submitConfirmation(allocator, button_index); 2283 break; 2284 } 2285 }, 2286 else => {}, 2287 } 2288 } 2289 2290 // the multiline hunk inputs keep enter and any up/down that has a row to 2291 // move to; enter on a "use this" link is a navigation the host follows 2292 // before this runs. 2293 fn resolveInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2294 const form = self.formBox() orelse return; 2295 const cid = form.getFocus().child_id orelse return; 2296 const cur = form.children.getIndex(cid) orelse return; 2297 const child = &form.children.values()[cur]; 2298 2299 if (child.widget == .text_input) { 2300 const ti = &child.widget.text_input; 2301 if (ti.options.multiline and try multilineKeepsKey(ti, allocator, key)) 2302 return child.widget.input(allocator, key, root_focus); 2303 } 2304 2305 const on_submit = child.widget == .submit_button; 2306 2307 switch (key) { 2308 .arrow_up, .back_tab => if (formStep(form, cur, false)) |i| self.focusResolveChild(form, i, root_focus) else self.focusHeader(root_focus), 2309 .arrow_down, .tab => if (formStep(form, cur, true)) |i| self.focusResolveChild(form, i, root_focus), 2310 .arrow_left, .arrow_right => switch (child.widget) { 2311 .box => |*row| { 2312 const row_cid = row.getFocus().child_id orelse return; 2313 const row_index = row.children.getIndex(row_cid) orelse return; 2314 if (key == .arrow_left) { 2315 if (row_index > 0) root_focus.setFocus(row.children.keys()[row_index - 1]); 2316 } else if (row_index + 1 < row.children.count()) { 2317 root_focus.setFocus(row.children.keys()[row_index + 1]); 2318 } 2319 }, 2320 else => try child.widget.input(allocator, key, root_focus), 2321 }, 2322 .enter => if (on_submit) try self.submitResolution(allocator), 2323 .mouse => |mouse| if (on_submit) { 2324 if (inp.leftClickOn(root_focus, child.widget.submit_button.buttonId(), mouse)) try self.submitResolution(allocator); 2325 }, 2326 else => try child.widget.input(allocator, key, root_focus), 2327 } 2328 } 2329 2330 // a multiline input keeps enter and any up/down that has a row to move to 2331 fn multilineKeepsKey(text_input: *wgt.TextInput, allocator: std.mem.Allocator, key: Key) !bool { 2332 return switch (key) { 2333 .enter => true, 2334 .arrow_up => !try text_input.cursorOnFirstRow(allocator), 2335 .arrow_down => !try text_input.cursorOnLastRow(allocator), 2336 else => false, 2337 }; 2338 } 2339 2340 // the neighboring form control, skipping spacers and blank rows 2341 fn formStep(form: *wgt.Box(Widget), cur: usize, down: bool) ?usize { 2342 var i = cur; 2343 while (true) { 2344 if (down) { 2345 i += 1; 2346 if (i >= form.children.count()) return null; 2347 } else { 2348 if (i == 0) return null; 2349 i -= 1; 2350 } 2351 const child = &form.children.values()[i]; 2352 if (child.hidden) continue; 2353 switch (child.widget) { 2354 .spacer, .text => continue, 2355 else => return i, 2356 } 2357 } 2358 } 2359 2360 // focus the resolve-form child at `i` (a version row focuses its selected 2361 // child) and keep it visible on the terminal 2362 fn focusResolveChild(self: *This, form: *wgt.Box(Widget), i: usize, root_focus: *Focus) void { 2363 const child = &form.children.values()[i]; 2364 switch (child.widget) { 2365 .box => |*row| root_focus.setFocus(row.getFocus().child_id orelse form.children.keys()[i]), 2366 else => root_focus.setFocus(form.children.keys()[i]), 2367 } 2368 if (self.session.is_terminal) { 2369 if (self.resolveScroll()) |sc| { 2370 if (child.rect) |rect| sc.scrollToRect(rect); 2371 } 2372 } 2373 } 2374 2375 // re-emit the conflicted thread's event with the resolved content; any 2376 // event on the thread settles its conflict entry. this is the terminal 2377 // path; the web posts the form to the resolve route. 2378 fn submitResolution(self: *This, allocator: std.mem.Allocator) !void { 2379 if (comptime wasm) return; 2380 const io = self.session.io orelse return; 2381 const src = self.data.repo_source orelse return; 2382 const entry = self.data.selectedThread() orelse return; 2383 const author = (try self.session.eventAuthor()) orelse return; 2384 const form = self.formBox() orelse return; 2385 2386 // gather the inputs by name; the d<n> hunk inputs appear in chunk order 2387 var title: ?[]u8 = null; 2388 var tags: ?[]u8 = null; 2389 var hunks: std.ArrayList([]const u8) = .empty; 2390 defer { 2391 if (title) |t| allocator.free(t); 2392 if (tags) |t| allocator.free(t); 2393 for (hunks.items) |h| allocator.free(h); 2394 hunks.deinit(allocator); 2395 } 2396 for (form.children.values()) |*child| switch (child.widget) { 2397 .text_input => |*ti| { 2398 const text = try ti.text(allocator); 2399 errdefer allocator.free(text); 2400 if (std.mem.eql(u8, ti.options.name, "title")) { 2401 title = text; 2402 } else if (std.mem.eql(u8, ti.options.name, "tags")) { 2403 tags = text; 2404 } else { 2405 try hunks.append(allocator, text); 2406 } 2407 }, 2408 else => {}, 2409 }; 2410 2411 const id_bytes = try evt.parseEventId(entry.id); 2412 2413 switch (src.repo_kind) { 2414 inline else => |repo_kind| { 2415 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2416 defer any_repo.deinit(io, allocator); 2417 switch (any_repo) { 2418 inline else => |*repo| { 2419 var resolution: Event.Resolve = .{ 2420 .title = title, 2421 .tags = tags, 2422 .hunks = hunks.items, 2423 }; 2424 if (comptime @hasField(Event.Resolve, "theirs")) resolution.theirs = self.data.theirs_picks; 2425 const change = Event.Update{ .resolve = resolution }; 2426 Event.update(self.session.data.host_kind, repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, change, author) catch |err| switch (err) { 2427 // leave the form up for correction 2428 error.InvalidFields => return, 2429 else => |e| return e, 2430 }; 2431 }, 2432 } 2433 }, 2434 } 2435 2436 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2437 try self.session.navigate(route); 2438 } 2439 2440 // commit a new or edited comment and navigate to its permalink. this is the 2441 // terminal path; the web posts the form to its comment route. 2442 fn submitComment(self: *This, allocator: std.mem.Allocator) !void { 2443 if (comptime wasm) return; 2444 const io = self.session.io orelse return; 2445 const src = self.data.repo_source orelse return; 2446 const author = (try self.session.eventAuthor()) orelse return; 2447 const form = self.formBox() orelse return; 2448 const body_input = try formField(form, "body"); 2449 const body = try body_input.text(allocator); 2450 defer allocator.free(body); 2451 if (!evt.Comment.fieldsValid(body)) return; 2452 2453 var thread_id: [evt.event_id_size * 2]u8 = undefined; 2454 @memcpy(&thread_id, self.data.selected_id); 2455 const parent_id = if (self.data.comment_id.len == 0) thread_id else blk: { 2456 var parent: [evt.event_id_size * 2]u8 = undefined; 2457 @memcpy(&parent, self.data.comment_id); 2458 break :blk parent; 2459 }; 2460 var event_id_hex: [evt.event_id_size * 2]u8 = undefined; 2461 const editing = self.data.view == .edit_comment; 2462 switch (src.repo_kind) { 2463 inline else => |repo_kind| { 2464 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2465 defer any_repo.deinit(io, allocator); 2466 switch (any_repo) { 2467 inline else => |*repo| if (editing) { 2468 const comment_id = evt.parseEventId(self.data.comment_id) catch return; 2469 try evt.Comment.update(self.session.data.host_kind, repo_kind, repo.self_repo_opts, io, allocator, repo, &thread_id, &comment_id, body, author); 2470 event_id_hex = std.fmt.bytesToHex(comment_id, .lower); 2471 } else { 2472 event_id_hex = try evt.Comment.create(self.session.data.host_kind, repo_kind, repo.self_repo_opts, io, allocator, repo, &thread_id, &parent_id, body, author); 2473 }, 2474 } 2475 }, 2476 } 2477 2478 body_input.clear(allocator); 2479 const route = ui.RoutablePage.repoThreadCommentRoute(kind, self.data.identity, &thread_id, &event_id_hex, 0) orelse return; 2480 try self.session.navigate(route); 2481 } 2482 2483 // terminal form submissions; the web posts to the forms' routes instead 2484 fn submitForm(self: *This, allocator: std.mem.Allocator) !void { 2485 switch (self.data.view) { 2486 .new_comment, .edit_comment => try self.submitComment(allocator), 2487 .edit => try self.submitEditedThread(allocator), 2488 else => try self.submitNewThread(allocator), 2489 } 2490 } 2491 2492 // commit the new thread to the repo's events branch and navigate to it. 2493 // this is the terminal path; the web posts the form to the thread route, 2494 // so the wasm side never runs (or compiles) the repo access below. 2495 fn submitNewThread(self: *This, allocator: std.mem.Allocator) !void { 2496 if (comptime wasm) return; 2497 const io = self.session.io orelse return; 2498 const src = self.data.repo_source orelse return; 2499 const author = (try self.session.eventAuthor()) orelse return; 2500 2501 const form = self.threadForm() orelse return; 2502 const title_input = try formField(form, "title"); 2503 const tags_input = try formField(form, "tags"); 2504 const description_input = try formField(form, "description"); 2505 2506 const title = try title_input.text(allocator); 2507 defer allocator.free(title); 2508 const tags = try tags_input.text(allocator); 2509 defer allocator.free(tags); 2510 const description = try description_input.text(allocator); 2511 defer allocator.free(description); 2512 const target_branch = if (comptime supports_drafts) try (try formField(form, "target_branch")).text(allocator) else ""; 2513 defer if (comptime supports_drafts) allocator.free(target_branch); 2514 2515 if (!Event.fieldsValid(title, tags)) { 2516 if (!evt.titleValid(title)) try self.rememberThreadFeedback(.required_title, title, tags, description, target_branch); 2517 return; 2518 } 2519 2520 if (comptime supports_drafts) { 2521 const source = formWidget(form, .patch_source) orelse return error.MissingFormField; 2522 const source_branch = switch (source.selected) { 2523 .fork => null, 2524 .branch => try source.field().text(allocator), 2525 }; 2526 defer if (source_branch) |branch| allocator.free(branch); 2527 const event_id_hex = Data.create(self.data, self.session, allocator, title, tags, description, target_branch, source_branch) catch |err| { 2528 const failure = FeedbackFailure.fromError(err) orelse return err; 2529 try self.rememberThreadFeedback(failure, title, tags, description, target_branch); 2530 return; 2531 }; 2532 title_input.clear(allocator); 2533 tags_input.clear(allocator); 2534 description_input.clear(allocator); 2535 (try formField(form, "target_branch")).clear(allocator); 2536 const route = ui.RoutablePage.repoThreadCommentsRoute(kind, self.data.identity, &event_id_hex, 0) orelse return; 2537 try self.session.navigate(route); 2538 return; 2539 } 2540 2541 var id_bytes: [evt.event_id_size]u8 = undefined; 2542 io.random(&id_bytes); 2543 const event_id_hex = std.fmt.bytesToHex(id_bytes, .lower); 2544 2545 const event = evt.EventWithId{ 2546 .id = event_id_hex, 2547 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 2548 .author = author, 2549 .event = @unionInit(evt.Event, @tagName(kind), Event{ 2550 .title = title, 2551 .description = description, 2552 .tags = tags, 2553 }), 2554 }; 2555 2556 switch (src.repo_kind) { 2557 inline else => |repo_kind| { 2558 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2559 defer any_repo.deinit(io, allocator); 2560 switch (any_repo) { 2561 inline else => |*repo| try evt.consume(self.session.data.host_kind, .repo, repo_kind, repo.self_repo_opts, io, allocator, repo, evt.events_ref, &.{event}), 2562 } 2563 }, 2564 } 2565 2566 // wipe the form so a return visit starts fresh 2567 title_input.clear(allocator); 2568 tags_input.clear(allocator); 2569 description_input.clear(allocator); 2570 2571 const route = listRoute(self.data.identity, @enumFromInt(0), "", &event_id_hex) orelse return; 2572 try self.session.navigate(route); 2573 } 2574 2575 // re-emit the selected thread's event with the form's content (status 2576 // preserved), then reload the thread's page. this is the terminal path; 2577 // the web posts the form to the edit route. 2578 fn submitEditedThread(self: *This, allocator: std.mem.Allocator) !void { 2579 if (comptime wasm) return; 2580 const io = self.session.io orelse return; 2581 const entry = self.data.selectedThread() orelse return; 2582 2583 const form = self.threadForm() orelse return; 2584 const title_input = try formField(form, "title"); 2585 const tags_input = try formField(form, "tags"); 2586 const description_input = try formField(form, "description"); 2587 2588 const title = try title_input.text(allocator); 2589 defer allocator.free(title); 2590 const tags = try tags_input.text(allocator); 2591 defer allocator.free(tags); 2592 const description = try description_input.text(allocator); 2593 defer allocator.free(description); 2594 const target_branch = if (comptime supports_drafts) try (try formField(form, "target_branch")).text(allocator) else ""; 2595 defer if (comptime supports_drafts) allocator.free(target_branch); 2596 2597 if (!Event.fieldsValid(title, tags)) { 2598 if (!evt.titleValid(title)) try self.rememberThreadFeedback(.required_title, title, tags, description, target_branch); 2599 return; 2600 } 2601 const src = self.data.repo_source orelse return; 2602 2603 if (comptime supports_drafts) { 2604 if (entryDraft(entry.*)) { 2605 self.data.editDraft(self.session, allocator, entry.id, title, tags, description, target_branch) catch |err| { 2606 const failure = FeedbackFailure.fromError(err) orelse return err; 2607 try self.rememberThreadFeedback(failure, title, tags, description, target_branch); 2608 return; 2609 }; 2610 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2611 try self.session.navigate(route); 2612 return; 2613 } 2614 } 2615 2616 const id_bytes = try evt.parseEventId(entry.id); 2617 const author = (try self.session.eventAuthor()) orelse return; 2618 2619 switch (src.repo_kind) { 2620 inline else => |repo_kind| { 2621 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2622 defer any_repo.deinit(io, allocator); 2623 switch (any_repo) { 2624 inline else => |*repo| if (supports_drafts) { 2625 Event.update(self.session.data.host_kind, repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 2626 .title = title, 2627 .tags = tags, 2628 .description = description, 2629 .target_branch = target_branch, 2630 } }, author) catch |err| { 2631 const failure = FeedbackFailure.fromError(err) orelse return err; 2632 try self.rememberThreadFeedback(failure, title, tags, description, target_branch); 2633 return; 2634 }; 2635 } else if (has_status) 2636 try Event.update(self.session.data.host_kind, repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 2637 .title = title, 2638 .tags = tags, 2639 .description = description, 2640 } }, author) 2641 else 2642 try Event.update(self.session.data.host_kind, repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, title, tags, description, author), 2643 } 2644 }, 2645 } 2646 2647 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2648 try self.session.navigate(route); 2649 } 2650 2651 fn rememberThreadFeedback(self: *This, failure: FeedbackFailure, title: []const u8, tags: []const u8, description: []const u8, target_branch: []const u8) !void { 2652 const aa = self.session.arena.allocator(); 2653 const source = if (supports_drafts) blk: { 2654 const form = self.threadForm() orelse break :blk null; 2655 break :blk formWidget(form, .patch_source); 2656 } else null; 2657 self.session.data.form_feedback = @unionInit(ui.Session.FormFeedback, @tagName(feedback_tag), .{ 2658 .failure = failure, 2659 .fields = if (comptime supports_drafts) .{ 2660 .title = try aa.dupe(u8, title), 2661 .tags = try aa.dupe(u8, tags), 2662 .description = try aa.dupe(u8, description), 2663 .target_branch = try aa.dupe(u8, target_branch), 2664 .source_branch = if (source) |value| try value.field().text(aa) else "", 2665 .source_kind = if (source) |value| value.selected else .fork, 2666 } else .{ 2667 .title = try aa.dupe(u8, title), 2668 .tags = try aa.dupe(u8, tags), 2669 .description = try aa.dupe(u8, description), 2670 }, 2671 }); 2672 } 2673 2674 fn submitConfirmation(self: *This, allocator: std.mem.Allocator, button_index: usize) !void { 2675 switch (confirmation(self.data.view) orelse return) { 2676 .publish => if (comptime supports_drafts) { 2677 if (comptime wasm) return; 2678 const entry = self.data.selectedThread() orelse return; 2679 if (!entryDraft(entry.*)) return; 2680 try self.data.publishDraft(self.session, allocator, entry.id); 2681 const route = listRoute(self.data.identity, @enumFromInt(0), "", entry.id) orelse return; 2682 try self.session.navigate(route); 2683 } else unreachable, 2684 .merge => if (comptime supports_merge) { 2685 if (comptime wasm) return; 2686 const entry = self.data.selectedThread() orelse return; 2687 const revision: evt.Patch.MergeRevision = if (button_index > 1) .squash else .source; 2688 if (!Data.canMerge(entry.*, self.session) or entry.mergeability.get(revision) != .clean) return; 2689 self.data.mergePatch(self.session, allocator, entry.id, revision) catch |err| switch (err) { 2690 error.MergeConflict, error.MergeCheckUnavailable, error.PatchOutOfDate, error.PatchDataUnavailable => { 2691 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2692 try self.session.navigate(route); 2693 return; 2694 }, 2695 else => return err, 2696 }; 2697 const merged = std.meta.stringToEnum(Status, "merged") orelse return; 2698 const route = listRoute(self.data.identity, merged, "", entry.id) orelse return; 2699 try self.session.navigate(route); 2700 } else unreachable, 2701 .remove => try self.submitRemove(allocator), 2702 } 2703 } 2704 2705 fn submitRemove(self: *This, allocator: std.mem.Allocator) !void { 2706 const comment = self.data.comment_id.len != 0; 2707 const event_id = if (comment) self.data.comment_id else self.data.selected_id; 2708 const id = evt.parseEventId(event_id) catch return; 2709 try self.removeEvent(allocator, if (comment) .comment else kind, id); 2710 } 2711 2712 fn removeEvent(self: *This, allocator: std.mem.Allocator, event_kind: evt.EventKind, id: [evt.event_id_size]u8) !void { 2713 if (comptime wasm) return; 2714 2715 if (comptime supports_drafts) { 2716 const selected = self.data.selectedThread() orelse return; 2717 if (event_kind == kind and entryDraft(selected.*)) { 2718 try Data.removeDraft(self.session, allocator, &id); 2719 const route = draftsRoute(self.data.identity) orelse return; 2720 try self.session.navigate(route); 2721 return; 2722 } 2723 } 2724 2725 const io = self.session.io orelse return; 2726 const src = self.data.repo_source orelse return; 2727 const author = (try self.session.eventAuthor()) orelse return; 2728 2729 switch (src.repo_kind) { 2730 inline else => |repo_kind| { 2731 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2732 defer any_repo.deinit(io, allocator); 2733 switch (any_repo) { 2734 inline else => |*repo| try evt.remove(self.session.data.host_kind, .repo, repo_kind, repo.self_repo_opts, io, allocator, repo, &id, event_kind, author), 2735 } 2736 }, 2737 } 2738 2739 const id_hex = std.fmt.bytesToHex(id, .lower); 2740 const route = ui.RoutablePage.repoEventsRoute(self.data.identity, .removed, event_kind, &id_hex) orelse return; 2741 try self.session.navigate(route); 2742 } 2743 2744 // return to the list. 2745 fn focusList(self: *This, index: usize, root_focus: *Focus) void { 2746 root_focus.setFocus(self.listScroll(index).getFocus().id); 2747 } 2748 2749 // cross to the header tabs above the stack. 2750 fn focusHeader(self: *This, root_focus: *Focus) void { 2751 root_focus.setFocus(self.box.children.keys()[header_index]); 2752 } 2753 2754 pub fn clearGrid(self: *This) void { 2755 self.box.clearGrid(); 2756 } 2757 2758 pub fn getGrid(self: This) ?Grid { 2759 return self.box.getGrid(); 2760 } 2761 2762 pub fn getFocus(self: *This) *Focus { 2763 return self.box.getFocus(); 2764 } 2765 2766 // for the parent's "scroll up at the top jumps to the header" check: at the 2767 // top only while the header tabs hold focus, so up from the split 2768 // crosses into the tabs first. 2769 pub fn atTop(self: *This) bool { 2770 return self.headerActive(); 2771 } 2772 }; 2773 } 2774