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