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

@@ -169,8 +169,8 @@ pub fn runEventLoop(
self: *TerminalBuffer,
allocator: Allocator,
shared_error: SharedError,
layers: [][]Widget,
active_widget: Widget,
layers: [][]*Widget,
active_widget: *Widget,
inactivity_delay: u16,
position_widgets_fn: *const fn (*anyopaque) anyerror!void,
inactivity_event_fn: ?*const fn (*anyopaque) anyerror!void,
@@ -189,7 +189,7 @@ pub fn runEventLoop(
var i: usize = 0;
for (layers) |layer| {
for (layer) |*widget| {
for (layer) |widget| {
try widget.update(context);
if (widget.vtable.handle_fn != null) {
@@ -210,7 +210,7 @@ pub fn runEventLoop(
while (self.run) {
if (self.update) {
for (layers) |layer| {
for (layer) |*widget| {
for (layer) |widget| {
try widget.update(context);
}
}
@@ -229,7 +229,7 @@ pub fn runEventLoop(
try TerminalBuffer.clearScreen(false);
for (layers) |layer| {
for (layer) |*widget| {
for (layer) |widget| {
widget.draw();
}
}
@@ -239,7 +239,7 @@ pub fn runEventLoop(
var maybe_timeout: ?usize = null;
for (layers) |layer| {
for (layer) |*widget| {
for (layer) |widget| {
if (try widget.calculateTimeout(context)) |widget_timeout| {
if (maybe_timeout == null or widget_timeout < maybe_timeout.?) maybe_timeout = widget_timeout;
}
@@ -275,7 +275,7 @@ pub fn runEventLoop(
);
for (layers) |layer| {
for (layer) |*widget| {
for (layer) |widget| {
widget.realloc() catch |err| {
shared_error.writeError(error.WidgetReallocationFailed);
try self.log_file.err(
@@ -326,7 +326,7 @@ pub fn getActiveWidget(self: *TerminalBuffer) *Widget {
return self.handlable_widgets.items[self.active_widget_index];
}
pub fn setActiveWidget(self: *TerminalBuffer, widget: Widget) void {
pub fn setActiveWidget(self: *TerminalBuffer, widget: *Widget) void {
for (self.handlable_widgets.items, 0..) |widg, i| {
if (widg.id == widget.id) self.active_widget_index = i;
}

View File

@@ -44,6 +44,7 @@ pub const BigLabelLocale = enum {
fa,
};
instance: ?Widget = null,
allocator: ?Allocator = null,
buffer: *TerminalBuffer,
text: []const u8,
@@ -67,6 +68,7 @@ pub fn init(
calculate_timeout_fn: ?*const fn (*BigLabel, *anyopaque) anyerror!?usize,
) BigLabel {
return .{
.instance = null,
.allocator = null,
.buffer = buffer,
.text = text,
@@ -85,8 +87,9 @@ pub fn deinit(self: *BigLabel) void {
if (self.allocator) |allocator| allocator.free(self.text);
}
pub fn widget(self: *BigLabel) Widget {
return Widget.init(
pub fn widget(self: *BigLabel) *Widget {
if (self.instance) |*instance| return instance;
self.instance = Widget.init(
"BigLabel",
null,
self,
@@ -97,6 +100,7 @@ pub fn widget(self: *BigLabel) Widget {
null,
calculateTimeout,
);
return &self.instance.?;
}
pub fn setTextAlloc(

View File

@@ -7,6 +7,7 @@ const Widget = @import("../Widget.zig");
const CenteredBox = @This();
instance: ?Widget = null,
buffer: *TerminalBuffer,
horizontal_margin: usize,
vertical_margin: usize,
@@ -40,6 +41,7 @@ pub fn init(
update_fn: ?*const fn (*CenteredBox, *anyopaque) anyerror!void,
) CenteredBox {
return .{
.instance = null,
.buffer = buffer,
.horizontal_margin = horizontal_margin,
.vertical_margin = vertical_margin,
@@ -59,8 +61,9 @@ pub fn init(
};
}
pub fn widget(self: *CenteredBox) Widget {
return Widget.init(
pub fn widget(self: *CenteredBox) *Widget {
if (self.instance) |*instance| return instance;
self.instance = Widget.init(
"CenteredBox",
null,
self,
@@ -71,6 +74,7 @@ pub fn widget(self: *CenteredBox) Widget {
null,
null,
);
return &self.instance.?;
}
pub fn positionXY(self: *CenteredBox, original_pos: Position) void {

View File

@@ -8,6 +8,7 @@ const Position = @import("../Position.zig");
const TerminalBuffer = @import("../TerminalBuffer.zig");
const Widget = @import("../Widget.zig");
instance: ?Widget,
allocator: ?Allocator,
text: []const u8,
max_width: ?usize,
@@ -27,6 +28,7 @@ pub fn init(
calculate_timeout_fn: ?*const fn (*Label, *anyopaque) anyerror!?usize,
) Label {
return .{
.instance = null,
.allocator = null,
.text = text,
.max_width = max_width,
@@ -43,8 +45,9 @@ pub fn deinit(self: *Label) void {
if (self.allocator) |allocator| allocator.free(self.text);
}
pub fn widget(self: *Label) Widget {
return Widget.init(
pub fn widget(self: *Label) *Widget {
if (self.instance) |*instance| return instance;
self.instance = Widget.init(
"Label",
null,
self,
@@ -55,6 +58,7 @@ pub fn widget(self: *Label) Widget {
null,
calculateTimeout,
);
return &self.instance.?;
}
pub fn setTextAlloc(

View File

@@ -10,6 +10,7 @@ const DynamicString = std.ArrayListUnmanaged(u8);
const Text = @This();
instance: ?Widget,
allocator: Allocator,
buffer: *TerminalBuffer,
text: DynamicString,
@@ -38,6 +39,7 @@ pub fn init(
) !*Text {
var self = try allocator.create(Text);
self.* = Text{
.instance = null,
.allocator = allocator,
.buffer = buffer,
.text = .empty,
@@ -70,8 +72,9 @@ pub fn deinit(self: *Text) void {
self.allocator.destroy(self);
}
pub fn widget(self: *Text) Widget {
return Widget.init(
pub fn widget(self: *Text) *Widget {
if (self.instance) |*instance| return instance;
self.instance = Widget.init(
"Text",
self.keybinds,
self,
@@ -82,6 +85,7 @@ pub fn widget(self: *Text) Widget {
handle,
null,
);
return &self.instance.?;
}
pub fn positionX(self: *Text, original_pos: Position) void {