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;
50mod net;
51/// The native binary's `log` sink (`main.rs`): `RUST_LOG` to stderr. Replaces
52/// `env_logger`; the wasm build logs through the browser console instead.
53#[cfg(not(target_arch = "wasm32"))]
54pub mod logger;
55/// The embedded MCP server (`brep-app --mcp`); native builds with `mcp`.
56#[cfg(all(not(target_arch = "wasm32"), feature = "mcp"))]
57pub mod mcp;
58/// Selection modifiers: Cmd/Ctrl primary, macOS Ctrl+click = secondary, list ranges.
59pub mod select_mods;
60/// What the Delete key / Delete command removes for the current selection (every target).
61pub mod delete_selection;
62/// The desktop event loop: eframe's winit application plus application-command
63/// dispatch that does not wait for a frame (a hidden Wayland window gets none).
64#[cfg(not(target_arch = "wasm32"))]
65pub mod desktop_loop;
66/// One egui pass drawn into an offscreen texture: the captures of a pass
67/// nothing presents (the desktop loop's, the browser pump's).
68pub mod offscreen;
69/// One egui pass the app runs itself when nothing is drawing it.
70#[cfg(feature = "automation")]
71pub mod on_demand;
72/// The browser's frame-free automation pump: commands answered while the tab
73/// is in the background.
74#[cfg(all(target_arch = "wasm32", feature = "automation"))]
75pub mod web_pump;
76pub mod offsite;
77pub mod palette;
78pub mod panels;
79/// Where a frame's milliseconds go — the per-frame timing record behind the
80/// `__brepPerf` blob and the Info window's Performance rows.
81pub mod perf;
82/// The PLM client: config, transport, sign-in and the PLM store backend. Inert
83/// with no server configured.
84pub mod plm;
85pub mod recovery;
86/// The problem reports this user submitted, and the notice when one is resolved.
87pub mod report_followup;
88/// Scripts in the CAD app: the CAD host for the PLM's interpreter core (S11).
89#[cfg(feature = "scripting")]
90pub mod scripting;
91#[cfg(not(target_arch = "wasm32"))]
92pub mod simulation_export;
93// SpaceMouse / Spaceball navigation: the shared core, native HID reader and
94// the exports `web/spacemouse.js` calls.
95pub mod spacemouse;
96pub mod store;
97/// A template's input marks and its spin-out (`.tbrep`).
98pub mod template;
99/// Theme tokens of the UI refresh; read only by `ui_kit` and its preview so far.
100pub mod theme;
101/// The UI refresh's components and the developer gallery that shows them.
102pub mod ui_kit;
103/// The desktop app's signed updates from next.brep.io (offered, never silent).
104#[cfg(not(target_arch = "wasm32"))]
105pub mod update;
106/// The desktop app's help, downloaded on the first Help press (help_local).
107#[cfg(not(target_arch = "wasm32"))]
108pub mod help_local;
109pub mod viewport;
110/// The browser's tool surface (WebMCP): the MCP server's tool set, served by
111/// the page as the tools it registers with `document.modelContext`, answered
112/// through the automation queue. The pure half compiles natively for its
113/// tests; the host and the exports `web/webmcp.js` imports exist on wasm only.
114#[cfg(feature = "mcp")]
115pub mod webmcp;
116pub mod workbench;
117pub mod plugins;
118pub mod javascript;
119
120
121// The wasm history runner: a dedicated web worker so a history run stays OFF the
122// browser main thread (single-threaded wasm) and the UI never freezes during a run.
123// Its `worker_entry` is the worker-side onmessage loop. wasm only.
124#[cfg(target_arch = "wasm32")]
125pub mod worker;
126
127// The browser's code worker: one `run` execution on Boa in a Web Worker
128// the page can terminate (`web/code-worker.js` → `code_worker_entry`).
129#[cfg(all(target_arch = "wasm32", feature = "scripting"))]
130pub mod code_worker;
131
132// --- wasm entry ----------------------------------------------------------------
133#[cfg(target_arch = "wasm32")]
134mod web {
135    use wasm_bindgen::prelude::*;
136    use wasm_bindgen::JsCast;
137
138    /// Start the eframe app on the given `<canvas>` element id. Called from JS.
139    #[wasm_bindgen]
140    pub async fn start(canvas_id: String) -> Result<(), JsValue> {
141        brep_render::brep_kernel::panic_hook::set_once();
142
143        // Bring up browser persistence FIRST. `ModelStore` is synchronous but every
144        // browser store large enough for a native BREP payload is async, so the
145        // whole key space is pulled into an in-memory mirror here — inside the one
146        // async seam the app has — BEFORE `BrepApp::new` performs its first read.
147        // See store.rs `mirror_store`.
148        crate::store::hydrate_web_store().await;
149
150        let document = web_sys::window()
151            .ok_or_else(|| JsValue::from_str("no window"))?
152            .document()
153            .ok_or_else(|| JsValue::from_str("no document"))?;
154        let canvas = document
155            .get_element_by_id(&canvas_id)
156            .ok_or_else(|| JsValue::from_str("canvas element not found"))?
157            .dyn_into::<web_sys::HtmlCanvasElement>()
158            .map_err(|_| JsValue::from_str("element is not a <canvas>"))?;
159
160        let runner = eframe::WebRunner::new();
161        // The automation pump borrows the app from this runner between
162        // frames (`web_pump`): commands answer in a background tab too.
163        #[cfg(feature = "automation")]
164        crate::web_pump::set_runner(&runner);
165        runner
166            .start(
167                canvas,
168                eframe::WebOptions::default(),
169                Box::new(|cc| {
170                    crate::fonts::install(&cc.egui_ctx);
171                    crate::app::BrepApp::new(cc)
172                        .map(|mut app| {
173                            app.start_report_followup(&cc.egui_ctx);
174                            #[cfg(feature = "automation")]
175                            crate::web_pump::install(&cc.egui_ctx, cc.wgpu_render_state.clone(), app.automation());
176                            // The page's tool surface (WebMCP) answers through
177                            // the app's own automation queue: hand it over
178                            // here, where the app first exists.
179                            #[cfg(feature = "mcp")]
180                            crate::webmcp::attach(app.automation().clone(), app.diagnostics().adapter_line());
181                            Box::new(app) as Box<dyn eframe::App>
182                        })
183                        .map_err(|e| e.into())
184                }),
185            )
186            .await
187    }
188}