mod: make Element's for rendering stateless
The elements derive their corresponding content to render from the `Model` without any local state.
This commit is contained in:
@@ -1,12 +1,10 @@
|
|||||||
pub fn Content(App: type) type {
|
pub fn Content(App: type) type {
|
||||||
return struct {
|
return struct {
|
||||||
allocator: Allocator,
|
allocator: Allocator,
|
||||||
document: *const App.Model.Document,
|
|
||||||
|
|
||||||
pub fn init(allocator: Allocator, document: *const App.Model.Document) @This() {
|
pub fn init(allocator: Allocator) @This() {
|
||||||
return .{
|
return .{
|
||||||
.allocator = allocator,
|
.allocator = allocator,
|
||||||
.document = document,
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -21,27 +19,22 @@ pub fn Content(App: type) type {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
fn minSize(ctx: *anyopaque, _: *const App.Model, size: zterm.Point) zterm.Point {
|
fn minSize(_: *anyopaque, model: *const App.Model, _: zterm.Point) zterm.Point {
|
||||||
const this: *const @This() = @ptrCast(@alignCast(ctx));
|
// NOTE currently the contents are assumed to fit into the viewport, excessive lines are cut off
|
||||||
_ = this;
|
// (there currently no horizontal scrolling)
|
||||||
//const text = this.document.content;
|
return .{ .y = @intCast(std.mem.count(u8, model.document.content, "\n") + 1) };
|
||||||
//var index: usize = 0;
|
|
||||||
const new_size: zterm.Point = .{ .x = size.x };
|
|
||||||
|
|
||||||
return new_size;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn handle(ctx: *anyopaque, model: *App.Model, event: App.Event) !void {
|
fn handle(ctx: *anyopaque, model: *App.Model, event: App.Event) !void {
|
||||||
|
// FIX this `Element` should not handle the de-/allocation of `Model` contents!
|
||||||
const this: *@This() = @ptrCast(@alignCast(ctx));
|
const this: *@This() = @ptrCast(@alignCast(ctx));
|
||||||
switch (event) {
|
switch (event) {
|
||||||
.init => this.document = &model.document,
|
|
||||||
.about => {
|
.about => {
|
||||||
model.page.deinit(this.allocator);
|
model.page.deinit(this.allocator);
|
||||||
model.page = .about;
|
model.page = .about;
|
||||||
|
|
||||||
model.document.deinit(this.allocator);
|
model.document.deinit(this.allocator);
|
||||||
model.document = .init(try std.fs.cwd().readFileAlloc(this.allocator, "./doc/about.md", std.math.maxInt(usize)));
|
model.document = .init(try std.fs.cwd().readFileAlloc(this.allocator, "./doc/about.md", std.math.maxInt(usize)));
|
||||||
this.document = &model.document;
|
|
||||||
},
|
},
|
||||||
.blog => |path| {
|
.blog => |path| {
|
||||||
model.page.deinit(this.allocator);
|
model.page.deinit(this.allocator);
|
||||||
@@ -54,15 +47,12 @@ pub fn Content(App: type) type {
|
|||||||
|
|
||||||
model.document = .init(try std.fs.cwd().readFileAlloc(this.allocator, if (path) |p| p else "./doc/blog.md", std.math.maxInt(usize)));
|
model.document = .init(try std.fs.cwd().readFileAlloc(this.allocator, if (path) |p| p else "./doc/blog.md", std.math.maxInt(usize)));
|
||||||
model.page = .{ .blog = path };
|
model.page = .{ .blog = path };
|
||||||
|
|
||||||
this.document = &model.document;
|
|
||||||
},
|
},
|
||||||
else => {},
|
else => {},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn content(ctx: *anyopaque, model: *const App.Model, cells: []zterm.Cell, size: zterm.Point) !void {
|
fn content(_: *anyopaque, model: *const App.Model, cells: []zterm.Cell, size: zterm.Point) !void {
|
||||||
_ = ctx;
|
|
||||||
assert(cells.len == @as(usize, size.x) * @as(usize, size.y));
|
assert(cells.len == @as(usize, size.x) * @as(usize, size.y));
|
||||||
|
|
||||||
const text = model.document.content;
|
const text = model.document.content;
|
||||||
@@ -81,6 +71,10 @@ pub fn Content(App: type) type {
|
|||||||
else => cp,
|
else => cp,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
if (text[index - 1] != '\n') {
|
||||||
|
// go to the end of the line
|
||||||
|
while (index < text.len and text[index] != '\n') index +=1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -97,9 +91,7 @@ pub fn Title(App: type) type {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
fn content(ctx: *anyopaque, model: *const App.Model, cells: []zterm.Cell, size: zterm.Point) !void {
|
fn content(_: *anyopaque, model: *const App.Model, cells: []zterm.Cell, size: zterm.Point) !void {
|
||||||
const this: *const @This() = @ptrCast(@alignCast(ctx));
|
|
||||||
_ = this;
|
|
||||||
assert(cells.len == @as(usize, size.x) * @as(usize, size.y));
|
assert(cells.len == @as(usize, size.x) * @as(usize, size.y));
|
||||||
|
|
||||||
if (model.document.title) |text| {
|
if (model.document.title) |text| {
|
||||||
@@ -117,10 +109,10 @@ pub fn Title(App: type) type {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn InfoBanner(App: type) type {
|
pub fn InfoBanner(App: type) type {
|
||||||
return struct {
|
const left_text: []const u8 = "Build with zig";
|
||||||
left_text: []const u8 = "Build with zig",
|
const right_text: []const u8 = "Yves Biener (@yves-biener)";
|
||||||
right_text: []const u8 = "Yves Biener (@yves-biener)",
|
|
||||||
|
|
||||||
|
return struct {
|
||||||
pub fn element(this: *@This()) App.Element {
|
pub fn element(this: *@This()) App.Element {
|
||||||
return .{
|
return .{
|
||||||
.ptr = this,
|
.ptr = this,
|
||||||
@@ -130,11 +122,10 @@ pub fn InfoBanner(App: type) type {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
fn content(ctx: *anyopaque, model: *const App.Model, cells: []zterm.Cell, size: zterm.Point) !void {
|
fn content(_: *anyopaque, model: *const App.Model, cells: []zterm.Cell, size: zterm.Point) !void {
|
||||||
const this: *const @This() = @ptrCast(@alignCast(ctx));
|
|
||||||
assert(cells.len == @as(usize, size.x) * @as(usize, size.y));
|
assert(cells.len == @as(usize, size.x) * @as(usize, size.y));
|
||||||
|
|
||||||
for (0.., this.left_text) |idx, cp| {
|
for (0.., left_text) |idx, cp| {
|
||||||
// NOTE do not write over the contents of this `Container`'s `Size`
|
// NOTE do not write over the contents of this `Container`'s `Size`
|
||||||
if (idx == cells.len) break;
|
if (idx == cells.len) break;
|
||||||
|
|
||||||
@@ -147,7 +138,7 @@ pub fn InfoBanner(App: type) type {
|
|||||||
.about => {},
|
.about => {},
|
||||||
.blog => |path| if (path) |p| page: {
|
.blog => |path| if (path) |p| page: {
|
||||||
const start_idx = (size.x -| p.len) / 2;
|
const start_idx = (size.x -| p.len) / 2;
|
||||||
if (start_idx <= this.left_text.len) break :page;
|
if (start_idx <= left_text.len) break :page;
|
||||||
|
|
||||||
for (start_idx.., p) |idx, cp| {
|
for (start_idx.., p) |idx, cp| {
|
||||||
// NOTE do not write over the contents of this `Container`'s `Size`
|
// NOTE do not write over the contents of this `Container`'s `Size`
|
||||||
@@ -159,10 +150,10 @@ pub fn InfoBanner(App: type) type {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
var start_idx = size.x -| this.right_text.len;
|
var start_idx = size.x -| right_text.len;
|
||||||
if (start_idx <= this.left_text.len) start_idx = this.left_text.len + 1;
|
if (start_idx <= left_text.len) start_idx = left_text.len + 1;
|
||||||
|
|
||||||
for (start_idx.., this.right_text) |idx, cp| {
|
for (start_idx.., right_text) |idx, cp| {
|
||||||
// NOTE do not write over the contents of this `Container`'s `Size`
|
// NOTE do not write over the contents of this `Container`'s `Size`
|
||||||
if (idx == cells.len) break;
|
if (idx == cells.len) break;
|
||||||
|
|
||||||
@@ -178,3 +169,4 @@ const std = @import("std");
|
|||||||
const Allocator = std.mem.Allocator;
|
const Allocator = std.mem.Allocator;
|
||||||
const assert = std.debug.assert;
|
const assert = std.debug.assert;
|
||||||
const zterm = @import("zterm");
|
const zterm = @import("zterm");
|
||||||
|
const Model = @import("model.zig");
|
||||||
|
|||||||
@@ -79,7 +79,7 @@ pub fn main() !void {
|
|||||||
}
|
}
|
||||||
// main actual tui_website page content
|
// main actual tui_website page content
|
||||||
{
|
{
|
||||||
var content: Content = .init(allocator, &app.model.document);
|
var content: Content = .init(allocator);
|
||||||
content_container = try .init(allocator, .{}, content.element());
|
content_container = try .init(allocator, .{}, content.element());
|
||||||
|
|
||||||
var scrollable: App.Scrollable = .init(content_container, .enabled(.green, false));
|
var scrollable: App.Scrollable = .init(content_container, .enabled(.green, false));
|
||||||
@@ -139,7 +139,7 @@ pub fn main() !void {
|
|||||||
.key => |key| {
|
.key => |key| {
|
||||||
if (key.eql(.{ .cp = 'c', .mod = .{ .ctrl = true } }) or key.eql(.{ .cp = 'q' })) app.quit();
|
if (key.eql(.{ .cp = 'c', .mod = .{ .ctrl = true } }) or key.eql(.{ .cp = 'q' })) app.quit();
|
||||||
// test if the event handling is working correctly
|
// test if the event handling is working correctly
|
||||||
if (key.eql(.{ .cp = zterm.input.Space })) app.postEvent(.{ .blog = allocator.dupe(u8, "./doc/test.md") catch unreachable });
|
if (key.eql(.{ .cp = zterm.input.Space })) app.postEvent(.{ .blog = try allocator.dupe(u8, "./doc/test.md") });
|
||||||
},
|
},
|
||||||
.about => log.info(ResourceRequestFormat, .{"./doc/about.md"}),
|
.about => log.info(ResourceRequestFormat, .{"./doc/about.md"}),
|
||||||
.blog => |path| log.info(ResourceRequestFormat, .{if (path) |p| p else "./doc/blog.md"}),
|
.blog => |path| log.info(ResourceRequestFormat, .{if (path) |p| p else "./doc/blog.md"}),
|
||||||
|
|||||||
Reference in New Issue
Block a user