Add the cascade animation as a separate widget

Signed-off-by: AnErrupTion <anerruption@disroot.org>
This commit is contained in:
AnErrupTion
2026-02-10 17:43:55 +01:00
parent f320d3f666
commit d268d5bb45
15 changed files with 276 additions and 148 deletions

View File

@@ -17,5 +17,5 @@ pub fn init(ch: u32, fg: u32, bg: u32) Cell {
pub fn put(self: Cell, x: usize, y: usize) void {
if (self.ch == 0) return;
TerminalBuffer.setCell(x, y, self.ch, self.fg, self.bg);
TerminalBuffer.setCell(x, y, self);
}

View File

@@ -172,19 +172,29 @@ pub fn presentBuffer() void {
_ = termbox.tb_present();
}
pub fn setCell(
x: usize,
y: usize,
ch: u32,
fg: u32,
bg: u32,
) void {
pub fn getCell(x: usize, y: usize) ?Cell {
var maybe_cell: ?*termbox.tb_cell = undefined;
_ = termbox.tb_get_cell(
@intCast(x),
@intCast(y),
1,
&maybe_cell,
);
if (maybe_cell) |cell| {
return Cell.init(cell.ch, cell.fg, cell.bg);
}
return null;
}
pub fn setCell(x: usize, y: usize, cell: Cell) void {
_ = termbox.tb_set_cell(
@intCast(x),
@intCast(y),
ch,
fg,
bg,
cell.ch,
cell.fg,
cell.bg,
);
}
@@ -201,40 +211,6 @@ pub fn reclaim(self: TerminalBuffer) !void {
}
}
pub fn cascade(self: TerminalBuffer) bool {
var changed = false;
var y = self.height - 2;
while (y > 0) : (y -= 1) {
for (0..self.width) |x| {
var cell: ?*termbox.tb_cell = undefined;
var cell_under: ?*termbox.tb_cell = undefined;
_ = termbox.tb_get_cell(@intCast(x), @intCast(y - 1), 1, &cell);
_ = termbox.tb_get_cell(@intCast(x), @intCast(y), 1, &cell_under);
// This shouldn't happen under normal circumstances, but because
// this is a *secret* animation, there's no need to care that much
if (cell == null or cell_under == null) continue;
const char: u8 = @truncate(cell.?.ch);
if (std.ascii.isWhitespace(char)) continue;
const char_under: u8 = @truncate(cell_under.?.ch);
if (!std.ascii.isWhitespace(char_under)) continue;
changed = true;
if ((self.random.int(u16) % 10) > 7) continue;
_ = termbox.tb_set_cell(@intCast(x), @intCast(y), cell.?.ch, cell.?.fg, cell.?.bg);
_ = termbox.tb_set_cell(@intCast(x), @intCast(y - 1), ' ', cell_under.?.fg, cell_under.?.bg);
}
}
return changed;
}
pub fn registerKeybind(self: *TerminalBuffer, keybind: []const u8, callback: KeybindCallbackFn) !void {
var key = std.mem.zeroes(keyboard.Key);
var iterator = std.mem.splitScalar(u8, keybind, '+');

View File

@@ -140,7 +140,7 @@ pub fn childrenPosition(self: BigLabel) Position {
return self.children_pos;
}
pub fn draw(self: *BigLabel) void {
fn draw(self: *BigLabel) void {
for (self.text, 0..) |c, i| {
const clock_cell = clockCell(
c,
@@ -159,7 +159,7 @@ pub fn draw(self: *BigLabel) void {
}
}
pub fn update(self: *BigLabel, context: *anyopaque) !void {
fn update(self: *BigLabel, context: *anyopaque) !void {
if (self.update_fn) |update_fn| {
return @call(
.auto,

View File

@@ -19,6 +19,7 @@ bottom_title: ?[]const u8,
border_fg: u32,
title_fg: u32,
bg: u32,
update_fn: ?*const fn (*CenteredBox, *anyopaque) anyerror!void,
left_pos: Position,
right_pos: Position,
children_pos: Position,
@@ -36,6 +37,7 @@ pub fn init(
border_fg: u32,
title_fg: u32,
bg: u32,
update_fn: ?*const fn (*CenteredBox, *anyopaque) anyerror!void,
) CenteredBox {
return .{
.buffer = buffer,
@@ -50,6 +52,7 @@ pub fn init(
.border_fg = border_fg,
.title_fg = title_fg,
.bg = bg,
.update_fn = update_fn,
.left_pos = TerminalBuffer.START_POSITION,
.right_pos = TerminalBuffer.START_POSITION,
.children_pos = TerminalBuffer.START_POSITION,
@@ -90,7 +93,7 @@ pub fn childrenPosition(self: CenteredBox) Position {
return self.children_pos;
}
pub fn draw(self: *CenteredBox) void {
fn draw(self: *CenteredBox) void {
if (self.show_borders) {
var left_up = Cell.init(
self.buffer.box_chars.left_up,
@@ -172,3 +175,13 @@ pub fn draw(self: *CenteredBox) void {
);
}
}
fn update(self: *CenteredBox, ctx: *anyopaque) !void {
if (self.update_fn) |update_fn| {
return @call(
.auto,
update_fn,
.{ self, ctx },
);
}
}

View File

@@ -56,14 +56,6 @@ pub fn widget(self: *InfoLine) Widget {
);
}
pub fn draw(self: *InfoLine) void {
self.label.draw();
}
pub fn handle(self: *InfoLine, maybe_key: ?keyboard.Key, insert_mode: bool) !void {
self.label.handle(maybe_key, insert_mode);
}
pub fn addMessage(self: *InfoLine, text: []const u8, bg: u32, fg: u32) !void {
if (text.len == 0) return;
@@ -91,6 +83,14 @@ pub fn clearRendered(self: InfoLine, allocator: Allocator) !void {
);
}
fn draw(self: *InfoLine) void {
self.label.draw();
}
fn handle(self: *InfoLine, maybe_key: ?keyboard.Key, insert_mode: bool) !void {
self.label.handle(maybe_key, insert_mode);
}
fn drawItem(label: *MessageLabel, message: Message, x: usize, y: usize, width: usize) void {
if (message.width == 0) return;

View File

@@ -98,7 +98,7 @@ pub fn childrenPosition(self: Label) Position {
return self.children_pos;
}
pub fn draw(self: *Label) void {
fn draw(self: *Label) void {
if (self.max_width) |width| {
TerminalBuffer.drawConfinedText(
self.text,
@@ -120,7 +120,7 @@ pub fn draw(self: *Label) void {
);
}
pub fn update(self: *Label, ctx: *anyopaque) !void {
fn update(self: *Label, ctx: *anyopaque) !void {
if (self.update_fn) |update_fn| {
return @call(
.auto,

View File

@@ -66,14 +66,6 @@ pub fn widget(self: *Session) Widget {
);
}
pub fn draw(self: *Session) void {
self.label.draw();
}
pub fn handle(self: *Session, maybe_key: ?keyboard.Key, insert_mode: bool) !void {
self.label.handle(maybe_key, insert_mode);
}
pub fn addEnvironment(self: *Session, environment: Environment) !void {
const env = Env{ .environment = environment, .index = self.label.list.items.len };
@@ -81,6 +73,14 @@ pub fn addEnvironment(self: *Session, environment: Environment) !void {
addedSession(env, self.user_list);
}
fn draw(self: *Session) void {
self.label.draw();
}
fn handle(self: *Session, maybe_key: ?keyboard.Key, insert_mode: bool) !void {
self.label.handle(maybe_key, insert_mode);
}
fn addedSession(env: Env, user_list: *UserList) void {
const user = user_list.label.list.items[user_list.label.current];
if (!user.first_run) return;

View File

@@ -87,6 +87,13 @@ pub fn childrenPosition(self: Text) Position {
return self.children_pos;
}
pub fn clear(self: *Text) void {
self.text.clearRetainingCapacity();
self.end = 0;
self.cursor = 0;
self.visible_start = 0;
}
pub fn handle(self: *Text, maybe_key: ?keyboard.Key, insert_mode: bool) !void {
if (maybe_key) |key| {
if (key.left or (!insert_mode and (key.h or key.backspace))) {
@@ -117,7 +124,7 @@ pub fn handle(self: *Text, maybe_key: ?keyboard.Key, insert_mode: bool) !void {
);
}
pub fn draw(self: *Text) void {
fn draw(self: *Text) void {
if (self.masked) {
if (self.maybe_mask) |mask| {
if (self.width < 1) return;
@@ -158,13 +165,6 @@ pub fn draw(self: *Text) void {
);
}
pub fn clear(self: *Text) void {
self.text.clearRetainingCapacity();
self.end = 0;
self.cursor = 0;
self.visible_start = 0;
}
fn goLeft(self: *Text) void {
if (self.cursor == 0) return;
if (self.visible_start > 0) self.visible_start -= 1;