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;
90pub mod store;
91/// A template's input marks and its spin-out (`.tbrep`).
92pub mod template;
93/// The desktop app's signed updates from next.brep.io (offered, never silent).
94#[cfg(not(target_arch = "wasm32"))]
95pub mod update;
96pub mod viewport;
97/// The browser's tool surface (WebMCP): the MCP server's tool set, served by
98/// the page as the tools it registers with `document.modelContext`, answered
99/// through the automation queue. The pure half compiles natively for its
100/// tests; the host and the exports `web/webmcp.js` imports exist on wasm only.
101#[cfg(feature = "mcp")]
102pub mod webmcp;
103pub mod workbench;
104pub mod plugins;
105pub mod javascript;
106
107
108// The wasm history runner: a dedicated web worker so a history run stays OFF the
109// browser main thread (single-threaded wasm) and the UI never freezes during a run.
110// Its `worker_entry` is the worker-side onmessage loop. wasm only.
111#[cfg(target_arch = "wasm32")]
112pub mod worker;
113
114// The browser's code worker: one run_script execution on Boa in a Web Worker
115// the page can terminate (`web/code-worker.js` → `code_worker_entry`).
116#[cfg(all(target_arch = "wasm32", feature = "scripting"))]
117pub mod code_worker;
118
119// --- wasm entry ----------------------------------------------------------------
120#[cfg(target_arch = "wasm32")]
121mod web {
122 use wasm_bindgen::prelude::*;
123 use wasm_bindgen::JsCast;
124
125 /// Start the eframe app on the given `<canvas>` element id. Called from JS.
126 #[wasm_bindgen]
127 pub async fn start(canvas_id: String) -> Result<(), JsValue> {
128 brep_render::brep_kernel::panic_hook::set_once();
129
130 // Bring up browser persistence FIRST. `ModelStore` is synchronous but every
131 // browser store large enough for a native BREP payload is async, so the
132 // whole key space is pulled into an in-memory mirror here — inside the one
133 // async seam the app has — BEFORE `BrepApp::new` performs its first read.
134 // See store.rs `mirror_store`.
135 crate::store::hydrate_web_store().await;
136
137 let document = web_sys::window()
138 .ok_or_else(|| JsValue::from_str("no window"))?
139 .document()
140 .ok_or_else(|| JsValue::from_str("no document"))?;
141 let canvas = document
142 .get_element_by_id(&canvas_id)
143 .ok_or_else(|| JsValue::from_str("canvas element not found"))?
144 .dyn_into::<web_sys::HtmlCanvasElement>()
145 .map_err(|_| JsValue::from_str("element is not a <canvas>"))?;
146
147 let runner = eframe::WebRunner::new();
148 // The automation pump borrows the app from this runner between
149 // frames (`web_pump`): commands answer in a background tab too.
150 #[cfg(feature = "automation")]
151 crate::web_pump::set_runner(&runner);
152 runner
153 .start(
154 canvas,
155 eframe::WebOptions::default(),
156 Box::new(|cc| {
157 crate::fonts::install(&cc.egui_ctx);
158 crate::app::BrepApp::new(cc)
159 .map(|mut app| {
160 app.start_report_followup(&cc.egui_ctx);
161 #[cfg(feature = "automation")]
162 crate::web_pump::install(&cc.egui_ctx, cc.wgpu_render_state.clone(), app.automation());
163 // The page's tool surface (WebMCP) answers through
164 // the app's own automation queue: hand it over
165 // here, where the app first exists.
166 #[cfg(feature = "mcp")]
167 crate::webmcp::attach(app.automation().clone(), app.diagnostics().adapter_line());
168 Box::new(app) as Box<dyn eframe::App>
169 })
170 .map_err(|e| e.into())
171 }),
172 )
173 .await
174 }
175}