╭ Repo.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1 const std = @import("std"); 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 Issues = @import("./Repo/Issues.zig"); 22 pub const Patches = @import("./Repo/Patches.zig"); 23 pub const Discussions = @import("./Repo/Discussions.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 Quit = @import("./Quit.zig"); 28 29 header: Header, 30 repo: evt.Repo.Record, 31 handle: ui.RepoHandle, 32 files: Files, 33 changes: Changes, 34 refs: Refs, 35 issues: Issues, 36 patches: Patches, 37 discussions: Discussions, 38 events: Events, 39 undo: ?Undo = null, 40 quit: Quit, 41 42 const Self = @This(); 43 44 const Changes = union(enum) { 45 commits: Commits, 46 diff: Diff, 47 }; 48 49 pub fn init( 50 arena: *std.heap.ArenaAllocator, 51 session: *ui.Session, 52 route: ui.RoutablePage, 53 ) !Self { 54 const DB = evt.AdminDB; 55 const hash_kind = evt.admin_repo_opts.hash; 56 57 // every repo route stores its identity as "owner/name" (or elides it in local mode) 58 const name_str = route.repoIdentity() orelse return error.UnexpectedRoute; 59 const repo_identity = ui.RoutablePage.RepoIdentity.parse(name_str) orelse return error.NotFound; 60 // files, commits and diff share the requested ref, or the default branch. 61 // directories and hunk windows only apply to their own tab. 62 const requested_ref_or_oid: ?ui.RoutablePage.RefOrOid = switch (route) { 63 .repo_files => |f| f.ref_kind, 64 .repo_commits => |c| c.ref_or_oid, 65 .repo_diff => |d| d.ref_or_oid, 66 else => null, 67 }; 68 const requested_ref_value: []const u8 = switch (route) { 69 .repo_files => |*f| f.ref_value.slice(), 70 .repo_commits => |*c| c.value.slice(), 71 .repo_diff => |*d| d.value.slice(), 72 else => "", 73 }; 74 const patchrev_id: []const u8 = switch (route) { 75 .repo_files => |*f| f.patchrev_id.slice(), 76 .repo_diff => |*d| d.patchrev_id.slice(), 77 else => "", 78 }; 79 const files_dir = switch (route) { 80 .repo_files => |*f| f.path.slice(), 81 else => "", 82 }; 83 const files_line = switch (route) { 84 .repo_files => |f| f.line, 85 else => 0, 86 }; 87 const files_find: []const u8 = switch (route) { 88 .repo_files => |*f| f.find.slice(), 89 else => "", 90 }; 91 // the commits view's pane shows the whole message of the commit it walks from 92 const commits_message = route == .repo_commits and route.repo_commits.message; 93 const commits_base_oid: []const u8 = switch (route) { 94 .repo_commits => |*c| c.base_oid.slice(), 95 else => "", 96 }; 97 const commits_search: []const u8 = switch (route) { 98 .repo_commits => |*c| c.search.slice(), 99 else => "", 100 }; 101 const commits_from: []const u8 = switch (route) { 102 .repo_commits => |*c| c.from.slice(), 103 else => "", 104 }; 105 const commits_merge: []const u8 = switch (route) { 106 .repo_commits => |*c| c.merge.slice(), 107 else => "", 108 }; 109 // the refs tab windows one column at a time: `refs_from` (a url-encoded 110 // ref name) roots `refs_kind`'s column, the other stays at its first window. 111 const refs_kind: ui.RoutablePage.RefKind = switch (route) { 112 .repo_refs => |r| r.kind, 113 else => .branch, 114 }; 115 const refs_from: []const u8 = switch (route) { 116 .repo_refs => |*r| r.from.slice(), 117 else => "", 118 }; 119 const refs_search: []const u8 = switch (route) { 120 .repo_refs => |*r| r.search.slice(), 121 else => "", 122 }; 123 // the issues tab's label filter, the issue its window is rooted at, and the 124 // view it shows. 125 const issues_label: []const u8 = switch (route) { 126 .repo_issues => |*i| i.label.slice(), 127 else => "", 128 }; 129 const issues_search: []const u8 = switch (route) { 130 .repo_issues => |*i| i.search.slice(), 131 else => "", 132 }; 133 const issues_selected: []const u8 = switch (route) { 134 .repo_issues => |*i| i.selected.slice(), 135 else => "", 136 }; 137 const issues_comment: []const u8 = switch (route) { 138 .repo_issues => |*i| i.comment.slice(), 139 else => "", 140 }; 141 const issues_theirs: []const u8 = switch (route) { 142 .repo_issues => |*i| i.theirs.slice(), 143 else => "", 144 }; 145 const issues_view: ui.RoutablePage.IssuesView = switch (route) { 146 .repo_issues => |i| i.view, 147 else => .open, 148 }; 149 const issues_comments_start: usize = switch (route) { 150 .repo_issues => |i| i.comments_start, 151 else => 0, 152 }; 153 const patches_label: []const u8 = switch (route) { 154 .repo_patches => |*p| p.label.slice(), 155 else => "", 156 }; 157 const patches_search: []const u8 = switch (route) { 158 .repo_patches => |*p| p.search.slice(), 159 else => "", 160 }; 161 const patches_selected: []const u8 = switch (route) { 162 .repo_patches => |*p| p.selected.slice(), 163 else => "", 164 }; 165 const patches_comment: []const u8 = switch (route) { 166 .repo_patches => |*p| p.comment.slice(), 167 else => "", 168 }; 169 const patches_theirs: []const u8 = switch (route) { 170 .repo_patches => |*p| p.theirs.slice(), 171 else => "", 172 }; 173 const patches_view: ui.RoutablePage.PatchesView = switch (route) { 174 .repo_patches => |p| p.view, 175 else => .open, 176 }; 177 const patches_comments_start: usize = switch (route) { 178 .repo_patches => |p| p.comments_start, 179 else => 0, 180 }; 181 const discussions_label: []const u8 = switch (route) { 182 .repo_discussions => |*t| t.label.slice(), 183 else => "", 184 }; 185 const discussions_search: []const u8 = switch (route) { 186 .repo_discussions => |*t| t.search.slice(), 187 else => "", 188 }; 189 const discussions_selected: []const u8 = switch (route) { 190 .repo_discussions => |*t| t.selected.slice(), 191 else => "", 192 }; 193 const discussions_comment: []const u8 = switch (route) { 194 .repo_discussions => |*t| t.comment.slice(), 195 else => "", 196 }; 197 const discussions_view: ui.RoutablePage.DiscussionsView = switch (route) { 198 .repo_discussions => |t| t.view, 199 else => .recent, 200 }; 201 const discussions_comments_start: usize = switch (route) { 202 .repo_discussions => |t| t.comments_start, 203 else => 0, 204 }; 205 const events_kind: ?evt.EventKind = switch (route) { 206 .repo_events => |e| e.kind, 207 else => null, 208 }; 209 const events_view: ui.RoutablePage.EventsView = switch (route) { 210 .repo_events => |e| e.view, 211 else => .active, 212 }; 213 const events_moment: ?u64 = switch (route) { 214 .repo_events => |e| e.moment, 215 else => null, 216 }; 217 const events_selected: []const u8 = switch (route) { 218 .repo_events => |*e| e.selected.slice(), 219 else => "", 220 }; 221 222 // where the on-disk repo lives (null keeps the views' empty fallback), plus 223 // the repo and owner-name metadata the header shows. local mode already 224 // knows all three; the server paths resolve them from the admin db. 225 var source: ?ui.RepoSource = null; 226 var repo_id_maybe: ?[evt.event_id_size]u8 = null; 227 var repo: evt.Repo.Record = undefined; 228 // local mode owns its repo 229 var role: evt.Repo.Role = .owner; 230 var owner_name: []const u8 = undefined; 231 if (session.local) |local| { 232 source = local; 233 // local routes elide the identity, so the display name comes from the 234 // repo's directory rather than the route. 235 repo = .{ .event = .{ 236 .user_id = "", 237 .name = try arena.allocator().dupe(u8, std.fs.path.basename(local.path)), 238 .description = "", 239 } }; 240 owner_name = ""; 241 } else { 242 const haxy_moment = session.haxy_moment orelse return error.NoMoment; 243 const io = session.io orelse return error.NoMoment; 244 const users_dir = session.users_dir orelse return error.NoMoment; 245 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; 246 // a repo the session can't read doesn't exist to it 247 if (found.role == .none) return error.NotFound; 248 repo = found.repo; 249 role = found.role; 250 repo_id_maybe = found.event_id; 251 252 // resolve the creating user so the header can show their name to the left 253 // of the repo title. 254 const owner = (try evt.User.readById(DB, hash_kind, haxy_moment, arena, repo.event.user_id)) orelse return error.NotFound; 255 owner_name = owner.event.name; 256 257 // the repo's working copy lives in its owner's dir. 258 source = .{ 259 .path = try evt.repoPath(arena.allocator(), users_dir, repo.event.user_id, &found.event_id), 260 .repo_kind = .xit, 261 }; 262 } 263 264 const handle = ui.RepoHandle{ 265 .location = .{ .repo = try arena.allocator().dupe(u8, repo_identity.identity) }, 266 .viewer = try ui.Viewer.init(session, arena, role), 267 }; 268 269 // open the repo once for every tab. files and changes share a ref or revision. 270 // no filesystem (wasm), nowhere to look, or a failed open: empty tabs. 271 // the events tab needs write access, like undo 272 if (route == .repo_events and !handle.canWrite()) return error.NotFound; 273 const undo_index = if (route == .repo_undo) route.repo_undo.index else null; 274 const undo_clear = route == .repo_undo and route.repo_undo.clear; 275 // only xit repos have undo history, so it's read in their branch below 276 var undo_data: ?Undo = null; 277 const files, const changes, const refs, var issues, var patches, var discussions, const events = blk: { 278 read: { 279 const io = session.io orelse break :read; 280 const src = source orelse break :read; 281 const gpa = arena.child_allocator; 282 switch (src.repo_kind) { 283 inline else => |repo_kind| { 284 var any_repo = rp.AnyRepo(repo_kind, .{}).open(io, gpa, src.localInitOpts()) catch break :read; 285 defer any_repo.deinit(io, gpa); 286 switch (any_repo) { 287 inline else => |*opened| { 288 // local mode: bring the event db up to date with the events branch 289 if (session.local != null) { 290 try evt.consume(.local, .repo, repo_kind, opened.self_repo_opts, io, gpa, opened, evt.events_ref, &.{}); 291 try pch.refreshBranches(.local, repo_kind, opened.self_repo_opts, io, gpa, opened, null, null); 292 } 293 // tabs switch in-page, so every tab's data is read here 294 if (repo_kind == .xit and handle.canWrite()) { 295 undo_data = Undo.init(opened.self_repo_opts, arena, opened, session.haxy_moment, repo_identity.identity, undo_index, handle) catch |err| switch (err) { 296 error.OutOfMemory => return err, 297 else => .{ .identity = repo_identity.identity, .handle = handle, .failure = @errorName(err) }, 298 }; 299 } 300 // a merge: route stands for the log of what the merge brought in 301 const ref_or_oid: ?ui.RoutablePage.RefOrOid, const ref_value, const base_oid = if (commits_merge.len == 0) 302 .{ requested_ref_or_oid, requested_ref_value, commits_base_oid } 303 else merged: { 304 const merged = try Commits.resolveMerge(repo_kind, opened.self_repo_opts, arena, opened, io, gpa, commits_merge); 305 break :merged .{ .object, merged.oid, merged.base_oid }; 306 }; 307 const files_data = if (patchrev_id.len != 0) 308 try Files.initPatchRev(repo_kind, opened.self_repo_opts, arena, opened, io, gpa, handle, patchrev_id, files_dir, files_line) 309 else 310 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); 311 const changes_data: Changes = if (route == .repo_diff) 312 .{ .diff = try Diff.init(repo_kind, opened.self_repo_opts, arena, opened, io, gpa, route.repo_diff) } 313 else if (patchrev_id.len != 0) diff: { 314 const diff_route = ui.RoutablePage.repoPatchRevDiffRoute(repo_identity.identity, patchrev_id, 0, "") orelse return error.RouteTooLong; 315 break :diff .{ .diff = try Diff.init(repo_kind, opened.self_repo_opts, arena, opened, io, gpa, diff_route.repo_diff) }; 316 } 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) }; 317 const target_branch = if (files_data.ref_or_oid == .branch) files_data.ref_or_oid_value else ""; 318 break :blk .{ 319 files_data, 320 changes_data, 321 try Refs.init(repo_kind, opened.self_repo_opts, arena, opened, io, gpa, repo_identity.identity, refs_kind, refs_from, refs_search), 322 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, handle.viewer), 323 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, handle.viewer), 324 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, handle.viewer), 325 try Events.init(repo_kind, opened.self_repo_opts, arena, opened, io, session.haxy_moment, repo_identity.identity, events_view, events_kind, events_selected, events_moment, session.local != null, session.data.sync_failure), 326 }; 327 }, 328 } 329 }, 330 } 331 } 332 const aa = arena.allocator(); 333 break :blk .{ 334 try Files.emptyResult(aa, handle, requested_ref_or_oid orelse .branch, requested_ref_value, files_dir), 335 Changes{ .commits = try Commits.emptyResult(aa, handle, requested_ref_or_oid orelse .branch, requested_ref_value, commits_base_oid) }, 336 try Refs.emptyResult(arena, repo_identity.identity, refs_kind, refs_from, refs_search), 337 try Issues.emptyResult(aa, repo_identity.identity, issues_label, issues_search, issues_selected, issues_comment, issues_comments_start, issues_theirs, issues_view), 338 try Patches.emptyResult(aa, repo_identity.identity, patches_label, patches_search, patches_selected, patches_comment, patches_comments_start, patches_theirs, patches_view), 339 try Discussions.emptyResult(aa, repo_identity.identity, discussions_label, discussions_search, discussions_selected, discussions_comment, discussions_comments_start, discussions_view), 340 try Events.empty(aa, repo_identity.identity, events_view, session.local != null, session.data.sync_failure), 341 }; 342 }; 343 if (route == .repo_undo and undo_data == null) return error.NotFound; 344 if (undo_data) |*undo| undo.clear = undo_clear; 345 issues.repo_source = source; 346 patches.repo_source = source; 347 patches.repo_id = repo_id_maybe; 348 discussions.repo_source = source; 349 350 return .{ 351 // use the files tab's resolved ref for the header 352 .header = try Header.init(arena, repo.event.name, owner_name, files.ref_or_oid, files.ref_or_oid_value, issues.label, patches.label, discussions.label), 353 .repo = repo, 354 .handle = handle, 355 .files = files, 356 .changes = changes, 357 .refs = refs, 358 .issues = issues, 359 .patches = patches, 360 .discussions = discussions, 361 .events = events, 362 .undo = undo_data, 363 .quit = Quit.init(), 364 }; 365 } 366 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 user 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 user 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_repo_new = 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_user_new = 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 user_view = try ui.UserLogout.initView(allocator, session); 467 errdefer user_view.deinit(allocator); 468 try stack.children.put(allocator, user_view.getFocus().id, user_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