From c4d4de67b0e3d5a7d71a868b1fc32cd9bb67e13b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81d=C3=A1m=20Kov=C3=A1cs?= Date: Fri, 23 May 2025 17:13:05 +0200 Subject: [PATCH] feat: refactor3 --- src/app_common/Model.zig | 36 +++++++++------ src/console/main.zig | 95 +++++++++++++++++++++------------------- src/core/tab/tab.zig | 2 - src/gui/main.zig | 8 ++-- 4 files changed, 74 insertions(+), 67 deletions(-) diff --git a/src/app_common/Model.zig b/src/app_common/Model.zig index 75d6a98..dcc799e 100644 --- a/src/app_common/Model.zig +++ b/src/app_common/Model.zig @@ -1,7 +1,7 @@ running: bool = true, usage_number: locked(u16) = .{ .data = 0 }, current_items: locked(?[]*models.Item) = .{ .data = null }, -current_items_allocator: std.heap.ArenaAllocator, +allocator: std.mem.Allocator, appState: AppState, _private: Private, @@ -9,9 +9,9 @@ const Private = struct { 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{ - .current_items_allocator = currentItemsAllocator, + .allocator = allocator, .appState = appState, ._private = .{ .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 { - { - self.current_items.mutex.lock(); - defer self.current_items.mutex.unlock(); + self.resetCurrentItems(); + + 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_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| { items[i] = item; } @@ -55,17 +53,27 @@ pub fn preCurrentItemsUnload(ctx: *anyopaque, tab: *Tab) void { const self: *Self = @ptrCast(@alignCast(ctx)); if (tab == self.appState.currentTab) { // @panic("asdasdasd"); - self.current_items.mutex.lock(); - defer self.current_items.mutex.unlock(); + self.resetCurrentItems(); + } +} - 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 { self.appState.tabPreCurrentItemsUnload.detach(&self._private.preCurrentItemsUnload); self.appState.deinit(); - self.current_items_allocator.deinit(); + + self.resetCurrentItems(); } const std = @import("std"); diff --git a/src/console/main.zig b/src/console/main.zig index f6ddee4..71891e0 100644 --- a/src/console/main.zig +++ b/src/console/main.zig @@ -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(); diff --git a/src/core/tab/tab.zig b/src/core/tab/tab.zig index 73b254b..b3f6445 100644 --- a/src/core/tab/tab.zig +++ b/src/core/tab/tab.zig @@ -55,7 +55,6 @@ pub const Tab = struct { self.currentLocation = newLocationContainer; self.currentLocationChanged.notify(newLocationContainer); - std.debug.print("\nASD1\n", .{}); //TODO: Proper error handling std.Thread.Pool.spawn(self.threadPool, loadItemsWrapper, .{ self, newLocationContainer }) catch unreachable; @@ -119,7 +118,6 @@ pub const Tab = struct { defer self.currentItems.mutex.unlock(); self.currentItemsChanged = true; } - std.debug.print("\nASDX\n", .{}); } pub fn deinit(self: *Tab) void { diff --git a/src/gui/main.zig b/src/gui/main.zig index a79af7c..bf39e87 100644 --- a/src/gui/main.zig +++ b/src/gui/main.zig @@ -53,21 +53,19 @@ pub fn main() !void { const homeFullName: models.FullName = .{ .path = start_path }; var tab1 = try allocator.create(Tab); - defer allocator.destroy(tab1); - tab1.init(&pool, &rootProvider, allocator); - defer tab1.deinit(); try tab1.setCurrentLocation(homeFullName); var appState: AppState = AppState.init(allocator); - appState.currentTab = tab1; + try appState.addTab(tab1); + try appState.setCurrentTab(tab1); const model = try allocator.create(Model); defer allocator.destroy(model); var core_model: CoreModel = undefined; - try CoreModel.init(&core_model, std.heap.ArenaAllocator.init(allocator), appState); + try CoreModel.init(&core_model, allocator, appState); model.* = .{ .allocator = allocator,