|
| 1 | +import HTML from '../HTML' |
| 2 | +import { AppClosedEvent, AppOpenedEvent, Process } from '../types' |
| 3 | +import { getTime } from '../utils' |
| 4 | +import nullIcon from '../assets/icons/application-default-icon.svg' |
| 5 | +import { parse } from 'js-ini' |
| 6 | +import { v4 as uuid } from 'uuid' |
| 7 | + |
| 8 | +const BootLoader: Process = { |
| 9 | + config: { |
| 10 | + name: 'Bootloader', |
| 11 | + type: 'process', |
| 12 | + targetVer: '1.0.0-indev.0' |
| 13 | + }, |
| 14 | + run: async (process) => { |
| 15 | + const splashScreen = await process.loadLibrary('lib/SplashScreen') |
| 16 | + const splashElement = splashScreen.getElement() |
| 17 | + splashElement.appendTo(document.body) |
| 18 | + |
| 19 | + const { fs } = process |
| 20 | + const wm = await process.loadLibrary('lib/WindowManager') |
| 21 | + const launcher = await process.loadLibrary('lib/Launcher') |
| 22 | + |
| 23 | + const config = Buffer.from(await fs.readFile('/etc/flow')).toString() |
| 24 | + process.kernel.setConfig(parse(config)) |
| 25 | + |
| 26 | + if ('serviceWorker' in navigator) { |
| 27 | + const registrations = await navigator.serviceWorker.getRegistrations() |
| 28 | + for (const registration of registrations) { |
| 29 | + await registration.unregister() |
| 30 | + } |
| 31 | + |
| 32 | + try { |
| 33 | + await navigator.serviceWorker.register(`/uv-sw.js?url=${encodeURIComponent(btoa(process.kernel.config.SERVER))}&e=${uuid()}`, { |
| 34 | + scope: '/service/' |
| 35 | + }) |
| 36 | + } catch (e) { |
| 37 | + console.error(e) |
| 38 | + } |
| 39 | + } else { |
| 40 | + console.warn('Service workers are not supported.') |
| 41 | + } |
| 42 | + |
| 43 | + const input = new HTML('input').attr({ |
| 44 | + type: 'text', |
| 45 | + placeholder: 'Search' |
| 46 | + }).on('keyup', () => { |
| 47 | + apps.elm.innerHTML = '' |
| 48 | + renderApps().catch(e => console.error(e)) |
| 49 | + }).appendTo(launcher.element) |
| 50 | + const apps = new HTML('apps').appendTo(launcher.element) |
| 51 | + |
| 52 | + const renderApps = async (): Promise<void> => { |
| 53 | + apps.html('') |
| 54 | + const files = await fs.readdir('/home/Applications/') |
| 55 | + files |
| 56 | + .filter((x: string) => x.endsWith('.app') && ((input.elm as HTMLInputElement) !== null ? x.toLowerCase().includes((input.elm as HTMLInputElement).value.toLowerCase()) : true)) |
| 57 | + .forEach((file: string) => { |
| 58 | + fs.readFile(`/home/Applications/${file}`).then(async (data: Uint8Array) => { |
| 59 | + const path = Buffer.from(data).toString() |
| 60 | + const executable = await process.kernel.getExecutable(path) as Process |
| 61 | + |
| 62 | + const appElement = new HTML('app').on('click', () => { |
| 63 | + process.launch(path).catch((e: any) => console.error(e)) |
| 64 | + launcher.toggle() |
| 65 | + }).appendTo(apps) |
| 66 | + new HTML('img').attr({ |
| 67 | + src: executable.config.icon ?? nullIcon, |
| 68 | + alt: `${executable.config.name} icon` |
| 69 | + }).appendTo(appElement) |
| 70 | + new HTML('div').text(executable.config.name).appendTo(appElement) |
| 71 | + }).catch((e: any) => console.error(e)) |
| 72 | + }) |
| 73 | + } |
| 74 | + |
| 75 | + await renderApps() |
| 76 | + document.addEventListener('fs_update', () => { |
| 77 | + renderApps().catch(e => console.error(e)) |
| 78 | + }) |
| 79 | + |
| 80 | + launcher.element.on('click', (e: Event) => { |
| 81 | + if (e.target !== e.currentTarget) return |
| 82 | + launcher.toggle() |
| 83 | + }) |
| 84 | + |
| 85 | + const statusBar = await process.loadLibrary('lib/StatusBar') |
| 86 | + |
| 87 | + statusBar.element.html(` |
| 88 | + <div class="outlined" data-toolbar-id="start"><span class="material-symbols-rounded">space_dashboard</span></div> |
| 89 | + |
| 90 | + <div data-toolbar-id="apps"></div> |
| 91 | + <flex></flex> |
| 92 | + <div class="outlined" data-toolbar-id="plugins"><span class="material-symbols-rounded">expand_less</span></div> |
| 93 | + <div class="outlined" data-toolbar-id="controls"> |
| 94 | + <span class="material-symbols-rounded battery">battery_2_bar</span> |
| 95 | + <span class="material-symbols-rounded signal">signal_cellular_4_bar</span> |
| 96 | + </div> |
| 97 | + <div class="outlined" data-toolbar-id="calendar"></div> |
| 98 | + |
| 99 | + `) |
| 100 | + |
| 101 | + setInterval((): any => { |
| 102 | + getTime().then((time) => { |
| 103 | + statusBar.element.qs('div[data-toolbar-id="calendar"]')?.text(time) |
| 104 | + }).catch(e => console.error) |
| 105 | + }, 1000) |
| 106 | + |
| 107 | + statusBar.element.qs('div[data-toolbar-id="start"]')?.on('click', () => { |
| 108 | + launcher.toggle() |
| 109 | + }) |
| 110 | + |
| 111 | + if ('getBattery' in navigator) { |
| 112 | + (navigator as any).getBattery().then((battery: any) => { |
| 113 | + statusBar.updateBatteryIcon(battery) |
| 114 | + |
| 115 | + battery.addEventListener('levelchange', () => { |
| 116 | + statusBar.updateBatteryIcon(battery) |
| 117 | + }) |
| 118 | + |
| 119 | + battery.addEventListener('chargingchange', () => { |
| 120 | + statusBar.updateBatteryIcon(battery) |
| 121 | + }) |
| 122 | + }) |
| 123 | + } else { |
| 124 | + const batteryDiv = document.querySelector('div[data-toolbar-id="controls"] > .battery') |
| 125 | + if (batteryDiv != null) { |
| 126 | + batteryDiv.innerHTML = 'battery_unknown' |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + async function ping (startTime: number): Promise<void> { |
| 131 | + fetch(`${process.kernel.config.SERVER as string}/bare/`) |
| 132 | + .then(() => { |
| 133 | + const endTime = performance.now() |
| 134 | + const pingTime = endTime - startTime |
| 135 | + statusBar.updateIcon(pingTime) |
| 136 | + }) |
| 137 | + .catch(() => { |
| 138 | + (document.querySelector('div[data-toolbar-id="controls"] > .signal') as HTMLElement).innerHTML = 'signal_cellular_connected_no_internet_4_bar' |
| 139 | + }) |
| 140 | + } |
| 141 | + |
| 142 | + setInterval((): any => ping(performance.now()), 10_000) |
| 143 | + |
| 144 | + document.addEventListener('app_opened', (e: AppOpenedEvent): void => { |
| 145 | + new HTML('app').appendMany( |
| 146 | + new HTML('img').attr({ |
| 147 | + alt: `${e.detail.proc.config.name} icon`, |
| 148 | + 'data-id': e.detail.token, |
| 149 | + src: e.detail.proc.config.icon ?? nullIcon |
| 150 | + }).on('click', () => { |
| 151 | + e.detail.win.focus() |
| 152 | + e.detail.win.toggleMin() |
| 153 | + }) |
| 154 | + ).appendTo(statusBar.element.qs('div[data-toolbar-id="apps"]')?.elm as HTMLElement) |
| 155 | + }) |
| 156 | + |
| 157 | + document.addEventListener('app_closed', (e: AppClosedEvent): void => { |
| 158 | + statusBar.element.qs('div[data-toolbar-id="apps"]')?.qs(`img[data-id="${e.detail.token}"]`)?.elm.parentElement?.remove() |
| 159 | + }) |
| 160 | + |
| 161 | + document.body.style.flexDirection = 'column-reverse' |
| 162 | + |
| 163 | + await statusBar.element.appendTo(document.body) |
| 164 | + await launcher.element.appendTo(document.body) |
| 165 | + await wm.windowArea.appendTo(document.body) |
| 166 | + |
| 167 | + splashElement.cleanup() |
| 168 | + } |
| 169 | +} |
| 170 | + |
| 171 | +export default BootLoader |
0 commit comments