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

View File

@@ -7,19 +7,15 @@ const provider = @import("../core/provider/provider.zig");
const local_provider = @import("../core/provider/local.zig");
const Tab = @import("../core/tab/tab.zig").Tab;
const locked = @import("../core/sync.zig").locked;
const CoreModel = @import("../app_common/Model.zig");
const core = @import("../app_common/root.zig");
/// Our main application state
const Model = struct {
crash: bool = false,
usage_number: locked(u16) = .{ .data = 0 },
running: bool = true,
allocator: std.mem.Allocator,
current_items: locked(?[]*models.Item) = .{ .data = null },
current_items_view: *vxfw.ListView,
current_items_allocator: std.heap.ArenaAllocator,
tab: *Tab,
/// State of the counter
count: usize = 0,
core_model: CoreModel,
/// Helper function to return a vxfw.Widget struct
pub fn widget(self: *Model) vxfw.Widget {
@@ -97,7 +93,7 @@ const Model = struct {
};
try rootWidgets.append(list_surface);
const current_location_text = if (vm.tab.currentLocation) |loc| loc.item.fullName.path else "";
const current_location_text = if (vm.core_model.tab.currentLocation) |loc| loc.item.fullName.path else "";
const current_location_text_element = try ctx.arena.create(vxfw.Text);
current_location_text_element.* = vxfw.Text{
.text = current_location_text,
@@ -126,9 +122,9 @@ const Model = struct {
fn createCurrentItems(ctx: vxfw.DrawContext, vm: *Model) !void {
if (vm.crash) @panic("asd123");
vm.current_items.mutex.lock();
defer vm.current_items.mutex.unlock();
const text_items = if (vm.current_items.data) |items| blk: {
vm.core_model.current_items.mutex.lock();
defer vm.core_model.current_items.mutex.unlock();
const text_items = if (vm.core_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;
@@ -179,60 +175,8 @@ const Model = struct {
vm.current_items_view.children.slice = widgets;
}
fn onClick(maybe_ptr: ?*anyopaque, ctx: *vxfw.EventContext) anyerror!void {
const ptr = maybe_ptr orelse return;
const self: *Model = @ptrCast(@alignCast(ptr));
self.count +|= 1;
return ctx.consumeAndRedraw();
}
};
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;
}
}
vm.tab.currentItems.mutex.lock();
defer vm.tab.currentItems.mutex.unlock();
vm.count = if (vm.tab.currentItems.data) |c| c.items.len else 999;
}
pub fn main() !void {
const DebugAllocator = std.heap.DebugAllocator(.{});
var gpa = DebugAllocator{};
@@ -277,7 +221,6 @@ pub fn main() !void {
const homeFullName: models.FullName = .{ .path = "/home/adam" };
const homeItem = try localContentProvider.getItemByFullName(homeFullName, &.{}, allocator);
// defer homeItem.deinit();
const c = switch (homeItem.item) {
.container => |c| c,
.element => unreachable,
@@ -294,6 +237,7 @@ pub fn main() !void {
const model = try allocator.create(Model);
defer allocator.destroy(model);
// TODO: remove
std.Thread.sleep(1 * std.time.ns_per_s);
const empty_text_element = try allocator.create(vxfw.Text);
@@ -318,16 +262,17 @@ pub fn main() !void {
};
model.* = .{
.allocator = allocator,
.current_items_allocator = std.heap.ArenaAllocator.init(allocator),
.core_model = .{
.current_items_allocator = std.heap.ArenaAllocator.init(allocator),
.tab = tab1,
},
.current_items_view = &list,
.tab = tab1,
.count = 0,
};
defer model.current_items_allocator.deinit();
defer model.core_model.deinit();
model.usage_number.data += 1;
model.core_model.usage_number.data += 1;
try pool.spawn(data_loop, .{model});
try pool.spawn(core.data_loop, .{&model.core_model});
var app = try vxfw.App.init(allocator);
defer app.deinit();
@@ -335,10 +280,10 @@ pub fn main() !void {
try app.run(model.widget(), .{});
// std.Thread.sleep(10 * std.time.ns_per_s);
model.usage_number.data -= 1;
model.running = false;
model.core_model.usage_number.data -= 1;
model.core_model.running = false;
while (model.usage_number.data > 0) {
while (model.core_model.usage_number.data > 0) {
std.Thread.sleep(10 * std.time.ns_per_ms);
}
}