╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Repo.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭ find file ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ ╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │ │ │http│ ssh │ │ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
231 var source: ?ui.RepoSource = null; 232 var repo_id_maybe: ?[evt.event_id_size]u8 = null; 233 // local mode has no owner 234 var owner_id: [evt.event_id_size]u8 = @splat(0); 235 var repo: evt.Repo.Record = undefined; 236 // local mode owns its repo 237 var role: evt.Repo.Role = .owner; 238 var owner_name: []const u8 = undefined; 239 if (session.local) |local| { 240 source = local; 241 // local routes elide the identity, so the display name comes from the 242 // repo's directory rather than the route. 243 repo = .{ 244 .event = .{ 245 .user_id = @splat('0'), 246 .name = try arena.allocator().dupe(u8, std.Io.Dir.path.basename(local.path)), 247 .description = "", 248 // local mode shows every tab 249 .discuss_role = .read, 250 .issue_role = .read, 251 .patch_role = .read, 252 }, 253 }; 254 owner_name = ""; 255 } else { 256 const haxy_moment = session.haxy_moment orelse return error.NoMoment; 257 const io = session.io orelse return error.NoMoment; 258 const users_dir = session.users_dir orelse return error.NoMoment; 259 const found = (try evt.readRepoByOwnerAndName(io, arena.child_allocator, arena, haxy_moment, users_dir, repo_identity.owner, repo_identity.name, session.userId())) orelse return error.NotFound; 260 // a repo the session can't read doesn't exist to it 261 role = found.role orelse return error.NotFound; 262 repo = found.repo; 263 repo_id_maybe = found.event_id; 264 265 // resolve the creating user so the header can show their name to the left 266 // of the repo title. 267 owner_id = try evt.parseEventId(&repo.event.user_id); 268 const owner = (try evt.User.readById(DB, hash_kind, haxy_moment, arena, &owner_id)) orelse return error.NotFound; 269 owner_name = owner.event.name; 270 271 // the repo's working copy lives in its owner's dir. 272 source = .{ 273 .path = try evt.repoPath(arena.allocator(), users_dir, &owner_id, &found.event_id), 274 .repo_kind = .xit, 275 }; 276 } 277 278 const handle = ui.RepoHandle{ 279 .location = .{ .repo = try arena.allocator().dupe(u8, repo_identity.identity) }, 280 .viewer = try ui.Viewer.init(session, arena, role), 281 .read_access = repo.event.read_access, 282 }; 283 284 // open the repo once for every tab. files and changes share a ref or revision. 285 // no filesystem (wasm), nowhere to look, or a failed open: empty tabs. 286 // the events tab needs write access, like undo 287 if (route == .repo_events and !handle.canWrite()) return error.NotFound; 288 // a tab that is off doesn't exist 289 const route_thread_kind: ?evt.EventKind = switch (route) { 290 .repo_discussions => .discuss, 291 .repo_issues => .issue, 292 .repo_patches => .patch, 293 else => null, 294 }; 295 if (route_thread_kind) |kind| if (repo.event.threadRole(kind) == null) return error.NotFound; 296 const undo_index = if (route == .repo_undo) route.repo_undo.index else null; 297 // only xit repos have undo history, so it's read in their branch below. 298 // local mode leaves undo to xit itself. 299 var undo_data: ?Undo = null; 300 // only the owner sees the settings, which show the repo's hash kind 301 var settings: ?Settings = null; 302 const files, const changes, const refs, var discussions, var issues, var patches, const events = blk: { 303 read: { 304 const io = session.io orelse break :read; 305 const src = source orelse break :read; 306 const gpa = arena.child_allocator; 307 switch (src.repo_kind) { 308 inline else => |repo_kind| { 309 var any_repo = rp.AnyRepo(repo_kind, .{}).open(io, gpa, src.localInitOpts()) catch break :read; 310 defer any_repo.deinit(io, gpa); 311 switch (any_repo) { 312 inline else => |*opened| { 313 // local mode: bring the event db up to date with the events branch 314 if (session.local != null) { 315 try evt.consume(.local, .repo, repo_kind, opened.self_repo_opts, io, gpa, opened, evt.events_ref, &.{}); 316 try pch.refreshBranches(.local, repo_kind, opened.self_repo_opts, io, gpa, opened, null, null); 317 } 318 if (session.data.host_kind == .server and handle.canUndo()) { 319 // a server page always resolved its repo id 320 const repo_id = repo_id_maybe orelse unreachable; 321 const identity = try arena.allocator().dupe(u8, repo_identity.identity); 322 // only the repo's creator may delete it, never a granted owner 323 const creator = if (session.userId()) |user_id| std.mem.eql(u8, &user_id, &owner_id) else false; 324 // the branch HEAD names, which the branches tab shows as the default 325 var head_buffer: [xit.ref.MAX_REF_CONTENT_SIZE]u8 = undefined; 326 const default_branch = if (opened.head(io, &head_buffer)) |head| switch (head) { 327 .ref => |ref| ref.name, 328 .oid => "", 329 } else |_| ""; 330 settings = .{ 331 .identity = identity, 332 .name = repo.event.name, 333 .description = repo.event.description, 334 .access = repo.event.read_access, 335 .discuss_role = repo.event.discuss_role, 336 .issue_role = repo.event.issue_role, 337 .patch_role = repo.event.patch_role, 338 .hash_kind = opened.self_repo_opts.hash, 339 .view = switch (route) { 340 .repo_members => .members, 341 .repo_branches => .branches, 342 .repo_delete => .delete, 343 else => .settings, 344 }, 345 .members = try Members.init(io, gpa, arena, session.haxy_moment orelse return error.NoMoment, session.users_dir orelse return error.NoMoment, repo_identity.identity, &owner_id, &repo_id, members_route.start, if (members_route.confirm) |action| .{ .action = action, .name = members_route.user.slice() } else null), 346 .branches = try Branches.init(arena, repo_identity.identity, repo.event, default_branch), 347 .delete = if (creator) .{ .target = .{ .repo = identity } } else null, 348 }; 349 } 350 // tabs switch in-page, so every tab's data is read here 351 if (repo_kind == .xit and session.data.host_kind == .server and handle.canWrite()) { 352 undo_data = Undo.init(opened.self_repo_opts, arena, opened, session.haxy_moment, repo_identity.identity, undo_index, handle) catch |err| switch (err) { 353 error.OutOfMemory => return err, 354 else => .{ .identity = try arena.allocator().dupe(u8, repo_identity.identity), .handle = handle, .failure = @errorName(err) }, 355 }; 356 } 357 // a merge: route stands for the log of what the merge brought in 358 const ref_or_oid: ?ui.RoutablePage.RefOrOid, const ref_value, const base_oid = if (commits_merge.len == 0) 359 .{ requested_ref_or_oid, requested_ref_value, commits_base_oid } 360 else merged: { 361 const merged = try Commits.resolveMerge(repo_kind, opened.self_repo_opts, arena, opened, io, gpa, commits_merge); 362 break :merged .{ .object, merged.oid, merged.base_oid }; 363 }; 364 const files_data = if (patchrev_id.len != 0) 365 try Files.initPatchRev(repo_kind, opened.self_repo_opts, arena, opened, io, gpa, handle, patchrev_id, files_dir, files_line) 366 else 367 try Files.init(repo_kind, opened.self_repo_opts, arena, opened, io, gpa, handle, ref_or_oid, ref_value, files_dir, files_line, files_find); 368 const changes_data: Changes = if (route == .repo_diff) 369 .{ .diff = try Diff.init(repo_kind, opened.self_repo_opts, arena, opened, io, gpa, route.repo_diff) } 370 else if (patchrev_id.len != 0) diff: { 371 const diff_route = ui.RoutablePage.repoPatchRevDiffRoute(repo_identity.identity, patchrev_id, 0, "") orelse return error.RouteTooLong; 372 break :diff .{ .diff = try Diff.init(repo_kind, opened.self_repo_opts, arena, opened, io, gpa, diff_route.repo_diff) }; 373 } else .{ .commits = try Commits.init(repo_kind, opened.self_repo_opts, arena, opened, io, gpa, session.haxy_moment, handle, ref_or_oid, ref_value, commits_message, base_oid, commits_search, commits_from) }; 374 const target_branch = if (files_data.ref_or_oid == .branch) files_data.ref_or_oid_value else ""; 375 var discussions_data = try Discussions.init(repo_kind, opened.self_repo_opts, arena, opened, io, session.haxy_moment, repo_identity.identity, discussions_search, discussions_selected, discussions_comment, discussions_comments_start, discussions_view, tabViewer(handle, repo, .discuss)); 376 var issues_data = try Issues.init(repo_kind, opened.self_repo_opts, arena, opened, io, session.haxy_moment, repo_identity.identity, issues_search, issues_selected, issues_comment, issues_comments_start, issues_theirs, issues_view, tabViewer(handle, repo, .issue)); 377 var patches_data = 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_search, patches_selected, patches_comment, patches_comments_start, patches_theirs, patches_view, tabViewer(handle, repo, .patch)); 378 if (repo_kind == .xit and session.data.host_kind == .server) { 379 // a server page always resolved its repo id 380 const repo_id = repo_id_maybe orelse unreachable; 381 readNotify(opened.self_repo_opts, session, arena, opened, &owner_id, &repo_id, viewed_thread, &discussions_data, &issues_data, &patches_data) catch |err| { 382 std.log.warn("failed to read thread notifications: {s}", .{@errorName(err)}); 383 }; 384 } 385 break :blk .{ 386 files_data, 387 changes_data, 388 try Refs.init(repo_kind, opened.self_repo_opts, arena, opened, io, gpa, repo_identity.identity, refs_kind, refs_from, refs_search), 389 discussions_data, 390 issues_data, 391 patches_data, 392 try Events.init(repo_kind, opened.self_repo_opts, arena, opened, io, session.haxy_moment, repo_identity.identity, repo.event, events_view, events_kind, events_selected, events_moment, session.local != null, session.data.sync_failure), 393 }; 394 }, 395 } 396 }, 397 } 398 } 399 const aa = arena.allocator(); 400 break :blk .{ 401 try Files.emptyResult(aa, handle, requested_ref_or_oid orelse .branch, requested_ref_value, files_dir), 402 Changes{ .commits = try Commits.emptyResult(aa, handle, requested_ref_or_oid orelse .branch, requested_ref_value, commits_base_oid) }, 403 try Refs.emptyResult(arena, repo_identity.identity, refs_kind, refs_from, refs_search), 404 try Discussions.emptyResult(aa, repo_identity.identity, discussions_search, discussions_selected, discussions_comment, discussions_comments_start, discussions_view), 405 try Issues.emptyResult(aa, repo_identity.identity, issues_search, issues_selected, issues_comment, issues_comments_start, issues_theirs, issues_view), 406 try Patches.emptyResult(aa, repo_identity.identity, patches_search, patches_selected, patches_comment, patches_comments_start, patches_theirs, patches_view), 407 try Events.empty(aa, repo_identity.identity, events_view, session.local != null, session.data.sync_failure), 408 }; 409 }; 410 if (route == .repo_undo and undo_data == null) return error.NotFound; 411 if ((route == .repo_settings or route == .repo_members or route == .repo_branches or route == .repo_delete) and settings == null) return error.NotFound; 412 if (settings) |value| if (route == .repo_delete and value.delete == null) return error.NotFound; 413 discussions.repo_source = source; 414 issues.repo_source = source; 415 patches.repo_source = source; 416 patches.repo_id = repo_id_maybe; 417 418 return .{ 419 // use the files tab's resolved ref for the header 420 .header = try Header.init(arena, repo.event.name, owner_name, files.ref_or_oid, files.ref_or_oid_value, discussions.search, issues.search, patches.search), 421 .repo = repo, 422 .handle = handle, 423 .files = files, 424 .changes = changes, 425 .refs = refs, 426 .discussions = discussions, 427 .issues = issues, 428 .patches = patches, 429 .events = events, 430 .undo = undo_data, 431 .settings = settings, 432 // after the shown thread's notif is marked read 433 .user_notifications = try ui.UserNotifications.init(arena, session, route.userTab()), 434 .quit = Quit.init(), 435 }; 436 } 437 438 // set the viewer's notify button on every listed thread and mark the viewed thread's notif read 439 fn readNotify( 440 comptime repo_opts: rp.RepoOpts(.xit), 441 session: *ui.Session, 442 arena: *std.heap.ArenaAllocator, 443 repo: *rp.Repo(.xit, repo_opts), 444 owner_id: *const [evt.event_id_size]u8, 445 repo_id: *const [evt.event_id_size]u8, 446 viewed_thread: []const u8, 447 discussions: *Discussions, 448 issues: *Issues, 449 patches: *Patches, 450 ) !void { 451 const user_id = session.userId() orelse return; 452 const io = session.io orelse return error.NoMoment; 453 const users_dir = session.users_dir orelse return error.NoMoment; 454 const gpa = arena.child_allocator; 455 456 { 457 // the owner's plain data, which holds the tier index 458 var owner_repo = (try evt.openUserRepo(io, gpa, users_dir, owner_id)) orelse return error.NotFound; 459 defer owner_repo.deinit(io, gpa); 460 const owner_moment = try notify.latestMoment(&owner_repo); 461 462 try setNotifyActions(arena, &discussions.recent, owner_moment, repo_id, &user_id); 463 for ([_]*Issues.Window{ &issues.open, &issues.closed, &issues.conflicts }) |window| try setNotifyActions(arena, window, owner_moment, repo_id, &user_id); 464 for ([_]*Patches.Window{ &patches.open, &patches.closed, &patches.merged, &patches.conflicts }) |window| try setNotifyActions(arena, window, owner_moment, repo_id, &user_id); 465 } 466 467 if (viewed_thread.len == 0) return; 468 var user_repo = (try evt.openUserRepo(io, gpa, users_dir, &user_id)) orelse return; 469 defer user_repo.deinit(io, gpa); 470 const latest = (try notify.latestMoment(&user_repo)) orelse return; 471 const notif_id = notify.idOf(repo_id, &try evt.parseEventId(viewed_thread)); 472 const item = (try notify.readItem(latest, arena, &notif_id)) orelse return; 473 if (!item.unread) return; 474 // the page reads this moment or a newer one, so a child in it is shown 475 const moment = try evt.currentMoment(repo_opts, repo); 476 const kinds_cursor = try moment.getCursor(hash.hashInt(repo_opts.hash, evt.event_index_key)) orelse return error.CursorNotFound; 477 const kinds = try evt.EventDB(repo_opts.hash).HashMap(.read_only).init(kinds_cursor); 478 // a child the page can't show leaves it unread 479 if (try evt.readEventKind(repo_opts.hash, kinds, &item.notif.last_child_id) == null) return; 480 try notify.markRead(io, gpa, users_dir, &user_id, &notif_id, &item.notif.last_child_id); 481 } 482 483 // set the viewer's notify button on each of a window's threads 484 fn setNotifyActions( 485 arena: *std.heap.ArenaAllocator, 486 window: anytype, 487 owner_moment: ?evt.UserDB.HashMap(.read_only), 488 repo_id: *const [evt.event_id_size]u8, 489 user_id: *const [evt.event_id_size]u8, 490 ) !void { 491 const items = try arena.allocator().dupe(std.meta.Elem(@TypeOf(window.items)), window.items); 492 for (items) |*entry| { 493 const notif_id = notify.idOf(repo_id, &try evt.parseEventId(entry.id)); 494 // muting drops the user from every tier, so membership alone decides 495 const notified = if (owner_moment) |moment| try notify.isNotified(moment, &notif_id, user_id) else false; 496 entry.notify_action = if (notified) .mute else .subscribe; 497 } 498 window.items = items; 499 } 500 501 // the viewer a tab's comment bar sees, which is none below the tab's role 502 fn tabViewer(handle: ui.RepoHandle, repo: evt.Repo.Record, kind: evt.EventKind) ?ui.Viewer { 503 const viewer = handle.viewer orelse return null; 504 const tab_role = repo.event.threadRole(kind) orelse return null; 505 return if (viewer.role.atLeast(tab_role)) viewer else null; 506 } 507 508 pub const View = struct { 509 box: wgt.Box(ui.Widget), 510 511 const header_index: usize = 0; 512 const stack_index: usize = 1; 513 514 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { 515 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border = null, .round_corners = true, .direction = .vert }); 516 errdefer box.deinit(allocator); 517 518 // build the header first so we can grab the files-tab id for the user 519 // view (it focuses there after login). 520 { 521 var header_view = try Header.View.init(allocator, data, session); 522 errdefer header_view.deinit(allocator); 523 try box.children.put(allocator, header_view.getFocus().id, .{ .widget = .{ .repo_header = header_view }, .rect = null, .min_size = null }); 524 } 525 526 { 527 var stack = try wgt.Stack(ui.Widget).init(allocator); 528 errdefer stack.deinit(allocator); 529 530 // files — the default tab: the current directory's listing. 531 { 532 var files_view = try Files.View.init(allocator, &data.files, session); 533 errdefer files_view.deinit(allocator); 534 try stack.children.put(allocator, files_view.getFocus().id, .{ .repo_files = files_view }); 535 } 536 537 switch (data.changes) { 538 .diff => |*diff| { 539 var diff_view = try Diff.View.init(allocator, diff, session); 540 errdefer diff_view.deinit(allocator); 541 try stack.children.put(allocator, diff_view.getFocus().id, .{ .diff_view = diff_view }); 542 }, 543 .commits => |*commits| { 544 var commits_view = try Commits.View.init(allocator, commits, session); 545 errdefer commits_view.deinit(allocator); 546 try stack.children.put(allocator, commits_view.getFocus().id, .{ .repo_commits = commits_view }); 547 }, 548 } 549 550 // refs — the repo's branches and tags. 551 { 552 var refs_view = try Refs.View.init(allocator, &data.refs, session); 553 errdefer refs_view.deinit(allocator); 554 try stack.children.put(allocator, refs_view.getFocus().id, .{ .repo_refs = refs_view }); 555 } 556 557 // discussions and their comment permalinks. 558 if (data.repo.event.discuss_role != null) { 559 var discussions_view = try Discussions.View.init(allocator, &data.discussions, session); 560 errdefer discussions_view.deinit(allocator); 561 try stack.children.put(allocator, discussions_view.getFocus().id, .{ .repo_discussions = discussions_view }); 562 } 563 564 // issues — the repo's issue tracker and comment permalinks. 565 if (data.repo.event.issue_role != null) { 566 var issues_view = try Issues.View.init(allocator, &data.issues, session); 567 errdefer issues_view.deinit(allocator); 568 try stack.children.put(allocator, issues_view.getFocus().id, .{ .repo_issues = issues_view }); 569 } 570 571 // patches and their comment permalinks. 572 if (data.repo.event.patch_role != null) { 573 var patches_view = try Patches.View.init(allocator, &data.patches, session); 574 errdefer patches_view.deinit(allocator); 575 try stack.children.put(allocator, patches_view.getFocus().id, .{ .repo_patches = patches_view }); 576 } 577 578 if (data.handle.canWrite()) { 579 var events_view = try Events.View.init(allocator, &data.events, session); 580 errdefer events_view.deinit(allocator); 581 try stack.children.put(allocator, events_view.getFocus().id, .{ .repo_events = events_view }); 582 } 583 584 if (data.undo) |*undo| { 585 var undo_view = try Undo.View.init(allocator, undo, session); 586 errdefer undo_view.deinit(allocator); 587 try stack.children.put(allocator, undo_view.getFocus().id, .{ .repo_undo = undo_view }); 588 } 589 590 if (data.settings) |*settings| { 591 var settings_view = try Settings.View.init(allocator, settings, session); 592 errdefer settings_view.deinit(allocator); 593 try stack.children.put(allocator, settings_view.getFocus().id, .{ .repo_settings = settings_view }); 594 } 595 596 // the header shows new repo with a login, and new user without one 597 // and user only outside local mode, so keep the stack's children 598 // 1:1 with the tabs by skipping the same views. 599 const identity = try session.page_arena.allocator().print("{s}:{s}", .{ data.header.owner_name, data.header.name }); 600 if (session.data.user_id != null) { 601 const route = ui.RoutablePage{ .repo_repo_new = ui.RoutablePage.Array(ui.RoutablePage.repo_route_max_len).from(identity) orelse return error.RouteTooLong }; 602 var new_repo_view = try ui.NewRepo.View.init(allocator, session, route); 603 errdefer new_repo_view.deinit(allocator); 604 try stack.children.put(allocator, new_repo_view.getFocus().id, .{ .new_repo = new_repo_view }); 605 } else if (session.data.host_kind == .server) { 606 const route = ui.RoutablePage{ .repo_user_new = ui.RoutablePage.Array(ui.RoutablePage.repo_route_max_len).from(identity) orelse return error.RouteTooLong }; 607 var new_user_view = try ui.NewUser.View.init(allocator, session, route); 608 errdefer new_user_view.deinit(allocator); 609 try stack.children.put(allocator, new_user_view.getFocus().id, .{ .new_user = new_user_view }); 610 } 611 612 if (session.data.host_kind == .server) { 613 const route = ui.RoutablePage{ .repo_user = .{ .name = ui.RoutablePage.Array(ui.RoutablePage.repo_route_max_len).from(identity) orelse return error.RouteTooLong } }; 614 var user_view = try ui.UserSettings.initView(allocator, session, &data.user_notifications, route); 615 errdefer user_view.deinit(allocator); 616 try stack.children.put(allocator, user_view.getFocus().id, user_view); 617 } 618 619 if (session.is_terminal) { 620 var quit_view = try Quit.View.init(allocator, session); 621 errdefer quit_view.deinit(allocator); 622 try stack.children.put(allocator, quit_view.getFocus().id, .{ .quit = quit_view }); 623 } 624 625 try box.children.put(allocator, stack.getFocus().id, .{ .widget = .{ .stack = stack }, .rect = null, .min_size = null }); 626 } 627 628 var self = View{ .box = box }; 629 // a page opens on its tabs, except that search results open in the 630 // tab's own search box. 631 const results = data.files.find != null or 632 (data.changes == .commits and data.changes.commits.search != null) or 633 data.discussions.search != null or data.issues.search != null or data.patches.search != null; 634 self.getFocus().child_id = box.children.keys()[if (results) stack_index else header_index]; 635 return self; 636 } 637 638 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 639 self.box.deinit(allocator); 640 } 641 642 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 643 self.clearGrid(); 644 const header = &self.box.children.values()[header_index].widget.repo_header; 645 const stack = &self.box.children.values()[stack_index].widget.stack; 646 647 // each header tab maps 1:1 to a stack child by position 648 if (header.getSelectedIndex()) |index| 649 stack.getFocus().child_id = stack.children.keys()[index]; 650 try self.box.build(allocator, constraint, root_focus); 651 } 652 653 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 654 const stack = &self.box.children.values()[stack_index].widget.stack; 655 if (self.getFocus().child_id) |child_id| { 656 if (self.box.children.getIndex(child_id)) |current_index| { 657 const child = &self.box.children.values()[current_index].widget; 658 var index = current_index; 659 660 const direction = inp.vertDirection(key); 661 662 switch (direction) { 663 .up => { 664 switch (child.*) { 665 .repo_header => { 666 try child.input(allocator, key, root_focus); 667 }, 668 .stack => { 669 if (child.stack.getSelected()) |selected_widget| { 670 if (selected_widget.atTop(root_focus)) { 671 index = header_index; 672 } else { 673 try child.input(allocator, key, root_focus); 674 } 675 } 676 }, 677 else => {}, 678 } 679 }, 680 .down => { 681 switch (child.*) { 682 .repo_header => { 683 if (stack.getSelected()) |selected_widget| switch (selected_widget.*) { 684 .repo_files => |*v| if (v.focusHeader(root_focus)) return, 685 .repo_commits => |*v| if (v.focusHeader(root_focus)) return, 686 .repo_refs => |*v| if (v.focusHeader(root_focus)) return, 687 .repo_events => |*v| if (v.focusHeader(root_focus)) return, 688 .repo_undo => |*v| if (v.focusHeader(root_focus)) return, 689 .repo_settings => |*v| if (v.focusHeader(root_focus)) return, 690 .user_notifications => |*v| if (v.focusHeader(root_focus)) return, 691 else => {}, 692 }; 693 index = stack_index; 694 }, 695 .stack => { 696 try child.input(allocator, key, root_focus); 697 }, 698 else => {}, 699 } 700 }, 701 .none => { 702 try child.input(allocator, key, root_focus); 703 }, 704 } 705 706 if (index != current_index) { 707 root_focus.setFocus(self.box.children.keys()[index]); 708 } 709 } 710 } 711 } 712 713 pub fn clearGrid(self: *View) void { 714 self.box.clearGrid(); 715 } 716 717 pub fn getGrid(self: View) ?Grid { 718 return self.box.getGrid(); 719 } 720 721 pub fn getFocus(self: *View) *Focus { 722 return self.box.getFocus(); 723 } 724 }; 725