add(elements): Template for defining an Element

2025-02-25 16:54:36 +01:00
parent 1a8f4a36f7
commit 48779e7326

@@ -8,6 +8,39 @@ library should instead provide small examples to show how you can implement
such user fields yourself and connect them using your own event system loops to
communicate with other `Container`s and/or `Element`s.
## Template
```zig
/// This is an empty template implementation for an `Element` type.
pub fn Template(Event: type) type {
return packed struct {
pub fn element(this: *@This()) Element(Event) {
return .{
.ptr = this,
.vtable = &.{
.handle = handle,
.content = content,
},
};
}
fn handle(ctx: *anyopaque, event: Event) !void {
const this: *@This() = @ptrCast(@alignCast(ctx));
_ = this;
switch (event) {
else => {},
}
}
fn content(ctx: *anyopaque, cells: []Cell, size: Size) !void {
const this: *@This() = @ptrCast(@alignCast(ctx));
std.debug.assert(cells.len == @as(usize, size.cols) * @as(usize, size.rows));
_ = this;
}
};
}
```
### User specific event handling and content rendering
For interactions controlled by the user each container can use an `Element`