Skip to main content

brep_app/
lib.rs

1//! Shared eframe application for native windows and WebAssembly canvases.
2//! [`app::BrepApp`] hosts the renderer and egui in one wgpu frame.
3
4pub mod app;
5pub mod automation;
6mod color;
7pub mod column_tree;
8pub mod diagnostics;
9pub mod document;
10/// The three document classes (`.nbrep` / `.fbrep` / `.tbrep`).
11pub mod document_class;
12/// A family seed's table and its Generate (`.fbrep`).
13pub mod family_table;
14/// Register this build as the system's `.nbrep` opener
15/// (`brep-app --install-file-associations`). Native only: the browser has no
16/// file association to claim.
17#[cfg(not(target_arch = "wasm32"))]
18pub mod file_association;
19pub mod fonts;
20pub mod form;
21pub mod form_view;
22pub mod icon_text;
23pub mod icons;
24pub mod instructions;
25mod http;
26/// The native binary's `log` sink (`main.rs`): `RUST_LOG` to stderr. Replaces
27/// `env_logger`; the wasm build logs through the browser console instead.
28#[cfg(not(target_arch = "wasm32"))]
29pub mod logger;
30/// The embedded MCP server (`brep-app --mcp`); native builds with `mcp`.
31#[cfg(all(not(target_arch = "wasm32"), feature = "mcp"))]
32pub mod mcp;
33pub mod offsite;
34pub mod palette;
35pub mod panels;
36/// Where a frame's milliseconds go — the per-frame timing record behind the
37/// `__brepPerf` blob and the Info window's Performance rows.
38pub mod perf;
39/// The PLM client: config, transport, sign-in and the PLM store backend. Inert
40/// with no server configured.
41pub mod plm;
42pub mod recovery;
43/// Scripts in the CAD app: the CAD host for the PLM's interpreter core (S11).
44#[cfg(feature = "scripting")]
45pub mod scripting;
46#[cfg(not(target_arch = "wasm32"))]
47pub mod simulation_export;
48pub mod store;
49/// A template's input marks and its spin-out (`.tbrep`).
50pub mod template;
51pub mod viewport;
52/// The browser's tool surface (WebMCP): the MCP server's tool set, served by
53/// the page as the tools it registers with `document.modelContext`, answered
54/// through the automation queue. The pure half compiles natively for its
55/// tests; the host and the exports `web/webmcp.js` imports exist on wasm only.
56#[cfg(feature = "mcp")]
57pub mod webmcp;
58pub mod workbench;
59pub mod plugins;
60pub mod javascript;
61
62
63// The wasm history runner: a dedicated web worker so a history run stays OFF the
64// browser main thread (single-threaded wasm) and the UI never freezes during a run.
65// Its `worker_entry` is the worker-side onmessage loop. wasm only.
66#[cfg(target_arch = "wasm32")]
67pub mod worker;
68
69// --- wasm entry ----------------------------------------------------------------
70#[cfg(target_arch = "wasm32")]
71mod web {
72    use wasm_bindgen::prelude::*;
73    use wasm_bindgen::JsCast;
74
75    /// Start the eframe app on the given `<canvas>` element id. Called from JS.
76    #[wasm_bindgen]
77    pub async fn start(canvas_id: String) -> Result<(), JsValue> {
78        brep_render::brep_kernel::panic_hook::set_once();
79
80        // Bring up browser persistence FIRST. `ModelStore` is synchronous but every
81        // browser store large enough for a native BREP payload is async, so the
82        // whole key space is pulled into an in-memory mirror here — inside the one
83        // async seam the app has — BEFORE `BrepApp::new` performs its first read.
84        // See store.rs `mirror_store`.
85        crate::store::hydrate_web_store().await;
86
87        let document = web_sys::window()
88            .ok_or_else(|| JsValue::from_str("no window"))?
89            .document()
90            .ok_or_else(|| JsValue::from_str("no document"))?;
91        let canvas = document
92            .get_element_by_id(&canvas_id)
93            .ok_or_else(|| JsValue::from_str("canvas element not found"))?
94            .dyn_into::<web_sys::HtmlCanvasElement>()
95            .map_err(|_| JsValue::from_str("element is not a <canvas>"))?;
96
97        eframe::WebRunner::new()
98            .start(
99                canvas,
100                eframe::WebOptions::default(),
101                Box::new(|cc| {
102                    crate::fonts::install(&cc.egui_ctx);
103                    crate::app::BrepApp::new(cc)
104                        .map(|app| {
105                            // The page's tool surface (WebMCP) answers through
106                            // the app's own automation queue: hand it over
107                            // here, where the app first exists.
108                            #[cfg(feature = "mcp")]
109                            crate::webmcp::attach(app.automation().clone(), app.diagnostics().adapter_line());
110                            Box::new(app) as Box<dyn eframe::App>
111                        })
112                        .map_err(|e| e.into())
113                }),
114            )
115            .await
116    }
117}