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