Skip to content

Visual Studio Code, rebuilt in Rust

Rust
5.5 KB
Raw Blame
#!/usr/bin/env bun
// Renders port/fixtures/corners/lab.json with the reference's Chromium (Electron, 2x display, sRGB) to
// port/fixtures/corners/chromium.png, the golden image `cargo gc test -p app rounded_quads_match_chromium` compares
// GPUI's quads against. Run on the Studio: port/tools/corner-lab [out.png]
import { mkdtempSync, rmSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join, resolve } from 'node:path';

type Case = { name: string; r: number[]; b: number[]; bg: string; bc?: string; w?: number; h?: number; dx?: number; dy?: number; under?: string; underWidth?: number; backdrop?: number; child?: { bg: string; h: number }; shadow?: { y: number; blur: number; spread: number; color: string } };
const lab: { page: string; cases: Case[] } = await Bun.file('port/fixtures/corners/lab.json').json();
const out = resolve(process.argv[2] ?? 'port/fixtures/corners/chromium.png');
const ref = process.env.REF ?? join(process.env.HOME!, 'Developer/vscode-ref-1.148.0');
const electron = join(ref, '.build/electron/Code - OSS.app/Contents/MacOS/Code - OSS');
// Keep in sync with `lab_case_origin` in native/crates/app/src/corner_lab.rs.
const CELL = 48, COLUMNS = 8, INSET = 8;
const width = CELL * COLUMNS, height = CELL * Math.ceil(lab.cases.length / COLUMNS);

const boxes = lab.cases.map((c, i) => {
	const left = (i % COLUMNS) * CELL + INSET + (c.dx ?? 0);
	const top = Math.floor(i / COLUMNS) * CELL + INSET + (c.dy ?? 0);
	const [bt, br, bb, bl] = c.b;
	const style = [
		`left:${left}px`, `top:${top}px`, `width:${c.w ?? 32}px`, `height:${c.h ?? 32}px`, `background:${c.backdrop ? 'transparent' : c.bg}`,
		`border-style:solid`, `border-width:${bt}px ${br}px ${bb}px ${bl}px`, `border-color:${c.bc ?? 'transparent'}`,
		`border-radius:${c.r.map(v => `${v}px`).join(' ')}`,
		c.shadow ? `box-shadow:0 ${c.shadow.y}px ${c.shadow.blur}px ${c.shadow.spread}px ${c.shadow.color}` : '',
		c.child ? 'overflow:hidden' : '',
		c.backdrop ? 'isolation:isolate' : '',
	].filter(Boolean).join(';');
	const child = c.child ? `<div style="position:absolute;left:0;top:0;right:0;height:${c.child.h}px;background:${c.child.bg}"></div>` : '';
	// frostedGlass.css's `::before`: the fill over a blur of the backdrop, in the padding box.
	const glass = c.backdrop ? `<div style="position:absolute;inset:0;border-radius:${c.r[0] - c.b[0]}px;background:${c.bg};backdrop-filter:blur(${c.backdrop}px)"></div>` : '';
	// A square of `under` color behind the box, for probes on a contrasting background.
	const under = c.under ? `<div style="position:absolute;left:${(i % COLUMNS) * CELL + 2}px;top:${Math.floor(i / COLUMNS) * CELL + 2}px;width:${c.underWidth ?? 44}px;height:44px;background:${c.under}"></div>` : '';
	return `<div data-case="${i}">${under}<div style="position:absolute;box-sizing:border-box;${style}">${child}${glass}</div></div>`;
});
const html = `<!DOCTYPE html><html><body style="margin:0;background:${lab.page};width:${width}px;height:${height}px;overflow:hidden">${boxes.join('')}</body></html>`;

const dir = mkdtempSync(join(tmpdir(), 'corner-lab-'));
writeFileSync(join(dir, 'lab.html'), html);
writeFileSync(join(dir, 'package.json'), JSON.stringify({ name: 'corner-lab', main: 'main.js' }));
writeFileSync(join(dir, 'main.js'), `
const { app, BrowserWindow, nativeImage } = require('electron');
app.whenReady().then(async () => {
	const window = new BrowserWindow({ x: 100, y: 100, width: ${width}, height: ${height}, useContentSize: true, frame: false, show: true, backgroundColor: '${lab.page}' });
	await window.loadFile(${JSON.stringify(join(dir, 'lab.html'))});
	// One case on the page at a time: a raster tile that holds a path draw (a translucent or uneven border) resolves
	// at 4x MSAA, which would change the rounded edges of every other case in that tile. The workbench mostly does not.
	let lab, size, scale;
	for (let index = 0; index < ${lab.cases.length}; index++) {
		await window.webContents.executeJavaScript('for (const box of document.querySelectorAll("[data-case]")) box.style.display = box.dataset.case == ' + index + ' ? "" : "none"');
		await new Promise(resolve => setTimeout(resolve, index ? 300 : 1500));
		const image = await window.webContents.capturePage();
		size = image.getSize();
		scale = size.width / ${width};
		const bitmap = image.toBitmap();
		lab ??= Buffer.from(bitmap);
		const cell = ${CELL} * scale, left = (index % ${COLUMNS}) * cell, top = Math.floor(index / ${COLUMNS}) * cell;
		for (let y = top; y < top + cell; y++) bitmap.copy(lab, (y * size.width + left) * 4, (y * size.width + left) * 4, (y * size.width + left + cell) * 4);
	}
	require('fs').writeFileSync(${JSON.stringify(out)}, nativeImage.createFromBitmap(lab, { width: size.width, height: size.height }).toPNG());
	console.log('scale', scale);
	app.quit();
});
`);
const lock = '/tmp/port-gui.lock';
while (true) {
	try {
		require('node:fs').mkdirSync(lock);
		writeFileSync(join(lock, 'pid'), String(process.pid));
		break;
	} catch {
		// gui-lock.sh's lock, as ui-pair takes it: a dead owner's lock is dropped.
		const owner = Number(await Bun.file(join(lock, 'pid')).text().catch(() => '0'));
		try {
			if (owner) process.kill(owner, 0);
			await Bun.sleep(3000);
		} catch {
			rmSync(lock, { recursive: true, force: true });
		}
	}
}
try {
	const child = Bun.spawn([electron, dir, '--force-color-profile=srgb'], { stdout: 'inherit', stderr: 'ignore' });
	await child.exited;
} finally {
	rmSync(lock, { recursive: true, force: true });
	rmSync(dir, { recursive: true, force: true });
}
console.log(out);