chessbaseLinux/apps/web/src/lib.rs

38 lines
1.5 KiB
Rust
Raw Permalink Normal View History

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>
2026-10-30 15:09:17 +03:00
//! ChessPenguin in a browser: the entry point the page (`page/index.html`) calls once it
//! has decided the device is a computer. The interface is the desktop program's
//! (`chesspenguin_app`), with the web platform under it (`docs/dev/web.md`).
#![cfg(target_family = "wasm")]
use wasm_bindgen::JsCast;
use wasm_bindgen::prelude::*;
/// The reader worker's work (`page/import-worker.js`): one more file of the database
/// to read (its own file first) …
#[wasm_bindgen]
pub fn reader_give(name: String, bytes: Vec<u8>) {
chesspenguin_app::reader_give(name, bytes);
}
/// … and the database read and indexed away from the page (or only its games counted).
#[wasm_bindgen]
pub fn reader_read(count: bool) -> JsValue {
chesspenguin_app::reader_read(count)
}
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>
2026-10-30 15:09:17 +03:00
/// Starts ChessPenguin in the page's canvas `canvas_id`. Resolves once the interface
/// runs; an error says why it could not start (no WebGL, …).
#[wasm_bindgen]
pub async fn start(canvas_id: String) -> Result<(), JsValue> {
// The program's log and its panics go to the browser's console.
let _ = eframe::WebLogger::init(log::LevelFilter::Info);
let window = web_sys::window().ok_or("no window")?;
let canvas = window
.document()
.ok_or("no document")?
.get_element_by_id(&canvas_id)
.ok_or("no canvas")?
.dyn_into::<web_sys::HtmlCanvasElement>()?;
let lang = web_sys::UrlSearchParams::new_with_str(&window.location().search()?)?.get("lang");
chesspenguin_app::start_web(canvas, lang).await
}