╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Repo.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭ find file ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ ╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │ │ │http│ ssh │ │ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
390 issues_data, 391 patches_data, 392 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), 393 }; 394 }, 395 } 396 }, 397 } 398 } 399 const aa = arena.allocator(); 400 break :blk .{ 401 try Files.emptyResult(aa, handle, requested_ref_or_oid orelse .branch, requested_ref_value, files_dir), 402 Changes{ .commits = try Commits.emptyResult(aa, handle, requested_ref_or_oid orelse .branch, requested_ref_value, commits_base_oid) }, 403 try Refs.emptyResult(arena, repo_identity.identity, refs_kind, refs_from, refs_search), 404 try Discussions.emptyResult(aa, repo_identity.identity, discussions_search, discussions_selected, discussions_comment, discussions_comments_start, discussions_view), 405 try Issues.emptyResult(aa, repo_identity.identity, issues_search, issues_selected, issues_comment, issues_comments_start, issues_theirs, issues_view), 406 try Patches.emptyResult(aa, repo_identity.identity, patches_search, patches_selected, patches_comment, patches_comments_start, patches_theirs, patches_view), 407 try Events.empty(aa, repo_identity.identity, events_view, session.local != null, session.data.sync_failure), 408 }; 409 }; 410 if (route == .repo_undo and undo_data == null) return error.NotFound; 411 if ((route == .repo_settings or route == .repo_members or route == .repo_branches or route == .repo_delete) and settings == null) return error.NotFound; 412 if (settings) |value| if (route == .repo_delete and value.delete == null) return error.NotFound; 413 discussions.repo_source = source; 414 issues.repo_source = source; 415 patches.repo_source = source; 416 patches.repo_id = repo_id_maybe; 417 418 return .{ 419 // use the files tab's resolved ref for the header 420 .header = try Header.init(arena, repo.event.name, owner_name, files.ref_or_oid, files.ref_or_oid_value, discussions.search, issues.search, patches.search), 421 .repo = repo, 422 .handle = handle, 423 .files = files, 424 .changes = changes, 425 .refs = refs, 426 .discussions = discussions, 427 .issues = issues, 428 .patches = patches, 429 .events = events, 430 .undo = undo_data, 431 .settings = settings, 432 // after the shown thread's notif is marked read 433 .user_notifications = try ui.UserNotifications.init(arena, session, route.userTab()), 434 .quit = Quit.init(), 435 }; 436 } 437 438 // set the viewer's notify button on every listed thread and mark the viewed thread's notif read 439 fn readNotify( 440 comptime repo_opts: rp.RepoOpts(.xit), 441 session: *ui.Session, 442 arena: *std.heap.ArenaAllocator, 443 repo: *rp.Repo(.xit, repo_opts), 444 owner_id: *const [evt.event_id_size]u8, 445 repo_id: *const [evt.event_id_size]u8, 446 viewed_thread: []const u8, 447 discussions: *Discussions, 448 issues: *Issues, 449 patches: *Patches, 450 ) !void { 451 const user_id = session.userId() orelse return; 452 const io = session.io orelse return error.NoMoment; 453 const users_dir = session.users_dir orelse return error.NoMoment; 454 const gpa = arena.child_allocator; 455 456 { 457 // the owner's plain data, which holds the tier index 458 var owner_repo = (try evt.openUserRepo(io, gpa, users_dir, owner_id)) orelse return error.NotFound; 459 defer owner_repo.deinit(io, gpa); 460 const owner_moment = try notify.latestMoment(&owner_repo); 461 462 try setNotifyActions(arena, &discussions.recent, owner_moment, repo_id, &user_id); 463 for ([_]*Issues.Window{ &issues.open, &issues.closed, &issues.conflicts }) |window| try setNotifyActions(arena, window, owner_moment, repo_id, &user_id); 464 for ([_]*Patches.Window{ &patches.open, &patches.closed, &patches.merged, &patches.conflicts }) |window| try setNotifyActions(arena, window, owner_moment, repo_id, &user_id); 465 } 466 467 if (viewed_thread.len == 0) return; 468 var user_repo = (try evt.openUserRepo(io, gpa, users_dir, &user_id)) orelse return; 469 defer user_repo.deinit(io, gpa); 470 const latest = (try notify.latestMoment(&user_repo)) orelse return; 471 const notif_id = notify.idOf(repo_id, &try evt.parseEventId(viewed_thread)); 472 const item = (try notify.readItem(latest, arena, &notif_id)) orelse return; 473 if (!item.unread) return; 474 // the page reads this moment or a newer one, so a child in it is shown 475 const moment = try evt.currentMoment(repo_opts, repo); 476 const kinds_cursor = try moment.getCursor(hash.hashInt(repo_opts.hash, evt.event_index_key)) orelse return error.CursorNotFound; 477 const kinds = try evt.EventDB(repo_opts.hash).HashMap(.read_only).init(kinds_cursor); 478 // a child the page can't show leaves it unread 479 if (try evt.readEventKind(repo_opts.hash, kinds, &item.notif.last_child_id) == null) return; 480 try notify.markRead(io, gpa, users_dir, &user_id, &notif_id, &item.notif.last_child_id); 481 } 482 483 // set the viewer's notify button on each of a window's threads 484 fn setNotifyActions( 485 arena: *std.heap.ArenaAllocator, 486 window: anytype, 487 owner_moment: ?evt.UserDB.HashMap(.read_only), 488 repo_id: *const [evt.event_id_size]u8, 489 user_id: *const [evt.event_id_size]u8, 490 ) !void { 491 const items = try arena.allocator().dupe(std.meta.Elem(@TypeOf(window.items)), window.items); 492 for (items) |*entry| { 493 const notif_id = notify.idOf(repo_id, &try evt.parseEventId(entry.id)); 494 // muting drops the user from every tier, so membership alone decides 495 const notified = if (owner_moment) |moment| try notify.isNotified(moment, &notif_id, user_id) else false; 496 entry.notify_action = if (notified) .mute else .subscribe; 497 } 498 window.items = items; 499 } 500 501 // the viewer a tab's comment bar sees, which is none below the tab's role 502 fn tabViewer(handle: ui.RepoHandle, repo: evt.Repo.Record, kind: evt.EventKind) ?ui.Viewer { 503 const viewer = handle.viewer orelse return null; 504 const tab_role = repo.event.threadRole(kind) orelse return null; 505 return if (viewer.role.atLeast(tab_role)) viewer else null; 506 } 507 508 pub const View = struct { 509 box: wgt.Box(ui.Widget), 510 511 const header_index: usize = 0; 512 const stack_index: usize = 1; 513 514 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { 515 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border = null, .round_corners = true, .direction = .vert }); 516 errdefer box.deinit(allocator); 517 518 // build the header first so we can grab the files-tab id for the user 519 // view (it focuses there after login). 520 { 521 var header_view = try Header.View.init(allocator, data, session); 522 errdefer header_view.deinit(allocator); 523 try box.children.put(allocator, header_view.getFocus().id, .{ .widget = .{ .repo_header = header_view }, .rect = null, .min_size = null }); 524 } 525 526 { 527 var stack = try wgt.Stack(ui.Widget).init(allocator); 528 errdefer stack.deinit(allocator); 529 530 // files — the default tab: the current directory's listing. 531 { 532 var files_view = try Files.View.init(allocator, &data.files, session); 533 errdefer files_view.deinit(allocator); 534 try stack.children.put(allocator, files_view.getFocus().id, .{ .repo_files = files_view }); 535 } 536 537 switch (data.changes) { 538 .diff => |*diff| { 539 var diff_view = try Diff.View.init(allocator, diff, session); 540 errdefer diff_view.deinit(allocator); 541 try stack.children.put(allocator, diff_view.getFocus().id, .{ .diff_view = diff_view }); 542 }, 543 .commits => |*commits| { 544 var commits_view = try Commits.View.init(allocator, commits, session); 545 errdefer commits_view.deinit(allocator); 546 try stack.children.put(allocator, commits_view.getFocus().id, .{ .repo_commits = commits_view }); 547 }, 548 } 549 550 // refs — the repo's branches and tags. 551 { 552 var refs_view = try Refs.View.init(allocator, &data.refs, session); 553 errdefer refs_view.deinit(allocator); 554 try stack.children.put(allocator, refs_view.getFocus().id, .{ .repo_refs = refs_view }); 555 } 556 557 // discussions and their comment permalinks. 558 if (data.repo.event.discuss_role != null) { 559 var discussions_view = try Discussions.View.init(allocator, &data.discussions, session); 560 errdefer discussions_view.deinit(allocator); 561 try stack.children.put(allocator, discussions_view.getFocus().id, .{ .repo_discussions = discussions_view }); 562 } 563 564 // issues — the repo's issue tracker and comment permalinks. 565 if (data.repo.event.issue_role != null) { 566 var issues_view = try Issues.View.init(allocator, &data.issues, session); 567 errdefer issues_view.deinit(allocator); 568 try stack.children.put(allocator, issues_view.getFocus().id, .{ .repo_issues = issues_view }); 569 } 570 571 // patches and their comment permalinks. 572 if (data.repo.event.patch_role != null) { 573 var patches_view = try Patches.View.init(allocator, &data.patches, session); 574 errdefer patches_view.deinit(allocator); 575 try stack.children.put(allocator, patches_view.getFocus().id, .{ .repo_patches = patches_view }); 576 } 577 578 if (data.handle.canWrite()) { 579 var events_view = try Events.View.init(allocator, &data.events, session); 580 errdefer events_view.deinit(allocator); 581 try stack.children.put(allocator, events_view.getFocus().id, .{ .repo_events = events_view }); 582 } 583 584 if (data.undo) |*undo| { 585 var undo_view = try Undo.View.init(allocator, undo, session); 586 errdefer undo_view.deinit(allocator); 587 try stack.children.put(allocator, undo_view.getFocus().id, .{ .repo_undo = undo_view }); 588 } 589 590 if (data.settings) |*settings| { 591 var settings_view = try Settings.View.init(allocator, settings, session); 592 errdefer settings_view.deinit(allocator); 593 try stack.children.put(allocator, settings_view.getFocus().id, .{ .repo_settings = settings_view }); 594 } 595 596 // the header shows new repo with a login, and new user without one 597 // and user only outside local mode, so keep the stack's children 598 // 1:1 with the tabs by skipping the same views. 599 const identity = try session.page_arena.allocator().print("{s}:{s}", .{ data.header.owner_name, data.header.name }); 600 if (session.data.user_id != null) { 601 const route = ui.RoutablePage{ .repo_repo_new = ui.RoutablePage.Array(ui.RoutablePage.repo_route_max_len).from(identity) orelse return error.RouteTooLong }; 602 var new_repo_view = try ui.NewRepo.View.init(allocator, session, route); 603 errdefer new_repo_view.deinit(allocator); 604 try stack.children.put(allocator, new_repo_view.getFocus().id, .{ .new_repo = new_repo_view }); 605 } else if (session.data.host_kind == .server) { 606 const route = ui.RoutablePage{ .repo_user_new = ui.RoutablePage.Array(ui.RoutablePage.repo_route_max_len).from(identity) orelse return error.RouteTooLong }; 607 var new_user_view = try ui.NewUser.View.init(allocator, session, route); 608 errdefer new_user_view.deinit(allocator); 609 try stack.children.put(allocator, new_user_view.getFocus().id, .{ .new_user = new_user_view }); 610 } 611 612 if (session.data.host_kind == .server) { 613 const route = ui.RoutablePage{ .repo_user = .{ .name = ui.RoutablePage.Array(ui.RoutablePage.repo_route_max_len).from(identity) orelse return error.RouteTooLong } }; 614 var user_view = try ui.UserSettings.initView(allocator, session, &data.user_notifications, route); 615 errdefer user_view.deinit(allocator); 616 try stack.children.put(allocator, user_view.getFocus().id, user_view); 617 } 618 619 if (session.is_terminal) { 620 var quit_view = try Quit.View.init(allocator, session); 621 errdefer quit_view.deinit(allocator); 622 try stack.children.put(allocator, quit_view.getFocus().id, .{ .quit = quit_view }); 623 } 624 625 try box.children.put(allocator, stack.getFocus().id, .{ .widget = .{ .stack = stack }, .rect = null, .min_size = null }); 626 } 627 628 var self = View{ .box = box }; 629 // a page opens on its tabs, except that search results open in the 630 // tab's own search box. 631 const results = data.files.find != null or 632 (data.changes == .commits and data.changes.commits.search != null) or 633 data.discussions.search != null or data.issues.search != null or data.patches.search != null; 634 self.getFocus().child_id = box.children.keys()[if (results) stack_index else header_index]; 635 return self; 636 } 637 638 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 639 self.box.deinit(allocator); 640 } 641 642 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 643 self.clearGrid(); 644 const header = &self.box.children.values()[header_index].widget.repo_header; 645 const stack = &self.box.children.values()[stack_index].widget.stack; 646 647 // each header tab maps 1:1 to a stack child by position 648 if (header.getSelectedIndex()) |index| 649 stack.getFocus().child_id = stack.children.keys()[index]; 650 try self.box.build(allocator, constraint, root_focus); 651 } 652 653 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 654 const stack = &self.box.children.values()[stack_index].widget.stack; 655 if (self.getFocus().child_id) |child_id| { 656 if (self.box.children.getIndex(child_id)) |current_index| { 657 const child = &self.box.children.values()[current_index].widget; 658 var index = current_index; 659 660 const direction = inp.vertDirection(key); 661 662 switch (direction) { 663 .up => { 664 switch (child.*) { 665 .repo_header => { 666 try child.input(allocator, key, root_focus); 667 }, 668 .stack => { 669 if (child.stack.getSelected()) |selected_widget| { 670 if (selected_widget.atTop(root_focus)) { 671 index = header_index; 672 } else { 673 try child.input(allocator, key, root_focus); 674 } 675 } 676 }, 677 else => {}, 678 } 679 }, 680 .down => { 681 switch (child.*) { 682 .repo_header => { 683 if (stack.getSelected()) |selected_widget| switch (selected_widget.*) { 684 .repo_files => |*v| if (v.focusHeader(root_focus)) return, 685 .repo_commits => |*v| if (v.focusHeader(root_focus)) return, 686 .repo_refs => |*v| if (v.focusHeader(root_focus)) return, 687 .repo_events => |*v| if (v.focusHeader(root_focus)) return, 688 .repo_undo => |*v| if (v.focusHeader(root_focus)) return, 689 .repo_settings => |*v| if (v.focusHeader(root_focus)) return, 690 .user_notifications => |*v| if (v.focusHeader(root_focus)) return, 691 else => {}, 692 }; 693 index = stack_index; 694 }, 695 .stack => { 696 try child.input(allocator, key, root_focus); 697 }, 698 else => {}, 699 } 700 }, 701 .none => { 702 try child.input(allocator, key, root_focus); 703 }, 704 } 705 706 if (index != current_index) { 707 root_focus.setFocus(self.box.children.keys()[index]); 708 } 709 } 710 } 711 } 712 713 pub fn clearGrid(self: *View) void { 714 self.box.clearGrid(); 715 } 716 717 pub fn getGrid(self: View) ?Grid { 718 return self.box.getGrid(); 719 } 720 721 pub fn getFocus(self: *View) *Focus { 722 return self.box.getFocus(); 723 } 724 }; 725