feat: selected's children
This commit is contained in:
@@ -1,25 +1,30 @@
|
||||
pub const UnknownTabError = error.UnknownTab;
|
||||
|
||||
pub const AppState = struct {
|
||||
allocator: std.mem.Allocator,
|
||||
currentTab: *Tab = undefined,
|
||||
tabs: std.ArrayList(*Tab),
|
||||
currentTabChanged: Observable(*Tab),
|
||||
tabPreCurrentItemsUnload: Observable(*Tab),
|
||||
tabChildrenLoaded: Observable(*Tab),
|
||||
|
||||
pub fn init(allocator: std.mem.Allocator) AppState {
|
||||
return .{
|
||||
.allocator = allocator,
|
||||
.tabs = std.ArrayList(*Tab).init(allocator),
|
||||
.currentTabChanged = Observable(*Tab).init(allocator),
|
||||
.tabPreCurrentItemsUnload = Observable(*Tab).init(allocator),
|
||||
.tabChildrenLoaded = Observable(*Tab).init(allocator),
|
||||
};
|
||||
}
|
||||
|
||||
pub fn addTab(self: *AppState, tab: *Tab) !void {
|
||||
try self.tabs.append(tab);
|
||||
// tab.preCurrentItemsUnload.attach(.{
|
||||
// .ctx = @ptrCast(@alignCast(self)),
|
||||
// .update = preCurrentItemsUnload,
|
||||
// });
|
||||
const tabChildrenLoadedHandlerObserver = try tab.childrenLoaded.allocator.create(Observer(*Tab));
|
||||
tabChildrenLoadedHandlerObserver.* = Observer(*Tab){
|
||||
.ctx = @ptrCast(@alignCast(self)),
|
||||
.update = tabChildrenLoadedHandler,
|
||||
};
|
||||
|
||||
try tab.childrenLoaded.attach(tabChildrenLoadedHandlerObserver);
|
||||
}
|
||||
|
||||
pub fn setCurrentTab(self: *AppState, newTab: *Tab) !void {
|
||||
@@ -34,8 +39,20 @@ pub const AppState = struct {
|
||||
self.currentTabChanged.notify(newTab);
|
||||
}
|
||||
|
||||
pub fn removeTab(self: *AppState, tab: *Tab) void {
|
||||
const index = for (tab.childrenLoaded.observers, 0..) |observer, i| {
|
||||
if (observer.ctx == self) break i;
|
||||
} else null;
|
||||
|
||||
if (index) |i| {
|
||||
tab.childrenLoaded.observers.swapRemove(i);
|
||||
}
|
||||
|
||||
//TODO: remove from tabs
|
||||
}
|
||||
|
||||
pub fn deinit(self: *AppState) void {
|
||||
self.tabPreCurrentItemsUnload.deinit();
|
||||
self.tabChildrenLoaded.deinit();
|
||||
self.currentTabChanged.deinit();
|
||||
for (self.tabs.items) |tab| {
|
||||
tab.deinit();
|
||||
@@ -43,12 +60,13 @@ pub const AppState = struct {
|
||||
self.tabs.deinit();
|
||||
}
|
||||
|
||||
fn preCurrentItemsUnload(ctx: *anyopaque, tab: *Tab) void {
|
||||
const self: AppState = @ptrCast(@alignCast(ctx));
|
||||
self.tabPreCurrentItemsUnload.notify(tab);
|
||||
fn tabChildrenLoadedHandler(ctx: *anyopaque, tab: *Tab) void {
|
||||
const self: *AppState = @ptrCast(@alignCast(ctx));
|
||||
self.tabChildrenLoaded.notify(tab);
|
||||
}
|
||||
};
|
||||
|
||||
const std = @import("std");
|
||||
const Tab = @import("tab/tab.zig").Tab;
|
||||
const Observable = @import("observable.zig").Observable;
|
||||
const Observer = @import("observable.zig").Observer;
|
||||
|
||||
Reference in New Issue
Block a user