M0: WIT package, MoonBit components, JS host, docs, CI (#1)
CI / test (push) Successful in 19s
CI / test (push) Successful in 19s
This commit was merged in pull request #1.
This commit is contained in:
1 parent
e5548fc067
commit
880ecd4102
141 files changed
+9681
-1
No files matched your search
@@ -0,0 +1,50 @@
|
||||
// Canvas2D backend: rasterises a render list (draw-ops in screen coordinates).
|
||||
import { colorCss } from '../src/scene.mjs';
|
||||
|
||||
function roundRectPath(ctx, r, radius) {
|
||||
ctx.beginPath();
|
||||
const rad = Math.min(radius, r.w / 2, r.h / 2);
|
||||
if (rad > 0) ctx.roundRect(r.x, r.y, r.w, r.h, rad); else ctx.rect(r.x, r.y, r.w, r.h);
|
||||
}
|
||||
|
||||
export function paint(ctx, ops, screen) {
|
||||
ctx.clearRect(0, 0, screen.w, screen.h);
|
||||
let depth = 0;
|
||||
for (const op of ops) {
|
||||
const v = op.val;
|
||||
switch (op.tag) {
|
||||
case 'push-clip':
|
||||
ctx.save(); depth++;
|
||||
ctx.beginPath(); ctx.rect(v.x, v.y, v.w, v.h); ctx.clip();
|
||||
break;
|
||||
case 'pop-clip':
|
||||
if (depth === 0) throw new Error('pop-clip without push-clip');
|
||||
ctx.restore(); depth--;
|
||||
break;
|
||||
case 'fill-rect':
|
||||
ctx.fillStyle = colorCss(v.color); roundRectPath(ctx, v.rect, v.radius); ctx.fill();
|
||||
break;
|
||||
case 'stroke-rect':
|
||||
ctx.strokeStyle = colorCss(v.color); ctx.lineWidth = v.width;
|
||||
roundRectPath(ctx, { x: v.rect.x + 0.5, y: v.rect.y + 0.5, w: v.rect.w - 1, h: v.rect.h - 1 }, v.radius); ctx.stroke();
|
||||
break;
|
||||
case 'line':
|
||||
ctx.strokeStyle = colorCss(v.color); ctx.lineWidth = v.width;
|
||||
ctx.beginPath(); ctx.moveTo(v.start.x + 0.5, v.start.y + 0.5); ctx.lineTo(v.end.x + 0.5, v.end.y + 0.5); ctx.stroke();
|
||||
break;
|
||||
case 'circle':
|
||||
ctx.beginPath(); ctx.arc(v.center.x, v.center.y, v.radius, 0, Math.PI * 2);
|
||||
if (v.filled) { ctx.fillStyle = colorCss(v.color); ctx.fill(); }
|
||||
else { ctx.strokeStyle = colorCss(v.color); ctx.lineWidth = 1.5; ctx.stroke(); }
|
||||
break;
|
||||
case 'text':
|
||||
ctx.font = `${v.style.bold ? '600 ' : ''}${v.style.size}px system-ui, "DejaVu Sans", sans-serif`;
|
||||
ctx.fillStyle = colorCss(v.style.color);
|
||||
ctx.textAlign = v.style.align; ctx.textBaseline = 'middle';
|
||||
ctx.fillText(v.text, v.origin.x, v.origin.y);
|
||||
break;
|
||||
default: throw new Error(`unknown draw-op ${op.tag}`);
|
||||
}
|
||||
}
|
||||
if (depth !== 0) throw new Error(`unbalanced clip stack (${depth})`);
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>moon-plasma</title>
|
||||
<style>
|
||||
html, body { margin: 0; background: #05070d; color: #aab; font: 12px/1.4 system-ui, sans-serif; }
|
||||
#screen { display: block; margin: 0 auto; max-width: 100vw; }
|
||||
#status { text-align: center; padding: 4px; font-family: monospace; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<canvas id="screen" width="1280" height="720"></canvas>
|
||||
<div id="status">loading components…</div>
|
||||
<script type="module" src="./main.mjs"></script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,46 @@
|
||||
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 { 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();
|
||||
};
|
||||
|
||||
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();
|
||||
|
||||
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(); }
|
||||
});
|
||||
redraw();
|
||||
if (!fixed) setInterval(redraw, 1000);
|
||||
status.textContent = `${desktop.widgets.map((w) => `${w.id}=${w.componentName}`).join(' ')} | theme: ${platform.tokens.name}`;
|
||||
// Handle for automation / the browser console.
|
||||
window.moonPlasma = { desktop, platform, 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.moonPlasmaError = String(e.stack ?? e);
|
||||
throw e;
|
||||
}
|
||||
Reference in new issue
Block a user