╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Issues.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
198 // "tag,status", so a tag's entries are adjacent and dedup by prefix. 199 var tags: std.ArrayList([]const u8) = .empty; 200 if (try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, "tag+status->issue-id-set"))) |tag_to_issues_cursor| { 201 const tag_to_issues = try DB.SortedMap(.read_only).init(tag_to_issues_cursor); 202 var tag_iter = try tag_to_issues.iterator(); 203 while (try tag_iter.next()) |kv_pair_cursor| { 204 if (tags.items.len == max_tags) break; 205 var kv_cursor = kv_pair_cursor; 206 const kv_pair = try kv_cursor.readKeyValuePair(); 207 const key = try kv_pair.key_cursor.readBytesAlloc(aa, null); 208 const space = std.mem.indexOfScalar(u8, key, ' ') orelse continue; 209 const key_tag = key[0..space]; 210 if (tags.getLastOrNull()) |last| { 211 if (std.mem.eql(u8, last, key_tag)) continue; 212 } 213 try tags.append(aa, key_tag); 214 } 215 } 216 217 return .{ 218 .identity = empty.identity, 219 .tag = empty.tag, 220 .selected_id = empty.selected_id, 221 .open = open_window, 222 .closed = closed_window, 223 .view = resolved_view, 224 .tags = tags.items, 225 }; 226 } 227 228 // `status`'s sorted set within `statuses` (null when the status has no issues) 229 fn statusSet( 230 comptime DB: type, 231 statuses: DB.SortedMap(.read_only), 232 status: evt.Issue.Status, 233 ) !?DB.SortedSet(.read_only) { 234 const cursor = (try statuses.getCursor(@tagName(status))) orelse return null; 235 return try DB.SortedSet(.read_only).init(cursor); 236 } 237 238 // the set at `tag_to_issues`'s "tag,status" key (null when no issue carries 239 // `tag` with `status`, or the tag is too long to exist) 240 fn tagStatusSet( 241 comptime DB: type, 242 tag_to_issues: DB.SortedMap(.read_only), 243 tag: []const u8, 244 status: evt.Issue.Status, 245 ) !?DB.SortedSet(.read_only) { 246 var key_buffer: evt.Issue.TagStatusKey = undefined; 247 const key = evt.Issue.tagStatusKey(&key_buffer, tag, status) catch return null; 248 const cursor = (try tag_to_issues.getCursor(key)) orelse return null; 249 return try DB.SortedSet(.read_only).init(cursor); 250 } 251 252 // read one window of `set_maybe` (null = an empty listing) starting at 253 // `root_key` (null = the beginning) 254 fn loadWindow( 255 comptime hash_kind: hash.HashKind, 256 arena: *std.heap.ArenaAllocator, 257 event_id_to_issue: evt.EventDB(hash_kind).HashMap(.read_only), 258 set_maybe: ?evt.EventDB(hash_kind).SortedSet(.read_only), 259 root_key: ?[]const u8, 260 ) !Window { 261 const set = set_maybe orelse return .empty; 262 const DB = evt.EventDB(hash_kind); 263 const aa = arena.allocator(); 264 265 // seek once to the window start: the root key, or the set's first entry. 266 var prev_id: ?[]const u8 = null; 267 var iter = if (root_key) |key| blk: { 268 // the previous window starts page_size ranks back ("" = the first 269 // window, linked as the bare list route). 270 const rank = try set.rank(key); 271 if (rank > 0 and rank <= page_size) { 272 prev_id = ""; 273 } else if (rank > page_size) { 274 const kv = try set.getIndexKeyValuePair(@intCast(rank - page_size)) orelse return error.NotFound; 275 var prev_key: [@sizeOf(u64) + evt.event_id_size]u8 = undefined; 276 _ = try kv.key_cursor.readBytes(&prev_key); 277 const prev_hex = std.fmt.bytesToHex(prev_key[@sizeOf(u64)..].*, .lower); 278 prev_id = try aa.dupe(u8, &prev_hex); 279 } 280 break :blk try set.iteratorFrom(key); 281 } else try set.iteratorFromIndex(0); 282 283 // collect this window's issues, plus a peek at the one after it (its id is 284 // the next window's start). the trailing bytes of each set key are the 285 // issue event id. 286 var issues: std.ArrayList(IssueWithId) = .empty; 287 var next_id: ?[]const u8 = null; 288 while (try iter.next()) |id_cursor_val| { 289 var id_cursor = id_cursor_val; 290 const id_kv = try id_cursor.readKeyValuePair(); 291 var order_key: [@sizeOf(u64) + evt.event_id_size]u8 = undefined; 292 _ = try id_kv.key_cursor.readBytes(&order_key); 293 const id_hex = std.fmt.bytesToHex(order_key[@sizeOf(u64)..].*, .lower); 294 if (issues.items.len == page_size) { 295 next_id = try aa.dupe(u8, &id_hex); 296 break; 297 } 298 const issue_cursor = try event_id_to_issue.getCursor(hash.hashInt(hash_kind, order_key[@sizeOf(u64)..])) orelse continue; 299 const issue_map = try DB.HashMap(.read_only).init(issue_cursor); 300 try issues.append(aa, .{ 301 .id = try aa.dupe(u8, &id_hex), 302 .issue = try evt.read(evt.Issue, DB, hash_kind, arena, issue_map), 303 }); 304 } 305 306 return .{ 307 .issues = issues.items, 308 .prev_id = prev_id, 309 .next_id = next_id, 310 .count = @intCast(try set.count()), 311 }; 312 } 313 314 pub const View = struct { 315 // a vertical box: the header tabs on top, then a stack holding a 316 // master-detail split (issue list + description pane) per status list, 317 // plus the tags view. 318 box: wgt.Box(ui.Widget), // vert: [header_index] = tabs, [stack_index] = stack 319 data: *const Self, 320 session: *ui.Session, 321 // per-split state, indexed like the stack's split children: the issue the 322 // pane shows, and its description text box's focus id. 323 detailed_index: [split_count]?usize, 324 description_id: [split_count]?usize, 325 326 const header_index: usize = 0; 327 const stack_index: usize = 1; 328 // indices within the stack, 1:1 with the header tabs. 329 const open_view_index: usize = 0; 330 const closed_view_index: usize = 1; 331 const tags_view_index: usize = 2; 332 // the new-issue form, or the edit form when the page was loaded at an 333 // edit url. 334 const form_view_index: usize = 3; 335 const split_count: usize = 2; 336 // indices within a split (the horizontal box inside the stack). 337 const list_index: usize = 0; 338 const detail_index: usize = 1; 339 const list_max_width: usize = 40; 340 const detail_min_width: usize = 40; 341 // indices within the new-issue form. 342 const title_field_index: usize = 0; 343 const tags_field_index: usize = 1; 344 const description_field_index: usize = 2; 345 const submit_field_index: usize = 3; 346 347 fn viewIndex(view: ui.RoutablePage.IssuesView) usize { 348 return switch (view) { 349 .open => open_view_index, 350 .closed => closed_view_index, 351 .tags => tags_view_index, 352 .new, .edit => form_view_index, 353 }; 354 } 355 356 fn splitStatus(index: usize) evt.Issue.Status { 357 return if (index == open_view_index) .open else .closed; 358 } 359 360 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { 361 var outer = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 362 errdefer outer.deinit(allocator); 363 364 // the tabs at the top. 365 { 366 var hdr = try Header.init(allocator, session, data); 367 errdefer hdr.deinit(allocator); 368 try outer.children.put(allocator, hdr.getFocus().id, .{ .widget = .{ .repo_issues_header = hdr }, .rect = null, .min_size = null }); 369 } 370 371 // the stack enters `outer` before its children enter it, so an error 372 // frees each child exactly once. 373 { 374 var stack = try wgt.Stack(ui.Widget).init(allocator); 375 errdefer stack.deinit(allocator); 376 try outer.children.put(allocator, stack.getFocus().id, .{ .widget = .{ .stack = stack }, .rect = null, .min_size = null }); 377 } 378 const stack = &outer.children.values()[stack_index].widget.stack; 379 380 // a master-detail split per status list. 381 for ([_]evt.Issue.Status{ .open, .closed }) |status| { 382 var split = try initSplit(allocator, session, data, status); 383 errdefer split.deinit(allocator); 384 try stack.children.put(allocator, split.getFocus().id, .{ .box = split }); 385 } 386 387 // the tags view 388 { 389 var tf = try ui.TagFlow.init(allocator); 390 errdefer tf.deinit(allocator); 391 var items: std.ArrayList(ui.TagFlow.Item) = .empty; 392 defer items.deinit(allocator); 393 // when filtered, the first item clears the filter 394 if (data.tag.len != 0) 395 try items.append(allocator, .{ .text = "✕", .link = try issuesLink(session.page_arena, data.identity, .open, "", "") }); 396 for (data.tags) |tag| 397 try items.append(allocator, .{ .text = tag, .link = try tagLink(session.page_arena, data.identity, .open, tag) }); 398 try tf.setItems(allocator, items.items); 399 try stack.children.put(allocator, tf.getFocus().id, .{ .tag_flow = tf }); 400 } 401 402 // the new-issue form, or — on an edit url — the edit form in its 403 // place, prefilled with the selected issue's content. 404 { 405 const aa = session.page_arena.allocator(); 406 const action = if (data.view == .edit) 407 (if (data.identity.len == 0) 408 try std.fmt.allocPrint(aa, "form:/issues/{s}/edit", .{data.selected_id}) 409 else 410 try std.fmt.allocPrint(aa, "form:/repo/{s}/issues/{s}/edit", .{ data.identity, data.selected_id })) 411 else if (data.identity.len == 0) 412 "form:/issue" 413 else 414 try std.fmt.allocPrint(aa, "form:/repo/{s}/issue", .{data.identity}); 415 const issue = if (data.view == .edit) 416 (if (data.selectedIssue()) |entry| &entry.issue else null) 417 else 418 null; 419 var form = try initIssueForm(allocator, action, issue); 420 errdefer form.deinit(allocator); 421 try stack.children.put(allocator, form.getFocus().id, .{ .box = form }); 422 } 423 424 // the stack starts on the page's view. 425 stack.getFocus().child_id = stack.children.keys()[viewIndex(data.view)]; 426 427 // focus entering the view lands on the tabs first. 428 outer.getFocus().child_id = outer.children.keys()[header_index]; 429 430 return .{ 431 .box = outer, 432 .data = data, 433 .session = session, 434 .detailed_index = .{ null, null }, 435 .description_id = .{ null, null }, 436 }; 437 } 438 439 // the master-detail split showing `status`'s window. 440 fn initSplit(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self, status: evt.Issue.Status) !wgt.Box(ui.Widget) { 441 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 442 errdefer box.deinit(allocator); 443 444 const win = data.window(status); 445 446 // the issue list (one focusable row per title), plus a "next" link that 447 // reloads the page rooted at the following issue. 448 { 449 var list_scroll = blk: { 450 var list_box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 451 errdefer list_box.deinit(allocator); 452 if (win.prev_id) |prev| 453 try addRow(allocator, &list_box, "← previous", try issuesLink(session.page_arena, data.identity, status, data.tag, prev)); 454 for (win.issues) |entry| 455 try addRow(allocator, &list_box, entry.issue.title, try issueRowLink(session.page_arena, data.identity, entry.id)); 456 if (win.next_id) |next| 457 try addRow(allocator, &list_box, "next →", try issuesLink(session.page_arena, data.identity, status, data.tag, next)); 458 // select the window's first issue (past a leading "previous" 459 // row) so its description shows on load. 460 if (win.issues.len > 0) 461 list_box.getFocus().child_id = list_box.children.keys()[if (win.prev_id != null) 1 else 0] 462 else if (list_box.children.count() > 0) 463 list_box.getFocus().child_id = list_box.children.keys()[0]; 464 break :blk try wgt.Scroll(ui.Widget).init(allocator, .{ .box = list_box }, .{ .direction = .vert, .web_native = !session.is_terminal }); 465 }; 466 errdefer list_scroll.deinit(allocator); 467 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 } }); 468 } 469 470 // the detail pane — a frame around a scroll of the description 471 { 472 var detail_outer = blk: { 473 var detail_scroll = blk2: { 474 var detail_inner = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 475 errdefer detail_inner.deinit(allocator); 476 // fill the pane (content top-left, scroll bar pinned to the 477 // edge) rather than shrinking to the description. 478 break :blk2 try wgt.Scroll(ui.Widget).init(allocator, .{ .box = detail_inner }, .{ .direction = .vert, .web_native = !session.is_terminal, .fill = true }); 479 }; 480 errdefer detail_scroll.deinit(allocator); 481 var frame = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = .hidden, .direction = .vert }); 482 errdefer frame.deinit(allocator); 483 // the tool row sits above the scroll (populateDetail fills 484 // it per issue) so it can't scroll out from under the web 485 // overlay <form>, whose position doesn't track pane scrolling. 486 { 487 var row = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 488 errdefer row.deinit(allocator); 489 try frame.children.put(allocator, row.getFocus().id, .{ .widget = .{ .box = row }, .rect = null, .min_size = null }); 490 } 491 // the frame's selected child is its scroll, so the focus chain 492 // reaches the description (populateDetail points the scroll's 493 // inner box at it), letting focus recovery descend into the pane. 494 frame.getFocus().child_id = detail_scroll.getFocus().id; 495 try frame.children.put(allocator, detail_scroll.getFocus().id, .{ .widget = .{ .scroll = detail_scroll }, .rect = null, .min_size = null }); 496 break :blk frame; 497 }; 498 errdefer detail_outer.deinit(allocator); 499 try box.children.put(allocator, detail_outer.getFocus().id, .{ .widget = .{ .box = detail_outer }, .rect = null, .min_size = .{ .width = detail_min_width, .height = null } }); 500 } 501 502 box.getFocus().child_id = box.children.keys()[list_index]; 503 return box; 504 } 505 506 // an issue form: title/tags/description inputs and a submit button, 507 // prefilled from `issue` when given. its form: subtree makes the web 508 // overlay wrap them in a <form> POSTing to `action`'s route. 509 fn initIssueForm(allocator: std.mem.Allocator, action: []const u8, issue: ?*const evt.Issue) !wgt.Box(ui.Widget) { 510 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 511 errdefer box.deinit(allocator); 512 box.getFocus().kind = .{ .custom = action }; 513 514 { 515 var title = try wgt.TextInput(ui.Widget).init(allocator, .{ .label = " title ", .name = "title", .visible_width = null, .rounded_corners = true, .render_content = !wasm }); 516 errdefer title.deinit(allocator); 517 title.getFocus().focusable = true; 518 if (issue) |i| try title.setContent(allocator, i.title); 519 try box.children.put(allocator, title.getFocus().id, .{ .widget = .{ .text_input = title }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 520 } 521 522 { 523 var tags = try wgt.TextInput(ui.Widget).init(allocator, .{ .label = " tags (separate with spaces) ", .name = "tags", .visible_width = null, .rounded_corners = true, .render_content = !wasm }); 524 errdefer tags.deinit(allocator); 525 tags.getFocus().focusable = true; 526 if (issue) |i| try tags.setContent(allocator, i.tags); 527 try box.children.put(allocator, tags.getFocus().id, .{ .widget = .{ .text_input = tags }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 528 } 529 530 { 531 var description = try wgt.TextInput(ui.Widget).init(allocator, .{ .label = " description ", .name = "description", .visible_width = null, .rounded_corners = true, .render_content = !wasm, .multiline = true, .scroll = .{ .fill = true } }); 532 errdefer description.deinit(allocator); 533 description.getFocus().focusable = true; 534 if (issue) |i| try description.setContent(allocator, i.description); 535 try box.children.put(allocator, description.getFocus().id, .{ .widget = .{ .text_input = description }, .rect = null, .min_size = null }); 536 } 537 538 { 539 var button = try wgt.TextBox(ui.Widget).init(allocator, "submit", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 540 errdefer button.deinit(allocator); 541 button.getFocus().focusable = true; 542 // the renderer distinguishes plain clickables from buttons that 543 // should POST to a server route by this kind. 544 button.getFocus().kind = .{ .custom = "submit" }; 545 try box.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 546 } 547 548 // absorbs the leftover min-height the box hands its last child, so 549 // the button keeps its natural height 550 { 551 var spacer = try ui.Spacer.init(allocator); 552 errdefer spacer.deinit(allocator); 553 try box.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 554 } 555 556 box.getFocus().child_id = box.children.keys()[title_field_index]; 557 return box; 558 } 559 560 fn addRow(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), label: []const u8, link: []const u8) !void { 561 var row = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .word }); 562 errdefer row.deinit(allocator); 563 row.getFocus().focusable = true; 564 if (link.len != 0) row.getFocus().kind = .{ .custom = link }; 565 try box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .text_box = row }, .rect = null, .min_size = null, .max_size = .{ .width = null, .height = 5 } }); 566 } 567 568 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 569 self.box.deinit(allocator); 570 } 571 572 fn header(self: *View) *Header { 573 return &self.box.children.values()[header_index].widget.repo_issues_header; 574 } 575 576 fn viewStack(self: *View) *wgt.Stack(ui.Widget) { 577 return &self.box.children.values()[stack_index].widget.stack; 578 } 579 580 // `index`'s master-detail split inside the stack. 581 fn resultsBox(self: *View, index: usize) *wgt.Box(ui.Widget) { 582 return &self.viewStack().children.values()[index].box; 583 } 584 585 // the tags view's flow inside the stack. 586 fn tagsView(self: *View) *ui.TagFlow { 587 return &self.viewStack().children.values()[tags_view_index].tag_flow; 588 } 589 590 // the new-issue (or edit) form inside the stack. 591 fn issueForm(self: *View) *wgt.Box(ui.Widget) { 592 return &self.viewStack().children.values()[form_view_index].box; 593 } 594 595 fn listScroll(self: *View, index: usize) *wgt.Scroll(ui.Widget) { 596 return &self.resultsBox(index).children.values()[list_index].widget.scroll; 597 } 598 599 fn listBox(self: *View, index: usize) *wgt.Box(ui.Widget) { 600 return &self.listScroll(index).child.box; 601 } 602 603 // the detail frame's children: the tool row above the scroll pane. 604 const tool_row_index: usize = 0; 605 const detail_scroll_index: usize = 1; 606 607 fn detailOuter(self: *View, index: usize) *wgt.Box(ui.Widget) { 608 return &self.resultsBox(index).children.values()[detail_index].widget.box; 609 } 610 611 fn toolRow(self: *View, index: usize) *wgt.Box(ui.Widget) { 612 return &self.detailOuter(index).children.values()[tool_row_index].widget.box; 613 } 614 615 fn detailScroll(self: *View, index: usize) *wgt.Scroll(ui.Widget) { 616 return &self.detailOuter(index).children.values()[detail_scroll_index].widget.scroll; 617 } 618 619 fn detailInner(self: *View, index: usize) *wgt.Box(ui.Widget) { 620 return &self.detailScroll(index).child.box; 621 } 622 623 fn window(self: *View, index: usize) *const Window { 624 return self.data.window(splitStatus(index)); 625 } 626 627 fn stackSelectedIndex(self: *View) ?usize { 628 const stack = self.viewStack(); 629 const cid = stack.getFocus().child_id orelse return null; 630 return stack.children.getIndex(cid); 631 } 632 633 // the stack's selected master-detail split (null when the tags view or the 634 // new-issue form shows). 635 fn selectedSplitIndex(self: *View) ?usize { 636 const idx = self.stackSelectedIndex() orelse return null; 637 return if (idx < split_count) idx else null; 638 } 639 640 fn detailActive(self: *View, index: usize) bool { 641 const rb = self.resultsBox(index); 642 const cid = rb.getFocus().child_id orelse return false; 643 return rb.children.getIndex(cid) == detail_index; 644 } 645 646 fn headerActive(self: *View) bool { 647 const cid = self.box.getFocus().child_id orelse return false; 648 return self.box.children.getIndex(cid) == header_index; 649 } 650 651 fn tagsViewActive(self: *View) bool { 652 if (self.headerActive()) return false; 653 return self.stackSelectedIndex() == tags_view_index; 654 } 655 656 fn formViewActive(self: *View) bool { 657 if (self.headerActive()) return false; 658 return self.stackSelectedIndex() == form_view_index; 659 } 660 661 // the selected issue's index, or null when a window-navigation row is 662 // selected (a leading "previous" row shifts the issue rows down by one). 663 fn selectedIssueIndex(self: *View, index: usize) ?usize { 664 const lb = self.listBox(index); 665 const cid = lb.getFocus().child_id orelse return null; 666 const idx = lb.children.getIndex(cid) orelse return null; 667 const win = self.window(index); 668 const lead: usize = if (win.prev_id != null) 1 else 0; 669 if (idx < lead or idx - lead >= win.issues.len) return null; 670 return idx - lead; 671 } 672 673 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 674 self.clearGrid(); 675 676 // each header tab maps 1:1 to a stack child by position. mirror the 677 // selection into the url: the view the page's window is rooted at 678 // keeps its rooted url, the others get their list route (keeping the 679 // tag filter). 680 if (self.header().getSelectedIndex()) |index| { 681 const stack = self.viewStack(); 682 stack.getFocus().child_id = stack.children.keys()[index]; 683 self.session.data.current_page = (if (index == form_view_index and self.data.view == .edit) 684 ui.RoutablePage.repoIssuesEditRoute(self.data.identity, self.data.selected_id) 685 else if (index == viewIndex(self.data.view) and self.data.selected_id.len != 0) 686 ui.RoutablePage.repoIssuesRoute(self.data.identity, .open, self.data.tag, self.data.selected_id) 687 else if (index == tags_view_index) 688 ui.RoutablePage.repoIssuesTagsRoute(self.data.identity, self.data.tag) 689 else if (index == form_view_index) 690 ui.RoutablePage.repoIssuesNewRoute(self.data.identity) 691 else 692 ui.RoutablePage.repoIssuesRoute(self.data.identity, splitStatus(index), self.data.tag, "")) orelse self.session.data.current_page; 693 } 694 695 if (self.selectedSplitIndex()) |i| { 696 // swap the detail pane to the selected issue when it changes. 697 try self.refreshDetail(allocator, i); 698 699 // mirror the focused issue into the url, but only while focus is 700 // inside the split. an issue's url is the same whether or not the 701 // list is filtered, so the mirror drops the tag. 702 if (root_focus.grandchild_id) |g| { 703 if (self.resultsBox(i).getFocus().children.contains(g)) { 704 if (self.selectedIssueIndex(i)) |sel| { 705 if (ui.RoutablePage.repoIssuesRoute(self.data.identity, splitStatus(i), "", self.window(i).issues[sel].id)) |route| 706 self.session.data.current_page = route; 707 } 708 } 709 } 710 711 // the selected list row shows a border (the focused TextBox 712 // upgrades it to a double border itself); the rest stay borderless. 713 const lb = self.listBox(i); 714 for (lb.children.keys(), lb.children.values()) |id, *child| { 715 switch (child.widget) { 716 .text_box => |*tb| tb.options.border_style = if (lb.getFocus().child_id == id) .single else .hidden, 717 else => {}, 718 } 719 } 720 721 // the pane's selected child shows the selection border: the 722 // description directly, the tags via the flow's selected item. 723 // cap the list at list_max_width only while the detail pane fits 724 // beside it. the box drops the detail when the width can't hold 725 // both minimums, so when it's that narrow we lift the cap and let 726 // the list fill the whole width. 727 const both_panes_fit = if (constraint.max_size.width) |w| w >= list_max_width + detail_min_width else true; 728 self.resultsBox(i).children.values()[list_index].max_size = if (both_panes_fit) .{ .width = list_max_width, .height = null } else null; 729 730 // stretch the detail pane across the rest of the width so it fills 731 // the area rather than shrinking to its content; its scroll fills 732 // the pane. 733 if (constraint.max_size.width) |w| { 734 self.resultsBox(i).children.values()[detail_index].min_size = .{ .width = if (both_panes_fit) w - list_max_width else w, .height = null }; 735 } else { 736 self.resultsBox(i).children.values()[detail_index].min_size = .{ .width = detail_min_width, .height = null }; 737 } 738 } 739 740 // refresh the form inputs' entries in the session's focus-id -> input 741 // map with this frame's addresses, so the web/wasm form handling can 742 // find them by focus id 743 const form = self.issueForm(); 744 const inputs_arena = self.session.arena.allocator(); 745 for (form.children.values()) |*child| switch (child.widget) { 746 .text_input => |*ti| try self.session.text_inputs.put(inputs_arena, ti.getFocus().id, ti), 747 else => {}, 748 }; 749 750 // on an edit page, register the issue's content as the inputs' 751 // page-constant initial values, which the web overlay renders 752 // into them. 753 if (self.data.view == .edit) { 754 if (self.data.selectedIssue()) |entry| { 755 const fields = form.children.values(); 756 try self.session.input_values.put(inputs_arena, fields[title_field_index].widget.text_input.getFocus().id, entry.issue.title); 757 try self.session.input_values.put(inputs_arena, fields[tags_field_index].widget.text_input.getFocus().id, entry.issue.tags); 758 try self.session.input_values.put(inputs_arena, fields[description_field_index].widget.text_input.getFocus().id, entry.issue.description); 759 } 760 } 761 762 try self.box.build(allocator, constraint, root_focus); 763 } 764 765 fn refreshDetail(self: *View, allocator: std.mem.Allocator, index: usize) !void { 766 const sel = self.selectedIssueIndex(index) orelse return; 767 if (self.detailed_index[index]) |d| if (d == sel) return; 768 try self.populateDetail(allocator, index, sel); 769 self.detailed_index[index] = sel; 770 } 771 772 fn populateDetail(self: *View, allocator: std.mem.Allocator, index: usize, sel: usize) !void { 773 const entry = self.window(index).issues[sel]; 774 const inner = self.detailInner(index); 775 776 for (inner.children.values()) |*child| child.widget.deinit(allocator); 777 inner.children.clearAndFree(allocator); 778 inner.getFocus().child_id = null; 779 780 // the open/close button 781 { 782 const action: []const u8 = switch (entry.issue.status) { 783 .open => "close", 784 .closed => "open", 785 }; 786 const row = self.toolRow(index); 787 for (row.children.values()) |*child| child.widget.deinit(allocator); 788 row.children.clearAndFree(allocator); 789 const pa = self.session.page_arena.allocator(); 790 row.getFocus().kind = .{ .custom = if (self.data.identity.len == 0) 791 try std.fmt.allocPrint(pa, "form:/issues/{s}/{s}", .{ entry.id, action }) 792 else 793 try std.fmt.allocPrint(pa, "form:/repo/{s}/issues/{s}/{s}", .{ self.data.identity, entry.id, action }) }; 794 795 { 796 var spacer = try ui.Spacer.init(allocator); 797 errdefer spacer.deinit(allocator); 798 try row.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 799 } 800 801 { 802 var button = try wgt.TextBox(ui.Widget).init(allocator, action, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 803 errdefer button.deinit(allocator); 804 button.getFocus().focusable = true; 805 // the renderer distinguishes plain clickables from buttons that 806 // should POST to a server route by this kind. 807 button.getFocus().kind = .{ .custom = "submit" }; 808 try row.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = .{ .width = action.len + 2, .height = null } }); 809 } 810 811 // the edit button links to the issue's edit page. 812 { 813 var button = try wgt.TextBox(ui.Widget).init(allocator, "edit", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 814 errdefer button.deinit(allocator); 815 button.getFocus().focusable = true; 816 const route = ui.RoutablePage.repoIssuesEditRoute(self.data.identity, entry.id) orelse return error.RouteTooLong; 817 button.getFocus().kind = .{ .custom = try std.fmt.allocPrint(pa, "a:{s}", .{try route.toUrl(self.session.page_arena)}) }; 818 try row.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = .{ .width = "edit".len + 2, .height = null } }); 819 } 820 821 row.getFocus().child_id = row.children.keys()[button_in_row_index]; 822 } 823 824 // the issue's title as a focusable word-wrapped text box. 825 { 826 var tb = try wgt.TextBox(ui.Widget).init(allocator, entry.issue.title, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .word }); 827 errdefer tb.deinit(allocator); 828 tb.getFocus().focusable = true; 829 try inner.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 830 } 831 832 // the issue's tags, each linking to this status's list filtered to 833 // that tag. 834 { 835 var items: std.ArrayList(ui.TagFlow.Item) = .empty; 836 defer items.deinit(allocator); 837 var tag_iter = evt.Issue.tagIterator(entry.issue.tags); 838 while (tag_iter.next()) |tag| { 839 if (tag.len == 0) continue; 840 try items.append(allocator, .{ .text = tag, .link = try tagLink(self.session.page_arena, self.data.identity, splitStatus(index), tag) }); 841 } 842 if (items.items.len > 0) { 843 var tf = try ui.TagFlow.init(allocator); 844 errdefer tf.deinit(allocator); 845 try tf.setItems(allocator, items.items); 846 try inner.children.put(allocator, tf.getFocus().id, .{ .widget = .{ .tag_flow = tf }, .rect = null, .min_size = null }); 847 } 848 } 849 850 // the description as a focusable word-wrapped text box. 851 self.description_id[index] = blk: { 852 const description = if (entry.issue.description.len == 0) "(no description)" else entry.issue.description; 853 var tb = try wgt.TextBox(ui.Widget).init(allocator, description, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .word }); 854 errdefer tb.deinit(allocator); 855 tb.getFocus().focusable = true; 856 try inner.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 857 break :blk tb.getFocus().id; 858 }; 859 860 // select the title by default 861 inner.getFocus().child_id = inner.children.keys()[title_child_index]; 862 863 // reset the scroll to the top for the newly-shown issue: directly on the 864 // terminal (the wasm offset), and via a version bump on the web (so the 865 // renderer's scroll id changes and JS drops the preserved position). 866 const sc = self.detailScroll(index); 867 sc.x = 0; 868 sc.y = 0; 869 sc.getFocus().version +%= 1; 870 } 871 872 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 873 if (self.headerActive()) { 874 // down from the tabs re-enters the stack if the selected view has 875 // something to focus; other keys move the tabs. 876 if (inp.vertDirection(key) == .down) { 877 const enterable = if (self.viewStack().getSelected()) |selected| switch (selected.*) { 878 .tag_flow => |*tf| tf.text_boxes.items.len > 0, 879 else => true, 880 } else false; 881 if (enterable) root_focus.setFocus(self.box.children.keys()[stack_index]); 882 } else { 883 try self.header().input(allocator, key, root_focus); 884 } 885 return; 886 } 887 if (self.tagsViewActive()) { 888 self.tagsViewInput(key, root_focus); 889 return; 890 } 891 if (self.formViewActive()) { 892 try self.formInput(allocator, key, root_focus); 893 return; 894 } 895 const i = self.selectedSplitIndex() orelse return; 896 if (self.detailActive(i)) { 897 try self.detailInput(allocator, i, key, root_focus); 898 } else { 899 try self.listInput(i, key, root_focus); 900 } 901 } 902 903 // arrow keys move the tag selection; up from the top row crosses to the 904 // header tabs. 905 fn tagsViewInput(self: *View, key: Key, root_focus: *Focus) void { 906 const tf = self.tagsView(); 907 const cid = tf.focus.child_id orelse return; 908 const cur = tf.indexOfFocusId(cid) orelse return; 909 const count = tf.text_boxes.items.len; 910 switch (key) { 911 .arrow_left => if (cur > 0) root_focus.setFocus(tf.text_boxes.items[cur - 1].getFocus().id), 912 .arrow_right => if (cur + 1 < count) root_focus.setFocus(tf.text_boxes.items[cur + 1].getFocus().id), 913 .arrow_up => if (tf.rowStep(cur, false)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id) else self.focusHeader(root_focus), 914 .arrow_down => if (tf.rowStep(cur, true)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id), 915 .home => root_focus.setFocus(tf.text_boxes.items[0].getFocus().id), 916 .end => root_focus.setFocus(tf.text_boxes.items[count - 1].getFocus().id), 917 else => {}, 918 } 919 } 920 921 fn listInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 922 // up/down (and the scroll wheel) move the selection a row; page up/down 923 // jump a fixed amount. right/Enter cross into the detail pane. up from 924 // the top row crosses into the header tabs. 925 if (inp.rowDelta(key, @intCast(self.listBox(index).children.count()))) |delta| { 926 const lb = self.listBox(index); 927 const at_top = if (lb.getFocus().child_id) |cid| lb.children.getIndex(cid) == 0 else true; 928 if (delta < 0 and at_top) return self.focusHeader(root_focus); 929 ui.moveRowFocus(lb, self.listScroll(index), root_focus, delta); 930 return; 931 } 932 switch (key) { 933 .enter, .arrow_right => try self.focusDetail(index, root_focus), 934 else => {}, 935 } 936 } 937 938 fn detailInput(self: *View, allocator: std.mem.Allocator, index: usize, key: Key, root_focus: *Focus) !void { 939 if (self.toolRowFocused(index)) { 940 try self.toolRowInput(allocator, index, key, root_focus); 941 } else if (self.titleFocused(index)) { 942 try self.titleInput(index, key, root_focus); 943 } else if (self.tagsFocused(index)) { 944 try self.tagsInput(index, key, root_focus); 945 } else { 946 try self.descriptionInput(index, key, root_focus); 947 } 948 } 949 950 // the tool row: enter or a click on the open/close button flips the 951 // issue's status; the edit button is an a: link the host follows. 952 // arrows cross between the buttons and to the neighboring widgets. 953 fn toolRowInput(self: *View, allocator: std.mem.Allocator, index: usize, key: Key, root_focus: *Focus) !void { 954 const row = self.toolRow(index); 955 const on_edit = if (row.getFocus().child_id) |cid| row.children.getIndex(cid) == edit_in_row_index else false; 956 switch (key) { 957 .arrow_left => if (on_edit) root_focus.setFocus(row.children.keys()[button_in_row_index]) else try self.focusList(index, root_focus), 958 .arrow_right => if (!on_edit) root_focus.setFocus(row.children.keys()[edit_in_row_index]), 959 .arrow_up => self.focusHeader(root_focus), 960 .arrow_down => self.focusTitle(index, root_focus), 961 .enter => if (!on_edit) try self.toggleIssueStatus(allocator, index), 962 .mouse => |mouse| switch (mouse.action) { 963 .scroll => |dir| { 964 const sc = self.detailScroll(index); 965 sc.y += if (dir == .up) @as(isize, -1) else 1; 966 sc.clampToContent(); 967 }, 968 else => if (self.statusButton(index)) |button| { 969 if (inp.leftClickOn(root_focus, button.getFocus().id, mouse)) try self.toggleIssueStatus(allocator, index); 970 }, 971 }, 972 else => {}, 973 } 974 } 975 976 fn titleInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 977 const sc = self.detailScroll(index); 978 switch (key) { 979 .arrow_left => try self.focusList(index, root_focus), 980 .arrow_up => self.focusToolRow(index, root_focus), 981 .arrow_down => if (self.tagFlow(index) != null) try self.focusTags(index, root_focus) else try self.focusDescription(index, root_focus), 982 .mouse => |mouse| switch (mouse.action) { 983 .scroll => |dir| { 984 sc.y += if (dir == .up) @as(isize, -1) else 1; 985 sc.clampToContent(); 986 }, 987 else => {}, 988 }, 989 else => {}, 990 } 991 } 992 993 fn descriptionInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 994 const sc = self.detailScroll(index); 995 switch (key) { 996 .arrow_left => return self.focusList(index, root_focus), 997 // once the scroll can't move further, cross into the tags (or the 998 // open/close button when the issue has none). 999 .arrow_up => { 1000 const before = sc.y; 1001 sc.y -= 1; 1002 sc.clampToContent(); 1003 if (sc.y == before) { 1004 if (self.tagFlow(index) != null) try self.focusTags(index, root_focus) else self.focusTitle(index, root_focus); 1005 } 1006 return; 1007 }, 1008 .arrow_down => sc.y += 1, 1009 .page_up => sc.y -= 10, 1010 .page_down => sc.y += 10, 1011 .home => sc.y = 0, 1012 .end => sc.y = std.math.maxInt(isize), 1013 .mouse => |mouse| switch (mouse.action) { 1014 .scroll => |dir| sc.y += if (dir == .up) @as(isize, -1) else 1, 1015 else => {}, 1016 }, 1017 else => return, 1018 } 1019 sc.clampToContent(); 1020 } 1021 1022 // up/down (and tab/shift+tab) move between a form's fields; up from the 1023 // title crosses into the header tabs. the multiline description keeps 1024 // enter and any up/down that has a row to move to. 1025 fn formInput(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1026 const form = self.issueForm(); 1027 const cid = form.getFocus().child_id orelse return; 1028 const cur = form.children.getIndex(cid) orelse return; 1029 1030 if (cur == description_field_index) { 1031 const child = &form.children.values()[cur]; 1032 const description = &child.widget.text_input; 1033 switch (key) { 1034 .enter => return child.widget.input(allocator, key, root_focus), 1035 .arrow_up => if (!try description.cursorOnFirstRow(allocator)) 1036 return child.widget.input(allocator, key, root_focus), 1037 .arrow_down => if (!try description.cursorOnLastRow(allocator)) 1038 return child.widget.input(allocator, key, root_focus), 1039 else => {}, 1040 } 1041 } 1042 1043 switch (key) { 1044 .arrow_up, .back_tab => if (cur > 0) 1045 root_focus.setFocus(form.children.keys()[cur - 1]) 1046 else 1047 self.focusHeader(root_focus), 1048 .arrow_down, .tab => if (cur < submit_field_index) { 1049 root_focus.setFocus(form.children.keys()[cur + 1]); 1050 }, 1051 .enter => if (cur == submit_field_index) try self.submitForm(allocator), 1052 .mouse => |mouse| if (cur == submit_field_index and inp.leftClickOn(root_focus, cid, mouse)) { 1053 try self.submitForm(allocator); 1054 }, 1055 else => try form.children.values()[cur].widget.input(allocator, key, root_focus), 1056 } 1057 } 1058 1059 // the terminal submit paths: the new form commits a new-issue event, the 1060 // edit form re-emits the selected issue's event with the form's content. 1061 // the web posts the forms to their routes instead. 1062 fn submitForm(self: *View, allocator: std.mem.Allocator) !void { 1063 if (self.data.view == .edit) { 1064 try self.submitEditedIssue(allocator); 1065 } else { 1066 try self.submitNewIssue(allocator); 1067 } 1068 } 1069 1070 // commit the new issue to the repo's events branch and navigate to it. 1071 // this is the terminal path; the web posts the form to the issue route, 1072 // so the wasm side never runs (or compiles) the repo access below. 1073 fn submitNewIssue(self: *View, allocator: std.mem.Allocator) !void { 1074 if (comptime wasm) return; 1075 const io = self.session.io orelse return; 1076 const src = self.data.repo_source orelse return; 1077 1078 const form = self.issueForm(); 1079 const title_input = &form.children.values()[title_field_index].widget.text_input; 1080 const tags_input = &form.children.values()[tags_field_index].widget.text_input; 1081 const description_input = &form.children.values()[description_field_index].widget.text_input; 1082 1083 const title = try title_input.text(allocator); 1084 defer allocator.free(title); 1085 const tags = try tags_input.text(allocator); 1086 defer allocator.free(tags); 1087 const description = try description_input.text(allocator); 1088 defer allocator.free(description); 1089 1090 if (!evt.Issue.fieldsValid(title, tags)) return; 1091 1092 var id_bytes: [evt.event_id_size]u8 = undefined; 1093 io.random(&id_bytes); 1094 const event_id_hex = std.fmt.bytesToHex(id_bytes, .lower); 1095 1096 const event = evt.EventWithId{ 1097 .id = event_id_hex, 1098 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 1099 .event = .{ .issue = .{ 1100 .title = title, 1101 .description = description, 1102 .tags = tags, 1103 } }, 1104 }; 1105 1106 switch (src.repo_kind) { 1107 inline else => |repo_kind| { 1108 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, .{ .path = src.path }); 1109 defer any_repo.deinit(io, allocator); 1110 switch (any_repo) { 1111 inline else => |*repo| try evt.commitAndConsume(repo_kind, repo.self_repo_opts, io, allocator, repo, evt.events_ref, &.{event}), 1112 } 1113 }, 1114 } 1115 1116 // wipe the form so a return visit starts fresh 1117 title_input.clear(allocator); 1118 tags_input.clear(allocator); 1119 description_input.clear(allocator); 1120 1121 const route = ui.RoutablePage.repoIssuesRoute(self.data.identity, .open, "", &event_id_hex) orelse return; 1122 try self.session.navigate(route); 1123 } 1124 1125 // re-emit the selected issue's event with the form's content (status 1126 // preserved), then reload the issue's page. this is the terminal path; 1127 // the web posts the form to the edit route. 1128 fn submitEditedIssue(self: *View, allocator: std.mem.Allocator) !void { 1129 if (comptime wasm) return; 1130 const io = self.session.io orelse return; 1131 const src = self.data.repo_source orelse return; 1132 const entry = self.data.selectedIssue() orelse return; 1133 1134 const form = self.issueForm(); 1135 const title_input = &form.children.values()[title_field_index].widget.text_input; 1136 const tags_input = &form.children.values()[tags_field_index].widget.text_input; 1137 const description_input = &form.children.values()[description_field_index].widget.text_input; 1138 1139 const title = try title_input.text(allocator); 1140 defer allocator.free(title); 1141 const tags = try tags_input.text(allocator); 1142 defer allocator.free(tags); 1143 const description = try description_input.text(allocator); 1144 defer allocator.free(description); 1145 1146 if (!evt.Issue.fieldsValid(title, tags)) return; 1147 1148 var id_bytes: [evt.event_id_size]u8 = undefined; 1149 _ = try std.fmt.hexToBytes(&id_bytes, entry.id[0 .. evt.event_id_size * 2]); 1150 1151 switch (src.repo_kind) { 1152 inline else => |repo_kind| { 1153 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, .{ .path = src.path }); 1154 defer any_repo.deinit(io, allocator); 1155 switch (any_repo) { 1156 inline else => |*repo| try evt.Issue.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 1157 .title = title, 1158 .tags = tags, 1159 .description = description, 1160 } }), 1161 } 1162 }, 1163 } 1164 1165 const route = ui.RoutablePage.repoIssuesRoute(self.data.identity, entry.issue.status, "", entry.id) orelse return; 1166 try self.session.navigate(route); 1167 } 1168 1169 // flip the shown issue's status by re-emitting its event, then reload the 1170 // page rooted at the issue so the view reflects the change. this is the 1171 // terminal path; the web posts the button's form to the status route. 1172 fn toggleIssueStatus(self: *View, allocator: std.mem.Allocator, index: usize) !void { 1173 if (comptime wasm) return; 1174 const io = self.session.io orelse return; 1175 const src = self.data.repo_source orelse return; 1176 const sel = self.detailed_index[index] orelse return; 1177 const entry = self.window(index).issues[sel]; 1178 1179 const status: evt.Issue.Status = switch (entry.issue.status) { 1180 .open => .closed, 1181 .closed => .open, 1182 }; 1183 1184 var id_bytes: [evt.event_id_size]u8 = undefined; 1185 _ = try std.fmt.hexToBytes(&id_bytes, entry.id[0 .. evt.event_id_size * 2]); 1186 1187 switch (src.repo_kind) { 1188 inline else => |repo_kind| { 1189 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, .{ .path = src.path }); 1190 defer any_repo.deinit(io, allocator); 1191 switch (any_repo) { 1192 inline else => |*repo| try evt.Issue.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .status = status }), 1193 } 1194 }, 1195 } 1196 1197 const route = ui.RoutablePage.repoIssuesRoute(self.data.identity, status, "", entry.id) orelse return; 1198 try self.session.navigate(route); 1199 } 1200 1201 // arrow keys move the tag selection; at the flow's edges focus crosses to 1202 // the neighboring widgets. 1203 fn tagsInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 1204 const tf = self.tagFlow(index) orelse return; 1205 const cid = tf.focus.child_id orelse return; 1206 const cur = tf.indexOfFocusId(cid) orelse return; 1207 const count = tf.text_boxes.items.len; 1208 const sc = self.detailScroll(index); 1209 switch (key) { 1210 .arrow_left => if (cur > 0) self.focusTag(index, tf, root_focus, cur - 1) else try self.focusList(index, root_focus), 1211 .arrow_right => if (cur + 1 < count) self.focusTag(index, tf, root_focus, cur + 1), 1212 .arrow_up => if (tf.rowStep(cur, false)) |i| self.focusTag(index, tf, root_focus, i) else self.focusTitle(index, root_focus), 1213 .arrow_down => if (tf.rowStep(cur, true)) |i| self.focusTag(index, tf, root_focus, i) else try self.focusDescription(index, root_focus), 1214 .home => self.focusTag(index, tf, root_focus, 0), 1215 .end => self.focusTag(index, tf, root_focus, count - 1), 1216 .mouse => |mouse| switch (mouse.action) { 1217 .scroll => |dir| { 1218 sc.y += if (dir == .up) @as(isize, -1) else 1; 1219 sc.clampToContent(); 1220 }, 1221 else => {}, 1222 }, 1223 else => {}, 1224 } 1225 } 1226 1227 const button_in_row_index: usize = 1; 1228 const edit_in_row_index: usize = 2; 1229 const title_child_index: usize = 0; 1230 const tags_child_index: usize = 1; 1231 1232 fn tagFlow(self: *View, index: usize) ?*ui.TagFlow { 1233 const inner = self.detailInner(index); 1234 if (inner.children.count() <= tags_child_index) return null; 1235 return switch (inner.children.values()[tags_child_index].widget) { 1236 .tag_flow => |*tf| tf, 1237 else => null, 1238 }; 1239 } 1240 1241 fn tagsFocused(self: *View, index: usize) bool { 1242 const inner = self.detailInner(index); 1243 const cid = inner.getFocus().child_id orelse return false; 1244 return inner.children.getIndex(cid) == tags_child_index and self.tagFlow(index) != null; 1245 } 1246 1247 // the open/close button inside the detail frame's tool row. 1248 fn statusButton(self: *View, index: usize) ?*wgt.TextBox(ui.Widget) { 1249 const row = self.toolRow(index); 1250 if (row.children.count() == 0) return null; 1251 return &row.children.values()[button_in_row_index].widget.text_box; 1252 } 1253 1254 fn toolRowFocused(self: *View, index: usize) bool { 1255 const outer = self.detailOuter(index); 1256 const cid = outer.getFocus().child_id orelse return false; 1257 return outer.children.getIndex(cid) == tool_row_index; 1258 } 1259 1260 fn focusTag(self: *View, index: usize, tf: *ui.TagFlow, root_focus: *Focus, item: usize) void { 1261 root_focus.setFocus(tf.text_boxes.items[item].getFocus().id); 1262 // keep the tag visible on the terminal: its rect offset by the flow's 1263 // position in the pane. 1264 if (self.session.is_terminal and item < tf.rects.items.len) { 1265 if (self.detailInner(index).children.values()[tags_child_index].rect) |flow_rect| { 1266 var rect = tf.rects.items[item]; 1267 rect.x += flow_rect.x; 1268 rect.y += flow_rect.y; 1269 self.detailScroll(index).scrollToRect(rect); 1270 } 1271 } 1272 } 1273 1274 fn titleFocused(self: *View, index: usize) bool { 1275 const inner = self.detailInner(index); 1276 const cid = inner.getFocus().child_id orelse return false; 1277 return inner.children.getIndex(cid) == title_child_index; 1278 } 1279 1280 // the title sits at the top of the pane, so focusing it scrolls there. 1281 fn focusTitle(self: *View, index: usize, root_focus: *Focus) void { 1282 const inner = self.detailInner(index); 1283 if (inner.children.count() == 0) return; 1284 root_focus.setFocus(inner.children.keys()[title_child_index]); 1285 self.detailScroll(index).y = 0; 1286 } 1287 1288 fn focusTags(self: *View, index: usize, root_focus: *Focus) !void { 1289 const tf = self.tagFlow(index) orelse return; 1290 if (tf.text_boxes.items.len == 0) return; 1291 const cid = tf.focus.child_id orelse tf.text_boxes.items[0].getFocus().id; 1292 const item = tf.indexOfFocusId(cid) orelse 0; 1293 self.focusTag(index, tf, root_focus, item); 1294 } 1295 1296 fn focusDescription(self: *View, index: usize, root_focus: *Focus) !void { 1297 if (self.description_id[index]) |id| root_focus.setFocus(id); 1298 } 1299 1300 // return to the tool row's last-focused button. 1301 fn focusToolRow(self: *View, index: usize, root_focus: *Focus) void { 1302 const cid = self.toolRow(index).getFocus().child_id orelse return; 1303 root_focus.setFocus(cid); 1304 } 1305 1306 // enter the detail pane. an empty pane (no issues) can't be entered. 1307 fn focusDetail(self: *View, index: usize, root_focus: *Focus) !void { 1308 if (self.detailInner(index).children.count() == 0) return; 1309 root_focus.setFocus(self.detailOuter(index).getFocus().id); 1310 } 1311 1312 // return to the list. 1313 fn focusList(self: *View, index: usize, root_focus: *Focus) !void { 1314 root_focus.setFocus(self.listScroll(index).getFocus().id); 1315 } 1316 1317 // cross to the header tabs above the stack. 1318 fn focusHeader(self: *View, root_focus: *Focus) void { 1319 root_focus.setFocus(self.box.children.keys()[header_index]); 1320 } 1321 1322 pub fn clearGrid(self: *View) void { 1323 self.box.clearGrid(); 1324 } 1325 1326 pub fn getGrid(self: View) ?Grid { 1327 return self.box.getGrid(); 1328 } 1329 1330 pub fn getFocus(self: *View) *Focus { 1331 return self.box.getFocus(); 1332 } 1333 1334 // for the parent's "scroll up at the top jumps to the header" check: at the 1335 // top only while the header tabs hold focus, so up from the split 1336 // crosses into the tabs first. 1337 pub fn getSelectedIndex(self: *View) ?usize { 1338 return if (self.headerActive()) 0 else 1; 1339 } 1340 }; 1341 1342 // the "a:" navigation link for `status`'s issues page filtered to the 1343 // url-encoded `tag` and rooted at issue `id` within `identity` ("owner/name"). 1344 // a rooted url ignores `status` (it derives its view from the issue's own); 1345 // with an empty `id` this is the bare list link. 1346 fn issuesLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, status: evt.Issue.Status, tag: []const u8, id: []const u8) ![]const u8 { 1347 const route = ui.RoutablePage.repoIssuesRoute(identity, status, tag, id) orelse return error.RouteTooLong; 1348 const url = try route.toUrl(page_arena); 1349 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 1350 } 1351 1352 // the in-page "ai:" anchor for selecting issue `id` in `identity`'s list; the 1353 // href is only followed with js off. 1354 fn issueRowLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, id: []const u8) ![]const u8 { 1355 const route = ui.RoutablePage.repoIssuesRoute(identity, .open, "", id) orelse return error.RouteTooLong; 1356 const url = try route.toUrl(page_arena); 1357 return std.fmt.allocPrint(page_arena.allocator(), "ai:{s}", .{url}); 1358 } 1359 1360 // the "a:" link to `status`'s issues list filtered to `tag` (raw; encoded here). 1361 fn tagLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, status: evt.Issue.Status, tag: []const u8) ![]const u8 { 1362 const encoded = try ui.urlEncodeRef(page_arena.allocator(), tag); 1363 const route = ui.RoutablePage.repoIssuesRoute(identity, status, encoded, "") orelse return error.RouteTooLong; 1364 const url = try route.toUrl(page_arena); 1365 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 1366 } 1367 1368 // tabs switching between the issues page's views. 1369 pub const Header = struct { 1370 box: wgt.Box(ui.Widget), 1371 tab_ids: std.AutoArrayHashMapUnmanaged(usize, void), 1372 1373 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self) !Header { 1374 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 1375 errdefer box.deinit(allocator); 1376 1377 var tab_ids: std.AutoArrayHashMapUnmanaged(usize, void) = .empty; 1378 errdefer tab_ids.deinit(allocator); 1379 1380 const aa = session.page_arena.allocator(); 1381 1382 // a list tab per status, labeled with its listing's issue count 1383 for ([_]evt.Issue.Status{ .open, .closed }) |status| { 1384 const route = ui.RoutablePage.repoIssuesRoute(data.identity, status, data.tag, "") orelse return error.RouteTooLong; 1385 const link = try std.fmt.allocPrint(aa, "ai:{s}", .{try route.toUrl(session.page_arena)}); 1386 const label = try std.fmt.allocPrint(aa, "{s} ({d})", .{ @tagName(status), data.window(status).count }); 1387 var text_box = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1388 errdefer text_box.deinit(allocator); 1389 text_box.getFocus().focusable = true; 1390 text_box.getFocus().kind = .{ .custom = link }; 1391 try tab_ids.put(allocator, text_box.getFocus().id, {}); 1392 try box.children.put(allocator, text_box.getFocus().id, .{ 1393 .widget = .{ .text_box = text_box }, 1394 .rect = null, 1395 .min_size = .{ .width = label.len + 2, .height = null }, 1396 }); 1397 } 1398 1399 // tags tab, labeled with the active tag filter 1400 { 1401 const tags_route = ui.RoutablePage.repoIssuesTagsRoute(data.identity, data.tag) orelse return error.RouteTooLong; 1402 const tags_link = try std.fmt.allocPrint(aa, "ai:{s}", .{try tags_route.toUrl(session.page_arena)}); 1403 const label = if (data.tag.len == 0) "tags" else blk: { 1404 const decoded = std.Uri.percentDecodeInPlace(try aa.dupe(u8, data.tag)); 1405 break :blk try std.fmt.allocPrint(aa, "tags ({s})", .{decoded}); 1406 }; 1407 var text_box = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1408 errdefer text_box.deinit(allocator); 1409 text_box.getFocus().focusable = true; 1410 text_box.getFocus().kind = .{ .custom = tags_link }; 1411 try tab_ids.put(allocator, text_box.getFocus().id, {}); 1412 try box.children.put(allocator, text_box.getFocus().id, .{ 1413 .widget = .{ .text_box = text_box }, 1414 .rect = null, 1415 .min_size = .{ .width = label.len + 2, .height = null }, 1416 }); 1417 } 1418 1419 // new-issue tab; an edit url shows the edit tab in its place 1420 { 1421 const route = if (data.view == .edit) 1422 ui.RoutablePage.repoIssuesEditRoute(data.identity, data.selected_id) orelse return error.RouteTooLong 1423 else 1424 ui.RoutablePage.repoIssuesNewRoute(data.identity) orelse return error.RouteTooLong; 1425 const link = try std.fmt.allocPrint(aa, "ai:{s}", .{try route.toUrl(session.page_arena)}); 1426 const label: []const u8 = if (data.view == .edit) "edit" else "new"; 1427 var text_box = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1428 errdefer text_box.deinit(allocator); 1429 text_box.getFocus().focusable = true; 1430 text_box.getFocus().kind = .{ .custom = link }; 1431 try tab_ids.put(allocator, text_box.getFocus().id, {}); 1432 try box.children.put(allocator, text_box.getFocus().id, .{ 1433 .widget = .{ .text_box = text_box }, 1434 .rect = null, 1435 .min_size = .{ .width = label.len + 2, .height = null }, 1436 }); 1437 } 1438 1439 var self = Header{ .box = box, .tab_ids = tab_ids }; 1440 // the tab matching the page's view is selected initially. 1441 self.getFocus().child_id = self.tab_ids.keys()[View.viewIndex(data.view)]; 1442 return self; 1443 } 1444 1445 pub fn deinit(self: *Header, allocator: std.mem.Allocator) void { 1446 self.box.deinit(allocator); 1447 self.tab_ids.deinit(allocator); 1448 } 1449 1450 pub fn build(self: *Header, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1451 self.clearGrid(); 1452 // only the selected tab shows its border 1453 for (self.box.children.keys(), self.box.children.values()) |id, *child| { 1454 switch (child.widget) { 1455 .text_box => |*tb| tb.options.border_style = if (self.getFocus().child_id == id) .single else .hidden, 1456 else => {}, 1457 } 1458 } 1459 try self.box.build(allocator, constraint, root_focus); 1460 } 1461 1462 pub fn input(self: *Header, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1463 _ = allocator; 1464 const current_tab = self.currentTabIndex() orelse return; 1465 if (inp.moveTab(key, current_tab, self.tab_ids.count())) |new_tab| { 1466 root_focus.setFocus(self.tab_ids.keys()[new_tab]); 1467 } 1468 } 1469 1470 pub fn clearGrid(self: *Header) void { 1471 self.box.clearGrid(); 1472 } 1473 1474 pub fn getGrid(self: Header) ?Grid { 1475 return self.box.getGrid(); 1476 } 1477 1478 pub fn getFocus(self: *Header) *Focus { 1479 return self.box.getFocus(); 1480 } 1481 1482 pub fn getSelectedIndex(self: Header) ?usize { 1483 return self.currentTabIndex(); 1484 } 1485 1486 fn currentTabIndex(self: Header) ?usize { 1487 const child_id = self.box.focus.child_id orelse return null; 1488 return self.tab_ids.getIndex(child_id); 1489 } 1490 }; 1491