╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Repo.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
265 // files and commits resolve the same ref, so either's serves the header, 266 // which points both tabs at it. 267 .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), 268 .repo = repo, 269 .files = files, 270 .commits = commits, 271 .refs = refs, 272 .issues = issues, 273 .patches = patches, 274 .discussions = discussions, 275 .events = events, 276 .settings = Settings.init(), 277 .auth = Auth.init(), 278 .quit = Quit.init(), 279 }; 280 } 281 282 pub const View = struct { 283 box: wgt.Box(ui.Widget), 284 285 const header_index: usize = 0; 286 const stack_index: usize = 1; 287 288 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { 289 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .rounded_corners = true, .direction = .vert }); 290 errdefer box.deinit(allocator); 291 292 // build the header first so we can grab the files-tab id for the auth 293 // view (it focuses there after login). 294 { 295 var header_view = try Header.View.init(allocator, &data.header, session); 296 errdefer header_view.deinit(allocator); 297 try box.children.put(allocator, header_view.getFocus().id, .{ .widget = .{ .repo_header = header_view }, .rect = null, .min_size = null }); 298 } 299 300 { 301 var stack = try wgt.Stack(ui.Widget).init(allocator); 302 errdefer stack.deinit(allocator); 303 304 // files — the default tab: the current directory's listing. 305 { 306 var files_view = try Files.View.init(allocator, &data.files, session); 307 errdefer files_view.deinit(allocator); 308 try stack.children.put(allocator, files_view.getFocus().id, .{ .repo_files = files_view }); 309 } 310 311 // commits — the current page of the commit log. 312 { 313 var commits_view = try Commits.View.init(allocator, &data.commits, session); 314 errdefer commits_view.deinit(allocator); 315 try stack.children.put(allocator, commits_view.getFocus().id, .{ .repo_commits = commits_view }); 316 } 317 318 // refs — the repo's branches and tags. 319 { 320 var refs_view = try Refs.View.init(allocator, &data.refs, session); 321 errdefer refs_view.deinit(allocator); 322 try stack.children.put(allocator, refs_view.getFocus().id, .{ .repo_refs = refs_view }); 323 } 324 325 // issues — the repo's issue tracker and comment permalinks. 326 { 327 var issues_view = try Issues.View.init(allocator, &data.issues, session); 328 errdefer issues_view.deinit(allocator); 329 try stack.children.put(allocator, issues_view.getFocus().id, .{ .repo_issues = issues_view }); 330 } 331 332 // patches and their comment permalinks. 333 { 334 var patches_view = try Patches.View.init(allocator, &data.patches, session); 335 errdefer patches_view.deinit(allocator); 336 try stack.children.put(allocator, patches_view.getFocus().id, .{ .repo_patches = patches_view }); 337 } 338 339 // discussions and their comment permalinks. 340 { 341 var discussions_view = try Discussions.View.init(allocator, &data.discussions, session); 342 errdefer discussions_view.deinit(allocator); 343 try stack.children.put(allocator, discussions_view.getFocus().id, .{ .repo_discussions = discussions_view }); 344 } 345 346 { 347 var events_view = try Events.View.init(allocator, &data.events, session); 348 errdefer events_view.deinit(allocator); 349 try stack.children.put(allocator, events_view.getFocus().id, .{ .repo_events = events_view }); 350 } 351 352 // the header only shows the settings tab with a login and the auth 353 // tab outside local mode, so keep the stack's children 1:1 with 354 // the tabs by skipping the same views. 355 if (session.data.user_id != null) { 356 var settings_view = try Settings.View.init(allocator, session); 357 errdefer settings_view.deinit(allocator); 358 try stack.children.put(allocator, settings_view.getFocus().id, .{ .home_settings = settings_view }); 359 } 360 361 if (!session.data.is_local) { 362 var auth_view = try Auth.View.init(allocator, &data.auth, session); 363 errdefer auth_view.deinit(allocator); 364 try stack.children.put(allocator, auth_view.getFocus().id, .{ .home_auth = auth_view }); 365 } 366 367 if (session.is_terminal) { 368 var quit_view = try Quit.View.init(allocator, session); 369 errdefer quit_view.deinit(allocator); 370 try stack.children.put(allocator, quit_view.getFocus().id, .{ .quit = quit_view }); 371 } 372 373 try box.children.put(allocator, stack.getFocus().id, .{ .widget = .{ .stack = stack }, .rect = null, .min_size = null }); 374 } 375 376 var self = View{ .box = box }; 377 self.getFocus().child_id = box.children.keys()[header_index]; 378 return self; 379 } 380 381 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 382 self.box.deinit(allocator); 383 } 384 385 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 386 self.clearGrid(); 387 const header = &self.box.children.values()[header_index].widget.repo_header; 388 const stack = &self.box.children.values()[stack_index].widget.stack; 389 390 // each header tab maps 1:1 to a stack child by position 391 if (header.getSelectedIndex()) |index| 392 stack.getFocus().child_id = stack.children.keys()[index]; 393 try self.box.build(allocator, constraint, root_focus); 394 } 395 396 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 397 const stack = &self.box.children.values()[stack_index].widget.stack; 398 if (self.getFocus().child_id) |child_id| { 399 if (self.box.children.getIndex(child_id)) |current_index| { 400 const child = &self.box.children.values()[current_index].widget; 401 var index = current_index; 402 403 const direction = inp.vertDirection(key); 404 405 switch (direction) { 406 .up => { 407 switch (child.*) { 408 .repo_header => { 409 try child.input(allocator, key, root_focus); 410 }, 411 .stack => { 412 if (child.stack.getSelected()) |selected_widget| { 413 const at_top = switch (selected_widget.*) { 414 .repo_files => |*v| v.getSelectedIndex() == 0, 415 .repo_commits => |*v| v.getSelectedIndex() == 0, 416 .repo_refs => |*v| v.getSelectedIndex() == 0, 417 .repo_issues => |*v| v.getSelectedIndex() == 0, 418 .repo_patches => |*v| v.getSelectedIndex() == 0, 419 .repo_discussions => |*v| v.getSelectedIndex() == 0, 420 .repo_events => |*v| v.getSelectedIndex() == 0, 421 .home_settings => |*v| v.getSelectedIndex() == 0, 422 .home_auth => |*v| v.getSelectedIndex() == 0, 423 .quit => |*v| v.getSelectedIndex() == 0, 424 else => false, 425 }; 426 if (at_top) { 427 index = header_index; 428 } else { 429 try child.input(allocator, key, root_focus); 430 } 431 } 432 }, 433 else => {}, 434 } 435 }, 436 .down => { 437 switch (child.*) { 438 .repo_header => { 439 if (stack.getSelected()) |selected_widget| switch (selected_widget.*) { 440 .repo_files => |*v| if (v.focusCloneUrl(root_focus)) return, 441 .repo_commits => |*v| if (v.focusCloneUrl(root_focus)) return, 442 else => {}, 443 }; 444 index = stack_index; 445 }, 446 .stack => { 447 try child.input(allocator, key, root_focus); 448 }, 449 else => {}, 450 } 451 }, 452 .none => { 453 try child.input(allocator, key, root_focus); 454 }, 455 } 456 457 if (index != current_index) { 458 root_focus.setFocus(self.box.children.keys()[index]); 459 } 460 } 461 } 462 } 463 464 pub fn clearGrid(self: *View) void { 465 self.box.clearGrid(); 466 } 467 468 pub fn getGrid(self: View) ?Grid { 469 return self.box.getGrid(); 470 } 471 472 pub fn getFocus(self: *View) *Focus { 473 return self.box.getFocus(); 474 } 475 }; 476