mod: replace Layout.content with Layout.render
Some checks failed
Zig Project Action / Lint, Spell-check and test zig project (push) Failing after 30s

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.
This commit is contained in:
2024-11-10 14:34:28 +01:00
parent b32556720e
commit b314ff7813
12 changed files with 112 additions and 92 deletions

View File

@@ -1,7 +1,7 @@
//! 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) {}
//! - render(this: *@This(), renderer: Renderer) anyerror!void {}
//! - deinit(this: *@This()) void {}
//!
//! Create a `Layout` using `createFrom(object: anytype)` and use them through
@@ -11,10 +11,13 @@
//! 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) type {
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)`.");
}
@@ -25,7 +28,7 @@ pub fn Layout(comptime Event: type) type {
const VTable = struct {
handle: *const fn (this: *LayoutType, event: Event) anyerror!*Events,
content: *const fn (this: *LayoutType) anyerror!*std.ArrayList(u8),
render: *const fn (this: *LayoutType, renderer: Renderer) anyerror!void,
deinit: *const fn (this: *LayoutType) void,
};
@@ -37,9 +40,9 @@ pub fn Layout(comptime Event: type) type {
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);
// 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 {
@@ -58,13 +61,13 @@ pub fn Layout(comptime Event: type) type {
return try layout.handle(event);
}
}.handle,
.content = struct {
// Return the entire content of this `Layout`.
fn content(this: *LayoutType) !*std.ArrayList(u8) {
.render = struct {
// Render the contents of this `Layout`.
fn render(this: *LayoutType, renderer: Renderer) !void {
const layout: @TypeOf(object) = @ptrFromInt(this.object);
return try layout.content();
try layout.render(renderer);
}
}.content,
}.render,
.deinit = struct {
fn deinit(this: *LayoutType) void {
const layout: @TypeOf(object) = @ptrFromInt(this.object);
@@ -76,9 +79,9 @@ pub fn Layout(comptime Event: type) type {
}
// 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);
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);
};
}