ref(container): move size: Size member from Scroll to Container

This commit is contained in:
2025-02-14 22:03:21 +01:00
parent c2a03e95c1
commit 73a7f740c9

View File

@@ -195,23 +195,8 @@ pub const Scroll = packed struct {
/// Enable vertical scrolling for this element
vertical: bool = false,
// TODO: enable changing the anchor position of this container along with its children
// - for that the contents need to be layed out in a way that is independent of the actual size of the container
// TODO: rendering enhancements:
// - render corresponding scroll-bars?
// TODO: does the `size` then actually need to be part of the `Scroll`, as the user should not tamper with the value anyway..
// TODO: should the container size be 'virtual' i.e. the complete
// - content size (which might be larger than the available screen)
// but how would a scrollable small portion of the screen work?
// -> this means that the size can remain and have its meaning, but the
// content needs another abstraction for the scroll
size: Size = .{},
// anchor => position in view of scrollable contents
// cols / rows => view port dimensions
// render => window in window
};
// TODO: can `Layout` become `packed`? -> for that the sizing cannot be a tagged enum!
@@ -265,7 +250,11 @@ pub fn Container(comptime Event: type) type {
return struct {
allocator: std.mem.Allocator,
/// Size of actual columns and rows used to render this `Container`
/// The anchor of this `Size` corresponds to the absolute screen position for the viewport.
viewport: Size,
/// Size of the contents columns and rows used for the contents of this `Container`
/// The anchor of this `Size` corresponds to the contents inside of itself.
size: Size = .{},
properties: Properties,
elements: std.ArrayList(@This()),
@@ -352,7 +341,7 @@ pub fn Container(comptime Event: type) type {
size.rows = @divTrunc(size.rows * percent, 100);
},
}
this.properties.scroll.size = size;
this.size = size;
if (this.elements.items.len == 0) break :resize;
@@ -471,47 +460,47 @@ pub fn Container(comptime Event: type) type {
}
pub fn contents(this: *const @This()) ![]const Cell {
const content_cells = try this.allocator.alloc(Cell, @as(usize, this.properties.scroll.size.cols) * @as(usize, this.properties.scroll.size.rows));
const content_cells = try this.allocator.alloc(Cell, @as(usize, this.size.cols) * @as(usize, this.size.rows));
defer this.allocator.free(content_cells);
@memset(content_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_cells, this.properties.scroll.size, this.properties.layout, @truncate(this.elements.items.len));
this.properties.rectangle.contents(content_cells, this.properties.scroll.size);
this.properties.border.contents(content_cells, this.size, this.properties.layout, @truncate(this.elements.items.len));
this.properties.rectangle.contents(content_cells, this.size);
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,
this.size.anchor.col,
this.size.anchor.row,
this.size.cols,
this.size.rows,
});
const cols = blk: {
var cols: u16 = this.properties.scroll.size.cols - this.properties.scroll.size.anchor.col;
var cols: u16 = this.size.cols - this.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;
var rows: u16 = this.size.rows - this.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 content_row: usize = this.size.anchor.row;
var content_col: usize = this.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];
const cell = content_cells[(content_row * this.size.cols) + content_col];
viewport_cells[(viewport_row * this.viewport.cols) + viewport_col] = cell;
content_col += 1;