From 6ae5dc5c4c2e969856d952f6f232caf1bf334b1e Mon Sep 17 00:00:00 2001 From: CJ van den Berg Date: Wed, 23 Apr 2025 19:46:27 +0200 Subject: [PATCH] feat: add support for init/deinit functions in Button context values --- src/tui/Button.zig | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/tui/Button.zig b/src/tui/Button.zig index ac9fa5b..60fbc41 100644 --- a/src/tui/Button.zig +++ b/src/tui/Button.zig @@ -57,6 +57,7 @@ pub fn create(ctx_type: type, allocator: std.mem.Allocator, parent: Plane, opts: .opts = opts, }; self.opts.label = try self.allocator.dupe(u8, opts.label); + try self.init(); return self; } @@ -75,8 +76,18 @@ pub fn State(ctx_type: type) type { const Self = @This(); pub const Context = ctx_type; + const child: type = switch (@typeInfo(Context)) { + .pointer => |p| p.child, + .@"struct" => Context, + else => struct {}, + }; + + pub fn init(self: *Self) error{OutOfMemory}!void { + if (@hasDecl(child, "ctx_init")) return self.opts.ctx.ctx_init(); + } pub fn deinit(self: *Self, allocator: std.mem.Allocator) void { + if (@hasDecl(child, "ctx_deinit")) self.opts.ctx.ctx_deinit(); self.allocator.free(self.opts.label); self.plane.deinit(); allocator.destroy(self);