ref(event): split Size into two Points (one for the size and one for the anchor / origin)
All checks were successful
Zig Project Action / Lint, Spell-check and test zig project (push) Successful in 39s

This commit is contained in:
2025-03-04 00:04:56 +01:00
parent 91ac6241f4
commit 591b990087
23 changed files with 477 additions and 459 deletions

View File

@@ -12,13 +12,14 @@ const QuitText = struct {
return .{ .ptr = this, .vtable = &.{ .content = content } };
}
pub fn content(ctx: *anyopaque, cells: []zterm.Cell, size: zterm.Size) !void {
fn content(ctx: *anyopaque, cells: []zterm.Cell, origin: zterm.Point, size: zterm.Point) !void {
_ = ctx;
std.debug.assert(cells.len == @as(usize, size.cols) * @as(usize, size.rows));
std.debug.assert(cells.len == @as(usize, size.x) * @as(usize, size.y));
_ = origin;
const row = 2;
const col = size.cols / 2 -| (text.len / 2);
const anchor = (row * size.cols) + col;
const col = size.x / 2 -| (text.len / 2);
const anchor = (row * size.x) + col;
for (text, 0..) |cp, idx| {
cells[anchor + idx].style.fg = .white;
@@ -63,7 +64,7 @@ pub fn main() !void {
if (comptime field.value == 0) continue; // zterm.Color.default == 0 -> skip
try box.append(try App.Container.init(allocator, .{ .rectangle = .{ .fill = @enumFromInt(field.value) } }, .{}));
}
var scrollable: App.Scrollable = .init(box, .{ .cols = 3 * std.meta.fields(zterm.Color).len }); // ensure enough columns to render all colors -> scrollable otherwise
var scrollable: App.Scrollable = .init(box, .{ .x = 3 * std.meta.fields(zterm.Color).len }); // ensure enough columns to render all colors -> scrollable otherwise
try container.append(try App.Container.init(allocator, .{}, scrollable.element()));
try app.start();
@@ -76,7 +77,7 @@ pub fn main() !void {
switch (event) {
.init => continue,
.quit => break,
.resize => |size| try renderer.resize(size),
.size => |size| try renderer.resize(size),
.key => |key| if (key.eql(.{ .cp = 'c', .mod = .{ .ctrl = true } })) app.quit(),
.err => |err| log.err("Received {s} with message: {s}", .{ @errorName(err.err), err.msg }),
else => {},

View File

@@ -12,13 +12,14 @@ const QuitText = struct {
return .{ .ptr = this, .vtable = &.{ .content = content } };
}
pub fn content(ctx: *anyopaque, cells: []zterm.Cell, size: zterm.Size) !void {
fn content(ctx: *anyopaque, cells: []zterm.Cell, origin: zterm.Point, size: zterm.Point) !void {
_ = ctx;
std.debug.assert(cells.len == @as(usize, size.cols) * @as(usize, size.rows));
std.debug.assert(cells.len == @as(usize, size.x) * @as(usize, size.y));
_ = origin;
const row = 2;
const col = size.cols / 2 -| (text.len / 2);
const anchor = (row * size.cols) + col;
const col = size.x / 2 -| (text.len / 2);
const anchor = (row * size.x) + col;
for (text, 0..) |cp, idx| {
cells[anchor + idx].style.fg = .white;
@@ -38,10 +39,11 @@ const TextStyles = struct {
return .{ .ptr = this, .vtable = &.{ .content = content } };
}
pub fn content(ctx: *anyopaque, cells: []zterm.Cell, size: zterm.Size) !void {
fn content(ctx: *anyopaque, cells: []zterm.Cell, origin: zterm.Point, size: zterm.Point) !void {
@setEvalBranchQuota(50000);
_ = ctx;
std.debug.assert(cells.len == @as(usize, size.cols) * @as(usize, size.rows));
std.debug.assert(cells.len == @as(usize, size.x) * @as(usize, size.y));
_ = origin;
var row: usize = 0;
var col: usize = 0;
@@ -56,9 +58,9 @@ const TextStyles = struct {
// witouth any emphasis
for (text) |cp| {
cells[(row * size.cols) + col].style.bg = @enumFromInt(bg_field.value);
cells[(row * size.cols) + col].style.fg = @enumFromInt(fg_field.value);
cells[(row * size.cols) + col].cp = cp;
cells[(row * size.x) + col].style.bg = @enumFromInt(bg_field.value);
cells[(row * size.x) + col].style.fg = @enumFromInt(fg_field.value);
cells[(row * size.x) + col].cp = cp;
col += 1;
}
@@ -68,10 +70,10 @@ const TextStyles = struct {
const emphasis: zterm.Style.Emphasis = @enumFromInt(emp_field.value);
for (text) |cp| {
cells[(row * size.cols) + col].style.bg = @enumFromInt(bg_field.value);
cells[(row * size.cols) + col].style.fg = @enumFromInt(fg_field.value);
cells[(row * size.cols) + col].style.emphasis = &.{emphasis};
cells[(row * size.cols) + col].cp = cp;
cells[(row * size.x) + col].style.bg = @enumFromInt(bg_field.value);
cells[(row * size.x) + col].style.fg = @enumFromInt(fg_field.value);
cells[(row * size.x) + col].style.emphasis = &.{emphasis};
cells[(row * size.x) + col].cp = cp;
col += 1;
}
}
@@ -113,8 +115,8 @@ pub fn main() !void {
defer box.deinit();
var scrollable: App.Scrollable = .init(box, .{
.rows = (std.meta.fields(zterm.Color).len - 1) * (std.meta.fields(zterm.Color).len - 2),
.cols = std.meta.fields(zterm.Style.Emphasis).len * TextStyles.text.len,
.x = std.meta.fields(zterm.Style.Emphasis).len * TextStyles.text.len,
.y = (std.meta.fields(zterm.Color).len - 1) * (std.meta.fields(zterm.Color).len - 2),
}); // ensure enough rows and/or columns to render all text styles -> scrollable otherwise
try container.append(try App.Container.init(allocator, .{}, scrollable.element()));
@@ -128,7 +130,7 @@ pub fn main() !void {
switch (event) {
.init => continue,
.quit => break,
.resize => |size| try renderer.resize(size),
.size => |size| try renderer.resize(size),
.key => |key| if (key.eql(.{ .cp = 'c', .mod = .{ .ctrl = true } })) app.quit(),
.err => |err| log.err("Received {s} with message: {s}", .{ @errorName(err.err), err.msg }),
else => {},