Make animation timeout independent of event loop

Signed-off-by: AnErrupTion <anerruption@disroot.org>
This commit is contained in:
AnErrupTion 2026-02-11 23:51:32 +01:00
commit 7c7aed9cb2
No known key found for this signature in database
6 changed files with 121 additions and 45 deletions

View file

@ -2,6 +2,10 @@ const std = @import("std");
const Allocator = std.mem.Allocator;
const Random = std.Random;
const ly_core = @import("ly-core");
const interop = ly_core.interop;
const TimeOfDay = interop.TimeOfDay;
const Cell = @import("../tui/Cell.zig");
const TerminalBuffer = @import("../tui/TerminalBuffer.zig");
const Widget = @import("../tui/Widget.zig");
@ -24,6 +28,7 @@ pub const Line = struct {
update: usize,
};
start_time: TimeOfDay,
allocator: Allocator,
terminal_buffer: *TerminalBuffer,
dots: []Dot,
@ -34,7 +39,8 @@ fg: u32,
head_col: u32,
min_codepoint: u16,
max_codepoint: u16,
timeout: *bool,
animate: *bool,
timeout_sec: u12,
default_cell: Cell,
pub fn init(
@ -44,7 +50,8 @@ pub fn init(
head_col: u32,
min_codepoint: u16,
max_codepoint: u16,
timeout: *bool,
animate: *bool,
timeout_sec: u12,
) !Matrix {
const dots = try allocator.alloc(Dot, terminal_buffer.width * (terminal_buffer.height + 1));
const lines = try allocator.alloc(Line, terminal_buffer.width);
@ -52,6 +59,7 @@ pub fn init(
initBuffers(dots, lines, terminal_buffer.width, terminal_buffer.height, terminal_buffer.random);
return .{
.start_time = try interop.getTimeOfDay(),
.allocator = allocator,
.terminal_buffer = terminal_buffer,
.dots = dots,
@ -62,7 +70,8 @@ pub fn init(
.head_col = head_col,
.min_codepoint = min_codepoint,
.max_codepoint = max_codepoint - min_codepoint,
.timeout = timeout,
.animate = animate,
.timeout_sec = timeout_sec,
.default_cell = .{ .ch = ' ', .fg = fg, .bg = terminal_buffer.bg },
};
}
@ -74,7 +83,7 @@ pub fn widget(self: *Matrix) Widget {
deinit,
realloc,
draw,
null,
update,
null,
);
}
@ -95,7 +104,7 @@ fn realloc(self: *Matrix) !void {
}
fn draw(self: *Matrix) void {
if (self.timeout.*) return;
if (!self.animate.*) return;
const buf_height = self.terminal_buffer.height;
const buf_width = self.terminal_buffer.width;
@ -188,6 +197,14 @@ fn draw(self: *Matrix) void {
}
}
fn update(self: *Matrix, _: *anyopaque) !void {
const time = try interop.getTimeOfDay();
if (self.timeout_sec > 0 and time.seconds - self.start_time.seconds > self.timeout_sec) {
self.animate.* = false;
}
}
fn initBuffers(dots: []Dot, lines: []Line, width: usize, height: usize, random: Random) void {
var y: usize = 0;
while (y <= height) : (y += 1) {