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