From e9be9a1c359a85f3ac5eb6c5dac00b2370316ac2 Mon Sep 17 00:00:00 2001 From: Yves Biener Date: Wed, 15 Jan 2025 11:00:11 +0100 Subject: [PATCH] add(View): `enable` and `disable` interface functions --- src/view.zig | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/src/view.zig b/src/view.zig index 7770ba7..cc9995b 100644 --- a/src/view.zig +++ b/src/view.zig @@ -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));