From 48779e7326dfa55ac77e99520d5a631d2eef3847 Mon Sep 17 00:00:00 2001 From: Yves Biener Date: Tue, 25 Feb 2025 16:54:36 +0100 Subject: [PATCH] add(elements): Template for defining an `Element` --- Elements.md | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/Elements.md b/Elements.md index 47932b0..c632077 100644 --- a/Elements.md +++ b/Elements.md @@ -8,6 +8,39 @@ library should instead provide small examples to show how you can implement such user fields yourself and connect them using your own event system loops to communicate with other `Container`s and/or `Element`s. +## Template + +```zig +/// This is an empty template implementation for an `Element` type. +pub fn Template(Event: type) type { + return packed struct { + pub fn element(this: *@This()) Element(Event) { + return .{ + .ptr = this, + .vtable = &.{ + .handle = handle, + .content = content, + }, + }; + } + + fn handle(ctx: *anyopaque, event: Event) !void { + const this: *@This() = @ptrCast(@alignCast(ctx)); + _ = this; + switch (event) { + else => {}, + } + } + + fn content(ctx: *anyopaque, cells: []Cell, size: Size) !void { + const this: *@This() = @ptrCast(@alignCast(ctx)); + std.debug.assert(cells.len == @as(usize, size.cols) * @as(usize, size.rows)); + _ = this; + } + }; +} +``` + ### User specific event handling and content rendering For interactions controlled by the user each container can use an `Element`