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