Switch to single-instance Widget model

And make widget() functions return pointers to widgets instead of just
widgets

Signed-off-by: AnErrupTion <anerruption@disroot.org>
This commit is contained in:
AnErrupTion
2026-03-21 16:19:33 +01:00
parent aa392837bc
commit 60e3380375
15 changed files with 93 additions and 41 deletions

View File

@@ -8,6 +8,7 @@ const Widget = ly_ui.Widget;
const Cascade = @This();
instance: ?Widget = null,
buffer: *TerminalBuffer,
current_auth_fails: *usize,
max_auth_fails: usize,
@@ -18,14 +19,16 @@ pub fn init(
max_auth_fails: usize,
) Cascade {
return .{
.instance = null,
.buffer = buffer,
.current_auth_fails = current_auth_fails,
.max_auth_fails = max_auth_fails,
};
}
pub fn widget(self: *Cascade) Widget {
return Widget.init(
pub fn widget(self: *Cascade) *Widget {
if (self.instance) |*instance| return instance;
self.instance = Widget.init(
"Cascade",
null,
self,
@@ -36,6 +39,7 @@ pub fn widget(self: *Cascade) Widget {
null,
null,
);
return &self.instance.?;
}
fn draw(self: *Cascade) void {

View File

@@ -21,6 +21,7 @@ fn length(vec: Vec2) f32 {
return math.sqrt(vec[0] * vec[0] + vec[1] * vec[1]);
}
instance: ?Widget = null,
start_time: TimeOfDay,
terminal_buffer: *TerminalBuffer,
animate: *bool,
@@ -41,6 +42,7 @@ pub fn init(
frame_delay: u16,
) !ColorMix {
return .{
.instance = null,
.start_time = try interop.getTimeOfDay(),
.terminal_buffer = terminal_buffer,
.animate = animate,
@@ -66,8 +68,9 @@ pub fn init(
};
}
pub fn widget(self: *ColorMix) Widget {
return Widget.init(
pub fn widget(self: *ColorMix) *Widget {
if (self.instance) |*instance| return instance;
self.instance = Widget.init(
"ColorMix",
null,
self,
@@ -78,6 +81,7 @@ pub fn widget(self: *ColorMix) Widget {
null,
calculateTimeout,
);
return &self.instance.?;
}
fn draw(self: *ColorMix) void {

View File

@@ -16,6 +16,7 @@ pub const STEPS = 12;
pub const HEIGHT_MAX = 9;
pub const SPREAD_MAX = 4;
instance: ?Widget = null,
start_time: TimeOfDay,
allocator: Allocator,
terminal_buffer: *TerminalBuffer,
@@ -60,6 +61,7 @@ pub fn init(
};
return .{
.instance = null,
.start_time = try interop.getTimeOfDay(),
.allocator = allocator,
.terminal_buffer = terminal_buffer,
@@ -73,8 +75,9 @@ pub fn init(
};
}
pub fn widget(self: *Doom) Widget {
return Widget.init(
pub fn widget(self: *Doom) *Widget {
if (self.instance) |*instance| return instance;
self.instance = Widget.init(
"Doom",
null,
self,
@@ -85,6 +88,7 @@ pub fn widget(self: *Doom) Widget {
null,
calculateTimeout,
);
return &self.instance.?;
}
fn deinit(self: *Doom) void {

View File

@@ -304,6 +304,7 @@ const VEC_Y = 1;
const DurFile = @This();
instance: ?Widget = null,
start_time: TimeOfDay,
allocator: Allocator,
terminal_buffer: *TerminalBuffer,
@@ -408,6 +409,7 @@ pub fn init(
const frame_time: u32 = @intFromFloat(1000 / dur_movie.framerate.?);
return .{
.instance = null,
.start_time = try interop.getTimeOfDay(),
.allocator = allocator,
.terminal_buffer = terminal_buffer,
@@ -427,8 +429,9 @@ pub fn init(
};
}
pub fn widget(self: *DurFile) Widget {
return Widget.init(
pub fn widget(self: *DurFile) *Widget {
if (self.instance) |*instance| return instance;
self.instance = Widget.init(
"DurFile",
null,
self,
@@ -439,6 +442,7 @@ pub fn widget(self: *DurFile) Widget {
null,
calculateTimeout,
);
return &self.instance.?;
}
fn deinit(self: *DurFile) void {

View File

@@ -21,6 +21,7 @@ const NEIGHBOR_DIRS = [_][2]i8{
.{ 1, 0 }, .{ 1, 1 },
};
instance: ?Widget = null,
start_time: TimeOfDay,
allocator: Allocator,
terminal_buffer: *TerminalBuffer,
@@ -58,6 +59,7 @@ pub fn init(
const next_grid = try allocator.alloc(bool, grid_size);
var game = GameOfLife{
.instance = null,
.start_time = try interop.getTimeOfDay(),
.allocator = allocator,
.terminal_buffer = terminal_buffer,
@@ -83,8 +85,9 @@ pub fn init(
return game;
}
pub fn widget(self: *GameOfLife) Widget {
return Widget.init(
pub fn widget(self: *GameOfLife) *Widget {
if (self.instance) |*instance| return instance;
self.instance = Widget.init(
"GameOfLife",
null,
self,
@@ -95,6 +98,7 @@ pub fn widget(self: *GameOfLife) Widget {
null,
calculateTimeout,
);
return &self.instance.?;
}
fn deinit(self: *GameOfLife) void {

View File

@@ -29,6 +29,7 @@ pub const Line = struct {
update: usize,
};
instance: ?Widget = null,
start_time: TimeOfDay,
allocator: Allocator,
terminal_buffer: *TerminalBuffer,
@@ -62,6 +63,7 @@ pub fn init(
initBuffers(dots, lines, terminal_buffer.width, terminal_buffer.height, terminal_buffer.random);
return .{
.instance = null,
.start_time = try interop.getTimeOfDay(),
.allocator = allocator,
.terminal_buffer = terminal_buffer,
@@ -80,8 +82,9 @@ pub fn init(
};
}
pub fn widget(self: *Matrix) Widget {
return Widget.init(
pub fn widget(self: *Matrix) *Widget {
if (self.instance) |*instance| return instance;
self.instance = Widget.init(
"Matrix",
null,
self,
@@ -92,6 +95,7 @@ pub fn widget(self: *Matrix) Widget {
null,
calculateTimeout,
);
return &self.instance.?;
}
fn deinit(self: *Matrix) void {

View File

@@ -18,6 +18,7 @@ const Message = struct {
fg: u32,
};
instance: ?Widget = null,
label: *MessageLabel,
pub fn init(
@@ -28,6 +29,7 @@ pub fn init(
arrow_bg: u32,
) !InfoLine {
return .{
.instance = null,
.label = try MessageLabel.init(
allocator,
buffer,
@@ -46,8 +48,9 @@ pub fn deinit(self: *InfoLine) void {
self.label.deinit();
}
pub fn widget(self: *InfoLine) Widget {
return Widget.init(
pub fn widget(self: *InfoLine) *Widget {
if (self.instance) |*instance| return instance;
self.instance = Widget.init(
"InfoLine",
self.label.keybinds,
self,
@@ -58,6 +61,7 @@ pub fn widget(self: *InfoLine) Widget {
handle,
null,
);
return &self.instance.?;
}
pub fn addMessage(self: *InfoLine, text: []const u8, bg: u32, fg: u32) !void {

View File

@@ -18,6 +18,7 @@ const EnvironmentLabel = CyclableLabel(Env, *UserList);
const Session = @This();
instance: ?Widget = null,
label: *EnvironmentLabel,
user_list: *UserList,
@@ -31,6 +32,7 @@ pub fn init(
bg: u32,
) !Session {
return .{
.instance = null,
.label = try EnvironmentLabel.init(
allocator,
buffer,
@@ -55,8 +57,9 @@ pub fn deinit(self: *Session) void {
self.label.deinit();
}
pub fn widget(self: *Session) Widget {
return Widget.init(
pub fn widget(self: *Session) *Widget {
if (self.instance) |*instance| return instance;
self.instance = Widget.init(
"Session",
self.label.keybinds,
self,
@@ -67,6 +70,7 @@ pub fn widget(self: *Session) Widget {
handle,
null,
);
return &self.instance.?;
}
pub fn addEnvironment(self: *Session, environment: Environment) !void {

View File

@@ -21,6 +21,7 @@ const UserLabel = CyclableLabel(User, *Session);
const UserList = @This();
instance: ?Widget = null,
label: *UserLabel,
pub fn init(
@@ -35,6 +36,7 @@ pub fn init(
bg: u32,
) !UserList {
var user_list = UserList{
.instance = null,
.label = try UserLabel.init(
allocator,
buffer,
@@ -89,8 +91,9 @@ pub fn deinit(self: *UserList) void {
self.label.deinit();
}
pub fn widget(self: *UserList) Widget {
return Widget.init(
pub fn widget(self: *UserList) *Widget {
if (self.instance) |*instance| return instance;
self.instance = Widget.init(
"UserList",
self.label.keybinds,
self,
@@ -101,6 +104,7 @@ pub fn widget(self: *UserList) Widget {
handle,
null,
);
return &self.instance.?;
}
pub fn getCurrentUsername(self: UserList) []const u8 {

View File

@@ -94,7 +94,7 @@ const UiState = struct {
saved_users: SavedUsers,
login: UserList,
password: *Text,
password_widget: Widget,
password_widget: *Widget,
insert_mode: bool,
edge_margin: Position,
config: Config,
@@ -910,7 +910,7 @@ pub fn main() !void {
}
// Initialize the animation, if any
var animation: ?Widget = null;
var animation: ?*Widget = null;
switch (state.config.animation) {
.none => {},
.doom => {
@@ -985,7 +985,7 @@ pub fn main() !void {
animation = dur.widget();
},
}
defer if (animation) |*a| a.deinit();
defer if (animation) |a| a.deinit();
var cascade = Cascade.init(
&state.buffer,
@@ -1030,17 +1030,17 @@ pub fn main() !void {
const session_widget = state.session.widget();
const login_widget = state.login.widget();
var widgets: std.ArrayList([]Widget) = .empty;
var widgets: std.ArrayList([]*Widget) = .empty;
defer widgets.deinit(state.allocator);
// Layer 1
if (animation) |a| {
var layer1 = [_]Widget{a};
var layer1 = [_]*Widget{a};
try widgets.append(state.allocator, &layer1);
}
// Layer 2
var layer2: std.ArrayList(Widget) = .empty;
var layer2: std.ArrayList(*Widget) = .empty;
defer layer2.deinit(state.allocator);
if (!state.config.hide_key_hints) {
@@ -1089,7 +1089,7 @@ pub fn main() !void {
// Layer 3
if (state.config.auth_fails > 0) {
var layer3 = [_]Widget{cascade.widget()};
var layer3 = [_]*Widget{cascade.widget()};
try widgets.append(state.allocator, &layer3);
}