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