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