Skip to main content

brep_app/
lib.rs

1//! brep-app — engine-native UI spike (an eframe host for the brep-render engine).
2//!
3//! The same [`app::BrepApp`] runs on native (`main.rs` → `run_native`) and on
4//! the web (`start` below → `eframe::WebRunner`), sharing one wgpu frame with
5//! egui. Additive spike: it does NOT touch the previous app or the render wasm build.
6
7pub mod app;
8pub mod column_tree;
9pub mod document;
10pub mod fonts;
11pub mod form;
12pub mod form_view;
13pub mod icon_text;
14pub mod icons;
15pub mod palette;
16pub mod panels;
17pub mod store;
18pub mod viewport;
19pub mod workbench;
20
21// The wasm history runner: a dedicated web worker so a history run stays OFF the
22// browser main thread (single-threaded wasm) and the UI never freezes during a run.
23// Its `worker_entry` is the worker-side onmessage loop. wasm only.
24#[cfg(target_arch = "wasm32")]
25pub mod worker;
26
27// --- wasm entry ----------------------------------------------------------------
28#[cfg(target_arch = "wasm32")]
29mod web {
30    use wasm_bindgen::prelude::*;
31    use wasm_bindgen::JsCast;
32
33    /// Start the eframe app on the given `<canvas>` element id. Called from JS.
34    #[wasm_bindgen]
35    pub async fn start(canvas_id: String) -> Result<(), JsValue> {
36        console_error_panic_hook::set_once();
37
38        // Bring up browser persistence FIRST. `ModelStore` is synchronous but every
39        // browser store large enough for a native BREP payload is async, so the
40        // whole key space is pulled into an in-memory mirror here — inside the one
41        // async seam the app has — BEFORE `BrepApp::new` performs its first read.
42        // See store.rs `mirror_store`.
43        crate::store::hydrate_web_store().await;
44
45        let document = web_sys::window()
46            .ok_or_else(|| JsValue::from_str("no window"))?
47            .document()
48            .ok_or_else(|| JsValue::from_str("no document"))?;
49        let canvas = document
50            .get_element_by_id(&canvas_id)
51            .ok_or_else(|| JsValue::from_str("canvas element not found"))?
52            .dyn_into::<web_sys::HtmlCanvasElement>()
53            .map_err(|_| JsValue::from_str("element is not a <canvas>"))?;
54
55        eframe::WebRunner::new()
56            .start(
57                canvas,
58                eframe::WebOptions::default(),
59                Box::new(|cc| {
60                    crate::fonts::install(&cc.egui_ctx);
61                    crate::app::BrepApp::new(cc)
62                        .map(|app| Box::new(app) as Box<dyn eframe::App>)
63                        .map_err(|e| e.into())
64                }),
65            )
66            .await
67    }
68}