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 fonts;
9pub mod form;
10pub mod palette;
11pub mod panels;
12pub mod store;
13pub mod viewport;
14
15// The wasm history runner: a dedicated web worker so a history run stays OFF the
16// browser main thread (single-threaded wasm) and the UI never freezes during a run.
17// Its `worker_entry` is the worker-side onmessage loop. wasm only.
18#[cfg(target_arch = "wasm32")]
19pub mod worker;
20
21// --- wasm entry ----------------------------------------------------------------
22#[cfg(target_arch = "wasm32")]
23mod web {
24    use wasm_bindgen::prelude::*;
25    use wasm_bindgen::JsCast;
26
27    /// Start the eframe app on the given `<canvas>` element id. Called from JS.
28    #[wasm_bindgen]
29    pub async fn start(canvas_id: String) -> Result<(), JsValue> {
30        console_error_panic_hook::set_once();
31
32        let document = web_sys::window()
33            .ok_or_else(|| JsValue::from_str("no window"))?
34            .document()
35            .ok_or_else(|| JsValue::from_str("no document"))?;
36        let canvas = document
37            .get_element_by_id(&canvas_id)
38            .ok_or_else(|| JsValue::from_str("canvas element not found"))?
39            .dyn_into::<web_sys::HtmlCanvasElement>()
40            .map_err(|_| JsValue::from_str("element is not a <canvas>"))?;
41
42        eframe::WebRunner::new()
43            .start(
44                canvas,
45                eframe::WebOptions::default(),
46                Box::new(|cc| {
47                    crate::fonts::install(&cc.egui_ctx);
48                    crate::app::BrepApp::new(cc)
49                        .map(|app| Box::new(app) as Box<dyn eframe::App>)
50                        .map_err(|e| e.into())
51                }),
52            )
53            .await
54    }
55}