add: layout and widget dynamic dispatch with interface definitions
This commit is contained in:
39
src/layout/Pane.zig
Normal file
39
src/layout/Pane.zig
Normal 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;
|
||||
}
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user