-
Notifications
You must be signed in to change notification settings - Fork 0
/
windowsManager.ts
65 lines (60 loc) · 1.23 KB
/
windowsManager.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import type { Slice } from 'coaction';
export interface WindowShape {
x: number;
y: number;
w: number;
h: number;
}
export interface WindowMetaData {
// example metadata
foo: string;
}
export interface WindowInfo {
id: number;
shape: WindowShape;
metaData: WindowMetaData;
}
export interface WindowsManager {
tabCount: number;
windows: WindowInfo[];
init({
shape,
metaData
}: {
shape: WindowShape;
metaData: WindowMetaData;
}): number;
update({ id, shape }: { id: number; shape: WindowShape }): void;
remove({ id }: { id: number }): void;
}
export const windowsManager: Slice<WindowsManager> = (set) => ({
tabCount: 0,
windows: [],
init({ shape, metaData }) {
set(() => {
this.tabCount++;
this.windows.push({
id: this.tabCount,
shape,
metaData
});
});
return this.tabCount;
},
update({ id, shape }) {
set(() => {
const idx = this.windows.findIndex((w) => w.id === id);
if (idx > -1) {
this.windows[idx].shape = shape;
}
});
},
remove({ id }) {
set(() => {
const idx = this.windows.findIndex((w) => w.id === id);
if (idx > -1) {
this.windows.splice(idx, 1);
}
});
}
});