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