╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Repo.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2 const evt = @import("../event.zig"); 3 const pch = @import("../patch.zig"); 4 const ui = @import("../ui.zig"); 5 const xit = @import("xit"); 6 const rp = xit.repo; 7 const xitui = xit.xitui; 8 const wgt = xitui.widget; 9 const layout = xitui.layout; 10 const Key = xitui.input.Key; 11 const Grid = xitui.grid.Grid; 12 const Focus = xitui.focus.Focus; 13 const inp = @import("./input.zig"); 14 15 pub const Header = @import("./Repo/Header.zig"); 16 pub const Files = @import("./Repo/Files.zig"); 17 pub const Markdown = @import("./Repo/Markdown.zig"); 18 pub const Commits = @import("./Repo/Commits.zig"); 19 pub const Diff = @import("./Repo/Diff.zig"); 20 pub const Refs = @import("./Repo/Refs.zig"); 21 pub const Discussions = @import("./Repo/Discussions.zig"); 22 pub const Issues = @import("./Repo/Issues.zig"); 23 pub const Patches = @import("./Repo/Patches.zig"); 24 pub const Comment = @import("./Repo/Comment.zig"); 25 pub const Undo = @import("./Repo/Undo.zig"); 26 pub const Events = @import("./Repo/Events.zig"); 27 pub const Settings = @import("./Repo/Settings.zig"); 28 pub const Quit = @import("./Quit.zig"); 29 30 header: Header, 31 repo: evt.Repo.Record, 32 handle: ui.RepoHandle, 33 files: Files, 34 changes: Changes, 35 refs: Refs, 36 discussions: Discussions, 37 issues: Issues, 38 patches: Patches, 39 events: Events, 40 undo: ?Undo = null, 41 settings: ?Settings = null, 42 quit: Quit, 43 44 const Self = @This(); 45 46 const Changes = union(enum) { 47 commits: Commits, 48 diff: Diff, 49 }; 50 51 pub fn init( 52 arena: *std.heap.ArenaAllocator, 53 session: *ui.Session, 54 route: ui.RoutablePage, 55 ) !Self { 56 const DB = evt.AdminDB; 57 const hash_kind = evt.admin_repo_opts.hash; 58 59 // every repo route stores its identity as "owner:name" (or elides it in local mode) 60 const name_str = route.repoIdentity() orelse return error.UnexpectedRoute; 61 const repo_identity = ui.RoutablePage.RepoIdentity.parse(name_str) orelse return error.NotFound; 62 // files, commits and diff share the requested ref, or the default branch. 63 // directories and hunk windows only apply to their own tab. 64 const requested_ref_or_oid: ?ui.RoutablePage.RefOrOid = switch (route) { 65 .repo_files => |f| f.ref_kind, 66 .repo_commits => |c| c.ref_or_oid, 67 .repo_diff => |d| d.ref_or_oid, 68 else => null, 69 }; 70 const requested_ref_value: []const u8 = switch (route) { 71 .repo_files => |*f| f.ref_value.slice(), 72 .repo_commits => |*c| c.value.slice(), 73 .repo_diff => |*d| d.value.slice(), 74 else => "", 75 }; 76 const patchrev_id: []const u8 = switch (route) { 77 .repo_files => |*f| f.patchrev_id.slice(), 78 .repo_diff => |*d| d.patchrev_id.slice(), 79 else => "", 80 }; 81 const files_dir = switch (route) { 82 .repo_files => |*f| f.path.slice(), 83 else => "", 84 }; 85 const files_line = switch (route) { 86 .repo_files => |f| f.line, 87 else => 0, 88 }; 89 const files_find: []const u8 = switch (route) { 90 .repo_files => |*f| f.find.slice(), 91 else => "", 92 }; 93 // the commits view's pane shows the whole message of the commit it walks from 94 const commits_message = route == .repo_commits and route.repo_commits.message; 95 const commits_base_oid: []const u8 = switch (route) { 96 .repo_commits => |*c| c.base_oid.slice(), 97 else => "", 98 }; 99 const commits_search: []const u8 = switch (route) { 100 .repo_commits => |*c| c.search.slice(), 101 else => "", 102 }; 103 const commits_from: []const u8 = switch (route) { 104 .repo_commits => |*c| c.from.slice(), 105 else => "", 106 }; 107 const commits_merge: []const u8 = switch (route) { 108 .repo_commits => |*c| c.merge.slice(), 109 else => "", 110 }; 111 // the refs tab windows one column at a time: `refs_from` (a url-encoded 112 // ref name) roots `refs_kind`'s column, the other stays at its first window. 113 const refs_kind: ui.RoutablePage.RefKind = switch (route) { 114 .repo_refs => |r| r.kind, 115 else => .branch, 116 }; 117 const refs_from: []const u8 = switch (route) { 118 .repo_refs => |*r| r.from.slice(), 119 else => "", 120 }; 121 const refs_search: []const u8 = switch (route) { 122 .repo_refs => |*r| r.search.slice(), 123 else => "", 124 }; 125 const discussions_label: []const u8 = switch (route) { 126 .repo_discussions => |*t| t.label.slice(), 127 else => "", 128 }; 129 const discussions_search: []const u8 = switch (route) { 130 .repo_discussions => |*t| t.search.slice(), 131 else => "", 132 }; 133 const discussions_selected: []const u8 = switch (route) { 134 .repo_discussions => |*t| t.selected.slice(), 135 else => "", 136 }; 137 const discussions_comment: []const u8 = switch (route) { 138 .repo_discussions => |*t| t.comment.slice(), 139 else => "", 140 }; 141 const discussions_view: ui.RoutablePage.DiscussionsView = switch (route) { 142 .repo_discussions => |t| t.view, 143 else => .recent, 144 }; 145 const discussions_comments_start: usize = switch (route) { 146 .repo_discussions => |t| t.comments_start, 147 else => 0, 148 }; 149 // the issues tab's label filter, the issue its window is rooted at, and the 150 // view it shows. 151 const issues_label: []const u8 = switch (route) { 152 .repo_issues => |*i| i.label.slice(), 153 else => "", 154 }; 155 const issues_search: []const u8 = switch (route) { 156 .repo_issues => |*i| i.search.slice(), 157 else => "", 158 }; 159 const issues_selected: []const u8 = switch (route) { 160 .repo_issues => |*i| i.selected.slice(), 161 else => "", 162 }; 163 const issues_comment: []const u8 = switch (route) { 164 .repo_issues => |*i| i.comment.slice(), 165 else => "", 166 }; 167 const issues_theirs: []const u8 = switch (route) { 168 .repo_issues => |*i| i.theirs.slice(), 169 else => "", 170 }; 171 const issues_view: ui.RoutablePage.IssuesView = switch (route) { 172 .repo_issues => |i| i.view, 173 else => .open, 174 }; 175 const issues_comments_start: usize = switch (route) { 176 .repo_issues => |i| i.comments_start, 177 else => 0, 178 }; 179 const patches_label: []const u8 = switch (route) { 180 .repo_patches => |*p| p.label.slice(), 181 else => "", 182 }; 183 const patches_search: []const u8 = switch (route) { 184 .repo_patches => |*p| p.search.slice(), 185 else => "", 186 }; 187 const patches_selected: []const u8 = switch (route) { 188 .repo_patches => |*p| p.selected.slice(), 189 else => "", 190 }; 191 const patches_comment: []const u8 = switch (route) { 192 .repo_patches => |*p| p.comment.slice(), 193 else => "", 194 }; 195 const patches_theirs: []const u8 = switch (route) { 196 .repo_patches => |*p| p.theirs.slice(), 197 else => "", 198 }; 199 const patches_view: ui.RoutablePage.PatchesView = switch (route) { 200 .repo_patches => |p| p.view, 201 else => .open, 202 }; 203 const patches_comments_start: usize = switch (route) { 204 .repo_patches => |p| p.comments_start, 205 else => 0, 206 }; 207 const events_kind: ?evt.EventKind = switch (route) { 208 .repo_events => |e| e.kind, 209 else => null, 210 }; 211 const events_view: ui.RoutablePage.EventsView = switch (route) { 212 .repo_events => |e| e.view, 213 else => .active, 214 }; 215 const events_moment: ?u64 = switch (route) { 216 .repo_events => |e| e.moment, 217 else => null, 218 }; 219 const events_selected: []const u8 = switch (route) { 220 .repo_events => |*e| e.selected.slice(), 221 else => "", 222 }; 223 224 // where the on-disk repo lives (null keeps the views' empty fallback), plus 225 // the repo and owner-name metadata the header shows. local mode already 226 // knows all three; the server paths resolve them from the admin db. 227 var source: ?ui.RepoSource = null; 228 var repo_id_maybe: ?[evt.event_id_size]u8 = null; 229 var repo: evt.Repo.Record = undefined; 230 // local mode owns its repo 231 var role: evt.Repo.Role = .owner; 232 var owner_name: []const u8 = undefined; 233 if (session.local) |local| { 234 source = local; 235 // local routes elide the identity, so the display name comes from the 236 // repo's directory rather than the route. 237 repo = .{ 238 .event = .{ 239 .user_id = "", 240 .name = try arena.allocator().dupe(u8, std.Io.Dir.path.basename(local.path)), 241 .description = "", 242 // local mode shows every tab 243 .discuss_role = .read, 244 .issue_role = .read, 245 .patch_role = .read, 246 }, 247 }; 248 owner_name = ""; 249 } else { 250 const haxy_moment = session.haxy_moment orelse return error.NoMoment; 251 const io = session.io orelse return error.NoMoment; 252 const users_dir = session.users_dir orelse return error.NoMoment; 253 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; 254 // a repo the session can't read doesn't exist to it 255 role = found.role orelse return error.NotFound; 256 repo = found.repo; 257 repo_id_maybe = found.event_id; 258 259 // resolve the creating user so the header can show their name to the left 260 // of the repo title. 261 const owner = (try evt.User.readById(DB, hash_kind, haxy_moment, arena, repo.event.user_id)) orelse return error.NotFound; 262 owner_name = owner.event.name; 263 264 // the repo's working copy lives in its owner's dir. 265 source = .{ 266 .path = try evt.repoPath(arena.allocator(), users_dir, repo.event.user_id, &found.event_id), 267 .repo_kind = .xit, 268 }; 269 } 270 271 const handle = ui.RepoHandle{ 272 .location = .{ .repo = try arena.allocator().dupe(u8, repo_identity.identity) }, 273 .viewer = try ui.Viewer.init(session, arena, role), 274 }; 275 276 // open the repo once for every tab. files and changes share a ref or revision. 277 // no filesystem (wasm), nowhere to look, or a failed open: empty tabs. 278 // the events tab needs write access, like undo 279 if (route == .repo_events and !handle.canWrite()) return error.NotFound; 280 // a tab that is off doesn't exist 281 const route_thread_kind: ?evt.EventKind = switch (route) { 282 .repo_discussions => .discuss, 283 .repo_issues => .issue, 284 .repo_patches => .patch, 285 else => null, 286 }; 287 if (route_thread_kind) |kind| if (repo.event.threadRole(kind) == null) return error.NotFound; 288 const undo_index = if (route == .repo_undo) route.repo_undo.index else null; 289 const undo_clear = route == .repo_undo and route.repo_undo.clear; 290 // only xit repos have undo history, so it's read in their branch below 291 var undo_data: ?Undo = null; 292 // only the owner sees the settings, which show the repo's hash kind 293 var settings: ?Settings = null; 294 const files, const changes, const refs, var discussions, var issues, var patches, const events = blk: { 295 read: { 296 const io = session.io orelse break :read; 297 const src = source orelse break :read; 298 const gpa = arena.child_allocator; 299 switch (src.repo_kind) { 300 inline else => |repo_kind| { 301 var any_repo = rp.AnyRepo(repo_kind, .{}).open(io, gpa, src.localInitOpts()) catch break :read; 302 defer any_repo.deinit(io, gpa); 303 switch (any_repo) { 304 inline else => |*opened| { 305 // local mode: bring the event db up to date with the events branch 306 if (session.local != null) { 307 try evt.consume(.local, .repo, repo_kind, opened.self_repo_opts, io, gpa, opened, evt.events_ref, &.{}); 308 try pch.refreshBranches(.local, repo_kind, opened.self_repo_opts, io, gpa, opened, null, null); 309 } 310 if (session.data.host_kind == .server and handle.canUndo()) settings = .{ 311 .identity = try arena.allocator().dupe(u8, repo_identity.identity), 312 .name = repo.event.name, 313 .description = repo.event.description, 314 .access = repo.event.read_access, 315 .discuss_role = repo.event.discuss_role, 316 .issue_role = repo.event.issue_role, 317 .patch_role = repo.event.patch_role, 318 .hash_kind = opened.self_repo_opts.hash, 319 }; 320 // tabs switch in-page, so every tab's data is read here 321 if (repo_kind == .xit and handle.canWrite()) { 322 undo_data = Undo.init(opened.self_repo_opts, arena, opened, session.haxy_moment, repo_identity.identity, undo_index, handle) catch |err| switch (err) { 323 error.OutOfMemory => return err, 324 else => .{ .identity = try arena.allocator().dupe(u8, repo_identity.identity), .handle = handle, .failure = @errorName(err) }, 325 }; 326 } 327 // a merge: route stands for the log of what the merge brought in 328 const ref_or_oid: ?ui.RoutablePage.RefOrOid, const ref_value, const base_oid = if (commits_merge.len == 0) 329 .{ requested_ref_or_oid, requested_ref_value, commits_base_oid } 330 else merged: { 331 const merged = try Commits.resolveMerge(repo_kind, opened.self_repo_opts, arena, opened, io, gpa, commits_merge); 332 break :merged .{ .object, merged.oid, merged.base_oid }; 333 }; 334 const files_data = if (patchrev_id.len != 0) 335 try Files.initPatchRev(repo_kind, opened.self_repo_opts, arena, opened, io, gpa, handle, patchrev_id, files_dir, files_line) 336 else 337 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); 338 const changes_data: Changes = if (route == .repo_diff) 339 .{ .diff = try Diff.init(repo_kind, opened.self_repo_opts, arena, opened, io, gpa, route.repo_diff) } 340 else if (patchrev_id.len != 0) diff: { 341 const diff_route = ui.RoutablePage.repoPatchRevDiffRoute(repo_identity.identity, patchrev_id, 0, "") orelse return error.RouteTooLong; 342 break :diff .{ .diff = try Diff.init(repo_kind, opened.self_repo_opts, arena, opened, io, gpa, diff_route.repo_diff) }; 343 } 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) }; 344 const target_branch = if (files_data.ref_or_oid == .branch) files_data.ref_or_oid_value else ""; 345 break :blk .{ 346 files_data, 347 changes_data, 348 try Refs.init(repo_kind, opened.self_repo_opts, arena, opened, io, gpa, repo_identity.identity, refs_kind, refs_from, refs_search), 349 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, tabViewer(handle, repo, .discuss)), 350 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, tabViewer(handle, repo, .issue)), 351 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, tabViewer(handle, repo, .patch)), 352 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), 353 }; 354 }, 355 } 356 }, 357 } 358 } 359 const aa = arena.allocator(); 360 break :blk .{ 361 try Files.emptyResult(aa, handle, requested_ref_or_oid orelse .branch, requested_ref_value, files_dir), 362 Changes{ .commits = try Commits.emptyResult(aa, handle, requested_ref_or_oid orelse .branch, requested_ref_value, commits_base_oid) }, 363 try Refs.emptyResult(arena, repo_identity.identity, refs_kind, refs_from, refs_search), 364 try Discussions.emptyResult(aa, repo_identity.identity, discussions_label, discussions_search, discussions_selected, discussions_comment, discussions_comments_start, discussions_view), 365 try Issues.emptyResult(aa, repo_identity.identity, issues_label, issues_search, issues_selected, issues_comment, issues_comments_start, issues_theirs, issues_view), 366 try Patches.emptyResult(aa, repo_identity.identity, patches_label, patches_search, patches_selected, patches_comment, patches_comments_start, patches_theirs, patches_view), 367 try Events.empty(aa, repo_identity.identity, events_view, session.local != null, session.data.sync_failure), 368 }; 369 }; 370 if (route == .repo_undo and undo_data == null) return error.NotFound; 371 if (route == .repo_repo and settings == null) return error.NotFound; 372 if (undo_data) |*undo| undo.clear = undo_clear; 373 discussions.repo_source = source; 374 issues.repo_source = source; 375 patches.repo_source = source; 376 patches.repo_id = repo_id_maybe; 377 378 return .{ 379 // use the files tab's resolved ref for the header 380 .header = try Header.init(arena, repo.event.name, owner_name, files.ref_or_oid, files.ref_or_oid_value, discussions.label, issues.label, patches.label), 381 .repo = repo, 382 .handle = handle, 383 .files = files, 384 .changes = changes, 385 .refs = refs, 386 .discussions = discussions, 387 .issues = issues, 388 .patches = patches, 389 .events = events, 390 .undo = undo_data, 391 .settings = settings, 392 .quit = Quit.init(), 393 }; 394 } 395 396 // the viewer a tab's comment bar sees, which is none below the tab's role 397 fn tabViewer(handle: ui.RepoHandle, repo: evt.Repo.Record, kind: evt.EventKind) ?ui.Viewer { 398 const viewer = handle.viewer orelse return null; 399 const tab_role = repo.event.threadRole(kind) orelse return null; 400 return if (viewer.role.atLeast(tab_role)) viewer else null; 401 } 402 403 pub const View = struct { 404 box: wgt.Box(ui.Widget), 405 406 const header_index: usize = 0; 407 const stack_index: usize = 1; 408 409 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { 410 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border = null, .round_corners = true, .direction = .vert }); 411 errdefer box.deinit(allocator); 412 413 // build the header first so we can grab the files-tab id for the user 414 // view (it focuses there after login). 415 { 416 var header_view = try Header.View.init(allocator, data, session); 417 errdefer header_view.deinit(allocator); 418 try box.children.put(allocator, header_view.getFocus().id, .{ .widget = .{ .repo_header = header_view }, .rect = null, .min_size = null }); 419 } 420 421 { 422 var stack = try wgt.Stack(ui.Widget).init(allocator); 423 errdefer stack.deinit(allocator); 424 425 // files — the default tab: the current directory's listing. 426 { 427 var files_view = try Files.View.init(allocator, &data.files, session); 428 errdefer files_view.deinit(allocator); 429 try stack.children.put(allocator, files_view.getFocus().id, .{ .repo_files = files_view }); 430 } 431 432 switch (data.changes) { 433 .diff => |*diff| { 434 var diff_view = try Diff.View.init(allocator, diff, session); 435 errdefer diff_view.deinit(allocator); 436 try stack.children.put(allocator, diff_view.getFocus().id, .{ .diff_view = diff_view }); 437 }, 438 .commits => |*commits| { 439 var commits_view = try Commits.View.init(allocator, commits, session); 440 errdefer commits_view.deinit(allocator); 441 try stack.children.put(allocator, commits_view.getFocus().id, .{ .repo_commits = commits_view }); 442 }, 443 } 444 445 // refs — the repo's branches and tags. 446 { 447 var refs_view = try Refs.View.init(allocator, &data.refs, session); 448 errdefer refs_view.deinit(allocator); 449 try stack.children.put(allocator, refs_view.getFocus().id, .{ .repo_refs = refs_view }); 450 } 451 452 // discussions and their comment permalinks. 453 if (data.repo.event.discuss_role != null) { 454 var discussions_view = try Discussions.View.init(allocator, &data.discussions, session); 455 errdefer discussions_view.deinit(allocator); 456 try stack.children.put(allocator, discussions_view.getFocus().id, .{ .repo_discussions = discussions_view }); 457 } 458 459 // issues — the repo's issue tracker and comment permalinks. 460 if (data.repo.event.issue_role != null) { 461 var issues_view = try Issues.View.init(allocator, &data.issues, session); 462 errdefer issues_view.deinit(allocator); 463 try stack.children.put(allocator, issues_view.getFocus().id, .{ .repo_issues = issues_view }); 464 } 465 466 // patches and their comment permalinks. 467 if (data.repo.event.patch_role != null) { 468 var patches_view = try Patches.View.init(allocator, &data.patches, session); 469 errdefer patches_view.deinit(allocator); 470 try stack.children.put(allocator, patches_view.getFocus().id, .{ .repo_patches = patches_view }); 471 } 472 473 if (data.handle.canWrite()) { 474 var events_view = try Events.View.init(allocator, &data.events, session); 475 errdefer events_view.deinit(allocator); 476 try stack.children.put(allocator, events_view.getFocus().id, .{ .repo_events = events_view }); 477 } 478 479 if (data.undo) |*undo| { 480 var undo_view = try Undo.View.init(allocator, undo, session); 481 errdefer undo_view.deinit(allocator); 482 try stack.children.put(allocator, undo_view.getFocus().id, .{ .repo_undo = undo_view }); 483 } 484 485 if (data.settings) |*settings| { 486 var settings_view = try Settings.View.init(allocator, settings, session); 487 errdefer settings_view.deinit(allocator); 488 try stack.children.put(allocator, settings_view.getFocus().id, .{ .repo_settings = settings_view }); 489 } 490 491 // the header shows new repo with a login, and new user without one 492 // and user only outside local mode, so keep the stack's children 493 // 1:1 with the tabs by skipping the same views. 494 const identity = try session.page_arena.allocator().print("{s}:{s}", .{ data.header.owner_name, data.header.name }); 495 if (session.data.user_id != null) { 496 const route = ui.RoutablePage{ .repo_repo_new = ui.RoutablePage.Array(ui.RoutablePage.repo_route_max_len).from(identity) orelse return error.RouteTooLong }; 497 var new_repo_view = try ui.NewRepo.View.init(allocator, session, route); 498 errdefer new_repo_view.deinit(allocator); 499 try stack.children.put(allocator, new_repo_view.getFocus().id, .{ .new_repo = new_repo_view }); 500 } else if (session.data.host_kind == .server) { 501 const route = ui.RoutablePage{ .repo_user_new = ui.RoutablePage.Array(ui.RoutablePage.repo_route_max_len).from(identity) orelse return error.RouteTooLong }; 502 var new_user_view = try ui.NewUser.View.init(allocator, session, route); 503 errdefer new_user_view.deinit(allocator); 504 try stack.children.put(allocator, new_user_view.getFocus().id, .{ .new_user = new_user_view }); 505 } 506 507 if (session.data.host_kind == .server) { 508 var user_view = try ui.UserLogout.initView(allocator, session); 509 errdefer user_view.deinit(allocator); 510 try stack.children.put(allocator, user_view.getFocus().id, user_view); 511 } 512 513 if (session.is_terminal) { 514 var quit_view = try Quit.View.init(allocator, session); 515 errdefer quit_view.deinit(allocator); 516 try stack.children.put(allocator, quit_view.getFocus().id, .{ .quit = quit_view }); 517 } 518 519 try box.children.put(allocator, stack.getFocus().id, .{ .widget = .{ .stack = stack }, .rect = null, .min_size = null }); 520 } 521 522 var self = View{ .box = box }; 523 // a page opens on its tabs, except that search results open in the 524 // tab's own search box. 525 const results = data.files.find != null or 526 (data.changes == .commits and data.changes.commits.search != null) or 527 data.discussions.search != null or data.issues.search != null or data.patches.search != null; 528 self.getFocus().child_id = box.children.keys()[if (results) stack_index else header_index]; 529 return self; 530 } 531 532 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 533 self.box.deinit(allocator); 534 } 535 536 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 537 self.clearGrid(); 538 const header = &self.box.children.values()[header_index].widget.repo_header; 539 const stack = &self.box.children.values()[stack_index].widget.stack; 540 541 // each header tab maps 1:1 to a stack child by position 542 if (header.getSelectedIndex()) |index| 543 stack.getFocus().child_id = stack.children.keys()[index]; 544 try self.box.build(allocator, constraint, root_focus); 545 } 546 547 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 548 const stack = &self.box.children.values()[stack_index].widget.stack; 549 if (self.getFocus().child_id) |child_id| { 550 if (self.box.children.getIndex(child_id)) |current_index| { 551 const child = &self.box.children.values()[current_index].widget; 552 var index = current_index; 553 554 const direction = inp.vertDirection(key); 555 556 switch (direction) { 557 .up => { 558 switch (child.*) { 559 .repo_header => { 560 try child.input(allocator, key, root_focus); 561 }, 562 .stack => { 563 if (child.stack.getSelected()) |selected_widget| { 564 if (selected_widget.atTop(root_focus)) { 565 index = header_index; 566 } else { 567 try child.input(allocator, key, root_focus); 568 } 569 } 570 }, 571 else => {}, 572 } 573 }, 574 .down => { 575 switch (child.*) { 576 .repo_header => { 577 if (stack.getSelected()) |selected_widget| switch (selected_widget.*) { 578 .repo_files => |*v| if (v.focusHeader(root_focus)) return, 579 .repo_commits => |*v| if (v.focusHeader(root_focus)) return, 580 .repo_refs => |*v| if (v.focusHeader(root_focus)) return, 581 .repo_events => |*v| if (v.focusHeader(root_focus)) return, 582 .repo_undo => |*v| if (v.focusHeader(root_focus)) return, 583 else => {}, 584 }; 585 index = stack_index; 586 }, 587 .stack => { 588 try child.input(allocator, key, root_focus); 589 }, 590 else => {}, 591 } 592 }, 593 .none => { 594 try child.input(allocator, key, root_focus); 595 }, 596 } 597 598 if (index != current_index) { 599 root_focus.setFocus(self.box.children.keys()[index]); 600 } 601 } 602 } 603 } 604 605 pub fn clearGrid(self: *View) void { 606 self.box.clearGrid(); 607 } 608 609 pub fn getGrid(self: View) ?Grid { 610 return self.box.getGrid(); 611 } 612 613 pub fn getFocus(self: *View) *Focus { 614 return self.box.getFocus(); 615 } 616 }; 617