╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
587 self.clearGrid(); 588 self.getFocus().clear(); 589 590 // build the child without forcing it to fill min_size; it sizes 591 // itself to its content within the available max. 592 try self.child.build(allocator, .{ 593 .min_size = .{ .width = null, .height = null }, 594 .max_size = constraint.max_size, 595 }, root_focus); 596 597 const child_grid = self.child.getGrid() orelse return; 598 599 // prefer max when given; otherwise grow to max(min, child) so that 600 // when only a min is set (e.g. the wasm path passes viewport rows 601 // as min_height with no max), we can still vertically center while 602 // letting taller content extend past the min. 603 const width = if (constraint.max_size.width) |w| w else if (constraint.min_size.width) |min_w| @max(min_w, child_grid.size.width) else child_grid.size.width; 604 const height = if (constraint.max_size.height) |h| h else if (constraint.min_size.height) |min_h| @max(min_h, child_grid.size.height) else child_grid.size.height; 605 if (width == 0 or height == 0) return; 606 607 const offset_x: usize = if (self.direction == .horiz or self.direction == .both) 608 (width -| child_grid.size.width) / 2 609 else 610 0; 611 const offset_y: usize = if (self.direction == .vert or self.direction == .both) 612 (height -| child_grid.size.height) / 2 613 else 614 0; 615 616 var grid = try Grid.init(allocator, .{ .width = width, .height = height }); 617 errdefer grid.deinit(); 618 try grid.drawGrid(child_grid, offset_x, offset_y); 619 try self.getFocus().addChild(allocator, self.child.getFocus(), child_grid.size, offset_x, offset_y); 620 self.getFocus().child_id = self.child.getFocus().id; 621 622 self.grid = grid; 623 } 624 625 pub fn input(self: *Center, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 626 try self.child.input(allocator, key, root_focus); 627 } 628 629 pub fn clearGrid(self: *Center) void { 630 if (self.grid) |*grid| { 631 grid.deinit(); 632 self.grid = null; 633 } 634 self.child.clearGrid(); 635 } 636 637 pub fn getGrid(self: Center) ?Grid { 638 return self.grid; 639 } 640 641 pub fn getFocus(self: *Center) *Focus { 642 return &self.focus; 643 } 644 }; 645