add(elements): Template for defining an Element
33
Elements.md
33
Elements.md
@@ -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`
|
||||
|
||||
Reference in New Issue
Block a user