File size: 7,215 Bytes
16c7afd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
// Window Management System
let highestZ = 100;
let activeWindow = null;
let dragOffset = { x: 0, y: 0 };
let isDragging = false;
let currentDragWindow = null;

// Initialize
document.addEventListener('DOMContentLoaded', () => {
    initializeWindows();
    startClock();
    addMatrixEffect();
});

// Clock
function startClock() {
    const clock = document.getElementById('clock');
    setInterval(() => {
        const now = new Date();
        clock.textContent = now.toLocaleTimeString('en-US', { 
            hour12: false,
            hour: '2-digit',
            minute: '2-digit',
            second: '2-digit'
        });
    }, 1000);
}

// Window Dragging Logic
function initializeWindows() {
    const windows = document.querySelectorAll('.window');
    
    windows.forEach(window => {
        const header = window.querySelector('.window-header');
        
        header.addEventListener('mousedown', (e) => startDrag(e, window));
        header.addEventListener('touchstart', (e) => startDrag(e, window), { passive: false });
        
        // Bring to front on click
        window.addEventListener('mousedown', () => bringToFront(window));
    });

    document.addEventListener('mousemove', drag);
    document.addEventListener('touchmove', drag, { passive: false });
    document.addEventListener('mouseup', stopDrag);
    document.addEventListener('touchend', stopDrag);
}

function startDrag(e, window) {
    if (window.classList.contains('minimized')) return;
    
    isDragging = true;
    currentDragWindow = window;
    window.classList.add('dragging');
    bringToFront(window);
    
    const clientX = e.type.includes('touch') ? e.touches[0].clientX : e.clientX;
    const clientY = e.type.includes('touch') ? e.touches[0].clientY : e.clientY;
    const rect = window.getBoundingClientRect();
    
    dragOffset.x = clientX - rect.left;
    dragOffset.y = clientY - rect.top;
    
    e.preventDefault();
}

function drag(e) {
    if (!isDragging || !currentDragWindow) return;
    
    const clientX = e.type.includes('touch') ? e.touches[0].clientX : e.clientX;
    const clientY = e.type.includes('touch') ? e.touches[0].clientY : e.clientY;
    
    let newX = clientX - dragOffset.x;
    let newY = clientY - dragOffset.y;
    
    // Boundary checks
    const maxX = window.innerWidth - currentDragWindow.offsetWidth;
    const maxY = window.innerHeight - currentDragWindow.offsetHeight - 48; // Account for taskbar
    
    newX = Math.max(0, Math.min(newX, maxX));
    newY = Math.max(48, Math.min(newY, maxY)); // Account for header
    
    currentDragWindow.style.left = newX + 'px';
    currentDragWindow.style.top = newY + 'px';
    
    e.preventDefault();
}

function stopDrag() {
    if (currentDragWindow) {
        currentDragWindow.classList.remove('dragging');
    }
    isDragging = false;
    currentDragWindow = null;
}

function bringToFront(window) {
    highestZ++;
    window.style.zIndex = highestZ;
    
    // Remove active class from all windows
    document.querySelectorAll('.window').forEach(w => w.classList.remove('active'));
    window.classList.add('active');
}

// Window Controls
function minimizeWindow(windowId) {
    const window = document.querySelector(`[data-window="${windowId}"]`);
    if (window) {
        window.classList.add('minimized');
        updateTaskbarButton(windowId, false);
    }
}

function closeWindow(windowId) {
    const window = document.querySelector(`[data-window="${windowId}"]`);
    if (window) {
        window.style.display = 'none';
        updateTaskbarButton(windowId, false);
        
        // Add closed animation
        window.style.animation = 'none';
        setTimeout(() => {
            window.style.animation = '';
        }, 10);
    }
}

function openWindow(windowId) {
    const window = document.querySelector(`[data-window="${windowId}"]`);
    if (window) {
        window.classList.remove('minimized');
        window.style.display = 'block';
        bringToFront(window);
        updateTaskbarButton(windowId, true);
        
        // Reset animation
        window.style.animation = 'windowOpen 0.3s ease-out';
    }
}

function updateTaskbarButton(windowId, isActive) {
    const buttons = document.querySelectorAll('.taskbar-btn');
    buttons.forEach(btn => {
        if (btn.getAttribute('onclick').includes(windowId)) {
            if (isActive) {
                btn.classList.add('active');
            } else {
                btn.classList.remove('active');
            }
        }
    });
}

// Matrix-like background effect
function addMatrixEffect() {
    const chars = "0123456789ABCDEF";
    const desktop = document.getElementById('desktop');
    
    // Create occasional falling characters
    setInterval(() => {
        if (Math.random() > 0.9) {
            createFallingChar();
        }
    }, 500);
}

function createFallingChar() {
    const char = document.createElement('div');
    char.textContent = Math.random() > 0.5 ? '0' : '1';
    char.style.position = 'absolute';
    char.style.left = Math.random() * 100 + '%';
    char.style.top = '-20px';
    char.style.color = 'rgba(245, 158, 11, 0.2)';
    char.style.fontFamily = 'JetBrains Mono, monospace';
    char.style.fontSize = '14px';
    char.style.pointerEvents = 'none';
    char.style.zIndex = '1';
    char.style.animation = 'fall 3s linear forwards';
    
    document.getElementById('desktop').appendChild(char);
    
    setTimeout(() => char.remove(), 3000);
}

// Add fall animation to CSS dynamically
const style = document.createElement('style');
style.textContent = `
    @keyframes fall {
        to {
            transform: translateY(100vh);
            opacity: 0;
        }
    }
`;
document.head.appendChild(style);

// Keyboard shortcuts
document.addEventListener('keydown', (e) => {
    // Alt + W to close active window
    if (e.altKey && e.key === 'w') {
        const active = document.querySelector('.window.active');
        if (active) {
            const windowId = active.getAttribute('data-window');
            closeWindow(windowId);
        }
    }
    
    // Escape to minimize active window
    if (e.key === 'Escape') {
        const active = document.querySelector('.window.active');
        if (active) {
            const windowId = active.getAttribute('data-window');
            minimizeWindow(windowId);
        }
    }
});

// Prevent text selection when dragging
document.addEventListener('selectstart', (e) => {
    if (isDragging) {
        e.preventDefault();
    }
});

// Double click header to maximize (simplified - just centers and enlarges)
document.querySelectorAll('.window-header').forEach(header => {
    header.addEventListener('dblclick', (e) => {
        const window = header.closest('.window');
        if (window.style.width === '90%') {
            // Restore
            window.style.width = '';
            window.style.height = '';
            window.style.left = '10%';
            window.style.top = '10%';
        } else {
            // Maximize
            window.style.width = '90%';
            window.style.height = '80%';
            window.style.left = '5%';
            window.style.top = '5%';
        }
    });
});