Web version, stage 1: the interface as a library over a platform layer; apps/web

apps/desktop becomes a library (chesspenguin_app: the interface) and a thin
binary; apps/web starts the same interface in a page's canvas (eframe's
WebRunner). cfg(web) (build.rs) marks WebAssembly in a browser.

The platform layer (apps/desktop/src/platform): the program's folders, files,
background work, the files the user opens or saves, the clipboard and what
the platform offers (CAPS). On a computer it is the system as before; in a
browser the program's data lives in IndexedDB behind an in-memory file tree
loaded before the start, the files the user opens are read into it for the
session (only read), background jobs run on the page's thread after the
frame, the clipboard is read through the browser's API, sounds play through
audio elements, a game is saved as a download.

What a browser does not have is not built for it: updates, the online games
window and its probe, engine processes (analysis starts a Web Worker there;
the review and preparation jobs, Rodent and the judge are native), blocking
requests (engine, Maia and Rodent downloads report no download for the
platform, translation reports that it makes no requests). The desktop
program behaves as before; its build finds unused code (the web build
compiles parts it never shows).

apps/web: the entry point, the page /app/ (device check before anything large
is loaded: phones and tablets get the page about the apps in the site's nine
languages, keys app_* in website/site/i18n), build.sh (wasm-bindgen, profile
web), a static server and headless Firefox checks.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
vrubelroman 2026-10-30 15:09:17 +03:00
parent a4445d2c15
commit 3e69a5d57c
61 changed files with 2628 additions and 552 deletions

43
apps/web/page/device.js Normal file
View file

@ -0,0 +1,43 @@
// What device opened the page (docs/design/web-version.md §3.6.1; decisions «Веб-версия»:
// the web application is for computers only, phones and tablets get the app page).
// Decided by the page itself before anything large is loaded: a server cannot tell an
// iPad (it says "Macintosh") nor see the size of the screen.
//
// One function for the site and /app/, so that they never disagree whom to send where
// (the site takes this file over when the web version is published).
(function (global) {
"use strict";
// "computer" | "iphone" | "ipad" | "android-phone" | "android-tablet" | "phone" | "tablet"
function deviceClass() {
var ua = navigator.userAgent || "";
var uad = navigator.userAgentData;
var plat = (uad && uad.platform) || navigator.platform || "";
var touch = navigator.maxTouchPoints > 0;
// CSS pixels of the whole screen, not of the window: a narrow window of a computer
// stays a computer; min() removes the orientation.
var short = Math.min(screen.width, screen.height);
var coarse = matchMedia("(pointer: coarse)").matches;
var finePresent = matchMedia("(any-pointer: fine)").matches;
// iPadOS (and an iPhone asking for the desktop site) says "Mac"; a Mac has no touch.
var ios = /iPhone|iPod|iPad/.test(ua) || (/Mac/.test(plat) && navigator.maxTouchPoints > 1);
if (ios) return short < 600 ? "iphone" : "ipad";
if (/Android/i.test(ua)) return short < 600 ? "android-phone" : "android-tablet";
if (uad && uad.mobile) return short < 600 ? "phone" : "tablet";
if (/CrOS/.test(ua)) return "computer";
// A phone or tablet asking for the desktop site (Chrome on large tablets does by
// default): a touch screen and no fine pointer. A laptop with a touch screen has its
// touchpad, so it stays a computer.
if (touch && coarse && !finePresent) return short < 600 ? "phone" : "tablet";
return "computer";
}
// The Android version of an Android device (0: not said).
function androidVersion() {
var m = /Android (\d+)/.exec(navigator.userAgent || "");
return m ? Number(m[1]) : 0;
}
global.CPDevice = { deviceClass: deviceClass, androidVersion: androidVersion };
})(window);

58
apps/web/page/index.html Normal file
View file

