add(element): interface for injecting user behavior to containers
Some additional refactoring and documentation updates have also been applied.
This commit is contained in:
@@ -27,17 +27,12 @@ pub const Border = packed struct {
|
||||
left: bool = false,
|
||||
right: bool = false,
|
||||
|
||||
pub fn all() @This() {
|
||||
return .{ .top = true, .bottom = true, .left = true, .right = true };
|
||||
}
|
||||
|
||||
pub fn horizontal() @This() {
|
||||
return .{ .left = true, .right = true };
|
||||
}
|
||||
|
||||
pub fn vertical() @This() {
|
||||
return .{ .top = true, .bottom = true };
|
||||
}
|
||||
/// Enable border sides for all four sides
|
||||
pub const all: @This() = .{ .top = true, .bottom = true, .left = true, .right = true };
|
||||
/// Enable border sides for the left and right sides
|
||||
pub const horizontal: @This() = .{ .left = true, .right = true };
|
||||
/// Enable border sides for the top and bottom sides
|
||||
pub const vertical: @This() = .{ .top = true, .bottom = true };
|
||||
} = .{},
|
||||
/// Configure separator borders between child element to added to the layout
|
||||
separator: packed struct {
|
||||
@@ -235,10 +230,12 @@ pub fn Container(comptime Event: type) type {
|
||||
if (!isTaggedUnion(Event)) {
|
||||
@compileError("Provided user event `Event` for `Container(comptime Event: type)`");
|
||||
}
|
||||
const Element = @import("element.zig").Element(Event);
|
||||
return struct {
|
||||
allocator: std.mem.Allocator,
|
||||
size: Size,
|
||||
properties: Properties,
|
||||
element: Element,
|
||||
elements: std.ArrayList(@This()),
|
||||
|
||||
/// Properties for each `Container` to configure their layout,
|
||||
@@ -251,11 +248,16 @@ pub fn Container(comptime Event: type) type {
|
||||
layout: Layout = .{},
|
||||
};
|
||||
|
||||
pub fn init(allocator: std.mem.Allocator, properties: Properties) !@This() {
|
||||
pub fn init(
|
||||
allocator: std.mem.Allocator,
|
||||
properties: Properties,
|
||||
element: Element,
|
||||
) !@This() {
|
||||
return .{
|
||||
.allocator = allocator,
|
||||
.size = .{},
|
||||
.properties = properties,
|
||||
.element = element,
|
||||
.elements = std.ArrayList(@This()).init(allocator),
|
||||
};
|
||||
}
|
||||
@@ -273,7 +275,6 @@ pub fn Container(comptime Event: type) type {
|
||||
|
||||
pub fn handle(this: *@This(), event: Event) !void {
|
||||
switch (event) {
|
||||
.init => log.debug(".init event", .{}),
|
||||
.resize => |size| resize: {
|
||||
log.debug("Event .resize: {{ .anchor = {{ .col = {d}, .row = {d} }}, .cols = {d}, .rows = {d} }}", .{
|
||||
size.anchor.col,
|
||||
@@ -384,29 +385,29 @@ pub fn Container(comptime Event: type) type {
|
||||
}
|
||||
|
||||
// border resizing
|
||||
if (sides.top) {
|
||||
element_size.anchor.row += 1;
|
||||
}
|
||||
if (sides.left) {
|
||||
element_size.anchor.col += 1;
|
||||
}
|
||||
|
||||
// padding resizing
|
||||
if (sides.top) element_size.anchor.row += 1;
|
||||
if (sides.left) element_size.anchor.col += 1;
|
||||
|
||||
try element.handle(.{ .resize = element_size });
|
||||
}
|
||||
},
|
||||
else => for (this.elements.items) |*element| try element.handle(event),
|
||||
else => {
|
||||
try this.element.handle(event);
|
||||
for (this.elements.items) |*element| try element.handle(event);
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
pub fn contents(this: *const @This()) ![]const Cell {
|
||||
const cells = try this.allocator.alloc(Cell, @as(usize, this.size.cols) * @as(usize, this.size.rows));
|
||||
@memset(cells, .{});
|
||||
errdefer this.allocator.free(cells);
|
||||
|
||||
this.properties.border.contents(cells, this.size, this.properties.layout, @truncate(this.elements.items.len));
|
||||
this.properties.rectangle.contents(cells, this.size);
|
||||
|
||||
try this.element.content(cells, this.size);
|
||||
|
||||
return cells;
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user