╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ thread.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2083 // focus the resolve-form child at `i` (a version row focuses its selected 2084 // child) and keep it visible on the terminal 2085 fn focusResolveChild(self: *This, form: *wgt.Box(Widget), i: usize, root_focus: *Focus) void { 2086 const child = &form.children.values()[i]; 2087 switch (child.widget) { 2088 .box => |*row| root_focus.setFocus(row.getFocus().child_id orelse form.children.keys()[i]), 2089 else => root_focus.setFocus(form.children.keys()[i]), 2090 } 2091 if (self.session.is_terminal) { 2092 if (self.resolveScroll()) |sc| { 2093 if (child.rect) |rect| sc.scrollToRect(rect); 2094 } 2095 } 2096 } 2097 2098 // re-emit the conflicted thread's event with the resolved content; any 2099 // event on the thread settles its conflict entry. this is the terminal 2100 // path; the web posts the form to the resolve route. 2101 fn submitResolution(self: *This, allocator: std.mem.Allocator) !void { 2102 if (comptime wasm) return; 2103 const io = self.session.io orelse return; 2104 const src = self.data.repo_source orelse return; 2105 const entry = self.data.selectedThread() orelse return; 2106 const author = (try self.session.eventAuthor()) orelse return; 2107 const form = self.threadForm() orelse return; 2108 2109 // gather the inputs by name; the d<n> hunk inputs appear in chunk order 2110 var title: ?[]u8 = null; 2111 var tags: ?[]u8 = null; 2112 var hunks: std.ArrayList([]const u8) = .empty; 2113 defer { 2114 if (title) |t| allocator.free(t); 2115 if (tags) |t| allocator.free(t); 2116 for (hunks.items) |h| allocator.free(h); 2117 hunks.deinit(allocator); 2118 } 2119 for (form.children.values()) |*child| switch (child.widget) { 2120 .text_input => |*ti| { 2121 const text = try ti.text(allocator); 2122 errdefer allocator.free(text); 2123 if (std.mem.eql(u8, ti.options.name, "title")) { 2124 title = text; 2125 } else if (std.mem.eql(u8, ti.options.name, "tags")) { 2126 tags = text; 2127 } else { 2128 try hunks.append(allocator, text); 2129 } 2130 }, 2131 else => {}, 2132 }; 2133 2134 const id_bytes = try evt.parseEventId(entry.id); 2135 2136 switch (src.repo_kind) { 2137 inline else => |repo_kind| { 2138 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2139 defer any_repo.deinit(io, allocator); 2140 switch (any_repo) { 2141 inline else => |*repo| { 2142 var resolution: Event.Resolve = .{ 2143 .title = title, 2144 .tags = tags, 2145 .hunks = hunks.items, 2146 }; 2147 if (comptime @hasField(Event.Resolve, "theirs")) resolution.theirs = self.data.theirs_picks; 2148 const change = Event.Update{ .resolve = resolution }; 2149 Event.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, change, author) catch |err| switch (err) { 2150 // leave the form up for correction 2151 error.InvalidFields => return, 2152 else => |e| return e, 2153 }; 2154 }, 2155 } 2156 }, 2157 } 2158 2159 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2160 try self.session.navigate(route); 2161 } 2162 2163 // commit a new or edited comment and navigate to its permalink. this is the 2164 // terminal path; the web posts the form to its comment route. 2165 fn submitComment(self: *This, allocator: std.mem.Allocator) !void { 2166 if (comptime wasm) return; 2167 const io = self.session.io orelse return; 2168 const src = self.data.repo_source orelse return; 2169 const author = (try self.session.eventAuthor()) orelse return; 2170 const form = self.threadForm() orelse return; 2171 const body_input = &form.children.values()[comment_body_field_index].widget.text_input; 2172 const body = try body_input.text(allocator); 2173 defer allocator.free(body); 2174 if (!evt.Comment.fieldsValid(body)) return; 2175 2176 var thread_id: [evt.event_id_size * 2]u8 = undefined; 2177 @memcpy(&thread_id, self.data.selected_id); 2178 const parent_id = if (self.data.comment_id.len == 0) thread_id else blk: { 2179 var parent: [evt.event_id_size * 2]u8 = undefined; 2180 @memcpy(&parent, self.data.comment_id); 2181 break :blk parent; 2182 }; 2183 var event_id_hex: [evt.event_id_size * 2]u8 = undefined; 2184 const editing = self.data.view == .edit_comment; 2185 switch (src.repo_kind) { 2186 inline else => |repo_kind| { 2187 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2188 defer any_repo.deinit(io, allocator); 2189 switch (any_repo) { 2190 inline else => |*repo| if (editing) { 2191 const comment_id = evt.parseEventId(self.data.comment_id) catch return; 2192 try evt.Comment.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &thread_id, &comment_id, body, author); 2193 event_id_hex = std.fmt.bytesToHex(comment_id, .lower); 2194 } else { 2195 event_id_hex = try evt.Comment.create(repo_kind, repo.self_repo_opts, io, allocator, repo, &thread_id, &parent_id, body, author); 2196 }, 2197 } 2198 }, 2199 } 2200 2201 body_input.clear(allocator); 2202 const route = ui.RoutablePage.repoThreadCommentRoute(kind, self.data.identity, &thread_id, &event_id_hex, 0) orelse return; 2203 try self.session.navigate(route); 2204 } 2205 2206 // the terminal submit paths: the new form commits a new thread event, the 2207 // edit form re-emits the selected thread's event with the form's content. 2208 // the web posts the forms to their routes instead. 2209 fn submitForm(self: *This, allocator: std.mem.Allocator) !void { 2210 if (self.data.view == .edit) { 2211 try self.submitEditedThread(allocator); 2212 } else { 2213 try self.submitNewThread(allocator); 2214 } 2215 } 2216 2217 // commit the new thread to the repo's events branch and navigate to it. 2218 // this is the terminal path; the web posts the form to the thread route, 2219 // so the wasm side never runs (or compiles) the repo access below. 2220 fn submitNewThread(self: *This, allocator: std.mem.Allocator) !void { 2221 if (comptime wasm) return; 2222 const io = self.session.io orelse return; 2223 const src = self.data.repo_source orelse return; 2224 const author = (try self.session.eventAuthor()) orelse return; 2225 2226 const form = self.threadForm() orelse return; 2227 const title_input = &form.children.values()[title_field_index].widget.text_input; 2228 const tags_input = &form.children.values()[tags_field_index].widget.text_input; 2229 const description_input = &form.children.values()[description_field_index].widget.text_input; 2230 2231 const title = try title_input.text(allocator); 2232 defer allocator.free(title); 2233 const tags = try tags_input.text(allocator); 2234 defer allocator.free(tags); 2235 const description = try description_input.text(allocator); 2236 defer allocator.free(description); 2237 2238 if (!Event.fieldsValid(title, tags)) return; 2239 2240 if (comptime supports_drafts) { 2241 const event_id_hex = try Data.createDraft(self.data, self.session, allocator, title, tags, description); 2242 title_input.clear(allocator); 2243 tags_input.clear(allocator); 2244 description_input.clear(allocator); 2245 const route = ui.RoutablePage.repoThreadCommentsRoute(kind, self.data.identity, &event_id_hex, 0) orelse return; 2246 try self.session.navigate(route); 2247 return; 2248 } 2249 2250 var id_bytes: [evt.event_id_size]u8 = undefined; 2251 io.random(&id_bytes); 2252 const event_id_hex = std.fmt.bytesToHex(id_bytes, .lower); 2253 2254 const event = evt.EventWithId{ 2255 .id = event_id_hex, 2256 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 2257 .author = author, 2258 .event = @unionInit(evt.Event, @tagName(kind), Event{ 2259 .title = title, 2260 .description = description, 2261 .tags = tags, 2262 }), 2263 }; 2264 2265 switch (src.repo_kind) { 2266 inline else => |repo_kind| { 2267 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2268 defer any_repo.deinit(io, allocator); 2269 switch (any_repo) { 2270 inline else => |*repo| try evt.consume(.repo, repo_kind, repo.self_repo_opts, io, allocator, repo, evt.events_ref, &.{event}), 2271 } 2272 }, 2273 } 2274 2275 // wipe the form so a return visit starts fresh 2276 title_input.clear(allocator); 2277 tags_input.clear(allocator); 2278 description_input.clear(allocator); 2279 2280 const route = listRoute(self.data.identity, @enumFromInt(0), "", &event_id_hex) orelse return; 2281 try self.session.navigate(route); 2282 } 2283 2284 // re-emit the selected thread's event with the form's content (status 2285 // preserved), then reload the thread's page. this is the terminal path; 2286 // the web posts the form to the edit route. 2287 fn submitEditedThread(self: *This, allocator: std.mem.Allocator) !void { 2288 if (comptime wasm) return; 2289 const io = self.session.io orelse return; 2290 const src = self.data.repo_source orelse return; 2291 const entry = self.data.selectedThread() orelse return; 2292 const author = (try self.session.eventAuthor()) orelse return; 2293 2294 const form = self.threadForm() orelse return; 2295 const title_input = &form.children.values()[title_field_index].widget.text_input; 2296 const tags_input = &form.children.values()[tags_field_index].widget.text_input; 2297 const description_input = &form.children.values()[description_field_index].widget.text_input; 2298 2299 const title = try title_input.text(allocator); 2300 defer allocator.free(title); 2301 const tags = try tags_input.text(allocator); 2302 defer allocator.free(tags); 2303 const description = try description_input.text(allocator); 2304 defer allocator.free(description); 2305 2306 if (!Event.fieldsValid(title, tags)) return; 2307 2308 const id_bytes = try evt.parseEventId(entry.id); 2309 2310 switch (src.repo_kind) { 2311 inline else => |repo_kind| { 2312 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2313 defer any_repo.deinit(io, allocator); 2314 switch (any_repo) { 2315 inline else => |*repo| if (has_status) 2316 try Event.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 2317 .title = title, 2318 .tags = tags, 2319 .description = description, 2320 } }, author) 2321 else 2322 try Event.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, title, tags, description, author), 2323 } 2324 }, 2325 } 2326 2327 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2328 try self.session.navigate(route); 2329 } 2330 2331 fn submitRemove(self: *This, allocator: std.mem.Allocator) !void { 2332 const comment = self.data.comment_id.len != 0; 2333 const event_id = if (comment) self.data.comment_id else self.data.selected_id; 2334 const id = evt.parseEventId(event_id) catch return; 2335 try self.removeEvent(allocator, if (comment) .comment else kind, id); 2336 } 2337 2338 fn removeEvent(self: *This, allocator: std.mem.Allocator, event_kind: evt.EventKind, id: [evt.event_id_size]u8) !void { 2339 if (comptime wasm) return; 2340 2341 if (comptime supports_drafts) { 2342 const selected = self.data.selectedThread() orelse return; 2343 if (event_kind == kind and entryDraft(selected.*)) { 2344 try Data.removeDraft(self.session, allocator, &id); 2345 const route = draftsRoute(self.data.identity) orelse return; 2346 try self.session.navigate(route); 2347 return; 2348 } 2349 } 2350 2351 const io = self.session.io orelse return; 2352 const src = self.data.repo_source orelse return; 2353 const author = (try self.session.eventAuthor()) orelse return; 2354 2355 switch (src.repo_kind) { 2356 inline else => |repo_kind| { 2357 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2358 defer any_repo.deinit(io, allocator); 2359 switch (any_repo) { 2360 inline else => |*repo| try evt.remove(.repo, repo_kind, repo.self_repo_opts, io, allocator, repo, &id, event_kind, author), 2361 } 2362 }, 2363 } 2364 2365 const id_hex = std.fmt.bytesToHex(id, .lower); 2366 const route = ui.RoutablePage.repoEventsRoute(self.data.identity, .removed, event_kind, &id_hex) orelse return; 2367 try self.session.navigate(route); 2368 } 2369 2370 // flip the shown thread's status by re-emitting its event, then reload the 2371 // page rooted at the thread so the view reflects the change. this is the 2372 // terminal path; the web posts the button's form to the status route. 2373 fn toggleStatus(self: *This, allocator: std.mem.Allocator, index: usize) !void { 2374 if (comptime !has_status) return; 2375 if (comptime wasm) return; 2376 const io = self.session.io orelse return; 2377 const src = self.data.repo_source orelse return; 2378 const sel = self.detailed_index[index] orelse return; 2379 const entry = self.window(index).items[sel]; 2380 const author = (try self.session.eventAuthor()) orelse return; 2381 2382 const status = (statusChange(entryStatus(entry)) orelse return).status; 2383 2384 const id_bytes = try evt.parseEventId(entry.id); 2385 2386 switch (src.repo_kind) { 2387 inline else => |repo_kind| { 2388 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2389 defer any_repo.deinit(io, allocator); 2390 switch (any_repo) { 2391 inline else => |*repo| try Event.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .status = status }, author), 2392 } 2393 }, 2394 } 2395 2396 const route = listRoute(self.data.identity, status, "", entry.id) orelse return; 2397 try self.session.navigate(route); 2398 } 2399 2400 // arrow keys move the tag selection; at the flow's edges focus crosses to 2401 // the neighboring widgets. 2402 fn tagsInput(self: *This, index: usize, child_index: usize, tf: *TagFlow, key: Key, root_focus: *Focus) void { 2403 const cid = tf.focus.child_id orelse return; 2404 const cur = tf.indexOfFocusId(cid) orelse return; 2405 const count = tf.text_boxes.items.len; 2406 switch (key) { 2407 .arrow_left => if (cur > 0) self.focusTag(index, child_index, tf, root_focus, cur - 1) else self.focusList(index, root_focus), 2408 .arrow_right => if (cur + 1 < count) self.focusTag(index, child_index, tf, root_focus, cur + 1), 2409 .arrow_up => if (tf.rowStep(cur, false)) |i| { 2410 self.focusTag(index, child_index, tf, root_focus, i); 2411 } else { 2412 _ = self.moveDetailVertical(index, root_focus, false); 2413 }, 2414 .arrow_down => if (tf.rowStep(cur, true)) |i| { 2415 self.focusTag(index, child_index, tf, root_focus, i); 2416 } else { 2417 _ = self.moveDetailVertical(index, root_focus, true); 2418 }, 2419 .home => self.focusTag(index, child_index, tf, root_focus, 0), 2420 .end => self.focusTag(index, child_index, tf, root_focus, count - 1), 2421 else => {}, 2422 } 2423 } 2424 2425 // the tool row's first child after the spacer that pushes it right 2426 const first_in_row_index: usize = 1; 2427 2428 fn toolRowFocused(self: *This, index: usize) bool { 2429 const outer = self.detailOuter(index); 2430 const cid = outer.getFocus().child_id orelse return false; 2431 return outer.children.getIndex(cid) == tool_row_index; 2432 } 2433 2434 fn focusTag(self: *This, index: usize, child_index: usize, tf: *TagFlow, root_focus: *Focus, item: usize) void { 2435 root_focus.setFocus(tf.text_boxes.items[item].getFocus().id); 2436 // keep the tag visible on the terminal: its rect offset by the flow's 2437 // position in the pane. 2438 if (self.session.is_terminal and item < tf.rects.items.len) { 2439 if (self.detailInner(index).children.values()[child_index].rect) |flow_rect| { 2440 var rect = tf.rects.items[item]; 2441 rect.x += flow_rect.x; 2442 rect.y += flow_rect.y; 2443 self.detailScroll(index).scrollToRect(rect); 2444 } 2445 } 2446 } 2447 2448 // return to the tool row's last-focused button (the header when the 2449 // description page shows no row). 2450 fn focusToolRow(self: *This, index: usize, root_focus: *Focus) void { 2451 const cid = self.toolRow(index).getFocus().child_id orelse return self.focusHeader(root_focus); 2452 root_focus.setFocus(cid); 2453 } 2454 2455 // enter the detail pane. an empty pane can't be entered. 2456 fn focusDetail(self: *This, index: usize, root_focus: *Focus) void { 2457 if (self.detailInner(index).children.count() == 0) return; 2458 root_focus.setFocus(self.detailOuter(index).getFocus().id); 2459 } 2460 2461 // return to the list. 2462 fn focusList(self: *This, index: usize, root_focus: *Focus) void { 2463 root_focus.setFocus(self.listScroll(index).getFocus().id); 2464 } 2465 2466 // cross to the header tabs above the stack. 2467 fn focusHeader(self: *This, root_focus: *Focus) void { 2468 root_focus.setFocus(self.box.children.keys()[header_index]); 2469 } 2470 2471 pub fn clearGrid(self: *This) void { 2472 self.box.clearGrid(); 2473 } 2474 2475 pub fn getGrid(self: This) ?Grid { 2476 return self.box.getGrid(); 2477 } 2478 2479 pub fn getFocus(self: *This) *Focus { 2480 return self.box.getFocus(); 2481 } 2482 2483 // for the parent's "scroll up at the top jumps to the header" check: at the 2484 // top only while the header tabs hold focus, so up from the split 2485 // crosses into the tabs first. 2486 pub fn getSelectedIndex(self: *This) ?usize { 2487 return if (self.headerActive()) 0 else 1; 2488 } 2489 }; 2490 } 2491