feat: refactor

This commit is contained in:
2025-06-04 17:32:01 +02:00
parent 9c4543eb4a
commit 1fc509f897

View File

@@ -261,68 +261,65 @@ const Model = struct {
} }
fn createItems( fn createItems(
ctx: vxfw.DrawContext, ctx: vxfw.DrawContext,
items1: *locked(?std.ArrayList(Arc(*models.Item))), locked_items: *locked(?std.ArrayList(Arc(*models.Item))),
current_full_name: ?models.FullName, current_full_name: ?models.FullName,
) !?[]vxfw.Widget { ) !?[]vxfw.Widget {
const text_items = blk2: { const text_items = blk: {
items1.mutex.lock(); locked_items.mutex.lock();
defer items1.mutex.unlock(); defer locked_items.mutex.unlock();
break :blk2 if (items1.data) |items| blk: { const items = locked_items.data orelse return null;
const children = try ctx.arena.create(std.ArrayList(*vxfw.Text)); const children = try ctx.arena.create(std.ArrayList(*vxfw.Text));
children.* = std.ArrayList(*vxfw.Text).init(ctx.arena); children.* = std.ArrayList(*vxfw.Text).init(ctx.arena);
for (items.items) |*original_arc_child| { for (items.items) |*original_arc_child| {
const arc_child = original_arc_child.retain(); const arc_child = original_arc_child.retain();
defer if (arc_child.releaseUnwrap()) |item| item.deinit(); defer if (arc_child.releaseUnwrap()) |item| item.deinit();
const child = arc_child.value.*; const child = arc_child.value.*;
const is_active = if (current_full_name) |c_full_name| const is_active = if (current_full_name) |c_full_name|
models.FullName.eql(&c_full_name, &child.fullName) models.FullName.eql(&c_full_name, &child.fullName)
else else
false; false;
const fg, const bg = colors: { const fg, const bg = colors: {
var fg: vaxis.Color = .default; var fg: vaxis.Color = .default;
var bg: vaxis.Color = .default; var bg: vaxis.Color = .default;
if (is_active) { if (is_active) {
fg = switch (child.item) { fg = switch (child.item) {
.container => .{ .index = 0 }, .container => .{ .index = 0 },
.element => .{ .index = 0 }, .element => .{ .index = 0 },
}; };
bg = switch (child.item) { bg = switch (child.item) {
.container => .{ .index = 4 }, .container => .{ .index = 4 },
.element => .{ .index = 7 }, .element => .{ .index = 7 },
}; };
} else { } else {
fg = switch (child.item) { fg = switch (child.item) {
.container => .{ .index = 4 }, .container => .{ .index = 4 },
.element => .default, .element => .default,
}; };
bg = .default; bg = .default;
} }
break :colors .{ fg, bg }; break :colors .{ fg, bg };
}; };
//NOTE: the right padding is wrong, if the text is too long, the remainder of the text and also the space will be clipped //NOTE: the right padding is wrong, if the text is too long, the remainder of the text and also the space will be clipped
const text = try std.fmt.allocPrint(ctx.arena, " {s} ", .{child.displayName}); const text = try std.fmt.allocPrint(ctx.arena, " {s} ", .{child.displayName});
const text_element = try ctx.arena.create(vxfw.Text); const text_element = try ctx.arena.create(vxfw.Text);
text_element.* = vxfw.Text{ text_element.* = vxfw.Text{
.text = text, .text = text,
.overflow = .clip, .overflow = .clip,
.softwrap = false, .softwrap = false,
.style = .{ .style = .{
.bg = bg, .bg = bg,
.fg = fg, .fg = fg,
}, },
}; };
try children.append(text_element); try children.append(text_element);
} }
break :blk children; break :blk children;
} else {
return null;
};
}; };
const widgets = try ctx.arena.alloc(vxfw.Widget, text_items.items.len); const widgets = try ctx.arena.alloc(vxfw.Widget, text_items.items.len);