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