From 3c54c1613a1073b6cb7bcbcc3473b08b475d166d Mon Sep 17 00:00:00 2001 From: RadsammyT Date: Sat, 21 Mar 2026 11:08:00 -0400 Subject: [PATCH] No more lids --- ly-ui/src/Widget.zig | 5 ++++- ly-ui/src/components/Label.zig | 9 +++++---- src/main.zig | 24 +++++++++++++----------- 3 files changed, 22 insertions(+), 16 deletions(-) diff --git a/ly-ui/src/Widget.zig b/ly-ui/src/Widget.zig index d66fce8..07f76c3 100644 --- a/ly-ui/src/Widget.zig +++ b/ly-ui/src/Widget.zig @@ -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, diff --git a/ly-ui/src/components/Label.zig b/ly-ui/src/components/Label.zig index 83c651b..86603bb 100644 --- a/ly-ui/src/components/Label.zig +++ b/ly-ui/src/components/Label.zig @@ -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( diff --git a/src/main.zig b/src/main.zig index c049be2..85ecc23 100644 --- a/src/main.zig +++ b/src/main.zig @@ -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;