diff --git a/src/app_common/Model.zig b/src/app_common/Model.zig index 63c3ad3..75d6a98 100644 --- a/src/app_common/Model.zig +++ b/src/app_common/Model.zig @@ -24,6 +24,33 @@ pub fn init(model: *Self, currentItemsAllocator: std.heap.ArenaAllocator, appSta try model.appState.tabPreCurrentItemsUnload.attach(&model._private.preCurrentItemsUnload); } +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.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; + } + + std.mem.sort(*models.Item, items, {}, struct { + fn sort(_: void, lhs: *models.Item, rhs: *models.Item) bool { + if (lhs.item == .container and rhs.item == .element) return true; + if (lhs.item == .element and rhs.item == .container) return false; + return std.mem.order(u8, lhs.displayName, rhs.displayName) == .lt; + } + }.sort); + + self.current_items.mutex.lock(); + defer self.current_items.mutex.unlock(); + self.current_items.data = items; +} + pub fn preCurrentItemsUnload(ctx: *anyopaque, tab: *Tab) void { const self: *Self = @ptrCast(@alignCast(ctx)); if (tab == self.appState.currentTab) { diff --git a/src/app_common/root.zig b/src/app_common/root.zig index 4df117b..6676f3c 100644 --- a/src/app_common/root.zig +++ b/src/app_common/root.zig @@ -19,30 +19,7 @@ fn inner_loop(appCommonModel: *Model) !void { std.Thread.sleep(10 * std.time.ns_per_ms); if (tab.currentItems.data) |*tab_current_items| { - { - appCommonModel.current_items.mutex.lock(); - defer appCommonModel.current_items.mutex.unlock(); - appCommonModel.current_items.data = null; - } - _ = appCommonModel.current_items_allocator.reset(.retain_capacity); - const allocator = appCommonModel.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; - } - - std.mem.sort(*models.Item, items, {}, struct { - fn sort(_: void, lhs: *models.Item, rhs: *models.Item) bool { - if (lhs.item == .container and rhs.item == .element) return true; - if (lhs.item == .element and rhs.item == .container) return false; - return std.mem.order(u8, lhs.displayName, rhs.displayName) == .lt; - } - }.sort); - - appCommonModel.current_items.mutex.lock(); - defer appCommonModel.current_items.mutex.unlock(); - appCommonModel.current_items.data = items; + try appCommonModel.updateCurrentItems(tab_current_items); tab.currentItemsChanged = false; } }