╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Issues.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
182 const closed_window = try loadWindow(repo_opts.hash, arena, event_id_to_issue, closed_set, closed_root); 183 184 // every tag in the repo, in the tag map's sorted order. the keys are 185 // "tag,status", so a tag's entries are adjacent and dedup by prefix. 186 var tags: std.ArrayList([]const u8) = .empty; 187 if (try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, "tag+status->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 const key = try kv_pair.key_cursor.readBytesAlloc(aa, null); 195 const comma = std.mem.indexOfScalar(u8, key, ',') orelse continue; 196 const key_tag = key[0..comma]; 197 if (tags.getLastOrNull()) |last| { 198 if (std.mem.eql(u8, last, key_tag)) continue; 199 } 200 try tags.append(aa, key_tag); 201 } 202 } 203 204 return .{ 205 .identity = empty.identity, 206 .tag = empty.tag, 207 .selected_id = empty.selected_id, 208 .open = open_window, 209 .closed = closed_window, 210 .view = resolved_view, 211 .tags = tags.items, 212 }; 213 } 214 215 // `status`'s sorted set within `statuses` (null when the status has no issues) 216 fn statusSet( 217 comptime DB: type, 218 statuses: DB.SortedMap(.read_only), 219 status: evt.Issue.Status, 220 ) !?DB.SortedSet(.read_only) { 221 const cursor = (try statuses.getCursor(@tagName(status))) orelse return null; 222 return try DB.SortedSet(.read_only).init(cursor); 223 } 224 225 // the set at `tag_to_issues`'s "tag,status" key (null when no issue carries 226 // `tag` with `status`, or the tag is too long to exist) 227 fn tagStatusSet( 228 comptime DB: type, 229 tag_to_issues: DB.SortedMap(.read_only), 230 tag: []const u8, 231 status: evt.Issue.Status, 232 ) !?DB.SortedSet(.read_only) { 233 var key_buffer: evt.Issue.TagStatusKey = undefined; 234 const key = evt.Issue.tagStatusKey(&key_buffer, tag, status) catch return null; 235 const cursor = (try tag_to_issues.getCursor(key)) orelse return null; 236 return try DB.SortedSet(.read_only).init(cursor); 237 } 238 239 // read one window of `set_maybe` (null = an empty listing) starting at 240 // `root_key` (null = the beginning) 241 fn loadWindow( 242 comptime hash_kind: hash.HashKind, 243 arena: *std.heap.ArenaAllocator, 244 event_id_to_issue: evt.EventDB(hash_kind).HashMap(.read_only), 245 set_maybe: ?evt.EventDB(hash_kind).SortedSet(.read_only), 246 root_key: ?[]const u8, 247 ) !Window { 248 const set = set_maybe orelse return .empty; 249 const DB = evt.EventDB(hash_kind); 250 const aa = arena.allocator(); 251 252 // seek once to the window start: the root key, or the set's first entry. 253 var prev_id: ?[]const u8 = null; 254 var iter = if (root_key) |key| blk: { 255 // the previous window starts page_size ranks back ("" = the first 256 // window, linked as the bare list route). 257 const rank = try set.rank(key); 258 if (rank > 0 and rank <= page_size) { 259 prev_id = ""; 260 } else if (rank > page_size) { 261 const kv = try set.getIndexKeyValuePair(@intCast(rank - page_size)) orelse return error.NotFound; 262 var prev_key: [@sizeOf(u64) + evt.event_id_size]u8 = undefined; 263 _ = try kv.key_cursor.readBytes(&prev_key); 264 const prev_hex = std.fmt.bytesToHex(prev_key[@sizeOf(u64)..].*, .lower); 265 prev_id = try aa.dupe(u8, &prev_hex); 266 } 267 break :blk try set.iteratorFrom(key); 268 } else try set.iteratorFromIndex(0); 269 270 // collect this window's issues, plus a peek at the one after it (its id is 271 // the next window's start). the set's keys are orderKey 272 // ([timestamp][event-id]); the trailing bytes of each key are the issue 273 // event id. 274 var issues: std.ArrayList(IssueWithId) = .empty; 275 var next_id: ?[]const u8 = null; 276 while (try iter.next()) |id_cursor_val| { 277 var id_cursor = id_cursor_val; 278 const id_kv = try id_cursor.readKeyValuePair(); 279 var order_key: [@sizeOf(u64) + evt.event_id_size]u8 = undefined; 280 _ = try id_kv.key_cursor.readBytes(&order_key); 281 const id_hex = std.fmt.bytesToHex(order_key[@sizeOf(u64)..].*, .lower); 282 if (issues.items.len == page_size) { 283 next_id = try aa.dupe(u8, &id_hex); 284 break; 285 } 286 const issue_cursor = try event_id_to_issue.getCursor(hash.hashInt(hash_kind, order_key[@sizeOf(u64)..])) orelse continue; 287 const issue_map = try DB.HashMap(.read_only).init(issue_cursor); 288 try issues.append(aa, .{ 289 .id = try aa.dupe(u8, &id_hex), 290 .issue = try evt.read(evt.Issue, DB, hash_kind, arena, issue_map), 291 }); 292 } 293 294 return .{ 295 .issues = issues.items, 296 .prev_id = prev_id, 297 .next_id = next_id, 298 .count = @intCast(try set.count()), 299 }; 300 } 301 302 pub const View = struct { 303 // a vertical box: the header tabs on top, then a stack holding a 304 // master-detail split (issue list + description pane) per status list, 305 // plus the tags view. 306 box: wgt.Box(ui.Widget), // vert: [header_index] = tabs, [stack_index] = stack 307 data: *const Self, 308 session: *ui.Session, 309 // per-split state, indexed like the stack's split children: the issue the 310 // pane shows, and its description text box's focus id. 311 detailed_index: [split_count]?usize, 312 description_id: [split_count]?usize, 313 314 const header_index: usize = 0; 315 const stack_index: usize = 1; 316 // indices within the stack, 1:1 with the header tabs. 317 const open_view_index: usize = 0; 318 const closed_view_index: usize = 1; 319 const tags_view_index: usize = 2; 320 const split_count: usize = 2; 321 // indices within a split (the horizontal box inside the stack). 322 const list_index: usize = 0; 323 const detail_index: usize = 1; 324 const list_max_width: usize = 40; 325 const detail_min_width: usize = 40; 326 327 fn viewIndex(view: ui.RoutablePage.IssuesView) usize { 328 return switch (view) { 329 .open => open_view_index, 330 .closed => closed_view_index, 331 .tags => tags_view_index, 332 }; 333 } 334 335 fn splitStatus(index: usize) evt.Issue.Status { 336 return if (index == open_view_index) .open else .closed; 337 } 338 339 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { 340 var outer = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 341 errdefer outer.deinit(allocator); 342 343 // the tabs at the top. 344 { 345 var hdr = try Header.init(allocator, session, data); 346 errdefer hdr.deinit(allocator); 347 try outer.children.put(allocator, hdr.getFocus().id, .{ .widget = .{ .repo_issues_header = hdr }, .rect = null, .min_size = null }); 348 } 349 350 // the stack enters `outer` before its children enter it, so an error 351 // frees each child exactly once. 352 { 353 var stack = try wgt.Stack(ui.Widget).init(allocator); 354 errdefer stack.deinit(allocator); 355 try outer.children.put(allocator, stack.getFocus().id, .{ .widget = .{ .stack = stack }, .rect = null, .min_size = null }); 356 } 357 const stack = &outer.children.values()[stack_index].widget.stack; 358 359 // a master-detail split per status list. 360 for ([_]evt.Issue.Status{ .open, .closed }) |status| { 361 var split = try initSplit(allocator, session, data, status); 362 errdefer split.deinit(allocator); 363 try stack.children.put(allocator, split.getFocus().id, .{ .box = split }); 364 } 365 366 // the tags view 367 { 368 var tf = try ui.TagFlow.init(allocator); 369 errdefer tf.deinit(allocator); 370 var items: std.ArrayList(ui.TagFlow.Item) = .empty; 371 defer items.deinit(allocator); 372 // when filtered, the first item clears the filter 373 if (data.tag.len != 0) 374 try items.append(allocator, .{ .text = "╳", .link = try issuesLink(session.page_arena, data.identity, .open, "", "") }); 375 for (data.tags) |tag| 376 try items.append(allocator, .{ .text = tag, .link = try tagLink(session.page_arena, data.identity, .open, tag) }); 377 try tf.setItems(allocator, items.items); 378 try stack.children.put(allocator, tf.getFocus().id, .{ .tag_flow = tf }); 379 } 380 381 // the stack starts on the page's view. 382 stack.getFocus().child_id = stack.children.keys()[viewIndex(data.view)]; 383 384 // focus entering the view lands on the tabs first. 385 outer.getFocus().child_id = outer.children.keys()[header_index]; 386 387 return .{ 388 .box = outer, 389 .data = data, 390 .session = session, 391 .detailed_index = .{ null, null }, 392 .description_id = .{ null, null }, 393 }; 394 } 395 396 // the master-detail split showing `status`'s window. 397 fn initSplit(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self, status: evt.Issue.Status) !wgt.Box(ui.Widget) { 398 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 399 errdefer box.deinit(allocator); 400 401 const win = data.window(status); 402 403 // the issue list (one focusable row per title), plus a "next" link that 404 // reloads the page rooted at the following issue. 405 { 406 var list_scroll = blk: { 407 var list_box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 408 errdefer list_box.deinit(allocator); 409 if (win.prev_id) |prev| 410 try addRow(allocator, &list_box, "← previous", try issuesLink(session.page_arena, data.identity, status, data.tag, prev)); 411 for (win.issues) |entry| 412 try addRow(allocator, &list_box, entry.issue.title, try issueRowLink(session.page_arena, data.identity, entry.id)); 413 if (win.next_id) |next| 414 try addRow(allocator, &list_box, "next →", try issuesLink(session.page_arena, data.identity, status, data.tag, next)); 415 // select the window's first issue (past a leading "previous" 416 // row) so its description shows on load. 417 if (win.issues.len > 0) 418 list_box.getFocus().child_id = list_box.children.keys()[if (win.prev_id != null) 1 else 0] 419 else if (list_box.children.count() > 0) 420 list_box.getFocus().child_id = list_box.children.keys()[0]; 421 break :blk try wgt.Scroll(ui.Widget).init(allocator, .{ .box = list_box }, .{ .direction = .vert, .web_native = !session.is_terminal }); 422 }; 423 errdefer list_scroll.deinit(allocator); 424 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 } }); 425 } 426 427 // the detail pane — a frame around a scroll of the description 428 { 429 var detail_outer = blk: { 430 var detail_scroll = blk2: { 431 var detail_inner = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 432 errdefer detail_inner.deinit(allocator); 433 // fill the pane (content top-left, scroll bar pinned to the 434 // edge) rather than shrinking to the description. 435 break :blk2 try wgt.Scroll(ui.Widget).init(allocator, .{ .box = detail_inner }, .{ .direction = .vert, .web_native = !session.is_terminal, .fill = true }); 436 }; 437 errdefer detail_scroll.deinit(allocator); 438 var frame = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = .hidden, .direction = .vert }); 439 errdefer frame.deinit(allocator); 440 // the frame's selected child is its scroll, so the focus chain 441 // reaches the description (populateDetail points the scroll's 442 // inner box at it), letting focus recovery descend into the pane. 443 frame.getFocus().child_id = detail_scroll.getFocus().id; 444 try frame.children.put(allocator, detail_scroll.getFocus().id, .{ .widget = .{ .scroll = detail_scroll }, .rect = null, .min_size = null }); 445 break :blk frame; 446 }; 447 errdefer detail_outer.deinit(allocator); 448 try box.children.put(allocator, detail_outer.getFocus().id, .{ .widget = .{ .box = detail_outer }, .rect = null, .min_size = .{ .width = detail_min_width, .height = null } }); 449 } 450 451 box.getFocus().child_id = box.children.keys()[list_index]; 452 return box; 453 } 454 455 fn addRow(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), label: []const u8, link: []const u8) !void { 456 var row = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .word }); 457 errdefer row.deinit(allocator); 458 row.getFocus().focusable = true; 459 if (link.len != 0) row.getFocus().kind = .{ .custom = link }; 460 try box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .text_box = row }, .rect = null, .min_size = null, .max_size = .{ .width = null, .height = 5 } }); 461 } 462 463 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 464 self.box.deinit(allocator); 465 } 466 467 fn header(self: *View) *Header { 468 return &self.box.children.values()[header_index].widget.repo_issues_header; 469 } 470 471 fn viewStack(self: *View) *wgt.Stack(ui.Widget) { 472 return &self.box.children.values()[stack_index].widget.stack; 473 } 474 475 // `index`'s master-detail split inside the stack. 476 fn resultsBox(self: *View, index: usize) *wgt.Box(ui.Widget) { 477 return &self.viewStack().children.values()[index].box; 478 } 479 480 // the tags view's flow inside the stack. 481 fn tagsView(self: *View) *ui.TagFlow { 482 return &self.viewStack().children.values()[tags_view_index].tag_flow; 483 } 484 485 fn listScroll(self: *View, index: usize) *wgt.Scroll(ui.Widget) { 486 return &self.resultsBox(index).children.values()[list_index].widget.scroll; 487 } 488 489 fn listBox(self: *View, index: usize) *wgt.Box(ui.Widget) { 490 return &self.listScroll(index).child.box; 491 } 492 493 fn detailOuter(self: *View, index: usize) *wgt.Box(ui.Widget) { 494 return &self.resultsBox(index).children.values()[detail_index].widget.box; 495 } 496 497 fn detailScroll(self: *View, index: usize) *wgt.Scroll(ui.Widget) { 498 return &self.detailOuter(index).children.values()[0].widget.scroll; 499 } 500 501 fn detailInner(self: *View, index: usize) *wgt.Box(ui.Widget) { 502 return &self.detailScroll(index).child.box; 503 } 504 505 fn window(self: *View, index: usize) *const Window { 506 return self.data.window(splitStatus(index)); 507 } 508 509 // the stack's selected master-detail split (null when the tags view shows). 510 fn selectedSplitIndex(self: *View) ?usize { 511 const stack = self.viewStack(); 512 const cid = stack.getFocus().child_id orelse return null; 513 const idx = stack.children.getIndex(cid) orelse return null; 514 return if (idx == tags_view_index) null else idx; 515 } 516 517 fn detailActive(self: *View, index: usize) bool { 518 const rb = self.resultsBox(index); 519 const cid = rb.getFocus().child_id orelse return false; 520 return rb.children.getIndex(cid) == detail_index; 521 } 522 523 fn headerActive(self: *View) bool { 524 const cid = self.box.getFocus().child_id orelse return false; 525 return self.box.children.getIndex(cid) == header_index; 526 } 527 528 fn tagsViewActive(self: *View) bool { 529 if (self.headerActive()) return false; 530 return self.selectedSplitIndex() == null; 531 } 532 533 // the selected issue's index, or null when a window-navigation row is 534 // selected (a leading "previous" row shifts the issue rows down by one). 535 fn selectedIssueIndex(self: *View, index: usize) ?usize { 536 const lb = self.listBox(index); 537 const cid = lb.getFocus().child_id orelse return null; 538 const idx = lb.children.getIndex(cid) orelse return null; 539 const win = self.window(index); 540 const lead: usize = if (win.prev_id != null) 1 else 0; 541 if (idx < lead or idx - lead >= win.issues.len) return null; 542 return idx - lead; 543 } 544 545 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 546 self.clearGrid(); 547 548 // each header tab maps 1:1 to a stack child by position. mirror the 549 // selection into the url: the view the page's window is rooted at 550 // keeps its rooted url, the others get their list route (keeping the 551 // tag filter). 552 if (self.header().getSelectedIndex()) |index| { 553 const stack = self.viewStack(); 554 stack.getFocus().child_id = stack.children.keys()[index]; 555 self.session.data.current_page = (if (index == viewIndex(self.data.view) and self.data.selected_id.len != 0) 556 ui.RoutablePage.repoIssuesRoute(self.data.identity, .open, self.data.tag, self.data.selected_id) 557 else if (index == tags_view_index) 558 ui.RoutablePage.repoIssuesTagsRoute(self.data.identity, self.data.tag) 559 else 560 ui.RoutablePage.repoIssuesRoute(self.data.identity, splitStatus(index), self.data.tag, "")) orelse self.session.data.current_page; 561 } 562 563 if (self.selectedSplitIndex()) |i| { 564 // swap the detail pane to the selected issue when it changes. 565 try self.refreshDetail(allocator, i); 566 567 // mirror the focused issue into the url, but only while focus is 568 // inside the split. an issue's url is the same whether or not the 569 // list is filtered, so the mirror drops the tag. 570 if (root_focus.grandchild_id) |g| { 571 if (self.resultsBox(i).getFocus().children.contains(g)) { 572 if (self.selectedIssueIndex(i)) |sel| { 573 if (ui.RoutablePage.repoIssuesRoute(self.data.identity, splitStatus(i), "", self.window(i).issues[sel].id)) |route| 574 self.session.data.current_page = route; 575 } 576 } 577 } 578 579 // the selected list row shows a border (the focused TextBox 580 // upgrades it to a double border itself); the rest stay borderless. 581 const lb = self.listBox(i); 582 for (lb.children.keys(), lb.children.values()) |id, *child| { 583 switch (child.widget) { 584 .text_box => |*tb| tb.options.border_style = if (lb.getFocus().child_id == id) .single else .hidden, 585 else => {}, 586 } 587 } 588 589 // the pane's selected child shows the selection border: the 590 // description directly, the tags via the flow's selected item. 591 const inner = self.detailInner(i); 592 for (inner.children.keys(), inner.children.values()) |id, *child| { 593 switch (child.widget) { 594 .text_box => |*tb| tb.options.border_style = if (inner.getFocus().child_id == id) .single else .hidden, 595 else => {}, 596 } 597 } 598 if (self.tagFlow(i)) |tf| tf.selected = self.tagsFocused(i); 599 600 // cap the list at list_max_width only while the detail pane fits 601 // beside it. the box drops the detail when the width can't hold 602 // both minimums, so when it's that narrow we lift the cap and let 603 // the list fill the whole width. 604 const both_panes_fit = if (constraint.max_size.width) |w| w >= list_max_width + detail_min_width else true; 605 self.resultsBox(i).children.values()[list_index].max_size = if (both_panes_fit) .{ .width = list_max_width, .height = null } else null; 606 607 // stretch the detail pane across the rest of the width so it fills 608 // the area rather than shrinking to its content; its scroll fills 609 // the pane. 610 if (constraint.max_size.width) |w| { 611 self.resultsBox(i).children.values()[detail_index].min_size = .{ .width = if (both_panes_fit) w - list_max_width else w, .height = null }; 612 } else { 613 self.resultsBox(i).children.values()[detail_index].min_size = .{ .width = detail_min_width, .height = null }; 614 } 615 } 616 617 self.tagsView().selected = self.tagsViewActive(); 618 619 try self.box.build(allocator, constraint, root_focus); 620 } 621 622 fn refreshDetail(self: *View, allocator: std.mem.Allocator, index: usize) !void { 623 const sel = self.selectedIssueIndex(index) orelse return; 624 if (self.detailed_index[index]) |d| if (d == sel) return; 625 try self.populateDetail(allocator, index, sel); 626 self.detailed_index[index] = sel; 627 } 628 629 fn populateDetail(self: *View, allocator: std.mem.Allocator, index: usize, sel: usize) !void { 630 const entry = self.window(index).issues[sel]; 631 const inner = self.detailInner(index); 632 633 for (inner.children.values()) |*child| child.widget.deinit(allocator); 634 inner.children.clearAndFree(allocator); 635 inner.getFocus().child_id = null; 636 637 // the issue's tags, each linking to this status's list filtered to 638 // that tag. 639 { 640 var items: std.ArrayList(ui.TagFlow.Item) = .empty; 641 defer items.deinit(allocator); 642 var tag_iter = evt.Issue.tagIterator(entry.issue.tags); 643 while (tag_iter.next()) |tag| { 644 if (tag.len == 0) continue; 645 try items.append(allocator, .{ .text = tag, .link = try tagLink(self.session.page_arena, self.data.identity, splitStatus(index), tag) }); 646 } 647 if (items.items.len > 0) { 648 var tf = try ui.TagFlow.init(allocator); 649 errdefer tf.deinit(allocator); 650 try tf.setItems(allocator, items.items); 651 try inner.children.put(allocator, tf.getFocus().id, .{ .widget = .{ .tag_flow = tf }, .rect = null, .min_size = null }); 652 } 653 } 654 655 // the description as a focusable word-wrapped text box. its hidden 656 // border reserves the space the border occupies when focused, so 657 // focusing doesn't shift layout. 658 self.description_id[index] = blk: { 659 var tb = try wgt.TextBox(ui.Widget).init(allocator, entry.issue.description, .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .word }); 660 errdefer tb.deinit(allocator); 661 tb.getFocus().focusable = true; 662 try inner.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 663 break :blk tb.getFocus().id; 664 }; 665 666 // select the description by default 667 inner.getFocus().child_id = self.description_id[index]; 668 669 // reset the scroll to the top for the newly-shown issue: directly on the 670 // terminal (the wasm offset), and via a version bump on the web (so the 671 // renderer's scroll id changes and JS drops the preserved position). 672 const sc = self.detailScroll(index); 673 sc.x = 0; 674 sc.y = 0; 675 sc.getFocus().version +%= 1; 676 } 677 678 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 679 if (self.headerActive()) { 680 // down from the tabs re-enters the stack if the selected view has 681 // something to focus; other keys move the tabs. 682 if (inp.vertDirection(key) == .down) { 683 const enterable = if (self.viewStack().getSelected()) |selected| switch (selected.*) { 684 .tag_flow => |*tf| tf.text_boxes.items.len > 0, 685 else => true, 686 } else false; 687 if (enterable) root_focus.setFocus(self.box.children.keys()[stack_index]); 688 } else { 689 try self.header().input(allocator, key, root_focus); 690 } 691 return; 692 } 693 if (self.tagsViewActive()) { 694 self.tagsViewInput(key, root_focus); 695 return; 696 } 697 const i = self.selectedSplitIndex() orelse return; 698 if (self.detailActive(i)) { 699 try self.detailInput(i, key, root_focus); 700 } else { 701 try self.listInput(i, key, root_focus); 702 } 703 } 704 705 // arrow keys move the tag selection; up from the top row crosses to the 706 // header tabs. 707 fn tagsViewInput(self: *View, key: Key, root_focus: *Focus) void { 708 const tf = self.tagsView(); 709 const cid = tf.focus.child_id orelse return; 710 const cur = tf.indexOfFocusId(cid) orelse return; 711 const count = tf.text_boxes.items.len; 712 switch (key) { 713 .arrow_left => if (cur > 0) root_focus.setFocus(tf.text_boxes.items[cur - 1].getFocus().id), 714 .arrow_right => if (cur + 1 < count) root_focus.setFocus(tf.text_boxes.items[cur + 1].getFocus().id), 715 .arrow_up => if (tf.rowStep(cur, false)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id) else self.focusHeader(root_focus), 716 .arrow_down => if (tf.rowStep(cur, true)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id), 717 .home => root_focus.setFocus(tf.text_boxes.items[0].getFocus().id), 718 .end => root_focus.setFocus(tf.text_boxes.items[count - 1].getFocus().id), 719 else => {}, 720 } 721 } 722 723 fn listInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 724 // up/down (and the scroll wheel) move the selection a row; page up/down 725 // jump a fixed amount. right/Enter cross into the detail pane. up from 726 // the top row crosses into the header tabs. 727 if (inp.rowDelta(key, @intCast(self.listBox(index).children.count()))) |delta| { 728 const lb = self.listBox(index); 729 const at_top = if (lb.getFocus().child_id) |cid| lb.children.getIndex(cid) == 0 else true; 730 if (delta < 0 and at_top) return self.focusHeader(root_focus); 731 ui.moveRowFocus(lb, self.listScroll(index), root_focus, delta); 732 return; 733 } 734 switch (key) { 735 .enter, .arrow_right => try self.focusDetail(index, root_focus), 736 else => {}, 737 } 738 } 739 740 fn detailInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 741 if (self.tagsFocused(index)) { 742 try self.tagsInput(index, key, root_focus); 743 } else { 744 try self.descriptionInput(index, key, root_focus); 745 } 746 } 747 748 fn descriptionInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 749 const sc = self.detailScroll(index); 750 switch (key) { 751 .arrow_left => return self.focusList(index, root_focus), 752 // once the scroll can't move further, cross into the tags (or the 753 // header tabs when the issue has none). 754 .arrow_up => { 755 const before = sc.y; 756 sc.y -= 1; 757 sc.clampToContent(); 758 if (sc.y == before) { 759 if (self.tagFlow(index) != null) try self.focusTags(index, root_focus) else self.focusHeader(root_focus); 760 } 761 return; 762 }, 763 .arrow_down => sc.y += 1, 764 .page_up => sc.y -= 10, 765 .page_down => sc.y += 10, 766 .home => sc.y = 0, 767 .end => sc.y = std.math.maxInt(isize), 768 .mouse => |mouse| switch (mouse.action) { 769 .scroll => |dir| sc.y += if (dir == .up) @as(isize, -1) else 1, 770 else => {}, 771 }, 772 else => return, 773 } 774 sc.clampToContent(); 775 } 776 777 // arrow keys move the tag selection; at the flow's edges focus crosses to 778 // the neighboring widgets. 779 fn tagsInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 780 const tf = self.tagFlow(index) orelse return; 781 const cid = tf.focus.child_id orelse return; 782 const cur = tf.indexOfFocusId(cid) orelse return; 783 const count = tf.text_boxes.items.len; 784 const sc = self.detailScroll(index); 785 switch (key) { 786 .arrow_left => if (cur > 0) self.focusTag(index, tf, root_focus, cur - 1) else try self.focusList(index, root_focus), 787 .arrow_right => if (cur + 1 < count) self.focusTag(index, tf, root_focus, cur + 1), 788 .arrow_up => if (tf.rowStep(cur, false)) |i| self.focusTag(index, tf, root_focus, i) else self.focusHeader(root_focus), 789 .arrow_down => if (tf.rowStep(cur, true)) |i| self.focusTag(index, tf, root_focus, i) else try self.focusDescription(index, root_focus), 790 .home => self.focusTag(index, tf, root_focus, 0), 791 .end => self.focusTag(index, tf, root_focus, count - 1), 792 .mouse => |mouse| switch (mouse.action) { 793 .scroll => |dir| { 794 sc.y += if (dir == .up) @as(isize, -1) else 1; 795 sc.clampToContent(); 796 }, 797 else => {}, 798 }, 799 else => {}, 800 } 801 } 802 803 const tags_child_index: usize = 0; 804 805 fn tagFlow(self: *View, index: usize) ?*ui.TagFlow { 806 const inner = self.detailInner(index); 807 if (inner.children.count() == 0) return null; 808 return switch (inner.children.values()[tags_child_index].widget) { 809 .tag_flow => |*tf| tf, 810 else => null, 811 }; 812 } 813 814 fn tagsFocused(self: *View, index: usize) bool { 815 const inner = self.detailInner(index); 816 const cid = inner.getFocus().child_id orelse return false; 817 return inner.children.getIndex(cid) == tags_child_index and self.tagFlow(index) != null; 818 } 819 820 fn focusTag(self: *View, index: usize, tf: *ui.TagFlow, root_focus: *Focus, item: usize) void { 821 root_focus.setFocus(tf.text_boxes.items[item].getFocus().id); 822 // keep the tag visible on the terminal: its rect offset by the flow's 823 // position in the pane. 824 if (self.session.is_terminal and item < tf.rects.items.len) { 825 if (self.detailInner(index).children.values()[tags_child_index].rect) |flow_rect| { 826 var rect = tf.rects.items[item]; 827 rect.x += flow_rect.x; 828 rect.y += flow_rect.y; 829 self.detailScroll(index).scrollToRect(rect); 830 } 831 } 832 } 833 834 fn focusTags(self: *View, index: usize, root_focus: *Focus) !void { 835 const tf = self.tagFlow(index) orelse return; 836 if (tf.text_boxes.items.len == 0) return; 837 const cid = tf.focus.child_id orelse tf.text_boxes.items[0].getFocus().id; 838 const item = tf.indexOfFocusId(cid) orelse 0; 839 self.focusTag(index, tf, root_focus, item); 840 } 841 842 fn focusDescription(self: *View, index: usize, root_focus: *Focus) !void { 843 if (self.description_id[index]) |id| root_focus.setFocus(id); 844 } 845 846 // enter the detail pane. an empty pane (no issues) can't be entered. 847 fn focusDetail(self: *View, index: usize, root_focus: *Focus) !void { 848 if (self.detailInner(index).children.count() == 0) return; 849 root_focus.setFocus(self.detailOuter(index).getFocus().id); 850 } 851 852 // return to the list. 853 fn focusList(self: *View, index: usize, root_focus: *Focus) !void { 854 root_focus.setFocus(self.listScroll(index).getFocus().id); 855 } 856 857 // cross to the header tabs above the stack. 858 fn focusHeader(self: *View, root_focus: *Focus) void { 859 root_focus.setFocus(self.box.children.keys()[header_index]); 860 } 861 862 pub fn clearGrid(self: *View) void { 863 self.box.clearGrid(); 864 } 865 866 pub fn getGrid(self: View) ?Grid { 867 return self.box.getGrid(); 868 } 869 870 pub fn getFocus(self: *View) *Focus { 871 return self.box.getFocus(); 872 } 873 874 // for the parent's "scroll up at the top jumps to the header" check: at the 875 // top only while the header tabs hold focus, so up from the split 876 // crosses into the tabs first. 877 pub fn getSelectedIndex(self: *View) ?usize { 878 return if (self.headerActive()) 0 else 1; 879 } 880 }; 881 882 // the "a:" navigation link for `status`'s issues page filtered to the 883 // url-encoded `tag` and rooted at issue `id` within `identity` ("owner/name"). 884 // a rooted url ignores `status` (it derives its view from the issue's own); 885 // with an empty `id` this is the bare list link. 886 fn issuesLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, status: evt.Issue.Status, tag: []const u8, id: []const u8) ![]const u8 { 887 const route = ui.RoutablePage.repoIssuesRoute(identity, status, tag, id) orelse return error.RouteTooLong; 888 const url = try route.toUrl(page_arena); 889 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 890 } 891 892 // the in-page "ai:" anchor for selecting issue `id` in `identity`'s list; the 893 // href is only followed with js off. 894 fn issueRowLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, id: []const u8) ![]const u8 { 895 const route = ui.RoutablePage.repoIssuesRoute(identity, .open, "", id) orelse return error.RouteTooLong; 896 const url = try route.toUrl(page_arena); 897 return std.fmt.allocPrint(page_arena.allocator(), "ai:{s}", .{url}); 898 } 899 900 // the "a:" link to `status`'s issues list filtered to `tag` (raw; encoded here). 901 fn tagLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, status: evt.Issue.Status, tag: []const u8) ![]const u8 { 902 const encoded = try ui.urlEncodeRef(page_arena.allocator(), tag); 903 const route = ui.RoutablePage.repoIssuesRoute(identity, status, encoded, "") orelse return error.RouteTooLong; 904 const url = try route.toUrl(page_arena); 905 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 906 } 907 908 // tabs switching between the issues page's views. 909 pub const Header = struct { 910 box: wgt.Box(ui.Widget), 911 tab_ids: std.AutoArrayHashMapUnmanaged(usize, void), 912 913 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self) !Header { 914 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 915 errdefer box.deinit(allocator); 916 917 var tab_ids: std.AutoArrayHashMapUnmanaged(usize, void) = .empty; 918 errdefer tab_ids.deinit(allocator); 919 920 const aa = session.page_arena.allocator(); 921 922 // a list tab per status, labeled with its listing's issue count 923 for ([_]evt.Issue.Status{ .open, .closed }) |status| { 924 const route = ui.RoutablePage.repoIssuesRoute(data.identity, status, data.tag, "") orelse return error.RouteTooLong; 925 const link = try std.fmt.allocPrint(aa, "ai:{s}", .{try route.toUrl(session.page_arena)}); 926 const label = try std.fmt.allocPrint(aa, "{s} ({d})", .{ @tagName(status), data.window(status).count }); 927 var text_box = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 928 errdefer text_box.deinit(allocator); 929 text_box.getFocus().focusable = true; 930 text_box.getFocus().kind = .{ .custom = link }; 931 try tab_ids.put(allocator, text_box.getFocus().id, {}); 932 try box.children.put(allocator, text_box.getFocus().id, .{ 933 .widget = .{ .text_box = text_box }, 934 .rect = null, 935 .min_size = .{ .width = label.len + 2, .height = null }, 936 }); 937 } 938 939 // tags tab, labeled with the active tag filter 940 { 941 const tags_route = ui.RoutablePage.repoIssuesTagsRoute(data.identity, data.tag) orelse return error.RouteTooLong; 942 const tags_link = try std.fmt.allocPrint(aa, "ai:{s}", .{try tags_route.toUrl(session.page_arena)}); 943 const label = if (data.tag.len == 0) "tags" else blk: { 944 const decoded = std.Uri.percentDecodeInPlace(try aa.dupe(u8, data.tag)); 945 break :blk try std.fmt.allocPrint(aa, "tags ({s})", .{decoded}); 946 }; 947 var text_box = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 948 errdefer text_box.deinit(allocator); 949 text_box.getFocus().focusable = true; 950 text_box.getFocus().kind = .{ .custom = tags_link }; 951 try tab_ids.put(allocator, text_box.getFocus().id, {}); 952 try box.children.put(allocator, text_box.getFocus().id, .{ 953 .widget = .{ .text_box = text_box }, 954 .rect = null, 955 .min_size = .{ .width = label.len + 2, .height = null }, 956 }); 957 } 958 959 var self = Header{ .box = box, .tab_ids = tab_ids }; 960 // the tab matching the page's view is selected initially. 961 self.getFocus().child_id = self.tab_ids.keys()[View.viewIndex(data.view)]; 962 return self; 963 } 964 965 pub fn deinit(self: *Header, allocator: std.mem.Allocator) void { 966 self.box.deinit(allocator); 967 self.tab_ids.deinit(allocator); 968 } 969 970 pub fn build(self: *Header, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 971 self.clearGrid(); 972 // only the selected tab shows its border 973 for (self.box.children.keys(), self.box.children.values()) |id, *child| { 974 switch (child.widget) { 975 .text_box => |*tb| tb.options.border_style = if (self.getFocus().child_id == id) .single else .hidden, 976 else => {}, 977 } 978 } 979 try self.box.build(allocator, constraint, root_focus); 980 } 981 982 pub fn input(self: *Header, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 983 _ = allocator; 984 const current_tab = self.currentTabIndex() orelse return; 985 if (inp.moveTab(key, current_tab, self.tab_ids.count())) |new_tab| { 986 root_focus.setFocus(self.tab_ids.keys()[new_tab]); 987 } 988 } 989 990 pub fn clearGrid(self: *Header) void { 991 self.box.clearGrid(); 992 } 993 994 pub fn getGrid(self: Header) ?Grid { 995 return self.box.getGrid(); 996 } 997 998 pub fn getFocus(self: *Header) *Focus { 999 return self.box.getFocus(); 1000 } 1001 1002 pub fn getSelectedIndex(self: Header) ?usize { 1003 return self.currentTabIndex(); 1004 } 1005 1006 fn currentTabIndex(self: Header) ?usize { 1007 const child_id = self.box.focus.child_id orelse return null; 1008 return self.tab_ids.getIndex(child_id); 1009 } 1010 }; 1011