feat(gui): base project

This commit is contained in:
2025-05-08 10:13:59 +02:00
parent 537e42ef5b
commit 277f87b8bb
7 changed files with 314 additions and 82 deletions

14
src/app_common/Model.zig Normal file
View File

@@ -0,0 +1,14 @@
const std = @import("std");
const models = @import("../core/models.zig");
const Tab = @import("../core/tab/tab.zig").Tab;
const locked = @import("../core/sync.zig").locked;
running: bool = true,
usage_number: locked(u16) = .{ .data = 0 },
current_items: locked(?[]*models.Item) = .{ .data = null },
current_items_allocator: std.heap.ArenaAllocator,
tab: *Tab,
pub fn deinit(self: *@This()) void {
self.current_items_allocator.deinit();
}

44
src/app_common/root.zig Normal file
View File

@@ -0,0 +1,44 @@
const std = @import("std");
const models = @import("../core/models.zig");
const Model = @import("Model.zig");
pub fn data_loop(vm: *Model) void {
vm.usage_number.data += 1;
while (vm.running) {
inner_loop(vm) catch {};
std.Thread.sleep(100 * std.time.ns_per_ms);
}
vm.usage_number.data -= 1;
}
fn inner_loop(vm: *Model) !void {
if (vm.tab.currentItemsChanged) {
std.Thread.sleep(10 * std.time.ns_per_ms);
vm.tab.currentItems.mutex.lock();
defer vm.tab.currentItems.mutex.unlock();
if (vm.tab.currentItems.data) |current_items| {
_ = vm.current_items_allocator.reset(.retain_capacity);
const allocator = vm.current_items_allocator.allocator();
const items = try allocator.alloc(*models.Item, current_items.items.len);
for (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);
vm.current_items.mutex.lock();
defer vm.current_items.mutex.unlock();
vm.current_items.data = items;
vm.tab.currentItemsChanged = false;
}
}
}