No more lids

This commit is contained in:
RadsammyT 2026-03-21 11:08:00 -04:00
commit 3c54c1613a
No known key found for this signature in database
3 changed files with 22 additions and 16 deletions

View file

@ -12,6 +12,8 @@ const VTable = struct {
calculate_timeout_fn: ?*const fn (ptr: *anyopaque, ctx: *anyopaque) anyerror!?usize,
};
pub var idCounter: u64 = 0;
id: u64,
display_name: []const u8,
keybinds: ?TerminalBuffer.KeybindMap,
@ -101,8 +103,9 @@ pub fn init(
};
};
idCounter += 1;
return .{
.id = @intFromPtr(Impl.vtable.draw_fn),
.id = idCounter,
.display_name = display_name,
.keybinds = keybinds,
.pointer = pointer,

View file

@ -8,9 +8,8 @@ const Position = @import("../Position.zig");
const TerminalBuffer = @import("../TerminalBuffer.zig");
const Widget = @import("../Widget.zig");
lid: u64, // HACK: we need to differenciate between labels in `updateCustomInfo`.
// The underlying widget ID doesn't have the properties needed.
allocator: ?Allocator,
instance: ?Widget,
text: []const u8,
max_width: ?usize,
fg: u32,
@ -32,8 +31,8 @@ pub fn init(
) Label {
lidCounter += 1;
return .{
.lid = lidCounter,
.allocator = null,
.instance = null,
.text = text,
.max_width = max_width,
.fg = fg,
@ -50,7 +49,8 @@ pub fn deinit(self: *Label) void {
}
pub fn widget(self: *Label) Widget {
return Widget.init(
if (self.instance) |inst| return inst;
self.instance = Widget.init(
"Label",
null,
self,
@ -61,6 +61,7 @@ pub fn widget(self: *Label) Widget {
null,
calculateTimeout,
);
return self.instance.?; // We already created the Widget.
}
pub fn setTextAlloc(

View file

@ -75,6 +75,7 @@ const CustomInfoLabel = struct {
lbl: Label
};
const UiState = struct {
allocator: Allocator,
auth_fails: u64,
@ -1084,19 +1085,20 @@ pub fn main() !void {
var lblIter = custom.labels.iterator();
while (lblIter.next()) |i| {
const w = Label.init("",
null,
state.buffer.fg,
state.buffer.bg,
&updateCustomInfo,
null,
);
i.value_ptr.id = w.lid;
i.value_ptr.counter = 2;
try state.custom_info.append(state.allocator, .{
.info = i.value_ptr.*,
.lbl = w
.lbl = .init(
"",
null,
state.buffer.fg,
state.buffer.bg,
&updateCustomInfo,
null
)
});
var latest = &state.custom_info.items[state.custom_info.items.len-1];
latest.info.id = latest.lbl.widget().id;
latest.info.counter = 2;
}
defer {
for (state.custom_info.items) |*item| {
@ -1763,7 +1765,7 @@ fn updateClock(self: *Label, ptr: *anyopaque) !void {
fn updateCustomInfo(lbl: *Label, ptr: *anyopaque) !void {
const state: *UiState = @ptrCast(@alignCast(ptr));
const wid = lbl.lid;
const wid = lbl.widget().id;
var stdout = std.ArrayList(u8).empty;
defer stdout.deinit(state.allocator);
var stderr = std.ArrayList(u8).empty;