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