add: layout and widget dynamic dispatch with interface definitions

This commit is contained in:
2024-11-02 17:52:44 +01:00
parent c62fc6fb43
commit 0330b3a2f5
11 changed files with 502 additions and 480 deletions

39
src/layout/Pane.zig Normal file
View File

@@ -0,0 +1,39 @@
const std = @import("std");
const lib_event = @import("../event.zig");
const widget = @import("../widget.zig");
pub fn Layout(comptime E: type) type {
if (!lib_event.isTaggedUnion(E)) {
@compileError("Provided user event `E` for `Layout(comptime E: type)` is not of type `union(enum)`.");
}
return struct {
pub const Event = lib_event.MergeTaggedUnions(lib_event.BuiltinEvent, E);
w: widget.Widget(E) = undefined,
c: std.ArrayList(u8) = undefined,
pub fn init(allocator: std.mem.Allocator, w: widget.Widget(E)) @This() {
return .{
.w = w,
.c = std.ArrayList(u8).init(allocator),
};
}
pub fn deinit(this: *@This()) void {
this.w.deinit();
this.c.deinit();
this.* = undefined;
}
pub fn handle(this: *@This(), event: Event) Event {
return this.w.handle(event);
}
pub fn content(this: *@This()) *std.ArrayList(u8) {
const widget_content = this.w.content();
this.c.clearRetainingCapacity();
this.c.appendSlice(widget_content.items) catch @panic("OOM");
return &this.c;
}
};
}