╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Repo.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
367 try Events.init(repo_kind, opened.self_repo_opts, arena, opened, io, session.haxy_moment, repo_identity.identity, repo.event, events_view, events_kind, events_selected, events_moment, session.local != null, session.data.sync_failure), 368 }; 369 }, 370 } 371 }, 372 } 373 } 374 const aa = arena.allocator(); 375 break :blk .{ 376 try Files.emptyResult(aa, handle, requested_ref_or_oid orelse .branch, requested_ref_value, files_dir), 377 Changes{ .commits = try Commits.emptyResult(aa, handle, requested_ref_or_oid orelse .branch, requested_ref_value, commits_base_oid) }, 378 try Refs.emptyResult(arena, repo_identity.identity, refs_kind, refs_from, refs_search), 379 try Discussions.emptyResult(aa, repo_identity.identity, discussions_label, discussions_search, discussions_selected, discussions_comment, discussions_comments_start, discussions_view), 380 try Issues.emptyResult(aa, repo_identity.identity, issues_label, issues_assignee, issues_search, issues_selected, issues_comment, issues_comments_start, issues_theirs, issues_view), 381 try Patches.emptyResult(aa, repo_identity.identity, patches_label, patches_search, patches_selected, patches_comment, patches_comments_start, patches_theirs, patches_view), 382 try Events.empty(aa, repo_identity.identity, events_view, session.local != null, session.data.sync_failure), 383 }; 384 }; 385 if (route == .repo_undo and undo_data == null) return error.NotFound; 386 if ((route == .repo_settings or route == .repo_roles) and settings == null) return error.NotFound; 387 if (undo_data) |*undo| undo.clear = undo_clear; 388 discussions.repo_source = source; 389 issues.repo_source = source; 390 patches.repo_source = source; 391 patches.repo_id = repo_id_maybe; 392 393 return .{ 394 // use the files tab's resolved ref for the header 395 .header = try Header.init(arena, repo.event.name, owner_name, files.ref_or_oid, files.ref_or_oid_value, discussions.label, issues.label, patches.label), 396 .repo = repo, 397 .handle = handle, 398 .files = files, 399 .changes = changes, 400 .refs = refs, 401 .discussions = discussions, 402 .issues = issues, 403 .patches = patches, 404 .events = events, 405 .undo = undo_data, 406 .settings = settings, 407 .quit = Quit.init(), 408 }; 409 } 410 411 // the viewer a tab's comment bar sees, which is none below the tab's role 412 fn tabViewer(handle: ui.RepoHandle, repo: evt.Repo.Record, kind: evt.EventKind) ?ui.Viewer { 413 const viewer = handle.viewer orelse return null; 414 const tab_role = repo.event.threadRole(kind) orelse return null; 415 return if (viewer.role.atLeast(tab_role)) viewer else null; 416 } 417 418 pub const View = struct { 419 box: wgt.Box(ui.Widget), 420 421 const header_index: usize = 0; 422 const stack_index: usize = 1; 423 424 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { 425 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border = null, .round_corners = true, .direction = .vert }); 426 errdefer box.deinit(allocator); 427 428 // build the header first so we can grab the files-tab id for the user 429 // view (it focuses there after login). 430 { 431 var header_view = try Header.View.init(allocator, data, session); 432 errdefer header_view.deinit(allocator); 433 try box.children.put(allocator, header_view.getFocus().id, .{ .widget = .{ .repo_header = header_view }, .rect = null, .min_size = null }); 434 } 435 436 { 437 var stack = try wgt.Stack(ui.Widget).init(allocator); 438 errdefer stack.deinit(allocator); 439 440 // files — the default tab: the current directory's listing. 441 { 442 var files_view = try Files.View.init(allocator, &data.files, session); 443 errdefer files_view.deinit(allocator); 444 try stack.children.put(allocator, files_view.getFocus().id, .{ .repo_files = files_view }); 445 } 446 447 switch (data.changes) { 448 .diff => |*diff| { 449 var diff_view = try Diff.View.init(allocator, diff, session); 450 errdefer diff_view.deinit(allocator); 451 try stack.children.put(allocator, diff_view.getFocus().id, .{ .diff_view = diff_view }); 452 }, 453 .commits => |*commits| { 454 var commits_view = try Commits.View.init(allocator, commits, session); 455 errdefer commits_view.deinit(allocator); 456 try stack.children.put(allocator, commits_view.getFocus().id, .{ .repo_commits = commits_view }); 457 }, 458 } 459 460 // refs — the repo's branches and tags. 461 { 462 var refs_view = try Refs.View.init(allocator, &data.refs, session); 463 errdefer refs_view.deinit(allocator); 464 try stack.children.put(allocator, refs_view.getFocus().id, .{ .repo_refs = refs_view }); 465 } 466 467 // discussions and their comment permalinks. 468 if (data.repo.event.discuss_role != null) { 469 var discussions_view = try Discussions.View.init(allocator, &data.discussions, session); 470 errdefer discussions_view.deinit(allocator); 471 try stack.children.put(allocator, discussions_view.getFocus().id, .{ .repo_discussions = discussions_view }); 472 } 473 474 // issues — the repo's issue tracker and comment permalinks. 475 if (data.repo.event.issue_role != null) { 476 var issues_view = try Issues.View.init(allocator, &data.issues, session); 477 errdefer issues_view.deinit(allocator); 478 try stack.children.put(allocator, issues_view.getFocus().id, .{ .repo_issues = issues_view }); 479 } 480 481 // patches and their comment permalinks. 482 if (data.repo.event.patch_role != null) { 483 var patches_view = try Patches.View.init(allocator, &data.patches, session); 484 errdefer patches_view.deinit(allocator); 485 try stack.children.put(allocator, patches_view.getFocus().id, .{ .repo_patches = patches_view }); 486 } 487 488 if (data.handle.canWrite()) { 489 var events_view = try Events.View.init(allocator, &data.events, session); 490 errdefer events_view.deinit(allocator); 491 try stack.children.put(allocator, events_view.getFocus().id, .{ .repo_events = events_view }); 492 } 493 494 if (data.undo) |*undo| { 495 var undo_view = try Undo.View.init(allocator, undo, session); 496 errdefer undo_view.deinit(allocator); 497 try stack.children.put(allocator, undo_view.getFocus().id, .{ .repo_undo = undo_view }); 498 } 499 500 if (data.settings) |*settings| { 501 var settings_view = try Settings.View.init(allocator, settings, session); 502 errdefer settings_view.deinit(allocator); 503 try stack.children.put(allocator, settings_view.getFocus().id, .{ .repo_settings = settings_view }); 504 } 505 506 // the header shows new repo with a login, and new user without one 507 // and user only outside local mode, so keep the stack's children 508 // 1:1 with the tabs by skipping the same views. 509 const identity = try session.page_arena.allocator().print("{s}:{s}", .{ data.header.owner_name, data.header.name }); 510 if (session.data.user_id != null) { 511 const route = ui.RoutablePage{ .repo_repo_new = ui.RoutablePage.Array(ui.RoutablePage.repo_route_max_len).from(identity) orelse return error.RouteTooLong }; 512 var new_repo_view = try ui.NewRepo.View.init(allocator, session, route); 513 errdefer new_repo_view.deinit(allocator); 514 try stack.children.put(allocator, new_repo_view.getFocus().id, .{ .new_repo = new_repo_view }); 515 } else if (session.data.host_kind == .server) { 516 const route = ui.RoutablePage{ .repo_user_new = ui.RoutablePage.Array(ui.RoutablePage.repo_route_max_len).from(identity) orelse return error.RouteTooLong }; 517 var new_user_view = try ui.NewUser.View.init(allocator, session, route); 518 errdefer new_user_view.deinit(allocator); 519 try stack.children.put(allocator, new_user_view.getFocus().id, .{ .new_user = new_user_view }); 520 } 521 522 if (session.data.host_kind == .server) { 523 var user_view = try ui.UserSettings.initView(allocator, session); 524 errdefer user_view.deinit(allocator); 525 try stack.children.put(allocator, user_view.getFocus().id, user_view); 526 } 527 528 if (session.is_terminal) { 529 var quit_view = try Quit.View.init(allocator, session); 530 errdefer quit_view.deinit(allocator); 531 try stack.children.put(allocator, quit_view.getFocus().id, .{ .quit = quit_view }); 532 } 533 534 try box.children.put(allocator, stack.getFocus().id, .{ .widget = .{ .stack = stack }, .rect = null, .min_size = null }); 535 } 536 537 var self = View{ .box = box }; 538 // a page opens on its tabs, except that search results open in the 539 // tab's own search box. 540 const results = data.files.find != null or 541 (data.changes == .commits and data.changes.commits.search != null) or 542 data.discussions.search != null or data.issues.search != null or data.patches.search != null; 543 self.getFocus().child_id = box.children.keys()[if (results) stack_index else header_index]; 544 return self; 545 } 546 547 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 548 self.box.deinit(allocator); 549 } 550 551 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 552 self.clearGrid(); 553 const header = &self.box.children.values()[header_index].widget.repo_header; 554 const stack = &self.box.children.values()[stack_index].widget.stack; 555 556 // each header tab maps 1:1 to a stack child by position 557 if (header.getSelectedIndex()) |index| 558 stack.getFocus().child_id = stack.children.keys()[index]; 559 try self.box.build(allocator, constraint, root_focus); 560 } 561 562 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 563 const stack = &self.box.children.values()[stack_index].widget.stack; 564 if (self.getFocus().child_id) |child_id| { 565 if (self.box.children.getIndex(child_id)) |current_index| { 566 const child = &self.box.children.values()[current_index].widget; 567 var index = current_index; 568 569 const direction = inp.vertDirection(key); 570 571 switch (direction) { 572 .up => { 573 switch (child.*) { 574 .repo_header => { 575 try child.input(allocator, key, root_focus); 576 }, 577 .stack => { 578 if (child.stack.getSelected()) |selected_widget| { 579 if (selected_widget.atTop(root_focus)) { 580 index = header_index; 581 } else { 582 try child.input(allocator, key, root_focus); 583 } 584 } 585 }, 586 else => {}, 587 } 588 }, 589 .down => { 590 switch (child.*) { 591 .repo_header => { 592 if (stack.getSelected()) |selected_widget| switch (selected_widget.*) { 593 .repo_files => |*v| if (v.focusHeader(root_focus)) return, 594 .repo_commits => |*v| if (v.focusHeader(root_focus)) return, 595 .repo_refs => |*v| if (v.focusHeader(root_focus)) return, 596 .repo_events => |*v| if (v.focusHeader(root_focus)) return, 597 .repo_undo => |*v| if (v.focusHeader(root_focus)) return, 598 .repo_settings => |*v| if (v.focusHeader(root_focus)) return, 599 else => {}, 600 }; 601 index = stack_index; 602 }, 603 .stack => { 604 try child.input(allocator, key, root_focus); 605 }, 606 else => {}, 607 } 608 }, 609 .none => { 610 try child.input(allocator, key, root_focus); 611 }, 612 } 613 614 if (index != current_index) { 615 root_focus.setFocus(self.box.children.keys()[index]); 616 } 617 } 618 } 619 } 620 621 pub fn clearGrid(self: *View) void { 622 self.box.clearGrid(); 623 } 624 625 pub fn getGrid(self: View) ?Grid { 626 return self.box.getGrid(); 627 } 628 629 pub fn getFocus(self: *View) *Focus { 630 return self.box.getFocus(); 631 } 632 }; 633