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