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