add(View): enable and disable interface functions
Some checks failed
Zig Project Action / Lint, Spell-check and test zig project (push) Failing after 38s

This commit is contained in:
2025-01-15 11:00:11 +01:00
parent f4a01f227e
commit e9be9a1c35

View File

@@ -23,12 +23,17 @@ pub fn View(comptime Event: type, comptime Renderer: type) type {
pub const Interface = @import("interface").Interface(.{
.handle = fn (anytype, Event) anyerror!*Events,
.render = fn (anytype, *Renderer) anyerror!void,
.enable = fn (anytype) void,
.disable = fn (anytype) void,
.deinit = fn (anytype) void,
}, .{});
// TODO: this VTable creation and abstraction could maybe even be done through a comptime implementation -> another library?
const VTable = struct {
handle: *const fn (this: *ViewType, event: Event) anyerror!*Events,
render: *const fn (this: *ViewType, renderer: *Renderer) anyerror!void,
enable: *const fn (this: *ViewType) void,
disable: *const fn (this: *ViewType) void,
deinit: *const fn (this: *ViewType) void,
};
@@ -36,7 +41,7 @@ pub fn View(comptime Event: type, comptime Renderer: type) type {
vtable: *const VTable = undefined,
/// Handle the provided `Event` for this `View`.
pub fn handle(this: *ViewType, event: Event) anyerror!*Events {
pub fn handle(this: *ViewType, event: Event) !*Events {
switch (event) {
.resize => |size| {
log.debug("Event .resize: {{ .anchor = {{ .col = {d}, .row = {d} }}, .cols = {d}, .rows = {d} }}", .{
@@ -56,6 +61,16 @@ pub fn View(comptime Event: type, comptime Renderer: type) type {
try this.vtable.render(this, renderer);
}
/// Function to call when this `View` will be handled and rendered as the 'active' `View`.
pub fn enable(this: *ViewType) void {
this.vtable.enable(this);
}
/// Function to call when this `View` will no longer be 'active'.
pub fn disable(this: *ViewType) void {
this.vtable.disable(this);
}
pub fn deinit(this: *ViewType) void {
this.vtable.deinit(this);
}
@@ -76,6 +91,18 @@ pub fn View(comptime Event: type, comptime Renderer: type) type {
try view.render(renderer);
}
}.render,
.enable = struct {
fn enable(this: *ViewType) void {
const view: @TypeOf(object) = @ptrCast(@alignCast(this.object));
view.enable();
}
}.enable,
.disable = struct {
fn disable(this: *ViewType) void {
const view: @TypeOf(object) = @ptrCast(@alignCast(this.object));
view.disable();
}
}.diable,
.deinit = struct {
fn deinit(this: *ViewType) void {
const view: @TypeOf(object) = @ptrCast(@alignCast(this.object));