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

@@ -30,11 +30,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,
elements: Elements = 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) {
@@ -55,10 +56,11 @@ pub fn Layout(comptime Event: type, comptime Element: type, comptime Renderer: t
}
@compileError("child: " ++ field.name ++ " is not of type " ++ @typeName(WidgetType) ++ " or " ++ @typeName(LayoutType) ++ " but " ++ @typeName(ChildType));
}
return .{
.elements = elements,
.events = Events.init(allocator),
};
const layout = allocator.create(@This()) catch @panic("OOM");
layout.allocator = allocator;
layout.elements = elements;
layout.events = Events.init(allocator);
return layout;
}
pub fn deinit(this: *@This()) void {
@@ -74,6 +76,8 @@ pub fn Layout(comptime Event: type, comptime Element: type, comptime Renderer: t
}
}
this.elements.deinit();
this.allocator.destroy(this);
this.* = undefined;
}
pub fn handle(this: *@This(), event: Event) !*Events {