mod: each layout and widget now allocates their own instance in memory using the provided allocator (and destroy's themselfes in the end)
All checks were successful
Zig Project Action / Lint, Spell-check and test zig project (push) Successful in 36s

This commit is contained in:
2024-11-16 19:56:36 +01:00
parent f4adf53067
commit ec71e34958
14 changed files with 232 additions and 270 deletions

View File

@@ -36,11 +36,12 @@ pub fn Layout(comptime Event: type, comptime Element: type, comptime Renderer: t
const Events = std.ArrayList(Event);
return struct {
// TODO: current focused `Element`?
allocator: std.mem.Allocator = undefined,
size: terminal.Size = undefined,
containers: Containers = undefined,
events: Events = undefined,
pub fn init(allocator: std.mem.Allocator, children: anytype) @This() {
pub fn init(allocator: std.mem.Allocator, children: anytype) *@This() {
const ArgsType = @TypeOf(children);
const args_type_info = @typeInfo(ArgsType);
if (args_type_info != .Struct) {
@@ -87,10 +88,11 @@ pub fn Layout(comptime Event: type, comptime Element: type, comptime Renderer: t
}
@compileError("nested child: " ++ field.name ++ " is not of type " ++ @typeName(WidgetType) ++ " or " ++ @typeName(LayoutType) ++ " but " ++ @typeName(ChildType));
}
return .{
.containers = containers,
.events = Events.init(allocator),
};
const layout = allocator.create(@This()) catch @panic("OOM");
layout.allocator = allocator;
layout.containers = containers;
layout.events = Events.init(allocator);
return layout;
}
pub fn deinit(this: *@This()) void {
@@ -106,6 +108,8 @@ pub fn Layout(comptime Event: type, comptime Element: type, comptime Renderer: t
}
}
this.containers.deinit();
this.allocator.destroy(this);
this.* = undefined;
}
pub fn handle(this: *@This(), event: Event) !*Events {