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