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