diff --git a/examples/container.zig b/examples/container.zig index 81babee..25031cc 100644 --- a/examples/container.zig +++ b/examples/container.zig @@ -31,20 +31,33 @@ pub fn main() !void { .separator = .{ .enabled = false }, }, .layout = .{ - .gap = 1, + .gap = 0, .padding = .all(5), .direction = .horizontal, }, }); - try container.append(try App.Container.init(allocator, .{ + var box = try App.Container.init(allocator, .{ .rectangle = .{ .fill = .blue }, .layout = .{ + .gap = 1, + .direction = .vertical, + .padding = .vertical(1), .sizing = .{ - .width = .{ .fixed = 70 }, - .height = .{ .fixed = 18 }, + // .width = .{ .fixed = 700 }, + // .height = .{ .fixed = 180 }, }, }, + }); + try box.append(try App.Container.init(allocator, .{ + .rectangle = .{ .fill = .light_green }, })); + try box.append(try App.Container.init(allocator, .{ + .rectangle = .{ .fill = .light_green }, + })); + try box.append(try App.Container.init(allocator, .{ + .rectangle = .{ .fill = .light_green }, + })); + try container.append(box); try container.append(try App.Container.init(allocator, .{ .border = .{ .color = .light_blue, .corners = .squared }, })); diff --git a/src/container.zig b/src/container.zig index 76f49c4..8f0a3c0 100644 --- a/src/container.zig +++ b/src/container.zig @@ -311,6 +311,7 @@ pub fn Container(comptime Event: type) type { }); this.viewport = s; // sizing + // FIX: fixed sizing for smaller and bigger values does not work yet var size = s; size.anchor = .{}; // reset relative anchor of size! const sizing = this.properties.layout.sizing; @@ -410,8 +411,8 @@ pub fn Container(comptime Event: type) type { } element_size = .{ .anchor = .{ - .col = size.anchor.col + offset, - .row = size.anchor.row, + .col = this.viewport.anchor.col + offset, + .row = this.viewport.anchor.row, }, .cols = cols, .rows = size.rows, @@ -429,8 +430,8 @@ pub fn Container(comptime Event: type) type { } element_size = .{ .anchor = .{ - .col = size.anchor.col, - .row = size.anchor.row + offset, + .col = this.viewport.anchor.col, + .row = this.viewport.anchor.row + offset, }, .cols = size.cols, .rows = rows, @@ -464,52 +465,58 @@ pub fn Container(comptime Event: type) type { } pub fn contents(this: *const @This()) ![]const Cell { - const content = try this.allocator.alloc(Cell, @as(usize, this.properties.scroll.size.cols) * @as(usize, this.properties.scroll.size.rows)); - defer this.allocator.free(content); - @memset(content, .{}); + const content_cells = try this.allocator.alloc(Cell, @as(usize, this.properties.scroll.size.cols) * @as(usize, this.properties.scroll.size.rows)); + defer this.allocator.free(content_cells); + @memset(content_cells, .{}); - const cells = try this.allocator.alloc(Cell, @as(usize, this.viewport.cols) * @as(usize, this.viewport.rows)); - @memset(cells, .{}); + const viewport_cells = try this.allocator.alloc(Cell, @as(usize, this.viewport.cols) * @as(usize, this.viewport.rows)); + @memset(viewport_cells, .{}); - this.properties.border.contents(content, this.properties.scroll.size, this.properties.layout, @truncate(this.elements.items.len)); - this.properties.rectangle.contents(content, this.properties.scroll.size); + this.properties.border.contents(content_cells, this.properties.scroll.size, this.properties.layout, @truncate(this.elements.items.len)); + this.properties.rectangle.contents(content_cells, this.properties.scroll.size); - const cols = blk: { - var cols: u16 = this.properties.scroll.size.cols; - if (this.properties.scroll.size.cols > this.viewport.cols) { - cols = this.viewport.cols; - } - break :blk cols; - }; - const rows = blk: { - var rows: u16 = this.properties.scroll.size.rows; - if (this.properties.scroll.size.rows > this.viewport.rows) { - rows = this.viewport.rows; - } - break :blk rows; - }; - const anchor = (this.properties.scroll.size.anchor.row * this.properties.scroll.size.cols) + this.properties.scroll.size.anchor.col; - log.debug("viewport = .{{ .anchor = {{ .col = {d}, .row = {d} }}, .cols = {d}, .rows = {d} }}", .{ - this.viewport.anchor.col, - this.viewport.anchor.row, - this.viewport.cols, - this.viewport.rows, - }); - log.debug("size = .{{ .anchor = {{ .col = {d}, .row = {d} }}, .cols = {d}, .rows = {d} }}", .{ + log.debug("Content::contents .scroll.size: {{ .anchor = {{ .col = {d}, .row = {d} }}, .cols = {d}, .rows = {d} }}", .{ this.properties.scroll.size.anchor.col, this.properties.scroll.size.anchor.row, this.properties.scroll.size.cols, this.properties.scroll.size.rows, }); - for (0..rows) |row| { - for (0..cols) |col| { - // TODO: try to do this with @memcpy instead to improve performance - const cell = content[anchor + (row * this.properties.scroll.size.cols) + col]; - cells[row * this.viewport.cols + col].cp = cell.cp; - cells[row * this.viewport.cols + col].style = cell.style; + + const cols = blk: { + var cols: u16 = this.properties.scroll.size.cols - this.properties.scroll.size.anchor.col; + if (cols > this.viewport.cols) { + cols = this.viewport.cols; } + break :blk cols; + }; + const rows = blk: { + var rows: u16 = this.properties.scroll.size.rows - this.properties.scroll.size.anchor.col; + if (rows > this.viewport.rows) { + rows = this.viewport.rows; + } + break :blk rows; + }; + + var content_row: usize = this.properties.scroll.size.anchor.row; + var content_col: usize = this.properties.scroll.size.anchor.col; + var viewport_row: usize = 0; + var viewport_col: usize = 0; + + for (0..rows) |_| { + for (0..cols) |_| { + // TODO: try to do this with @memcpy instead to improve performance + const cell = content_cells[(content_row * this.properties.scroll.size.cols) + content_col]; + viewport_cells[(viewport_row * this.viewport.cols) + viewport_col] = cell; + + content_col += 1; + viewport_col += 1; + } + content_row += 1; + viewport_row += 1; + content_col = 0; + viewport_col = 0; } - return cells; + return viewport_cells; } }; }