import { Desktop } from '../src/desktop.mjs'; import { createPlatform } from '../src/platform.mjs'; import { tokensFromJson } from '../src/theme.mjs'; import { webLoader } from '../src/loader-web.mjs'; import { WasiGfxSurface, WasiGfxFrameBufferContext } from '../src/wasi-gfx.mjs'; import { paint } from './canvas.mjs'; const base = new URL('../../', import.meta.url).href.replace(/\/$/, ''); const q = new URLSearchParams(location.search); const getJson = async (p) => { const r = await fetch(`${base}/${p}`); if (!r.ok) throw new Error(`GET ${p}: HTTP ${r.status}`); return r.json(); }; async function drainStream(stream, handler) { const reader = stream.getReader(); while (true) { const { value, done } = await reader.read(); if (done) break; handler(value); } } const status = document.getElementById('status'); try { const config = await getJson(`config/${q.get('config') ?? 'desktop.json'}`); const apps = await getJson('config/apps.json'); const tokens = tokensFromJson(await getJson(`config/themes/${q.get('theme') ?? config.theme}.json`)); const fixed = q.get('time') ? new Date(q.get('time')) : null; const platform = createPlatform({ apps, tokens, now: fixed ? () => fixed : undefined }); const desktop = await new Desktop({ config, loader: webLoader(base), platform }).start(); // Present the desktop GUI through a wasi-gfx Surface + FrameBuffer Context. const canvas = document.getElementById('screen'); const gfxSurface = new WasiGfxSurface( { width: desktop.screen.w, height: desktop.screen.h }, { canvas }, ); const gfxContext = new WasiGfxFrameBufferContext(gfxSurface); const offscreen = document.createElement('canvas'); offscreen.width = desktop.screen.w; offscreen.height = desktop.screen.h; const offCtx = offscreen.getContext('2d'); const redraw = () => { paint(offCtx, desktop.frame().ops, desktop.screen); const img = offCtx.getImageData(0, 0, desktop.screen.w, desktop.screen.h); const buf = gfxContext.getCurrentBuffer(); buf.setWithCopy(new Uint8Array(img.data.buffer, img.data.byteOffset, img.data.byteLength)); gfxContext.present(); }; // Route input events from wasi-gfx Surface streams into Desktop. drainStream(gfxSurface.onPointerMove(), (e) => { if (desktop.pointer('move', Math.round(e.x), Math.round(e.y))) redraw(); }); drainStream(gfxSurface.onPointerDown(), (e) => { if (desktop.pointer('down', Math.round(e.x), Math.round(e.y))) redraw(); }); drainStream(gfxSurface.onPointerUp(), (e) => { if (desktop.pointer('up', Math.round(e.x), Math.round(e.y))) redraw(); }); drainStream(gfxSurface.onKeyDown(), (e) => { const k = e.text && (e.text.length === 1 || ['Backspace', 'Enter', 'Escape'].includes(e.text)) ? e.text : null; if (k && desktop.key(k)) redraw(); }); redraw(); if (!fixed) setInterval(redraw, 1000); status.textContent = `${desktop.widgets.map((w) => `${w.id}=${w.componentName}`).join(' ')} | theme: ${platform.tokens.name} | wasi-gfx`; // Handle for automation / the browser console. window.chandra = { desktop, platform, gfxSurface, gfxContext, redraw, swap: async (id, c) => { await desktop.swap(id, c); redraw(); }, ready: true, }; } catch (e) { status.textContent = `ERROR: ${e.message}`; status.style.color = '#f66'; window.chandraError = String(e.stack ?? e); throw e; }