fix(widget): respect size for RawText and Spacer Widgets
All checks were successful
Zig Project Action / Lint, Spell-check and test zig project (push) Successful in 36s

This commit is contained in:
2024-11-10 16:02:49 +01:00
parent a201f2b653
commit 8ae9129403
4 changed files with 15 additions and 24 deletions

View File

@@ -142,25 +142,12 @@ pub fn Layout(comptime Event: type, comptime Renderer: type) type {
pub fn render(this: *@This(), renderer: Renderer) !void {
// FIX: renderer should clear only what is going to change! (i.e. the 'active' widget / layout)
try renderer.clear(this.anchor, this.size);
var overflow = this.size.rows % this.elements.items.len;
for (this.elements.items, 0..) |*element, i| {
const row_mul: u16 = @truncate(i);
var row = row_mul * this.element_rows + 1;
if (i > 0 and overflow > 0) {
overflow -|= 1;
row += 1;
}
// TODO: that's the anchor of each component (only necessary for each widget rendering)
const pos: terminal.Position = .{ .col = 1, .row = row };
// TODO: do this using the renderer
try terminal.setCursorPosition(pos);
for (this.elements.items) |*element| {
switch (element.*) {
.layout => |*layout| {
try layout.render(renderer);
},
.widget => |*widget| {
// TODO: clear per widget if necesary (i.e. can I query that?)
try widget.render(renderer);
},
}

View File

@@ -43,9 +43,9 @@ pub fn Buffered(comptime fullscreen: bool) type {
pub fn Plain(comptime _: bool) type {
return struct {
pub fn clear(this: @This(), anchor: Position, size: Size) !void {
pub fn clear(this: @This(), size: Size) !void {
_ = this;
_ = size;
const anchor = size.anchor;
// NOTE: clear entire screen for the first content (derived from the anchor being at the very left-top)
if (anchor.col == 1 and anchor.row == 1) {
try terminal.clearScreen();

View File

@@ -76,16 +76,16 @@ pub fn Widget(comptime Event: type, comptime Renderer: type) type {
}
pub fn render(this: *@This(), renderer: Renderer) !void {
try renderer.clear(this.size);
try terminal.setCursorPosition(this.size.anchor);
// TODO: render `this.contents`
_ = renderer;
if (this.size.rows >= this.line_index.items.len) {
_ = try terminal.write(this.contents.items);
} else {
// more rows than we can display
const i = this.line_index.items[this.line];
const e = this.size.rows + this.line + 1;
if (e >= this.line_index.items.len) {
const e = this.size.rows + this.line;
if (e > this.line_index.items.len) {
_ = try terminal.write(this.contents.items[i..]);
}
// last line should not end with the last character (likely a newline character)

View File

@@ -11,6 +11,8 @@ pub fn Widget(comptime Event: type, comptime Renderer: type) type {
@compileError("Provided user event `Event` for `Layout(comptime Event: type)` is not of type `union(enum)`.");
}
return struct {
size: terminal.Size = undefined,
pub fn init() @This() {
return .{};
}
@@ -20,15 +22,17 @@ pub fn Widget(comptime Event: type, comptime Renderer: type) type {
}
pub fn handle(this: *@This(), event: Event) ?Event {
_ = this;
_ = event;
switch (event) {
.resize => |size| {
this.size = size;
},
else => {},
}
return null;
}
pub fn render(this: *@This(), renderer: Renderer) !void {
// FIX: this should rather clear the `terminal.Size` provided through the event system using the _renderer_.
_ = this;
_ = renderer;
try renderer.clear(this.size);
}
};
}