mod: change widget interface Widget.content replaced with Widget.render
Some checks failed
Zig Project Action / Lint, Spell-check and test zig project (push) Failing after 29s

The .resize `Event` has been adapted to include an _anchor_, which
provide the full necessary information for each widget where to render
on the screen with what requested size. Each Widget can then dynamically
decide how and what to render (i.e. provide placeholder text in case the
size is too small, etc.).
This commit is contained in:
2024-11-10 15:53:28 +01:00
parent 7872223c24
commit a201f2b653
9 changed files with 54 additions and 32 deletions

View File

@@ -1,7 +1,7 @@
//! Dynamic dispatch for widget implementations.
//! Each widget should at last implement these functions:
//! - handle(this: *@This(), event: Event) ?Event {}
//! - content(this: *@This()) ![]u8 {}
//! - render(this: *@This(), renderer: Renderer) !void {}
//! - deinit(this: *@This()) void {}
//!
//! Create a `Widget` using `createFrom(object: anytype)` and use them through
@@ -10,12 +10,14 @@
//!
//! Each `Widget` may cache its content and should if the contents will not
//! change for a long time.
const std = @import("std");
//!
//! When `Widget.render` is called the provided `Renderer` type is expected
//! which handles how contents are rendered for a given widget.
const isTaggedUnion = @import("event.zig").isTaggedUnion;
const log = std.log.scoped(.widget);
const log = @import("std").log.scoped(.widget);
pub fn Widget(comptime Event: type) type {
pub fn Widget(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 +27,7 @@ pub fn Widget(comptime Event: type) type {
const VTable = struct {
handle: *const fn (this: *WidgetType, event: Event) ?Event,
content: *const fn (this: *WidgetType) anyerror![]u8,
render: *const fn (this: *WidgetType, renderer: Renderer) anyerror!void,
deinit: *const fn (this: *WidgetType) void,
};
@@ -36,16 +38,21 @@ pub fn Widget(comptime Event: type) type {
pub fn handle(this: *WidgetType, event: Event) ?Event {
switch (event) {
.resize => |size| {
log.debug("received size: .{{ .cols = {d}, .rows = {d} }}", .{ size.cols, size.rows });
log.debug("Event .resize: {{ .anchor = {{ .col = {d}, .row = {d} }}, .cols = {d}, .rows = {d} }}", .{
size.anchor.col,
size.anchor.row,
size.cols,
size.rows,
});
},
else => {},
}
return this.vtable.handle(this, event);
}
// Return the entire content of this `Widget`.
pub fn content(this: *WidgetType) ![]u8 {
return try this.vtable.content(this);
// Render the content of this `Widget` given the `Size` of the widget (.resize System`Event`).
pub fn render(this: *WidgetType, renderer: Renderer) !void {
try this.vtable.render(this, renderer);
}
pub fn deinit(this: *WidgetType) void {
@@ -64,13 +71,13 @@ pub fn Widget(comptime Event: type) type {
return widget.handle(event);
}
}.handle,
.content = struct {
.render = struct {
// Return the entire content of this `Widget`.
fn content(this: *WidgetType) ![]u8 {
fn render(this: *WidgetType, renderer: Renderer) !void {
const widget: @TypeOf(object) = @ptrFromInt(this.object);
return try widget.content();
try widget.render(renderer);
}
}.content,
}.render,
.deinit = struct {
fn deinit(this: *WidgetType) void {
const widget: @TypeOf(object) = @ptrFromInt(this.object);
@@ -82,7 +89,7 @@ pub fn Widget(comptime Event: type) type {
}
// TODO: import and export of `Widget` implementations (with corresponding initialization using `Event`)
pub const RawText = @import("widget/RawText.zig").Widget(Event);
pub const Spacer = @import("widget/Spacer.zig").Widget(Event);
pub const RawText = @import("widget/RawText.zig").Widget(Event, Renderer);
pub const Spacer = @import("widget/Spacer.zig").Widget(Event, Renderer);
};
}