Skip to content

Commit a39b65e

Browse files
committed
d
1 parent 88ff34f commit a39b65e

File tree

10 files changed

+140
-58
lines changed

10 files changed

+140
-58
lines changed

src/main/browser/tabs/managers/tabs-container-manager.ts

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export class TabsContainerManager extends TypedEventEmitter<TabsContainerManager
1616
public isDestroyed: boolean = false;
1717

1818
private readonly browser: Browser;
19+
private spaceContainerMap: Map<string, TabContainer> = new Map();
1920

2021
constructor(browser: Browser) {
2122
super();
@@ -58,8 +59,6 @@ export class TabsContainerManager extends TypedEventEmitter<TabsContainerManager
5859

5960
updateSpaces();
6061
spacesEmitter.on("changed", updateSpaces);
61-
62-
const favouritesContainer = new TabContainer(spaces[0]?.id ?? "");
6362
}
6463

6564
/**
@@ -69,17 +68,32 @@ export class TabsContainerManager extends TypedEventEmitter<TabsContainerManager
6968
* @param {SpaceData & { id: string }} space - The space data including its ID
7069
*/
7170
private _setupSpace(space: SpaceData & { id: string }): void {
72-
const normalContainer = new TabContainer(space.id);
71+
const spaceContainer = new TabContainer(space.id);
72+
this.spaceContainerMap.set(space.id, spaceContainer);
7373

7474
// Add tab group to the correct container on create
75-
const browser = this.browser;
76-
const tabOrchestrator = browser.tabs;
77-
tabOrchestrator.tabGroupManager.on("tab-group-created", (tabGroup) => {
78-
normalContainer.addChild({
79-
type: "tab-group",
80-
item: tabGroup
81-
});
82-
});
75+
// const browser = this.browser;
76+
// const tabOrchestrator = browser.tabs;
77+
// tabOrchestrator.tabGroupManager.on("tab-group-created", (tabGroup) => {
78+
// spaceContainer.addChild({
79+
// type: "tab-group",
80+
// item: tabGroup
81+
// });
82+
// });
83+
}
84+
85+
/**
86+
* Gets the tab container for a specific space
87+
* @public
88+
* @param {string} spaceId - The ID of the space to get the container for
89+
* @returns {TabContainer} - The tab container for the space
90+
*/
91+
public getSpaceContainer(spaceId: string): TabContainer {
92+
const spaceContainer = this.spaceContainerMap.get(spaceId);
93+
if (!spaceContainer) {
94+
throw new Error(`Space container not found for spaceId: ${spaceId}`);
95+
}
96+
return spaceContainer;
8397
}
8498

8599
/**

src/main/browser/tabs/objects/tab-containers/base.ts

Lines changed: 66 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { TabGroup } from "@/browser/tabs/objects/tab-group";
22
import { TabFolder } from "@/browser/tabs/objects/tab-containers/tab-folder";
33
import { TypedEventEmitter } from "@/modules/typed-event-emitter";
44
import { NormalTabGroup } from "@/browser/tabs/objects/tab-group/types/normal";
5+
import { TabbedBrowserWindow } from "@/browser/window";
6+
import { Browser } from "@/browser/browser";
57

68
// Container Child //
79
type TabGroupChild = {
@@ -35,6 +37,13 @@ export type ExportedTabsFolder = {
3537
// ExportedTabContainer would not be a child: it should only be the root container.
3638
type ExportedTabData = ExportedTabGroup | ExportedTabsFolder;
3739

40+
// Tab Container Shared Data //
41+
export interface TabContainerSharedData {
42+
browser: Browser;
43+
window: TabbedBrowserWindow;
44+
space: string;
45+
}
46+
3847
// Base Tab Container //
3948
export type BaseTabContainerEvents = {
4049
"child-added": [child: ContainerChild];
@@ -47,34 +56,32 @@ export class BaseTabContainer<
4756
> extends TypedEventEmitter<TEvents> {
4857
public children: ContainerChild[];
4958

50-
constructor() {
59+
public sharedData: TabContainerSharedData;
60+
61+
constructor(sharedData: TabContainerSharedData) {
5162
super();
5263

5364
this.children = [];
65+
this.sharedData = sharedData;
5466
}
5567

56-
public getAllTabGroups(): TabGroup[] {
57-
const scanTabContainer = (container: BaseTabContainer): TabGroup[] => {
58-
return container.children.flatMap((child) => {
59-
if (child.type === "tab-group") {
60-
return [child.item];
61-
}
62-
if (child.type === "tab-folder") {
63-
return scanTabContainer(child.item);
64-
}
65-
return [];
66-
});
67-
};
68-
69-
return scanTabContainer(this);
70-
}
68+
// Modify Children //
7169

72-
public addChild(child: ContainerChild): void {
70+
/**
71+
* Add a child to the container.
72+
* @param child - The child to add.
73+
*/
74+
private addChild(child: ContainerChild): void {
7375
this.children.push(child);
7476
this.emit("child-added", child);
7577
}
7678

