╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ User.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
354 box: wgt.Box(ui.Widget), 355 data: *const Self, 356 session: *ui.Session, 357 358 const header_index = 0; 359 const list_index = 1; 360 361 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !ReposView { 362 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 363 errdefer box.deinit(allocator); 364 365 { 366 var header = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 367 errdefer header.deinit(allocator); 368 { 369 var search_box = try ui.widget.SearchBox.init(allocator, session, " search ", "search", data.repos_search); 370 errdefer search_box.deinit(allocator); 371 header.getFocus().child_id = search_box.getFocus().id; 372 try header.children.put(allocator, search_box.getFocus().id, .{ .widget = .{ .search_box = search_box }, .rect = null, .min_size = ui.widget.SearchBox.min_size }); 373 } 374 // the spacer keeps the box at its own width 375 { 376 var spacer = try ui.widget.Spacer.init(allocator); 377 errdefer spacer.deinit(allocator); 378 try header.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 379 } 380 try box.children.put(allocator, header.getFocus().id, .{ .widget = .{ .box = header }, .rect = null, .min_size = .{ .width = null, .height = ui.widget.SearchBox.min_size.height } }); 381 } 382 383 { 384 var list = try ui.widget.FlowBox.Scroll.init(allocator, .{}, !session.is_terminal); 385 errdefer list.deinit(allocator); 386 387 var arena = std.heap.ArenaAllocator.init(allocator); 388 defer arena.deinit(); 389 const aa = arena.allocator(); 390 391 const route = (reposRoute(data.user.name) orelse return error.RouteTooLong).withSearch(data.repos_search orelse "") orelse return error.RouteTooLong; 392 393 // a leading "previous" row off the first window, one row per repo, 394 // then a trailing "next" row when more remain. each window row 395 // navigates to the adjacent window of this user's repos. 396 var items: std.ArrayList(ui.widget.FlowBox.Item) = .empty; 397 if (data.repos_start > 0) { 398 var prev = route; 399 prev.user_repos.start = data.repos_start -| page_size; 400 try items.append(aa, .{ .text = "← previous", .link = try std.fmt.allocPrint(aa, "a:{s}", .{try prev.toUrl(&arena)}) }); 401 } 402 for (data.repos) |repo| 403 // clicking a repo opens its page; the "a:" prefix makes the web 404 // renderer emit an <a href="/alice:foo"> anchor. 405 try items.append(aa, .{ 406 .text = if (repo.event.description.len == 0) repo.event.name else try std.fmt.allocPrint(aa, "{s} - {s}", .{ repo.event.name, repo.event.description }), 407 .link = try std.fmt.allocPrint(aa, "a:/{s}:{s}", .{ data.user.name, repo.event.name }), 408 .bottom_label = if (repo.event.read_access == .private) "(private)" else "", 409 }); 410 if (data.repos_next_start) |next_start| { 411 var next = route; 412 next.user_repos.start = next_start; 413 try items.append(aa, .{ .text = "next →", .link = try std.fmt.allocPrint(aa, "a:{s}", .{try next.toUrl(&arena)}) }); 414 } 415 try list.setItems(allocator, items.items); 416 417 try box.children.put(allocator, list.getFocus().id, .{ .widget = .{ .flow_box_scroll = list }, .rect = null, .min_size = null }); 418 } 419 420 // search results start in the box so the term can be refined right away 421 box.getFocus().child_id = box.children.keys()[if (data.repos_search != null) header_index else list_index]; 422 return .{ .box = box, .data = data, .session = session }; 423 } 424 425 pub fn deinit(self: *ReposView, allocator: std.mem.Allocator) void { 426 self.box.deinit(allocator); 427 } 428 429 pub fn build(self: *ReposView, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 430 self.clearGrid(); 431 // clear the incoming min height so the list scrolls within what's left 432 try self.box.build(allocator, .{ 433 .min_size = .{ .width = null, .height = null }, 434 .max_size = constraint.max_size, 435 }, root_focus); 436 } 437 438 pub fn input(self: *ReposView, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 439 const direction = inp.vertDirection(key); 440 if (self.headerActive()) { 441 const search_box = self.searchBox(); 442 if (direction == .down) return root_focus.setFocus(self.listScroll().getFocus().id); 443 if (key == .enter) { 444 const text = try search_box.text(allocator); 445 defer allocator.free(text); 446 return self.submit(text); 447 } 448 return search_box.input(allocator, key, root_focus); 449 } 450 // up from the first row reaches the search box 451 if (direction == .up and self.listScroll().atTop()) return self.focusHeader(root_focus); 452 try self.listScroll().input(allocator, key, root_focus); 453 } 454 455 // the repos narrowed to `text` from the first window. an empty box on an 456 // unsearched page has nothing to clear. 457 fn submit(self: *ReposView, text: []const u8) !void { 458 if (text.len == 0 and self.data.repos_search == null) return; 459 const route = reposRoute(self.data.user.name) orelse return; 460 try self.session.navigate(route.withSearch(text) orelse return); 461 } 462 463 fn searchBox(self: *ReposView) *ui.widget.SearchBox { 464 return &self.box.children.values()[header_index].widget.box.children.values()[0].widget.search_box; 465 } 466 467 fn listScroll(self: *ReposView) *ui.widget.FlowBox.Scroll { 468 return &self.box.children.values()[list_index].widget.flow_box_scroll; 469 } 470 471 fn headerActive(self: *ReposView) bool { 472 return self.box.getFocus().child_id == self.box.children.keys()[header_index]; 473 } 474 475 pub fn clearGrid(self: *ReposView) void { 476 self.box.clearGrid(); 477 } 478 479 pub fn getGrid(self: ReposView) ?Grid { 480 return self.box.getGrid(); 481 } 482 483 pub fn getFocus(self: *ReposView) *Focus { 484 return self.box.getFocus(); 485 } 486 487 // up leaves the tab from the search box 488 pub fn atTop(self: *ReposView) bool { 489 return self.headerActive(); 490 } 491 492 pub fn focusHeader(self: *ReposView, root_focus: *Focus) void { 493 root_focus.setFocus(self.searchBox().getFocus().id); 494 } 495 }; 496 497 // the first window of `name`'s repos 498 fn reposRoute(name: []const u8) ?ui.RoutablePage { 499 return .{ .user_repos = .{ .name = ui.RoutablePage.Array(evt.User.name_max_len).from(name) orelse return null } }; 500 } 501