Files
zterm/src/layout.zig
Yves Biener b314ff7813
Some checks failed
Zig Project Action / Lint, Spell-check and test zig project (push) Failing after 30s
mod: replace Layout.content with Layout.render
The App.Renderer is used for the new `Layout.render` method. Each layout
renders itself now with corresponding renderers which might only update
parts of the screen, etc.
2024-11-10 14:34:28 +01:00

88 lines
3.7 KiB
Zig

//! Dynamic dispatch for layout implementations.
//! Each layout should at last implement these functions:
//! - handle(this: *@This(), event: Event) anyerror!*std.ArrayList(Event) {}
//! - render(this: *@This(), renderer: Renderer) anyerror!void {}
//! - deinit(this: *@This()) void {}
//!
//! Create a `Layout` using `createFrom(object: anytype)` and use them through
//! the defined interface. The layout will take care of calling the correct
//! implementation of the corresponding underlying type.
//!
//! Each `Layout` is responsible for clearing the allocated memory of the used
//! widgets when deallocated. This means that `deinit()` will also deallocate
//! every used widget too.
//!
//! When `Layout.render` is called the provided `Renderer` type is expected
//! which handles how contents are rendered for a given layout.
const std = @import("std");
const isTaggedUnion = @import("event.zig").isTaggedUnion;
pub fn Layout(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)`.");
}
const Events = std.ArrayList(Event);
return struct {
const LayoutType = @This();
const Ptr = usize;
const VTable = struct {
handle: *const fn (this: *LayoutType, event: Event) anyerror!*Events,
render: *const fn (this: *LayoutType, renderer: Renderer) anyerror!void,
deinit: *const fn (this: *LayoutType) void,
};
object: Ptr = undefined,
vtable: *const VTable = undefined,
// Handle the provided `Event` for this `Layout`.
pub fn handle(this: *LayoutType, event: Event) !*Events {
return try this.vtable.handle(this, event);
}
// Render this `Layout` completely. This will render contained sub-elements too.
pub fn render(this: *LayoutType, renderer: Renderer) !void {
return try this.vtable.render(this, renderer);
}
pub fn deinit(this: *LayoutType) void {
this.vtable.deinit(this);
this.* = undefined;
}
pub fn createFrom(object: anytype) LayoutType {
return LayoutType{
.object = @intFromPtr(object),
.vtable = &.{
.handle = struct {
// Handle the provided `Event` for this `Layout`.
fn handle(this: *LayoutType, event: Event) !*Events {
const layout: @TypeOf(object) = @ptrFromInt(this.object);
return try layout.handle(event);
}
}.handle,
.render = struct {
// Render the contents of this `Layout`.
fn render(this: *LayoutType, renderer: Renderer) !void {
const layout: @TypeOf(object) = @ptrFromInt(this.object);
try layout.render(renderer);
}
}.render,
.deinit = struct {
fn deinit(this: *LayoutType) void {
const layout: @TypeOf(object) = @ptrFromInt(this.object);
layout.deinit();
}
}.deinit,
},
};
}
// import and export of `Layout` implementations
pub const HStack = @import("layout/HStack.zig").Layout(Event, Renderer);
pub const VStack = @import("layout/VStack.zig").Layout(Event, Renderer);
pub const Padding = @import("layout/Padding.zig").Layout(Event, Renderer);
pub const Framing = @import("layout/Framing.zig").Layout(Event, Renderer);
};
}