╭ Issues.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1 const std = @import("std"); 2 const evt = @import("../../event.zig"); 3 const ui = @import("../../ui.zig"); 4 const xit = @import("xit"); 5 const rp = xit.repo; 6 const hash = xit.hash; 7 const xitui = xit.xitui; 8 const wgt = xitui.widget; 9 const layout = xitui.layout; 10 const Key = xitui.input.Key; 11 const Grid = xitui.grid.Grid; 12 const Focus = xitui.focus.Focus; 13 const inp = @import("../input.zig"); 14 15 // how many issues one window shows before a "next" link appears. 16 pub const page_size = 20; 17 18 // one issue from the repo's consumed event database, with its hex event id 19 // (the id lives in the event envelope, not the payload). 20 pub const IssueWithId = struct { 21 id: []const u8, 22 issue: evt.Issue, 23 }; 24 25 // "owner/name", so the view can build /repo/owner/name/issues/... links. 26 identity: []const u8, 27 // the url-encoded tag the list is filtered to ("" = unfiltered). 28 tag: []const u8, 29 // the hex event id of the issue this window is rooted at ("" = the first 30 // window), mirrored into the url. 31 selected_id: []const u8, 32 issues: []const IssueWithId, 33 // the id of the previous window's first issue ("" = the bare first window), or 34 // null when this window is already the first. 35 prev_id: ?[]const u8, 36 // the id of the next window's first issue, or null when this is the last window. 37 next_id: ?[]const u8, 38 39 const Self = @This(); 40 41 pub fn init( 42 arena: *std.heap.ArenaAllocator, 43 session: *ui.Session, 44 event_id: *const [evt.event_id_size]u8, 45 identity: []const u8, 46 tag: []const u8, 47 selected_id: []const u8, 48 ) !Self { 49 const aa = arena.allocator(); 50 const empty: Self = .{ 51 .identity = try aa.dupe(u8, identity), 52 .tag = try aa.dupe(u8, tag), 53 .selected_id = try aa.dupe(u8, selected_id), 54 .issues = &.{}, 55 .prev_id = null, 56 .next_id = null, 57 }; 58 59 // no filesystem (wasm) or nowhere to look: empty listing. the wasm path 60 // never calls init anyway — it rebuilds from the serialized snapshot. 61 const io = session.io orelse return empty; 62 const repos_dir = session.repos_dir orelse return empty; 63 64 // the repo's working copy lives at <repos_dir>/<hex event id>. 65 const hex = std.fmt.bytesToHex(event_id.*, .lower); 66 const repo_path = try std.fs.path.join(aa, &.{ repos_dir, &hex }); 67 68 // open with the arena's backing allocator (transient; the issue strings are 69 // duped into the page arena so they outlive the repo handle). 70 const gpa = arena.child_allocator; 71 var any_repo = rp.AnyRepo(.xit, .{}).open(io, gpa, .{ .path = repo_path }) catch return empty; 72 defer any_repo.deinit(io, gpa); 73 74 return switch (any_repo) { 75 inline else => |*repo| collect(repo.self_repo_opts, arena, repo, empty), 76 }; 77 } 78 79 // read one window of the repo's issues (filtered to empty.tag when set), 80 // ordered by creation time (oldest first), starting at the issue 81 // empty.selected_id names ("" = the beginning). 82 fn collect( 83 comptime repo_opts: rp.RepoOpts(.xit), 84 arena: *std.heap.ArenaAllocator, 85 repo: *rp.Repo(.xit, repo_opts), 86 empty: Self, 87 ) !Self { 88 const aa = arena.allocator(); 89 const DB = rp.Repo(.xit, repo_opts).DB; 90 const rooted = empty.selected_id.len != 0; 91 const tagged = empty.tag.len != 0; 92 // an explicitly named issue or tag that doesn't exist is a bad url 93 // (NotFound -> 404); the bare route falls through to an empty listing. 94 const strict = rooted or tagged; 95 96 // a repo with no consumed events has no moment yet. 97 const haxy_moment = evt.currentMoment(repo_opts, repo) catch { 98 if (strict) return error.NotFound; 99 return empty; 100 }; 101 102 // the ordered set to window: the tag's issue set when filtered, else the 103 // full issue set. 104 const issue_id_set_cursor = blk: { 105 if (tagged) { 106 const tag_to_issues_cursor = try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, "tag->issue-id-set")) orelse return error.NotFound; 107 const tag_to_issues = try DB.SortedMap(.read_only).init(tag_to_issues_cursor); 108 const decoded = std.Uri.percentDecodeInPlace(try aa.dupe(u8, empty.tag)); 109 break :blk (try tag_to_issues.getCursor(decoded)) orelse return error.NotFound; 110 } 111 break :blk (try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, "issue-id-set"))) orelse { 112 if (strict) return error.NotFound; 113 return empty; 114 }; 115 }; 116 const issue_id_set = try DB.SortedSet(.read_only).init(issue_id_set_cursor); 117 118 const event_id_to_issue_cursor = try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, "event-id->issue")) orelse { 119 if (strict) return error.NotFound; 120 return empty; 121 }; 122 const event_id_to_issue = try DB.HashMap(.read_only).init(event_id_to_issue_cursor); 123 124 // seek once to the window start: the named issue's order key (its recorded 125 // creation timestamp plus its id, so the seek lands on it), or the set's 126 // first entry. 127 var prev_id: ?[]const u8 = null; 128 var iter = if (!rooted) 129 try issue_id_set.iteratorFromIndex(0) 130 else blk: { 131 if (empty.selected_id.len != evt.event_id_size * 2) return error.NotFound; 132 var id_bytes: [evt.event_id_size]u8 = undefined; 133 _ = std.fmt.hexToBytes(&id_bytes, empty.selected_id) catch return error.NotFound; 134 const issue_cursor = try event_id_to_issue.getCursor(hash.hashInt(repo_opts.hash, &id_bytes)) orelse return error.NotFound; 135 const issue_map = try DB.HashMap(.read_only).init(issue_cursor); 136 const issue_event = try evt.read(evt.Issue, DB, repo_opts.hash, arena, issue_map); 137 const order_key = evt.orderKey(issue_event.created_ts, &id_bytes); 138 139 // the named issue must be in the windowed set (a tag url can name an 140 // issue that doesn't carry the tag). 141 if (!try issue_id_set.contains(&order_key)) return error.NotFound; 142 143 // the previous window starts page_size ranks back. 144 const rank = try issue_id_set.rank(&order_key); 145 if (rank > 0) { 146 const prev_rank = rank -| page_size; 147 if (prev_rank == 0) { 148 prev_id = ""; 149 } else { 150 const kv = try issue_id_set.getIndexKeyValuePair(@intCast(prev_rank)) orelse return error.NotFound; 151 var prev_key: [@sizeOf(u64) + evt.event_id_size]u8 = undefined; 152 _ = try kv.key_cursor.readBytes(&prev_key); 153 const prev_hex = std.fmt.bytesToHex(prev_key[@sizeOf(u64)..].*, .lower); 154 prev_id = try aa.dupe(u8, &prev_hex); 155 } 156 } 157 158 break :blk try issue_id_set.iteratorFrom(&order_key); 159 }; 160 161 // collect this window's issues, plus a peek at the one after it (its id is 162 // the next window's start). the set's keys are orderKey 163 // ([timestamp][event-id]); the trailing bytes of each key are the issue 164 // event id. 165 var issues: std.ArrayList(IssueWithId) = .empty; 166 var next_id: ?[]const u8 = null; 167 while (try iter.next()) |id_cursor_val| { 168 var id_cursor = id_cursor_val; 169 const id_kv = try id_cursor.readKeyValuePair(); 170 var order_key: [@sizeOf(u64) + evt.event_id_size]u8 = undefined; 171 _ = try id_kv.key_cursor.readBytes(&order_key); 172 const id_hex = std.fmt.bytesToHex(order_key[@sizeOf(u64)..].*, .lower); 173 if (issues.items.len == page_size) { 174 next_id = try aa.dupe(u8, &id_hex); 175 break; 176 } 177 const issue_cursor = try event_id_to_issue.getCursor(hash.hashInt(repo_opts.hash, order_key[@sizeOf(u64)..])) orelse continue; 178 const issue_map = try DB.HashMap(.read_only).init(issue_cursor); 179 try issues.append(aa, .{ 180 .id = try aa.dupe(u8, &id_hex), 181 .issue = try evt.read(evt.Issue, DB, repo_opts.hash, arena, issue_map), 182 }); 183 } 184 185 return .{ 186 .identity = empty.identity, 187 .tag = empty.tag, 188 .selected_id = empty.selected_id, 189 .issues = issues.items, 190 .prev_id = prev_id, 191 .next_id = next_id, 192 }; 193 } 194 195 pub const View = struct { 196 // a horizontal split with the issue list on the left and a detail pane on 197 // the right showing the selected issue's description. 198 box: wgt.Box(ui.Widget), 199 data: *const Self, 200 session: *ui.Session, 201 // the issue whose description the pane currently shows (index into data.issues). 202 detailed_index: ?usize, 203 204 const list_index: usize = 0; 205 const detail_index: usize = 1; 206 const list_max_width: usize = 40; 207 const detail_min_width: usize = 40; 208 209 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { 210 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 211 errdefer box.deinit(allocator); 212 213 // the issue list (one focusable row per title), plus a "next" link that 214 // reloads the page rooted at the following issue. 215 { 216 var list_scroll = blk: { 217 var list_box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 218 errdefer list_box.deinit(allocator); 219 if (data.prev_id) |prev| 220 try addRow(allocator, &list_box, "← previous", try issuesLink(session.page_arena, data.identity, data.tag, prev)); 221 for (data.issues) |entry| 222 try addRow(allocator, &list_box, entry.issue.title, try issueRowLink(session.page_arena, data.identity, entry.id)); 223 if (data.next_id) |next| 224 try addRow(allocator, &list_box, "next →", try issuesLink(session.page_arena, data.identity, data.tag, next)); 225 // select the window's first issue (past a leading "previous" 226 // row) so its description shows on load. 227 if (data.issues.len > 0) 228 list_box.getFocus().child_id = list_box.children.keys()[if (data.prev_id != null) 1 else 0] 229 else if (list_box.children.count() > 0) 230 list_box.getFocus().child_id = list_box.children.keys()[0]; 231 break :blk try wgt.Scroll(ui.Widget).init(allocator, .{ .box = list_box }, .{ .direction = .vert, .web_native = !session.is_terminal }); 232 }; 233 errdefer list_scroll.deinit(allocator); 234 try box.children.put(allocator, list_scroll.getFocus().id, .{ .widget = .{ .scroll = list_scroll }, .rect = null, .min_size = .{ .width = list_max_width, .height = null }, .max_size = .{ .width = list_max_width, .height = null } }); 235 } 236 237 // the detail pane — a frame around a scroll of the description 238 { 239 var detail_outer = blk: { 240 var detail_scroll = blk2: { 241 var detail_inner = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 242 errdefer detail_inner.deinit(allocator); 243 // fill the pane (content top-left, scroll bar pinned to the 244 // edge) rather than shrinking to the description. 245 break :blk2 try wgt.Scroll(ui.Widget).init(allocator, .{ .box = detail_inner }, .{ .direction = .vert, .web_native = !session.is_terminal, .fill = true }); 246 }; 247 errdefer detail_scroll.deinit(allocator); 248 var frame = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = .hidden, .direction = .vert }); 249 errdefer frame.deinit(allocator); 250 // the frame's selected child is its scroll, so the focus chain 251 // reaches the description (populateDetail points the scroll's 252 // inner box at it), letting focus recovery descend into the pane. 253 frame.getFocus().child_id = detail_scroll.getFocus().id; 254 try frame.children.put(allocator, detail_scroll.getFocus().id, .{ .widget = .{ .scroll = detail_scroll }, .rect = null, .min_size = null }); 255 break :blk frame; 256 }; 257 errdefer detail_outer.deinit(allocator); 258 try box.children.put(allocator, detail_outer.getFocus().id, .{ .widget = .{ .box = detail_outer }, .rect = null, .min_size = .{ .width = detail_min_width, .height = null } }); 259 } 260 261 box.getFocus().child_id = box.children.keys()[list_index]; 262 263 return .{ 264 .box = box, 265 .data = data, 266 .session = session, 267 .detailed_index = null, 268 }; 269 } 270 271 fn addRow(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), label: []const u8, link: []const u8) !void { 272 var row = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .word }); 273 errdefer row.deinit(allocator); 274 row.getFocus().focusable = true; 275 if (link.len != 0) row.getFocus().kind = .{ .custom = link }; 276 try box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .text_box = row }, .rect = null, .min_size = null, .max_size = .{ .width = null, .height = 5 } }); 277 } 278 279 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 280 self.box.deinit(allocator); 281 } 282 283 fn listScroll(self: *View) *wgt.Scroll(ui.Widget) { 284 return &self.box.children.values()[list_index].widget.scroll; 285 } 286 287 fn listBox(self: *View) *wgt.Box(ui.Widget) { 288 return &self.listScroll().child.box; 289 } 290 291 fn detailOuter(self: *View) *wgt.Box(ui.Widget) { 292 return &self.box.children.values()[detail_index].widget.box; 293 } 294 295 fn detailScroll(self: *View) *wgt.Scroll(ui.Widget) { 296 return &self.detailOuter().children.values()[0].widget.scroll; 297 } 298 299 fn detailInner(self: *View) *wgt.Box(ui.Widget) { 300 return &self.detailScroll().child.box; 301 } 302 303 fn detailActive(self: *View) bool { 304 const cid = self.box.getFocus().child_id orelse return false; 305 return self.box.children.getIndex(cid) == detail_index; 306 } 307 308 // the selected issue's index, or null when a window-navigation row is 309 // selected (a leading "previous" row shifts the issue rows down by one). 310 fn selectedIssueIndex(self: *View) ?usize { 311 const lb = self.listBox(); 312 const cid = lb.getFocus().child_id orelse return null; 313 const idx = lb.children.getIndex(cid) orelse return null; 314 const lead: usize = if (self.data.prev_id != null) 1 else 0; 315 if (idx < lead or idx - lead >= self.data.issues.len) return null; 316 return idx - lead; 317 } 318 319 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 320 self.clearGrid(); 321 322 // swap the detail pane to the selected issue when it changes. 323 try self.refreshDetail(allocator); 324 325 // mirror the focused issue into the url so it updates as the selection 326 // moves, but only while focus is inside this view (the list or detail). 327 // when focus sits on the header tab, use the page's base /issues route. 328 // an issue's url is the same whether or not the list is filtered, so 329 // the mirror drops the tag. 330 if (root_focus.grandchild_id) |g| { 331 if (self.box.getFocus().children.contains(g)) { 332 if (self.selectedIssueIndex()) |sel| { 333 if (ui.RoutablePage.repoIssuesRoute(self.data.identity, "", self.data.issues[sel].id)) |route| 334 self.session.data.current_page = route; 335 } 336 } 337 } 338 339 // the selected list row shows a border (the focused TextBox upgrades it 340 // to a double border itself); the rest stay borderless. 341 const lb = self.listBox(); 342 for (lb.children.keys(), lb.children.values()) |id, *child| { 343 switch (child.widget) { 344 .text_box => |*tb| tb.options.border_style = if (lb.getFocus().child_id == id) .single else .hidden, 345 else => {}, 346 } 347 } 348 349 // the description shows a single border while selected (the focused 350 // TextBox upgrades it to a double border itself). 351 const inner = self.detailInner(); 352 for (inner.children.keys(), inner.children.values()) |id, *child| { 353 switch (child.widget) { 354 .text_box => |*tb| tb.options.border_style = if (inner.getFocus().child_id == id) .single else .hidden, 355 else => {}, 356 } 357 } 358 359 // cap the list at list_max_width only while the detail pane fits beside 360 // it. the box drops the detail when the width can't hold both minimums, 361 // so when it's that narrow we lift the cap and let the list fill the 362 // whole width. 363 const both_panes_fit = if (constraint.max_size.width) |w| w >= list_max_width + detail_min_width else true; 364 self.box.children.values()[list_index].max_size = if (both_panes_fit) .{ .width = list_max_width, .height = null } else null; 365 366 // stretch the detail pane across the rest of the width so it fills the 367 // area rather than shrinking to its content; its scroll fills the pane. 368 if (constraint.max_size.width) |w| { 369 self.box.children.values()[detail_index].min_size = .{ .width = if (both_panes_fit) w - list_max_width else w, .height = null }; 370 } else { 371 self.box.children.values()[detail_index].min_size = .{ .width = detail_min_width, .height = null }; 372 } 373 374 try self.box.build(allocator, constraint, root_focus); 375 } 376 377 fn refreshDetail(self: *View, allocator: std.mem.Allocator) !void { 378 const sel = self.selectedIssueIndex() orelse return; 379 if (self.detailed_index) |d| if (d == sel) return; 380 try self.populateDetail(allocator, sel); 381 self.detailed_index = sel; 382 } 383 384 fn populateDetail(self: *View, allocator: std.mem.Allocator, sel: usize) !void { 385 const entry = self.data.issues[sel]; 386 const inner = self.detailInner(); 387 388 for (inner.children.values()) |*child| child.widget.deinit(allocator); 389 inner.children.clearAndFree(allocator); 390 inner.getFocus().child_id = null; 391 392 // the description as a focusable word-wrapped text box. its hidden 393 // border reserves the space the border occupies when focused, so 394 // focusing doesn't shift layout. 395 { 396 var tb = try wgt.TextBox(ui.Widget).init(allocator, entry.issue.description, .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .word }); 397 errdefer tb.deinit(allocator); 398 tb.getFocus().focusable = true; 399 try inner.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 400 } 401 402 // the issue's tags flow under the description like a paragraph; each 403 // links to the list filtered to that tag. 404 { 405 var items: std.ArrayList(ui.TagFlow.Item) = .empty; 406 defer items.deinit(allocator); 407 var tag_iter = evt.Issue.tagIterator(entry.issue.tags); 408 while (tag_iter.next()) |tag| { 409 if (tag.len == 0) continue; 410 try items.append(allocator, .{ .text = tag, .link = try tagLink(self.session.page_arena, self.data.identity, tag) }); 411 } 412 if (items.items.len > 0) { 413 var tf = try ui.TagFlow.init(allocator); 414 errdefer tf.deinit(allocator); 415 try tf.setItems(allocator, items.items); 416 try inner.children.put(allocator, tf.getFocus().id, .{ .widget = .{ .tag_flow = tf }, .rect = null, .min_size = null }); 417 } 418 } 419 420 // point the pane at its row so focus recovery can land here. 421 inner.getFocus().child_id = inner.children.keys()[0]; 422 423 // reset the scroll to the top for the newly-shown issue: directly on the 424 // terminal (the wasm offset), and via a version bump on the web (so the 425 // renderer's scroll id changes and JS drops the preserved position). 426 const sc = self.detailScroll(); 427 sc.x = 0; 428 sc.y = 0; 429 sc.getFocus().version +%= 1; 430 } 431 432 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 433 _ = allocator; 434 if (self.detailActive()) { 435 try self.detailInput(key, root_focus); 436 } else { 437 try self.listInput(key, root_focus); 438 } 439 } 440 441 fn listInput(self: *View, key: Key, root_focus: *Focus) !void { 442 // up/down (and the scroll wheel) move the selection a row; page up/down 443 // jump a fixed amount. right/Enter cross into the detail pane. 444 if (inp.rowDelta(key, @intCast(self.listBox().children.count()))) |delta| { 445 ui.moveRowFocus(self.listBox(), self.listScroll(), root_focus, delta); 446 return; 447 } 448 switch (key) { 449 .enter, .arrow_right => try self.focusDetail(root_focus), 450 else => {}, 451 } 452 } 453 454 fn detailInput(self: *View, key: Key, root_focus: *Focus) !void { 455 if (self.tagsFocused()) { 456 try self.tagsInput(key, root_focus); 457 } else { 458 try self.descriptionInput(key, root_focus); 459 } 460 } 461 462 fn descriptionInput(self: *View, key: Key, root_focus: *Focus) !void { 463 const sc = self.detailScroll(); 464 switch (key) { 465 .arrow_left => return self.focusList(root_focus), 466 .arrow_up => sc.y -= 1, 467 // scroll through the description; once it can't scroll further 468 // (always, on the web's native scroll), move into the tags. 469 .arrow_down => { 470 const before = sc.y; 471 sc.y += 1; 472 sc.clampToContent(); 473 if (sc.y == before) try self.focusTags(root_focus); 474 return; 475 }, 476 .page_up => sc.y -= 10, 477 .page_down => sc.y += 10, 478 .home => sc.y = 0, 479 .end => sc.y = std.math.maxInt(isize), 480 .mouse => |mouse| switch (mouse.action) { 481 .scroll => |dir| sc.y += if (dir == .up) @as(isize, -1) else 1, 482 else => {}, 483 }, 484 else => return, 485 } 486 sc.clampToContent(); 487 } 488 489 // left/right step through the tags (left exits to the list at the first); 490 // up/down move to the first tag of the adjacent row, up escaping to the 491 // description from the top row. 492 fn tagsInput(self: *View, key: Key, root_focus: *Focus) !void { 493 const tf = self.tagFlow() orelse return; 494 const cid = tf.focus.child_id orelse return; 495 const cur = tf.indexOfFocusId(cid) orelse return; 496 const count = tf.text_boxes.items.len; 497 const sc = self.detailScroll(); 498 switch (key) { 499 .arrow_left => if (cur > 0) self.focusTag(tf, root_focus, cur - 1) else try self.focusList(root_focus), 500 .arrow_right => if (cur + 1 < count) self.focusTag(tf, root_focus, cur + 1), 501 .arrow_up => if (tf.rowStep(cur, false)) |i| self.focusTag(tf, root_focus, i) else try self.focusDescription(root_focus), 502 .arrow_down => if (tf.rowStep(cur, true)) |i| self.focusTag(tf, root_focus, i), 503 .home => self.focusTag(tf, root_focus, 0), 504 .end => self.focusTag(tf, root_focus, count - 1), 505 .mouse => |mouse| switch (mouse.action) { 506 .scroll => |dir| { 507 sc.y += if (dir == .up) @as(isize, -1) else 1; 508 sc.clampToContent(); 509 }, 510 else => {}, 511 }, 512 else => {}, 513 } 514 } 515 516 const tags_child_index: usize = 1; 517 518 fn tagFlow(self: *View) ?*ui.TagFlow { 519 const inner = self.detailInner(); 520 if (inner.children.count() <= tags_child_index) return null; 521 return switch (inner.children.values()[tags_child_index].widget) { 522 .tag_flow => |*tf| tf, 523 else => null, 524 }; 525 } 526 527 fn tagsFocused(self: *View) bool { 528 const inner = self.detailInner(); 529 const cid = inner.getFocus().child_id orelse return false; 530 return inner.children.getIndex(cid) == tags_child_index; 531 } 532 533 fn focusTag(self: *View, tf: *ui.TagFlow, root_focus: *Focus, index: usize) void { 534 root_focus.setFocus(tf.text_boxes.items[index].getFocus().id); 535 // keep the tag visible on the terminal: its rect offset by the flow's 536 // position in the pane. 537 if (self.session.is_terminal and index < tf.rects.items.len) { 538 if (self.detailInner().children.values()[tags_child_index].rect) |flow_rect| { 539 var rect = tf.rects.items[index]; 540 rect.x += flow_rect.x; 541 rect.y += flow_rect.y; 542 self.detailScroll().scrollToRect(rect); 543 } 544 } 545 } 546 547 fn focusTags(self: *View, root_focus: *Focus) !void { 548 const tf = self.tagFlow() orelse return; 549 if (tf.text_boxes.items.len == 0) return; 550 const cid = tf.focus.child_id orelse tf.text_boxes.items[0].getFocus().id; 551 const index = tf.indexOfFocusId(cid) orelse 0; 552 self.focusTag(tf, root_focus, index); 553 } 554 555 fn focusDescription(self: *View, root_focus: *Focus) !void { 556 const inner = self.detailInner(); 557 if (inner.children.count() == 0) return; 558 root_focus.setFocus(inner.children.keys()[0]); 559 } 560 561 // enter the detail pane. an empty pane (no issues) can't be entered. 562 fn focusDetail(self: *View, root_focus: *Focus) !void { 563 if (self.detailInner().children.count() == 0) return; 564 root_focus.setFocus(self.detailOuter().getFocus().id); 565 } 566 567 // return to the list. 568 fn focusList(self: *View, root_focus: *Focus) !void { 569 root_focus.setFocus(self.listScroll().getFocus().id); 570 } 571 572 pub fn clearGrid(self: *View) void { 573 self.box.clearGrid(); 574 } 575 576 pub fn getGrid(self: View) ?Grid { 577 return self.box.getGrid(); 578 } 579 580 pub fn getFocus(self: *View) *Focus { 581 return self.box.getFocus(); 582 } 583 584 // for the parent's "scroll up at the top jumps to the header" check. in the 585 // detail pane that means the description is focused and can't scroll up any 586 // further; up from the tags moves to the description instead. when the list 587 // holds focus, report its selected row directly (window-navigation rows 588 // included). 589 pub fn getSelectedIndex(self: *View) ?usize { 590 if (self.detailActive()) { 591 if (self.tagsFocused()) return 1; 592 return if (self.detailScroll().y == 0) 0 else 1; 593 } 594 const lb = self.listBox(); 595 const cid = lb.getFocus().child_id orelse return null; 596 return lb.children.getIndex(cid); 597 } 598 }; 599 600 // the "a:" navigation link for the issues page filtered to the url-encoded 601 // `tag` and rooted at issue `id` within `identity` ("owner/name"). 602 fn issuesLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, tag: []const u8, id: []const u8) ![]const u8 { 603 const route = ui.RoutablePage.repoIssuesRoute(identity, tag, id) orelse return error.RouteTooLong; 604 const url = try route.urlAlloc(page_arena); 605 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 606 } 607 608 // the in-page "ai:" anchor for selecting issue `id` in `identity`'s list; the 609 // href is only followed with js off. 610 fn issueRowLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, id: []const u8) ![]const u8 { 611 const route = ui.RoutablePage.repoIssuesRoute(identity, "", id) orelse return error.RouteTooLong; 612 const url = try route.urlAlloc(page_arena); 613 return std.fmt.allocPrint(page_arena.allocator(), "ai:{s}", .{url}); 614 } 615 616 // the "a:" link to the issues list filtered to `tag` (raw; encoded here). 617 fn tagLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, tag: []const u8) ![]const u8 { 618 const encoded = try ui.urlEncodeRef(page_arena.allocator(), tag); 619 const route = ui.RoutablePage.repoIssuesRoute(identity, encoded, "") orelse return error.RouteTooLong; 620 const url = try route.urlAlloc(page_arena); 621 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 622 } 623