mirror of
https://github.com/fairyglade/ly.git
synced 2026-03-25 09:46:06 +00:00
Add Widget.calculateTimeout function
Signed-off-by: AnErrupTion <anerruption@disroot.org>
This commit is contained in:
@@ -32,6 +32,7 @@ pub fn widget(self: *Cascade) Widget {
|
|||||||
draw,
|
draw,
|
||||||
null,
|
null,
|
||||||
null,
|
null,
|
||||||
|
null,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -24,6 +24,7 @@ start_time: TimeOfDay,
|
|||||||
terminal_buffer: *TerminalBuffer,
|
terminal_buffer: *TerminalBuffer,
|
||||||
animate: *bool,
|
animate: *bool,
|
||||||
timeout_sec: u12,
|
timeout_sec: u12,
|
||||||
|
frame_delay: u16,
|
||||||
frames: u64,
|
frames: u64,
|
||||||
pattern_cos_mod: f32,
|
pattern_cos_mod: f32,
|
||||||
pattern_sin_mod: f32,
|
pattern_sin_mod: f32,
|
||||||
@@ -36,12 +37,14 @@ pub fn init(
|
|||||||
col3: u32,
|
col3: u32,
|
||||||
animate: *bool,
|
animate: *bool,
|
||||||
timeout_sec: u12,
|
timeout_sec: u12,
|
||||||
|
frame_delay: u16,
|
||||||
) !ColorMix {
|
) !ColorMix {
|
||||||
return .{
|
return .{
|
||||||
.start_time = try interop.getTimeOfDay(),
|
.start_time = try interop.getTimeOfDay(),
|
||||||
.terminal_buffer = terminal_buffer,
|
.terminal_buffer = terminal_buffer,
|
||||||
.animate = animate,
|
.animate = animate,
|
||||||
.timeout_sec = timeout_sec,
|
.timeout_sec = timeout_sec,
|
||||||
|
.frame_delay = frame_delay,
|
||||||
.frames = 0,
|
.frames = 0,
|
||||||
.pattern_cos_mod = terminal_buffer.random.float(f32) * math.pi * 2.0,
|
.pattern_cos_mod = terminal_buffer.random.float(f32) * math.pi * 2.0,
|
||||||
.pattern_sin_mod = terminal_buffer.random.float(f32) * math.pi * 2.0,
|
.pattern_sin_mod = terminal_buffer.random.float(f32) * math.pi * 2.0,
|
||||||
@@ -71,6 +74,7 @@ pub fn widget(self: *ColorMix) Widget {
|
|||||||
draw,
|
draw,
|
||||||
update,
|
update,
|
||||||
null,
|
null,
|
||||||
|
calculateTimeout,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -116,3 +120,7 @@ fn update(self: *ColorMix, _: *anyopaque) !void {
|
|||||||
self.animate.* = false;
|
self.animate.* = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn calculateTimeout(self: *ColorMix, _: *anyopaque) !?usize {
|
||||||
|
return self.frame_delay;
|
||||||
|
}
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ allocator: Allocator,
|
|||||||
terminal_buffer: *TerminalBuffer,
|
terminal_buffer: *TerminalBuffer,
|
||||||
animate: *bool,
|
animate: *bool,
|
||||||
timeout_sec: u12,
|
timeout_sec: u12,
|
||||||
|
frame_delay: u16,
|
||||||
buffer: []u8,
|
buffer: []u8,
|
||||||
height: u8,
|
height: u8,
|
||||||
spread: u8,
|
spread: u8,
|
||||||
@@ -35,6 +36,7 @@ pub fn init(
|
|||||||
fire_spread: u8,
|
fire_spread: u8,
|
||||||
animate: *bool,
|
animate: *bool,
|
||||||
timeout_sec: u12,
|
timeout_sec: u12,
|
||||||
|
frame_delay: u16,
|
||||||
) !Doom {
|
) !Doom {
|
||||||
const buffer = try allocator.alloc(u8, terminal_buffer.width * terminal_buffer.height);
|
const buffer = try allocator.alloc(u8, terminal_buffer.width * terminal_buffer.height);
|
||||||
initBuffer(buffer, terminal_buffer.width);
|
initBuffer(buffer, terminal_buffer.width);
|
||||||
@@ -62,6 +64,7 @@ pub fn init(
|
|||||||
.terminal_buffer = terminal_buffer,
|
.terminal_buffer = terminal_buffer,
|
||||||
.animate = animate,
|
.animate = animate,
|
||||||
.timeout_sec = timeout_sec,
|
.timeout_sec = timeout_sec,
|
||||||
|
.frame_delay = frame_delay,
|
||||||
.buffer = buffer,
|
.buffer = buffer,
|
||||||
.height = @min(HEIGHT_MAX, fire_height),
|
.height = @min(HEIGHT_MAX, fire_height),
|
||||||
.spread = @min(SPREAD_MAX, fire_spread),
|
.spread = @min(SPREAD_MAX, fire_spread),
|
||||||
@@ -78,6 +81,7 @@ pub fn widget(self: *Doom) Widget {
|
|||||||
draw,
|
draw,
|
||||||
update,
|
update,
|
||||||
null,
|
null,
|
||||||
|
calculateTimeout,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -147,3 +151,7 @@ fn update(self: *Doom, _: *anyopaque) !void {
|
|||||||
self.animate.* = false;
|
self.animate.* = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn calculateTimeout(self: *Doom, _: *anyopaque) !?usize {
|
||||||
|
return self.frame_delay;
|
||||||
|
}
|
||||||
|
|||||||
@@ -312,6 +312,7 @@ start_pos: IVec2,
|
|||||||
full_color: bool,
|
full_color: bool,
|
||||||
animate: *bool,
|
animate: *bool,
|
||||||
timeout_sec: u12,
|
timeout_sec: u12,
|
||||||
|
frame_delay: u16,
|
||||||
frame_time: u32,
|
frame_time: u32,
|
||||||
time_previous: i64,
|
time_previous: i64,
|
||||||
is_color_format_16: bool,
|
is_color_format_16: bool,
|
||||||
@@ -373,6 +374,7 @@ pub fn init(
|
|||||||
full_color: bool,
|
full_color: bool,
|
||||||
animate: *bool,
|
animate: *bool,
|
||||||
timeout_sec: u12,
|
timeout_sec: u12,
|
||||||
|
frame_delay: u16,
|
||||||
) !DurFile {
|
) !DurFile {
|
||||||
var dur_movie: DurFormat = .init(allocator);
|
var dur_movie: DurFormat = .init(allocator);
|
||||||
|
|
||||||
@@ -414,6 +416,7 @@ pub fn init(
|
|||||||
.full_color = full_color,
|
.full_color = full_color,
|
||||||
.animate = animate,
|
.animate = animate,
|
||||||
.timeout_sec = timeout_sec,
|
.timeout_sec = timeout_sec,
|
||||||
|
.frame_delay = frame_delay,
|
||||||
.dur_movie = dur_movie,
|
.dur_movie = dur_movie,
|
||||||
.frame_time = frame_time,
|
.frame_time = frame_time,
|
||||||
.is_color_format_16 = eql(u8, dur_movie.colorFormat.?, "16"),
|
.is_color_format_16 = eql(u8, dur_movie.colorFormat.?, "16"),
|
||||||
@@ -431,6 +434,7 @@ pub fn widget(self: *DurFile) Widget {
|
|||||||
draw,
|
draw,
|
||||||
update,
|
update,
|
||||||
null,
|
null,
|
||||||
|
calculateTimeout,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -508,3 +512,7 @@ fn update(self: *DurFile, _: *anyopaque) !void {
|
|||||||
self.animate.* = false;
|
self.animate.* = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn calculateTimeout(self: *DurFile, _: *anyopaque) !?usize {
|
||||||
|
return self.frame_delay;
|
||||||
|
}
|
||||||
|
|||||||
@@ -33,6 +33,7 @@ frame_delay: usize,
|
|||||||
initial_density: f32,
|
initial_density: f32,
|
||||||
animate: *bool,
|
animate: *bool,
|
||||||
timeout_sec: u12,
|
timeout_sec: u12,
|
||||||
|
animation_frame_delay: u16,
|
||||||
dead_cell: Cell,
|
dead_cell: Cell,
|
||||||
width: usize,
|
width: usize,
|
||||||
height: usize,
|
height: usize,
|
||||||
@@ -46,6 +47,7 @@ pub fn init(
|
|||||||
initial_density: f32,
|
initial_density: f32,
|
||||||
animate: *bool,
|
animate: *bool,
|
||||||
timeout_sec: u12,
|
timeout_sec: u12,
|
||||||
|
animation_frame_delay: u16,
|
||||||
) !GameOfLife {
|
) !GameOfLife {
|
||||||
const width = terminal_buffer.width;
|
const width = terminal_buffer.width;
|
||||||
const height = terminal_buffer.height;
|
const height = terminal_buffer.height;
|
||||||
@@ -68,6 +70,7 @@ pub fn init(
|
|||||||
.initial_density = initial_density,
|
.initial_density = initial_density,
|
||||||
.animate = animate,
|
.animate = animate,
|
||||||
.timeout_sec = timeout_sec,
|
.timeout_sec = timeout_sec,
|
||||||
|
.animation_frame_delay = animation_frame_delay,
|
||||||
.dead_cell = .{ .ch = DEAD_CHAR, .fg = @intCast(TerminalBuffer.Color.DEFAULT), .bg = terminal_buffer.bg },
|
.dead_cell = .{ .ch = DEAD_CHAR, .fg = @intCast(TerminalBuffer.Color.DEFAULT), .bg = terminal_buffer.bg },
|
||||||
.width = width,
|
.width = width,
|
||||||
.height = height,
|
.height = height,
|
||||||
@@ -88,6 +91,7 @@ pub fn widget(self: *GameOfLife) Widget {
|
|||||||
draw,
|
draw,
|
||||||
update,
|
update,
|
||||||
null,
|
null,
|
||||||
|
calculateTimeout,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -149,6 +153,10 @@ fn update(self: *GameOfLife, _: *anyopaque) !void {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn calculateTimeout(self: *GameOfLife, _: *anyopaque) !?usize {
|
||||||
|
return self.animation_frame_delay;
|
||||||
|
}
|
||||||
|
|
||||||
fn updateGeneration(self: *GameOfLife) void {
|
fn updateGeneration(self: *GameOfLife) void {
|
||||||
// Conway's Game of Life rules with optimized neighbor counting
|
// Conway's Game of Life rules with optimized neighbor counting
|
||||||
for (0..self.height) |y| {
|
for (0..self.height) |y| {
|
||||||
|
|||||||
@@ -41,6 +41,7 @@ min_codepoint: u16,
|
|||||||
max_codepoint: u16,
|
max_codepoint: u16,
|
||||||
animate: *bool,
|
animate: *bool,
|
||||||
timeout_sec: u12,
|
timeout_sec: u12,
|
||||||
|
frame_delay: u16,
|
||||||
default_cell: Cell,
|
default_cell: Cell,
|
||||||
|
|
||||||
pub fn init(
|
pub fn init(
|
||||||
@@ -52,6 +53,7 @@ pub fn init(
|
|||||||
max_codepoint: u16,
|
max_codepoint: u16,
|
||||||
animate: *bool,
|
animate: *bool,
|
||||||
timeout_sec: u12,
|
timeout_sec: u12,
|
||||||
|
frame_delay: u16,
|
||||||
) !Matrix {
|
) !Matrix {
|
||||||
const dots = try allocator.alloc(Dot, terminal_buffer.width * (terminal_buffer.height + 1));
|
const dots = try allocator.alloc(Dot, terminal_buffer.width * (terminal_buffer.height + 1));
|
||||||
const lines = try allocator.alloc(Line, terminal_buffer.width);
|
const lines = try allocator.alloc(Line, terminal_buffer.width);
|
||||||
@@ -72,6 +74,7 @@ pub fn init(
|
|||||||
.max_codepoint = max_codepoint - min_codepoint,
|
.max_codepoint = max_codepoint - min_codepoint,
|
||||||
.animate = animate,
|
.animate = animate,
|
||||||
.timeout_sec = timeout_sec,
|
.timeout_sec = timeout_sec,
|
||||||
|
.frame_delay = frame_delay,
|
||||||
.default_cell = .{ .ch = ' ', .fg = fg, .bg = terminal_buffer.bg },
|
.default_cell = .{ .ch = ' ', .fg = fg, .bg = terminal_buffer.bg },
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -85,6 +88,7 @@ pub fn widget(self: *Matrix) Widget {
|
|||||||
draw,
|
draw,
|
||||||
update,
|
update,
|
||||||
null,
|
null,
|
||||||
|
calculateTimeout,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -205,6 +209,10 @@ fn update(self: *Matrix, _: *anyopaque) !void {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn calculateTimeout(self: *Matrix, _: *anyopaque) !?usize {
|
||||||
|
return self.frame_delay;
|
||||||
|
}
|
||||||
|
|
||||||
fn initBuffers(dots: []Dot, lines: []Line, width: usize, height: usize, random: Random) void {
|
fn initBuffers(dots: []Dot, lines: []Line, width: usize, height: usize, random: Random) void {
|
||||||
var y: usize = 0;
|
var y: usize = 0;
|
||||||
while (y <= height) : (y += 1) {
|
while (y <= height) : (y += 1) {
|
||||||
|
|||||||
59
src/main.zig
59
src/main.zig
@@ -363,6 +363,7 @@ pub fn main() !void {
|
|||||||
state.buffer.fg,
|
state.buffer.fg,
|
||||||
state.buffer.bg,
|
state.buffer.bg,
|
||||||
null,
|
null,
|
||||||
|
null,
|
||||||
);
|
);
|
||||||
defer state.shutdown_label.deinit();
|
defer state.shutdown_label.deinit();
|
||||||
|
|
||||||
@@ -372,6 +373,7 @@ pub fn main() !void {
|
|||||||
state.buffer.fg,
|
state.buffer.fg,
|
||||||
state.buffer.bg,
|
state.buffer.bg,
|
||||||
null,
|
null,
|
||||||
|
null,
|
||||||
);
|
);
|
||||||
defer state.restart_label.deinit();
|
defer state.restart_label.deinit();
|
||||||
|
|
||||||
@@ -381,6 +383,7 @@ pub fn main() !void {
|
|||||||
state.buffer.fg,
|
state.buffer.fg,
|
||||||
state.buffer.bg,
|
state.buffer.bg,
|
||||||
null,
|
null,
|
||||||
|
null,
|
||||||
);
|
);
|
||||||
defer state.sleep_label.deinit();
|
defer state.sleep_label.deinit();
|
||||||
|
|
||||||
@@ -390,6 +393,7 @@ pub fn main() !void {
|
|||||||
state.buffer.fg,
|
state.buffer.fg,
|
||||||
state.buffer.bg,
|
state.buffer.bg,
|
||||||
null,
|
null,
|
||||||
|
null,
|
||||||
);
|
);
|
||||||
defer state.hibernate_label.deinit();
|
defer state.hibernate_label.deinit();
|
||||||
|
|
||||||
@@ -399,6 +403,7 @@ pub fn main() !void {
|
|||||||
state.buffer.fg,
|
state.buffer.fg,
|
||||||
state.buffer.bg,
|
state.buffer.bg,
|
||||||
null,
|
null,
|
||||||
|
null,
|
||||||
);
|
);
|
||||||
defer state.brightness_down_label.deinit();
|
defer state.brightness_down_label.deinit();
|
||||||
|
|
||||||
@@ -408,6 +413,7 @@ pub fn main() !void {
|
|||||||
state.buffer.fg,
|
state.buffer.fg,
|
||||||
state.buffer.bg,
|
state.buffer.bg,
|
||||||
null,
|
null,
|
||||||
|
null,
|
||||||
);
|
);
|
||||||
defer state.brightness_up_label.deinit();
|
defer state.brightness_up_label.deinit();
|
||||||
|
|
||||||
@@ -458,6 +464,7 @@ pub fn main() !void {
|
|||||||
state.buffer.fg,
|
state.buffer.fg,
|
||||||
state.buffer.bg,
|
state.buffer.bg,
|
||||||
&updateNumlock,
|
&updateNumlock,
|
||||||
|
null,
|
||||||
);
|
);
|
||||||
defer state.numlock_label.deinit();
|
defer state.numlock_label.deinit();
|
||||||
|
|
||||||
@@ -467,6 +474,7 @@ pub fn main() !void {
|
|||||||
state.buffer.fg,
|
state.buffer.fg,
|
||||||
state.buffer.bg,
|
state.buffer.bg,
|
||||||
&updateCapslock,
|
&updateCapslock,
|
||||||
|
null,
|
||||||
);
|
);
|
||||||
defer state.capslock_label.deinit();
|
defer state.capslock_label.deinit();
|
||||||
|
|
||||||
@@ -476,6 +484,7 @@ pub fn main() !void {
|
|||||||
state.buffer.fg,
|
state.buffer.fg,
|
||||||
state.buffer.bg,
|
state.buffer.bg,
|
||||||
&updateBattery,
|
&updateBattery,
|
||||||
|
null,
|
||||||
);
|
);
|
||||||
defer state.battery_label.deinit();
|
defer state.battery_label.deinit();
|
||||||
|
|
||||||
@@ -485,6 +494,7 @@ pub fn main() !void {
|
|||||||
state.buffer.fg,
|
state.buffer.fg,
|
||||||
state.buffer.bg,
|
state.buffer.bg,
|
||||||
&updateClock,
|
&updateClock,
|
||||||
|
&calculateClockTimeout,
|
||||||
);
|
);
|
||||||
defer state.clock_label.deinit();
|
defer state.clock_label.deinit();
|
||||||
|
|
||||||
@@ -499,6 +509,7 @@ pub fn main() !void {
|
|||||||
.fa => .fa,
|
.fa => .fa,
|
||||||
},
|
},
|
||||||
&updateBigClock,
|
&updateBigClock,
|
||||||
|
&calculateBigClockTimeout,
|
||||||
);
|
);
|
||||||
defer state.bigclock_label.deinit();
|
defer state.bigclock_label.deinit();
|
||||||
|
|
||||||
@@ -624,6 +635,7 @@ pub fn main() !void {
|
|||||||
state.buffer.fg,
|
state.buffer.fg,
|
||||||
state.buffer.bg,
|
state.buffer.bg,
|
||||||
&updateSessionSpecifier,
|
&updateSessionSpecifier,
|
||||||
|
null,
|
||||||
);
|
);
|
||||||
defer state.session_specifier_label.deinit();
|
defer state.session_specifier_label.deinit();
|
||||||
|
|
||||||
@@ -644,6 +656,7 @@ pub fn main() !void {
|
|||||||
state.buffer.fg,
|
state.buffer.fg,
|
||||||
state.buffer.bg,
|
state.buffer.bg,
|
||||||
null,
|
null,
|
||||||
|
null,
|
||||||
);
|
);
|
||||||
defer state.login_label.deinit();
|
defer state.login_label.deinit();
|
||||||
|
|
||||||
@@ -764,6 +777,7 @@ pub fn main() !void {
|
|||||||
state.buffer.fg,
|
state.buffer.fg,
|
||||||
state.buffer.bg,
|
state.buffer.bg,
|
||||||
null,
|
null,
|
||||||
|
null,
|
||||||
);
|
);
|
||||||
defer state.password_label.deinit();
|
defer state.password_label.deinit();
|
||||||
|
|
||||||
@@ -786,6 +800,7 @@ pub fn main() !void {
|
|||||||
state.buffer.fg,
|
state.buffer.fg,
|
||||||
state.buffer.bg,
|
state.buffer.bg,
|
||||||
null,
|
null,
|
||||||
|
null,
|
||||||
);
|
);
|
||||||
defer state.version_label.deinit();
|
defer state.version_label.deinit();
|
||||||
|
|
||||||
@@ -882,6 +897,7 @@ pub fn main() !void {
|
|||||||
state.config.doom_fire_spread,
|
state.config.doom_fire_spread,
|
||||||
&state.animate,
|
&state.animate,
|
||||||
state.config.animation_timeout_sec,
|
state.config.animation_timeout_sec,
|
||||||
|
state.config.animation_frame_delay,
|
||||||
);
|
);
|
||||||
animation = doom.widget();
|
animation = doom.widget();
|
||||||
},
|
},
|
||||||
@@ -895,6 +911,7 @@ pub fn main() !void {
|
|||||||
state.config.cmatrix_max_codepoint,
|
state.config.cmatrix_max_codepoint,
|
||||||
&state.animate,
|
&state.animate,
|
||||||
state.config.animation_timeout_sec,
|
state.config.animation_timeout_sec,
|
||||||
|
state.config.animation_frame_delay,
|
||||||
);
|
);
|
||||||
animation = matrix.widget();
|
animation = matrix.widget();
|
||||||
},
|
},
|
||||||
@@ -906,6 +923,7 @@ pub fn main() !void {
|
|||||||
state.config.colormix_col3,
|
state.config.colormix_col3,
|
||||||
&state.animate,
|
&state.animate,
|
||||||
state.config.animation_timeout_sec,
|
state.config.animation_timeout_sec,
|
||||||
|
state.config.animation_frame_delay,
|
||||||
);
|
);
|
||||||
animation = color_mix.widget();
|
animation = color_mix.widget();
|
||||||
},
|
},
|
||||||
@@ -919,6 +937,7 @@ pub fn main() !void {
|
|||||||
state.config.gameoflife_initial_density,
|
state.config.gameoflife_initial_density,
|
||||||
&state.animate,
|
&state.animate,
|
||||||
state.config.animation_timeout_sec,
|
state.config.animation_timeout_sec,
|
||||||
|
state.config.animation_frame_delay,
|
||||||
);
|
);
|
||||||
animation = game_of_life.widget();
|
animation = game_of_life.widget();
|
||||||
},
|
},
|
||||||
@@ -934,6 +953,7 @@ pub fn main() !void {
|
|||||||
state.config.full_color,
|
state.config.full_color,
|
||||||
&state.animate,
|
&state.animate,
|
||||||
state.config.animation_timeout_sec,
|
state.config.animation_timeout_sec,
|
||||||
|
state.config.animation_frame_delay,
|
||||||
);
|
);
|
||||||
animation = dur.widget();
|
animation = dur.widget();
|
||||||
},
|
},
|
||||||
@@ -1145,19 +1165,11 @@ pub fn main() !void {
|
|||||||
TerminalBuffer.presentBuffer();
|
TerminalBuffer.presentBuffer();
|
||||||
}
|
}
|
||||||
|
|
||||||
var timeout: i32 = -1;
|
var maybe_timeout: ?usize = null;
|
||||||
|
for (widgets.items) |*widget| {
|
||||||
// Calculate the maximum timeout based on current animations, or the (big) clock. If there's none, we wait for the event indefinitely instead
|
if (try widget.calculateTimeout(&state)) |widget_timeout| {
|
||||||
if (state.animate) {
|
if (maybe_timeout == null or widget_timeout < maybe_timeout.?) maybe_timeout = widget_timeout;
|
||||||
timeout = state.config.animation_frame_delay;
|
}
|
||||||
} else if (state.config.bigclock != .none and state.config.clock == null) {
|
|
||||||
const time = try interop.getTimeOfDay();
|
|
||||||
|
|
||||||
timeout = @intCast((60 - @rem(time.seconds, 60)) * 1000 - @divTrunc(time.microseconds, 1000) + 1);
|
|
||||||
} else if (state.config.clock != null) {
|
|
||||||
const time = try interop.getTimeOfDay();
|
|
||||||
|
|
||||||
timeout = @intCast(1000 - @divTrunc(time.microseconds, 1000) + 1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (state.config.inactivity_cmd) |inactivity_cmd| {
|
if (state.config.inactivity_cmd) |inactivity_cmd| {
|
||||||
@@ -1190,9 +1202,9 @@ pub fn main() !void {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const event_error = if (timeout == -1) termbox.tb_poll_event(&event) else termbox.tb_peek_event(&event, timeout);
|
const event_error = if (maybe_timeout) |timeout| termbox.tb_peek_event(&event, @intCast(timeout)) else termbox.tb_poll_event(&event);
|
||||||
|
|
||||||
state.update = timeout != -1;
|
state.update = maybe_timeout != null;
|
||||||
|
|
||||||
if (event_error < 0) continue;
|
if (event_error < 0) continue;
|
||||||
|
|
||||||
@@ -1741,6 +1753,12 @@ fn updateClock(self: *Label, ptr: *anyopaque) !void {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn calculateClockTimeout(_: *Label, _: *anyopaque) !?usize {
|
||||||
|
const time = try interop.getTimeOfDay();
|
||||||
|
|
||||||
|
return @intCast(1000 - @divTrunc(time.microseconds, 1000) + 1);
|
||||||
|
}
|
||||||
|
|
||||||
fn updateBigClock(self: *BigLabel, ptr: *anyopaque) !void {
|
fn updateBigClock(self: *BigLabel, ptr: *anyopaque) !void {
|
||||||
var state: *UiState = @ptrCast(@alignCast(ptr));
|
var state: *UiState = @ptrCast(@alignCast(ptr));
|
||||||
|
|
||||||
@@ -1766,6 +1784,17 @@ fn updateBigClock(self: *BigLabel, ptr: *anyopaque) !void {
|
|||||||
self.setText(clock_str);
|
self.setText(clock_str);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn calculateBigClockTimeout(_: *BigLabel, ptr: *anyopaque) !?usize {
|
||||||
|
const state: *UiState = @ptrCast(@alignCast(ptr));
|
||||||
|
const time = try interop.getTimeOfDay();
|
||||||
|
|
||||||
|
if (state.config.bigclock_seconds) {
|
||||||
|
return @intCast(1000 - @divTrunc(time.microseconds, 1000) + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
return @intCast((60 - @rem(time.seconds, 60)) * 1000 - @divTrunc(time.microseconds, 1000) + 1);
|
||||||
|
}
|
||||||
|
|
||||||
fn updateBox(self: *CenteredBox, ptr: *anyopaque) !void {
|
fn updateBox(self: *CenteredBox, ptr: *anyopaque) !void {
|
||||||
const state: *UiState = @ptrCast(@alignCast(ptr));
|
const state: *UiState = @ptrCast(@alignCast(ptr));
|
||||||
|
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ const VTable = struct {
|
|||||||
draw_fn: *const fn (ptr: *anyopaque) void,
|
draw_fn: *const fn (ptr: *anyopaque) void,
|
||||||
update_fn: ?*const fn (ptr: *anyopaque, ctx: *anyopaque) anyerror!void,
|
update_fn: ?*const fn (ptr: *anyopaque, ctx: *anyopaque) anyerror!void,
|
||||||
handle_fn: ?*const fn (ptr: *anyopaque, maybe_key: ?keyboard.Key, insert_mode: bool) anyerror!void,
|
handle_fn: ?*const fn (ptr: *anyopaque, maybe_key: ?keyboard.Key, insert_mode: bool) anyerror!void,
|
||||||
|
calculate_timeout_fn: ?*const fn (ptr: *anyopaque, ctx: *anyopaque) anyerror!?usize,
|
||||||
};
|
};
|
||||||
|
|
||||||
id: u64,
|
id: u64,
|
||||||
@@ -24,6 +25,7 @@ pub fn init(
|
|||||||
comptime draw_fn: fn (ptr: @TypeOf(pointer)) void,
|
comptime draw_fn: fn (ptr: @TypeOf(pointer)) void,
|
||||||
comptime update_fn: ?fn (ptr: @TypeOf(pointer), ctx: *anyopaque) anyerror!void,
|
comptime update_fn: ?fn (ptr: @TypeOf(pointer), ctx: *anyopaque) anyerror!void,
|
||||||
comptime handle_fn: ?fn (ptr: @TypeOf(pointer), maybe_key: ?keyboard.Key, insert_mode: bool) anyerror!void,
|
comptime handle_fn: ?fn (ptr: @TypeOf(pointer), maybe_key: ?keyboard.Key, insert_mode: bool) anyerror!void,
|
||||||
|
comptime calculate_timeout_fn: ?fn (ptr: @TypeOf(pointer), ctx: *anyopaque) anyerror!?usize,
|
||||||
) Widget {
|
) Widget {
|
||||||
const Pointer = @TypeOf(pointer);
|
const Pointer = @TypeOf(pointer);
|
||||||
const Impl = struct {
|
const Impl = struct {
|
||||||
@@ -77,12 +79,23 @@ pub fn init(
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn calculateTimeoutImpl(ptr: *anyopaque, ctx: *anyopaque) !?usize {
|
||||||
|
const impl: Pointer = @ptrCast(@alignCast(ptr));
|
||||||
|
|
||||||
|
return @call(
|
||||||
|
.always_inline,
|
||||||
|
calculate_timeout_fn.?,
|
||||||
|
.{ impl, ctx },
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
const vtable = VTable{
|
const vtable = VTable{
|
||||||
.deinit_fn = if (deinit_fn != null) deinitImpl else null,
|
.deinit_fn = if (deinit_fn != null) deinitImpl else null,
|
||||||
.realloc_fn = if (realloc_fn != null) reallocImpl else null,
|
.realloc_fn = if (realloc_fn != null) reallocImpl else null,
|
||||||
.draw_fn = drawImpl,
|
.draw_fn = drawImpl,
|
||||||
.update_fn = if (update_fn != null) updateImpl else null,
|
.update_fn = if (update_fn != null) updateImpl else null,
|
||||||
.handle_fn = if (handle_fn != null) handleImpl else null,
|
.handle_fn = if (handle_fn != null) handleImpl else null,
|
||||||
|
.calculate_timeout_fn = if (calculate_timeout_fn != null) calculateTimeoutImpl else null,
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -151,3 +164,17 @@ pub fn handle(self: *Widget, maybe_key: ?keyboard.Key, insert_mode: bool) !void
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn calculateTimeout(self: *Widget, ctx: *anyopaque) !?usize {
|
||||||
|
const impl: @TypeOf(self.pointer) = @ptrCast(@alignCast(self.pointer));
|
||||||
|
|
||||||
|
if (self.vtable.calculate_timeout_fn) |calculate_timeout_fn| {
|
||||||
|
return @call(
|
||||||
|
.auto,
|
||||||
|
calculate_timeout_fn,
|
||||||
|
.{ impl, ctx },
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|||||||
@@ -52,6 +52,7 @@ fg: u32,
|
|||||||
bg: u32,
|
bg: u32,
|
||||||
locale: BigLabelLocale,
|
locale: BigLabelLocale,
|
||||||
update_fn: ?*const fn (*BigLabel, *anyopaque) anyerror!void,
|
update_fn: ?*const fn (*BigLabel, *anyopaque) anyerror!void,
|
||||||
|
calculate_timeout_fn: ?*const fn (*BigLabel, *anyopaque) anyerror!?usize,
|
||||||
component_pos: Position,
|
component_pos: Position,
|
||||||
children_pos: Position,
|
children_pos: Position,
|
||||||
|
|
||||||
@@ -63,6 +64,7 @@ pub fn init(
|
|||||||
bg: u32,
|
bg: u32,
|
||||||
locale: BigLabelLocale,
|
locale: BigLabelLocale,
|
||||||
update_fn: ?*const fn (*BigLabel, *anyopaque) anyerror!void,
|
update_fn: ?*const fn (*BigLabel, *anyopaque) anyerror!void,
|
||||||
|
calculate_timeout_fn: ?*const fn (*BigLabel, *anyopaque) anyerror!?usize,
|
||||||
) BigLabel {
|
) BigLabel {
|
||||||
return .{
|
return .{
|
||||||
.allocator = null,
|
.allocator = null,
|
||||||
@@ -73,6 +75,7 @@ pub fn init(
|
|||||||
.bg = bg,
|
.bg = bg,
|
||||||
.locale = locale,
|
.locale = locale,
|
||||||
.update_fn = update_fn,
|
.update_fn = update_fn,
|
||||||
|
.calculate_timeout_fn = calculate_timeout_fn,
|
||||||
.component_pos = TerminalBuffer.START_POSITION,
|
.component_pos = TerminalBuffer.START_POSITION,
|
||||||
.children_pos = TerminalBuffer.START_POSITION,
|
.children_pos = TerminalBuffer.START_POSITION,
|
||||||
};
|
};
|
||||||
@@ -91,6 +94,7 @@ pub fn widget(self: *BigLabel) Widget {
|
|||||||
draw,
|
draw,
|
||||||
update,
|
update,
|
||||||
null,
|
null,
|
||||||
|
calculateTimeout,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -170,6 +174,18 @@ fn update(self: *BigLabel, context: *anyopaque) !void {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn calculateTimeout(self: *BigLabel, ctx: *anyopaque) !?usize {
|
||||||
|
if (self.calculate_timeout_fn) |calculate_timeout_fn| {
|
||||||
|
return @call(
|
||||||
|
.auto,
|
||||||
|
calculate_timeout_fn,
|
||||||
|
.{ self, ctx },
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
fn clockCell(char: u8, fg: u32, bg: u32, locale: BigLabelLocale) [CHAR_SIZE]Cell {
|
fn clockCell(char: u8, fg: u32, bg: u32, locale: BigLabelLocale) [CHAR_SIZE]Cell {
|
||||||
var cells: [CHAR_SIZE]Cell = undefined;
|
var cells: [CHAR_SIZE]Cell = undefined;
|
||||||
|
|
||||||
|
|||||||
@@ -68,6 +68,7 @@ pub fn widget(self: *CenteredBox) Widget {
|
|||||||
draw,
|
draw,
|
||||||
null,
|
null,
|
||||||
null,
|
null,
|
||||||
|
null,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -54,6 +54,7 @@ pub fn widget(self: *InfoLine) Widget {
|
|||||||
draw,
|
draw,
|
||||||
null,
|
null,
|
||||||
handle,
|
handle,
|
||||||
|
null,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ max_width: ?usize,
|
|||||||
fg: u32,
|
fg: u32,
|
||||||
bg: u32,
|
bg: u32,
|
||||||
update_fn: ?*const fn (*Label, *anyopaque) anyerror!void,
|
update_fn: ?*const fn (*Label, *anyopaque) anyerror!void,
|
||||||
|
calculate_timeout_fn: ?*const fn (*Label, *anyopaque) anyerror!?usize,
|
||||||
component_pos: Position,
|
component_pos: Position,
|
||||||
children_pos: Position,
|
children_pos: Position,
|
||||||
|
|
||||||
@@ -23,6 +24,7 @@ pub fn init(
|
|||||||
fg: u32,
|
fg: u32,
|
||||||
bg: u32,
|
bg: u32,
|
||||||
update_fn: ?*const fn (*Label, *anyopaque) anyerror!void,
|
update_fn: ?*const fn (*Label, *anyopaque) anyerror!void,
|
||||||
|
calculate_timeout_fn: ?*const fn (*Label, *anyopaque) anyerror!?usize,
|
||||||
) Label {
|
) Label {
|
||||||
return .{
|
return .{
|
||||||
.allocator = null,
|
.allocator = null,
|
||||||
@@ -31,6 +33,7 @@ pub fn init(
|
|||||||
.fg = fg,
|
.fg = fg,
|
||||||
.bg = bg,
|
.bg = bg,
|
||||||
.update_fn = update_fn,
|
.update_fn = update_fn,
|
||||||
|
.calculate_timeout_fn = calculate_timeout_fn,
|
||||||
.component_pos = TerminalBuffer.START_POSITION,
|
.component_pos = TerminalBuffer.START_POSITION,
|
||||||
.children_pos = TerminalBuffer.START_POSITION,
|
.children_pos = TerminalBuffer.START_POSITION,
|
||||||
};
|
};
|
||||||
@@ -49,6 +52,7 @@ pub fn widget(self: *Label) Widget {
|
|||||||
draw,
|
draw,
|
||||||
update,
|
update,
|
||||||
null,
|
null,
|
||||||
|
calculateTimeout,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -130,3 +134,15 @@ fn update(self: *Label, ctx: *anyopaque) !void {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn calculateTimeout(self: *Label, ctx: *anyopaque) !?usize {
|
||||||
|
if (self.calculate_timeout_fn) |calculate_timeout_fn| {
|
||||||
|
return @call(
|
||||||
|
.auto,
|
||||||
|
calculate_timeout_fn,
|
||||||
|
.{ self, ctx },
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|||||||
@@ -64,6 +64,7 @@ pub fn widget(self: *Session) Widget {
|
|||||||
draw,
|
draw,
|
||||||
null,
|
null,
|
||||||
handle,
|
handle,
|
||||||
|
null,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -63,6 +63,7 @@ pub fn widget(self: *Text) Widget {
|
|||||||
draw,
|
draw,
|
||||||
null,
|
null,
|
||||||
handle,
|
handle,
|
||||||
|
null,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -96,6 +96,7 @@ pub fn widget(self: *UserList) Widget {
|
|||||||
draw,
|
draw,
|
||||||
null,
|
null,
|
||||||
handle,
|
handle,
|
||||||
|
null,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user