add(element): interface for injecting user behavior to containers
Some additional refactoring and documentation updates have also been applied.
This commit is contained in:
@@ -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);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user