Some checks failed
Zig Project Action / Lint, Spell-check and test zig project (push) Failing after 4m26s
48 lines
1.4 KiB
Zig
48 lines
1.4 KiB
Zig
const std = @import("std");
|
|
const terminal = @import("../terminal.zig");
|
|
|
|
const isTaggedUnion = @import("../event.zig").isTaggedUnion;
|
|
const Error = @import("../event.zig").Error;
|
|
|
|
const log = std.log.scoped(.widget_spacer);
|
|
|
|
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 {
|
|
allocator: std.mem.Allocator,
|
|
size: terminal.Size,
|
|
size_changed: bool,
|
|
|
|
pub fn init(allocator: std.mem.Allocator) *@This() {
|
|
var this = allocator.create(@This()) catch @panic("Space.zig: Failed to create.");
|
|
this.allocator = allocator;
|
|
this.size_changed = true;
|
|
return this;
|
|
}
|
|
|
|
pub fn deinit(this: *@This()) void {
|
|
this.allocator.destroy(this);
|
|
}
|
|
|
|
pub fn handle(this: *@This(), event: Event) ?Event {
|
|
switch (event) {
|
|
.resize => |size| {
|
|
this.size = size;
|
|
this.size_changed = true;
|
|
},
|
|
else => {},
|
|
}
|
|
return null;
|
|
}
|
|
|
|
pub fn render(this: *@This(), renderer: *Renderer) !void {
|
|
if (this.size_changed) {
|
|
try renderer.clear(this.size);
|
|
this.size_changed = false;
|
|
}
|
|
}
|
|
};
|
|
}
|