Skip to main content

brep_app/
lib.rs

1//! The BREP CAD application: one eframe (egui + wgpu) codebase for native
2//! windows and WebAssembly canvases.
3//!
4//! [`app::BrepApp`] is the `eframe::App`; it hosts the `brep_render` 3D engine
5//! and egui in one wgpu frame, and [`app::BrepApp::new`] builds it from an
6//! `eframe::CreationContext`. The native binary is `brep-app`; the WebAssembly
7//! build exports `start(canvas_id)`, which runs the application on a
8//! `<canvas>` element.
9//!
10//! Main modules:
11//! - [`document`] — the open documents, each with its own engine state;
12//! - [`workbench`] — the workbench definitions, the PMI command set and the
13//!   panels each workbench claims;
14//! - [`panels`], [`form`], [`form_view`] — the panels and the shared schema
15//!   forms;
16//! - [`viewport`] — offscreen 3D rendering composited into egui, with input;
17//! - [`store`], [`recovery`] — native and browser persistence, autosave and
18//!   startup recovery;
19//! - [`automation`] — the command, state and hit-key registries, queue and
20//!   virtual input an automation host drives the application through;
21//! - `mcp` (native) and `webmcp` (browser) — the MCP hosts (feature `mcp`);
22//! - [`plm`] — the client for a `BREP_plm` server;
23//! - [`javascript`] — the in-app JavaScript editor.
24//!
25//! See the README for the command line, feature flags and architecture.
26
27pub mod app;
28pub mod automation;
29mod color;
30pub mod column_tree;
31pub mod diagnostics;
32pub mod document;
33/// The three document classes (`.nbrep` / `.fbrep` / `.tbrep`).
34pub mod document_class;
35/// A family seed's table and its Generate (`.fbrep`).
36pub mod family_table;
37/// Register this build as the system's `.nbrep` opener
38/// (`brep-app --install-file-associations`). Native only: the browser has no
39/// file association to claim.
40#[cfg(not(target_arch = "wasm32"))]
41pub mod file_association;
42pub mod handles;
43pub mod fonts;
44pub mod form;
45pub mod form_view;
46pub mod icon_text;
47pub mod icons;
48pub mod instructions;
49mod http;
50/// The native binary's `log` sink (`main.rs`): `RUST_LOG` to stderr. Replaces
51/// `env_logger`; the wasm build logs through the browser console instead.
52#[cfg(not(target_arch = "wasm32"))]
53pub mod logger;
54/// The embedded MCP server (`brep-app --mcp`); native builds with `mcp`.
55#[cfg(all(not(target_arch = "wasm32"), feature = "mcp"))]
56pub mod mcp;
57/// What the Delete key / Delete command removes for the current selection (every target).
58pub mod delete_selection;
59/// The desktop event loop: eframe's winit application plus application-command
60/// dispatch that does not wait for a frame (a hidden Wayland window gets none).
61#[cfg(not(target_arch = "wasm32"))]
62pub mod desktop_loop;
63/// One egui pass drawn into an offscreen texture: the captures of a pass
64/// nothing presents (the desktop loop's, the browser pump's).
65pub mod offscreen;
66/// One egui pass the app runs itself when nothing is drawing it.
67#[cfg(feature = "automation")]
68pub mod on_demand;
69/// The browser's frame-free automation pump: commands answered while the tab
70/// is in the background.
71#[cfg(all(target_arch = "wasm32", feature = "automation"))]
72pub mod web_pump;
73pub mod offsite;
74pub mod palette;
75pub mod panels;
76/// Where a frame's milliseconds go — the per-frame timing record behind the
77/// `__brepPerf` blob and the Info window's Performance rows.
78pub mod perf;
79/// The PLM client: config, transport, sign-in and the PLM store backend. Inert
80/// with no server configured.
81pub mod plm;
82pub mod recovery;
83/// The problem reports this user submitted, and the notice when one is resolved.
84pub mod report_followup;
85/// Scripts in the CAD app: the CAD host for the PLM's interpreter core (S11).
86#[cfg(feature = "scripting")]
87pub mod scripting;
88#[cfg(not(target_arch = "wasm32"))]
89pub mod simulation_export;
90// SpaceMouse / Spaceball navigation: the shared core, native HID reader and
91// the exports `web/spacemouse.js` calls.
92pub mod spacemouse;
93pub mod store;
94/// A template's input marks and its spin-out (`.tbrep`).
95pub mod template;
96/// The desktop app's signed updates from next.brep.io (offered, never silent).
97#[cfg(not(target_arch = "wasm32"))]
98pub mod update;
99pub mod viewport;
100/// The browser's tool surface (WebMCP): the MCP server's tool set, served by
101/// the page as the tools it registers with `document.modelContext`, answered
102/// through the automation queue. The pure half compiles natively for its
103/// tests; the host and the exports `web/webmcp.js` imports exist on wasm only.
104#[cfg(feature = "mcp")]
105pub mod webmcp;
106pub mod workbench;
107pub mod plugins;
108pub mod javascript;
109
110
111// The wasm history runner: a dedicated web worker so a history run stays OFF the
112// browser main thread (single-threaded wasm) and the UI never freezes during a run.
113// Its `worker_entry` is the worker-side onmessage loop. wasm only.
114#[cfg(target_arch = "wasm32")]
115pub mod worker;
116
117// The browser's code worker: one run_script execution on Boa in a Web Worker
118// the page can terminate (`web/code-worker.js` → `code_worker_entry`).
119#[cfg(all(target_arch = "wasm32", feature = "scripting"))]
120pub mod code_worker;
121
122// --- wasm entry ----------------------------------------------------------------
123#[cfg(target_arch = "wasm32")]
124mod web {
125    use wasm_bindgen::prelude::*;
126    use wasm_bindgen::JsCast;
127
128    /// Start the eframe app on the given `<canvas>` element id. Called from JS.
129    #[wasm_bindgen]
130    pub async fn start(canvas_id: String) -> Result<(), JsValue> {
131        brep_render::brep_kernel::panic_hook::set_once();
132
133        // Bring up browser persistence FIRST. `ModelStore` is synchronous but every
134        // browser store large enough for a native BREP payload is async, so the
135        // whole key space is pulled into an in-memory mirror here — inside the one
136        // async seam the app has — BEFORE `BrepApp::new` performs its first read.
137        // See store.rs `mirror_store`.
138        crate::store::hydrate_web_store().await;
139
140        let document = web_sys::window()
141            .ok_or_else(|| JsValue::from_str("no window"))?
142            .document()
143            .ok_or_else(|| JsValue::from_str("no document"))?;
144        let canvas = document
145            .get_element_by_id(&canvas_id)
146            .ok_or_else(|| JsValue::from_str("canvas element not found"))?
147            .dyn_into::<web_sys::HtmlCanvasElement>()
148            .map_err(|_| JsValue::from_str("element is not a <canvas>"))?;
149
150        let runner = eframe::WebRunner::new();
151        // The automation pump borrows the app from this runner between
152        // frames (`web_pump`): commands answer in a background tab too.
153        #[cfg(feature = "automation")]
154        crate::web_pump::set_runner(&runner);
155        runner
156            .start(
157                canvas,
158                eframe::WebOptions::default(),
159                Box::new(|cc| {
160                    crate::fonts::install(&cc.egui_ctx);
161                    crate::app::BrepApp::new(cc)
162                        .map(|mut app| {
163                            app.start_report_followup(&cc.egui_ctx);
164                            #[cfg(feature = "automation")]
165                            crate::web_pump::install(&cc.egui_ctx, cc.wgpu_render_state.clone(), app.automation());
166                            // The page's tool surface (WebMCP) answers through
167                            // the app's own automation queue: hand it over
168                            // here, where the app first exists.
169                            #[cfg(feature = "mcp")]
170                            crate::webmcp::attach(app.automation().clone(), app.diagnostics().adapter_line());
171                            Box::new(app) as Box<dyn eframe::App>
172                        })
173                        .map_err(|e| e.into())
174                }),
175            )
176            .await
177    }
178}