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