51 lines
1.5 KiB
Zig
51 lines
1.5 KiB
Zig
const std = @import("std");
|
|
const terminal = @import("../terminal.zig");
|
|
|
|
const isTaggedUnion = @import("../event.zig").isTaggedUnion;
|
|
const Error = @import("../event.zig").Error;
|
|
const Cell = terminal.Cell;
|
|
|
|
const log = std.log.scoped(.widget_text);
|
|
|
|
pub fn Widget(comptime Event: type, comptime Renderer: type) type {
|
|
if (!isTaggedUnion(Event)) {
|
|
@compileError("Provided user event `Event` for `Layout(comptime Event: type)` is not of type `union(enum)`.");
|
|
}
|
|
return struct {
|
|
contents: []const Cell = undefined,
|
|
size: terminal.Size = undefined,
|
|
require_render: bool = false,
|
|
|
|
pub fn init(contents: []const Cell) @This() {
|
|
return .{
|
|
.contents = contents,
|
|
};
|
|
}
|
|
|
|
pub fn deinit(this: *@This()) void {
|
|
this.* = undefined;
|
|
}
|
|
|
|
pub fn handle(this: *@This(), event: Event) ?Event {
|
|
switch (event) {
|
|
// store the received size
|
|
.resize => |size| {
|
|
this.size = size;
|
|
this.require_render = true;
|
|
},
|
|
else => {},
|
|
}
|
|
return null;
|
|
}
|
|
|
|
pub fn render(this: *@This(), renderer: *Renderer) !void {
|
|
if (!this.require_render) {
|
|
return;
|
|
}
|
|
try renderer.clear(this.size);
|
|
try renderer.render(this.size, this.contents);
|
|
this.require_render = false;
|
|
}
|
|
};
|
|
}
|