77-
public removeChild(child: ContainerChild): boolean {
79+
/**
80+
* Remove a child from the container.
81+
* @param child - The child to remove.
82+
* @returns Whether the child was removed.
83+
*/
84+
private removeChild(child: ContainerChild): boolean {
7885
const index = this.children.indexOf(child);
7986
if (index === -1) return false;
8087

@@ -83,6 +90,12 @@ export class BaseTabContainer<
8390
return true;
8491
}
8592

93+
/**
94+
* Move a child to a new index.
95+
* @param from - The index to move from.
96+
* @param to - The index to move to.
97+
* @returns Whether the child was moved.
98+
*/
8699
public moveChild(from: number, to: number): boolean {
87100
if (from < 0 || from >= this.children.length || to < 0 || to >= this.children.length) {
88101
return false;
@@ -94,6 +107,8 @@ export class BaseTabContainer<
94107
return true;
95108
}
96109

110+
// Get Children //
111+
97112
public findChildrenByType(type: "tab-group" | "tab-container"): ContainerChild[] {
98113
return this.children.filter((child) => child.type === type);
99114
}
@@ -106,22 +121,48 @@ export class BaseTabContainer<
106121
return this.children.length;
107122
}
108123

124+
// New Children //
125+
126+
public newTabFolder(name: string): TabFolder {
127+
const folder = new TabFolder(name, this);
128+
this.addChild({ type: "tab-folder", item: folder });
129+
return folder;
130+
}
131+
109132
public newNormalTabGroup(): NormalTabGroup {
110133
const tabGroup = new NormalTabGroup({
111-
browser: this.browser,
112-
window: this.window,
113-
space: this.space
134+
browser: this.sharedData.browser,
135+
window: this.sharedData.window,
136+
space: this.sharedData.space
114137
});
115138
this.addChild({ type: "tab-group", item: tabGroup });
116139
return tabGroup;
117140
}
118141

119-
public createTabFolder(name: string): TabFolder {
120-
const folder = new TabFolder(name);
121-
this.addChild({ type: "tab-folder", item: folder });
122-
return folder;
142+
// Others //
143+
144+
/**
145+
* Get all tab groups in the container.
146+
* @returns All tab groups in the container.
147+
*/
148+
public getAllTabGroups(): TabGroup[] {
149+
const scanTabContainer = (container: BaseTabContainer): TabGroup[] => {
150+
return container.children.flatMap((child) => {
151+
if (child.type === "tab-group") {
152+
return [child.item];
153+
}
154+
if (child.type === "tab-folder") {
155+
return scanTabContainer(child.item);
156+
}
157+
return [];
158+
});
159+
};
160+
161+
return scanTabContainer(this);
123162
}
124163

164+
// Export //
165+
125166
protected baseExport(): ExportedBaseTabContainer {
126167
return {
127168
type: "tab-container",

src/main/browser/tabs/objects/tab-containers/tab-container.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { BaseTabContainer, BaseTabContainerEvents, ExportedTabContainer } from "./base";
1+
import { BaseTabContainer, BaseTabContainerEvents, ExportedTabContainer, TabContainerSharedData } from "./base";
22

33
type TabContainerEvents = BaseTabContainerEvents & {
44
"space-changed": [spaceId: string];
@@ -7,8 +7,8 @@ type TabContainerEvents = BaseTabContainerEvents & {
77
export class TabContainer extends BaseTabContainer<TabContainerEvents> {
88
public spaceId: string;
99

10-
constructor(spaceId: string) {
11-
super();
10+
constructor(spaceId: string, sharedData: TabContainerSharedData) {
11+
super(sharedData);
1212
this.spaceId = spaceId;
1313
}
1414

src/main/browser/tabs/objects/tab-containers/tab-folder.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ import { BaseTabContainer, ExportedTabsFolder } from "./base";
22

33
export class TabFolder extends BaseTabContainer {
44
public name: string;
5+
public parent: BaseTabContainer;
56

6-
constructor(name: string) {
7-
super();
7+
constructor(name: string, parent: BaseTabContainer) {
8+
super(parent.sharedData);
89
this.name = name;
10+
this.parent = parent;
911
}
1012

1113
public setName(name: string): void {

src/main/browser/tabs/objects/tab/controllers/bounds.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
/*
2+
TabBoundsController:
3+
- This controller is responsible for managing the bounds of the tab
4+
- Methods should be called by the Tab Container.
5+
*/
6+
17
import { Tab } from "@/browser/tabs/objects/tab";
28
import { PageBounds } from "~/flow/types";
39

src/main/browser/tabs/objects/tab/controllers/context-menu.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/*
2+
TabContextMenuController:
3+
- This controller is responsible for creating and controlling the context menu for the tab
4+
*/
5+
16
import { Tab } from "@/browser/tabs/objects/tab";
27
import { Browser } from "@/browser/browser";
38
import { TabbedBrowserWindow } from "@/browser/window";
@@ -46,12 +51,15 @@ function createTabContextMenu(browser: Browser, tab: Tab, profileId: string) {
4651

4752
// Helper function to create a new tab
4853
const createNewTab = async (url: string, window?: TabbedBrowserWindow) => {
49-
const tabbedWindow = tab.window.get();
50-
const spaceId = tab.space.get();
54+
// TODO: Implement this
55+
console.log("createNewTab", url, window, profileId);
56+
57+
// const tabbedWindow = tab.window.get();
58+
// const spaceId = tab.space.get();
5159

52-
const sourceTab = await browser.tabs.createTab(window ? window.id : tabbedWindow?.id, profileId, spaceId, url);
53-
sourceTab.loadURL(url);
54-
browser.tabs.setActiveTab(sourceTab);
60+
// const sourceTab = await browser.tabs.createTab(window ? window.id : tabbedWindow?.id, profileId, spaceId, url);
61+
// sourceTab.loadURL(url);
62+
// browser.tabs.setActiveTab(sourceTab);
5563
};
5664

5765
// Create all menu sections
@@ -288,10 +296,10 @@ function combineSections(
288296
}
289297

290298
export class TabContextMenuController {
291-
private readonly tab: Tab;
299+
// private readonly tab: Tab;
292300

293301
constructor(tab: Tab) {
294-
this.tab = tab;
302+
// this.tab = tab;
295303

296304
tab.on("webview-attached", () => {
297305
const browser = tab.browser;

src/main/browser/tabs/objects/tab/controllers/data.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
/*
22
TabDataController:
3-
- This controller stores all the data that needs to persist between browser restarts
4-
- Other datas will be stored in their respective controllers
3+
- This controller stores all the data that needs to be synced with the frontend
54
*/
65

76
import { Tab } from "@/browser/tabs/objects/tab";
@@ -41,6 +40,7 @@ export class TabDataController {
4140

4241
tab.on("webview-detached", () => this.onWebviewDetached());
4342

43+
// Wait for every controller to be ready
4444
setImmediate(() => this.refreshData());
4545
}
4646

@@ -70,19 +70,19 @@ export class TabDataController {
7070
const pipActive = tab.pip.active;
7171
setProperty("pipActive", pipActive);
7272

73-
// Asleep
73+
// asleep
7474
const asleep = tab.sleep.asleep;
7575
setProperty("asleep", asleep);
7676

7777
/// From webview ///
7878

7979
const webContents = tab.webview.webContents;
8080
if (webContents) {
81-
// Title
81+
// title
8282
const title = webContents.getTitle();
8383
setProperty("title", title);
8484

85-
// URL
85+
// url
8686
const url = webContents.getURL();
8787
setProperty("url", url);
8888

@@ -91,7 +91,7 @@ export class TabDataController {
9191
setProperty("isLoading", isLoading);
9292

9393
// audible
94-
const audible = webContents.isAudioMuted();
94+
const audible = webContents.isCurrentlyAudible();
9595
setProperty("audible", audible);
9696

9797
// muted
@@ -103,12 +103,12 @@ export class TabDataController {
103103

104104
// Process changes
105105
if (changed) {
106-
this.tab.emit("data-changed");
106+
this.emitDataChanged();
107107
}
108108
return changed;
109109
}
110110

111-
public setupWebviewData(webContents: WebContents) {
111+
public setupWebviewChangeHooks(webContents: WebContents) {
112112
// audible
113113
webContents.on("audio-state-changed", () => this.refreshData());
114114
webContents.on("media-started-playing", () => this.refreshData());
@@ -142,7 +142,6 @@ export class TabDataController {
142142
// from other controllers
143143
window: this.window,
144144
pipActive: this.pipActive,
145-
asleep: this.asleep,
146145

147146
// from navigation
148147
navHistory: navHistory,

src/main/browser/tabs/objects/tab/controllers/visiblity.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
/*
2+
TabVisiblityController:
3+
- This controller is responsible for managing the visiblity of the tab
4+
- Methods should be called by the Tab Container.
5+
*/
6+
17
import { Tab } from "@/browser/tabs/objects/tab";
28

39
export class TabVisiblityController {

src/main/browser/tabs/objects/tab/controllers/webview.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ export class TabWebviewController {
7777
});
7878

7979
tab.navigation.setupWebviewNavigation(this.webContents);
80-
tab.data.setupWebviewData(this.webContents);
80+
tab.data.setupWebviewChangeHooks(this.webContents);
8181

8282
tab.emit("webview-attached");
8383

src/main/browser/tabs/objects/tab/controllers/window.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
/*
2+
TabWindowController:
3+
- This controller is responsible for managing the window of the tab
4+
- Methods should be called by the Tab Container.
5+
*/
6+
17
import { Tab } from "@/browser/tabs/objects/tab";
28
import { TabbedBrowserWindow } from "@/browser/window";
39

0 commit comments

Comments
 (0)