ywana-core8 0.2.4 → 0.2.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,12 +1,63 @@
1
- import React, { useRef, useEffect } from 'react'
1
+ import React, { useRef, useEffect, useState, createContext, useContext } from 'react'
2
2
  import { WindowProvider, useWindows } from './WindowContext'
3
3
  import { Window } from './window'
4
+ import { ApplicationMenu } from './ApplicationMenu'
5
+ import { defaultAppManager } from './AppManager'
4
6
  import './desktop.css'
7
+ import './desktop-windows.css'
8
+ import './desktop-linux.css'
9
+ import './desktop-macos.css'
10
+
11
+ /**
12
+ * Context for Application state
13
+ */
14
+ const AppContext = createContext()
15
+
16
+ export const useApplicationMenu = () => {
17
+ const context = useContext(AppContext)
18
+ if (!context) {
19
+ throw new Error('useApplicationMenu must be used within AppProvider')
20
+ }
21
+ return context.applicationMenu
22
+ }
23
+
24
+ export const useAppManager = () => {
25
+ const context = useContext(AppContext)
26
+ if (!context) {
27
+ throw new Error('useAppManager must be used within AppProvider')
28
+ }
29
+ return context.appManager
30
+ }
31
+
32
+ /**
33
+ * AppProvider - Provides Application state and AppManager to React components
34
+ */
35
+ export const AppProvider = ({ children, appManager = defaultAppManager }) => {
36
+ const [isApplicationMenuOpen, setIsApplicationMenuOpen] = useState(false)
37
+
38
+ const value = {
39
+ // Application Menu state
40
+ applicationMenu: {
41
+ isOpen: isApplicationMenuOpen,
42
+ open: () => setIsApplicationMenuOpen(true),
43
+ close: () => setIsApplicationMenuOpen(false),
44
+ toggle: () => setIsApplicationMenuOpen(!isApplicationMenuOpen)
45
+ },
46
+ // App Manager instance
47
+ appManager: appManager
48
+ }
49
+
50
+ return (
51
+ <AppContext.Provider value={value}>
52
+ {children}
53
+ </AppContext.Provider>
54
+ )
55
+ }
5
56
 
6
57
  /**
7
58
  * Desktop layout component - manages overall desktop structure and sizing
8
59
  */
