From 4145ff497b30194a9b6ab358c7ebec0b78fcec2d Mon Sep 17 00:00:00 2001 From: Yves Biener Date: Sun, 2 Mar 2025 22:27:26 +0100 Subject: [PATCH] fix(elements/scrollable): nested container rendering --- src/element.zig | 43 +++++++++++++++++++++++-------------------- src/render.zig | 5 +---- 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/src/element.zig b/src/element.zig index 64c69ef..b373d6f 100644 --- a/src/element.zig +++ b/src/element.zig @@ -126,6 +126,27 @@ pub fn Scrollable(Event: type) type { } } + fn render_container(container: Container(Event), cells: []Cell, container_size: Size) !void { + const size = container.size; + const contents = try container.contents(); + defer container.allocator.free(contents); + + const anchor = (@as(usize, size.anchor.row -| container_size.anchor.row) * @as(usize, container_size.cols)) + + @as(usize, size.anchor.col -| container_size.anchor.col); + + var idx: usize = 0; + blk: for (0..size.rows) |row| { + for (0..size.cols) |col| { + cells[anchor + (row * container_size.cols) + col] = contents[idx]; + idx += 1; + + if (contents.len == idx) break :blk; + } + } + + for (container.elements.items) |child| try render_container(child, cells, size); + } + fn content(ctx: *anyopaque, cells: []Cell, size: Size) !void { const this: *@This() = @ptrCast(@alignCast(ctx)); std.debug.assert(cells.len == @as(usize, this.size.cols) * @as(usize, this.size.rows)); @@ -139,26 +160,8 @@ pub fn Scrollable(Event: type) type { @memcpy(container_cells, container_cells_const); } - for (this.container.elements.items) |*e| { - const e_size = e.size; - const element_cells = try e.contents(); - defer e.allocator.free(element_cells); - - const anchor = (@as(usize, e_size.anchor.row -| container_size.anchor.row) * @as(usize, container_size.cols)) + - @as(usize, e_size.anchor.col -| container_size.anchor.col); - - var idx: usize = 0; - blk: for (0..e_size.rows) |row| { - for (0..e_size.cols) |col| { - const cell = element_cells[idx]; - idx += 1; - - container_cells[anchor + (row * container_size.cols) + col] = cell; - - if (element_cells.len == idx) break :blk; - } - } - } + // FIX this is not resolving the rendering recursively! This means that the content is only shown for the first children + for (this.container.elements.items) |child| try render_container(child, container_cells, container_size); const anchor = (@as(usize, this.anchor.row) * @as(usize, container_size.cols)) + @as(usize, this.anchor.col); // TODO: render scrollbar according to configuration! diff --git a/src/render.zig b/src/render.zig index 1ffe0e6..6558b6f 100644 --- a/src/render.zig +++ b/src/render.zig @@ -71,12 +71,9 @@ pub const Buffered = struct { blk: for (0..size.rows) |row| { for (0..size.cols) |col| { - const cell = cells[idx]; + vs[anchor + (row * this.size.cols) + col] = cells[idx]; idx += 1; - vs[anchor + (row * this.size.cols) + col].style = cell.style; - vs[anchor + (row * this.size.cols) + col].cp = cell.cp; - if (cells.len == idx) break :blk; } }