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

@@ -1,7 +1,7 @@
running: bool = true, running: bool = true,
usage_number: locked(u16) = .{ .data = 0 }, usage_number: locked(u16) = .{ .data = 0 },
current_items: locked(?[]*models.Item) = .{ .data = null }, current_items: locked(?[]*models.Item) = .{ .data = null },
current_items_allocator: std.heap.ArenaAllocator, allocator: std.mem.Allocator,
appState: AppState, appState: AppState,
_private: Private, _private: Private,
@@ -9,9 +9,9 @@ const Private = struct {
preCurrentItemsUnload: Observer(*Tab), preCurrentItemsUnload: Observer(*Tab),
}; };
pub fn init(model: *Self, currentItemsAllocator: std.heap.ArenaAllocator, appState: AppState) !void { pub fn init(model: *Self, allocator: std.mem.Allocator, appState: AppState) !void {
model.* = Self{ model.* = Self{
.current_items_allocator = currentItemsAllocator, .allocator = allocator,
.appState = appState, .appState = appState,
._private = .{ ._private = .{
.preCurrentItemsUnload = Observer(*Tab){ .preCurrentItemsUnload = Observer(*Tab){
@@ -25,15 +25,13 @@ pub fn init(model: *Self, currentItemsAllocator: std.heap.ArenaAllocator, appSta
} }
pub fn updateCurrentItems(self: *Self, tab_current_items: *std.ArrayList(*models.Item)) !void { pub fn updateCurrentItems(self: *Self, tab_current_items: *std.ArrayList(*models.Item)) !void {
{ self.resetCurrentItems();
self.current_items.mutex.lock();
defer self.current_items.mutex.unlock(); const items = try self.allocator.alloc(*models.Item, tab_current_items.items.len);
errdefer {
self.allocator.free(items);
self.current_items.data = null; self.current_items.data = null;
} }
_ = self.current_items_allocator.reset(.retain_capacity);
const allocator = self.current_items_allocator.allocator();
const items = try allocator.alloc(*models.Item, tab_current_items.items.len);
for (tab_current_items.items, 0..) |item, i| { for (tab_current_items.items, 0..) |item, i| {
items[i] = item; items[i] = item;
} }
@@ -55,17 +53,27 @@ pub fn preCurrentItemsUnload(ctx: *anyopaque, tab: *Tab) void {
const self: *Self = @ptrCast(@alignCast(ctx)); const self: *Self = @ptrCast(@alignCast(ctx));
if (tab == self.appState.currentTab) { if (tab == self.appState.currentTab) {
// @panic("asdasdasd"); // @panic("asdasdasd");
self.current_items.mutex.lock(); self.resetCurrentItems();
defer self.current_items.mutex.unlock(); }
}
self.current_items.data = null; fn resetCurrentItems(self: *@This()) void {
self.current_items.mutex.lock();
defer self.current_items.mutex.unlock();
const data = self.current_items.data;
// self.current_items.data = null;
if (data) |currentItems| {
self.allocator.free(currentItems);
} }
} }
pub fn deinit(self: *@This()) void { pub fn deinit(self: *@This()) void {
self.appState.tabPreCurrentItemsUnload.detach(&self._private.preCurrentItemsUnload); self.appState.tabPreCurrentItemsUnload.detach(&self._private.preCurrentItemsUnload);
self.appState.deinit(); self.appState.deinit();
self.current_items_allocator.deinit();
self.resetCurrentItems();
} }
const std = @import("std"); const std = @import("std");

View File

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

View File

@@ -55,7 +55,6 @@ pub const Tab = struct {
self.currentLocation = newLocationContainer; self.currentLocation = newLocationContainer;
self.currentLocationChanged.notify(newLocationContainer); self.currentLocationChanged.notify(newLocationContainer);
std.debug.print("\nASD1\n", .{});
//TODO: Proper error handling //TODO: Proper error handling
std.Thread.Pool.spawn(self.threadPool, loadItemsWrapper, .{ self, newLocationContainer }) catch unreachable; std.Thread.Pool.spawn(self.threadPool, loadItemsWrapper, .{ self, newLocationContainer }) catch unreachable;
@@ -119,7 +118,6 @@ pub const Tab = struct {
defer self.currentItems.mutex.unlock(); defer self.currentItems.mutex.unlock();
self.currentItemsChanged = true; self.currentItemsChanged = true;
} }
std.debug.print("\nASDX\n", .{});
} }
pub fn deinit(self: *Tab) void { pub fn deinit(self: *Tab) void {

View File

@@ -53,21 +53,19 @@ pub fn main() !void {
const homeFullName: models.FullName = .{ .path = start_path }; const homeFullName: models.FullName = .{ .path = start_path };
var tab1 = try allocator.create(Tab); var tab1 = try allocator.create(Tab);
defer allocator.destroy(tab1);
tab1.init(&pool, &rootProvider, allocator); tab1.init(&pool, &rootProvider, allocator);
defer tab1.deinit();
try tab1.setCurrentLocation(homeFullName); try tab1.setCurrentLocation(homeFullName);
var appState: AppState = AppState.init(allocator); var appState: AppState = AppState.init(allocator);
appState.currentTab = tab1; try appState.addTab(tab1);
try appState.setCurrentTab(tab1);
const model = try allocator.create(Model); const model = try allocator.create(Model);
defer allocator.destroy(model); defer allocator.destroy(model);
var core_model: CoreModel = undefined; var core_model: CoreModel = undefined;
try CoreModel.init(&core_model, std.heap.ArenaAllocator.init(allocator), appState); try CoreModel.init(&core_model, allocator, appState);
model.* = .{ model.* = .{
.allocator = allocator, .allocator = allocator,