╭ 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 Settings = @import("./Settings.zig"); 28 pub const Auth = @import("./Auth.zig"); 29 pub const Quit = @import("./Quit.zig"); 30 31 header: Header, 32 repo: evt.Repo.Record, 33 files: Files, 34 changes: Changes, 35 refs: Refs, 36 issues: Issues, 37 patches: Patches, 38 discussions: Discussions, 39 events: Events, 40 undo: ?Undo = null, 41 settings: Settings, 42 auth: Auth, 43 quit: Quit, 44 45 const Self = @This(); 46 47 const Changes = union(enum) { 48 commits: Commits, 49 diff: Diff, 50 }; 51 52 pub fn init( 53 arena: *std.heap.ArenaAllocator, 54 session: *ui.Session, 55 route: ui.RoutablePage, 56 ) !Self { 57 const DB = evt.AdminDB; 58 const hash_kind = evt.admin_repo_opts.hash; 59 60 // every repo route stores its identity as "owner/name" (or elides it in local mode) 61 const name_str = route.repoIdentity() orelse return error.UnexpectedRoute; 62 const repo_identity = ui.RoutablePage.RepoIdentity.parse(name_str) orelse return error.NotFound; 63 const location = ui.RoutablePage.RepoLocation{ .repo = repo_identity.identity }; 64 // files, commits and diff share the requested ref, or the default branch. 65 // directories and hunk windows only apply to their own tab. 66 const requested_ref_or_oid: ?ui.RoutablePage.RefOrOid = switch (route) { 67 .repo_files => |f| f.ref_kind, 68 .repo_commits => |c| c.ref_or_oid, 69 .repo_diff => |d| d.ref_or_oid, 70 else => null, 71 }; 72 const requested_ref_value: []const u8 = switch (route) { 73 .repo_files => |*f| f.ref_value.slice(), 74 .repo_commits => |*c| c.value.slice(), 75 .repo_diff => |*d| d.value.slice(), 76 else => "", 77 }; 78 const patchrev_id: []const u8 = switch (route) { 79 .repo_files => |*f| f.patchrev_id.slice(), 80 .repo_diff => |*d| d.patchrev_id.slice(), 81 else => "", 82 }; 83 const files_dir = switch (route) { 84 .repo_files => |*f| f.path.slice(), 85 else => "", 86 }; 87 const files_line = switch (route) { 88 .repo_files => |f| f.line, 89 else => 0, 90 }; 91 const files_find: []const u8 = switch (route) { 92 .repo_files => |*f| f.find.slice(), 93 else => "", 94 }; 95 // what the commits view's pane shows for the commit it walks from: a diff 96 // window (with the file it's filtered to) or that commit's message. 97 const commits_content: ui.RoutablePage.RepoCommitsRoute.Content = switch (route) { 98 .repo_commits => |c| c.content, 99 else => .{ .diff = .{} }, 100 }; 101 const commits_base_oid: []const u8 = switch (route) { 102 .repo_commits => |*c| c.base_oid.slice(), 103 else => "", 104 }; 105 const commits_search: []const u8 = switch (route) { 106 .repo_commits => |*c| c.search.slice(), 107 else => "", 108 }; 109 const commits_from: []const u8 = switch (route) { 110 .repo_commits => |*c| c.from.slice(), 111 else => "", 112 }; 113 const commits_merge: []const u8 = switch (route) { 114 .repo_commits => |*c| c.merge.slice(), 115 else => "", 116 }; 117 // the refs tab windows one column at a time: `refs_from` (a url-encoded 118 // ref name) roots `refs_kind`'s column, the other stays at its first window. 119 const refs_kind: ui.RoutablePage.RefKind = switch (route) { 120 .repo_refs => |r| r.kind, 121 else => .branch, 122 }; 123 const refs_from: []const u8 = switch (route) { 124 .repo_refs => |*r| r.from.slice(), 125 else => "", 126 }; 127 const refs_search: []const u8 = switch (route) { 128 .repo_refs => |*r| r.search.slice(), 129 else => "", 130 }; 131 // the issues tab's label filter, the issue its window is rooted at, and the 132 // view it shows. 133 const issues_label: []const u8 = switch (route) { 134 .repo_issues => |*i| i.label.slice(), 135 else => "", 136 }; 137 const issues_search: []const u8 = switch (route) { 138 .repo_issues => |*i| i.search.slice(), 139 else => "", 140 }; 141 const issues_selected: []const u8 = switch (route) { 142 .repo_issues => |*i| i.selected.slice(), 143 else => "", 144 }; 145 const issues_comment: []const u8 = switch (route) { 146 .repo_issues => |*i| i.comment.slice(), 147 else => "", 148 }; 149 const issues_theirs: []const u8 = switch (route) { 150 .repo_issues => |*i| i.theirs.slice(), 151 else => "", 152 }; 153 const issues_view: ui.RoutablePage.IssuesView = switch (route) { 154 .repo_issues => |i| i.view, 155 else => .open, 156 }; 157 const issues_comments_start: usize = switch (route) { 158 .repo_issues => |i| i.comments_start, 159 else => 0, 160 }; 161 const patches_label: []const u8 = switch (route) { 162 .repo_patches => |*p| p.label.slice(), 163 else => "", 164 }; 165 const patches_search: []const u8 = switch (route) { 166 .repo_patches => |*p| p.search.slice(), 167 else => "", 168 }; 169 const patches_selected: []const u8 = switch (route) { 170 .repo_patches => |*p| p.selected.slice(), 171 else => "", 172 }; 173 const patches_comment: []const u8 = switch (route) { 174 .repo_patches => |*p| p.comment.slice(), 175 else => "", 176 }; 177 const patches_theirs: []const u8 = switch (route) { 178 .repo_patches => |*p| p.theirs.slice(), 179 else => "", 180 }; 181 const patches_view: ui.RoutablePage.PatchesView = switch (route) { 182 .repo_patches => |p| p.view, 183 else => .open, 184 }; 185 const patches_comments_start: usize = switch (route) { 186 .repo_patches => |p| p.comments_start, 187 else => 0, 188 }; 189 const discussions_label: []const u8 = switch (route) { 190 .repo_discussions => |*t| t.label.slice(), 191 else => "", 192 }; 193 const discussions_search: []const u8 = switch (route) { 194 .repo_discussions => |*t| t.search.slice(), 195 else => "", 196 }; 197 const discussions_selected: []const u8 = switch (route) { 198 .repo_discussions => |*t| t.selected.slice(), 199 else => "", 200 }; 201 const discussions_comment: []const u8 = switch (route) { 202 .repo_discussions => |*t| t.comment.slice(), 203 else => "", 204 }; 205 const discussions_view: ui.RoutablePage.DiscussionsView = switch (route) { 206 .repo_discussions => |t| t.view, 207 else => .recent, 208 }; 209 const discussions_comments_start: usize = switch (route) { 210 .repo_discussions => |t| t.comments_start, 211 else => 0, 212 }; 213 const events_kind: ?evt.EventKind = switch (route) { 214 .repo_events => |e| e.kind, 215 else => null, 216 }; 217 const events_view: ui.RoutablePage.EventsView = switch (route) { 218 .repo_events => |e| e.view, 219 else => .active, 220 }; 221 const events_moment: ?u64 = switch (route) { 222 .repo_events => |e| e.moment, 223 else => null, 224 }; 225 const events_selected: []const u8 = switch (route) { 226 .repo_events => |*e| e.selected.slice(), 227 else => "", 228 }; 229 230 // where the on-disk repo lives (null keeps the views' empty fallback), plus 231 // the repo and owner-name metadata the header shows. local mode already 232 // knows all three; the server paths resolve them from the admin db. 233 var source: ?ui.RepoSource = null; 234 var repo_id_maybe: ?[evt.event_id_size]u8 = null; 235 var repo: evt.Repo.Record = undefined; 236 var owner_name: []const u8 = undefined; 237 if (session.local) |local| { 238 source = local; 239 // local routes elide the identity, so the display name comes from the 240 // repo's directory rather than the route. 241 repo = .{ .event = .{ 242 .user_id = "", 243 .name = try arena.allocator().dupe(u8, std.fs.path.basename(local.path)), 244 .description = "", 245 } }; 246 owner_name = ""; 247 } else { 248 const haxy_moment = session.haxy_moment orelse return error.NoMoment; 249 const io = session.io orelse return error.NoMoment; 250 const repos_dir = session.repos_dir orelse return error.NoMoment; 251 const found = (try evt.readRepoByOwnerAndName(io, arena.child_allocator, arena, haxy_moment, repos_dir, repo_identity.owner, repo_identity.name)) orelse return error.NotFound; 252 // a repo the session can't read doesn't exist to it 253 if (evt.Repo.roleOf(found.repo, session.userId()) == .none) return error.NotFound; 254 repo = found.repo; 255 repo_id_maybe = found.event_id; 256 257 // resolve the creating user so the header can show their name to the left 258 // of the repo title. 259 const owner = (try evt.User.readById(DB, hash_kind, haxy_moment, arena, repo.event.user_id)) orelse return error.NotFound; 260 owner_name = owner.event.name; 261 262 // the repo's working copy lives at <repos_dir>/<hex event id>. 263 const hex = std.fmt.bytesToHex(found.event_id, .lower); 264 source = .{ 265 .path = try std.fs.path.join(arena.allocator(), &.{ repos_dir, &hex }), 266 .repo_kind = .xit, 267 }; 268 } 269 270 // open the repo once for every tab. files and changes share a ref or revision. 271 // no filesystem (wasm), nowhere to look, or a failed open: empty tabs. 272 const undo_allowed = if (session.local) |local| local.repo_kind == .xit else session.userId() != null and evt.Repo.roleOf(repo, session.userId()).atLeast(.write); 273 if (route == .repo_undo and !undo_allowed) return error.NotFound; 274 // a server shows events to whoever it shows undo to, so the route follows 275 if (route == .repo_events and session.data.host_kind != .local and !undo_allowed) return error.NotFound; 276 const undo_index = if (route == .repo_undo) route.repo_undo.index else null; 277 const undo_clear = route == .repo_undo and route.repo_undo.clear; 278 // an unreadable repo still shows the tab, like every other tab 279 var undo_data: ?Undo = if (undo_allowed) .{ .identity = repo_identity.identity } else null; 280 const files, const changes, const refs, var issues, var patches, var discussions, const events = blk: { 281 read: { 282 const io = session.io orelse break :read; 283 const src = source orelse break :read; 284 const gpa = arena.child_allocator; 285 switch (src.repo_kind) { 286 inline else => |repo_kind| { 287 var any_repo = rp.AnyRepo(repo_kind, .{}).open(io, gpa, src.localInitOpts()) catch break :read; 288 defer any_repo.deinit(io, gpa); 289 switch (any_repo) { 290 inline else => |*opened| { 291 // local mode: bring the event db up to date with the events branch 292 if (session.local != null) { 293 try evt.consume(.local, .repo, repo_kind, opened.self_repo_opts, io, gpa, opened, evt.events_ref, &.{}); 294 try pch.refreshBranches(.local, repo_kind, opened.self_repo_opts, io, gpa, opened, null, null); 295 } 296 // tabs switch in-page, so every tab's data is read here 297 if (repo_kind == .xit and undo_allowed) { 298 undo_data = Undo.init(opened.self_repo_opts, arena, opened, session.haxy_moment, repo_identity.identity, undo_index) catch |err| switch (err) { 299 error.OutOfMemory => return err, 300 else => .{ .identity = repo_identity.identity, .failure = @errorName(err) }, 301 }; 302 } 303 // a merge: route stands for the log of what the merge brought in 304 const ref_or_oid: ?ui.RoutablePage.RefOrOid, const ref_value, const base_oid = if (commits_merge.len == 0) 305 .{ requested_ref_or_oid, requested_ref_value, commits_base_oid } 306 else merged: { 307 const merged = try Commits.resolveMerge(repo_kind, opened.self_repo_opts, arena, opened, io, gpa, commits_merge); 308 break :merged .{ .object, merged.oid, merged.base_oid }; 309 }; 310 const files_data = if (patchrev_id.len != 0) 311 try Files.initPatchRev(repo_kind, opened.self_repo_opts, arena, opened, io, gpa, location, patchrev_id, files_dir, files_line) 312 else 313 try Files.init(repo_kind, opened.self_repo_opts, arena, opened, io, gpa, location, ref_or_oid, ref_value, files_dir, files_line, files_find); 314 const changes_data: Changes = if (route == .repo_diff) 315 .{ .diff = try Diff.init(repo_kind, opened.self_repo_opts, arena, opened, io, gpa, route.repo_diff) } 316 else if (patchrev_id.len != 0) diff: { 317 const diff_route = ui.RoutablePage.repoPatchRevDiffRoute(repo_identity.identity, patchrev_id, 0, "") orelse return error.RouteTooLong; 318 break :diff .{ .diff = try Diff.init(repo_kind, opened.self_repo_opts, arena, opened, io, gpa, diff_route.repo_diff) }; 319 } else .{ .commits = try Commits.init(repo_kind, opened.self_repo_opts, arena, opened, io, gpa, session.haxy_moment, location, ref_or_oid, ref_value, commits_content, base_oid, commits_search, commits_from) }; 320 const target_branch = if (files_data.ref_or_oid == .branch) files_data.ref_or_oid_value else ""; 321 break :blk .{ 322 files_data, 323 changes_data, 324 try Refs.init(repo_kind, opened.self_repo_opts, arena, opened, io, gpa, repo_identity.identity, refs_kind, refs_from, refs_search), 325 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), 326 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), 327 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), 328 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), 329 }; 330 }, 331 } 332 }, 333 } 334 } 335 const aa = arena.allocator(); 336 break :blk .{ 337 try Files.emptyResult(aa, location, requested_ref_or_oid orelse .branch, requested_ref_value, files_dir), 338 Changes{ .commits = try Commits.emptyResult(aa, location, requested_ref_or_oid orelse .branch, requested_ref_value, commits_content, commits_base_oid) }, 339 try Refs.emptyResult(arena, repo_identity.identity, refs_kind, refs_from, refs_search), 340 try Issues.emptyResult(aa, repo_identity.identity, issues_label, issues_search, issues_selected, issues_comment, issues_comments_start, issues_theirs, issues_view), 341 try Patches.emptyResult(aa, repo_identity.identity, patches_label, patches_search, patches_selected, patches_comment, patches_comments_start, patches_theirs, patches_view), 342 try Discussions.emptyResult(aa, repo_identity.identity, discussions_label, discussions_search, discussions_selected, discussions_comment, discussions_comments_start, discussions_view), 343 try Events.empty(aa, repo_identity.identity, events_view, session.local != null, session.data.sync_failure), 344 }; 345 }; 346 if (undo_data) |*undo| { 347 undo.can_undo = session.local != null or evt.Repo.roleOf(repo, session.userId()) == .owner; 348 undo.clear = undo_clear; 349 } 350 if (route == .repo_undo and undo_data == null) return error.NotFound; 351 issues.repo_source = source; 352 patches.repo_source = source; 353 patches.repo_id = repo_id_maybe; 354 discussions.repo_source = source; 355 356 return .{ 357 // use the files tab's resolved ref for the header 358 .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), 359 .repo = repo, 360 .files = files, 361 .changes = changes, 362 .refs = refs, 363 .issues = issues, 364 .patches = patches, 365 .discussions = discussions, 366 .events = events, 367 .undo = undo_data, 368 .settings = Settings.init(), 369 .auth = Auth.init(), 370 .quit = Quit.init(), 371 }; 372 } 373 374 pub const View = struct { 375 box: wgt.Box(ui.Widget), 376 377 const header_index: usize = 0; 378 const stack_index: usize = 1; 379 380 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { 381 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .round_corners = true, .direction = .vert }); 382 errdefer box.deinit(allocator); 383 384 // build the header first so we can grab the files-tab id for the auth 385 // view (it focuses there after login). 386 { 387 var header_view = try Header.View.init(allocator, data, session); 388 errdefer header_view.deinit(allocator); 389 try box.children.put(allocator, header_view.getFocus().id, .{ .widget = .{ .repo_header = header_view }, .rect = null, .min_size = null }); 390 } 391 392 { 393 var stack = try wgt.Stack(ui.Widget).init(allocator); 394 errdefer stack.deinit(allocator); 395 396 // files — the default tab: the current directory's listing. 397 { 398 var files_view = try Files.View.init(allocator, &data.files, session); 399 errdefer files_view.deinit(allocator); 400 try stack.children.put(allocator, files_view.getFocus().id, .{ .repo_files = files_view }); 401 } 402 403 switch (data.changes) { 404 .diff => |*diff| { 405 var diff_view = try Diff.View.init(allocator, diff, session); 406 errdefer diff_view.deinit(allocator); 407 try stack.children.put(allocator, diff_view.getFocus().id, .{ .diff_view = diff_view }); 408 }, 409 .commits => |*commits| { 410 var commits_view = try Commits.View.init(allocator, commits, session); 411 errdefer commits_view.deinit(allocator); 412 try stack.children.put(allocator, commits_view.getFocus().id, .{ .repo_commits = commits_view }); 413 }, 414 } 415 416 // refs — the repo's branches and tags. 417 { 418 var refs_view = try Refs.View.init(allocator, &data.refs, session); 419 errdefer refs_view.deinit(allocator); 420 try stack.children.put(allocator, refs_view.getFocus().id, .{ .repo_refs = refs_view }); 421 } 422 423 // issues — the repo's issue tracker and comment permalinks. 424 { 425 var issues_view = try Issues.View.init(allocator, &data.issues, session); 426 errdefer issues_view.deinit(allocator); 427 try stack.children.put(allocator, issues_view.getFocus().id, .{ .repo_issues = issues_view }); 428 } 429 430 // patches and their comment permalinks. 431 { 432 var patches_view = try Patches.View.init(allocator, &data.patches, session); 433 errdefer patches_view.deinit(allocator); 434 try stack.children.put(allocator, patches_view.getFocus().id, .{ .repo_patches = patches_view }); 435 } 436 437 // discussions and their comment permalinks. 438 { 439 var discussions_view = try Discussions.View.init(allocator, &data.discussions, session); 440 errdefer discussions_view.deinit(allocator); 441 try stack.children.put(allocator, discussions_view.getFocus().id, .{ .repo_discussions = discussions_view }); 442 } 443 444 if (session.data.host_kind == .local or data.undo != null) { 445 var events_view = try Events.View.init(allocator, &data.events, session); 446 errdefer events_view.deinit(allocator); 447 try stack.children.put(allocator, events_view.getFocus().id, .{ .repo_events = events_view }); 448 } 449 450 if (data.undo) |*undo| { 451 var undo_view = try Undo.View.init(allocator, undo, session); 452 errdefer undo_view.deinit(allocator); 453 try stack.children.put(allocator, undo_view.getFocus().id, .{ .repo_undo = undo_view }); 454 } 455 456 // the header only shows the settings tab with a login and the auth 457 // tab outside local mode, so keep the stack's children 1:1 with 458 // the tabs by skipping the same views. 459 if (session.data.user_id != null) { 460 var settings_view = try Settings.View.init(allocator, session); 461 errdefer settings_view.deinit(allocator); 462 try stack.children.put(allocator, settings_view.getFocus().id, .{ .home_settings = settings_view }); 463 } 464 465 if (session.data.host_kind == .server) { 466 var auth_view = try Auth.View.init(allocator, &data.auth, session); 467 errdefer auth_view.deinit(allocator); 468 try stack.children.put(allocator, auth_view.getFocus().id, .{ .home_auth = auth_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