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