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