-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWindowManagerContext.jsx
More file actions
48 lines (40 loc) · 1.3 KB
/
WindowManagerContext.jsx
File metadata and controls
48 lines (40 loc) · 1.3 KB
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
import React, { createContext, useContext, useState } from 'react';
const WindowContext = createContext();
export function useWindowManager() {
return useContext(WindowContext);
}
export function WindowManagerProvider({ children }) {
const [windows, setWindows] = useState([]);
const [focusedWindowId, setFocusedWindowId] = useState(null);
const openWindow = (appId, title, component, icon) => {
// Check if already open
const existing = windows.find(w => w.id === appId);
if (existing) {
setFocusedWindowId(appId);
return;
}
const newWindow = {
id: appId,
title,
component,
icon,
isMinimized: false,
};
setWindows([...windows, newWindow]);
setFocusedWindowId(appId);
};
const closeWindow = (id) => {
setWindows(windows.filter(w => w.id !== id));
if (focusedWindowId === id) {
setFocusedWindowId(null);
}
};
const focusWindow = (id) => {
setFocusedWindowId(id);
};
return (
<WindowContext.Provider value={{ windows, openWindow, closeWindow, focusWindow, focusedWindowId }}>
{children}
</WindowContext.Provider>
);
}