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

@@ -26,6 +26,7 @@ pub fn Layout(comptime Event: type, comptime Element: type, comptime Renderer: t
}
const Events = std.ArrayList(Event);
return struct {
allocator: std.mem.Allocator = undefined,
size: terminal.Size = undefined,
require_render: bool = false,
element: Element = undefined,
@@ -40,18 +41,19 @@ pub fn Layout(comptime Event: type, comptime Element: type, comptime Renderer: t
bottom: u8 = 0,
};
pub fn init(allocator: std.mem.Allocator, config: Config, element: Element) @This() {
pub fn init(allocator: std.mem.Allocator, config: Config, element: Element) *@This() {
if (config.margin) |margin| {
std.debug.assert(margin <= 50);
} else {
std.debug.assert(config.left + config.right < 100);
std.debug.assert(config.top + config.bottom < 100);
}
return .{
.config = config,
.element = element,
.events = Events.init(allocator),
};
const layout = allocator.create(@This()) catch @panic("OOM");
layout.allocator = allocator;
layout.config = config;
layout.element = element;
layout.events = Events.init(allocator);
return layout;
}
pub fn deinit(this: *@This()) void {
@@ -64,6 +66,8 @@ pub fn Layout(comptime Event: type, comptime Element: type, comptime Renderer: t
widget.deinit();
},
}
this.allocator.destroy(this);
this.* = undefined;
}
pub fn handle(this: *@This(), event: Event) !*Events {