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