brep_app/lib.rs
1//! Shared eframe application for native windows and WebAssembly canvases.
2//! [`app::BrepApp`] hosts the renderer and egui in one wgpu frame.
3
4pub mod app;
5pub mod automation;
6mod color;
7pub mod column_tree;
8pub mod diagnostics;
9pub mod document;
10/// The three document classes (`.nbrep` / `.fbrep` / `.tbrep`).
11pub mod document_class;
12/// A family seed's table and its Generate (`.fbrep`).
13pub mod family_table;
14/// Register this build as the system's `.nbrep` opener
15/// (`brep-app --install-file-associations`). Native only: the browser has no
16/// file association to claim.
17#[cfg(not(target_arch = "wasm32"))]
18pub mod file_association;
19pub mod handles;
20pub mod fonts;
21pub mod form;
22pub mod form_view;
23pub mod icon_text;
24pub mod icons;
25pub mod instructions;
26mod http;
27/// The native binary's `log` sink (`main.rs`): `RUST_LOG` to stderr. Replaces
28/// `env_logger`; the wasm build logs through the browser console instead.
29#[cfg(not(target_arch = "wasm32"))]
30pub mod logger;
31/// The embedded MCP server (`brep-app --mcp`); native builds with `mcp`.
32#[cfg(all(not(target_arch = "wasm32"), feature = "mcp"))]
33pub mod mcp;
34/// The desktop event loop: eframe's winit application plus application-command
35/// dispatch that does not wait for a frame (a hidden Wayland window gets none).
36#[cfg(not(target_arch = "wasm32"))]
37pub mod desktop_loop;
38pub mod offsite;
39pub mod palette;
40pub mod panels;
41/// Where a frame's milliseconds go — the per-frame timing record behind the
42/// `__brepPerf` blob and the Info window's Performance rows.
43pub mod perf;
44/// The PLM client: config, transport, sign-in and the PLM store backend. Inert
45/// with no server configured.
46pub mod plm;
47pub mod recovery;
48/// Scripts in the CAD app: the CAD host for the PLM's interpreter core (S11).
49#[cfg(feature = "scripting")]
50pub mod scripting;
51#[cfg(not(target_arch = "wasm32"))]
52pub mod simulation_export;
53pub mod store;
54/// A template's input marks and its spin-out (`.tbrep`).
55pub mod template;
56pub mod viewport;
57/// The browser's tool surface (WebMCP): the MCP server's tool set, served by
58/// the page as the tools it registers with `document.modelContext`, answered
59/// through the automation queue. The pure half compiles natively for its
60/// tests; the host and the exports `web/webmcp.js` imports exist on wasm only.
61#[cfg(feature = "mcp")]
62pub mod webmcp;
63pub mod workbench;
64pub mod plugins;
65pub mod javascript;
66
67
68// The wasm history runner: a dedicated web worker so a history run stays OFF the
69// browser main thread (single-threaded wasm) and the UI never freezes during a run.
70// Its `worker_entry` is the worker-side onmessage loop. wasm only.
71#[cfg(target_arch = "wasm32")]
72pub mod worker;
73
74// The browser's code worker: one run_script execution on Boa in a Web Worker
75// the page can terminate (`web/code-worker.js` → `code_worker_entry`).
76#[cfg(all(target_arch = "wasm32", feature = "scripting"))]
77pub mod code_worker;
78
79// --- wasm entry ----------------------------------------------------------------
80#[cfg(target_arch = "wasm32")]
81mod web {
82 use wasm_bindgen::prelude::*;
83 use wasm_bindgen::JsCast;
84
85 /// Start the eframe app on the given `<canvas>` element id. Called from JS.
86 #[wasm_bindgen]
87 pub async fn start(canvas_id: String) -> Result<(), JsValue> {
88 brep_render::brep_kernel::panic_hook::set_once();
89
90 // Bring up browser persistence FIRST. `ModelStore` is synchronous but every
91 // browser store large enough for a native BREP payload is async, so the
92 // whole key space is pulled into an in-memory mirror here — inside the one
93 // async seam the app has — BEFORE `BrepApp::new` performs its first read.
94 // See store.rs `mirror_store`.
95 crate::store::hydrate_web_store().await;
96
97 let document = web_sys::window()
98 .ok_or_else(|| JsValue::from_str("no window"))?
99 .document()
100 .ok_or_else(|| JsValue::from_str("no document"))?;
101 let canvas = document
102 .get_element_by_id(&canvas_id)
103 .ok_or_else(|| JsValue::from_str("canvas element not found"))?
104 .dyn_into::<web_sys::HtmlCanvasElement>()
105 .map_err(|_| JsValue::from_str("element is not a <canvas>"))?;
106
107 eframe::WebRunner::new()
108 .start(
109 canvas,
110 eframe::WebOptions::default(),
111 Box::new(|cc| {
112 crate::fonts::install(&cc.egui_ctx);
113 crate::app::BrepApp::new(cc)
114 .map(|app| {
115 // The page's tool surface (WebMCP) answers through
116 // the app's own automation queue: hand it over
117 // here, where the app first exists.
118 #[cfg(feature = "mcp")]
119 crate::webmcp::attach(app.automation().clone(), app.diagnostics().adapter_line());
120 Box::new(app) as Box<dyn eframe::App>
121 })
122 .map_err(|e| e.into())
123 }),
124 )
125 .await
126 }
127}