refactor: add Widget.get_at function

This commit is contained in:
CJ van den Berg 2025-11-06 18:18:49 +01:00
parent 3437f4fd20
commit c02a5d939c
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9

View file

@ -115,6 +115,17 @@ pub fn addP(self: *Self, w_: Widget) !*Widget {
return &w.widget; return &w.widget;
} }
pub fn get(self: *Self, name_: []const u8) ?*Widget {
for (self.widgets.items) |*w|
if (w.widget.get(name_)) |p|
return p;
return null;
}
pub fn get_at(self: *const Self, n: usize) ?*Widget {
return if (n < self.widgets.items.len) &self.widgets.items[n].widget else null;
}
pub fn remove(self: *Self, w: Widget) void { pub fn remove(self: *Self, w: Widget) void {
for (self.widgets.items, 0..) |p, i| if (p.widget.ptr == w.ptr) { for (self.widgets.items, 0..) |p, i| if (p.widget.ptr == w.ptr) {
self.widgets.orderedRemove(i).widget.deinit(self.allocator); self.widgets.orderedRemove(i).widget.deinit(self.allocator);
@ -391,13 +402,6 @@ fn do_resize(self: *Self, padding: Widget.Style.Margin) void {
} }
} }
pub fn get(self: *Self, name_: []const u8) ?*Widget {
for (self.widgets.items) |*w|
if (w.widget.get(name_)) |p|
return p;
return null;
}
pub fn walk(self: *Self, ctx: *anyopaque, f: Widget.WalkFn, self_w: *Widget) bool { pub fn walk(self: *Self, ctx: *anyopaque, f: Widget.WalkFn, self_w: *Widget) bool {
for (self.widgets.items) |*w| for (self.widgets.items) |*w|
if (w.widget.walk(ctx, f)) return true; if (w.widget.walk(ctx, f)) return true;