'use client' import React, { useState, useEffect } from 'react' import { Dock } from './Dock' import { TopBar } from './TopBar' import { FileManager } from './FileManager' import { Calendar } from './Calendar' import { DraggableDesktopIcon } from './DraggableDesktopIcon' import { Messages } from './Messages' import { HelpModal } from './HelpModal' import { DesktopContextMenu } from './DesktopContextMenu' import { BackgroundSelector } from './BackgroundSelector' import { GeminiChat } from './GeminiChat' import { Clock } from './Clock' import { SpotlightSearch } from './SpotlightSearch' import { ContextMenu } from './ContextMenu' import { AboutModal } from './AboutModal' import { FlutterRunner } from './FlutterRunner' import { QuizApp } from './QuizApp' import { TextEditor } from './TextEditor' import { VoiceApp } from './VoiceApp' import { motion, AnimatePresence } from 'framer-motion' import { SystemPowerOverlay } from './SystemPowerOverlay' import { Folder, Calendar as CalendarIcon, Clock as ClockIcon, Globe, Sparkle, Terminal as TerminalIcon, Key, Brain, Code, FileText, DeviceMobile, Lightning, Function, ChatCircleDots, MusicNote } from '@phosphor-icons/react' export function Desktop() { const [fileManagerOpen, setFileManagerOpen] = useState(false) const [calendarOpen, setCalendarOpen] = useState(false) const [clockOpen, setClockOpen] = useState(false) const [messagesOpen, setMessagesOpen] = useState(false) const [geminiChatOpen, setGeminiChatOpen] = useState(false) const [spotlightOpen, setSpotlightOpen] = useState(false) const [contextMenuVisible, setContextMenuVisible] = useState(false) const [contextMenuPosition, setContextMenuPosition] = useState({ x: 0, y: 0 }) const [currentPath, setCurrentPath] = useState('') const [helpModalOpen, setHelpModalOpen] = useState(false) const [contextMenuOpen, setContextMenuOpen] = useState(false) const [contextMenuPos, setContextMenuPos] = useState({ x: 0, y: 0 }) const [backgroundSelectorOpen, setBackgroundSelectorOpen] = useState(false) const [currentBackground, setCurrentBackground] = useState('https://images.unsplash.com/photo-1545159639-3f3534aa074e') const [aboutModalOpen, setAboutModalOpen] = useState(false) const [flutterRunnerOpen, setFlutterRunnerOpen] = useState(false) const [activeFlutterApp, setActiveFlutterApp] = useState(null) const [flutterCodeEditorOpen, setFlutterCodeEditorOpen] = useState(false) const [quizAppOpen, setQuizAppOpen] = useState(false) const [textEditorOpen, setTextEditorOpen] = useState(false) const [activeTextFile, setActiveTextFile] = useState<{ content: string, fileName: string, filePath: string, passkey: string } | null>(null) const [voiceAppOpen, setVoiceAppOpen] = useState(false) // Minimized states const [fileManagerMinimized, setFileManagerMinimized] = useState(false) const [calendarMinimized, setCalendarMinimized] = useState(false) const [clockMinimized, setClockMinimized] = useState(false) const [messagesMinimized, setMessagesMinimized] = useState(false) const [geminiChatMinimized, setGeminiChatMinimized] = useState(false) const [flutterRunnerMinimized, setFlutterRunnerMinimized] = useState(false) const [flutterCodeEditorMinimized, setFlutterCodeEditorMinimized] = useState(false) const [quizAppMinimized, setQuizAppMinimized] = useState(false) const [textEditorMinimized, setTextEditorMinimized] = useState(false) const [voiceAppMinimized, setVoiceAppMinimized] = useState(false) const [powerState, setPowerState] = useState<'active' | 'sleep' | 'restart' | 'shutdown'>('active') const [globalZIndex, setGlobalZIndex] = useState(1000) const [windowZIndices, setWindowZIndices] = useState<{ [key: string]: number }>({}) const getNextZIndex = () => { const nextZ = globalZIndex + 1 setGlobalZIndex(nextZ) return nextZ } const bringWindowToFront = (windowId: string) => { setWindowZIndices(prev => ({ ...prev, [windowId]: getNextZIndex() })) } const closeAllApps = () => { // Close all apps setFileManagerOpen(false) setCalendarOpen(false) setClockOpen(false) setMessagesOpen(false) setGeminiChatOpen(false) setFlutterRunnerOpen(false) setFlutterCodeEditorOpen(false) setQuizAppOpen(false) setTextEditorOpen(false) setVoiceAppOpen(false) // Reset all minimized states setFileManagerMinimized(false) setCalendarMinimized(false) setClockMinimized(false) setMessagesMinimized(false) setGeminiChatMinimized(false) setFlutterRunnerMinimized(false) setFlutterCodeEditorMinimized(false) setQuizAppMinimized(false) setTextEditorMinimized(false) setVoiceAppMinimized(false) // Reset window z-indices setWindowZIndices({}) setGlobalZIndex(1000) } const openFileManager = (path: string) => { console.log('Opening File Manager with path:', path) setCurrentPath(path) setFileManagerOpen(true) setFileManagerMinimized(false) bringWindowToFront('fileManager') } const closeFileManager = () => { setFileManagerOpen(false) setFileManagerMinimized(false) } const openCalendar = () => { console.log('Opening Calendar') setCalendarOpen(true) setCalendarMinimized(false) bringWindowToFront('calendar') } const closeCalendar = () => { setCalendarOpen(false) setCalendarMinimized(false) } const openClock = () => { setClockOpen(true) setClockMinimized(false) bringWindowToFront('clock') } const closeClock = () => { setClockOpen(false) setClockMinimized(false) } const openMessages = () => { setMessagesOpen(true) setMessagesMinimized(false) bringWindowToFront('messages') } const closeMessages = () => { setMessagesOpen(false) setMessagesMinimized(false) } const openGeminiChat = () => { console.log('Opening Gemini Chat') setGeminiChatOpen(true) setGeminiChatMinimized(false) bringWindowToFront('gemini') } const closeGeminiChat = () => { setGeminiChatOpen(false) setGeminiChatMinimized(false) } const openFlutterRunner = (appFile: any) => { setActiveFlutterApp(appFile) setFlutterRunnerOpen(true) setFlutterRunnerMinimized(false) bringWindowToFront('flutterRunner') } const closeFlutterRunner = () => { setFlutterRunnerOpen(false) setFlutterRunnerMinimized(false) setActiveFlutterApp(null) } const openFlutterCodeEditor = () => { // Don't read/remove sessionStorage here - let FlutterRunner handle it // This allows the polling mechanism in FlutterRunner to detect new files setFlutterCodeEditorOpen(true) setFlutterCodeEditorMinimized(false) bringWindowToFront('flutterCodeEditor') } const closeFlutterCodeEditor = () => { setFlutterCodeEditorOpen(false) setFlutterCodeEditorMinimized(false) } const openQuizApp = () => { setQuizAppOpen(true) setQuizAppMinimized(false) bringWindowToFront('quizApp') } const closeQuizApp = () => { setQuizAppOpen(false) setQuizAppMinimized(false) } const openTextEditor = (fileData: { content: string, fileName: string, filePath: string, passkey: string }) => { setActiveTextFile(fileData) setTextEditorOpen(true) setTextEditorMinimized(false) bringWindowToFront('textEditor') } const closeTextEditor = () => { setTextEditorOpen(false) setTextEditorMinimized(false) setActiveTextFile(null) } const openVoiceApp = () => { setVoiceAppOpen(true) setVoiceAppMinimized(false) bringWindowToFront('voiceApp') } const closeVoiceApp = () => { setVoiceAppOpen(false) setVoiceAppMinimized(false) } const handleOpenApp = (appId: string) => { switch (appId) { case 'files': openFileManager('') break case 'calendar': openCalendar() break case 'clock': openClock() break case 'messages': openMessages() break case 'gemini': openGeminiChat() break case 'flutter-editor': openFlutterCodeEditor() break case 'quiz': openQuizApp() break case 'voice-app': openVoiceApp() break } } const handleContextMenuAction = (action: string) => { switch (action) { case 'change-wallpaper': setBackgroundSelectorOpen(true) break case 'get-info': setAboutModalOpen(true) break case 'refresh': window.location.reload() break } } const openHelpModal = () => { setHelpModalOpen(true) } const closeHelpModal = () => { setHelpModalOpen(false) } const handleDesktopRightClick = (e: React.MouseEvent) => { e.preventDefault() setContextMenuPosition({ x: e.clientX, y: e.clientY }) setContextMenuVisible(true) } const handleChangeBackground = (type: 'upload' | 'preset') => { setContextMenuOpen(false) setBackgroundSelectorOpen(true) } const handleSelectBackground = (background: string | File) => { if (typeof background === 'string') { // Handle preset backgrounds if (background.startsWith('gradient-')) { setCurrentBackground(background) } else { setCurrentBackground(background) } } else { // Handle uploaded file const url = URL.createObjectURL(background) setCurrentBackground(url) } } const handleSleep = () => setPowerState('sleep') const handleRestart = () => setPowerState('restart') const handleShutdown = () => setPowerState('shutdown') const handleWake = () => setPowerState('active') // Keyboard shortcuts useEffect(() => { const handleKeyDown = (e: KeyboardEvent) => { // Cmd/Ctrl + Space for Spotlight Search if ((e.metaKey || e.ctrlKey) && e.key === ' ') { e.preventDefault() setSpotlightOpen(true) } // Cmd/Ctrl + K for Spotlight Search (alternative) if ((e.metaKey || e.ctrlKey) && e.key === 'k') { e.preventDefault() setSpotlightOpen(true) } } window.addEventListener('keydown', handleKeyDown) return () => window.removeEventListener('keydown', handleKeyDown) }, []) const getBackgroundStyle = () => { if (currentBackground.startsWith('gradient-')) { const gradients: Record = { 'gradient-sonoma': 'linear-gradient(135deg, #e0c3fc 0%, #8ec5fc 100%)', 'gradient-purple': 'linear-gradient(135deg, #77216F 0%, #5E2750 50%, #2C001E 100%)', 'gradient-blue': 'linear-gradient(135deg, #1e3c72 0%, #2a5298 50%, #7e8ba3 100%)', 'gradient-green': 'linear-gradient(135deg, #134e5e 0%, #71b280 50%, #a8e063 100%)', 'gradient-orange': 'linear-gradient(135deg, #ff512f 0%, #dd2476 50%, #f09819 100%)', 'gradient-dark': 'linear-gradient(135deg, #0f0f0f 0%, #1a1a1a 50%, #2d2d2d 100%)', 'gradient-cosmic': 'linear-gradient(135deg, #667eea 0%, #764ba2 50%, #f093fb 100%)' } return { background: gradients[currentBackground] || gradients['gradient-sonoma'] } } else { return { backgroundImage: `url('${currentBackground}')`, backgroundSize: 'cover', backgroundPosition: 'center', backgroundRepeat: 'no-repeat' } } } // Build minimized apps list for dock const minimizedApps = [] if (fileManagerMinimized && fileManagerOpen) { minimizedApps.push({ id: 'files', label: 'Files', icon: (
), onRestore: () => { setFileManagerMinimized(false) bringWindowToFront('fileManager') } }) } if (calendarMinimized && calendarOpen) { minimizedApps.push({ id: 'calendar', label: 'Calendar', icon: (
), onRestore: () => { setCalendarMinimized(false) bringWindowToFront('calendar') } }) } if (clockMinimized && clockOpen) { minimizedApps.push({ id: 'clock', label: 'Clock', icon: (
), onRestore: () => { setClockMinimized(false) bringWindowToFront('clock') } }) } if (messagesMinimized && messagesOpen) { minimizedApps.push({ id: 'messages', label: 'Messages', icon: (
), onRestore: () => { setMessagesMinimized(false) bringWindowToFront('messages') } }) } if (geminiChatMinimized && geminiChatOpen) { minimizedApps.push({ id: 'gemini', label: 'Gemini', icon: (
), onRestore: () => { setGeminiChatMinimized(false) bringWindowToFront('gemini') } }) } if (flutterRunnerMinimized && flutterRunnerOpen) { minimizedApps.push({ id: 'flutter-runner', label: 'Flutter Runner', icon: (
), onRestore: () => { setFlutterRunnerMinimized(false) bringWindowToFront('flutterRunner') } }) } if (flutterCodeEditorMinimized && flutterCodeEditorOpen) { minimizedApps.push({ id: 'flutter-editor', label: 'Flutter IDE', icon: (
), onRestore: () => { setFlutterCodeEditorMinimized(false) bringWindowToFront('flutterCodeEditor') } }) } if (quizAppMinimized && quizAppOpen) { minimizedApps.push({ id: 'quiz', label: 'Quiz Master', icon: (
), onRestore: () => { setQuizAppMinimized(false) bringWindowToFront('quizApp') } }) } if (textEditorMinimized && textEditorOpen) { minimizedApps.push({ id: 'text-editor', label: activeTextFile?.fileName || 'Text Editor', icon: (
), onRestore: () => { setTextEditorMinimized(false) bringWindowToFront('textEditor') } }) } if (voiceAppMinimized && voiceAppOpen) { minimizedApps.push({ id: 'voice-app', label: 'Voice Studio', icon: (
), onRestore: () => { setVoiceAppMinimized(false) bringWindowToFront('voiceApp') } }) } // Debug info useEffect(() => { console.log('App States:', { fileManagerOpen, calendarOpen, clockOpen, messagesOpen, geminiChatOpen, fileManagerMinimized, calendarMinimized, clockMinimized, messagesMinimized, geminiChatMinimized }) }, [fileManagerOpen, calendarOpen, clockOpen, messagesOpen, geminiChatOpen, fileManagerMinimized, calendarMinimized, clockMinimized, messagesMinimized, geminiChatMinimized]) return (
{/* Overlay for better text visibility */} {!currentBackground.startsWith('gradient-') && (
)}
setAboutModalOpen(true)} onSleep={handleSleep} onRestart={handleRestart} onShutdown={handleShutdown} />
{/* Desktop Icons - Responsive grid layout, visible on all screen sizes */}
{ }} onDoubleClick={() => openFileManager('')} />
{ }} onDoubleClick={openMessages} />
{ }} onDoubleClick={openGeminiChat} />
{ }} onDoubleClick={openClock} />
{ }} onDoubleClick={openCalendar} />
{ }} onDoubleClick={() => openFileManager('')} />
{ }} onDoubleClick={openFlutterCodeEditor} />
{ }} onDoubleClick={openQuizApp} />
{ }} onDoubleClick={openVoiceApp} />
{/* Windows Container - adjusted for mobile topbar height */}
{fileManagerOpen && ( setFileManagerMinimized(true)} onFocus={() => bringWindowToFront('fileManager')} zIndex={windowZIndices.fileManager || 1000} onOpenApp={handleOpenApp} onOpenTextFile={openTextEditor} /> )} {calendarOpen && ( setCalendarMinimized(true)} onFocus={() => bringWindowToFront('calendar')} zIndex={windowZIndices.calendar || 1000} /> )} {clockOpen && ( setClockMinimized(true)} onFocus={() => bringWindowToFront('clock')} zIndex={windowZIndices.clock || 1000} /> )} {messagesOpen && ( setMessagesMinimized(true)} onFocus={() => bringWindowToFront('messages')} zIndex={windowZIndices.messages || 1000} /> )} {geminiChatOpen && ( setGeminiChatMinimized(true)} onFocus={() => bringWindowToFront('gemini')} zIndex={windowZIndices.gemini || 1000} /> )} {flutterRunnerOpen && activeFlutterApp && ( { setFlutterRunnerOpen(false) setActiveFlutterApp(null) }} onMinimize={() => setFlutterRunnerMinimized(true)} onFocus={() => bringWindowToFront('flutterRunner')} zIndex={windowZIndices.flutterRunner || 1000} /> )} {flutterCodeEditorOpen && ( setFlutterCodeEditorMinimized(true)} onFocus={() => bringWindowToFront('flutterCodeEditor')} zIndex={windowZIndices.flutterCodeEditor || 1000} /> )} {quizAppOpen && ( setQuizAppMinimized(true)} onFocus={() => bringWindowToFront('quizApp')} zIndex={windowZIndices.quizApp || 1000} /> )} {textEditorOpen && activeTextFile && ( setTextEditorMinimized(true)} onFocus={() => bringWindowToFront('textEditor')} zIndex={windowZIndices.textEditor || 1000} /> )} {voiceAppOpen && ( setVoiceAppMinimized(true)} onFocus={() => bringWindowToFront('voiceApp')} zIndex={windowZIndices.voiceApp || 1000} /> )}
{/* Spotlight Search */} setSpotlightOpen(false)} onOpenApp={handleOpenApp} /> {/* Context Menu */} setContextMenuVisible(false)} onAction={handleContextMenuAction} /> {/* Help Modal */} {/* Desktop Context Menu */} setContextMenuOpen(false)} onChangeBackground={handleChangeBackground} /> {/* Background Selector Modal */} setBackgroundSelectorOpen(false)} onSelectBackground={handleSelectBackground} currentBackground={currentBackground} /> {/* System Power Overlay */} {/* About Modal */} setAboutModalOpen(false)} />
) }