9
- const DesktopLayout = ({ children, className = '', ...props }) => {
60
+ const DesktopLayout = ({ children, className = '', theme = 'windows', ...props }) => {
10
61
  const desktopRef = useRef(null)
11
62
  const { windowManager } = useWindows()
12
63
 
@@ -51,10 +102,13 @@ const DesktopLayout = ({ children, className = '', ...props }) => {
51
102
  console.log('Desktop context menu at:', e.clientX, e.clientY)
52
103
  }
53
104
 
105
+ // Generate theme class name
106
+ const themeClass = `desktop--${theme}`
107
+
54
108
  return (
55
109
  <div
56
110
  ref={desktopRef}
57
- className={`desktop ${className}`}
111
+ className={`desktop ${themeClass} ${className}`}
58
112
  onContextMenu={handleContextMenu}
59
113
  {...props}
60
114
  >
@@ -112,6 +166,8 @@ export const DesktopTaskbar = () => {
112
166
  closeWindow
113
167
  } = useWindows()
114
168
 
169
+ const { open: openApplicationMenu } = useApplicationMenu()
170
+
115
171
  const handleCreateWindow = () => {
116
172
  const windowTypes = [
117
173
  { title: 'File Explorer', icon: '📁', size: { width: 600, height: 400 } },
@@ -171,34 +227,57 @@ export const DesktopTaskbar = () => {
171
227
 
172
228
  return (
173
229
  <div style={{
174
- position: 'absolute',
175
- bottom: 0,
176
- left: 0,
177
- right: 0,
178
- height: '50px',
179
- background: 'rgba(0,0,0,0.8)',
180
- display: 'flex',
181
- alignItems: 'center',
182
- padding: '0 16px',
183
- gap: '8px'
184
- }}>
185
- {/* Create window button */}
186
- <button
187
- onClick={handleCreateWindow}
188
- style={{
189
- padding: '8px 12px',
190
- background: '#1976d2',
191
- color: 'white',
192
- border: 'none',
193
- borderRadius: '4px',
194
- cursor: 'pointer',
195
- fontSize: '12px',
196
- flexShrink: 0
197
- }}
198
- title="Create new window"
199
- >
200
- +
201
- </button>
230
+ position: 'absolute',
231
+ bottom: 0,
232
+ left: 0,
233
+ right: 0,
234
+ height: '50px',
235
+ background: 'rgba(0,0,0,0.8)',
236
+ display: 'flex',
237
+ alignItems: 'center',
238
+ padding: '0 16px',
239
+ gap: '8px'
240
+ }}>
241
+ {/* Start button */}
242
+ <button
243
+ onClick={openApplicationMenu}
244
+ style={{
245
+ padding: '8px 16px',
246
+ background: '#1976d2',
247
+ color: 'white',
248
+ border: 'none',
249
+ borderRadius: '4px',
250
+ cursor: 'pointer',
251
+ fontSize: '14px',
252
+ fontWeight: 'bold',
253
+ flexShrink: 0,
254
+ display: 'flex',
255
+ alignItems: 'center',
256
+ gap: '6px'
257
+ }}
258
+ title="Open application menu"
259
+ >
260
+ <span style={{ fontSize: '16px' }}>🚀</span>
261
+ Start
262
+ </button>
263
+
264
+ {/* Create window button (for testing) */}
265
+ <button
266
+ onClick={handleCreateWindow}
267
+ style={{
268
+ padding: '8px 12px',
269
+ background: '#666',
270
+ color: 'white',
271
+ border: 'none',
272
+ borderRadius: '4px',
273
+ cursor: 'pointer',
274
+ fontSize: '12px',
275
+ flexShrink: 0
276
+ }}
277
+ title="Create random window (for testing)"
278
+ >
279
+ +
280
+ </button>
202
281
 
203
282
  {/* Separator */}
204
283
  <div style={{
@@ -292,9 +371,11 @@ export const DesktopTaskbar = () => {
292
371
  }
293
372
 
294
373
  /**
295
- * Main Desktop component with WindowProvider
374
+ * Internal Desktop component that uses the contexts
296
375
  */
297
- export const Desktop = ({ desktopSize, children, ...props }) => {
376
+ const DesktopInternal = ({ desktopSize, children, ...props }) => {
377
+ const { isOpen, close } = useApplicationMenu()
378
+
298
379
  return (
299
380
  <WindowProvider desktopSize={desktopSize}>
300
381
  <DesktopLayout {...props}>
@@ -302,7 +383,24 @@ export const Desktop = ({ desktopSize, children, ...props }) => {
302
383
  {children}
303
384
  </Workspace>
304
385
  <DesktopTaskbar />
386
+ <ApplicationMenu
387
+ isOpen={isOpen}
388
+ onClose={close}
389
+ />
305
390
  </DesktopLayout>
306
391
  </WindowProvider>
307
392
  )
308
393
  }
394
+
395
+ /**
396
+ * Main Desktop component with AppProvider wrapper
397
+ */
398
+ export const Desktop = ({ desktopSize, children, ...props }) => {
399
+ return (
400
+ <AppProvider>
401
+ <DesktopInternal desktopSize={desktopSize} {...props}>
402
+ {children}
403
+ </DesktopInternal>
404
+ </AppProvider>
405
+ )
406
+ }
@@ -1,6 +1,10 @@
1
1
  // Desktop Components - Window management system for web applications
2
- export { Desktop, Workspace, DesktopTaskbar } from './desktop'
2
+ export { Desktop, Workspace, DesktopTaskbar, AppProvider, useApplicationMenu, useAppManager } from './desktop'
3
3
  export { Window } from './window'
4
+ export { ApplicationMenu } from './ApplicationMenu'
5
+
6
+ // Application Management
7
+ export { AppManager, defaultAppManager } from './AppManager'
4
8
 
5
9
  // Window Management
6
10
  export { WindowManager } from './WindowManager'
@@ -25,14 +25,21 @@ export const Window = ({
25
25
  const headerRef = useRef(null)
26
26
 
27
27
  // Get window data from WindowManager
28
- const { getWindow, updateWindowPosition, closeWindow, minimizeWindow, maximizeWindow, focusWindow } = useWindows()
28
+ const { getWindow, updateWindowPosition, updateWindowSize, closeWindow, minimizeWindow, maximizeWindow, focusWindow } = useWindows()
29
29
  const windowData = getWindow(id)
30
-
30
+
31
31
  // Local state for dragging
32
32
  const [isDragging, setIsDragging] = useState(false)
33
33
  const [dragOffset, setDragOffset] = useState({ x: 0, y: 0 })
34
34
  const [dragStartPosition, setDragStartPosition] = useState({ x: 0, y: 0 })
35
35
 
36
+ // Local state for resizing
37
+ const [isResizing, setIsResizing] = useState(false)
38
+ const [resizeDirection, setResizeDirection] = useState('')
39
+ const [resizeStartSize, setResizeStartSize] = useState({ width: 0, height: 0 })
40
+ const [resizeStartPosition, setResizeStartPosition] = useState({ x: 0, y: 0 })
41
+ const [resizeStartMouse, setResizeStartMouse] = useState({ x: 0, y: 0 })
42
+
36
43
  // If window doesn't exist in WindowManager, don't render
37
44
  if (!windowData) {
38
45
  return null
@@ -0,0 +1,361 @@
1
+ import React from 'react'
2
+ import { Desktop, AppProvider, AppManager, defaultAppManager, useApplicationMenu, useAppManager } from '../desktop'
3
+
4
+ // Example custom components for applications
5
+ const TextEditorComponent = () => (
6
+ <div style={{ padding: '20px', height: '100%', display: 'flex', flexDirection: 'column' }}>
7
+ <div style={{ marginBottom: '10px', display: 'flex', gap: '10px' }}>
8
+ <button style={{ padding: '5px 10px', fontSize: '12px' }}>New</button>
9
+ <button style={{ padding: '5px 10px', fontSize: '12px' }}>Open</button>
10
+ <button style={{ padding: '5px 10px', fontSize: '12px' }}>Save</button>
11
+ </div>
12
+ <textarea
13
+ style={{
14
+ flex: 1,
15
+ border: '1px solid #ccc',
16
+ padding: '10px',
17
+ fontFamily: 'monospace',
18
+ fontSize: '14px',
19
+ resize: 'none'
20
+ }}
21
+ placeholder="Start typing your text here..."
22
+ defaultValue="Welcome to the Text Editor!\n\nThis is a simple text editor application launched from the Application Menu."
23
+ />
24
+ </div>
25
+ )
26
+
27
+ const CalculatorComponent = () => {
28
+ const [display, setDisplay] = React.useState('0')
29
+ const [previousValue, setPreviousValue] = React.useState(null)
30
+ const [operation, setOperation] = React.useState(null)
31
+ const [waitingForOperand, setWaitingForOperand] = React.useState(false)
32
+
33
+ const inputNumber = (num) => {
34
+ if (waitingForOperand) {
35
+ setDisplay(String(num))
36
+ setWaitingForOperand(false)
37
+ } else {
38
+ setDisplay(display === '0' ? String(num) : display + num)
39
+ }
40
+ }
41
+
42
+ const inputOperation = (nextOperation) => {
43
+ const inputValue = parseFloat(display)
44
+
45
+ if (previousValue === null) {
46
+ setPreviousValue(inputValue)
47
+ } else if (operation) {
48
+ const currentValue = previousValue || 0
49
+ const newValue = calculate(currentValue, inputValue, operation)
50
+
51
+ setDisplay(String(newValue))
52
+ setPreviousValue(newValue)
53
+ }
54
+
55
+ setWaitingForOperand(true)
56
+ setOperation(nextOperation)
57
+ }
58
+
59
+ const calculate = (firstValue, secondValue, operation) => {
60
+ switch (operation) {
61
+ case '+': return firstValue + secondValue
62
+ case '-': return firstValue - secondValue
63
+ case '×': return firstValue * secondValue
64
+ case '÷': return firstValue / secondValue
65
+ case '=': return secondValue
66
+ default: return secondValue
67
+ }
68
+ }
69
+
70
+ const performCalculation = () => {
71
+ const inputValue = parseFloat(display)
72
+
73
+ if (previousValue !== null && operation) {
74
+ const newValue = calculate(previousValue, inputValue, operation)
75
+ setDisplay(String(newValue))
76
+ setPreviousValue(null)
77
+ setOperation(null)
78
+ setWaitingForOperand(true)
79
+ }
80
+ }
81
+
82
+ const clear = () => {
83
+ setDisplay('0')
84
+ setPreviousValue(null)
85
+ setOperation(null)
86
+ setWaitingForOperand(false)
87
+ }
88
+
89
+ const buttonStyle = {
90
+ padding: '15px',
91
+ margin: '2px',
92
+ border: 'none',
93
+ borderRadius: '4px',
94
+ cursor: 'pointer',
95
+ fontSize: '16px',
96
+ fontWeight: 'bold'
97
+ }
98
+
99
+ return (
100
+ <div style={{ padding: '20px', maxWidth: '250px' }}>
101
+ <div style={{
102
+ background: '#000',
103
+ color: '#fff',
104
+ padding: '15px',
105
+ textAlign: 'right',
106
+ fontSize: '24px',
107
+ marginBottom: '10px',
108
+ borderRadius: '4px',
109
+ minHeight: '40px',
110
+ display: 'flex',
111
+ alignItems: 'center',
112
+ justifyContent: 'flex-end'
113
+ }}>
114
+ {display}
115
+ </div>
116
+ <div style={{ display: 'grid', gridTemplateColumns: 'repeat(4, 1fr)', gap: '2px' }}>
117
+ <button style={{...buttonStyle, background: '#f0f0f0'}} onClick={clear}>C</button>
118
+ <button style={{...buttonStyle, background: '#f0f0f0'}} onClick={() => {}}>±</button>
119
+ <button style={{...buttonStyle, background: '#f0f0f0'}} onClick={() => {}}>%</button>
120
+ <button style={{...buttonStyle, background: '#ff9500', color: 'white'}} onClick={() => inputOperation('÷')}>÷</button>
121
+
122
+ <button style={{...buttonStyle, background: '#333', color: 'white'}} onClick={() => inputNumber(7)}>7</button>
123
+ <button style={{...buttonStyle, background: '#333', color: 'white'}} onClick={() => inputNumber(8)}>8</button>
124
+ <button style={{...buttonStyle, background: '#333', color: 'white'}} onClick={() => inputNumber(9)}>9</button>
125
+ <button style={{...buttonStyle, background: '#ff9500', color: 'white'}} onClick={() => inputOperation('×')}>×</button>
126
+
127
+ <button style={{...buttonStyle, background: '#333', color: 'white'}} onClick={() => inputNumber(4)}>4</button>
128
+ <button style={{...buttonStyle, background: '#333', color: 'white'}} onClick={() => inputNumber(5)}>5</button>
129
+ <button style={{...buttonStyle, background: '#333', color: 'white'}} onClick={() => inputNumber(6)}>6</button>
130
+ <button style={{...buttonStyle, background: '#ff9500', color: 'white'}} onClick={() => inputOperation('-')}>-</button>
131
+
132
+ <button style={{...buttonStyle, background: '#333', color: 'white'}} onClick={() => inputNumber(1)}>1</button>
133
+ <button style={{...buttonStyle, background: '#333', color: 'white'}} onClick={() => inputNumber(2)}>2</button>
134
+ <button style={{...buttonStyle, background: '#333', color: 'white'}} onClick={() => inputNumber(3)}>3</button>
135
+ <button style={{...buttonStyle, background: '#ff9500', color: 'white'}} onClick={() => inputOperation('+')}>+</button>
136
+
137
+ <button style={{...buttonStyle, background: '#333', color: 'white', gridColumn: 'span 2'}} onClick={() => inputNumber(0)}>0</button>
138
+ <button style={{...buttonStyle, background: '#333', color: 'white'}} onClick={() => {}}>.</button>
139
+ <button style={{...buttonStyle, background: '#ff9500', color: 'white'}} onClick={performCalculation}>=</button>
140
+ </div>
141
+ </div>
142
+ )
143
+ }
144
+
145
+ // Register custom applications with components
146
+ const setupCustomApps = () => {
147
+ // Register Text Editor with actual component
148
+ defaultAppManager.registerApp({
149
+ id: 'text-editor-custom',
150
+ name: 'Advanced Text Editor',
151
+ description: 'Full-featured text editor with toolbar',
152
+ icon: '📝',
153
+ category: 'Productivity',
154
+ component: <TextEditorComponent />,
155
+ size: { width: 600, height: 400 }
156
+ })
157
+
158
+ // Register Calculator with actual component
159
+ defaultAppManager.registerApp({
160
+ id: 'calculator-custom',
161
+ name: 'Calculator Pro',
162
+ description: 'Advanced calculator with full functionality',
163
+ icon: '🧮',
164
+ category: 'Utilities',
165
+ component: <CalculatorComponent />,
166
+ size: { width: 300, height: 450 }
167
+ })
168
+
169
+ // Register a custom dashboard app
170
+ defaultAppManager.registerApp({
171
+ id: 'dashboard',
172
+ name: 'Dashboard',
173
+ description: 'System monitoring dashboard',
174
+ icon: '📊',
175
+ category: 'System',
176
+ component: (
177
+ <div style={{ padding: '20px' }}>
178
+ <h2>System Dashboard</h2>
179
+ <div style={{ display: 'grid', gridTemplateColumns: '1fr 1fr', gap: '20px', marginTop: '20px' }}>
180
+ <div style={{ padding: '15px', background: '#e3f2fd', borderRadius: '8px' }}>
181
+ <h4>CPU Usage</h4>
182
+ <div style={{ fontSize: '24px', fontWeight: 'bold', color: '#1976d2' }}>45%</div>
183
+ </div>
184
+ <div style={{ padding: '15px', background: '#e8f5e8', borderRadius: '8px' }}>
185
+ <h4>Memory</h4>
186
+ <div style={{ fontSize: '24px', fontWeight: 'bold', color: '#388e3c' }}>2.1GB</div>
187
+ </div>
188
+ <div style={{ padding: '15px', background: '#fff3e0', borderRadius: '8px' }}>
189
+ <h4>Disk Space</h4>
190
+ <div style={{ fontSize: '24px', fontWeight: 'bold', color: '#f57c00' }}>78%</div>
191
+ </div>
192
+ <div style={{ padding: '15px', background: '#fce4ec', borderRadius: '8px' }}>
193
+ <h4>Network</h4>
194
+ <div style={{ fontSize: '24px', fontWeight: 'bold', color: '#c2185b' }}>125 MB/s</div>
195
+ </div>
196
+ </div>
197
+ </div>
198
+ ),
199
+ size: { width: 500, height: 400 }
200
+ })
201
+ }
202
+
203
+ // Component that demonstrates using both AppProvider hooks
204
+ const DemoControls = () => {
205
+ const { isOpen, open, close, toggle } = useApplicationMenu()
206
+ const appManager = useAppManager()
207
+ const [appCount, setAppCount] = React.useState(0)
208
+
209
+ React.useEffect(() => {
210
+ const updateCount = () => setAppCount(appManager.getAllApps().length)
211
+ updateCount()
212
+ appManager.addListener(updateCount)
213
+ return () => appManager.removeListener(updateCount)
214
+ }, [appManager])
215
+
216
+ const addCustomApp = () => {
217
+ const randomId = `custom-app-${Date.now()}`
218
+ appManager.registerApp({
219
+ id: randomId,
220
+ name: `Custom App ${Math.floor(Math.random() * 100)}`,
221
+ description: 'Dynamically added application',
222
+ icon: ['🎨', '🎯', '🎪', '🎭', '🎨'][Math.floor(Math.random() * 5)],
223
+ category: 'Custom',
224
+ component: (
225
+ <div style={{ padding: '20px', textAlign: 'center' }}>
226
+ <h2>Dynamic App</h2>
227
+ <p>This app was added dynamically using AppManager!</p>
228
+ <p>ID: {randomId}</p>
229
+ </div>
230
+ ),
231
+ size: { width: 400, height: 300 }
232
+ })
233
+ }
234
+
235
+ return (
236
+ <div style={{
237
+ position: 'absolute',
238
+ top: '20px',
239
+ right: '20px',
240
+ background: 'rgba(255,255,255,0.9)',
241
+ padding: '20px',
242
+ borderRadius: '8px',
243
+ boxShadow: '0 2px 10px rgba(0,0,0,0.1)',
244
+ maxWidth: '300px'
245
+ }}>
246
+ <h4 style={{ margin: '0 0 15px 0' }}>🎮 App Controls</h4>
247
+ <p style={{ margin: '0 0 10px 0', fontSize: '14px', color: '#666' }}>
248
+ Menu: <strong>{isOpen ? 'Open' : 'Closed'}</strong><br/>
249
+ Apps: <strong>{appCount}</strong>
250
+ </p>
251
+
252
+ <div style={{ display: 'flex', flexDirection: 'column', gap: '8px' }}>
253
+ <button
254
+ onClick={open}
255
+ style={{
256
+ padding: '8px 12px',
257
+ background: '#4caf50',
258
+ color: 'white',
259
+ border: 'none',
260
+ borderRadius: '4px',
261
+ cursor: 'pointer',
262
+ fontSize: '12px'
263
+ }}
264
+ >
265
+ Open Menu
266
+ </button>
267
+ <button
268
+ onClick={close}
269
+ style={{
270
+ padding: '8px 12px',
271
+ background: '#f44336',
272
+ color: 'white',
273
+ border: 'none',
274
+ borderRadius: '4px',
275
+ cursor: 'pointer',
276
+ fontSize: '12px'
277
+ }}
278
+ >
279
+ Close Menu
280
+ </button>
281
+ <button
282
+ onClick={toggle}
283
+ style={{
284
+ padding: '8px 12px',
285
+ background: '#2196f3',
286
+ color: 'white',
287
+ border: 'none',
288
+ borderRadius: '4px',
289
+ cursor: 'pointer',
290
+ fontSize: '12px'
291
+ }}
292
+ >
293
+ Toggle Menu
294
+ </button>
295
+ <hr style={{ margin: '10px 0', border: 'none', borderTop: '1px solid #eee' }} />
296
+ <button
297
+ onClick={addCustomApp}
298
+ style={{
299
+ padding: '8px 12px',
300
+ background: '#ff9800',
301
+ color: 'white',
302
+ border: 'none',
303
+ borderRadius: '4px',
304
+ cursor: 'pointer',
305
+ fontSize: '12px'
306
+ }}
307
+ >
308
+ Add Custom App
309
+ </button>
310
+ </div>
311
+ </div>
312
+ )
313
+ }
314
+
315
+ /**
316
+ * Example component demonstrating the ApplicationMenu system
317
+ */
318
+ export const ApplicationMenuExample = () => {
319
+ React.useEffect(() => {
320
+ setupCustomApps()
321
+ }, [])
322
+
323
+ return (
324
+ <div style={{ height: '100vh', position: 'relative', backgroundColor: '#f0f0f0' }}>
325
+ <Desktop desktopSize={{ width: 1200, height: 800 }}>
326
+ {/* Desktop background content */}
327
+ <div style={{
328
+ position: 'absolute',
329
+ top: '20px',
330
+ left: '20px',
331
+ background: 'rgba(255,255,255,0.9)',
332
+ padding: '20px',
333
+ borderRadius: '8px',
334
+ boxShadow: '0 2px 10px rgba(0,0,0,0.1)',
335
+ maxWidth: '400px'
336
+ }}>
337
+ <h3 style={{ margin: '0 0 15px 0' }}>🚀 AppProvider + AppManager Demo</h3>
338
+ <p style={{ margin: '0 0 15px 0', lineHeight: '1.5' }}>
339
+ AppProvider and AppManager now collaborate seamlessly. The ApplicationMenu gets apps
340
+ from AppManager through the context. Try adding custom apps dynamically!
341
+ </p>
342
+ <div style={{ fontSize: '14px', color: '#666' }}>
343
+ <p><strong>Collaboration Features:</strong></p>
344
+ <ul style={{ margin: '5px 0', paddingLeft: '20px' }}>
345
+ <li>AppProvider provides AppManager via context</li>
346
+ <li>ApplicationMenu uses useAppManager() hook</li>
347
+ <li>Dynamic app registration and updates</li>
348
+ <li>Real-time app count updates</li>
349
+ <li>Clean separation of concerns</li>
350
+ </ul>
351
+ </div>
352
+ </div>
353
+
354
+ {/* Demo controls using the hook */}
355
+ <DemoControls />
356
+ </Desktop>
357
+ </div>
358
+ )
359
+ }
360
+
361
+ export default ApplicationMenuExample