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