mod: change interface for rendering of Layouts and Widgets to use a non-const pointer to the renderer instead
All checks were successful
Zig Project Action / Lint, Spell-check and test zig project (push) Successful in 38s

This commit is contained in:
2024-11-11 13:47:01 +01:00
parent 8b3f863404
commit d2d655c829
10 changed files with 23 additions and 19 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) {}
//! - render(this: *@This(), renderer: Renderer) anyerror!void {}
//! - render(this: *@This(), renderer: *Renderer) anyerror!void {}
//! - deinit(this: *@This()) void {}
//!
//! Create a `Layout` using `createFrom(object: anytype)` and use them through
@@ -28,7 +28,7 @@ pub fn Layout(comptime Event: type, comptime Renderer: type) type {
const VTable = struct {
handle: *const fn (this: *LayoutType, event: Event) anyerror!*Events,
render: *const fn (this: *LayoutType, renderer: Renderer) anyerror!void,
render: *const fn (this: *LayoutType, renderer: *Renderer) anyerror!void,
deinit: *const fn (this: *LayoutType) void,
};
@@ -41,7 +41,7 @@ pub fn Layout(comptime Event: type, comptime Renderer: type) type {
}
// Render this `Layout` completely. This will render contained sub-elements too.
pub fn render(this: *LayoutType, renderer: Renderer) !void {
pub fn render(this: *LayoutType, renderer: *Renderer) !void {
return try this.vtable.render(this, renderer);
}
@@ -63,7 +63,7 @@ pub fn Layout(comptime Event: type, comptime Renderer: type) type {
}.handle,
.render = struct {
// Render the contents of this `Layout`.
fn render(this: *LayoutType, renderer: Renderer) !void {
fn render(this: *LayoutType, renderer: *Renderer) !void {
const layout: @TypeOf(object) = @ptrFromInt(this.object);
try layout.render(renderer);
}

View File

@@ -118,7 +118,7 @@ pub fn Layout(comptime Event: type, comptime Renderer: type) type {
const round_frame = .{ "", "", "", "", "", "" };
const square_frame = .{ "", "", "", "", "", "" };
fn renderFrame(this: *@This(), renderer: Renderer) !void {
fn renderFrame(this: *@This(), renderer: *Renderer) !void {
// FIXME: use renderer instead!
_ = renderer;
const frame = switch (this.config.frame) {
@@ -167,7 +167,7 @@ pub fn Layout(comptime Event: type, comptime Renderer: type) type {
try this.config.style.value(writer, frame[5]);
}
pub fn render(this: *@This(), renderer: Renderer) !void {
pub fn render(this: *@This(), renderer: *Renderer) !void {
try renderer.clear(this.size);
try this.renderFrame(renderer);

View File

@@ -138,7 +138,7 @@ pub fn Layout(comptime Event: type, comptime Renderer: type) type {
return &this.events;
}
pub fn render(this: *@This(), renderer: Renderer) !void {
pub fn render(this: *@This(), renderer: *Renderer) !void {
for (this.elements.items) |*element| {
switch (element.*) {
.layout => |*layout| {

View File

@@ -123,7 +123,7 @@ pub fn Layout(comptime Event: type, comptime Renderer: type) type {
return &this.events;
}
pub fn render(this: *@This(), renderer: Renderer) !void {
pub fn render(this: *@This(), renderer: *Renderer) !void {
switch ((&this.element).*) {
.layout => |*layout| {
try layout.render(renderer);

View File

@@ -137,7 +137,7 @@ pub fn Layout(comptime Event: type, comptime Renderer: type) type {
return &this.events;
}
pub fn render(this: *@This(), renderer: Renderer) !void {
pub fn render(this: *@This(), renderer: *Renderer) !void {
for (this.elements.items) |*element| {
switch (element.*) {
.layout => |*layout| {

View File

@@ -26,7 +26,7 @@ pub fn main() !void {
const allocator = gpa.allocator();
var app: App = .{};
const renderer: App.Renderer = .{};
var renderer: App.Renderer = .{};
// FIX: when not running fullscreen, the application needs to screen down accordingly to display the contents
// -> size hint how much should it use?
@@ -142,6 +142,6 @@ pub fn main() !void {
for (events.items) |e| {
app.postEvent(e);
}
try layout.render(renderer);
try layout.render(&renderer);
}
}

View File

@@ -44,7 +44,11 @@ pub fn Buffered(comptime _: bool) type {
pub fn Direct(comptime _: bool) type {
return struct {
pub fn clear(this: @This(), size: Size) !void {
pub fn resize(this: *@This(), size: Size) void {
_ = this;
_ = size;
}
pub fn clear(this: *@This(), size: Size) !void {
_ = this;
const anchor = size.anchor;
// NOTE: clear entire screen for the first content (derived from the anchor being at the very left-top)
@@ -53,7 +57,7 @@ pub fn Direct(comptime _: bool) type {
}
}
pub fn render(this: @This(), size: Size, contents: []u8) !void {
pub fn render(this: *@This(), size: Size, contents: []u8) !void {
_ = this;
try terminal.setCursorPosition(size.anchor);
var row: u16 = 0;

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 {}
//! - render(this: *@This(), renderer: Renderer) !void {}
//! - render(this: *@This(), renderer: *Renderer) !void {}
//! - deinit(this: *@This()) void {}
//!
//! Create a `Widget` using `createFrom(object: anytype)` and use them through
@@ -27,7 +27,7 @@ pub fn Widget(comptime Event: type, comptime Renderer: type) type {
const VTable = struct {
handle: *const fn (this: *WidgetType, event: Event) ?Event,
render: *const fn (this: *WidgetType, renderer: Renderer) anyerror!void,
render: *const fn (this: *WidgetType, renderer: *Renderer) anyerror!void,
deinit: *const fn (this: *WidgetType) void,
};
@@ -51,7 +51,7 @@ pub fn Widget(comptime Event: type, comptime Renderer: type) type {
}
// Render the content of this `Widget` given the `Size` of the widget (.resize System`Event`).
pub fn render(this: *WidgetType, renderer: Renderer) !void {
pub fn render(this: *WidgetType, renderer: *Renderer) !void {
try this.vtable.render(this, renderer);
}
@@ -73,7 +73,7 @@ pub fn Widget(comptime Event: type, comptime Renderer: type) type {
}.handle,
.render = struct {
// Return the entire content of this `Widget`.
fn render(this: *WidgetType, renderer: Renderer) !void {
fn render(this: *WidgetType, renderer: *Renderer) !void {
const widget: @TypeOf(object) = @ptrFromInt(this.object);
try widget.render(renderer);
}

View File

@@ -75,7 +75,7 @@ pub fn Widget(comptime Event: type, comptime Renderer: type) type {
return null;
}
pub fn render(this: *@This(), renderer: Renderer) !void {
pub fn render(this: *@This(), renderer: *Renderer) !void {
try renderer.clear(this.size);
try terminal.setCursorPosition(this.size.anchor);
if (this.size.rows >= this.line_index.items.len) {

View File

@@ -31,7 +31,7 @@ pub fn Widget(comptime Event: type, comptime Renderer: type) type {
return null;
}
pub fn render(this: *@This(), renderer: Renderer) !void {
pub fn render(this: *@This(), renderer: *Renderer) !void {
try renderer.clear(this.size);
}
};