initial commit
Some checks failed
Zig Project Action / Lint, Spell-check and test zig project (push) Failing after 30s

This commit is contained in:
2024-11-09 21:24:42 +01:00
parent ff58e7ef69
commit 6d389bcd4b
21 changed files with 2738 additions and 0 deletions

84
src/layout.zig Normal file
View File

@@ -0,0 +1,84 @@
//! Dynamic dispatch for layout implementations.
//! Each layout should at last implement these functions:
//! - handle(this: *@This(), event: Event) anyerror!*std.ArrayList(Event) {}
//! - content(this: *@This()) anyerror!*std.ArrayList(u8) {}
//! - 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.
const std = @import("std");
const isTaggedUnion = @import("event.zig").isTaggedUnion;
pub fn Layout(comptime Event: 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,
content: *const fn (this: *LayoutType) anyerror!*std.ArrayList(u8),
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);
}
// Return the entire content of this `Layout`.
pub fn content(this: *LayoutType) !*std.ArrayList(u8) {
return try this.vtable.content(this);
}
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,
.content = struct {
// Return the entire content of this `Layout`.
fn content(this: *LayoutType) !*std.ArrayList(u8) {
const layout: @TypeOf(object) = @ptrFromInt(this.object);
return try layout.content();
}
}.content,
.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);
pub const VStack = @import("layout/VStack.zig").Layout(Event);
pub const Padding = @import("layout/Padding.zig").Layout(Event);
pub const Framing = @import("layout/Framing.zig").Layout(Event);
};
}