mod(container/element): remove origin: Point argument from Element.content function
All checks were successful
Zig Project Action / Lint, Spell-check and test zig project (push) Successful in 1m12s

Renamed `Container.contents` to `Container.content` to be in line with
the corresponding `Element` function name. This has also been done for
the properties structs used by the `Container`.
This commit is contained in:
2025-03-11 07:52:35 +01:00
parent adda53c5a9
commit dddc09b4ce
15 changed files with 37 additions and 56 deletions

View File

@@ -38,7 +38,7 @@ pub const Border = packed struct {
pub const vertical: @This() = .{ .top = true, .bottom = true };
} = .{},
pub fn contents(this: @This(), cells: []Cell, size: Point) void {
pub fn content(this: @This(), cells: []Cell, size: Point) void {
std.debug.assert(cells.len == @as(usize, size.x) * @as(usize, size.y));
const frame = switch (this.corners) {
@@ -154,7 +154,7 @@ pub const Rectangle = packed struct {
fill: Color = .default,
// NOTE caller owns `Cells` slice and ensures that `cells.len == size.x * size.y`
pub fn contents(this: @This(), cells: []Cell, size: Point) void {
pub fn content(this: @This(), cells: []Cell, size: Point) void {
std.debug.assert(cells.len == @as(usize, size.x) * @as(usize, size.y));
for (0..size.y) |row| {
@@ -341,7 +341,7 @@ pub const Layout = packed struct {
} = .line,
} = .{},
pub fn contents(this: @This(), comptime C: type, cells: []Cell, origin: Point, size: Point, children: []const C) void {
pub fn content(this: @This(), comptime C: type, cells: []Cell, origin: Point, size: Point, children: []const C) void {
std.debug.assert(cells.len == @as(usize, size.x) * @as(usize, size.y));
if (this.separator.enabled and children.len > 1) {
@@ -878,17 +878,17 @@ pub fn Container(comptime Event: type) type {
}
}
pub fn contents(this: *const @This()) ![]const Cell {
pub fn content(this: *const @This()) ![]const Cell {
if (this.size.x == 0 or this.size.y == 0) return Error.TooSmall;
const cells = try this.allocator.alloc(Cell, @as(usize, this.size.x) * @as(usize, this.size.y));
@memset(cells, .{});
errdefer this.allocator.free(cells);
this.properties.layout.contents(@This(), cells, this.origin, this.size, this.elements.items);
this.properties.border.contents(cells, this.size);
this.properties.rectangle.contents(cells, this.size);
this.properties.layout.content(@This(), cells, this.origin, this.size, this.elements.items);
this.properties.border.content(cells, this.size);
this.properties.rectangle.content(cells, this.size);
try this.element.content(cells, this.origin, this.size);
try this.element.content(cells, this.size);
// DEBUG render corresponding top left corner of this `Container` *red*
// cells[0].style.fg = .red;

View File

@@ -16,7 +16,7 @@ pub fn Element(Event: type) type {
resize: ?*const fn (ctx: *anyopaque, size: Point) void = null,
reposition: ?*const fn (ctx: *anyopaque, origin: Point) void = null,
handle: ?*const fn (ctx: *anyopaque, event: Event) anyerror!void = null,
content: ?*const fn (ctx: *anyopaque, cells: []Cell, origin: Point, size: Point) anyerror!void = null,
content: ?*const fn (ctx: *anyopaque, cells: []Cell, size: Point) anyerror!void = null,
};
/// Resize the corresponding `Element` with the given *size*.
@@ -58,9 +58,9 @@ pub fn Element(Event: type) type {
/// non-recoverable (i.e. an allocation error, system error, etc.).
/// Otherwise user specific errors should be caught using the `handle`
/// function before the rendering of the `Container` happens.
pub inline fn content(this: @This(), cells: []Cell, origin: Point, size: Point) !void {
pub inline fn content(this: @This(), cells: []Cell, size: Point) !void {
if (this.vtable.content) |content_fn|
try content_fn(this.ptr, cells, origin, size);
try content_fn(this.ptr, cells, size);
}
};
}
@@ -130,13 +130,13 @@ pub fn Scrollable(Event: type) type {
}
}
fn render_container(container: Container(Event), cells: []Cell, container_origin: Point, container_size: Point) !void {
fn render_container(container: Container(Event), cells: []Cell, container_size: Point) !void {
const size = container.size;
const origin = container.origin;
const contents = try container.contents();
const contents = try container.content();
defer container.allocator.free(contents);
const anchor = (@as(usize, origin.y -| container_origin.y) * @as(usize, container_size.x)) + @as(usize, origin.x -| container_origin.x);
const anchor = (@as(usize, origin.y) * @as(usize, container_size.x)) + @as(usize, origin.x);
var idx: usize = 0;
blk: for (0..size.y) |row| {
@@ -148,25 +148,23 @@ pub fn Scrollable(Event: type) type {
}
}
for (container.elements.items) |child| try render_container(child, cells, origin, size);
for (container.elements.items) |child| try render_container(child, cells, size);
}
fn content(ctx: *anyopaque, cells: []Cell, origin: Point, size: Point) !void {
fn content(ctx: *anyopaque, cells: []Cell, size: Point) !void {
const this: *@This() = @ptrCast(@alignCast(ctx));
_ = origin;
std.debug.assert(cells.len == @as(usize, this.size.x) * @as(usize, this.size.y));
const container_size = this.container.size;
const container_origin = this.container.origin;
const container_cells = try this.container.allocator.alloc(Cell, @as(usize, container_size.x) * @as(usize, container_size.y));
{
const container_cells_const = try this.container.contents();
const container_cells_const = try this.container.content();
defer this.container.allocator.free(container_cells_const);
std.debug.assert(container_cells_const.len == @as(usize, container_size.x) * @as(usize, container_size.y));
@memcpy(container_cells, container_cells_const);
}
for (this.container.elements.items) |child| try render_container(child, container_cells, container_origin, container_size);
for (this.container.elements.items) |child| try render_container(child, container_cells, container_size);
const anchor = (@as(usize, this.anchor.y) * @as(usize, container_size.x)) + @as(usize, this.anchor.x);
// TODO render scrollbar according to configuration!

View File

@@ -64,7 +64,7 @@ pub const Buffered = struct {
pub fn render(this: *@This(), comptime T: type, container: *T) !void {
const size: Point = container.size;
const origin: Point = container.origin;
const cells: []const Cell = try container.contents();
const cells: []const Cell = try container.content();
if (cells.len == 0) return;

View File

@@ -48,7 +48,7 @@ pub const Renderer = struct {
pub fn render(this: *@This(), comptime T: type, container: *const T) !void {
const size: Point = container.size;
const origin: Point = container.origin;
const cells: []const Cell = try container.contents();
const cells: []const Cell = try container.content();
if (cells.len == 0) return;