feat(core): appstate
This commit is contained in:
50
src/core/app_state.zig
Normal file
50
src/core/app_state.zig
Normal file
@@ -0,0 +1,50 @@
|
||||
pub const UnknownTabError = error.UnknownTab;
|
||||
|
||||
pub const AppState = struct {
|
||||
currentTab: *Tab = undefined,
|
||||
tabs: std.ArrayList(Tab),
|
||||
currentTabChanged: Observable(*Tab),
|
||||
tabPreCurrentItemsUnload: Observable(*Tab),
|
||||
|
||||
pub fn init(allocator: std.mem.Allocator) AppState {
|
||||
return .{
|
||||
.tabs = std.ArrayList(Tab).init(allocator),
|
||||
.currentTabChanged = Observable(*Tab).init(allocator),
|
||||
.tabPreCurrentItemsUnload = Observable(*Tab).init(allocator),
|
||||
};
|
||||
}
|
||||
|
||||
pub fn addTab(self: *AppState, tab: Tab) void {
|
||||
self.tabs.append(tab);
|
||||
// tab.preCurrentItemsUnload.attach(.{
|
||||
// .ctx = @ptrCast(@alignCast(self)),
|
||||
// .update = preCurrentItemsUnload,
|
||||
// });
|
||||
}
|
||||
|
||||
pub fn setCurrentTab(self: *AppState, newTab: *Tab) !void {
|
||||
blk: {
|
||||
for (self.tabs.items) |item| {
|
||||
if (item == newTab) break :blk;
|
||||
}
|
||||
return UnknownTabError;
|
||||
}
|
||||
|
||||
self.currentTab = newTab;
|
||||
self.currentTabChanged.notify(newTab);
|
||||
}
|
||||
|
||||
pub fn deinit(self: *AppState) void {
|
||||
self.currentTabChanged.deinit();
|
||||
self.tabs.deinit();
|
||||
}
|
||||
|
||||
fn preCurrentItemsUnload(ctx: *anyopaque, tab: *Tab) void {
|
||||
const self: AppState = @ptrCast(@alignCast(ctx));
|
||||
self.tabPreCurrentItemsUnload.notify(tab);
|
||||
}
|
||||
};
|
||||
|
||||
const std = @import("std");
|
||||
const Tab = @import("tab/tab.zig").Tab;
|
||||
const Observable = @import("observable.zig").Observable;
|
||||
Reference in New Issue
Block a user