╭ Repo.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1 const std = @import("std"); 2 const evt = @import("../event.zig"); 3 const ui = @import("../ui.zig"); 4 const xit = @import("xit"); 5 const rp = xit.repo; 6 const xitui = xit.xitui; 7 const wgt = xitui.widget; 8 const layout = xitui.layout; 9 const Key = xitui.input.Key; 10 const Grid = xitui.grid.Grid; 11 const Focus = xitui.focus.Focus; 12 const inp = @import("./input.zig"); 13 14 pub const Header = @import("./Repo/Header.zig"); 15 pub const Files = @import("./Repo/Files.zig"); 16 pub const Commits = @import("./Repo/Commits.zig"); 17 pub const Refs = @import("./Repo/Refs.zig"); 18 pub const Issues = @import("./Repo/Issues.zig"); 19 pub const Patches = @import("./Repo/Patches.zig"); 20 pub const Discussions = @import("./Repo/Discussions.zig"); 21 pub const Comment = @import("./Repo/Comment.zig"); 22 pub const Events = @import("./Repo/Events.zig"); 23 pub const Settings = @import("./Settings.zig"); 24 pub const Auth = @import("./Auth.zig"); 25 pub const Quit = @import("./Quit.zig"); 26 27 const Array = ui.RoutablePage.Array(ui.RoutablePage.repo_identity_max_len); 28 29 header: Header, 30 repo: evt.Repo.Record, 31 files: Files, 32 commits: Commits, 33 refs: Refs, 34 issues: Issues, 35 patches: Patches, 36 discussions: Discussions, 37 events: Events, 38 settings: Settings, 39 auth: Auth, 40 quit: Quit, 41 // the full files route this page renders, mirrored into current_page when 42 // the files tab is selected. 43 route_name: ui.RoutablePage.RepoFilesRoute, 44 // the commits route this page renders ("owner/name/commits[/<oid>]"), mirrored 45 // when the commits tab is selected. 46 commits_route_name: ui.RoutablePage.RepoCommitsRoute, 47 // "owner/name" alone, mirrored when the settings/auth tab is selected. 48 identity: Array, 49 50 const Self = @This(); 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 61 // local mode); files and commits carry their remaining fields directly. 62 const name_str = route.repoIdentity() orelse return error.UnexpectedRoute; 63 const repo_identity = ui.RoutablePage.RepoIdentity.parse(name_str) orelse return error.NotFound; 64 // the files and commits tabs share one ref/oid: whichever the incoming route 65 // names (it rides on the route's target tab), or the default branch when 66 // neither tab is targeted. building both views at it keeps switching tabs 67 // (even by key, without a reload) on the same ref. a null ref means the 68 // default branch (Files/Commits.init resolve it). the directory and the diff 69 // window only apply to their own tab. 70 const requested_ref_or_oid: ?ui.RoutablePage.RefOrOid = switch (route) { 71 .repo_files => |f| f.ref_kind, 72 .repo_commits => |c| c.ref_or_oid, 73 else => null, 74 }; 75 const requested_ref_value: []const u8 = switch (route) { 76 .repo_files => |*f| f.ref_value.slice(), 77 .repo_commits => |*c| c.value.slice(), 78 else => "", 79 }; 80 const files_dir = switch (route) { 81 .repo_files => |*f| f.path.slice(), 82 else => "", 83 }; 84 const files_line = switch (route) { 85 .repo_files => |f| f.line, 86 else => 0, 87 }; 88 // what the commits view's pane shows for the commit it walks from: a diff 89 // window (with the file it's filtered to) or that commit's message. 90 const commits_content: ui.RoutablePage.RepoCommitsRoute.Content = switch (route) { 91 .repo_commits => |c| c.content, 92 else => .{ .diff = .{} }, 93 }; 94 const commits_start = switch (commits_content) { 95 .diff => |d| d.start, 96 .message => 0, 97 }; 98 // the refs tab windows one column at a time: `refs_from` (a url-encoded 99 // ref name) roots `refs_kind`'s column, the other stays at its first window. 100 const refs_kind: ui.RoutablePage.RefKind = switch (route) { 101 .repo_refs => |r| r.kind, 102 else => .branch, 103 }; 104 const refs_from: []const u8 = switch (route) { 105 .repo_refs => |*r| r.from.slice(), 106 else => "", 107 }; 108 // the issues tab's tag filter, the issue its window is rooted at, and the 109 // view it shows. 110 const issues_tag: []const u8 = switch (route) { 111 .repo_issues => |*i| i.tag.slice(), 112 else => "", 113 }; 114 const issues_selected: []const u8 = switch (route) { 115 .repo_issues => |*i| i.selected.slice(), 116 else => "", 117 }; 118 const issues_comment: []const u8 = switch (route) { 119 .repo_issues => |*i| i.comment.slice(), 120 else => "", 121 }; 122 const issues_theirs: []const u8 = switch (route) { 123 .repo_issues => |*i| i.theirs.slice(), 124 else => "", 125 }; 126 const issues_view: ui.RoutablePage.IssuesView = switch (route) { 127 .repo_issues => |i| i.view, 128 else => .open, 129 }; 130 const issues_comments_start: usize = switch (route) { 131 .repo_issues => |i| i.comments_start, 132 else => 0, 133 }; 134 const patches_tag: []const u8 = switch (route) { 135 .repo_patches => |*p| p.tag.slice(), 136 else => "", 137 }; 138 const patches_selected: []const u8 = switch (route) { 139 .repo_patches => |*p| p.selected.slice(), 140 else => "", 141 }; 142 const patches_comment: []const u8 = switch (route) { 143 .repo_patches => |*p| p.comment.slice(), 144 else => "", 145 }; 146 const patches_theirs: []const u8 = switch (route) { 147 .repo_patches => |*p| p.theirs.slice(), 148 else => "", 149 }; 150 const patches_view: ui.RoutablePage.PatchesView = switch (route) { 151 .repo_patches => |p| p.view, 152 else => .open, 153 }; 154 const patches_comments_start: usize = switch (route) { 155 .repo_patches => |p| p.comments_start, 156 else => 0, 157 }; 158 const discussions_tag: []const u8 = switch (route) { 159 .repo_discussions => |*t| t.tag.slice(), 160 else => "", 161 }; 162 const discussions_selected: []const u8 = switch (route) { 163 .repo_discussions => |*t| t.selected.slice(), 164 else => "", 165 }; 166 const discussions_comment: []const u8 = switch (route) { 167 .repo_discussions => |*t| t.comment.slice(), 168 else => "", 169 }; 170 const discussions_view: ui.RoutablePage.DiscussionsView = switch (route) { 171 .repo_discussions => |t| t.view, 172 else => .recent, 173 }; 174 const discussions_comments_start: usize = switch (route) { 175 .repo_discussions => |t| t.comments_start, 176 else => 0, 177 }; 178 const events_kind: ?evt.EventKind = switch (route) { 179 .repo_events => |e| e.kind, 180 else => null, 181 }; 182 const events_view: ui.RoutablePage.EventsView = switch (route) { 183 .repo_events => |e| e.view, 184 else => .active, 185 }; 186 const events_selected: []const u8 = switch (route) { 187 .repo_events => |*e| e.selected.slice(), 188 else => "", 189 }; 190 191 // where the on-disk repo lives (null keeps the views' empty fallback), plus 192 // the repo and owner-name metadata the header shows. local mode already 193 // knows all three; the server paths resolve them from the admin db. 194 var source: ?ui.RepoSource = null; 195 var repo_id_maybe: ?[evt.event_id_size]u8 = null; 196 var repo: evt.Repo.Record = undefined; 197 var owner_name: []const u8 = undefined; 198 if (session.local) |local| { 199 source = local; 200 // local routes elide the identity, so the display name comes from the 201 // repo's directory rather than the route. 202 repo = .{ .event = .{ 203 .user_id = "", 204 .name = try arena.allocator().dupe(u8, std.fs.path.basename(local.path)), 205 .description = "", 206 } }; 207 owner_name = ""; 208 } else { 209 const haxy_moment = session.haxy_moment orelse return error.NoMoment; 210 const found = (try evt.Repo.readByOwnerAndName(DB, hash_kind, haxy_moment, arena, repo_identity.owner, repo_identity.name)) orelse return error.NotFound; 211 repo = found.repo; 212 repo_id_maybe = found.event_id; 213 214 // resolve the creating user so the header can show their name to the left 215 // of the repo title. 216 const owner = (try evt.User.readById(DB, hash_kind, haxy_moment, arena, repo.event.user_id)) orelse return error.NotFound; 217 owner_name = owner.event.name; 218 219 // the repo's working copy lives at <repos_dir>/<hex event id>. 220 if (session.repos_dir) |repos_dir| { 221 const hex = std.fmt.bytesToHex(found.event_id, .lower); 222 source = .{ 223 .path = try std.fs.path.join(arena.allocator(), &.{ repos_dir, &hex }), 224 .repo_kind = .xit, 225 }; 226 } 227 } 228 229 // open the on-disk repo once and read every tab's data from it. files and 230 // commits resolve the same requested ref (the default branch when the 231 // route named none), so they end up viewing the same one and either's 232 // resolved ref can canonicalize the tab mirror urls below. no filesystem 233 // (wasm), nowhere to look, or a failed open: empty tabs. 234 const files, const commits, const refs, var issues, var patches, var discussions, const events = blk: { 235 read: { 236 const io = session.io orelse break :read; 237 const src = source orelse break :read; 238 const gpa = arena.child_allocator; 239 switch (src.repo_kind) { 240 inline else => |repo_kind| { 241 var any_repo = rp.AnyRepo(repo_kind, .{}).open(io, gpa, src.localInitOpts()) catch break :read; 242 defer any_repo.deinit(io, gpa); 243 switch (any_repo) { 244 inline else => |*opened| { 245 // local mode: bring the event db up to date with the events branch 246 if (session.local != null) try evt.consume(.repo, repo_kind, opened.self_repo_opts, io, gpa, opened, evt.events_ref, &.{}); 247 break :blk .{ 248 try Files.init(repo_kind, opened.self_repo_opts, arena, opened, io, gpa, repo_identity.identity, requested_ref_or_oid, requested_ref_value, files_dir, files_line), 249 try Commits.init(repo_kind, opened.self_repo_opts, arena, opened, io, gpa, session.haxy_moment, repo_identity.identity, requested_ref_or_oid, requested_ref_value, commits_content), 250 try Refs.init(repo_kind, opened.self_repo_opts, arena, opened, io, gpa, repo_identity.identity, refs_kind, refs_from), 251 try Issues.init(repo_kind, opened.self_repo_opts, arena, opened, io, session.haxy_moment, repo_identity.identity, issues_tag, issues_selected, issues_comment, issues_comments_start, issues_theirs, issues_view), 252 try Patches.init(repo_kind, opened.self_repo_opts, arena, opened, io, session.haxy_moment, session, repo_id_maybe, repo_identity.identity, patches_tag, patches_selected, patches_comment, patches_comments_start, patches_theirs, patches_view), 253 try Discussions.init(repo_kind, opened.self_repo_opts, arena, opened, io, session.haxy_moment, repo_identity.identity, discussions_tag, discussions_selected, discussions_comment, discussions_comments_start, discussions_view), 254 try Events.init(repo_kind, opened.self_repo_opts, arena, opened, io, session.haxy_moment, repo_identity.identity, events_view, events_kind, events_selected, session.local != null, session.data.sync_failure), 255 }; 256 }, 257 } 258 }, 259 } 260 } 261 const aa = arena.allocator(); 262 break :blk .{ 263 try Files.emptyResult(aa, repo_identity.identity, requested_ref_or_oid orelse .branch, requested_ref_value, files_dir), 264 try Commits.emptyResult(aa, repo_identity.identity, requested_ref_or_oid orelse .branch, requested_ref_value, commits_content), 265 try Refs.emptyResult(arena, repo_identity.identity, refs_kind, refs_from), 266 try Issues.emptyResult(aa, repo_identity.identity, issues_tag, issues_selected, issues_comment, issues_comments_start, issues_theirs, issues_view), 267 try Patches.emptyResult(aa, repo_identity.identity, patches_tag, patches_selected, patches_comment, patches_comments_start, patches_theirs, patches_view), 268 try Discussions.emptyResult(aa, repo_identity.identity, discussions_tag, discussions_selected, discussions_comment, discussions_comments_start, discussions_view), 269 try Events.empty(aa, repo_identity.identity, events_view, session.local != null, session.data.sync_failure), 270 }; 271 }; 272 issues.repo_source = source; 273 patches.repo_source = source; 274 patches.repo_id = repo_id_maybe; 275 patches.target_branch = if (files.ref_or_oid == .branch) files.ref_or_oid_value else ""; 276 discussions.repo_source = source; 277 278 // each tab mirror carries this page's route for that tab; tabs not targeted 279 // by the incoming route fall back to their root/first-page route. the files 280 // mirror carries the selected file (when the route named one) so the url 281 // keeps it before focus enters the view. 282 const files_path = if (files.selected_file) |f| try Files.childDir(arena.allocator(), files.dir, f) else files.dir; 283 // the content window only applies to a selected file, so the bare 284 // directory route drops it. 285 const files_route_line = if (files.selected_file != null) files_line else 0; 286 const route_name = (ui.RoutablePage.repoFilesRoute(repo_identity.identity, files.ref_or_oid, files.ref_or_oid_value, files_path, files_route_line) orelse return error.NotFound).repo_files; 287 // the message page mirrors its own url, not the log's 288 const commits_route = switch (commits.content) { 289 .message => ui.RoutablePage.repoCommitMessageRoute(repo_identity.identity, commits.ref_or_oid, commits.ref_or_oid_value), 290 .diff => |d| ui.RoutablePage.repoCommitsRoute(repo_identity.identity, commits.ref_or_oid, commits.ref_or_oid_value, commits_start, d.path), 291 }; 292 const commits_route_name = (commits_route orelse return error.NotFound).repo_commits; 293 294 return .{ 295 // files and commits resolve the same ref, so either's serves the header, 296 // which points both tabs at it. 297 .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), 298 .repo = repo, 299 .files = files, 300 .commits = commits, 301 .refs = refs, 302 .issues = issues, 303 .patches = patches, 304 .discussions = discussions, 305 .events = events, 306 .settings = Settings.init(), 307 .auth = Auth.init(), 308 .quit = Quit.init(), 309 .route_name = route_name, 310 .commits_route_name = commits_route_name, 311 .identity = Array.from(repo_identity.identity) orelse return error.NotFound, 312 }; 313 } 314 315 pub const View = struct { 316 box: wgt.Box(ui.Widget), 317 data: *const Self, 318 session: *ui.Session, 319 320 const header_index: usize = 0; 321 const stack_index: usize = 1; 322 323 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { 324 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .rounded_corners = true, .direction = .vert }); 325 errdefer box.deinit(allocator); 326 327 // build the header first so we can grab the files-tab id for the auth 328 // view (it focuses there after login). 329 { 330 var header_view = try Header.View.init(allocator, &data.header, session); 331 errdefer header_view.deinit(allocator); 332 try box.children.put(allocator, header_view.getFocus().id, .{ .widget = .{ .repo_header = header_view }, .rect = null, .min_size = null }); 333 } 334 335 { 336 var stack = try wgt.Stack(ui.Widget).init(allocator); 337 errdefer stack.deinit(allocator); 338 339 // files — the default tab: the current directory's listing. 340 { 341 var files_view = try Files.View.init(allocator, &data.files, session); 342 errdefer files_view.deinit(allocator); 343 try stack.children.put(allocator, files_view.getFocus().id, .{ .repo_files = files_view }); 344 } 345 346 // commits — the current page of the commit log. 347 { 348 var commits_view = try Commits.View.init(allocator, &data.commits, session); 349 errdefer commits_view.deinit(allocator); 350 try stack.children.put(allocator, commits_view.getFocus().id, .{ .repo_commits = commits_view }); 351 } 352 353 // refs — the repo's branches and tags. 354 { 355 var refs_view = try Refs.View.init(allocator, &data.refs, session); 356 errdefer refs_view.deinit(allocator); 357 try stack.children.put(allocator, refs_view.getFocus().id, .{ .repo_refs = refs_view }); 358 } 359 360 // issues — the repo's issue tracker and comment permalinks. 361 { 362 var issues_view = try Issues.View.init(allocator, &data.issues, session); 363 errdefer issues_view.deinit(allocator); 364 try stack.children.put(allocator, issues_view.getFocus().id, .{ .repo_issues = issues_view }); 365 } 366 367 // patches and their comment permalinks. 368 { 369 var patches_view = try Patches.View.init(allocator, &data.patches, session); 370 errdefer patches_view.deinit(allocator); 371 try stack.children.put(allocator, patches_view.getFocus().id, .{ .repo_patches = patches_view }); 372 } 373 374 // discussions and their comment permalinks. 375 { 376 var discussions_view = try Discussions.View.init(allocator, &data.discussions, session); 377 errdefer discussions_view.deinit(allocator); 378 try stack.children.put(allocator, discussions_view.getFocus().id, .{ .repo_discussions = discussions_view }); 379 } 380 381 { 382 var events_view = try Events.View.init(allocator, &data.events, session); 383 errdefer events_view.deinit(allocator); 384 try stack.children.put(allocator, events_view.getFocus().id, .{ .repo_events = events_view }); 385 } 386 387 // the header only shows the settings tab with a login and the auth 388 // tab outside local mode, so keep the stack's children 1:1 with 389 // the tabs by skipping the same views. 390 if (session.data.user_id != null) { 391 var settings_view = try Settings.View.init(allocator, session); 392 errdefer settings_view.deinit(allocator); 393 try stack.children.put(allocator, settings_view.getFocus().id, .{ .home_settings = settings_view }); 394 } 395 396 if (!session.data.is_local) { 397 var auth_view = try Auth.View.init(allocator, &data.auth, session); 398 errdefer auth_view.deinit(allocator); 399 try stack.children.put(allocator, auth_view.getFocus().id, .{ .home_auth = auth_view }); 400 } 401 402 if (session.is_terminal) { 403 var quit_view = try Quit.View.init(allocator, session); 404 errdefer quit_view.deinit(allocator); 405 try stack.children.put(allocator, quit_view.getFocus().id, .{ .quit = quit_view }); 406 } 407 408 try box.children.put(allocator, stack.getFocus().id, .{ .widget = .{ .stack = stack }, .rect = null, .min_size = null }); 409 } 410 411 var self = View{ .box = box, .data = data, .session = session }; 412 self.getFocus().child_id = box.children.keys()[header_index]; 413 return self; 414 } 415 416 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 417 self.box.deinit(allocator); 418 } 419 420 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 421 self.clearGrid(); 422 const header = &self.box.children.values()[header_index].widget.repo_header; 423 const stack = &self.box.children.values()[stack_index].widget.stack; 424 425 // each header tab maps 1:1 to a stack child by position. mirror the 426 // selection into current_page so the host can push the matching url; 427 // all repo tabs share the .repo parent, so this stays on the page 428 // rather than navigating. 429 if (header.getSelectedIndex()) |index| { 430 stack.getFocus().child_id = stack.children.keys()[index]; 431 switch (std.meta.activeTag(stack.children.values()[index])) { 432 // files/commits carry this page's content route (directory / log 433 // page); settings/auth carry only "owner/name". 434 .repo_commits => self.session.data.current_page = .{ .repo_commits = self.data.commits_route_name }, 435 // the refs tab mirrors this page's windowed column. 436 .repo_refs => self.session.data.current_page = ui.RoutablePage.repoRefsRoute(self.data.identity.slice(), self.data.refs.kind, self.data.refs.from) orelse self.session.data.current_page, 437 // the issues tab mirrors this page's tag filter (issue urls 438 // themselves never carry the tag). 439 .repo_issues => self.session.data.current_page = ui.RoutablePage.repoIssuesRoute(self.data.identity.slice(), .open, self.data.issues.tag, "") orelse self.session.data.current_page, 440 .repo_patches => self.session.data.current_page = ui.RoutablePage.repoPatchesRoute(self.data.identity.slice(), .open, self.data.patches.tag, "") orelse self.session.data.current_page, 441 .repo_discussions => self.session.data.current_page = ui.RoutablePage.repoDiscussionsRoute(self.data.identity.slice(), self.data.discussions.tag, "") orelse self.session.data.current_page, 442 .repo_events => self.session.data.current_page = ui.RoutablePage.repoEventsRoute(self.data.identity.slice(), self.data.events.view, null, "") orelse self.session.data.current_page, 443 .home_settings => { 444 if (ui.RoutablePage.Array(ui.RoutablePage.repo_route_max_len).from(self.data.identity.slice())) |identity| 445 self.session.data.current_page = .{ .repo_settings = identity }; 446 }, 447 .home_auth => { 448 if (ui.RoutablePage.Array(ui.RoutablePage.repo_route_max_len).from(self.data.identity.slice())) |identity| 449 self.session.data.current_page = .{ .repo_auth = identity }; 450 }, 451 // the quit tab is tty-only and not a route, so leave current_page 452 // alone (nothing to mirror into the url). 453 .quit => {}, 454 // the files tab (this page's directory route) 455 else => self.session.data.current_page = .{ .repo_files = self.data.route_name }, 456 } 457 } 458 try self.box.build(allocator, constraint, root_focus); 459 } 460 461 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 462 const stack = &self.box.children.values()[stack_index].widget.stack; 463 if (self.getFocus().child_id) |child_id| { 464 if (self.box.children.getIndex(child_id)) |current_index| { 465 const child = &self.box.children.values()[current_index].widget; 466 var index = current_index; 467 468 const direction = inp.vertDirection(key); 469 470 switch (direction) { 471 .up => { 472 switch (child.*) { 473 .repo_header => { 474 try child.input(allocator, key, root_focus); 475 }, 476 .stack => { 477 if (child.stack.getSelected()) |selected_widget| { 478 const at_top = switch (selected_widget.*) { 479 .repo_files => |*v| v.getSelectedIndex() == 0, 480 .repo_commits => |*v| v.getSelectedIndex() == 0, 481 .repo_refs => |*v| v.getSelectedIndex() == 0, 482 .repo_issues => |*v| v.getSelectedIndex() == 0, 483 .repo_patches => |*v| v.getSelectedIndex() == 0, 484 .repo_discussions => |*v| v.getSelectedIndex() == 0, 485 .repo_events => |*v| v.getSelectedIndex() == 0, 486 .home_settings => |*v| v.getSelectedIndex() == 0, 487 .home_auth => |*v| v.getSelectedIndex() == 0, 488 .quit => |*v| v.getSelectedIndex() == 0, 489 else => false, 490 }; 491 if (at_top) { 492 index = header_index; 493 } else { 494 try child.input(allocator, key, root_focus); 495 } 496 } 497 }, 498 else => {}, 499 } 500 }, 501 .down => { 502 switch (child.*) { 503 .repo_header => { 504 if (stack.getSelected()) |selected_widget| switch (selected_widget.*) { 505 .repo_files => |*v| if (v.focusCloneUrl(root_focus)) return, 506 .repo_commits => |*v| if (v.focusCloneUrl(root_focus)) return, 507 else => {}, 508 }; 509 index = stack_index; 510 }, 511 .stack => { 512 try child.input(allocator, key, root_focus); 513 }, 514 else => {}, 515 } 516 }, 517 .none => { 518 try child.input(allocator, key, root_focus); 519 }, 520 } 521 522 if (index != current_index) { 523 root_focus.setFocus(self.box.children.keys()[index]); 524 } 525 } 526 } 527 } 528 529 pub fn clearGrid(self: *View) void { 530 self.box.clearGrid(); 531 } 532 533 pub fn getGrid(self: View) ?Grid { 534 return self.box.getGrid(); 535 } 536 537 pub fn getFocus(self: *View) *Focus { 538 return self.box.getFocus(); 539 } 540 }; 541