add(view): View type for composing view modules
Some checks failed
Zig Project Action / Lint, Spell-check and test zig project (push) Failing after 22s
Some checks failed
Zig Project Action / Lint, Spell-check and test zig project (push) Failing after 22s
This commit is contained in:
@@ -48,6 +48,7 @@ pub fn App(comptime E: type, comptime R: fn (comptime bool) type, comptime fulls
|
||||
pub const Renderer = R(fullscreen);
|
||||
pub const Layout = @import("layout.zig").Layout(Event, Renderer);
|
||||
pub const Widget = @import("widget.zig").Widget(Event, Renderer);
|
||||
pub const View = @import("view.zig").View(Event, Renderer);
|
||||
|
||||
queue: Queue(Event, 256) = .{},
|
||||
thread: ?std.Thread = null,
|
||||
|
||||
@@ -12,6 +12,7 @@ pub const Error = struct {
|
||||
};
|
||||
|
||||
// System events available to every application.
|
||||
// TODO: should this also already include the .view enum option?
|
||||
pub const SystemEvent = union(enum) {
|
||||
quit,
|
||||
err: Error,
|
||||
@@ -21,6 +22,7 @@ pub const SystemEvent = union(enum) {
|
||||
};
|
||||
|
||||
pub fn mergeTaggedUnions(comptime A: type, comptime B: type) type {
|
||||
// TODO: should this expect one of the unions to contain the .view value option with its corresponding associated type?
|
||||
if (!isTaggedUnion(A) or !isTaggedUnion(B)) {
|
||||
@compileError("Both types for merging tagged unions need to be of type `union(enum)`.");
|
||||
}
|
||||
|
||||
89
src/view.zig
Normal file
89
src/view.zig
Normal file
@@ -0,0 +1,89 @@
|
||||
//! Dynamic dispatch for view implementations. Each `View` has to implement the `View.Interface`
|
||||
//!
|
||||
//! Create a `View` using `createFrom(object: anytype)` and use them through
|
||||
//! the defined `View.Interface`. The view will take care of calling the
|
||||
//! correct implementation of the corresponding underlying type.
|
||||
//!
|
||||
//! A `View` holds the necessary `Layout`'s for different screen sizes as well
|
||||
//! as the corresponding used `Widget`'s alongside holding the corresponding memory
|
||||
//! for the data shown through the `View`.
|
||||
const std = @import("std");
|
||||
|
||||
const isTaggedUnion = @import("event.zig").isTaggedUnion;
|
||||
|
||||
const log = std.log.scoped(.view);
|
||||
|
||||
pub fn View(comptime Event: type, comptime Renderer: type) type {
|
||||
if (!isTaggedUnion(Event)) {
|
||||
@compileError("Provided user event `Event` for `View(comptime Event: type)` is not of type `union(enum)`.");
|
||||
}
|
||||
const Events = std.ArrayList(Event);
|
||||
return struct {
|
||||
const ViewType = @This();
|
||||
pub const Interface = @import("interface").Interface(.{
|
||||
.handle = fn (anytype, Event) anyerror!*Events,
|
||||
.render = fn (anytype, *Renderer) anyerror!void,
|
||||
.deinit = fn (anytype) void,
|
||||
}, .{});
|
||||
|
||||
const VTable = struct {
|
||||
handle: *const fn (this: *ViewType, event: Event) anyerror!*Events,
|
||||
render: *const fn (this: *ViewType, renderer: *Renderer) anyerror!void,
|
||||
deinit: *const fn (this: *ViewType) void,
|
||||
};
|
||||
|
||||
object: *anyopaque = undefined,
|
||||
vtable: *const VTable = undefined,
|
||||
|
||||
/// Handle the provided `Event` for this `View`.
|
||||
pub fn handle(this: *ViewType, event: Event) anyerror!*Events {
|
||||
switch (event) {
|
||||
.resize => |size| {
|
||||
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);
|
||||
}
|
||||
|
||||
/// Render the content of this `View` given the `Size` of the available terminal (.resize System`Event`).
|
||||
pub fn render(this: *ViewType, renderer: *Renderer) !void {
|
||||
try this.vtable.render(this, renderer);
|
||||
}
|
||||
|
||||
pub fn deinit(this: *ViewType) void {
|
||||
this.vtable.deinit(this);
|
||||
}
|
||||
|
||||
pub fn createFrom(object: anytype) ViewType {
|
||||
return ViewType{
|
||||
.object = @ptrCast(@alignCast(object)),
|
||||
.vtable = &.{
|
||||
.handle = struct {
|
||||
fn handle(this: *ViewType, event: Event) !*Events {
|
||||
const view: @TypeOf(object) = @ptrCast(@alignCast(this.object));
|
||||
return view.handle(event);
|
||||
}
|
||||
}.handle,
|
||||
.render = struct {
|
||||
fn render(this: *ViewType, renderer: *Renderer) !void {
|
||||
const view: @TypeOf(object) = @ptrCast(@alignCast(this.object));
|
||||
try view.render(renderer);
|
||||
}
|
||||
}.render,
|
||||
.deinit = struct {
|
||||
fn deinit(this: *ViewType) void {
|
||||
const view: @TypeOf(object) = @ptrCast(@alignCast(this.object));
|
||||
view.deinit();
|
||||
}
|
||||
}.deinit,
|
||||
},
|
||||
};
|
||||
}
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user