╭ 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 pub const SubHeader = @import("Issues/SubHeader.zig"); 16 17 // how many issues one window shows before a "next" link appears. 18 pub const page_size = 20; 19 20 // how many tags the tags view shows at most. 21 pub const max_tags = 1000; 22 23 // one issue from the repo's consumed event database, with its hex event id 24 // (the id lives in the event envelope, not the payload). 25 pub const IssueWithId = struct { 26 id: []const u8, 27 issue: evt.Issue, 28 }; 29 30 // "owner/name", so the view can build /repo/owner/name/issues/... links. 31 identity: []const u8, 32 // the url-encoded tag the list is filtered to ("" = unfiltered). 33 tag: []const u8, 34 // the hex event id of the issue this window is rooted at ("" = the first 35 // window), mirrored into the url. 36 selected_id: []const u8, 37 issues: []const IssueWithId, 38 // the id of the previous window's first issue, or null when this window is 39 // already the first. 40 prev_id: ?[]const u8, 41 // the id of the next window's first issue, or null when this is the last window. 42 next_id: ?[]const u8, 43 // how many issues the listing holds across all windows. 44 count: usize, 45 // every tag in the repo, in sorted order, for the tags view. 46 tags: []const []const u8, 47 48 const Self = @This(); 49 50 // an empty listing, for the wasm / no-repo paths. 51 pub fn emptyResult(aa: std.mem.Allocator, identity: []const u8, tag: []const u8, selected_id: []const u8) !Self { 52 return .{ 53 .identity = try aa.dupe(u8, identity), 54 .tag = try aa.dupe(u8, tag), 55 .selected_id = try aa.dupe(u8, selected_id), 56 .issues = &.{}, 57 .prev_id = null, 58 .next_id = null, 59 .count = 0, 60 .tags = &.{}, 61 }; 62 } 63 64 // read one window of an opened repo's issues (filtered to `tag` when set), 65 // ordered by creation time (oldest first), starting at the issue 66 // `selected_id` names ("" = the beginning). a local session reads the event 67 // db next to the repo (synced from the events branch on each page build); 68 // a server repo reads its own db. 69 pub fn init( 70 comptime repo_kind: rp.RepoKind, 71 comptime repo_opts: rp.RepoOpts(repo_kind), 72 arena: *std.heap.ArenaAllocator, 73 repo: *rp.Repo(repo_kind, repo_opts), 74 io: std.Io, 75 is_local: bool, 76 identity: []const u8, 77 tag: []const u8, 78 selected_id: []const u8, 79 ) !Self { 80 const empty = try emptyResult(arena.allocator(), identity, tag, selected_id); 81 82 const aa = arena.allocator(); 83 const DB = evt.EventDB(repo_opts.hash); 84 const rooted = empty.selected_id.len != 0; 85 const tagged = empty.tag.len != 0; 86 // an explicitly named issue or tag that doesn't exist is a bad url 87 // (NotFound -> 404); the bare route falls through to an empty listing. 88 const strict = rooted or tagged; 89 90 // a repo with no consumed events has no moment yet. 91 const gpa = arena.child_allocator; 92 var event_db_maybe = if (is_local) try evt.LocalEventDB(repo_opts.hash).open(io, gpa, repo.core.repo_dir) else null; 93 defer if (event_db_maybe) |*event_db| event_db.deinit(io, gpa); 94 const haxy_moment = (if (event_db_maybe) |*event_db| 95 evt.currentMomentFromDb(repo_opts.hash, event_db.db) 96 else if (repo_kind == .git) 97 return empty 98 else if (is_local) 99 return empty 100 else 101 evt.currentMoment(repo_opts, repo)) catch { 102 if (strict) return error.NotFound; 103 return empty; 104 }; 105 106 // the ordered set to window: the tag's issue set when filtered, else the 107 // full issue set. 108 const issue_id_set_cursor = blk: { 109 if (tagged) { 110 const tag_to_issues_cursor = try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, "tag->issue-id-set")) orelse return error.NotFound; 111 const tag_to_issues = try DB.SortedMap(.read_only).init(tag_to_issues_cursor); 112 const decoded = std.Uri.percentDecodeInPlace(try aa.dupe(u8, empty.tag)); 113 break :blk (try tag_to_issues.getCursor(decoded)) orelse return error.NotFound; 114 } 115 break :blk (try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, "issue-id-set"))) orelse { 116 if (strict) return error.NotFound; 117 return empty; 118 }; 119 }; 120 const issue_id_set = try DB.SortedSet(.read_only).init(issue_id_set_cursor); 121 122 const event_id_to_issue_cursor = try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, "event-id->issue")) orelse { 123 if (strict) return error.NotFound; 124 return empty; 125 }; 126 const event_id_to_issue = try DB.HashMap(.read_only).init(event_id_to_issue_cursor); 127 128 // seek once to the window start: the named issue's order key (its recorded 129 // creation timestamp plus its id, so the seek lands on it), or the set's 130 // first entry. 131 var prev_id: ?[]const u8 = null; 132 var iter = if (!rooted) 133 try issue_id_set.iteratorFromIndex(0) 134 else blk: { 135 if (empty.selected_id.len != evt.event_id_size * 2) return error.NotFound; 136 var id_bytes: [evt.event_id_size]u8 = undefined; 137 _ = std.fmt.hexToBytes(&id_bytes, empty.selected_id) catch return error.NotFound; 138 const issue_cursor = try event_id_to_issue.getCursor(hash.hashInt(repo_opts.hash, &id_bytes)) orelse return error.NotFound; 139 const issue_map = try DB.HashMap(.read_only).init(issue_cursor); 140 const issue_event = try evt.read(evt.Issue, DB, repo_opts.hash, arena, issue_map); 141 const order_key = evt.orderKey(issue_event.created_ts, &id_bytes); 142 143 // the named issue must be in the windowed set (a tag url can name an 144 // issue that doesn't carry the tag). 145 if (!try issue_id_set.contains(&order_key)) return error.NotFound; 146 147 // the previous window starts page_size ranks back. 148 const rank = try issue_id_set.rank(&order_key); 149 if (rank > 0) { 150 const prev_rank = rank -| page_size; 151 const kv = try issue_id_set.getIndexKeyValuePair(@intCast(prev_rank)) orelse return error.NotFound; 152 var prev_key: [@sizeOf(u64) + evt.event_id_size]u8 = undefined; 153 _ = try kv.key_cursor.readBytes(&prev_key); 154 const prev_hex = std.fmt.bytesToHex(prev_key[@sizeOf(u64)..].*, .lower); 155 prev_id = try aa.dupe(u8, &prev_hex); 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 // every tag in the repo, in the tag map's sorted order. 186 var tags: std.ArrayList([]const u8) = .empty; 187 if (try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, "tag->issue-id-set"))) |tag_to_issues_cursor| { 188 const tag_to_issues = try DB.SortedMap(.read_only).init(tag_to_issues_cursor); 189 var tag_iter = try tag_to_issues.iterator(); 190 while (try tag_iter.next()) |kv_pair_cursor| { 191 if (tags.items.len == max_tags) break; 192 var kv_cursor = kv_pair_cursor; 193 const kv_pair = try kv_cursor.readKeyValuePair(); 194 try tags.append(aa, try kv_pair.key_cursor.readBytesAlloc(aa, null)); 195 } 196 } 197 198 return .{ 199 .identity = empty.identity, 200 .tag = empty.tag, 201 .selected_id = empty.selected_id, 202 .issues = issues.items, 203 .prev_id = prev_id, 204 .next_id = next_id, 205 .count = @intCast(try issue_id_set.count()), 206 .tags = tags.items, 207 }; 208 } 209 210 pub const View = struct { 211 // a vertical box: the sub header tabs on top, then a stack holding the 212 // results view — a horizontal split with the issue list on the left and a 213 // detail pane on the right showing the selected issue's description. 214 box: wgt.Box(ui.Widget), // vert: [sub_header_index] = tabs, [stack_index] = stack 215 data: *const Self, 216 session: *ui.Session, 217 // the issue whose description the pane currently shows (index into data.issues). 218 detailed_index: ?usize, 219 220 const sub_header_index: usize = 0; 221 const stack_index: usize = 1; 222 // indices within the stack, 1:1 with the sub header tabs. 223 const results_view_index: usize = 0; 224 const tags_view_index: usize = 1; 225 // indices within the results box (the horizontal split inside the stack). 226 const list_index: usize = 0; 227 const detail_index: usize = 1; 228 const list_max_width: usize = 40; 229 const detail_min_width: usize = 40; 230 231 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { 232 var outer = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 233 errdefer outer.deinit(allocator); 234 235 // the tabs at the top. 236 { 237 var sub_header = try SubHeader.View.init(allocator, session, data.count, data.tag); 238 errdefer sub_header.deinit(allocator); 239 try outer.children.put(allocator, sub_header.getFocus().id, .{ .widget = .{ .repo_issues_sub_header = sub_header }, .rect = null, .min_size = null }); 240 } 241 242 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 243 errdefer box.deinit(allocator); 244 245 // the issue list (one focusable row per title), plus a "next" link that 246 // reloads the page rooted at the following issue. 247 { 248 var list_scroll = blk: { 249 var list_box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 250 errdefer list_box.deinit(allocator); 251 if (data.prev_id) |prev| 252 try addRow(allocator, &list_box, "← previous", try issuesLink(session.page_arena, data.identity, data.tag, prev)); 253 for (data.issues) |entry| 254 try addRow(allocator, &list_box, entry.issue.title, try issueRowLink(session.page_arena, data.identity, entry.id)); 255 if (data.next_id) |next| 256 try addRow(allocator, &list_box, "next →", try issuesLink(session.page_arena, data.identity, data.tag, next)); 257 // select the window's first issue (past a leading "previous" 258 // row) so its description shows on load. 259 if (data.issues.len > 0) 260 list_box.getFocus().child_id = list_box.children.keys()[if (data.prev_id != null) 1 else 0] 261 else if (list_box.children.count() > 0) 262 list_box.getFocus().child_id = list_box.children.keys()[0]; 263 break :blk try wgt.Scroll(ui.Widget).init(allocator, .{ .box = list_box }, .{ .direction = .vert, .web_native = !session.is_terminal }); 264 }; 265 errdefer list_scroll.deinit(allocator); 266 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 } }); 267 } 268 269 // the detail pane — a frame around a scroll of the description 270 { 271 var detail_outer = blk: { 272 var detail_scroll = blk2: { 273 var detail_inner = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 274 errdefer detail_inner.deinit(allocator); 275 // fill the pane (content top-left, scroll bar pinned to the 276 // edge) rather than shrinking to the description. 277 break :blk2 try wgt.Scroll(ui.Widget).init(allocator, .{ .box = detail_inner }, .{ .direction = .vert, .web_native = !session.is_terminal, .fill = true }); 278 }; 279 errdefer detail_scroll.deinit(allocator); 280 var frame = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = .hidden, .direction = .vert }); 281 errdefer frame.deinit(allocator); 282 // the frame's selected child is its scroll, so the focus chain 283 // reaches the description (populateDetail points the scroll's 284 // inner box at it), letting focus recovery descend into the pane. 285 frame.getFocus().child_id = detail_scroll.getFocus().id; 286 try frame.children.put(allocator, detail_scroll.getFocus().id, .{ .widget = .{ .scroll = detail_scroll }, .rect = null, .min_size = null }); 287 break :blk frame; 288 }; 289 errdefer detail_outer.deinit(allocator); 290 try box.children.put(allocator, detail_outer.getFocus().id, .{ .widget = .{ .box = detail_outer }, .rect = null, .min_size = .{ .width = detail_min_width, .height = null } }); 291 } 292 293 box.getFocus().child_id = box.children.keys()[list_index]; 294 295 // the split sits alone in a stack so future tabs can swap other views 296 // in. the stack enters `outer` before the split enters the stack, so an 297 // error frees the split exactly once. 298 { 299 var stack = try wgt.Stack(ui.Widget).init(allocator); 300 errdefer stack.deinit(allocator); 301 try outer.children.put(allocator, stack.getFocus().id, .{ .widget = .{ .stack = stack }, .rect = null, .min_size = null }); 302 } 303 const stack = &outer.children.values()[stack_index].widget.stack; 304 stack.getFocus().child_id = box.getFocus().id; 305 try stack.children.put(allocator, box.getFocus().id, .{ .box = box }); 306 307 // the tags view 308 { 309 var tf = try ui.TagFlow.init(allocator); 310 errdefer tf.deinit(allocator); 311 var items: std.ArrayList(ui.TagFlow.Item) = .empty; 312 defer items.deinit(allocator); 313 // when filtered, the first item clears the filter 314 if (data.tag.len != 0) 315 try items.append(allocator, .{ .text = "╳", .link = try issuesLink(session.page_arena, data.identity, "", "") }); 316 for (data.tags) |tag| 317 try items.append(allocator, .{ .text = tag, .link = try tagLink(session.page_arena, data.identity, tag) }); 318 try tf.setItems(allocator, items.items); 319 try stack.children.put(allocator, tf.getFocus().id, .{ .tag_flow = tf }); 320 } 321 322 // focus entering the view lands on the tabs first. 323 outer.getFocus().child_id = outer.children.keys()[sub_header_index]; 324 325 return .{ 326 .box = outer, 327 .data = data, 328 .session = session, 329 .detailed_index = null, 330 }; 331 } 332 333 fn addRow(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), label: []const u8, link: []const u8) !void { 334 var row = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .word }); 335 errdefer row.deinit(allocator); 336 row.getFocus().focusable = true; 337 if (link.len != 0) row.getFocus().kind = .{ .custom = link }; 338 try box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .text_box = row }, .rect = null, .min_size = null, .max_size = .{ .width = null, .height = 5 } }); 339 } 340 341 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 342 self.box.deinit(allocator); 343 } 344 345 fn subHeader(self: *View) *SubHeader.View { 346 return &self.box.children.values()[sub_header_index].widget.repo_issues_sub_header; 347 } 348 349 fn viewStack(self: *View) *wgt.Stack(ui.Widget) { 350 return &self.box.children.values()[stack_index].widget.stack; 351 } 352 353 // the master-detail split inside the stack. 354 fn resultsBox(self: *View) *wgt.Box(ui.Widget) { 355 return &self.viewStack().children.values()[results_view_index].box; 356 } 357 358 // the tags view's flow inside the stack. 359 fn tagsView(self: *View) *ui.TagFlow { 360 return &self.viewStack().children.values()[tags_view_index].tag_flow; 361 } 362 363 fn listScroll(self: *View) *wgt.Scroll(ui.Widget) { 364 return &self.resultsBox().children.values()[list_index].widget.scroll; 365 } 366 367 fn listBox(self: *View) *wgt.Box(ui.Widget) { 368 return &self.listScroll().child.box; 369 } 370 371 fn detailOuter(self: *View) *wgt.Box(ui.Widget) { 372 return &self.resultsBox().children.values()[detail_index].widget.box; 373 } 374 375 fn detailScroll(self: *View) *wgt.Scroll(ui.Widget) { 376 return &self.detailOuter().children.values()[0].widget.scroll; 377 } 378 379 fn detailInner(self: *View) *wgt.Box(ui.Widget) { 380 return &self.detailScroll().child.box; 381 } 382 383 fn detailActive(self: *View) bool { 384 const rb = self.resultsBox(); 385 const cid = rb.getFocus().child_id orelse return false; 386 return rb.children.getIndex(cid) == detail_index; 387 } 388 389 fn subHeaderActive(self: *View) bool { 390 const cid = self.box.getFocus().child_id orelse return false; 391 return self.box.children.getIndex(cid) == sub_header_index; 392 } 393 394 fn tagsViewActive(self: *View) bool { 395 if (self.subHeaderActive()) return false; 396 const stack = self.viewStack(); 397 const cid = stack.getFocus().child_id orelse return false; 398 return stack.children.getIndex(cid) == tags_view_index; 399 } 400 401 // the selected issue's index, or null when a window-navigation row is 402 // selected (a leading "previous" row shifts the issue rows down by one). 403 fn selectedIssueIndex(self: *View) ?usize { 404 const lb = self.listBox(); 405 const cid = lb.getFocus().child_id orelse return null; 406 const idx = lb.children.getIndex(cid) orelse return null; 407 const lead: usize = if (self.data.prev_id != null) 1 else 0; 408 if (idx < lead or idx - lead >= self.data.issues.len) return null; 409 return idx - lead; 410 } 411 412 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 413 self.clearGrid(); 414 415 // each sub header tab maps 1:1 to a stack child by position. 416 if (self.subHeader().getSelectedIndex()) |index| { 417 const stack = self.viewStack(); 418 stack.getFocus().child_id = stack.children.keys()[index]; 419 } 420 421 // swap the detail pane to the selected issue when it changes. 422 try self.refreshDetail(allocator); 423 424 // mirror the focused issue into the url so it updates as the selection 425 // moves, but only while focus is inside this view (the list or detail). 426 // when focus sits on the header tab, use the page's base /issues route. 427 // an issue's url is the same whether or not the list is filtered, so 428 // the mirror drops the tag. 429 if (root_focus.grandchild_id) |g| { 430 if (self.resultsBox().getFocus().children.contains(g)) { 431 if (self.selectedIssueIndex()) |sel| { 432 if (ui.RoutablePage.repoIssuesRoute(self.data.identity, "", self.data.issues[sel].id)) |route| 433 self.session.data.current_page = route; 434 } 435 } 436 } 437 438 // the selected list row shows a border (the focused TextBox upgrades it 439 // to a double border itself); the rest stay borderless. 440 const lb = self.listBox(); 441 for (lb.children.keys(), lb.children.values()) |id, *child| { 442 switch (child.widget) { 443 .text_box => |*tb| tb.options.border_style = if (lb.getFocus().child_id == id) .single else .hidden, 444 else => {}, 445 } 446 } 447 448 // the pane's selected child shows the selection border: the description 449 // directly, the tags via the flow's selected item. 450 const inner = self.detailInner(); 451 for (inner.children.keys(), inner.children.values()) |id, *child| { 452 switch (child.widget) { 453 .text_box => |*tb| tb.options.border_style = if (inner.getFocus().child_id == id) .single else .hidden, 454 else => {}, 455 } 456 } 457 if (self.tagFlow()) |tf| tf.selected = self.tagsFocused(); 458 self.tagsView().selected = self.tagsViewActive(); 459 460 // cap the list at list_max_width only while the detail pane fits beside 461 // it. the box drops the detail when the width can't hold both minimums, 462 // so when it's that narrow we lift the cap and let the list fill the 463 // whole width. 464 const both_panes_fit = if (constraint.max_size.width) |w| w >= list_max_width + detail_min_width else true; 465 self.resultsBox().children.values()[list_index].max_size = if (both_panes_fit) .{ .width = list_max_width, .height = null } else null; 466 467 // stretch the detail pane across the rest of the width so it fills the 468 // area rather than shrinking to its content; its scroll fills the pane. 469 if (constraint.max_size.width) |w| { 470 self.resultsBox().children.values()[detail_index].min_size = .{ .width = if (both_panes_fit) w - list_max_width else w, .height = null }; 471 } else { 472 self.resultsBox().children.values()[detail_index].min_size = .{ .width = detail_min_width, .height = null }; 473 } 474 475 try self.box.build(allocator, constraint, root_focus); 476 } 477 478 fn refreshDetail(self: *View, allocator: std.mem.Allocator) !void { 479 const sel = self.selectedIssueIndex() orelse return; 480 if (self.detailed_index) |d| if (d == sel) return; 481 try self.populateDetail(allocator, sel); 482 self.detailed_index = sel; 483 } 484 485 fn populateDetail(self: *View, allocator: std.mem.Allocator, sel: usize) !void { 486 const entry = self.data.issues[sel]; 487 const inner = self.detailInner(); 488 489 for (inner.children.values()) |*child| child.widget.deinit(allocator); 490 inner.children.clearAndFree(allocator); 491 inner.getFocus().child_id = null; 492 493 // the issue's tags, each linking to the list filtered to that tag. 494 { 495 var items: std.ArrayList(ui.TagFlow.Item) = .empty; 496 defer items.deinit(allocator); 497 var tag_iter = evt.Issue.tagIterator(entry.issue.tags); 498 while (tag_iter.next()) |tag| { 499 if (tag.len == 0) continue; 500 try items.append(allocator, .{ .text = tag, .link = try tagLink(self.session.page_arena, self.data.identity, tag) }); 501 } 502 if (items.items.len > 0) { 503 var tf = try ui.TagFlow.init(allocator); 504 errdefer tf.deinit(allocator); 505 try tf.setItems(allocator, items.items); 506 try inner.children.put(allocator, tf.getFocus().id, .{ .widget = .{ .tag_flow = tf }, .rect = null, .min_size = null }); 507 } 508 } 509 510 // the description as a focusable word-wrapped text box. its hidden 511 // border reserves the space the border occupies when focused, so 512 // focusing doesn't shift layout. 513 { 514 var tb = try wgt.TextBox(ui.Widget).init(allocator, entry.issue.description, .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .word }); 515 errdefer tb.deinit(allocator); 516 tb.getFocus().focusable = true; 517 try inner.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 518 } 519 520 // select the description by default 521 inner.getFocus().child_id = inner.children.keys()[inner.children.count() - 1]; 522 523 // reset the scroll to the top for the newly-shown issue: directly on the 524 // terminal (the wasm offset), and via a version bump on the web (so the 525 // renderer's scroll id changes and JS drops the preserved position). 526 const sc = self.detailScroll(); 527 sc.x = 0; 528 sc.y = 0; 529 sc.getFocus().version +%= 1; 530 } 531 532 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 533 if (self.subHeaderActive()) { 534 // down from the tabs re-enters the split; other keys move the tabs. 535 if (inp.vertDirection(key) == .down) { 536 root_focus.setFocus(self.box.children.keys()[stack_index]); 537 } else { 538 try self.subHeader().input(allocator, key, root_focus); 539 } 540 return; 541 } 542 if (self.tagsViewActive()) { 543 self.tagsViewInput(key, root_focus); 544 } else if (self.detailActive()) { 545 try self.detailInput(key, root_focus); 546 } else { 547 try self.listInput(key, root_focus); 548 } 549 } 550 551 // arrow keys move the tag selection; up from the top row crosses to the 552 // sub header tabs. 553 fn tagsViewInput(self: *View, key: Key, root_focus: *Focus) void { 554 const tf = self.tagsView(); 555 const cid = tf.focus.child_id orelse return; 556 const cur = tf.indexOfFocusId(cid) orelse return; 557 const count = tf.text_boxes.items.len; 558 switch (key) { 559 .arrow_left => if (cur > 0) root_focus.setFocus(tf.text_boxes.items[cur - 1].getFocus().id), 560 .arrow_right => if (cur + 1 < count) root_focus.setFocus(tf.text_boxes.items[cur + 1].getFocus().id), 561 .arrow_up => if (tf.rowStep(cur, false)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id) else self.focusSubHeader(root_focus), 562 .arrow_down => if (tf.rowStep(cur, true)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id), 563 .home => root_focus.setFocus(tf.text_boxes.items[0].getFocus().id), 564 .end => root_focus.setFocus(tf.text_boxes.items[count - 1].getFocus().id), 565 else => {}, 566 } 567 } 568 569 fn listInput(self: *View, key: Key, root_focus: *Focus) !void { 570 // up/down (and the scroll wheel) move the selection a row; page up/down 571 // jump a fixed amount. right/Enter cross into the detail pane. up from 572 // the top row crosses into the sub header tabs. 573 if (inp.rowDelta(key, @intCast(self.listBox().children.count()))) |delta| { 574 const lb = self.listBox(); 575 const at_top = if (lb.getFocus().child_id) |cid| lb.children.getIndex(cid) == 0 else true; 576 if (delta < 0 and at_top) return self.focusSubHeader(root_focus); 577 ui.moveRowFocus(lb, self.listScroll(), root_focus, delta); 578 return; 579 } 580 switch (key) { 581 .enter, .arrow_right => try self.focusDetail(root_focus), 582 else => {}, 583 } 584 } 585 586 fn detailInput(self: *View, key: Key, root_focus: *Focus) !void { 587 if (self.tagsFocused()) { 588 try self.tagsInput(key, root_focus); 589 } else { 590 try self.descriptionInput(key, root_focus); 591 } 592 } 593 594 fn descriptionInput(self: *View, key: Key, root_focus: *Focus) !void { 595 const sc = self.detailScroll(); 596 switch (key) { 597 .arrow_left => return self.focusList(root_focus), 598 // once the scroll can't move further, cross into the tags (or the 599 // sub header tabs when the issue has none). 600 .arrow_up => { 601 const before = sc.y; 602 sc.y -= 1; 603 sc.clampToContent(); 604 if (sc.y == before) { 605 if (self.tagFlow() != null) try self.focusTags(root_focus) else self.focusSubHeader(root_focus); 606 } 607 return; 608 }, 609 .arrow_down => sc.y += 1, 610 .page_up => sc.y -= 10, 611 .page_down => sc.y += 10, 612 .home => sc.y = 0, 613 .end => sc.y = std.math.maxInt(isize), 614 .mouse => |mouse| switch (mouse.action) { 615 .scroll => |dir| sc.y += if (dir == .up) @as(isize, -1) else 1, 616 else => {}, 617 }, 618 else => return, 619 } 620 sc.clampToContent(); 621 } 622 623 // arrow keys move the tag selection; at the flow's edges focus crosses to 624 // the neighboring widgets. 625 fn tagsInput(self: *View, key: Key, root_focus: *Focus) !void { 626 const tf = self.tagFlow() orelse return; 627 const cid = tf.focus.child_id orelse return; 628 const cur = tf.indexOfFocusId(cid) orelse return; 629 const count = tf.text_boxes.items.len; 630 const sc = self.detailScroll(); 631 switch (key) { 632 .arrow_left => if (cur > 0) self.focusTag(tf, root_focus, cur - 1) else try self.focusList(root_focus), 633 .arrow_right => if (cur + 1 < count) self.focusTag(tf, root_focus, cur + 1), 634 .arrow_up => if (tf.rowStep(cur, false)) |i| self.focusTag(tf, root_focus, i) else self.focusSubHeader(root_focus), 635 .arrow_down => if (tf.rowStep(cur, true)) |i| self.focusTag(tf, root_focus, i) else try self.focusDescription(root_focus), 636 .home => self.focusTag(tf, root_focus, 0), 637 .end => self.focusTag(tf, root_focus, count - 1), 638 .mouse => |mouse| switch (mouse.action) { 639 .scroll => |dir| { 640 sc.y += if (dir == .up) @as(isize, -1) else 1; 641 sc.clampToContent(); 642 }, 643 else => {}, 644 }, 645 else => {}, 646 } 647 } 648 649 const tags_child_index: usize = 0; 650 651 fn tagFlow(self: *View) ?*ui.TagFlow { 652 const inner = self.detailInner(); 653 if (inner.children.count() == 0) return null; 654 return switch (inner.children.values()[tags_child_index].widget) { 655 .tag_flow => |*tf| tf, 656 else => null, 657 }; 658 } 659 660 fn tagsFocused(self: *View) bool { 661 const inner = self.detailInner(); 662 const cid = inner.getFocus().child_id orelse return false; 663 return inner.children.getIndex(cid) == tags_child_index and self.tagFlow() != null; 664 } 665 666 fn focusTag(self: *View, tf: *ui.TagFlow, root_focus: *Focus, index: usize) void { 667 root_focus.setFocus(tf.text_boxes.items[index].getFocus().id); 668 // keep the tag visible on the terminal: its rect offset by the flow's 669 // position in the pane. 670 if (self.session.is_terminal and index < tf.rects.items.len) { 671 if (self.detailInner().children.values()[tags_child_index].rect) |flow_rect| { 672 var rect = tf.rects.items[index]; 673 rect.x += flow_rect.x; 674 rect.y += flow_rect.y; 675 self.detailScroll().scrollToRect(rect); 676 } 677 } 678 } 679 680 fn focusTags(self: *View, root_focus: *Focus) !void { 681 const tf = self.tagFlow() orelse return; 682 if (tf.text_boxes.items.len == 0) return; 683 const cid = tf.focus.child_id orelse tf.text_boxes.items[0].getFocus().id; 684 const index = tf.indexOfFocusId(cid) orelse 0; 685 self.focusTag(tf, root_focus, index); 686 } 687 688 fn focusDescription(self: *View, root_focus: *Focus) !void { 689 const inner = self.detailInner(); 690 const index: usize = if (self.tagFlow() != null) 1 else 0; 691 if (inner.children.count() <= index) return; 692 root_focus.setFocus(inner.children.keys()[index]); 693 } 694 695 // enter the detail pane. an empty pane (no issues) can't be entered. 696 fn focusDetail(self: *View, root_focus: *Focus) !void { 697 if (self.detailInner().children.count() == 0) return; 698 root_focus.setFocus(self.detailOuter().getFocus().id); 699 } 700 701 // return to the list. 702 fn focusList(self: *View, root_focus: *Focus) !void { 703 root_focus.setFocus(self.listScroll().getFocus().id); 704 } 705 706 // cross to the sub header tabs above the split. 707 fn focusSubHeader(self: *View, root_focus: *Focus) void { 708 root_focus.setFocus(self.box.children.keys()[sub_header_index]); 709 } 710 711 pub fn clearGrid(self: *View) void { 712 self.box.clearGrid(); 713 } 714 715 pub fn getGrid(self: View) ?Grid { 716 return self.box.getGrid(); 717 } 718 719 pub fn getFocus(self: *View) *Focus { 720 return self.box.getFocus(); 721 } 722 723 // for the parent's "scroll up at the top jumps to the header" check: at the 724 // top only while the sub header tabs hold focus, so up from the split 725 // crosses into the tabs first. 726 pub fn getSelectedIndex(self: *View) ?usize { 727 return if (self.subHeaderActive()) 0 else 1; 728 } 729 }; 730 731 // the "a:" navigation link for the issues page filtered to the url-encoded 732 // `tag` and rooted at issue `id` within `identity` ("owner/name"). 733 fn issuesLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, tag: []const u8, id: []const u8) ![]const u8 { 734 const route = ui.RoutablePage.repoIssuesRoute(identity, tag, id) orelse return error.RouteTooLong; 735 const url = try route.urlAlloc(page_arena); 736 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 737 } 738 739 // the in-page "ai:" anchor for selecting issue `id` in `identity`'s list; the 740 // href is only followed with js off. 741 fn issueRowLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, id: []const u8) ![]const u8 { 742 const route = ui.RoutablePage.repoIssuesRoute(identity, "", id) orelse return error.RouteTooLong; 743 const url = try route.urlAlloc(page_arena); 744 return std.fmt.allocPrint(page_arena.allocator(), "ai:{s}", .{url}); 745 } 746 747 // the "a:" link to the issues list filtered to `tag` (raw; encoded here). 748 fn tagLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, tag: []const u8) ![]const u8 { 749 const encoded = try ui.urlEncodeRef(page_arena.allocator(), tag); 750 const route = ui.RoutablePage.repoIssuesRoute(identity, encoded, "") orelse return error.RouteTooLong; 751 const url = try route.urlAlloc(page_arena); 752 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 753 } 754