feat: refactor3

This commit is contained in:
2025-05-23 17:13:05 +02:00
parent 882f38b90f
commit c4d4de67b0
4 changed files with 74 additions and 67 deletions

View File

@@ -3,7 +3,7 @@ const Model = struct {
crash: bool = false,
allocator: std.mem.Allocator,
current_items_view: *vxfw.ListView,
app_common_model: AppCommonModel,
app_common_model: *AppCommonModel,
root_provider: *RootProvider,
/// Helper function to return a vxfw.Widget struct
@@ -119,51 +119,54 @@ const Model = struct {
fn createCurrentItems(ctx: vxfw.DrawContext, vm: *Model) !void {
if (vm.crash) @panic("asd123");
vm.app_common_model.current_items.mutex.lock();
defer vm.app_common_model.current_items.mutex.unlock();
const text_items = if (vm.app_common_model.current_items.data) |items| blk: {
const children = try ctx.arena.alloc(*vxfw.Text, items.len);
for (0.., items[0..children.len]) |i, child| {
const is_active = i == vm.current_items_view.cursor;
const text_items = blk2: {
vm.app_common_model.current_items.mutex.lock();
defer vm.app_common_model.current_items.mutex.unlock();
const fg, const bg = colors: {
var fg: vaxis.Color = .default;
var bg: vaxis.Color = .default;
if (is_active) {
fg = switch (child.item) {
.container => .{ .index = 0 },
.element => .{ .index = 0 },
};
bg = switch (child.item) {
.container => .{ .index = 4 },
.element => .{ .index = 7 },
};
} else {
fg = switch (child.item) {
.container => .{ .index = 4 },
.element => .default,
};
bg = .default;
}
break :colors .{ fg, bg };
};
break :blk2 if (vm.app_common_model.current_items.data) |items| blk: {
const children = try ctx.arena.alloc(*vxfw.Text, items.len);
for (0.., items[0..children.len]) |i, child| {
const is_active = i == vm.current_items_view.cursor;
const text = try ctx.arena.dupe(u8, child.fullName.path);
const text_element = try ctx.arena.create(vxfw.Text);
text_element.* = vxfw.Text{
.text = text,
.overflow = .clip,
.softwrap = false,
.style = .{
.bg = bg,
.fg = fg,
},
};
const fg, const bg = colors: {
var fg: vaxis.Color = .default;
var bg: vaxis.Color = .default;
if (is_active) {
fg = switch (child.item) {
.container => .{ .index = 0 },
.element => .{ .index = 0 },
};
bg = switch (child.item) {
.container => .{ .index = 4 },
.element => .{ .index = 7 },
};
} else {
fg = switch (child.item) {
.container => .{ .index = 4 },
.element => .default,
};
bg = .default;
}
break :colors .{ fg, bg };
};
children[i] = text_element;
}
break :blk children;
} else &.{};
const text = try ctx.arena.dupe(u8, child.fullName.path);
const text_element = try ctx.arena.create(vxfw.Text);
text_element.* = vxfw.Text{
.text = text,
.overflow = .clip,
.softwrap = false,
.style = .{
.bg = bg,
.fg = fg,
},
};
children[i] = text_element;
}
break :blk children;
} else &.{};
};
const widgets = try ctx.arena.alloc(vxfw.Widget, text_items.len);
for (text_items, 0..) |t, i| {
@@ -266,19 +269,19 @@ pub fn main() !void {
defer allocator.destroy(model);
var app_common_model: AppCommonModel = undefined;
try AppCommonModel.init(&app_common_model, std.heap.ArenaAllocator.init(allocator), appState);
try AppCommonModel.init(&app_common_model, allocator, appState);
defer app_common_model.deinit();
model.* = .{
.allocator = allocator,
.app_common_model = app_common_model,
.app_common_model = &app_common_model,
.current_items_view = &list,
.root_provider = &rootProvider,
};
model.app_common_model.usage_number.data += 1;
try pool.spawn(core.data_loop, .{&model.app_common_model});
try pool.spawn(core.data_loop, .{model.app_common_model});
var app = try vxfw.App.init(allocator);
defer app.deinit();