feat(gfx): integrate wasi-gfx for loading and presenting GUI
CI / test (push) Successful in 24s

This commit is contained in:
Antigravity committed 2026-11-25 02:54:03 +00:00
1 parent ccb809bdc3
commit 7caadb3f7f
59 files changed
+7495 -16

No files matched your search

+53 -12
View File
@@ -2,6 +2,7 @@ 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(/\/$/, '');
@@ -12,6 +13,15 @@ const getJson = async (p) => {
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'}`);
@@ -21,23 +31,54 @@ try {
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');
canvas.width = desktop.screen.w; canvas.height = desktop.screen.h;
const ctx = canvas.getContext('2d');
const redraw = () => paint(ctx, desktop.frame().ops, desktop.screen);
const pos = (e) => { const r = canvas.getBoundingClientRect(); return [Math.round((e.clientX - r.left) * canvas.width / r.width), Math.round((e.clientY - r.top) * canvas.height / r.height)]; };
for (const [ev, kind] of [['pointermove', 'move'], ['pointerdown', 'down'], ['pointerup', 'up']]) {
canvas.addEventListener(ev, (e) => { const [x, y] = pos(e); if (desktop.pointer(kind, x, y)) redraw(); });
}
window.addEventListener('keydown', (e) => {
const k = e.key.length === 1 || ['Backspace', 'Enter', 'Escape'].includes(e.key) ? e.key : null;
if (k && desktop.key(k)) { e.preventDefault(); redraw(); }
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}`;
status.textContent = `${desktop.widgets.map((w) => `${w.id}=${w.componentName}`).join(' ')} | theme: ${platform.tokens.name} | wasi-gfx`;
// Handle for automation / the browser console.
window.moonPlasma = { desktop, platform, redraw, swap: async (id, c) => { await desktop.swap(id, c); redraw(); }, ready: true };
window.moonPlasma = {
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';