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