ref(container): move size: Size member from Scroll to Container
This commit is contained in:
@@ -195,23 +195,8 @@ pub const Scroll = packed struct {
|
|||||||
/// Enable vertical scrolling for this element
|
/// Enable vertical scrolling for this element
|
||||||
vertical: bool = false,
|
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:
|
// TODO: rendering enhancements:
|
||||||
// - render corresponding scroll-bars?
|
// - 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!
|
// 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 {
|
return struct {
|
||||||
allocator: std.mem.Allocator,
|
allocator: std.mem.Allocator,
|
||||||
/// Size of actual columns and rows used to render this `Container`
|
/// 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,
|
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,
|
properties: Properties,
|
||||||
elements: std.ArrayList(@This()),
|
elements: std.ArrayList(@This()),
|
||||||
|
|
||||||
@@ -352,7 +341,7 @@ pub fn Container(comptime Event: type) type {
|
|||||||
size.rows = @divTrunc(size.rows * percent, 100);
|
size.rows = @divTrunc(size.rows * percent, 100);
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
this.properties.scroll.size = size;
|
this.size = size;
|
||||||
|
|
||||||
if (this.elements.items.len == 0) break :resize;
|
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 {
|
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);
|
defer this.allocator.free(content_cells);
|
||||||
@memset(content_cells, .{});
|
@memset(content_cells, .{});
|
||||||
|
|
||||||
const viewport_cells = try this.allocator.alloc(Cell, @as(usize, this.viewport.cols) * @as(usize, this.viewport.rows));
|
const viewport_cells = try this.allocator.alloc(Cell, @as(usize, this.viewport.cols) * @as(usize, this.viewport.rows));
|
||||||
@memset(viewport_cells, .{});
|
@memset(viewport_cells, .{});
|
||||||
|
|
||||||
this.properties.border.contents(content_cells, this.properties.scroll.size, this.properties.layout, @truncate(this.elements.items.len));
|
this.properties.border.contents(content_cells, this.size, this.properties.layout, @truncate(this.elements.items.len));
|
||||||
this.properties.rectangle.contents(content_cells, this.properties.scroll.size);
|
this.properties.rectangle.contents(content_cells, this.size);
|
||||||
|
|
||||||
log.debug("Content::contents .scroll.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.size.anchor.col,
|
||||||
this.properties.scroll.size.anchor.row,
|
this.size.anchor.row,
|
||||||
this.properties.scroll.size.cols,
|
this.size.cols,
|
||||||
this.properties.scroll.size.rows,
|
this.size.rows,
|
||||||
});
|
});
|
||||||
|
|
||||||
const cols = blk: {
|
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) {
|
if (cols > this.viewport.cols) {
|
||||||
cols = this.viewport.cols;
|
cols = this.viewport.cols;
|
||||||
}
|
}
|
||||||
break :blk cols;
|
break :blk cols;
|
||||||
};
|
};
|
||||||
const rows = blk: {
|
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) {
|
if (rows > this.viewport.rows) {
|
||||||
rows = this.viewport.rows;
|
rows = this.viewport.rows;
|
||||||
}
|
}
|
||||||
break :blk rows;
|
break :blk rows;
|
||||||
};
|
};
|
||||||
|
|
||||||
var content_row: usize = this.properties.scroll.size.anchor.row;
|
var content_row: usize = this.size.anchor.row;
|
||||||
var content_col: usize = this.properties.scroll.size.anchor.col;
|
var content_col: usize = this.size.anchor.col;
|
||||||
var viewport_row: usize = 0;
|
var viewport_row: usize = 0;
|
||||||
var viewport_col: usize = 0;
|
var viewport_col: usize = 0;
|
||||||
|
|
||||||
for (0..rows) |_| {
|
for (0..rows) |_| {
|
||||||
for (0..cols) |_| {
|
for (0..cols) |_| {
|
||||||
// TODO: try to do this with @memcpy instead to improve performance
|
// 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;
|
viewport_cells[(viewport_row * this.viewport.cols) + viewport_col] = cell;
|
||||||
|
|
||||||
content_col += 1;
|
content_col += 1;
|
||||||
|
|||||||
Reference in New Issue
Block a user