feat: refactor3
This commit is contained in:
@@ -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.resetCurrentItems();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn resetCurrentItems(self: *@This()) void {
|
||||||
self.current_items.mutex.lock();
|
self.current_items.mutex.lock();
|
||||||
defer self.current_items.mutex.unlock();
|
defer self.current_items.mutex.unlock();
|
||||||
|
|
||||||
self.current_items.data = null;
|
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");
|
||||||
|
|||||||
@@ -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,9 +119,11 @@ 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");
|
||||||
|
|
||||||
|
const text_items = blk2: {
|
||||||
vm.app_common_model.current_items.mutex.lock();
|
vm.app_common_model.current_items.mutex.lock();
|
||||||
defer vm.app_common_model.current_items.mutex.unlock();
|
defer vm.app_common_model.current_items.mutex.unlock();
|
||||||
const text_items = if (vm.app_common_model.current_items.data) |items| blk: {
|
|
||||||
|
break :blk2 if (vm.app_common_model.current_items.data) |items| blk: {
|
||||||
const children = try ctx.arena.alloc(*vxfw.Text, items.len);
|
const children = try ctx.arena.alloc(*vxfw.Text, items.len);
|
||||||
for (0.., items[0..children.len]) |i, child| {
|
for (0.., items[0..children.len]) |i, child| {
|
||||||
const is_active = i == vm.current_items_view.cursor;
|
const is_active = i == vm.current_items_view.cursor;
|
||||||
@@ -164,6 +166,7 @@ const Model = struct {
|
|||||||
}
|
}
|
||||||
break :blk children;
|
break :blk children;
|
||||||
} else &.{};
|
} 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();
|
||||||
|
|||||||
@@ -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 {
|
||||||
|
|||||||
@@ -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,
|
||||||
|
|||||||
Reference in New Issue
Block a user