File size: 4,668 Bytes
b4733be | 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 | // DOM Elements
const homeScreen = document.getElementById('home-screen');
const appContainer = document.getElementById('app-container');
const appIcons = document.querySelectorAll('.app-icon, .dock-icon');
const wallpaper = document.getElementById('wallpaper');
// Current State
let currentApp = null;
let apps = {
phone: {
name: 'Phone',
icon: 'phone',
color: 'blue-500',
component: 'phone-app'
},
camera: {
name: 'Camera',
icon: 'camera',
color: 'green-500',
component: 'camera-app'
},
gallery: {
name: 'Gallery',
icon: 'image',
color: 'yellow-500',
component: 'gallery-app'
},
calculator: {
name: 'Calculator',
icon: 'cpu',
color: 'red-500',
component: 'calculator-app'
},
clock: {
name: 'Clock',
icon: 'clock',
color: 'purple-500',
component: 'clock-app'
},
notes: {
name: 'Notes',
icon: 'edit',
color: 'pink-500',
component: 'notes-app'
},
settings: {
name: 'Settings',
icon: 'settings',
color: 'gray-500',
component: 'settings-app'
},
browser: {
name: 'Browser',
icon: 'globe',
color: 'indigo-500',
component: 'browser-app'
}
};
// Initialize the OS
function initOS() {
// Load components
loadComponents();
// Set up event listeners
setupEventListeners();
// Update time every minute
updateTime();
setInterval(updateTime, 60000);
// Simulate battery drain
simulateBattery();
}
// Load all web components
function loadComponents() {
const components = [
'phone-app.js',
'camera-app.js',
'gallery-app.js',
'calculator-app.js',
'clock-app.js',
'notes-app.js',
'settings-app.js',
'browser-app.js'
];
components.forEach(component => {
const script = document.createElement('script');
script.src = `components/${component}`;
document.body.appendChild(script);
});
}
// Set up event listeners
function setupEventListeners() {
// App icon clicks
appIcons.forEach(icon => {
icon.addEventListener('click', (e) => {
const appName = e.currentTarget.getAttribute('data-app');
openApp(appName);
});
});
// Back button (implemented in navbar component)
// Home button (implemented in navbar component)
// Recent apps button (implemented in navbar component)
// Gesture detection for swipe up to home
let startY;
const gestureArea = document.createElement('div');
gestureArea.className = 'gesture-area';
document.querySelector('.w-full.max-w-md').appendChild(gestureArea);
gestureArea.addEventListener('touchstart', (e) => {
startY = e.touches[0].clientY;
});
gestureArea.addEventListener('touchend', (e) => {
const endY = e.changedTouches[0].clientY;
if (startY - endY > 50) { // Swipe up
goHome();
}
});
}
// Open an app
function openApp(appName) {
if (!apps[appName]) return;
currentApp = appName;
homeScreen.classList.add('hidden');
appContainer.classList.remove('hidden');
appContainer.innerHTML = `
<${apps[appName].component} class="app-window active"></${apps[appName].component}>
`;
}
// Close current app
function closeApp() {
const appWindow = document.querySelector('.app-window');
if (appWindow) {
appWindow.classList.remove('active');
setTimeout(() => {
appContainer.classList.add('hidden');
homeScreen.classList.remove('hidden');
appContainer.innerHTML = '';
currentApp = null;
}, 300);
}
}
// Go to home screen
function goHome() {
if (currentApp) {
closeApp();
}
}
// Update time in status bar
function updateTime() {
const now = new Date();
const timeElement = document.querySelector('.time');
if (timeElement) {
timeElement.textContent = now.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' });
}
}
// Simulate battery drain
function simulateBattery() {
const batteryElement = document.querySelector('.battery');
if (batteryElement) {
let battery = 100;
setInterval(() => {
battery = Math.max(5, battery - 1);
batteryElement.textContent = `${battery}%`;
}, 60000);
}
}
// Initialize the OS when DOM is loaded
document.addEventListener('DOMContentLoaded', initOS); |