@ -0,0 +1,58 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>ChessPenguin</title>
<link rel="icon" type="image/png" sizes="32x32" href="icon-32.png">
<link rel="apple-touch-icon" href="icon-180.png">
<meta name="color-scheme" content="dark light">
<style>
:root { --bg: #1b1b1b; --fg: #e6e6e6; --weak: #9a9a9a; --accent: #4f8fe6; --card: #262626; --line: #3a3a3a; }
@media (prefers-color-scheme: light) {
:root { --bg: #f7f7f7; --fg: #1d1d1d; --weak: #666; --accent: #1f6fd1; --card: #fff; --line: #ddd; }
}
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--fg);
font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; }
/* The program: the whole page is its canvas. */
#cp-app { position: fixed; inset: 0; }
#cp-canvas { width: 100%; height: 100%; display: block; outline: none; }
#cp-status { position: fixed; inset: 0; display: flex; align-items: center; justify-content: center;
padding: 16px; text-align: center; color: var(--weak); }
#cp-status.failed { color: var(--fg); }
[hidden] { display: none !important; }
/* The page for phones and tablets. */
#cp-device { max-width: 560px; margin: 0 auto; padding: 16px; box-sizing: border-box; }
#cp-device header { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
#cp-device .brand { display: flex; align-items: center; gap: 8px; font-weight: 600; }
#cp-device .brand img { width: 28px; height: 28px; }
#cp-device h1 { font-size: 24px; line-height: 1.25; margin: 24px 0 12px; }
#cp-device .lead { font-size: 20px; margin: 0 0 12px; }
#cp-device p { margin: 0 0 12px; }
#cp-device .btn { display: block; box-sizing: border-box; width: 100%; padding: 14px 16px;
border-radius: 10px; background: var(--accent); color: #fff; text-decoration: none;
font-weight: 600; text-align: center; border: 0; font-size: 17px; cursor: pointer; }
#cp-device .btn small { display: block; font-weight: 400; opacity: .85; font-size: 14px; }
#cp-device .link { color: var(--accent); }
#cp-device .weak { color: var(--weak); font-size: 14px; }
#cp-device .shots { display: flex; gap: 8px; overflow-x: auto; margin: 16px 0; }
#cp-device .shots img { height: 300px; border-radius: 8px; border: 1px solid var(--line); }
#cp-device .contacts { display: flex; gap: 16px; margin-bottom: 12px; }
#cp-device hr { border: 0; border-top: 1px solid var(--line); margin: 24px 0 12px; }
#cp-device .anyway { background: none; border: 0; padding: 0; font: inherit; color: var(--weak);
text-decoration: underline; cursor: pointer; }
#cp-device select { font: inherit; background: var(--card); color: var(--fg); border: 1px solid var(--line);
border-radius: 6px; padding: 4px 6px; }
</style>
</head>
<body>
<noscript><p style="padding:16px">ChessPenguin needs JavaScript. · Для ChessPenguin нужен JavaScript.</p></noscript>
<div id="cp-app" hidden><canvas id="cp-canvas"></canvas></div>
<div id="cp-status" hidden></div>
<main id="cp-device" hidden></main>
<!-- The page's texts in the site's nine languages (website/site/i18n/*.json, put in by build.sh). -->
<script>window.CP_TEXTS = {{TEXTS}};</script>
<script src="device.js"></script>
<script src="page.js"></script>
</body>
</html>

208
apps/web/page/page.js Normal file
View file

@ -0,0 +1,208 @@
// The page /app/: a computer gets ChessPenguin (the WebAssembly program, loaded only
// then); a phone or a tablet gets the page about the apps (docs/design/web-version.md
// §3.6, decisions «Веб-версия»), with a quiet way to open the web version anyway.
//
// ?phone=1 the device page on any device (checks, screenshots of the site)
// ?phone=0 the program on any device
// ?lang=xx the language (also of the program)
(function () {
"use strict";
var LANGS = [
["en", "English"], ["ru", "Русский"], ["fr", "Français"], ["de", "Deutsch"], ["es", "Español"],
["it", "Italiano"], ["pt", "Português"], ["uz", "Oʻzbekcha"], ["kk", "Қазақша"],
];
// The engine the web version downloads for analysis (Stockfish 19, brotli).
var ENGINE_MB = 78;
var ANYWAY_KEY = "cp.web.phoneAnyway";
var SITE = "https://chess.vrubel.xyz";
var params = new URLSearchParams(location.search);
var texts = window.CP_TEXTS || {};
function pickLang() {
var asked = (params.get("lang") || "").toLowerCase();
if (texts[asked]) return asked;
var prefs = navigator.languages || [navigator.language || ""];
for (var i = 0; i < prefs.length; i++) {
var code = String(prefs[i]).split(/[-_]/)[0].toLowerCase();
if (texts[code]) return code;
}
return "en";
}
var lang = pickLang();
function t(key, vars) {
var s = (texts[lang] && texts[lang][key]) || (texts.en && texts.en[key]) || key;
for (var k in vars || {}) s = s.split("{" + k + "}").join(vars[k]);
return s;
}
function el(tag, attrs, children) {
var e = document.createElement(tag);
for (var a in attrs || {}) {
if (a === "text") e.textContent = attrs[a];
else if (a === "onclick") e.addEventListener("click", attrs[a]);
else e.setAttribute(a, attrs[a]);
}
(children || []).forEach(function (c) { if (c) e.appendChild(c); });
return e;
}
function storage(get, value) {
try {
if (get) return localStorage.getItem(ANYWAY_KEY);
if (value === null) localStorage.removeItem(ANYWAY_KEY);
else localStorage.setItem(ANYWAY_KEY, value);
} catch (e) {}
return null;
}
// ----- the program --------------------------------------------------------------
function startApp() {
document.documentElement.lang = lang;
document.title = t("app_title");
document.getElementById("cp-device").hidden = true;
var status = document.getElementById("cp-status");
status.textContent = t("app_loading");
status.hidden = false;
var failed = function (e) {
console.error("ChessPenguin did not start:", e);
document.getElementById("cp-app").hidden = true;
status.textContent = t("app_failed");
status.className = "failed";
status.hidden = false;
};
import("./chesspenguin_web.js")
.then(function (m) { return m.default().then(function () { return m; }); })
.then(function (m) {
document.getElementById("cp-app").hidden = false;
return m.start("cp-canvas");
})
.then(function () {
status.hidden = true;
document.getElementById("cp-canvas").focus();
})
.catch(failed);
}
// ----- the page for phones and tablets ---------------------------------------------
function shots(names) {
return el("div", { class: "shots" }, names.map(function (n) {
return el("img", { src: "../static/screenshots/" + n, alt: "", loading: "lazy" });
}));
}
function shareButton() {
var url = SITE + "/app/?lang=" + lang;
var label = navigator.share ? t("app_send_link") : t("copy_link");
var b = el("button", { class: "btn", type: "button", text: "⇪ " + label });
b.addEventListener("click", function () {
if (navigator.share) {
navigator.share({ title: "ChessPenguin", url: url }).catch(function () {});
} else if (navigator.clipboard) {
navigator.clipboard.writeText(url).then(function () { b.textContent = t("link_copied"); });
}
});
return b;
}
function anyway() {
var size = ENGINE_MB + (lang === "ru" || lang === "kk" ? " МБ" : " MB");
return el("div", null, [
el("hr"),
el("button", {
class: "anyway", type: "button", text: t("app_open_anyway"),
onclick: function () { storage(false, "1"); startApp(); },
}),
el("p", { class: "weak", text: t("app_open_anyway_note", { size: size }) }),
]);
}
function androidPart() {
var v = CPDevice.androidVersion();
if (v && v < 12) {
return [el("p", { text: t("app_and_old", { version: String(v) }) }), shareButton()];
}
var hint = el("p", { class: "weak", text: t("next_android"), hidden: "" });
var show = function () { hint.hidden = false; };
return [
el("a", { class: "btn", href: "/download/android-noengine", rel: "nofollow", onclick: show }, [
document.createTextNode(t("btn_android")),
el("small", { text: t("and_light_title") }),
]),
el("p", null, [
el("a", { class: "link", href: "/download/android", rel: "nofollow", onclick: show, text: t("and_full_title") + " ›" }),
]),
el("p", { class: "weak", text: t("and_requires") }),
hint,
shots(["phone/01-home-360.webp", "phone/03-game-360.webp", "phone/04-engine-360.webp", "phone/08-review-360.webp"]),
el("p", null, [el("a", { class: "link", href: "/" + (lang === "en" ? "" : lang + "/") + "#download-android", text: t("app_and_more") + " ›" })]),
];
}
function iosPart() {
return [
el("p", { class: "lead", text: t("app_ios_lead") }),
el("p", { text: t("app_ios_now") }),
shareButton(),
el("p", { style: "margin-top:16px", text: t("app_ios_notify") }),
el("div", { class: "contacts" }, [
el("a", { class: "link", href: "mailto:vrubelroman@gmail.com?subject=ChessPenguin", text: "✉ " + t("email") }),
el("a", { class: "link", href: "https://t.me/vrubelr", rel: "noopener", text: "✈ Telegram" }),
]),
shots(["01-game-640.webp", "04-review-640.webp"]),
];
}
function showDevicePage(kind) {
document.documentElement.lang = lang;
document.title = t("app_phone_title");
var page = document.getElementById("cp-device");
page.textContent = "";
var select = el("select", { "aria-label": t("language") }, LANGS.map(function (l) {
var o = el("option", { value: l[0], text: l[1] });
if (l[0] === lang) o.selected = true;
return o;
}));
select.addEventListener("change", function () {
lang = select.value;
showDevicePage(kind);
});
page.appendChild(el("header", null, [
el("span", { class: "brand" }, [el("img", { src: "icon-64.png", alt: "" }), document.createTextNode("ChessPenguin")]),
select,
]));
page.appendChild(el("h1", { text: t("app_phone_title") }));
var body;
if (kind === "android-phone" || kind === "android-tablet") {
body = [el("p", { text: t("app_and_lead") })].concat(androidPart());
} else if (kind === "iphone" || kind === "ipad") {
body = iosPart();
} else {
body = [
el("p", { text: t("app_phone_generic") }),
el("a", { class: "btn", href: "/download/android-noengine", rel: "nofollow", text: t("btn_android") }),
el("p"),
shareButton(),
];
}
body.forEach(function (n) { page.appendChild(n); });
page.appendChild(anyway());
page.hidden = false;
}
// ----- which one --------------------------------------------------------------------
var asked = params.get("phone");
var kind = CPDevice.deviceClass();
if (asked === "1") {
showDevicePage(kind === "computer" ? "phone" : kind);
} else if (asked === "0" || kind === "computer" || storage(true) === "1") {
startApp();
} else {
showDevicePage(kind);
}
})();