add(element): interface for injecting user behavior to containers

Some additional refactoring and documentation updates have also been
applied.
This commit is contained in:
2025-02-15 15:56:30 +01:00
parent 5c148e1aa5
commit 4781e9ce39
5 changed files with 128 additions and 45 deletions

View File

@@ -0,0 +1,23 @@
//! Interface for Element's which describe the contents of a `Container`.
const Cell = @import("cell.zig");
const Size = @import("size.zig").Size;
pub fn Element(Event: type) type {
return struct {
ptr: *anyopaque = undefined,
vtable: *const VTable = &.{},
pub const VTable = struct {
handle: ?*const fn (ctx: *anyopaque, event: Event) anyerror!void = null,
content: ?*const fn (ctx: *anyopaque, cells: []Cell, size: Size) anyerror!void = null,
};
pub inline fn handle(this: @This(), event: Event) !void {
if (this.vtable.handle) |handle_fn| try handle_fn(this.ptr, event);
}
pub inline fn content(this: @This(), cells: []Cell, size: Size) !void {
if (this.vtable.content) |content_fn| try content_fn(this.ptr, cells, size);
}
};
}