Skip to main content

brep_app/panels/
file.rs

1//! File dialog — a **reusable modal** for model-document management: **New /
2//! Open / Save / Save As** of the MODEL, driven from the toolbar. Mirrors the
3//! shape of [`crate::palette::Palette`]: the caller owns ONE [`FileDialog`],
4//! calls [`FileDialog::dispatch`] when a toolbar file button is clicked (which
5//! either acts immediately or opens the modal in a mode), and calls
6//! [`FileDialog::show`] every frame to draw the (possibly open) modal.
7//!
8//! The model document IS the engine-owned history: `EngineState` serializes it
9//! with `history_request_json()` (the `.nbrep` recipe) and loads it with
10//! `set_history_json` / `load_model_and_fit`. This dialog keeps NO model state
11//! and no document IDENTITY — the name and the clean baseline live on
12//! [`Document`], and the open set on [`Documents`]. It holds only transient UI
13//! buffers (the name field, a status line, the open modal) and drives the
14//! documents + the [`ModelStore`] seam.
15//!
16//! # New and Open never discard anything now
17//!
18//! Both ADD A TAB: New pushes an empty document, Open pushes the opened one (or
19//! focuses the tab already holding it). Nothing is replaced, so neither needs
20//! the "discard unsaved changes?" prompt they used to raise — the only place
21//! unsaved work can still be lost is CLOSING a tab, which is where that
22//! confirmation now lives (`Mode::ConfirmClose`).
23//!
24//! Persistence crosses the unified [`ModelStore`] seam — the ONE platform
25//! exception. The same embeddable explorer renders on web and desktop. The web
26//! backend lists localStorage models and can Upload; desktop lists the app's
27//! models directory. No OS-native file dialog is used.
28
29mod plm_save_as;
30
31use crate::automation::hit_keys::HitKeyDoc;
32use crate::document::{Document, Documents};
33use crate::document_class::{self, DocumentClass};
34use crate::family_table::{self, FamilySource};
35use crate::template::{self, TemplateInput};
36use crate::panels::parts_library::document_signature;
37use crate::panels::file_explorer::{self, FileExplorer, FileExplorerOptions};
38use crate::panels::kicad_import::{self, DiskFiles, KicadImport, KicadKind};
39use crate::store::{model_display_name, sibling_identity, ModelStore, PlaceKind, MODEL_EXTENSIONS};
40use brep_render::engine_state::{
41    ComponentInsert, EngineState, PartSink, StepAssemblyImport, StepAssemblyProbe,
42    StepAssemblyReport, StepProbeOutcome,
43};
44use brep_render::runner::MeshImportFormat;
45use eframe::egui;
46use std::collections::{BTreeMap, HashMap, HashSet};
47
48/// The file operation a toolbar button requests. The shell maps a clicked
49/// toolbar button to one of these and hands it to [`FileDialog::dispatch`].
50#[derive(Clone, Copy, Debug, PartialEq, Eq)]
51pub enum FileAction {
52    New,
53    /// New, of a chosen class: an empty family seed or template in a new tab
54    /// (the toolbar's New menu). `NewOfClass(Normal)` is plain New.
55    NewOfClass(DocumentClass),
56    Open,
57    Save,
58    SaveAs,
59    /// Import a CAD or mesh file FROM the user's filesystem, appending it to the
60    /// model rather than replacing it. STL/OBJ use RANSAC reconstruction.
61    Import,
62    /// Export the model TO the user's filesystem in a chosen format (STEP /
63    /// IGES / STL / OBJ / JSON).
64    Export,
65    /// Export the sheet-metal FLAT PATTERN (unfold) as a 2D vector file
66    /// (DXF / SVG). Opens the flat-pattern export modal.
67    ExportFlatPattern,
68    /// Insert an ASSEMBLY COMPONENT: opens the component selector — existing
69    /// parts-library entries first, then the model store's Open list (+ Upload)
70    /// — and routes the chosen document through the engine's insert flow
71    /// (`add_part_to_library` → an ACOMP instance referencing the returned
72    /// effective part name). Dispatched when the palette picks `ACOMP`.
73    InsertComponent,
74    /// Import a part from KiCad into the open part: a `.kicad_sym` symbol (the
75    /// Symbol workbench) or a `.kicad_mod` footprint (the Pads workbench), with
76    /// the footprint and 3D model KiCad's chain leads to
77    /// ([`crate::panels::kicad_import`]).
78    ImportKicad(KicadKind),
79}
80
81/// Which modal (if any) the dialog is currently showing.
82#[derive(Clone, Copy, PartialEq, Eq)]
83enum Mode {
84    /// Confirm recovery of a missing component source from its embedded part.
85    CreateComponentPart,
86    /// Open a saved model through the common explorer.
87    Open,
88    /// Prompt for a name and save under it.
89    SaveAs,
90    /// Confirm discarding unsaved changes before CLOSING a document tab —
91    /// the ONE place unsaved work can still be lost now that New and Open
92    /// both add a tab. `pending_close` holds the tab index.
93    ConfirmClose,
94    /// Choose an export format (STEP / IGES / STL / OBJ / JSON) for the current model.
95    Export,
96    /// Choose a STEP / IGES / STL / OBJ file through the common explorer.
97    Import,
98    /// Choose a 2D vector format (DXF / SVG) for the sheet-metal flat pattern.
99    FlatPattern,
100    /// Pick a part to insert as an assembly component (library entries + the
101    /// stored-model list + Upload).
102    InsertComponent,
103    /// A `.step` upload whose product structure the probe found: choose whether
104    /// to keep that structure (parts + component instances) or flatten it to
105    /// bodies. Backed by [`FileDialog::pending_step_import`].
106    StepAssembly,
107    /// A KiCad import: its file through the common explorer, then its own
108    /// stages ([`KicadImport`]).
109    Kicad,
110    /// Insert component chose a FAMILY: pick one of its members, which is
111    /// what gets placed ([`FileDialog::pick_member`]).
112    PickMember,
113    /// Insert component chose a TEMPLATE: name the new part and set its
114    /// inputs; the spun-out part is what gets placed ([`FileDialog::spin_out`]).
115    SpinOut,
116    /// The active document is a family MEMBER and the user just changed it:
117    /// save it as a new part, open the family, or undo ([`FileDialog::member_prompt`]).
118    MemberEdit,
119    /// The file chooser: ANY file on this machine, for a panel that asked
120    /// ([`FileDialog::request_pick_file`]). Browses the local files even in a
121    /// PLM session, whose own explorer shows the server's documents.
122    PickFile,
123    /// The assembly Save's questions about the documents it owns
124    /// ([`crate::document::owned_save`]): one row per document, every row
125    /// defaulting to yes ([`FileDialog::owned_prompt`]).
126    OwnedSave,
127    /// A part was saved under a new name while open assemblies place it:
128    /// point them at the new file too? Yes by default ([`FileDialog::retarget`]).
129    Retarget,
130    /// The Save-file chooser: WHERE an export goes, on desktop. A format
131    /// button (or a panel's Export) produced the file; this picks its folder
132    /// through the explorer over the local files and its name through a
133    /// field, then writes it ([`FileDialog::save_file`]). The browser never
134    /// sees it: there an export is a download, and the browser asks.
135    SaveFile,
136}
137
138/// A file a panel wants handed to the user: the one door every UI export
139/// takes ([`FileDialog::request_save_file`]). On desktop it opens the
140/// Save-file chooser (`Mode::SaveFile`); in the browser it is a download.
141#[derive(Clone, Debug, PartialEq)]
142pub struct SaveRequest {
143    /// The chooser's heading: "Export STEP", "Export the script".
144    pub title: String,
145    /// The file's full name, extension included; the chooser's name field
146    /// starts from it and the browser downloads under it.
147    pub file_name: String,
148    pub body: SaveBody,
149}
150
151/// An export's content. Text keeps the browser's string lane (its `Blob`
152/// takes a string directly, see [`ModelStore::export_file_named`]); on
153/// desktop both are bytes on disk.
154#[derive(Clone, Debug, PartialEq)]
155pub enum SaveBody {
156    Text(String),
157    Bytes(Vec<u8>),
158}
159
160impl SaveBody {
161    fn as_bytes(&self) -> &[u8] {
162        match self {
163            SaveBody::Text(text) => text.as_bytes(),
164            SaveBody::Bytes(bytes) => bytes,
165        }
166    }
167}
168
169impl SaveRequest {
170    pub fn text(title: impl Into<String>, file_name: impl Into<String>, text: String) -> Self {
171        Self { title: title.into(), file_name: file_name.into(), body: SaveBody::Text(text) }
172    }
173
174    pub fn bytes(title: impl Into<String>, file_name: impl Into<String>, bytes: Vec<u8>) -> Self {
175        Self { title: title.into(), file_name: file_name.into(), body: SaveBody::Bytes(bytes) }
176    }
177}
178
179/// An export produced and waiting for its folder: the Save-file chooser's
180/// payload, with what the write then says. Only the file is held; the
181/// dialog that produced it has closed (a format chooser) or never had a
182/// modal (a panel's Export button).
183struct PendingSave {
184    request: SaveRequest,
185    /// What the status says after the file name: " (13 files)", " (2 pages)".
186    detail: String,
187    /// The export's name for its toast ("Netlist", "Fabrication"): the PCB's
188    /// manufacturing outputs raise one, named by their output, on every
189    /// platform. Every other export's toast on desktop is the generic one.
190    label: Option<String>,
191    /// The bundle's warnings (design-rule findings): the chooser stays open
192    /// on them after the write, as the format dialog did, and they go to a
193    /// warning toast.
194    warnings: Vec<String>,
195    /// A family row was previewed: say so after the write
196    /// ([`FileDialog::say_previewed_export`]).
197    preview: bool,
198}
199
200/// How a produced export left the dialog ([`FileDialog::deliver`]).
201#[derive(Clone, Copy, Debug, PartialEq, Eq)]
202enum Delivery {
203    /// Written (desktop, no chooser) or downloaded (browser): done.
204    Done,
205    /// The Save-file chooser is up with it; the write happens on its Save.
206    Chooser,
207    /// Not delivered; the status line says why.
208    Failed,
209}
210
211/// The file-chooser tag of the Open modal's workspace (a PLM session's Open).
212const PICK_OPEN_WORKSPACE: &str = "file:open-workspace";
213
214/// A file the chooser delivered ([`FileDialog::take_picked_file`]).
215#[derive(Clone, Debug, PartialEq)]
216pub struct PickedFile {
217    /// The file's own name, extension included (`photo.jpg`).
218    pub name: String,
219    /// Where it was on this machine; `None` in the browser, which never says.
220    pub path: Option<String>,
221    pub bytes: Vec<u8>,
222}
223
224/// What an explorer folder is FOR. The explorer's location lives in the
225/// store and every modal shares it, so each purpose remembers where the user
226/// last was and the modal opens there. With one shared folder, a KiCad import
227/// from `…/kicad/symbols` sent the next Save As and Insert component into the
228/// KiCad library (ecad-reaudit-2026-09-23, B8).
229#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
230enum Purpose {
231    /// The user's own documents: Open, Save As, Insert component.
232    Models,
233    /// Foreign CAD and mesh files: Import.
234    Imports,
235    /// KiCad symbol and footprint libraries.
236    Kicad,
237    /// Where exports go: the Save-file chooser's folder, on the local files.
238    Exports,
239}
240
241impl Purpose {
242    const ALL: [Purpose; 4] = [Purpose::Models, Purpose::Imports, Purpose::Kicad, Purpose::Exports];
243
244    fn slug(self) -> &'static str {
245        match self {
246            Purpose::Models => "models",
247            Purpose::Imports => "imports",
248            Purpose::Kicad => "kicad",
249            Purpose::Exports => "exports",
250        }
251    }
252}
253
254impl Mode {
255    /// The folder this modal browses, for the modals that browse one.
256    fn purpose(self) -> Option<Purpose> {
257        match self {
258            Mode::Open | Mode::SaveAs | Mode::InsertComponent | Mode::CreateComponentPart => {
259                Some(Purpose::Models)
260            }
261            Mode::Import => Some(Purpose::Imports),
262            Mode::Kicad => Some(Purpose::Kicad),
263            Mode::SaveFile => Some(Purpose::Exports),
264            Mode::ConfirmClose
265            | Mode::Export
266            | Mode::FlatPattern
267            | Mode::StepAssembly
268            | Mode::PickMember
269            | Mode::SpinOut
270            | Mode::MemberEdit
271            | Mode::OwnedSave
272            | Mode::Retarget
273            | Mode::PickFile => None,
274        }
275    }
276}
277
278/// The store a purpose's explorer browses. Import, the KiCad import and the
279/// Save-file chooser deal in files on THIS MACHINE: in a PLM session the session
280/// store is the server's documents, so they browse the local files the session
281/// keeps beside it ([`ModelStore::local_files`]); on a file store they are the
282/// same store.
283fn browse_store(purpose: Purpose, store: &dyn ModelStore) -> &dyn ModelStore {
284    match purpose {
285        Purpose::Imports | Purpose::Kicad | Purpose::Exports => store.local_files().unwrap_or(store),
286        Purpose::Models => store,
287    }
288}
289
290/// The store's models folder, where a purpose not yet used starts (the
291/// Save-file chooser's exports included). `None` where the store has no
292/// such place (the browser), which leaves the location alone.
293fn models_folder(store: &dyn ModelStore) -> Option<String> {
294    store.browser_places().into_iter().find(|place| place.kind == PlaceKind::Models).map(|place| place.location)
295}
296
297/// A probed `.step` upload waiting on the user's choice — the state that makes
298/// the §3.9 modal work across frames (egui draws every frame; the click can
299/// land many frames after the upload).
300///
301/// The PARSED assembly itself is NOT here: it lives in the engine's stash
302/// (`EngineState::probe_step_assembly` put it there), and the import consumes
303/// that stash. This holds only what the prompt says and the `text` the flat
304/// lane needs — the two lanes that re-import from source rather than from the
305/// parse ("Import as bodies", and the structured lane's own failure fallback).
306struct PendingStepImport {
307    /// The uploaded file's name — the prompt's subject and the status line's.
308    name: String,
309    /// The file text. The engine's stash holds the PARSE, not the source, so
310    /// the flat lane's input has to be kept here.
311    text: String,
312    /// The counts the prompt shows, from the same walk the import runs.
313    probe: StepAssemblyProbe,
314    /// The §3.9 checkbox: flatten the sub-assembly tree to leaf occurrences
315    /// instead of building nested rigid sub-assembly documents. Only shown (and
316    /// only meaningful) when `probe.nested_depth > 1`; a depth-1 file imports
317    /// identically either way.
318    flatten: bool,
319}
320
321/// A `.step` upload whose structure probe is RUNNING on the document's
322/// background runner (native thread / browser worker — the parse builds every
323/// product's bodies and takes seconds on a real assembly, so it left the UI
324/// thread). Resolved by [`FileDialog::poll_step_probe`] into either the §3.9
325/// choice ([`PendingStepImport`]) or the flat lane.
326struct PendingStepProbe {
327    /// The engine's probe id, so a stale answer (a superseded upload) is ignored.
328    id: u64,
329    name: String,
330    /// The file text, kept for the flat lane the outcome may route to.
331    text: String,
332}
333
334/// What the user clicked in the §3.9 assembly-choice modal.
335#[derive(Clone, Copy, PartialEq, Eq)]
336enum StepChoice {
337    /// Keep the structure: parts-library entries + one component per occurrence.
338    Assembly,
339    /// Today's flat lane, unchanged: one IMPORT3D feature carrying the text.
340    Bodies,
341    /// Import nothing, and drop the parse (Esc / click-outside land here too).
342    Cancel,
343}
344
345/// What Insert component lists, and so the only files it inserts: parts, and
346/// the families and templates that each lead to a part.
347const INSERT_EXTENSIONS: &[&str] = MODEL_EXTENSIONS;
348
349/// One row of the member picker: a family row and where its member is.
350struct MemberChoice {
351    part_number: String,
352    description: String,
353    /// The member's store identity; `None` when the part number cannot be a
354    /// file name.
355    identity: Option<String>,
356    /// Whether that file exists (Generate has written it).
357    exists: bool,
358    /// On a PLM store: why the member cannot be placed (not generated, waiting
359    /// for its bake, failed it), as the server's family view says.
360    why_not: Option<String>,
361}
362
363/// An armed member picker: the family Insert component chose.
364struct PickMember {
365    /// The family's file name, for the heading.
366    family_file: String,
367    rows: Vec<MemberChoice>,
368    /// On a PLM store the members are parts: the list comes from the server
369    /// (`loading` until it has answered), and a chosen member's document is
370    /// read from it.
371    plm: bool,
372    loading: bool,
373}
374
375/// An armed template spin-out: the template Insert component chose, and the
376/// user's answers so far.
377struct SpinOut {
378    template_file: String,
379    template: serde_json::Value,
380    inputs: Vec<TemplateInput>,
381    /// The new part's name (no extension).
382    name: String,
383    /// Input name -> the value text the user typed or chose.
384    values: BTreeMap<String, String>,
385    /// Where the new part goes: the folder of the assembly being edited, or
386    /// the models folder when that assembly has never been saved.
387    assembly: Option<String>,
388    /// On a PLM store: the template part the SERVER spins out, and the new
389    /// part's number (empty lets a counter part type number it).
390    plm_template: Option<String>,
391    number: String,
392}
393
394/// PLM work the dialog waits on (plm-cad-integration-todo S9), polled each
395/// frame so the web build works the same.
396enum PlmWork {
397    /// The member picker's list.
398    Members(crate::panels::plm_family::Pending<Vec<crate::plm::family::Member>>),
399    /// A chosen member's (or a spun-out part's) document, to place: the key,
400    /// the text, and the part number it is named by.
401    Place(crate::panels::plm_family::Pending<(String, String, String)>),
402    /// Open the family a member came from: its newest revision's key.
403    OpenFamily(crate::panels::plm_family::Pending<String>),
404}
405
406/// "Also point these assemblies at the new file?" ([`Mode::Retarget`]).
407struct Retarget {
408    /// The part's previous name, as the assemblies spell their copies of it.
409    old: String,
410    new_key: String,
411    /// The part as it was saved under the new name.
412    text: String,
413    tabs: Vec<(u64, String)>,
414}
415
416/// A Save waiting for its answers ([`Mode::OwnedSave`]).
417struct OwnedPrompt {
418    plan: crate::document::owned_save::OwnedSavePlan,
419    /// The name the tab is being saved under.
420    name: String,
421    /// Save As through the explorer (`save_to_browser`), not a plain Save.
422    browser: bool,
423}
424
425/// The hand-edit prompt's subject: which tab, and the family it came from.
426struct MemberPrompt {
427    doc_id: u64,
428    source: FamilySource,
429    /// The family file's identity, beside the member.
430    family_identity: String,
431    /// The prompt came from plain Save rather than from an edit.
432    from_save: bool,
433}
434
435/// Make the engine's document a `class` document: the class field, and none
436/// of the other classes' blocks (a template saved as a part stops asking for
437/// inputs; a family saved as a template drops its table). No undo step: this
438/// is what the new FILE is, written as it is saved.
439/// The replace-component lineage stamp: a Save As under a NEW identity
440/// appends the identity the tab was saved under (and, for a family member,
441/// its family) to the document's `derivedFrom` chain. Silent: it is part of
442/// what the copy is, not an edit. A never-saved document records nothing.
443pub(crate) fn stamp_save_as_lineage(docs: &mut Documents, store: &dyn ModelStore, new_name: &str) {
444    let Some(old) = docs.active().name().map(str::to_string) else { return };
445    let old = store.canonical_identity(&old);
446    if old == store.canonical_identity(new_name) {
447        return;
448    }
449    let engine = docs.tab_engine_mut();
450    let mut document = serde_json::json!({});
451    for key in [brep_render::engine_state::replace_component::DERIVED_FROM_KEY, family_table::FAMILY_SOURCE_KEY] {
452        if let Some(block) = engine.history.document_block(key) {
453            document[key] = block.clone();
454        }
455    }
456    brep_render::engine_state::replace_component::stamp_derived_from(&mut document, &old);
457    engine.history.set_document_block_no_undo(
458        brep_render::engine_state::replace_component::DERIVED_FROM_KEY,
459        Some(document[brep_render::engine_state::replace_component::DERIVED_FROM_KEY].clone()),
460    );
461}
462
463fn adopt_class(engine: &mut EngineState, class: DocumentClass) {
464    document_class::set_document_class(engine, class);
465    let drop: &[&str] = match class {
466        DocumentClass::Normal => &[family_table::FAMILY_TABLE_KEY, template::TEMPLATE_INPUTS_KEY],
467        DocumentClass::Family => &[template::TEMPLATE_INPUTS_KEY],
468        DocumentClass::Template => &[family_table::FAMILY_TABLE_KEY],
469        DocumentClass::Instructions => &[family_table::FAMILY_TABLE_KEY, template::TEMPLATE_INPUTS_KEY],
470    };
471    for key in drop {
472        if engine.history.document_block(key).is_some() {
473            engine.history.set_document_block_no_undo(key, None);
474        }
475    }
476}
477
478/// Where a spun-out part is written: `<name>.nbrep` in the assembly's folder,
479/// or the bare file name (the models folder) when the assembly has none.
480fn spin_out_target(spin: &SpinOut) -> String {
481    let file = DocumentClass::Normal.file_name(spin.name.trim());
482    match &spin.assembly {
483        Some(assembly) => sibling_identity(assembly, &file),
484        None => file,
485    }
486}
487
488/// `"s"` unless there is exactly one — the difference between "1 parts" and a
489/// sentence a user believes.
490fn plural(count: usize) -> &'static str {
491    if count == 1 {
492        ""
493    } else {
494        "s"
495    }
496}
497
498/// The document name an imported assembly's unnamed products are stemmed from:
499/// the file's base name without its STEP extension, so a nameless product reads
500/// `bracket-assy-part-7`, not `bracket-assy.step-part-7`.
501/// A document title as a file stem: path separators and the characters a
502/// file name cannot hold become `-`; leading and trailing dots, spaces and
503/// dashes go.
504fn file_stem_of_title(title: &str) -> String {
505    let stem: String = title.chars().map(|c| if c.is_control() || "/\\:*?\"<>|".contains(c) { '-' } else { c }).collect();
506    stem.trim_matches(|c: char| c == '.' || c == ' ' || c == '-').to_string()
507}
508
509fn step_document_name(file_name: &str) -> String {
510    let base = file_name.rsplit(['/', '\\']).next().unwrap_or(file_name);
511    let cut = base
512        .rfind('.')
513        .filter(|dot| is_step_name(&base[*dot..]))
514        .unwrap_or(base.len());
515    let stem = &base[..cut];
516    if stem.is_empty() {
517        base.to_string()
518    } else {
519        stem.to_string()
520    }
521}
522
523/// The §3.9 outcome line: `imported bracket-assy.step — 7 parts, 23 components
524/// (2 mirrored instances baked)`, plus the tail an imperfect import owes the
525/// user. Every qualifier is reported ONLY when it happened, so a clean import
526/// reads clean — but a partial one never reads as a whole one:
527///
528/// * `baked_nonrigid` — occurrences whose mirror/scale was baked into a part of
529///   their own (§3.4), which is why the part count can exceed the file's;
530/// * `failed_products` — products that did not encode, skipped and counted;
531/// * `flat_fallback` — the user asked for an assembly and got bodies. Said
532///   plainly, never silently (the dialog lane reports this through the Err
533///   branch instead, which is the only way it can reach the user from here);
534/// * `first_error` — the one thing that explains the rest.
535/// The STEP-assembly import's [`PartSink`]: writes each unique part document to
536/// the model store and hands back the identity it was stored under, so an
537/// imported part carries a REAL `sourceKey` and there is no second kind of part.
538///
539/// # The destination
540///
541/// `browser_write` at the explorer's CURRENT location, under
542/// `{assembly}-{part}` — the convention `panels::step_parts` already uses for a
543/// single STEP part (that panel lets the user pick the folder first; the
544/// assembly modal inherits wherever the explorer is pointing).
545///
546/// The kernel plan's alternative — a `{assembly}/{part}.nbrep` SUB-FOLDER —
547/// is not reachable through this door: every `browser_write` implementation
548/// flattens the name to a single file (native takes `file_name()`, web takes
549/// `model_display_name`), so a sub-path would silently collapse. Writing the
550/// assembly name into the FILE name keeps a 50-part import grouped in one
551/// listing without a folder convention this seam cannot express. Creating and
552/// navigating into a folder as a side effect of an import is the prompt this
553/// lane would have to grow, and it is not bolted on here.
554///
555/// A failed write is per part: that part stays embedded-only (`None`) and the
556/// import continues. Losing one part's FILE is recoverable; losing the import
557/// is not.
558///
559/// # Known limit of a flat name
560///
561/// `taken` is per IMPORT, so re-importing the same file reuses the same names —
562/// which is what makes cross-import dedup work (same key, same signature, the
563/// resident entry is reused). But two DIFFERENT assemblies whose document names
564/// sanitize to the same stem (`as1-ug` and `as1_ug`) write to each other's file
565/// names. The second import wins the file; the first assembly's entry then
566/// disagrees with it, so it badges outdated and the write-through guard refuses
567/// to clobber it — visible and recoverable (the embedded document is intact),
568/// but two assemblies sharing one name. The fix is the per-assembly sub-folder
569/// `browser_write` cannot express; see the kernel plan's §3.5.
570struct StorePartSink<'a> {
571    store: &'a dyn ModelStore,
572    /// Prefixes every file name, so one import's parts sort together.
573    prefix: String,
574    /// File names already claimed this import — two STEP products can sanitize
575    /// to the same name, and the second must not overwrite the first's file
576    /// (that would leave two entries pointing at one document).
577    taken: std::collections::HashSet<String>,
578    written: usize,
579    failures: Vec<String>,
580    /// On a PLM store a part is an ITEM, not a file at a location: the
581    /// browser's `/models/<name>` key is one the PLM cannot hold. The parts
582    /// stay embedded and become items when the assembly is saved (the Save's
583    /// promotion, `document::owned_save`, at every depth, one item per
584    /// content), so nothing is written here and `deferred` counts them.
585    plm: bool,
586    deferred: usize,
587}
588
589impl<'a> StorePartSink<'a> {
590    fn new(store: &'a dyn ModelStore, prefix: &str) -> Self {
591        Self {
592            store,
593            prefix: sanitize_file_stem(prefix),
594            taken: std::collections::HashSet::new(),
595            written: 0,
596            failures: Vec::new(),
597            plm: store.plm_client().is_some(),
598            deferred: 0,
599        }
600    }
601}
602
603impl PartSink for StorePartSink<'_> {
604    fn store_part(&mut self, part_name: &str, document_json: &str) -> Option<String> {
605        if self.plm {
606            self.deferred += 1;
607            return None;
608        }
609        let base = format!("{}-{}", self.prefix, sanitize_file_stem(part_name));
610        let mut name = base.clone();
611        let mut suffix = 2;
612        while !self.taken.insert(name.clone()) {
613            name = format!("{base}-{suffix}");
614            suffix += 1;
615        }
616        match self.store.browser_write(&name, document_json) {
617            Ok(identity) => {
618                self.written += 1;
619                Some(identity)
620            }
621            Err(error) => {
622                self.failures.push(format!("{name}: {error}"));
623                None
624            }
625        }
626    }
627}
628
629/// A STEP product name reduced to something every store backend can hold as a
630/// file name: ASCII word characters, `.`, `-` kept; everything else (spaces,
631/// slashes, the `(mirrored)` parentheses this crate appends) becomes `_`. Runs
632/// of `_` collapse and the ends are trimmed, so a name is readable rather than
633/// a row of underscores. Empty input yields `part`.
634/// The Save dialog's row for the document being saved itself, when it is a
635/// PLM revision this user cannot save yet: check it out (default yes), or
636/// `Blocked` when nobody can say yes (checked out by someone else, released).
637/// `None` when it can be saved as it is, or is not a PLM revision.
638fn top_checkout_row(docs: &Documents, store: &dyn ModelStore, name: &str) -> Option<crate::document::owned_save::Planned> {
639    use crate::document::owned_save::{Issue, OwnedWrite, Planned};
640    let client = store.plm_client()?;
641    let (part, revision) = crate::panels::plm_host::revision_of(name)?;
642    let doc = docs.active();
643    if matches!(doc.save_access(), crate::document::Access::Editable) && doc.has_plm_save_access() {
644        return None;
645    }
646    let viewer = client.can_author() == Some(false);
647    let issue = match store.plm_revision(&crate::plm::identity::document_key(&part, &revision)) {
648        Some(entry) if entry.editable && entry.locked_by_me => return None,
649        _ if viewer => Issue::Blocked(crate::document::owned_save::VIEW_ONLY.into()),
650        Some(entry) if !entry.editable => {
651            Issue::Blocked(format!("PLM revision {} is {} and not editable", entry.revision_label, entry.lifecycle))
652        }
653        Some(entry) if entry.locked_by.is_some() => Issue::Blocked(format!("checked out by {}", entry.locked_by.unwrap_or_default())),
654        _ => Issue::CheckOut { part, revision, newer: false },
655    };
656    let write = OwnedWrite { key: name.to_string(), names: Vec::new(), depth: 0, text: String::new(), synced: String::new(), variants: Vec::new() };
657    Some(Planned { write, issue: Some(issue), proceed: true, choice: 0, edited_last: None })
658}
659
660/// A checkout or new revision the Save asked for failed AFTER the assembly
661/// `id` was written, and its copies were just marked: the stored assembly
662/// holds the change without the mark, so the next open (here or on another
663/// client) would refresh the copy and lose it. Write the mark into it.
664fn write_late_marks(docs: &mut Documents, store: &dyn ModelStore, id: u64, was_clean: bool) {
665    if let Err(error) = docs.write_unsaved_marks(store, id, was_clean) {
666        docs.engine_mut().push_notice(format!("Save the assembly again: the change it keeps could not be marked in it ({error})"));
667    }
668}
669
670/// One PLM item to make per distinct new key of `plan`'s promotions:
671/// `(new key it replaces, part name, part type)`. The type is the document's
672/// own kind, as the PLM import wizard picks it: `assembly` when it places
673/// components, else `component`.
674fn plm_promotion_requests(current: &serde_json::Value, plan: &crate::document::owned_save::OwnedSavePlan) -> Vec<(String, String, String)> {
675    let mut requests: Vec<(String, String, String)> = Vec::new();
676    for promotion in &plan.promotions {
677        if requests.iter().any(|(key, ..)| *key == promotion.new_key) {
678            continue;
679        }
680        let mut document = current;
681        for name in &promotion.names {
682            document = &document["partsLibrary"][name]["document"];
683        }
684        let part = promotion.names.last().cloned().unwrap_or_default();
685        requests.push((promotion.new_key.clone(), part, crate::plm::promote::part_type_of(document).to_string()));
686    }
687    requests
688}
689
690/// Every promotion of `plan` takes the PLM key made for its new key.
691fn assign_plm_keys(plan: &mut crate::document::owned_save::OwnedSavePlan, keys: &[String]) {
692    let mut order: Vec<String> = Vec::new();
693    for promotion in &plan.promotions {
694        if !order.contains(&promotion.new_key) {
695            order.push(promotion.new_key.clone());
696        }
697    }
698    for promotion in &mut plan.promotions {
699        if let Some(key) = order.iter().position(|k| *k == promotion.new_key).and_then(|i| keys.get(i)) {
700            promotion.new_key = key.clone();
701        }
702    }
703}
704
705pub(crate) fn sanitize_file_stem(name: &str) -> String {
706    let mut out = String::with_capacity(name.len());
707    for ch in name.chars() {
708        if ch.is_ascii_alphanumeric() || ch == '.' || ch == '-' {
709            out.push(ch);
710        } else if !out.ends_with('_') {
711            out.push('_');
712        }
713    }
714    let trimmed = out.trim_matches('_');
715    if trimmed.is_empty() {
716        "part".to_string()
717    } else {
718        trimmed.to_string()
719    }
720}
721
722fn assembly_import_message(name: &str, report: &StepAssemblyReport) -> String {
723    let mut message = format!(
724        "imported {name} \u{2014} {} part{}, {} component{}",
725        report.parts,
726        plural(report.parts),
727        report.instances,
728        plural(report.instances)
729    );
730    if report.baked_nonrigid > 0 {
731        message.push_str(&format!(
732            " ({} mirrored instance{} baked)",
733            report.baked_nonrigid,
734            plural(report.baked_nonrigid)
735        ));
736    }
737    if report.failed_products > 0 {
738        message.push_str(&format!(
739            "; {} product{} could not be built",
740            report.failed_products,
741            plural(report.failed_products)
742        ));
743    }
744    if report.flat_fallback {
745        message.push_str("; the structure was NOT used \u{2014} the bodies came in flat");
746    }
747    if let Some(error) = &report.first_error {
748        message.push_str(&format!(" [{error}]"));
749    }
750    message
751}
752
753/// Whether an imported file name is a STEP file (`.step` / `.stp`, any case) —
754/// the routing key in [`FileDialog::show`]. Model documents arrive
755/// extension-stripped (web) or as a stored model name, so this
756/// never mis-routes a `.nbrep` file.
757fn is_step_name(name: &str) -> bool {
758    let lower = name.to_ascii_lowercase();
759    lower.ends_with(".step") || lower.ends_with(".stp")
760}
761
762/// Whether an imported file name is an IGES file (`.iges` / `.igs`, any case) —
763/// the sibling routing key of [`is_step_name`].
764fn is_iges_name(name: &str) -> bool {
765    let lower = name.to_ascii_lowercase();
766    lower.ends_with(".iges") || lower.ends_with(".igs")
767}
768
769fn is_stl_name(name: &str) -> bool {
770    name.to_ascii_lowercase().ends_with(".stl")
771}
772
773fn is_obj_name(name: &str) -> bool {
774    name.to_ascii_lowercase().ends_with(".obj")
775}
776
777/// Whether an imported file name is a 3MF package. It takes the STL lane: the
778/// same reconstruction preview, the same tolerances, the same accept.
779fn is_3mf_name(name: &str) -> bool {
780    name.to_ascii_lowercase().ends_with(".3mf")
781}
782
783mod component_part;
784
785/// A Save's new PLM revisions, in flight ([`FileDialog::poll_owned_revisions`]).
786struct OwnedRevisions {
787    /// The assembly tab whose copies re-point to them.
788    doc: u64,
789    /// Deepest first, as planned.
790    requests: Vec<crate::document::owned_save::CheckoutRequest>,
791    /// Each request's new key and the text written there, or why not.
792    future: crate::plm::PlmFuture<Vec<Result<(String, String), String>>>,
793}
794
795/// The Save that follows a Save's new revisions ([`FileDialog::poll_owned_checkouts`]).
796struct OwnedFollowup {
797    doc: u64,
798    /// The new revisions' keys: the Save waits until the index has each.
799    keys: Vec<String>,
800    /// Frames waited for the index; past the cap the Save goes ahead and
801    /// asks about what the index still lacks.
802    polls: u32,
803}
804
805struct PendingInstructionsPdf {
806    document: u64,
807    name: String,
808    preview: bool,
809    job: brep_render::engine_state::InstructionsPdfExport,
810}
811
812/// The reusable file dialog — transient UI buffers + the current document
813/// identity; the model itself lives in `EngineState`'s history.
814pub struct FileDialog {
815    /// Demand-loaded open, scoped to the requesting tab. A newer open or
816    /// navigation cancels it so a delayed response cannot steal focus.
817    pending_open: Option<(String, u64)>,
818    /// A cold component read belongs to the assembly tab that requested it.
819    pending_insert: Option<(String, u64)>,
820    component_part: Option<component_part::Recovery>,
821    /// Reusable browser body shared by Open, Save As, and ACOMP selection.
822    explorer: FileExplorer,
823    /// In a PLM session the Open modal is the workspace browser instead (S14, D12). It
824    /// lives here so the folder the user was in is where Open starts next time.
825    workspace: crate::panels::plm_workspace::WorkspaceBrowser,
826    /// The name field buffer, used by the Save As modal.
827    name_buf: String,
828    /// Last-action status line, surfaced inside the modal.
829    pub(crate) status: String,
830    /// Whether a modal is currently open.
831    open: bool,
832    /// The open modal's mode (only meaningful while `open`).
833    mode: Mode,
834    /// Set on open so the Save As text input grabs focus on the next frame.
835    want_focus: bool,
836    /// A pending real-file pick belongs to the INSERT-COMPONENT flow (the
837    /// modal's Upload fired there): the next completed `take_import` routes to
838    /// the component insert instead of Open's load-document.
839    pending_component_import: bool,
840    /// The tab index a pending [`Mode::ConfirmClose`] will close on "Discard".
841    pending_close: Option<usize>,
842    /// A probed STEP assembly waiting on the §3.9 choice modal. `Some` means the
843    /// ENGINE is holding a parsed assembly for us (with every product's solids
844    /// resident), so every exit from that modal must end its life: import it,
845    /// discard it, or be superseded by the next upload.
846    pending_step_import: Option<PendingStepImport>,
847    /// A `.step` upload whose probe has not answered yet (see
848    /// [`PendingStepProbe`]); the status line says "reading…" meanwhile.
849    pending_step_probe: Option<PendingStepProbe>,
850    /// A step-images or offline-player export held until its geometry or tool
851    /// pictures arrive. PDFs use their own incremental job below.
852    pending_instructions_export: Option<String>,
853    /// PDF preparation is polled between frames; its document and name stay pinned.
854    instructions_pdf: Option<PendingInstructionsPdf>,
855    /// A mesh file waiting to open its reconstruction preview, with the reader
856    /// the picker routed it to (STL or 3MF).
857    pending_stl_import: Option<(MeshImportFormat, String, Vec<u8>)>,
858    /// Bumped on every SUCCESSFUL store save (plain Save / Save As / native
859    /// Save-As) — one half of the update-components staleness key (saving a
860    /// part's source must re-check the outdated badges without a reload).
861    save_generation: u64,
862    plm_save_as: Option<plm_save_as::SaveAs>,
863    /// The hand-edit prompt chose "Save as a new part" on a PLM: the Save As
864    /// it opens starts on the NEW PART destination (never on overwriting the
865    /// generated member or its revision). Consumed when that Save As opens.
866    member_new_part: bool,
867    /// Per-frame widget hit-rects for the headed verifier (wasm only).
868    hits: HashMap<String, egui::Rect>,
869    /// The KiCad import's stages after its file.
870    kicad: KicadImport,
871    /// The last folder of each [`Purpose`], taken from the store's explorer
872    /// location every frame its modal is open.
873    folders: HashMap<Purpose, String>,
874    /// The file chooser: which panel asked (its tag) and the modal's title,
875    /// the folder it was last in (on the LOCAL files), and what it delivered.
876    pick_request: Option<(String, String)>,
877    pick_folder: Option<String>,
878    picked: Option<(String, PickedFile)>,
879    /// Save As → "A new part" in a PLM session: the shell opens the PLM pane's New part
880    /// form (S7), which writes this document into the new part's first revision.
881    plm_new_part: bool,
882    /// The class Save As writes (its class chooser).
883    save_class: DocumentClass,
884    /// The armed member picker ([`Mode::PickMember`]).
885    pick_member: Option<PickMember>,
886    plm_work: Option<PlmWork>,
887    /// The armed template spin-out ([`Mode::SpinOut`]).
888    spin_out: Option<SpinOut>,
889    /// The armed hand-edit prompt ([`Mode::MemberEdit`]).
890    member_prompt: Option<MemberPrompt>,
891    /// An assembly Save waiting for the answers to its questions
892    /// ([`Mode::OwnedSave`]).
893    owned_prompt: Option<OwnedPrompt>,
894    /// The last assembly Save's owned documents, for automation: the open
895    /// question (`prompt`) or what was done (`report`).
896    owned_state: serde_json::Value,
897    /// PLM items being checked out by an assembly Save; each is written when
898    /// its checkout lands.
899    owned_checkouts: Vec<(crate::document::owned_save::CheckoutRequest, crate::plm::PlmFuture<()>)>,
900    /// The new revisions a Save asked for, made one after another deepest
901    /// first (a holder's revision names the revisions made under it): each
902    /// request with its new key and written text, or why not.
903    owned_revisions: Option<OwnedRevisions>,
904    /// The tab a Save made new revisions for: saved again once every request
905    /// has landed and the PLM index knows the new revisions, so the documents
906    /// holding them name them (and are not taken for missing files).
907    owned_followup: Option<OwnedFollowup>,
908    /// A Save of a PLM assembly held while the PLM makes an item for each part
909    /// that has no document of its own; the answer is each item's key, in the
910    /// order of [`plm_promotion_requests`].
911    /// A Save held while its PLM items are made, with where they are linked
912    /// ("your workspace folder 'X'" or "the top of your workspace").
913    /// A Save held while PLM items are made: its prompt, and the items' keys
914    /// with the workspace folder they were linked in (`(id, name)`; `None`:
915    /// the top).
916    #[allow(clippy::type_complexity)]
917    owned_promoting: Option<(OwnedPrompt, crate::plm::PlmFuture<(Vec<String>, Option<(String, String)>)>)>,
918    /// A Save held until the stored documents it writes have loaded (a PLM
919    /// session lists every revision but reads one's bytes only when asked):
920    /// `(tab id, name, keys, frames waited)`. Then the Save is planned again.
921    owned_loading: Option<(u64, String, Vec<String>, u32)>,
922    /// The held Save is being made again: plan it with what loaded, and do
923    /// not hold it a second time.
924    owned_loaded: bool,
925    /// The Save's own document, a PLM revision that was not checked out to
926    /// this user: written once its checkout lands. `(tab id, signature it
927    /// was saved at, request, checkout)`.
928    owned_top: Option<(u64, String, crate::document::owned_save::CheckoutRequest, crate::plm::PlmFuture<()>)>,
929    /// The open assemblies that place a part just saved under a new name,
930    /// waiting for the answer to "point them at the new file too?".
931    retarget: Option<Retarget>,
932    /// "Check out without asking again in this session" (the Save dialog's
933    /// checkbox): checkout rows no longer ask.
934    checkout_remembered: bool,
935    /// Tabs whose user chose "Save as a new part": they may keep editing
936    /// without being asked again; plain Save still asks.
937    member_ack: HashSet<u64>,
938    /// The last Generate's report, for the automation blob.
939    last_generate: Option<family_table::GenerateReport>,
940    /// The export the Save-file chooser holds ([`Mode::SaveFile`]), and the
941    /// chooser's own name field — its own, so the Export dialog's stem
942    /// (`name_buf`) is what the next export starts from again.
943    save_file: Option<PendingSave>,
944    save_name: String,
945}
946
947impl FileDialog {
948    /// A closed dialog with empty buffers. Document identity (name + clean
949    /// baseline) lives on [`Document`], so there is nothing to seed here.
950    pub fn new() -> Self {
951        Self {
952            pending_open: None,
953            pending_insert: None,
954            component_part: None,
955            explorer: FileExplorer::new(),
956            workspace: crate::panels::plm_workspace::WorkspaceBrowser::new(),
957            name_buf: String::new(),
958            status: String::new(),
959            open: false,
960            mode: Mode::Open,
961            want_focus: false,
962            pending_component_import: false,
963            pending_close: None,
964            pending_step_import: None,
965            pending_step_probe: None,
966            pending_instructions_export: None,
967            instructions_pdf: None,
968            pending_stl_import: None,
969            save_generation: 0,
970            plm_save_as: None,
971            member_new_part: false,
972            hits: HashMap::new(),
973            kicad: KicadImport::new(),
974            folders: HashMap::new(),
975            pick_request: None,
976            pick_folder: None,
977            picked: None,
978            plm_new_part: false,
979            save_class: DocumentClass::Normal,
980            pick_member: None,
981            plm_work: None,
982            spin_out: None,
983            member_prompt: None,
984            owned_prompt: None,
985            owned_state: serde_json::Value::Null,
986            owned_checkouts: Vec::new(),
987            owned_revisions: None,
988            owned_followup: None,
989            owned_promoting: None,
990            owned_loading: None,
991            owned_loaded: false,
992            owned_top: None,
993            retarget: None,
994            checkout_remembered: false,
995            member_ack: HashSet::new(),
996            last_generate: None,
997            save_file: None,
998            save_name: String::new(),
999        }
1000    }
1001
1002    /// The dialog's own work in flight, for the status bar's working
1003    /// indicator: a KiCad part's 3D model being parsed off the UI thread.
1004    pub fn busy_activities(&self) -> Vec<crate::panels::busy::Activity> {
1005        let mut out = Vec::new();
1006        if let Some(export) = &self.instructions_pdf {
1007            let (done, total) = export.job.progress();
1008            out.push(crate::panels::busy::Activity::new("instructionsPdf", "instructionsPdf", format!("Preparing instructions PDF: {done}/{total} pictures")));
1009        }
1010        if self.owned_loading.is_some() {
1011            out.push(crate::panels::busy::Activity::new("saveReading", "saveReading", "Reading parts from the PLM before saving"));
1012        }
1013        if self.kicad.reading() {
1014            out.push(crate::panels::busy::Activity::new("kicadRead", "kicadRead", "Reading the KiCad part's 3D model"));
1015        }
1016        out
1017    }
1018
1019    pub fn instructions_pdf_pending(&self) -> bool {
1020        self.instructions_pdf.is_some()
1021    }
1022
1023    /// The monotonic successful-save counter — the shell feeds it to the
1024    /// update-components checker as half its staleness key.
1025    pub fn save_generation(&self) -> u64 {
1026        self.save_generation
1027    }
1028
1029    // --- dispatch: a toolbar button was clicked -------------------------------
1030
1031    /// Act on a toolbar file button. New acts immediately (it adds a tab, so
1032    /// there is nothing to discard); all browsing flows use the same in-app
1033    /// modal on both platforms.
1034    pub fn dispatch(&mut self, action: FileAction, docs: &mut Documents, store: &dyn ModelStore) {
1035        if matches!(action, FileAction::New | FileAction::NewOfClass(_) | FileAction::Open | FileAction::Import) {
1036            self.pending_open = None;
1037            self.pending_insert = None;
1038        }
1039        match action {
1040            FileAction::New => self.new_document(docs, DocumentClass::Normal),
1041            FileAction::NewOfClass(class) => self.new_document(docs, class),
1042            FileAction::Open => {
1043                self.use_folder(Purpose::Models, store);
1044                self.open_modal(Mode::Open);
1045            }
1046            FileAction::Save => match docs.active().name().map(str::to_string) {
1047                // A family member is not saved over by hand: the prompt says
1048                // why and offers the two ways that are.
1049                Some(name) if self.arm_member_prompt(docs, &name, true) => {}
1050                // A PLM revision saves like any document: a checkout it
1051                // needs is a row of the one Save dialog (see `top_checkout_row`).
1052                // A named document saves straight to its name.
1053                Some(name) => {
1054                    let _ = self.save_to(docs, store, name);
1055                }
1056                // An unnamed document falls through to Save As.
1057                None => self.dispatch(FileAction::SaveAs, docs, store),
1058            },
1059            FileAction::SaveAs => {
1060                self.name_buf = Self::proposed_name(docs);
1061                self.save_class = document_class::document_class(docs.engine());
1062                self.use_folder(Purpose::Models, store);
1063                self.open_modal(Mode::SaveAs);
1064            }
1065            FileAction::Import => {
1066                self.use_folder(Purpose::Imports, browse_store(Purpose::Imports, store));
1067                self.open_modal(Mode::Import);
1068            }
1069            // The stem without the class extension, like Save As: a family's
1070            // STEP is `bolt-family.step`, not `bolt-family.fbrep.step`.
1071            FileAction::Export => {
1072                self.name_buf = Self::proposed_name(docs);
1073                self.open_modal(Mode::Export);
1074            }
1075            FileAction::ExportFlatPattern => {
1076                self.name_buf = Self::proposed_name(docs);
1077                self.open_modal(Mode::FlatPattern);
1078            }
1079            // ALWAYS the in-app modal (never the native picker directly): the
1080            // "existing library entries first" list is an in-app concept; the
1081            // modal's own Upload button drives the platform picker when the
1082            // store supports interchange.
1083            FileAction::InsertComponent => {
1084                self.use_folder(Purpose::Models, store);
1085                self.open_modal(Mode::InsertComponent);
1086            }
1087            FileAction::ImportKicad(kind) => {
1088                self.kicad.start(kind, store);
1089                self.use_folder(Purpose::Kicad, browse_store(Purpose::Kicad, store));
1090                self.open_modal(Mode::Kicad);
1091            }
1092        }
1093    }
1094
1095    /// Ask for a file (plm-cad-integration-todo S8 / S14's "Add file…"). `tag`
1096    /// names the asker, which takes the file with [`Self::take_picked_file`];
1097    /// `title` heads the chooser. Natively the chooser is this dialog's own
1098    /// explorer over the machine's files (one behaviour on every platform, and
1099    /// drivable by scripts); in the browser it is the page's file input.
1100    /// `Err` where neither exists.
1101    pub fn request_pick_file(&mut self, store: &dyn ModelStore, tag: &str, title: &str) -> Result<(), String> {
1102        self.picked = None;
1103        if let Some(local) = store.local_files() {
1104            let arrived = self.pick_folder.as_deref().is_some_and(|folder| local.browser_navigate(folder).is_ok());
1105            if !arrived {
1106                let _ = local.browser_home();
1107            }
1108            self.pick_request = Some((tag.to_string(), title.to_string()));
1109            self.status.clear();
1110            self.open_modal(Mode::PickFile);
1111            return Ok(());
1112        }
1113        store.begin_pick_file()?;
1114        self.pick_request = Some((tag.to_string(), title.to_string()));
1115        Ok(())
1116    }
1117
1118    /// The file the chooser delivered for `tag`, once.
1119    pub fn take_picked_file(&mut self, tag: &str) -> Option<PickedFile> {
1120        if self.picked.as_ref().is_some_and(|(asker, _)| asker == tag) {
1121            return self.picked.take().map(|(_, file)| file);
1122        }
1123        None
1124    }
1125
1126    /// Whether a pick for `tag` is still open (the chooser is up, or the
1127    /// browser's is and has not answered).
1128    pub fn picking(&self, tag: &str) -> bool {
1129        self.pick_request.as_ref().is_some_and(|(asker, _)| asker == tag)
1130    }
1131
1132    /// The chooser: the shared explorer over the LOCAL files, every file shown.
1133    fn show_pick_file(&mut self, ctx: &egui::Context, store: &dyn ModelStore) {
1134        let Some(local) = store.local_files() else {
1135            self.open = false;
1136            self.pick_request = None;
1137            return;
1138        };
1139        let title = self.pick_request.as_ref().map(|(_, title)| title.clone()).unwrap_or_else(|| "Choose a file".into());
1140        let modal = egui::Modal::new(egui::Id::new("brep-file-pick-file")).show(ctx, |ui| {
1141            file_explorer::dialog_body(ui, |ui| {
1142                ui.heading(&title);
1143                ui.add_space(4.0);
1144                file_explorer::dialog_footer(ui, "pickfile", |ui| {
1145                    if !self.status.is_empty() {
1146                        ui.add_space(4.0);
1147                        ui.weak(&self.status);
1148                    }
1149                });
1150                let options = FileExplorerOptions {
1151                    hit_prefix: "pickfile",
1152                    empty_label: "(this folder is empty)",
1153                    row_icon: "\u{1F5CE}",
1154                    current: None,
1155                    allow_delete: false,
1156                    allow_import: false,
1157                    import_label: "",
1158                    import_hit: "pickfile:upload",
1159                    show_cancel: true,
1160                    confirm_label: Some("Choose"),
1161                    extensions: &[],
1162                };
1163                let output = self.explorer.show_store(ui, local, options);
1164                self.record_explorer_hits(&output.hits);
1165                output
1166            })
1167        });
1168        let should_close = modal.should_close();
1169        let output = modal.inner;
1170        self.pick_folder = Some(local.browser_location());
1171        if let Some(path) = output.activated {
1172            match local.read_external_file(&path) {
1173                Some(bytes) => {
1174                    let name = std::path::Path::new(&path)
1175                        .file_name()
1176                        .map(|n| n.to_string_lossy().into_owned())
1177                        .unwrap_or_else(|| path.clone());
1178                    if let Some((tag, _)) = self.pick_request.take() {
1179                        self.status = format!("chose {name}");
1180                        self.picked = Some((tag, PickedFile { name, path: Some(path), bytes }));
1181                    }
1182                    self.open = false;
1183                }
1184                None => self.status = format!("'{path}' could not be read"),
1185            }
1186        } else if output.cancel || should_close {
1187            self.pick_request = None;
1188            self.status.clear();
1189            self.open = false;
1190        }
1191    }
1192
1193    /// Move the explorer to where `purpose` was last, or to the models folder
1194    /// the first time. A remembered folder that is gone falls back to the
1195    /// models folder too.
1196    fn use_folder(&mut self, purpose: Purpose, store: &dyn ModelStore) {
1197        let remembered = self.folders.get(&purpose).cloned();
1198        let arrived = remembered.is_some_and(|folder| store.browser_navigate(&folder).is_ok());
1199        if !arrived {
1200            if let Some(models) = models_folder(store) {
1201                let _ = store.browser_navigate(&models);
1202            }
1203        }
1204    }
1205
1206    /// Where the open modal's explorer is is where its purpose was last.
1207    fn remember_folder(&mut self, store: &dyn ModelStore) {
1208        if let Some(purpose) = self.mode.purpose() {
1209            self.folders.insert(purpose, browse_store(purpose, store).browser_location());
1210        }
1211    }
1212
1213    /// Open the modal in `mode` (and focus its input next frame).
1214    fn open_modal(&mut self, mode: Mode) {
1215        // Every opening is a new choice: the explorer is shared by every mode,
1216        // so a highlight left by the last one (Import's `Timer.kicad_sym`) would
1217        // otherwise be this one's confirm target (Insert component's).
1218        self.pending_insert = None;
1219        self.explorer.clear_selection();
1220        self.mode = mode;
1221        self.open = true;
1222        self.want_focus = true;
1223    }
1224
1225    // --- per-frame draw -------------------------------------------------------
1226
1227    /// Draw the modal (if open) and pick up any completed async import. Called
1228    /// every frame by the shell with a ctx-level handle (the modal is ctx-level,
1229    /// like the command palette).
1230    pub fn show(&mut self, ctx: &egui::Context, docs: &mut Documents, store: &dyn ModelStore) {
1231        self.hits.clear();
1232        self.poll_pending_open(ctx, docs, store);
1233        self.poll_pending_insert(ctx, docs, store);
1234        self.poll_plm_save_as(ctx, docs, store);
1235        self.poll_plm_work(ctx, docs, store);
1236        self.poll_open_workspace_pick(store);
1237        self.poll_owned_checkouts(ctx, docs, store);
1238        self.poll_owned_promotions(ctx, docs, store);
1239        self.poll_owned_loading(ctx, docs, store);
1240        if self.retarget.is_some() && !self.open {
1241            self.open_modal(Mode::Retarget);
1242        }
1243        // The browser's file chooser answered a pick.
1244        if let Some(file) = store.take_picked_file() {
1245            if let Some((tag, _)) = self.pick_request.take() {
1246                self.picked = Some((tag, PickedFile { name: file.name, path: None, bytes: file.bytes }));
1247            }
1248        }
1249
1250        // A STEP probe the runner has answered since last frame resolves into
1251        // the assembly choice or the flat import now, before drawing.
1252        self.poll_step_probe(docs.engine_mut());
1253        self.poll_instructions_pdf(ctx, docs, store);
1254        // An instructions export held for the exact solids runs once they
1255        // have landed (the busy line says "Loading exact geometry" meanwhile).
1256        if self.pending_instructions_export.is_some() && !docs.engine().topology_pending() && crate::plm::tool_pictures::pending() == 0 {
1257            if let Some(format) = self.pending_instructions_export.take() {
1258                self.export_as(docs, store, &format);
1259            }
1260        }
1261
1262        // A completed async browser upload is picked up
1263        // here, before drawing, so the model is live for this frame. Routed by
1264        // extension: a STEP file (`.step`/`.stp`) is APPENDED to the model as an
1265        // IMPORT3D feature; anything else is a `.nbrep` model document loaded
1266        // (replacing the model). The model lanes deliver an extension-less name
1267        // (web, stripped by `model_display_name`) or a stored model name, so only
1268        // real STEP files route here.
1269        if let Some(imported) = store.take_import() {
1270            // A new delivery SUPERSEDES an armed assembly choice. The modal
1271            // describes a parse this routing is about to replace (a STEP probe
1272            // drops the previous stash by contract, and a document load drops it
1273            // outright), so leaving the choice armed would offer the user a
1274            // button whose stash is already gone.
1275            self.cancel_pending_step_import(docs.engine_mut());
1276            let name = imported.name;
1277            let bytes = imported.bytes;
1278            // CAD/mesh imports target the active document. STL waits for
1279            // preview acceptance; model documents open in a separate tab.
1280            if KicadKind::of_file(&name).is_some() {
1281                self.take_kicad_file(&name, &bytes, store);
1282                return;
1283            } else if is_step_name(&name) {
1284                self.import_text(docs.engine_mut(), &name, &bytes, Self::import_step);
1285            } else if is_iges_name(&name) {
1286                self.import_text(docs.engine_mut(), &name, &bytes, Self::import_iges);
1287            } else if is_stl_name(&name) {
1288                self.stage_mesh_preview(MeshImportFormat::Stl, name, bytes);
1289            } else if is_3mf_name(&name) {
1290                self.stage_mesh_preview(MeshImportFormat::ThreeMf, name, bytes);
1291            } else if is_obj_name(&name) {
1292                self.import_obj(docs.engine_mut(), &name, &bytes);
1293            } else if std::mem::take(&mut self.pending_component_import) {
1294                // The insert-component modal's Upload fired this pick: the
1295                // chosen document becomes a parts-library entry + an instance,
1296                // NOT a new tab — or, for a family or a template, the prompt
1297                // that leads to one.
1298                match String::from_utf8(bytes) {
1299                    Ok(contents) => {
1300                        let assembly = docs.active().name().map(str::to_string);
1301                        if !self.begin_class_insert(store, &name, &contents, assembly) {
1302                            self.insert_component_document(docs.engine_mut(), &name, &contents)
1303                        }
1304                        if self.pick_member.is_some() || self.spin_out.is_some() {
1305                            return;
1306                        }
1307                    }
1308                    Err(_) => self.status = format!("open failed: {name} is not UTF-8 text"),
1309                }
1310            } else {
1311                match String::from_utf8(bytes) {
1312                    Ok(contents) => self.load_document(docs, &name, &contents, store),
1313                    Err(_) => self.status = format!("open failed: {name} is not UTF-8 text"),
1314                }
1315            }
1316            self.close_after_import();
1317        }
1318
1319        // A family member the user has just started to change raises the
1320        // hand-edit prompt (unless they already chose "Save as a new part").
1321        // On every store (integrator ruling 2026-10-03): on a PLM the
1322        // prompt's "Save as a new part" hands off to the PLM Save As with a
1323        // NEW PART preselected, so the generated member is never written
1324        // over by hand.
1325        if !self.open {
1326            self.watch_member_edit(docs);
1327        }
1328
1329        if !self.open {
1330            return;
1331        }
1332        self.remember_folder(store);
1333
1334        match self.mode {
1335            Mode::CreateComponentPart => self.show_create_component_part(ctx, docs, store),
1336            Mode::ConfirmClose => self.show_confirm_close(ctx, docs),
1337            Mode::SaveAs => self.show_save_as(ctx, docs, store),
1338            Mode::Open => self.show_open(ctx, docs, store),
1339            Mode::Import => self.show_import(ctx, docs.engine_mut(), store),
1340            Mode::Export => self.show_export(ctx, docs, store),
1341            Mode::FlatPattern => self.show_flat_pattern(ctx, docs, store),
1342            Mode::InsertComponent => self.show_insert_component(ctx, docs, store),
1343            Mode::PickMember => self.show_pick_member(ctx, docs.engine_mut(), store),
1344            Mode::SpinOut => self.show_spin_out(ctx, docs.engine_mut(), store),
1345            Mode::MemberEdit => self.show_member_edit(ctx, docs, store),
1346            Mode::OwnedSave => self.show_owned_save(ctx, docs, store),
1347            Mode::Retarget => self.show_retarget(ctx, docs),
1348            Mode::PickFile => self.show_pick_file(ctx, store),
1349            Mode::SaveFile => self.show_save_file(ctx, docs, store),
1350            Mode::StepAssembly => self.show_step_assembly(ctx, docs.engine_mut(), store),
1351            Mode::Kicad => self.show_kicad(ctx, docs.engine_mut(), store),
1352        }
1353    }
1354
1355    /// Close the modal after a completed import — unless the import ARMED the
1356    /// §3.9 assembly choice, in which case the modal stays open and switches to
1357    /// it. Both import routing sites (the async-upload poll and the Import
1358    /// modal's own pick) end here, so neither can close a dialog the probe just
1359    /// raised.
1360    fn close_after_import(&mut self) {
1361        if self.pending_step_import.is_some() {
1362            self.open_modal(Mode::StepAssembly);
1363        } else {
1364            self.open = false;
1365        }
1366    }
1367
1368    /// Abandon an armed assembly choice and the engine stash behind it. The ONE
1369    /// place the app-side pending state and the engine-side parse are dropped
1370    /// together — they are two halves of one thing, and a half-drop is either a
1371    /// dialog with no stash or solids nobody will ever consume.
1372    fn cancel_pending_step_import(&mut self, state: &mut EngineState) {
1373        if self.pending_step_import.take().is_some() {
1374            state.discard_probed_step_assembly();
1375        }
1376    }
1377
1378    /// Common CAD/mesh browser. Desktop enumerates files in the application's
1379    /// models directory; web shows the same explorer shell with an Upload action.
1380    fn show_import(
1381        &mut self,
1382        ctx: &egui::Context,
1383        state: &mut EngineState,
1384        store: &dyn ModelStore,
1385    ) {
1386        const EXTENSIONS: &[&str] =
1387            &["step", "stp", "iges", "igs", "stl", "3mf", "obj", "kicad_sym", "kicad_mod"];
1388        let modal = egui::Modal::new(egui::Id::new("brep-file-import")).show(ctx, |ui| {
1389            file_explorer::dialog_body(ui, |ui| {
1390                ui.heading("Import CAD file");
1391                ui.add_space(4.0);
1392                file_explorer::dialog_footer(ui, "import", |ui| {
1393                    if !self.status.is_empty() {
1394                        ui.add_space(4.0);
1395                        ui.weak(&self.status);
1396                    }
1397                });
1398                let options = FileExplorerOptions {
1399                    hit_prefix: "import",
1400                    empty_label: "(no STEP, IGES, STL, 3MF, OBJ, or KiCad files)",
1401                    row_icon: "\u{1F5CE}",
1402                    current: None,
1403                    allow_delete: false,
1404                    allow_import: store.supports_file_interchange(),
1405                    import_label: "Upload\u{2026}",
1406                    import_hit: "import:upload",
1407                    show_cancel: true,
1408                    confirm_label: Some("Import"),
1409                    extensions: EXTENSIONS,
1410                };
1411                let output = self.explorer.show_store(ui, browse_store(Purpose::Imports, store), options);
1412                self.record_explorer_hits(&output.hits);
1413                output
1414            })
1415        });
1416        let should_close = modal.should_close();
1417        let output = modal.inner;
1418        if let Some(name) = output.activated {
1419            match browse_store(Purpose::Imports, store).read_external_file(&name) {
1420                Some(bytes) if KicadKind::of_file(&name).is_some() => {
1421                    // Not an import yet: the KiCad dialog takes it from here.
1422                    self.take_kicad_file(&name, &bytes, store);
1423                    return;
1424                }
1425                Some(bytes) if is_step_name(&name) => {
1426                    self.import_text(state, &name, &bytes, Self::import_step)
1427                }
1428                Some(bytes) if is_iges_name(&name) => {
1429                    self.import_text(state, &name, &bytes, Self::import_iges)
1430                }
1431                Some(bytes) if is_stl_name(&name) => {
1432                    self.stage_mesh_preview(MeshImportFormat::Stl, name, bytes)
1433                }
1434                Some(bytes) if is_3mf_name(&name) => {
1435                    self.stage_mesh_preview(MeshImportFormat::ThreeMf, name, bytes)
1436                }
1437                Some(bytes) if is_obj_name(&name) => self.import_obj(state, &name, &bytes),
1438                Some(_) => self.status = format!("unsupported import file: {name}"),
1439                None => self.status = format!("import failed: '{name}' not found"),
1440            }
1441            self.close_after_import();
1442        } else if output.import {
1443            match store.begin_import_filtered(("CAD / mesh", EXTENSIONS)) {
1444                Ok(()) => {
1445                    self.status =
1446                        "choose a STEP, IGES, STL, 3MF, OBJ, or KiCad file\u{2026}".into();
1447                    self.open = false;
1448                }
1449                Err(e) => self.status = format!("import failed: {e}"),
1450            }
1451        } else if output.cancel || should_close {
1452            self.open = false;
1453        }
1454    }
1455
1456    /// A `.kicad_sym` or `.kicad_mod` arrived (picked in Import, in the KiCad
1457    /// explorer, or uploaded): the KiCad dialog reads it and goes on to its
1458    /// next stage. `name` is the file's full path on desktop, which is where a
1459    /// footprint's relative model path starts from.
1460    fn take_kicad_file(&mut self, name: &str, bytes: &[u8], store: &dyn ModelStore) {
1461        if !(self.open && self.mode == Mode::Kicad) {
1462            if let Some(kind) = KicadKind::of_file(name) {
1463                self.kicad.start(kind, store);
1464            }
1465        }
1466        self.kicad.take_file(name, bytes, &DiskFiles);
1467        self.open_modal(Mode::Kicad);
1468    }
1469
1470    /// The KiCad import: the common explorer for its file, then its own
1471    /// stages. Hit keys are `kicad:*`.
1472    fn show_kicad(&mut self, ctx: &egui::Context, state: &mut EngineState, store: &dyn ModelStore) {
1473        if !self.kicad.wants_file() {
1474            match self.kicad.show(ctx, state, store, &mut self.hits) {
1475                kicad_import::Outcome::Open => {}
1476                kicad_import::Outcome::Closed => self.open = false,
1477                kicad_import::Outcome::Imported(imported) => {
1478                    // The modal closes on the click, so the toasts are what the
1479                    // user sees: the summary, then each note.
1480                    self.status = imported.summary.clone();
1481                    state.push_notice_as(brep_render::engine_state::NoticeSeverity::Info, imported.summary);
1482                    const SHOWN: usize = 5;
1483                    let hidden = imported.notes.len().saturating_sub(SHOWN);
1484                    for note in imported.notes.into_iter().take(SHOWN) {
1485                        state.push_notice_as(brep_render::engine_state::NoticeSeverity::Warning, note);
1486                    }
1487                    if hidden > 0 {
1488                        state.push_notice_as(
1489                            brep_render::engine_state::NoticeSeverity::Warning,
1490                            format!("and {hidden} more notes on this import"),
1491                        );
1492                    }
1493                    self.open = false;
1494                }
1495            }
1496            return;
1497        }
1498        let extension = self.kicad.kind().extension();
1499        let heading = match self.kicad.kind() {
1500            KicadKind::Symbol => "Import a symbol from KiCad",
1501            KicadKind::Footprint => "Import a footprint from KiCad",
1502        };
1503        let extensions = [extension];
1504        let modal = egui::Modal::new(egui::Id::new("brep-file-kicad")).show(ctx, |ui| {
1505            file_explorer::dialog_body(ui, |ui| {
1506                ui.heading(heading);
1507                ui.add_space(4.0);
1508                file_explorer::dialog_footer(ui, "kicad", |ui| {
1509                    let status = if self.kicad.status.is_empty() { &self.status } else { &self.kicad.status };
1510                    if !status.is_empty() {
1511                        ui.add_space(4.0);
1512                        ui.weak(status);
1513                    }
1514                });
1515                let empty_label = format!("(no .{extension} files)");
1516                let options = FileExplorerOptions {
1517                    hit_prefix: "kicad",
1518                    empty_label: &empty_label,
1519                    row_icon: "\u{1F5CE}",
1520                    current: None,
1521                    allow_delete: false,
1522                    allow_import: store.supports_file_interchange(),
1523                    import_label: "Upload\u{2026}",
1524                    import_hit: "kicad:upload",
1525                    show_cancel: true,
1526                    confirm_label: Some("Open"),
1527                    extensions: &extensions,
1528                };
1529                let output = self.explorer.show_store(ui, browse_store(Purpose::Kicad, store), options);
1530                self.record_explorer_hits(&output.hits);
1531                output
1532            })
1533        });
1534        let should_close = modal.should_close();
1535        let output = modal.inner;
1536        if let Some(name) = output.activated {
1537            match browse_store(Purpose::Kicad, store).read_external_file(&name) {
1538                Some(bytes) => self.take_kicad_file(&name, &bytes, store),
1539                None => self.kicad.status = format!("import failed: '{name}' not found"),
1540            }
1541        } else if output.import {
1542            match store.begin_import_filtered(("KiCad", &extensions)) {
1543                Ok(()) => {
1544                    self.status = format!("choose a .{extension} file\u{2026}");
1545                    self.open = false;
1546                }
1547                Err(e) => self.kicad.status = format!("import failed: {e}"),
1548            }
1549        } else if output.cancel || should_close {
1550            self.open = false;
1551        }
1552    }
1553
1554    /// CLOSE tab `index` with the unsaved-changes contract: a DIRTY document
1555    /// prompts to discard first (the confirm modal); a clean one closes
1556    /// immediately. The tab strip's `\u{2715}` routes here — it is the only
1557    /// door through which unsaved work can be dropped, so it is the only one
1558    /// that asks.
1559    pub fn request_close(&mut self, docs: &mut Documents, index: usize) {
1560        let dirty = docs.get(index).is_some_and(Document::is_dirty);
1561        if dirty {
1562            self.pending_close = Some(index);
1563            self.open_modal(Mode::ConfirmClose);
1564        } else {
1565            docs.close(index);
1566        }
1567    }
1568
1569    /// The **Discard unsaved changes?** confirmation shown before closing a
1570    /// dirty document tab. Keeps the verifier's `confirm:discard` /
1571    /// `confirm:cancel` hit keys — the same two buttons, one door further on.
1572    fn show_confirm_close(&mut self, ctx: &egui::Context, docs: &mut Documents) {
1573        // A tab closed / reordered under an open prompt (there is no such path
1574        // today, but the index is only meaningful while it resolves).
1575        let Some(title) = self
1576            .pending_close
1577            .and_then(|index| docs.get(index))
1578            .map(Document::title)
1579        else {
1580            self.pending_close = None;
1581            self.open = false;
1582            return;
1583        };
1584        let mut discard = false;
1585        let mut cancel = false;
1586        let modal = egui::Modal::new(egui::Id::new("brep-file-confirm-close")).show(ctx, |ui| {
1587            ui.set_width(340.0);
1588            ui.heading("Discard unsaved changes?");
1589            ui.add_space(4.0);
1590            ui.label(format!("\"{title}\" has unsaved changes. Close it?"));
1591            ui.add_space(6.0);
1592            ui.horizontal(|ui| {
1593                let d = ui.button("Discard and close");
1594                self.hit("confirm:discard", &d);
1595                if d.clicked() {
1596                    discard = true;
1597                }
1598                let c = ui.button("Cancel");
1599                self.hit("confirm:cancel", &c);
1600                if c.clicked() {
1601                    cancel = true;
1602                }
1603            });
1604        });
1605        if discard {
1606            if let Some(index) = self.pending_close.take() {
1607                docs.close(index);
1608            }
1609            self.open = false;
1610        } else if cancel || modal.should_close() {
1611            self.pending_close = None;
1612            self.open = false;
1613        }
1614    }
1615
1616    /// The §3.9 **assembly choice**, raised when a `.step` upload's probe found a
1617    /// product structure:
1618    ///
1619    /// > **"bracket-assy.step" contains an assembly** — 7 parts, 23 instances.
1620    /// > [ Import as assembly ] [ Import as bodies ] [ Cancel ]
1621    ///
1622    /// Default (and first) is **Import as assembly**. "Import as bodies" is
1623    /// today's flat lane, unchanged. Cancel — and Esc / click-outside, which
1624    /// egui folds into `should_close` — import nothing and DROP the parse: the
1625    /// engine is holding every product's solids until one of these three lands.
1626    ///
1627    /// **Flatten sub-assemblies** (§3.9) is offered only when the file actually
1628    /// HAS sub-assemblies (`nested_depth > 1`) — on a single-level file both
1629    /// lanes produce the identical document, so a box there would teach the user
1630    /// a distinction that does not exist. Unchecked (the default) keeps the
1631    /// tree; checked flattens to leaf occurrences, which is the right answer for
1632    /// a deep or pathological file and stores a part reused across two levels
1633    /// once rather than once per level.
1634    fn show_step_assembly(
1635        &mut self,
1636        ctx: &egui::Context,
1637        state: &mut EngineState,
1638        store: &dyn ModelStore,
1639    ) {
1640        // Nothing armed means nothing to choose about (a supersession raced the
1641        // draw): close rather than render an empty prompt.
1642        let Some(pending) = self.pending_step_import.as_ref() else {
1643            self.open = false;
1644            return;
1645        };
1646        let name = pending.name.clone();
1647        let probe = pending.probe;
1648        let mut flatten = pending.flatten;
1649        let mut choice: Option<StepChoice> = None;
1650        let modal = egui::Modal::new(egui::Id::new("brep-file-step-assembly")).show(ctx, |ui| {
1651            ui.set_width(360.0);
1652            ui.heading(format!("\"{name}\" contains an assembly"));
1653            ui.add_space(4.0);
1654            ui.label(format!(
1655                "{} part{}, {} instance{}{}.",
1656                probe.parts,
1657                plural(probe.parts),
1658                probe.instances,
1659                plural(probe.instances),
1660                match probe.nested_depth {
1661                    0 | 1 => String::new(),
1662                    depth => format!(", {depth} levels deep"),
1663                }
1664            ));
1665            if probe.nested_depth > 1 {
1666                ui.add_space(4.0);
1667                let check = ui.checkbox(&mut flatten, "Flatten sub-assemblies");
1668                self.hit("stepassembly:flatten", &check);
1669                check.on_hover_text(
1670                    "Off: each sub-assembly becomes one rigid component you can \
1671                     expand in the structure tree.\nOn: every part is placed \
1672                     directly in this document at its world position.",
1673                );
1674            }
1675            ui.add_space(6.0);
1676            ui.horizontal(|ui| {
1677                let a = ui.button("Import as assembly");
1678                self.hit("stepassembly:assembly", &a);
1679                if a.clicked() {
1680                    choice = Some(StepChoice::Assembly);
1681                }
1682                let b = ui.button("Import as bodies");
1683                self.hit("stepassembly:bodies", &b);
1684                if b.clicked() {
1685                    choice = Some(StepChoice::Bodies);
1686                }
1687                let c = ui.button("Cancel");
1688                self.hit("stepassembly:cancel", &c);
1689                if c.clicked() {
1690                    choice = Some(StepChoice::Cancel);
1691                }
1692            });
1693        });
1694        // The box must survive the frames between the tick and the click.
1695        if let Some(pending) = self.pending_step_import.as_mut() {
1696            pending.flatten = flatten;
1697        }
1698        // Esc / click-outside is a Cancel, not a no-op: the stash must not
1699        // outlive the prompt that was going to consume it.
1700        let choice = choice.or_else(|| modal.should_close().then_some(StepChoice::Cancel));
1701        let Some(choice) = choice else { return };
1702        match choice {
1703            StepChoice::Assembly => self.step_assembly_import(state, store),
1704            StepChoice::Bodies => self.step_assembly_bodies(state),
1705            StepChoice::Cancel => self.step_assembly_cancel(state),
1706        }
1707        self.open = false;
1708    }
1709
1710    /// Save As asked for a new PLM part: the shell opens the PLM pane's New part form.
1711    /// Once: true the frame after it was chosen.
1712    pub fn take_plm_new_part(&mut self) -> bool {
1713        std::mem::take(&mut self.plm_new_part)
1714    }
1715
1716    /// The **Save As** name prompt.
1717    fn show_save_as(&mut self, ctx: &egui::Context, docs: &mut Documents, store: &dyn ModelStore) {
1718        // A PLM session saves as a new part or a new revision, not under a file name.
1719        if let Some(client) = store.plm_client() {
1720            self.show_save_as_plm(ctx, docs, store, client);
1721            return;
1722        }
1723        let current = docs.active().name().map(str::to_string);
1724        let mut do_save = false;
1725        let mut cancel = false;
1726        let modal = egui::Modal::new(egui::Id::new("brep-file-saveas")).show(ctx, |ui| {
1727            file_explorer::dialog_body(ui, |ui| {
1728                ui.heading("Save model as");
1729                ui.add_space(4.0);
1730                // The name field and its buttons are the dialog's footer: pinned
1731                // under the browser, which fills whatever is left.
1732                file_explorer::dialog_footer(ui, "saveas", |ui| {
1733                    ui.label("File name");
1734                    let field = ui.add(
1735                        egui::TextEdit::singleline(&mut self.name_buf).margin(crate::viewport::touch::text_edit_margin(ui)).min_size(crate::viewport::touch::target_size(ui.ctx(), egui::Vec2::ZERO))
1736                            .hint_text("model name")
1737                            .desired_width(f32::INFINITY),
1738                    );
1739                    self.hit("field:name", &field);
1740                    if self.want_focus {
1741                        field.request_focus();
1742                        self.want_focus = false;
1743                    }
1744                    let enter =
1745                        field.lost_focus() && ui.input(|i| i.key_pressed(egui::Key::Enter));
1746                    // The class the file is saved as. A part's class is fixed
1747                    // for its life, so Save As is the one place it is chosen:
1748                    // saving a part as a Family or a Template makes a new file
1749                    // of that class.
1750                    ui.horizontal(|ui| {
1751                        ui.label("Save as");
1752                        // One dropdown (the form's enum field): saveas:class, its
1753                        // open entries saveas:class#<slug>.
1754                        let texts: Vec<String> = DocumentClass::ALL.iter().map(|class| format!("{} ({})", class.label(), class.extension())).collect();
1755                        let choices: Vec<(DocumentClass, &str, &str)> =
1756                            DocumentClass::ALL.iter().zip(&texts).map(|(class, text)| (*class, class.slug(), text.as_str())).collect();
1757                        crate::form::enum_dropdown(ui, "saveas:class", &choices, &mut self.save_class, None, Some(&mut self.hits));
1758                    });
1759                    ui.add_space(6.0);
1760                    ui.horizontal(|ui| {
1761                        let save = ui.button("Save");
1762                        self.hit("save", &save);
1763                        if save.clicked() || enter {
1764                            do_save = true;
1765                        }
1766                        let c = ui.button("Cancel");
1767                        self.hit("cancel", &c);
1768                        if c.clicked() {
1769                            cancel = true;
1770                        }
1771                    });
1772                    if !self.status.is_empty() {
1773                        ui.add_space(4.0);
1774                        ui.weak(&self.status);
1775                    }
1776                });
1777                let options = FileExplorerOptions {
1778                    hit_prefix: "saveas:file",
1779                    empty_label: "(no saved models)",
1780                    row_icon: "\u{1F5CE}",
1781                    current: current.as_deref(),
1782                    allow_delete: false,
1783                    allow_import: false,
1784                    import_label: "",
1785                    import_hit: "saveas:upload",
1786                    show_cancel: false,
1787                    confirm_label: None,
1788                    extensions: MODEL_EXTENSIONS,
1789                };
1790                let output = self.explorer.show_store(ui, store, options);
1791                self.record_explorer_hits(&output.hits);
1792                // Selecting (or double-clicking) a stored model fills the name
1793                // field so it can be overwritten; `picked`/`activated` are
1794                // one-shot so it never clobbers a name the user then types. The
1795                // field itself was drawn above, so the explorer asks for the
1796                // repaint that shows the new name.
1797                if let Some(name) = output.picked.or(output.activated) {
1798                    self.name_buf =
1799                        document_class::strip_class_extension(&model_display_name(&name)).to_string();
1800                    if let Some(class) = DocumentClass::of_name(&name) {
1801                        self.save_class = class;
1802                    }
1803                }
1804            })
1805        });
1806        if do_save {
1807            let name = self.save_class.file_name(self.name_buf.trim());
1808            if self.save_to_browser(docs, store, name) {
1809                self.open = false;
1810            }
1811        } else if cancel || modal.should_close() {
1812            self.open = false;
1813        }
1814    }
1815
1816    /// The **Export** chooser: pick a format (STEP / IGES / STL / OBJ / GLB /
1817    /// 3MF) and write the current model to the user's filesystem under
1818    /// `<name>.<ext>` through the store's format-typed interchange. STEP and
1819    /// IGES serialize the exact NURBS topology; STL, OBJ, GLB and 3MF are the
1820    /// display mesh (ASCII facets, one indexed object, binary glTF 2.0 with a
1821    /// mesh per body, and a 3MF package with an object per body and the
1822    /// document's colours as base materials).
1823    /// No solids → a clear status line,
1824    /// nothing written. A second row offers the BOM (CSV / JSON), enabled only
1825    /// while the document has components or harness wires.
1826    fn show_export(&mut self, ctx: &egui::Context, docs: &mut Documents, store: &dyn ModelStore) {
1827        // BOM gating: `component_ids` scans the history (cheap, no kernel
1828        // session), and a harness wire is a BOM line of its own, so a
1829        // harness-only document exports too. A rolled-back/failed ACOMP can
1830        // still enable the buttons — the export's own loud "no components or
1831        // harness wires" error covers that gap.
1832        let has_bom = !docs.engine().component_ids().is_empty()
1833            || !docs.engine().wire_harness_state().connections.is_empty();
1834        // The drawing sheets of THIS document: the sheet button writes the open
1835        // one (else the first), so it is offered only when there is one to
1836        // write — the BOM row's gating, for the same reason.
1837        let has_sheets = !docs.engine().sheet_state().sheets.is_empty();
1838        let has_instructions = docs.engine().is_instructions();
1839        // The board's manufacturing files: offered once the PCB has parts on
1840        // its board, the BOM row's gating for the same reason.
1841        let has_board = super::fabrication_export::has_board(docs.engine());
1842        let has_schematic = super::fabrication_export::has_schematic(docs.engine());
1843        let mut chosen: Option<&'static str> = None;
1844        let mut bom_chosen: Option<&'static str> = None;
1845        let mut fab_chosen: Option<super::fabrication_export::Output> = None;
1846        let mut cancel = false;
1847        let modal = egui::Modal::new(egui::Id::new("brep-file-export")).show(ctx, |ui| {
1848            // Wide enough for the 3D formats on one line; each group wraps
1849            // rather than widening the modal (lane U audit item 7).
1850            ui.set_width(440.0);
1851            ui.heading("Export model");
1852            ui.add_space(4.0);
1853            Self::preview_export_line(ui, docs);
1854            let field = ui.add(
1855                egui::TextEdit::singleline(&mut self.name_buf).margin(crate::viewport::touch::text_edit_margin(ui)).min_size(crate::viewport::touch::target_size(ui.ctx(), egui::Vec2::ZERO))
1856                    .hint_text("file name")
1857                    .desired_width(f32::INFINITY),
1858            );
1859            self.hit("field:name", &field);
1860            if self.want_focus {
1861                field.request_focus();
1862                self.want_focus = false;
1863            }
1864            // One headed, wrapping row per kind of output, and Cancel on its
1865            // own row below them all — it used to sit among the 3D formats,
1866            // with "Sheets + 3D (PDF)" orphaned on a line of its own.
1867            let group = |ui: &mut egui::Ui, title: &str| {
1868                ui.add_space(6.0);
1869                ui.label(egui::RichText::new(title).weak());
1870            };
1871            group(ui, "3D model");
1872            ui.horizontal_wrapped(|ui| {
1873                let step = ui.button("STEP (.step)");
1874                self.hit("export:step", &step);
1875                if step.clicked() {
1876                    chosen = Some("step");
1877                }
1878                let iges = ui.button("IGES (.igs)");
1879                self.hit("export:iges", &iges);
1880                if iges.clicked() {
1881                    chosen = Some("iges");
1882                }
1883                let stl = ui.button("STL (.stl)");
1884                self.hit("export:stl", &stl);
1885                if stl.clicked() {
1886                    chosen = Some("stl");
1887                }
1888                let obj = ui.button("OBJ (.obj)");
1889                self.hit("export:obj", &obj);
1890                if obj.clicked() {
1891                    chosen = Some("obj");
1892                }
1893                let glb = ui.button("GLB (.glb)");
1894                self.hit("export:glb", &glb);
1895                if glb.clicked() {
1896                    chosen = Some("glb");
1897                }
1898                // The slicer format: the display mesh as a 3MF package, one
1899                // object per body, with the document's colours carried as
1900                // base materials so a multi-colour print arrives painted.
1901                let three_mf = ui
1902                    .button("3MF (.3mf)")
1903                    .on_hover_text("For slicers: one object per body, with the model's colours");
1904                self.hit("export:3mf", &three_mf);
1905                if three_mf.clicked() {
1906                    chosen = Some("3mf");
1907                }
1908                // The full model RECIPE (`.nbrep`) — for saving a document to
1909                // disk / sharing a failing model for a bug report. Re-openable via
1910                // Open / Import.
1911                let json = ui.button("Model (.nbrep)");
1912                self.hit("export:json", &json);
1913                if json.clicked() {
1914                    chosen = Some("json");
1915                }
1916            });
1917            group(ui, "Bill of materials");
1918            ui.horizontal_wrapped(|ui| {
1919                // The BOM: one row per parts-library entry with the live
1920                // instance count, then one per harness wire with its cut
1921                // length. Disabled while the document has neither.
1922                let csv = ui.add_enabled(has_bom, egui::Button::new("BOM (CSV)"));
1923                self.hit("export:bomcsv", &csv);
1924                if csv.clicked() {
1925                    bom_chosen = Some("csv");
1926                }
1927                let json = ui.add_enabled(has_bom, egui::Button::new("BOM (JSON)"));
1928                self.hit("export:bomjson", &json);
1929                if json.clicked() {
1930                    bom_chosen = Some("json");
1931                }
1932            });
1933            group(ui, "Drawing");
1934            ui.horizontal_wrapped(|ui| {
1935                // The DRAWING SHEET: the paper as an SVG of lines and text.
1936                // Not the sheet-METAL flat pattern, which is its own dialog.
1937                let sheet = ui
1938                    .add_enabled(has_sheets, egui::Button::new("Sheet (SVG)"))
1939                    .on_disabled_hover_text("Add a drawing sheet in the Drawing workbench first");
1940                self.hit("export:sheetsvg", &sheet);
1941                if sheet.clicked() {
1942                    chosen = Some("sheetsvg");
1943                }
1944                // EVERY sheet as one PDF: a page per sheet, in sheet order, each
1945                // at its own paper size — the drawing set, not the page on
1946                // screen. The SVG beside it stays one file per sheet.
1947                let pdf = ui
1948                    .add_enabled(has_sheets, egui::Button::new("Sheets (PDF)"))
1949                    .on_hover_text("Every drawing sheet of the document as one PDF, a page per sheet")
1950                    .on_disabled_hover_text("Add a drawing sheet in the Drawing workbench first");
1951                self.hit("export:sheetpdf", &pdf);
1952                if pdf.clicked() {
1953                    chosen = Some("sheetpdf");
1954                }
1955                // The same drawing set with the LIVE 3D model: a 3D box over
1956                // every placement marked "Live 3D in PDF", and a last page
1957                // given over to the model with a button per saved PMI view.
1958                // Its own file, so the plain drawing set is never replaced.
1959                let pdf3d = ui
1960                    .add_enabled(has_sheets, egui::Button::new("Sheets + 3D (PDF)"))
1961                    .on_hover_text("The drawing set with the live 3D model: Acrobat and Foxit turn and zoom it and switch PMI views; other PDF readers show the drawings")
1962                    .on_disabled_hover_text("Add a drawing sheet in the Drawing workbench first");
1963                self.hit("export:sheetpdf3d", &pdf3d);
1964                if pdf3d.clicked() {
1965                    chosen = Some("sheetpdf3d");
1966                }
1967            });
1968            group(ui, "Electronics");
1969            ui.horizontal_wrapped(|ui| {
1970                // The PCB's manufacturing files. The zip is what a board house
1971                // takes; the two CSVs are also inside it, and on their own for
1972                // an assembler that asks for them separately.
1973                use super::fabrication_export::Output;
1974                let no_board = "Place the schematic's parts on the PCB board first";
1975                for (key, label, hover, output) in [
1976                    ("export:fabrication", "Fabrication (zip)", "Gerber X2 layers, Excellon drills, pick and place, the parts BOM and a README of the layers, in one zip for the board house", Output::Bundle),
1977                    ("export:pickplace", "Pick & place (CSV)", "One row per placed part: Designator, Val, Package, Mid X, Mid Y, Rotation, Layer (JLCPCB's columns), in mm from the board's lower-left corner", Output::PickPlace),
1978                    ("export:ecadbom", "Parts BOM (CSV)", "The electronic parts: Comment (the value), Designator, Footprint, Quantity (JLCPCB's columns), one line per value and footprint", Output::Bom),
1979                    ("export:kicadnet", "Netlist (KiCad)", "The PCB schematic's nets as a KiCad .net file, the netlist Pcbnew and other tools read: every part with its value and footprint, every net with its pins, under the names the Connectivity panel lists", Output::Netlist),
1980                ] {
1981                    let (enabled, why) = match output {
1982                        Output::Netlist => (has_schematic, "Put parts on the PCB schematic first"),
1983                        _ => (has_board, no_board),
1984                    };
1985                    let button = ui
1986                        .add_enabled(enabled, egui::Button::new(label))
1987                        .on_hover_text(hover)
1988                        .on_disabled_hover_text(why);
1989                    self.hit(key, &button);
1990                    if button.clicked() {
1991                        fab_chosen = Some(output);
1992                    }
1993                }
1994            });
1995            group(ui, "Instructions");
1996            ui.horizontal_wrapped(|ui| {
1997                // The printed instructions on the page template the Instructions
1998                // pane sets, and every step's picture as PNG and SVG in one zip.
1999                let not_instructions = "Open manufacturing instructions first (New from assembly in the Instructions workbench)";
2000                let pdf = ui
2001                    .add_enabled(has_instructions, egui::Button::new("PDF"))
2002                    .on_hover_text("The cover, the parts list, the tools list and the steps, on the page template set in the Instructions pane")
2003                    .on_disabled_hover_text(not_instructions);
2004                self.hit("export:instructionspdf", &pdf);
2005                if pdf.clicked() {
2006                    chosen = Some("instructionspdf");
2007                }
2008                let images = ui
2009                    .add_enabled(has_instructions, egui::Button::new("Step images"))
2010                    .on_hover_text("Every step's picture as PNG and SVG, in one zip")
2011                    .on_disabled_hover_text(not_instructions);
2012                self.hit("export:instructionsimages", &images);
2013                if images.clicked() {
2014                    chosen = Some("instructionsimages");
2015                }
2016                let html = ui
2017                    .add_enabled(has_instructions, egui::Button::new("Player (HTML)"))
2018                    .on_hover_text("The interactive player in one self-contained page: Next/Back, a live 3D view per step and a step checklist, with no server")
2019                    .on_disabled_hover_text(not_instructions);
2020                self.hit("export:instructionshtml", &html);
2021                if html.clicked() {
2022                    chosen = Some("instructionshtml");
2023                }
2024            });
2025            if !self.status.is_empty() {
2026                ui.add_space(4.0);
2027                ui.weak(&self.status);
2028            }
2029            ui.add_space(6.0);
2030            ui.separator();
2031            let c = ui.button("Cancel");
2032            self.hit("cancel", &c);
2033            if c.clicked() {
2034                cancel = true;
2035            }
2036        });
2037        // A delivered export closes this dialog; one the Save-file chooser
2038        // took over has switched the modal to the chooser, which stays up.
2039        if let Some(format) = chosen {
2040            if self.export_as(docs, store, format) && self.mode == Mode::Export {
2041                self.open = false;
2042            }
2043        } else if let Some(format) = bom_chosen {
2044            if self.export_bom_as(docs, store, format) && self.mode == Mode::Export {
2045                self.open = false;
2046            }
2047        } else if let Some(output) = fab_chosen {
2048            if self.export_fabrication_as(docs, store, output) && self.mode == Mode::Export {
2049                self.open = false;
2050            }
2051        } else if cancel || modal.should_close() {
2052            self.open = false;
2053        }
2054    }
2055
2056    /// The **Flat pattern** chooser: pick a 2D vector format (DXF R12 / SVG) and
2057    /// write the sheet-metal body's unfolded flat pattern to the user's filesystem
2058    /// under `<name>.<ext>`. The unfold runs TRANSIENTLY in the engine (no feature,
2059    /// no history change). A part with no sheet-metal body reports it in the status
2060    /// line AND queues a toast (see [`Self::export_flat_pattern_as`]).
2061    fn show_flat_pattern(
2062        &mut self,
2063        ctx: &egui::Context,
2064        docs: &mut Documents,
2065        store: &dyn ModelStore,
2066    ) {
2067        let mut chosen: Option<&'static str> = None;
2068        let mut cancel = false;
2069        let modal = egui::Modal::new(egui::Id::new("brep-file-flatpattern")).show(ctx, |ui| {
2070            ui.set_width(340.0);
2071            ui.heading("Export flat pattern");
2072            ui.add_space(2.0);
2073            Self::preview_export_line(ui, docs);
2074            ui.weak("Unfolds the sheet-metal body to a 2D vector file.");
2075            ui.add_space(4.0);
2076            let field = ui.add(
2077                egui::TextEdit::singleline(&mut self.name_buf).margin(crate::viewport::touch::text_edit_margin(ui)).min_size(crate::viewport::touch::target_size(ui.ctx(), egui::Vec2::ZERO))
2078                    .hint_text("file name")
2079                    .desired_width(f32::INFINITY),
2080            );
2081            self.hit("field:name", &field);
2082            if self.want_focus {
2083                field.request_focus();
2084                self.want_focus = false;
2085            }
2086            ui.add_space(6.0);
2087            ui.horizontal(|ui| {
2088                let dxf = ui.button("DXF (.dxf)");
2089                self.hit("flat:dxf", &dxf);
2090                if dxf.clicked() {
2091                    chosen = Some("dxf");
2092                }
2093                let svg = ui.button("SVG (.svg)");
2094                self.hit("flat:svg", &svg);
2095                if svg.clicked() {
2096                    chosen = Some("svg");
2097                }
2098                let c = ui.button("Cancel");
2099                self.hit("cancel", &c);
2100                if c.clicked() {
2101                    cancel = true;
2102                }
2103            });
2104            if !self.status.is_empty() {
2105                ui.add_space(4.0);
2106                ui.weak(&self.status);
2107            }
2108        });
2109        if let Some(format) = chosen {
2110            if self.export_flat_pattern_as(docs, store, format) && self.mode == Mode::FlatPattern {
2111                self.open = false;
2112            }
2113        } else if cancel || modal.should_close() {
2114            self.open = false;
2115        }
2116    }
2117
2118    /// The **Open** browser: the saved-model list (+ Upload where the platform
2119    /// supports real-file interchange).
2120    fn show_open(&mut self, ctx: &egui::Context, docs: &mut Documents, store: &dyn ModelStore) {
2121        // A PLM session opens from the user's workspace, not from a list of store keys.
2122        if let Some(client) = store.plm_client() {
2123            self.show_open_workspace(ctx, docs, store, client);
2124            return;
2125        }
2126        let current = docs.active().name().map(str::to_string);
2127        let modal = egui::Modal::new(egui::Id::new("brep-file-open")).show(ctx, |ui| {
2128            file_explorer::dialog_body(ui, |ui| {
2129                ui.heading("Open model");
2130                ui.add_space(4.0);
2131                file_explorer::dialog_footer(ui, "open", |ui| {
2132                    if !self.status.is_empty() {
2133                        ui.add_space(4.0);
2134                        ui.weak(&self.status);
2135                    }
2136                });
2137                let mut options = FileExplorerOptions::open(current.as_deref());
2138                options.allow_import = store.supports_file_interchange();
2139                let output = self.explorer.show_store(ui, store, options);
2140                self.record_explorer_hits(&output.hits);
2141                output
2142            })
2143        });
2144        let should_close = modal.should_close();
2145        let output = modal.inner;
2146        if let Some(name) = output.activated {
2147            self.open_document(docs, store, &name);
2148            self.open = false;
2149        } else if let Some(name) = output.remove {
2150            let _ = store.remove(&name);
2151            self.status = format!("removed {name}");
2152            // A tab holding the removed document keeps its content but loses its
2153            // file: it becomes an untitled document, so a later Save asks where
2154            // to put it rather than silently recreating what was deleted.
2155            if let Some(index) = docs.index_of(&name) {
2156                if let Some(doc) = docs.get_mut(index) {
2157                    doc.set_name(None);
2158                }
2159            }
2160        } else if output.import {
2161            // Fire the platform picker; the file arrives via take_import() and is
2162            // loaded on a later frame (the modal closes now).
2163            match store.begin_import() {
2164                Ok(()) => {
2165                    self.status = "choose a file…".into();
2166                    self.open = false;
2167                }
2168                Err(e) => self.status = format!("import failed: {e}"),
2169            }
2170        } else if output.cancel || should_close {
2171            self.open = false;
2172        }
2173    }
2174
2175    /// The **Open** browser in a PLM session: the workspace (S14) — folders of links and
2176    /// files, other users' workspaces read-only, and Open by number. A link opens its
2177    /// part's document. Workspace changes are linked to the web app; downloads
2178    /// go out through the store's export lane.
2179    fn show_open_workspace(
2180        &mut self,
2181        ctx: &egui::Context,
2182        docs: &mut Documents,
2183        store: &dyn ModelStore,
2184        client: std::rc::Rc<crate::plm::client::PlmClient>,
2185    ) {
2186        use crate::panels::plm_workspace::{document_name, load_pins, save_pins, PlmWorkspaces};
2187        let plm = PlmWorkspaces { client };
2188        if self.workspace.pins.is_none() {
2189            self.workspace.pins = Some(load_pins(store));
2190        }
2191        self.workspace.browse_only = true;
2192        let mut cancel = false;
2193        let modal = egui::Modal::new(egui::Id::new("brep-file-open")).show(ctx, |ui| {
2194            file_explorer::dialog_body(ui, |ui| {
2195                ui.heading("Open from the PLM");
2196                if let Some(url) = crate::panels::plm_host::web_url(store, "#/workspace") {
2197                    crate::panels::plm_host::web_link(ui, "Manage workspace in PLM", url);
2198                }
2199                ui.add_space(4.0);
2200                file_explorer::dialog_footer(ui, "open", |ui| {
2201                    if !self.status.is_empty() {
2202                        ui.add_space(4.0);
2203                        ui.weak(&self.status);
2204                    }
2205                    let button = ui.button("Cancel");
2206                    self.hits.insert("open:cancel".into(), button.rect);
2207                    cancel = button.clicked();
2208                });
2209                self.workspace.show(ui, &plm, "open:")
2210            })
2211        });
2212        let hits: Vec<(String, egui::Rect)> = self.workspace.hits.iter().map(|(k, r)| (k.clone(), *r)).collect();
2213        self.record_explorer_hits(&hits);
2214        let should_close = modal.should_close();
2215        let outcome = modal.inner;
2216        if let Some(pins) = &outcome.pins {
2217            if let Err(e) = save_pins(store, pins) {
2218                self.status = format!("could not save the pinned folders: {e}");
2219            }
2220        }
2221        if let Some((name, bytes)) = outcome.downloaded {
2222            self.status = match store.export_file_named_bytes(&name, &bytes) {
2223                Ok(()) => format!("saved {name}"),
2224                Err(e) => format!("could not save {name}: {e}"),
2225            };
2226        }
2227        if outcome.pick_file {
2228            // The chooser takes the modal over; the file comes back to
2229            // `poll_open_workspace_pick`, which reopens Open on the folder.
2230            if let Err(error) = self.request_pick_file(store, PICK_OPEN_WORKSPACE, "Add a file to this folder") {
2231                self.status = error;
2232            }
2233            return;
2234        }
2235        if let Some(key) = outcome.open {
2236            let name = document_name(store, &key);
2237            self.open_document(docs, store, &name);
2238            self.open = false;
2239        } else if cancel || should_close {
2240            self.open = false;
2241        }
2242    }
2243
2244    /// A file picked for the Open modal's workspace: added to the folder shown,
2245    /// and the Open modal comes back.
2246    fn poll_open_workspace_pick(&mut self, store: &dyn ModelStore) {
2247        let Some(file) = self.take_picked_file(PICK_OPEN_WORKSPACE) else { return };
2248        if let Some(client) = store.plm_client() {
2249            let plm = crate::panels::plm_workspace::PlmWorkspaces { client };
2250            self.workspace.add_file(&plm, &file.name, file.bytes);
2251            self.open_modal(Mode::Open);
2252        }
2253    }
2254
2255    /// The **Insert component** selector: the EXISTING parts-library entries
2256    /// first (instant re-insert — no store read), then the model store's
2257    /// document list, + Upload where the platform supports real-file
2258    /// interchange. REUSES the Open modal's machinery (the same list +
2259    /// `take_import` poll), routed to the engine's insert flow.
2260    fn show_insert_component(
2261        &mut self,
2262        ctx: &egui::Context,
2263        docs: &mut Documents,
2264        store: &dyn ModelStore,
2265    ) {
2266        let library = docs.engine_mut().parts_library_names();
2267        let modal = egui::Modal::new(egui::Id::new("brep-file-insert-component")).show(ctx, |ui| {
2268            file_explorer::dialog_body(ui, |ui| {
2269                ui.heading("Insert component");
2270                ui.add_space(4.0);
2271                file_explorer::dialog_footer(ui, "insert", |ui| {
2272                    if !self.status.is_empty() {
2273                        ui.add_space(4.0);
2274                        ui.weak(&self.status);
2275                    }
2276                });
2277                let mut chosen_library = None;
2278                if !library.is_empty() {
2279                    ui.weak("In this document (parts library)");
2280                    for name in &library {
2281                        let row = ui.add_sized(
2282                            egui::vec2(ui.available_width(), 18.0),
2283                            crate::icon_text::icon_button(ui, &format!("\u{25A3} {name}"))
2284                                .frame(false),
2285                        );
2286                        self.hit(&format!("insert:lib:{name}"), &row);
2287                        if row.clicked() {
2288                            chosen_library = Some(name.clone());
2289                        }
2290                    }
2291                    ui.add_space(4.0);
2292                }
2293                let options = FileExplorerOptions {
2294                    hit_prefix: "insert:model",
2295                    empty_label: "(no saved models)",
2296                    row_icon: "\u{1F5CE}",
2297                    current: None,
2298                    allow_delete: false,
2299                    allow_import: store.supports_file_interchange(),
2300                    import_label: "Upload\u{2026}",
2301                    import_hit: "insert:upload",
2302                    show_cancel: true,
2303                    confirm_label: Some("Insert"),
2304                    extensions: INSERT_EXTENSIONS,
2305                };
2306                let output = self.explorer.show_store(ui, store, options);
2307                self.record_explorer_hits(&output.hits);
2308                (chosen_library, output)
2309            })
2310        });
2311        let should_close = modal.should_close();
2312        let (chosen_library, output) = modal.inner;
2313        if let Some(part_name) = chosen_library {
2314            // An already-inserted library part: skip the store read entirely.
2315            match docs.engine_mut().insert_component(ComponentInsert::Existing { part_name: &part_name }) {
2316                Ok(id) => {
2317                    self.status = format!("inserted {part_name} ({id})");
2318                    self.open = false;
2319                }
2320                Err(e) => self.status = format!("insert failed: {e}"),
2321            }
2322        } else if let Some(name) = output.activated.filter(|name| {
2323            // Only a file the list OFFERS is a component: a part document. A
2324            // highlight left by another purpose (Import's `.kicad_sym`) is not
2325            // one, and reached the parts library as a non-document.
2326            let listed = store
2327                .browser_entries(INSERT_EXTENSIONS)
2328                .iter()
2329                .any(|entry| !entry.is_dir && entry.identity == *name);
2330            if !listed {
2331                self.status = format!(
2332                    "insert refused: {name} is not a part, family or template document (.nbrep / .fbrep / .tbrep)"
2333                );
2334            }
2335            listed
2336        }) {
2337            self.request_component_document(docs, store, &name);
2338            if self.pending_insert.is_some() {
2339                ctx.request_repaint_after(std::time::Duration::from_millis(100));
2340            }
2341        } else if output.import {
2342            match store.begin_import() {
2343                Ok(()) => {
2344                    // The picked file routes to the component insert (not Open).
2345                    self.pending_component_import = true;
2346                    self.status = "choose a part file…".into();
2347                    self.open = false;
2348                }
2349                Err(e) => self.status = format!("insert failed: {e}"),
2350            }
2351        } else if output.cancel || should_close {
2352            self.open = false;
2353        }
2354    }
2355
2356    fn request_component_document(&mut self, docs: &mut Documents, store: &dyn ModelStore, name: &str) {
2357        self.pending_insert = None;
2358        match crate::store::read_now(store, name) {
2359            crate::store::ReadNow::Ready(contents) => self.finish_component_document(docs, store, name, &contents),
2360            crate::store::ReadNow::Loading => {
2361                self.pending_insert = Some((name.to_owned(), docs.active_id()));
2362                self.status = format!("'{name}' is still loading");
2363            }
2364            crate::store::ReadNow::Absent => self.status = format!("insert failed: '{name}' not found"),
2365        }
2366    }
2367
2368    fn finish_component_document(&mut self, docs: &mut Documents, store: &dyn ModelStore, name: &str, contents: &str) {
2369        let assembly = docs.active().name().map(str::to_string);
2370        if !self.begin_class_insert(store, name, contents, assembly) {
2371            self.insert_component_document(docs.engine_mut(), name, contents);
2372            self.open = false;
2373        }
2374    }
2375
2376    fn poll_pending_insert(&mut self, ctx: &egui::Context, docs: &mut Documents, store: &dyn ModelStore) {
2377        let Some((name, requester)) = self.pending_insert.take() else { return };
2378        if !self.open || self.mode != Mode::InsertComponent || docs.active_id() != requester {
2379            return;
2380        }
2381        match crate::store::read_now(store, &name) {
2382            crate::store::ReadNow::Ready(contents) => self.finish_component_document(docs, store, &name, &contents),
2383            crate::store::ReadNow::Loading => {
2384                self.pending_insert = Some((name, requester));
2385                ctx.request_repaint_after(std::time::Duration::from_millis(100));
2386            }
2387            crate::store::ReadNow::Absent => self.status = format!("insert failed: '{name}' not found"),
2388        }
2389    }
2390
2391    /// Insert a part DOCUMENT as an assembly component: library-add (dedup by
2392    /// sourceKey + content signature; the RETURNED effective name is what the
2393    /// instance references) + an ACOMP feature, through the engine's one insert
2394    /// flow. The first instance of an empty assembly is written `isFixed:true`.
2395    /// `sourceSignature` is written with [`document_signature`] — the ONE
2396    /// signature fn — so the update-components comparison reads a freshly
2397    /// inserted, unchanged part as up-to-date.
2398    fn insert_component_document(&mut self, state: &mut EngineState, name: &str, contents: &str) {
2399        self.insert_component_named(state, name, &model_display_name(name), contents);
2400    }
2401
2402    /// [`Self::insert_component_document`] under a given name: a PLM member
2403    /// is named by its part number, which its key does not carry.
2404    fn insert_component_named(&mut self, state: &mut EngineState, name: &str, display: &str, contents: &str) {
2405        let display = display.to_string();
2406        match state.insert_component(ComponentInsert::New {
2407            name: &display,
2408            source_key: name,
2409            source_signature: &document_signature(contents),
2410            document_json: contents,
2411        }) {
2412            Ok(id) => self.status = format!("inserted {display} ({id})"),
2413            Err(e) => self.status = format!("insert failed: {e}"),
2414        }
2415    }
2416
2417    // --- model operations -----------------------------------------------------
2418
2419    /// The name to save under: the field if non-empty, else the active
2420    /// document's display name, else its untitled name (`"untitled"`, `"untitled 2"`, …).
2421    fn effective_name(&self, docs: &Documents) -> String {
2422        let field = self.name_buf.trim();
2423        if !field.is_empty() {
2424            field.to_string()
2425        } else {
2426            Self::proposed_name(docs)
2427        }
2428    }
2429
2430    /// The name Save As and every export start from: the active document's
2431    /// stem, or, for an instructions tab not saved yet, the instructions'
2432    /// own title (`valve`), so its player, PDF and pictures are
2433    /// `valve.html`, `valve.pdf`, … rather than `untitled 2.html`.
2434    fn proposed_name(docs: &Documents) -> String {
2435        if docs.active().name().is_none() {
2436            if let Ok(Some(block)) = docs.engine().instructions() {
2437                let title = file_stem_of_title(&block.front.title);
2438                if !title.is_empty() {
2439                    return title;
2440                }
2441            }
2442        }
2443        document_class::strip_class_extension(&Self::document_name(docs)).to_string()
2444    }
2445
2446    /// The ACTIVE document's display name, what Save As and Export start
2447    /// their name field from: the file's stem, never the full path a saved
2448    /// document is named by (whose `/`s became `_`s in every exported file
2449    /// name), and never the field's last contents, which belong to whichever
2450    /// tab last used it.
2451    fn document_name(docs: &Documents) -> String {
2452        docs.active().name().map(model_display_name).unwrap_or_else(|| docs.active().untitled_title())
2453    }
2454
2455    /// **New** — a fresh document in a NEW TAB. Nothing is replaced, so there is
2456    /// nothing to confirm.
2457    fn new_document(&mut self, docs: &mut Documents, class: DocumentClass) {
2458        docs.open_document(Document::fresh(docs.spawn_engine(), class));
2459        self.name_buf.clear();
2460        self.status = match class {
2461            DocumentClass::Normal => "new model".into(),
2462            _ => format!("new {}", class.label().to_ascii_lowercase()),
2463        };
2464    }
2465
2466    /// Refuse a save that would give TWO open tabs the same store identity —
2467    /// "focus the tab holding this document" has no answer then, and the second
2468    /// save would silently overwrite the first tab's file. `true` = go ahead.
2469    ///
2470    /// PLM documents compare their full part/revision key in either spelling.
2471    /// Local documents compare DISPLAY names: on desktop an open document
2472    /// carries the full path it was loaded from while the Save As field holds a
2473    /// bare name, so an identity compare would never match and the clobber would
2474    /// happen before anything noticed. Two same-stemmed files in different
2475    /// folders are refused too — stricter than strictly necessary, and the side
2476    /// to err on when the alternative is overwriting another tab's file.
2477    fn name_is_free(&mut self, docs: &mut Documents, name: &str) -> bool {
2478        let display = model_display_name(name);
2479        let target_revision = crate::plm::uses::revision_key_in(name);
2480        let taken = docs.iter().enumerate().any(|(index, doc)| {
2481            index != docs.active_index()
2482                && doc
2483                    .name()
2484                    .is_some_and(|open| {
2485                        match (&target_revision, crate::plm::uses::revision_key_in(open)) {
2486                            (Some(target), Some(other)) => *target == other,
2487                            (None, None) => model_display_name(open) == display,
2488                            _ => false,
2489                        }
2490                    })
2491        });
2492        if taken {
2493            let identity = target_revision.as_deref().unwrap_or(&display);
2494            self.status = format!("Save refused: '{identity}' is already open in another tab");
2495            docs.engine_mut().push_notice(&self.status);
2496        }
2497        !taken
2498    }
2499
2500    /// Write the active document's request JSON through the store under `name`.
2501    /// Returns `true` on success (so the caller can close the modal).
2502    fn save_to(&mut self, docs: &mut Documents, store: &dyn ModelStore, name: String) -> bool {
2503        let name = name.trim().to_string();
2504        if name.is_empty() {
2505            self.status = "enter a name to save".into();
2506            return false;
2507        }
2508        if !self.name_is_free(docs, &name) {
2509            return false;
2510        }
2511        // A Work part edited in place: Save writes the ASSEMBLY (its tab),
2512        // with the part's latest state pushed into it first.
2513        docs.flush_work_part();
2514        let mut plan = docs.plan_owned_save(store, &name);
2515        // A document the Save writes whose bytes have not been read yet (the
2516        // plan's read asked for them): wait for them rather than refuse it
2517        // "still loading", which left a held part unsaved on the first Save.
2518        if !std::mem::take(&mut self.owned_loaded) {
2519            let loading: Vec<String> = plan
2520                .rows
2521                .iter()
2522                .map(|row| row.write.key.clone())
2523                .filter(|key| store.residency(key) == crate::store::Residency::OnDisk)
2524                .collect();
2525            for key in &loading {
2526                // A row the plan settled without its bytes (a checkout) asked
2527                // for none: ask now, so the wait is only as long as the load.
2528                let _ = store.read(key);
2529            }
2530            if !loading.is_empty() {
2531                self.status = format!("Reading {} part(s) from the PLM before saving…", loading.len());
2532                self.owned_loading = Some((docs.active_id(), name, loading, 0));
2533                return false;
2534            }
2535        }
2536        match top_checkout_row(docs, store, &name) {
2537            Some(row) if matches!(row.issue, Some(crate::document::owned_save::Issue::Blocked(_))) => {
2538                // The revision this tab is cannot take the Save (another user
2539                // holds it, or it is released): the changes exist only in the
2540                // tab, so offer the way to keep them instead of a dead end (owner
2541                // rule: prompts that default to proceed). Held by someone else,
2542                // the Save As starts on a new part, as a new revision would sit
2543                // beside their draft; not editable, on its next revision.
2544                let Some(crate::document::owned_save::Issue::Blocked(why)) = &row.issue else { unreachable!() };
2545                // A viewer has nowhere on the PLM to save to: Save As would
2546                // offer destinations that are all refused.
2547                if why == crate::document::owned_save::VIEW_ONLY {
2548                    self.status = format!(
2549                        "{} is not saved: {why}. Your changes are only in this tab: Export keeps a copy as a file.",
2550                        docs.active().title()
2551                    );
2552                    docs.engine_mut().push_notice_as(brep_render::engine_state::NoticeSeverity::Warning, self.status.clone());
2553                    return false;
2554                }
2555                let held = why.starts_with("checked out by");
2556                self.status = format!(
2557                    "{} is not saved: {why}. Your changes are only in this tab: Save As keeps them as {}, or Save again once it can be written.",
2558                    docs.active().title(),
2559                    if held { "a new part" } else { "a new revision" }
2560                );
2561                docs.engine_mut().push_notice_as(brep_render::engine_state::NoticeSeverity::Warning, self.status.clone());
2562                let status = self.status.clone();
2563                self.member_new_part = held;
2564                self.dispatch(FileAction::SaveAs, docs, store);
2565                self.status = status;
2566                return false;
2567            }
2568            Some(row) => plan.rows.insert(0, row),
2569            None => {}
2570        }
2571        if self.hold_for_plm_items(docs, store, plan.clone(), &name, false) {
2572            return false;
2573        }
2574        if self.asks(&plan) {
2575            self.ask_owned_save(plan, name, false);
2576            return false;
2577        }
2578        self.commit_save(docs, store, name, plan)
2579    }
2580
2581    /// Whether `plan` needs the Save dialog: a question, unless the only
2582    /// questions are checkouts the user said not to ask about again.
2583    fn asks(&self, plan: &crate::document::owned_save::OwnedSavePlan) -> bool {
2584        use crate::document::owned_save::Issue;
2585        plan.rows
2586            .iter()
2587            .any(|row| row.asks() && !(self.checkout_remembered && matches!(row.issue, Some(Issue::CheckOut { newer: false, .. }))))
2588    }
2589
2590    /// A PLM assembly's parts with no document get PLM items, not files: start
2591    /// making them and hold the Save until they exist (`true`).
2592    fn hold_for_plm_items(&mut self, docs: &Documents, store: &dyn ModelStore, plan: crate::document::owned_save::OwnedSavePlan, name: &str, browser: bool) -> bool {
2593        let Some(client) = store.plm_client() else { return false };
2594        if plan.promotions.is_empty() || crate::panels::plm_host::revision_of(name).is_none() {
2595            return false;
2596        }
2597        let current: serde_json::Value = serde_json::from_str(&docs.tab_engine().history_request_json()).unwrap_or_default();
2598        let requests = plm_promotion_requests(&current, &plan);
2599        let count = requests.len();
2600        // Linked into the workspace folder the assembly was opened from;
2601        // opened any other way (by number, from a search), the one folder of
2602        // the user's workspace that links it; else the top of the workspace.
2603        let opened = docs.active().plm_folder().cloned();
2604        let part = crate::panels::plm_host::revision_of(name).map(|(part, _)| part);
2605        let future = async move {
2606            let folder = match (opened, part) {
2607                (Some(folder), _) => Some(folder),
2608                (None, Some(part)) => crate::plm::promote::linking_folder(&client, &part).await.filter(|(id, _)| !id.is_empty()),
2609                (None, None) => None,
2610            };
2611            let keys = crate::plm::promote::make_items(client, requests, folder.as_ref().map_or(String::new(), |(id, _)| id.clone())).await?;
2612            Ok((keys, folder))
2613        };
2614        self.owned_promoting = Some((OwnedPrompt { plan, name: name.to_string(), browser }, Box::pin(future)));
2615        self.status = format!("Making PLM items for {count} part(s) with no document of their own…");
2616        true
2617    }
2618
2619    /// Continue a Save held until its documents loaded: plan it again once
2620    /// none is waiting, or after ~20 s (the Save then says which part is still
2621    /// loading, as before). A load that failed is asked again every ~2 s.
2622    fn poll_owned_loading(&mut self, ctx: &egui::Context, docs: &mut Documents, store: &dyn ModelStore) {
2623        const LOAD_WAIT_FRAMES: u32 = 400;
2624        let Some((_, _, keys, frames)) = self.owned_loading.as_mut() else { return };
2625        if *frames < LOAD_WAIT_FRAMES && keys.iter().any(|key| store.residency(key) == crate::store::Residency::OnDisk) {
2626            *frames += 1;
2627            if *frames % 40 == 0 {
2628                for key in keys.iter() {
2629                    let _ = store.read(key);
2630                }
2631            }
2632            ctx.request_repaint_after(std::time::Duration::from_millis(50));
2633            return;
2634        }
2635        let Some((doc, name, _, _)) = self.owned_loading.take() else { return };
2636        if doc != docs.active_id() {
2637            self.status = "Save cancelled because the active document changed".into();
2638            return;
2639        }
2640        self.owned_loaded = true;
2641        self.save_to(docs, store, name);
2642    }
2643
2644    /// Continue a Save held by [`Self::hold_for_plm_items`] once its items
2645    /// exist; a failure saves nothing and says why (never into the assembly).
2646    fn poll_owned_promotions(&mut self, ctx: &egui::Context, docs: &mut Documents, store: &dyn ModelStore) {
2647        let Some((_, future)) = self.owned_promoting.as_mut() else { return };
2648        let mut cx = std::task::Context::from_waker(std::task::Waker::noop());
2649        let std::task::Poll::Ready(result) = std::future::Future::poll(future.as_mut(), &mut cx) else {
2650            ctx.request_repaint_after(std::time::Duration::from_millis(50));
2651            return;
2652        };
2653        let Some((mut prompt, _)) = self.owned_promoting.take() else { return };
2654        if prompt.plan.doc_id != docs.active_id() {
2655            self.status = "Save cancelled because the active document changed".into();
2656            return;
2657        }
2658        match result {
2659            Ok((keys, folder)) => {
2660                let place = folder.map_or_else(|| "the top of your workspace".to_string(), |(_, name)| format!("your workspace folder '{name}'"));
2661                docs.engine_mut().push_notice_as(
2662                    brep_render::engine_state::NoticeSeverity::Info,
2663                    format!("Made {} PLM item(s) for parts with no document, linked in {place}", keys.len()),
2664                );
2665                assign_plm_keys(&mut prompt.plan, &keys);
2666                if self.asks(&prompt.plan) {
2667                    self.ask_owned_save(prompt.plan, prompt.name, prompt.browser);
2668                } else if prompt.browser {
2669                    self.commit_save_browser(docs, store, prompt.name, prompt.plan);
2670                } else {
2671                    self.commit_save(docs, store, prompt.name, prompt.plan);
2672                }
2673            }
2674            Err(error) => {
2675                self.status = format!("Save stopped: the PLM could not make an item for a part with no document: {error}");
2676                docs.engine_mut().push_notice(&self.status);
2677            }
2678        }
2679    }
2680
2681    /// Hold a Save until the user answers its questions about the documents
2682    /// the assembly owns.
2683    fn ask_owned_save(&mut self, plan: crate::document::owned_save::OwnedSavePlan, name: String, browser: bool) {
2684        self.owned_state = serde_json::json!({"prompt": plan.to_json()});
2685        self.owned_prompt = Some(OwnedPrompt { plan, name, browser });
2686        self.open_modal(Mode::OwnedSave);
2687    }
2688
2689    /// Write the documents the assembly owns (with the user's answers), then
2690    /// return the Save summary to fold into the status line.
2691    fn save_owned(&mut self, docs: &mut Documents, store: &dyn ModelStore, plan: crate::document::owned_save::OwnedSavePlan) -> String {
2692        let (report, checkouts) = docs.execute_owned_save(store, plan);
2693        if let Some(client) = store.plm_client() {
2694            use crate::panels::plm::PlmLifecycle;
2695            let (revisions, checkouts): (Vec<_>, Vec<_>) = checkouts.into_iter().partition(|r| r.new_revision);
2696            for request in checkouts {
2697                let future = client.checkout(&request.part, &request.revision);
2698                self.owned_checkouts.push((request, future));
2699            }
2700            if !revisions.is_empty() {
2701                // Deepest first, as the Save planned them: each text names the
2702                // revisions made before it.
2703                let client = client.clone();
2704                let requests = revisions.clone();
2705                let future: crate::plm::PlmFuture<Vec<Result<(String, String), String>>> = Box::pin(async move {
2706                    let mut done: Vec<(String, String, String)> = Vec::new();
2707                    let mut answers = Vec::new();
2708                    for request in &requests {
2709                        let text = crate::document::owned_save::retarget_text(&request.text, &done);
2710                        match crate::panels::plm_parts::save_as_new_revision(&client, &request.part, "", &text).await {
2711                            Ok(new_key) => {
2712                                done.push((request.key.clone(), new_key.clone(), text.clone()));
2713                                answers.push(Ok((new_key, text)));
2714                            }
2715                            Err(why) => answers.push(Err(why)),
2716                        }
2717                    }
2718                    Ok(answers)
2719                });
2720                self.owned_revisions = Some(OwnedRevisions { doc: docs.active_id(), requests: revisions, future });
2721            }
2722        }
2723        self.owned_state = serde_json::json!({"report": report.to_json()});
2724        let summary = report.summary();
2725        if !summary.is_empty() {
2726            docs.engine_mut().push_notice_as(brep_render::engine_state::NoticeSeverity::Info, format!("Save: {summary}"));
2727        }
2728        summary
2729    }
2730
2731    /// The new revisions a Save asked for, once their chain has answered:
2732    /// each one's copies re-point to it (in order, so a holder's re-point
2733    /// keeps the ones beneath it), a failed one stays marked.
2734    fn poll_owned_revisions(&mut self, docs: &mut Documents, store: &dyn ModelStore) {
2735        let Some(revisions) = self.owned_revisions.as_mut() else { return };
2736        let mut cx = std::task::Context::from_waker(std::task::Waker::noop());
2737        let std::task::Poll::Ready(result) = std::future::Future::poll(revisions.future.as_mut(), &mut cx) else { return };
2738        let OwnedRevisions { doc, requests, .. } = self.owned_revisions.take().unwrap();
2739        let answers = result.unwrap_or_else(|why| requests.iter().map(|_| Err(why.clone())).collect());
2740        for (request, answer) in requests.iter().zip(answers) {
2741            match answer {
2742                Ok((new_key, text)) => match docs.retarget_revised_copies(doc, &request.key, &new_key, &text) {
2743                    Ok(()) => {
2744                        // As Save As → new revision: the mirror holds it now and
2745                        // the index learns it, or the next Save reads it absent.
2746                        store.remember_saved_document(&store.canonical_identity(&new_key), &text);
2747                        store.refresh_plm_index(std::slice::from_ref(&new_key), false);
2748                        docs.engine_mut().push_notice_as(
2749                            brep_render::engine_state::NoticeSeverity::Info,
2750                            format!(
2751                                "Made a new revision of {} and saved the change into it: {}",
2752                                request.shown,
2753                                crate::document::owned_save::shown(&new_key, &[])
2754                            ),
2755                        );
2756                        let followup = self.owned_followup.get_or_insert(OwnedFollowup { doc, keys: Vec::new(), polls: 0 });
2757                        followup.keys.push(new_key);
2758                    }
2759                    Err(why) => docs.engine_mut().push_notice(format!("'{new_key}' holds the change, but the assembly could not name it: {why}")),
2760                },
2761                Err(why) => {
2762                    docs.engine_mut().push_notice(format!("{} NOT saved: {why} — the change is kept in the assembly", request.shown));
2763                    let was_clean = docs.iter().find(|d| d.id() == doc).is_some_and(|d| !d.is_dirty());
2764                    if docs.mark_unsaved_copies(doc, &request.key, &format!("a new revision could not be made: {why}")) {
2765                        write_late_marks(docs, store, doc, was_clean);
2766                    }
2767                }
2768            }
2769        }
2770    }
2771
2772    /// Write the PLM items whose checkout landed since last frame.
2773    fn poll_owned_checkouts(&mut self, ctx: &egui::Context, docs: &mut Documents, store: &dyn ModelStore) {
2774        if let Some((_, _, _, future)) = self.owned_top.as_mut() {
2775            let mut cx = std::task::Context::from_waker(std::task::Waker::noop());
2776            match std::future::Future::poll(future.as_mut(), &mut cx) {
2777                std::task::Poll::Ready(result) => {
2778                    if let Some((id, signature, request, _)) = self.owned_top.take() {
2779                        let saved = crate::document::owned_save::finish_checkout(docs.engine_mut(), store, &request, result);
2780                        if saved {
2781                            if let Some(doc) = docs.iter_mut().find(|d| d.id() == id) {
2782                                doc.mark_saved_signature(signature);
2783                                doc.set_plm_save_access(crate::document::Access::Editable);
2784                            }
2785                            self.save_generation += 1;
2786                            remember_document(store, &request.key);
2787                            self.status = format!("saved {}", request.key);
2788                        } else {
2789                            self.status = format!("{} NOT saved: the checkout or the write failed; your edits are kept", request.key);
2790                        }
2791                    }
2792                }
2793                std::task::Poll::Pending => ctx.request_repaint_after(std::time::Duration::from_millis(50)),
2794            }
2795        }
2796        self.poll_owned_revisions(docs, store);
2797        if self.owned_checkouts.is_empty() && self.owned_revisions.is_none() {
2798            // The follow-up Save also waits for the assembly's own checkout.
2799            if self.owned_top.is_some() {
2800                return;
2801            }
2802            if let Some(followup) = self.owned_followup.as_mut() {
2803                // ~10 s at the 50 ms repaint below.
2804                const INDEX_WAIT_POLLS: u32 = 200;
2805                if followup.polls < INDEX_WAIT_POLLS && followup.keys.iter().any(|key| store.plm_revision(key).is_none()) {
2806                    followup.polls += 1;
2807                    ctx.request_repaint_after(std::time::Duration::from_millis(50));
2808                    return;
2809                }
2810            }
2811            if let Some(OwnedFollowup { doc: id, .. }) = self.owned_followup.take() {
2812                // Every new revision landed: save the assembly again so the
2813                // documents holding them (and the assembly itself) name them.
2814                if docs.active_id() == id {
2815                    if let Some(name) = docs.active().name().map(str::to_string) {
2816                        self.save_to(docs, store, name);
2817                    }
2818                } else {
2819                    docs.engine_mut().push_notice("Save the assembly again: it names the new revisions now");
2820                }
2821            }
2822            return;
2823        }
2824        let mut cx = std::task::Context::from_waker(std::task::Waker::noop());
2825        let mut i = 0;
2826        while i < self.owned_checkouts.len() {
2827            match std::future::Future::poll(self.owned_checkouts[i].1.as_mut(), &mut cx) {
2828                std::task::Poll::Ready(result) => {
2829                    let (request, _) = self.owned_checkouts.remove(i);
2830                    let failure = result.as_ref().err().cloned();
2831                    let id = docs.active_id();
2832                    if crate::document::owned_save::finish_checkout(docs.engine_mut(), store, &request, result) {
2833                        if docs.clear_unsaved_copies(id, &request.key) {
2834                            // A change kept by an earlier Save is written now; the
2835                            // assembly was written before it landed and still marks
2836                            // it: save the assembly again, as for new revisions.
2837                            self.owned_followup.get_or_insert(OwnedFollowup { doc: id, keys: Vec::new(), polls: 0 });
2838                        }
2839                    } else {
2840                        // Kept in the assembly: marked, so it reads unsaved, the
2841                        // next Save asks again and no refresh replaces it.
2842                        let why = failure.unwrap_or_else(|| "the write after the checkout failed".into());
2843                        let was_clean = docs.iter().find(|d| d.id() == id).is_some_and(|d| !d.is_dirty());
2844                        if docs.mark_unsaved_copies(id, &request.key, &format!("checkout failed: {why}")) {
2845                            write_late_marks(docs, store, id, was_clean);
2846                        }
2847                    }
2848                }
2849                std::task::Poll::Pending => i += 1,
2850            }
2851        }
2852        if !self.owned_checkouts.is_empty() || self.owned_revisions.is_some() {
2853            ctx.request_repaint_after(std::time::Duration::from_millis(50));
2854        }
2855    }
2856
2857    /// After the owned documents: a part that should have been given a file
2858    /// of its own and could not be stops the Save BEFORE the assembly is
2859    /// written — written now, it would keep that part only inside the
2860    /// assembly. Everything else already saved stays saved, the tab stays
2861    /// unsaved, and Save can simply be pressed again.
2862    fn promotion_failed(&mut self, docs: &mut Documents) -> bool {
2863        let failed: Vec<String> = self.owned_state["report"]["failedPromotions"]
2864            .as_array()
2865            .map(|keys| keys.iter().filter_map(|k| k.as_str().map(str::to_string)).collect())
2866            .unwrap_or_default();
2867        if failed.is_empty() {
2868            return false;
2869        }
2870        self.status = format!(
2871            "Save stopped: could not create {} — the assembly was not written, so no part is kept only inside it. Fix the problem and Save again.",
2872            failed.join(", ")
2873        );
2874        docs.engine_mut().push_notice(&self.status);
2875        true
2876    }
2877
2878    /// The Save itself, once nothing is left to ask: the owned documents
2879    /// first (deepest first), then the active tab.
2880    fn commit_save(&mut self, docs: &mut Documents, store: &dyn ModelStore, name: String, plan: crate::document::owned_save::OwnedSavePlan) -> bool {
2881        use crate::document::owned_save::{CheckoutRequest, Issue};
2882        let top = plan.rows.first().filter(|row| row.write.key == name).cloned();
2883        if let Some(row) = &top {
2884            if !row.proceed {
2885                self.status = format!("Save cancelled: {name} was not checked out");
2886                return false;
2887            }
2888        }
2889        let summary = self.save_owned(docs, store, plan);
2890        if self.promotion_failed(docs) {
2891            return false;
2892        }
2893        let document = docs.tab_engine().history_request_json();
2894        if let (Some(row), Some(client)) = (top, store.plm_client()) {
2895            if let Some(Issue::CheckOut { part, revision, .. }) = row.issue {
2896                use crate::panels::plm::PlmLifecycle;
2897                let signature = docs.active().save_signature();
2898                let future = client.checkout(&part, &revision);
2899                let shown = crate::document::owned_save::shown(&name, &[]);
2900                let request = CheckoutRequest { key: name.clone(), part, revision, text: document, shown: shown.clone(), new_revision: false };
2901                self.owned_top = Some((docs.active_id(), signature, request, future));
2902                self.status = if summary.is_empty() {
2903                    format!("checking out and saving {shown}")
2904                } else {
2905                    format!("checking out and saving {shown}; {summary}")
2906                };
2907                return true;
2908            }
2909        }
2910        crate::plm::thumbnail::stage_save(store, &name, &document, docs.tab_engine(), docs.active_id());
2911        match store.write(&name, &document) {
2912            Ok(()) => {
2913                // The document keeps the raw identity (a full path when a
2914                // native dialog chose it); the name field shows the bare name.
2915                self.name_buf = model_display_name(&name);
2916                let doc = docs.active_mut();
2917                doc.set_name(Some(name.clone()));
2918                doc.mark_clean();
2919                self.save_generation += 1;
2920                remember_document(store, &name);
2921                self.status = if summary.is_empty() { format!("saved {name}") } else { format!("saved {name}; {summary}") };
2922                true
2923            }
2924            Err(e) => {
2925                self.status = format!("save failed: {e}");
2926                docs.engine_mut().push_notice(&self.status);
2927                false
2928            }
2929        }
2930    }
2931
2932    /// Save As through the explorer's current directory, then retain the
2933    /// backend's returned identity (an absolute path on desktop or a virtual
2934    /// `/models/...` path in the browser) for subsequent plain Save commands.
2935    fn save_to_browser(
2936        &mut self,
2937        docs: &mut Documents,
2938        store: &dyn ModelStore,
2939        name: String,
2940    ) -> bool {
2941        let name = name.trim().to_string();
2942        if name.is_empty() {
2943            self.status = "enter a name to save".into();
2944            return false;
2945        }
2946        if !self.name_is_free(docs, &name) {
2947            return false;
2948        }
2949        // Save As makes a NEW file: its class is the one the name carries
2950        // (the chooser put it there), the blocks of the other classes go, and
2951        // a family member's stamp goes — the copy is a part of its own, which
2952        // is what "Save as a new part" means.
2953        docs.flush_work_part();
2954        let plan = docs.plan_owned_save(store, &name);
2955        if self.hold_for_plm_items(docs, store, plan.clone(), &name, true) {
2956            return false;
2957        }
2958        if plan.needs_asking() {
2959            self.ask_owned_save(plan, name, true);
2960            return false;
2961        }
2962        self.commit_save_browser(docs, store, name, plan)
2963    }
2964
2965    /// [`Self::save_to_browser`] once nothing is left to ask.
2966    fn commit_save_browser(&mut self, docs: &mut Documents, store: &dyn ModelStore, name: String, plan: crate::document::owned_save::OwnedSavePlan) -> bool {
2967        let summary = self.save_owned(docs, store, plan);
2968        if self.promotion_failed(docs) {
2969            return false;
2970        }
2971        let class = DocumentClass::of_name(&name).unwrap_or_default();
2972        adopt_class(docs.tab_engine_mut(), class);
2973        // The copy remembers what it was saved from (before the family stamp
2974        // goes: the chain keeps the family too), so Replace component knows
2975        // an edited variation is still that part's relative.
2976        stamp_save_as_lineage(docs, store, &name);
2977        if docs.tab_engine().history.document_block(family_table::FAMILY_SOURCE_KEY).is_some() {
2978            docs.tab_engine_mut()
2979                .history
2980                .set_document_block_no_undo(family_table::FAMILY_SOURCE_KEY, None);
2981        }
2982        let previous = docs.active().name().map(str::to_string);
2983        let saved_text = docs.tab_engine().history_request_json();
2984        match store.browser_write(&name, &saved_text) {
2985            Ok(identity) => {
2986                // Open assemblies that place this part under its OLD name:
2987                // ask once whether they follow it to the new file.
2988                if let Some(old) = previous.filter(|old| store.canonical_identity(old) != store.canonical_identity(&identity)) {
2989                    let tabs = docs.tabs_holding(&old, docs.active_id());
2990                    if !tabs.is_empty() {
2991                        self.retarget = Some(Retarget { old, new_key: identity.clone(), text: saved_text.clone(), tabs });
2992                    }
2993                }
2994                // The recent list's picture, under the identity the backend
2995                // chose (the name the recent row will carry).
2996                crate::plm::thumbnail::stage_recent(&identity, docs.tab_engine(), docs.active_id());
2997                self.name_buf = model_display_name(&identity);
2998                let doc = docs.active_mut();
2999                self.member_ack.remove(&doc.id());
3000                doc.set_name(Some(identity.clone()));
3001                doc.mark_clean();
3002                self.save_generation += 1;
3003                remember_document(store, &identity);
3004                self.status = if summary.is_empty() { format!("saved {identity}") } else { format!("saved {identity}; {summary}") };
3005                true
3006            }
3007            Err(e) => {
3008                self.status = format!("save failed: {e}");
3009                false
3010            }
3011        }
3012    }
3013
3014    /// **Open** — read a stored document into its own tab (or focus the tab
3015    /// already holding it). The one door every open lane uses: File>Open, the
3016    /// Edit-Part flow, and the session restore's siblings.
3017    pub fn open_document(&mut self, docs: &mut Documents, store: &dyn ModelStore, name: &str) {
3018        self.pending_open = None;
3019        if docs.focus_named(name) {
3020            remember_document(store, name);
3021            self.status = format!("{name} is already open");
3022            return;
3023        }
3024        match crate::store::read_now(store, name) {
3025            crate::store::ReadNow::Ready(contents) => self.load_document(docs, name, &contents, store),
3026            // An index-hydrated PLM store lists what it has not loaded yet;
3027            // the read above asked for it.
3028            crate::store::ReadNow::Loading => {
3029                self.pending_open = Some((name.to_owned(), docs.active_id()));
3030                self.status = format!("'{name}' is still loading");
3031            }
3032            crate::store::ReadNow::Absent => self.status = format!("open failed: '{name}' not found"),
3033        }
3034    }
3035
3036    /// Complete the common File>Open / Edit-Part demand read on a later frame.
3037    /// Store reads are already deduplicated while a request is in flight.
3038    fn poll_pending_open(&mut self, ctx: &egui::Context, docs: &mut Documents, store: &dyn ModelStore) {
3039        let Some((name, requester)) = self.pending_open.take() else { return };
3040        if docs.active_id() != requester {
3041            return;
3042        }
3043        if docs.focus_named(&name) {
3044            self.status = format!("{name} is already open");
3045            return;
3046        }
3047        match crate::store::read_now(store, &name) {
3048            crate::store::ReadNow::Ready(contents) => {
3049                self.load_document(docs, &name, &contents, store);
3050            }
3051            crate::store::ReadNow::Loading => {
3052                self.pending_open = Some((name, requester));
3053                ctx.request_repaint_after(std::time::Duration::from_millis(100));
3054            }
3055            crate::store::ReadNow::Absent => {
3056                self.status = format!("open failed: '{name}' not found");
3057            }
3058        }
3059    }
3060
3061    /// Serialize the current model in `format` (`"step"` | `"iges"` | `"stl"` |
3062    /// `"obj"` | `"glb"` | `"3mf"` | `"json"`) and
3063    /// write it through the store's format-typed interchange under `<name>.<ext>`.
3064    /// `"json"` is the full model RECIPE (`.nbrep`, `history_request_json`) —
3065    /// the exact document Open/Import consume, for sharing a failing model. Returns
3066    /// `true` on success (so the caller can close the modal); a guard message and
3067    /// `false` when there is nothing to export or the engine/store errs.
3068    /// An export of the geometry taken while a family row is previewed is of
3069    /// THAT row: say so on the status line and in a toast, so a user does not
3070    /// ship the wrong size without knowing.
3071    fn say_previewed_export(&mut self, docs: &mut Documents) {
3072        let Some(label) = docs.engine().expression_preview().map(|p| p.label.clone()) else {
3073            return;
3074        };
3075        let note = format!("exported {label} (preview): the family's own values are not in this file");
3076        self.status = format!("{} — {label} (preview)", self.status);
3077        docs.engine_mut().push_notice(note);
3078    }
3079
3080    /// The line the export dialogs show while a family row is previewed.
3081    fn preview_export_line(ui: &mut egui::Ui, docs: &Documents) {
3082        if let Some(label) = docs.engine().expression_preview().map(|p| p.label.clone()) {
3083            let text = format!(
3084                "Previewing {label}: STEP, IGES, meshes, drawings and flat patterns export {label}, \
3085                 not the family's own values. The .nbrep recipe keeps the family's own."
3086            );
3087            ui.add(egui::Label::new(egui::RichText::new(text).color(ui.visuals().warn_fg_color)).wrap());
3088            ui.add_space(4.0);
3089        }
3090    }
3091
3092    fn poll_instructions_pdf(&mut self, ctx: &egui::Context, docs: &mut Documents, store: &dyn ModelStore) {
3093        let Some(mut export) = self.instructions_pdf.take() else { return };
3094        if export.document != docs.view_id() {
3095            self.status = "PDF export cancelled: the active document changed".into();
3096            docs.engine_mut().push_notice(self.status.clone());
3097            return;
3098        }
3099        match docs.engine_mut().poll_instructions_pdf(&mut export.job) {
3100            Ok(None) => {
3101                self.instructions_pdf = Some(export);
3102                ctx.request_repaint_after(std::time::Duration::from_millis(16));
3103            }
3104            Ok(Some((bytes, pages))) => {
3105                let pending = PendingSave {
3106                    request: SaveRequest::bytes("Export the instructions", format!("{}.pdf", export.name), bytes),
3107                    detail: format!(" ({pages} page{})", if pages == 1 { "" } else { "s" }),
3108                    label: None, warnings: vec![], preview: export.preview,
3109                };
3110                self.deliver(docs, store, pending);
3111            }
3112            Err(error) => {
3113                self.status = format!("PDF export failed: {error}");
3114                docs.engine_mut().push_notice(self.status.clone());
3115            }
3116        }
3117    }
3118
3119    fn export_as(&mut self, docs: &mut Documents, store: &dyn ModelStore, format: &str) -> bool {
3120        let name = self.effective_name(docs);
3121        // The `.nbrep` recipe is the document; every other format is of the
3122        // geometry, which a family-row preview has replaced — said once the
3123        // file is delivered.
3124        let preview = format != "json";
3125        let export = |request: SaveRequest| PendingSave { request, detail: String::new(), label: None, warnings: vec![], preview };
3126        // GLB and 3MF are BINARY, so they take the store's byte-shaped
3127        // interchange rather than the text one below — a `.glb` routed through
3128        // a `&str` would be mangled the moment a chunk length happened not to
3129        // be valid UTF-8, and a 3MF is a ZIP.
3130        // The offline player: one page, named for the document, the tools'
3131        // pictures fetched from the library and inlined (the door is held
3132        // while they load; without a session the page carries none).
3133        if format == "instructionshtml" {
3134            let ids = linked_tool_ids(docs.engine());
3135            if let Some(client) = store.plm_client().filter(|_| !ids.is_empty()) {
3136                crate::plm::tool_pictures::request(&client, &ids);
3137                if crate::plm::tool_pictures::pending_of(&ids) > 0 {
3138                    self.pending_instructions_export = Some(format.to_string());
3139                    self.status = "loading the tool pictures for the export…".into();
3140                    return true;
3141                }
3142            }
3143            let made = docs.engine().instructions_player_data().map(|mut data| {
3144                data.inline_tool_pictures(&crate::plm::tool_pictures::fetched(&ids));
3145                brep_instructions::player::offline_html(&data, brep_instructions::player::Mode::Shop)
3146            });
3147            return match made {
3148                Ok(html) => {
3149                    let pending = export(SaveRequest::text("Export the offline player", format!("{name}.html"), html));
3150                    self.deliver(docs, store, pending) != Delivery::Failed
3151                }
3152                Err(e) => {
3153                    self.status = format!("export failed: {e}");
3154                    false
3155                }
3156            };
3157        }
3158        // The instructions' PDF and their step pictures: byte formats, named
3159        // for the document (`<name>.pdf`, `<name>-steps.zip`).
3160        if format == "instructionspdf" {
3161            if self.instructions_pdf.is_some() {
3162                self.status = "an instructions PDF is already being prepared".into();
3163                return false;
3164            }
3165            return match docs.engine_mut().begin_instructions_pdf() {
3166                Ok(job) => {
3167                    self.instructions_pdf = Some(PendingInstructionsPdf { document: docs.view_id(), name, preview, job });
3168                    self.status = "preparing the instructions PDF…".into();
3169                    true
3170                }
3171                Err(error) => { self.status = format!("export failed: {error}"); false }
3172            };
3173        }
3174        if format == "instructionsimages" {
3175            // The pictures are drawn from the exact solids: asked of the
3176            // runner first, and on a runner that cannot be waited on the
3177            // export is held until they land.
3178            if !docs.engine_mut().instructions_topology_ready() {
3179                self.pending_instructions_export = Some(format.to_string());
3180                self.status = "loading the exact geometry for the export…".into();
3181                return true;
3182            }
3183            let made = super::instructions::images_zip(docs.engine_mut()).map(|bytes| (bytes, String::new()));
3184            return match made {
3185                Ok((bytes, detail)) => {
3186                    let (title, file) = ("Export the step images", format!("{name}-steps.zip"));
3187                    let pending = PendingSave { detail, ..export(SaveRequest::bytes(title, file, bytes)) };
3188                    self.deliver(docs, store, pending) != Delivery::Failed
3189                }
3190                Err(e) => {
3191                    self.status = format!("export failed: {e}");
3192                    false
3193                }
3194            };
3195        }
3196        if format == "glb" || format == "3mf" {
3197            // The document's name becomes the root NODE's (GLB) or the model's
3198            // Title (3MF), as it becomes the root PRODUCT's on a STEP export.
3199            let written = if format == "glb" {
3200                docs.engine().export_glb_bytes(&name)
3201            } else {
3202                docs.engine().export_3mf_bytes(&name)
3203            };
3204            return match written {
3205                Ok(bytes) => {
3206                    let title = if format == "glb" { "Export GLB" } else { "Export 3MF" };
3207                    let request = SaveRequest::bytes(title, format!("{name}.{format}"), bytes);
3208                    self.deliver(docs, store, export(request)) != Delivery::Failed
3209                }
3210                Err(e) => {
3211                    self.status = format!("export failed: {e}");
3212                    false
3213                }
3214            };
3215        }
3216        let state = docs.engine_mut();
3217        // A drawing sheet's SVG writes `<name>-<sheet>.svg`: a document can
3218        // carry several sheets, and one file per export must not overwrite the
3219        // last. The PDF is the whole drawing SET, one page per sheet, so it is
3220        // the document's own `<name>.pdf`. It takes the BYTE lane — it is a
3221        // byte format, and both lanes type the file from its extension, so the
3222        // user gets `application/pdf` either way.
3223        // The 3D drawing set is `<name>-3d.pdf`, beside the plain one rather
3224        // than over it.
3225        if format == "sheetsvg" || format == "sheetpdf" || format == "sheetpdf3d" {
3226            let pdf = format != "sheetsvg";
3227            let three_d = format == "sheetpdf3d";
3228            let (file, made) = if pdf {
3229                let file = if three_d { format!("{name}-3d.pdf") } else { format!("{name}.pdf") };
3230                let bytes = if three_d { state.export_document_pdf_3d() } else { state.export_document_pdf() };
3231                (file, bytes.map(SaveBody::Bytes))
3232            } else {
3233                let sheet_name = state
3234                    .resolve_sheet("")
3235                    .ok()
3236                    .and_then(|id| state.sheet_state().find_sheet(&id).map(|sheet| sheet.name.clone()))
3237                    .unwrap_or_default();
3238                let file = format!("{name}-{}.svg", slug(&sheet_name));
3239                let text = state.resolve_sheet("").and_then(|id| state.export_sheet_svg(&id));
3240                (file, text.map(SaveBody::Text))
3241            };
3242            let pages = state.sheet_state().sheets.len() + usize::from(three_d);
3243            return match made {
3244                Ok(body) => {
3245                    let detail = if pdf {
3246                        format!(" ({pages} page{})", if pages == 1 { "" } else { "s" })
3247                    } else {
3248                        String::new()
3249                    };
3250                    let title = if pdf { "Export the drawing set" } else { "Export the drawing sheet" };
3251                    let request = SaveRequest { title: title.into(), file_name: file, body };
3252                    let pending = PendingSave { detail, ..export(request) };
3253                    self.deliver(docs, store, pending) != Delivery::Failed
3254                }
3255                Err(e) => {
3256                    self.status = format!("export failed: {e}");
3257                    false
3258                }
3259            };
3260        }
3261        let (text, ext) = match format {
3262            "stl" => (state.export_stl_text(), "stl"),
3263            "obj" => (state.export_obj_text(), "obj"),
3264            "iges" => (state.export_iges_text(), "igs"),
3265            "json" => (Ok(state.history_request_json()), "nbrep"),
3266            // The document's name becomes the root PRODUCT's — an assembly
3267            // exports as `<name>` with its parts named under it.
3268            _ => (state.export_step_text_named(&name), "step"),
3269        };
3270        match text {
3271            Ok(contents) => {
3272                let title = if format == "json" { "Export the model".to_string() } else { format!("Export {}", ext.to_ascii_uppercase()) };
3273                let request = SaveRequest::text(title, format!("{name}.{ext}"), contents);
3274                self.deliver(docs, store, export(request)) != Delivery::Failed
3275            }
3276            Err(e) => {
3277                self.status = format!("export failed: {e}");
3278                false
3279            }
3280        }
3281    }
3282
3283    /// Hand a produced export to the user — the one door every export takes.
3284    /// Desktop (a store with local files): the Save-file chooser, which the
3285    /// modal switches to; the write happens on its Save. Browser: the
3286    /// download, now. What a written export says (status, toasts, the
3287    /// preview note) is [`Self::say_written`], on both roads.
3288    fn deliver(&mut self, docs: &mut Documents, store: &dyn ModelStore, pending: PendingSave) -> Delivery {
3289        if let Some(local) = store.local_files() {
3290            self.save_name = pending.request.file_name.clone();
3291            self.save_file = Some(pending);
3292            self.use_folder(Purpose::Exports, local);
3293            self.status.clear();
3294            self.open_modal(Mode::SaveFile);
3295            return Delivery::Chooser;
3296        }
3297        let file = pending.request.file_name.clone();
3298        let written = match &pending.request.body {
3299            SaveBody::Text(text) => store.export_file_named(&file, text),
3300            SaveBody::Bytes(bytes) => store.export_file_named_bytes(&file, bytes),
3301        };
3302        match written {
3303            Ok(()) => {
3304                self.say_written(docs, &pending, &file, None);
3305                Delivery::Done
3306            }
3307            Err(e) => {
3308                self.status = match &pending.label {
3309                    Some(label) => format!("{label} export failed: {e}"),
3310                    None => format!("export failed: {e}"),
3311                };
3312                Delivery::Failed
3313            }
3314        }
3315    }
3316
3317    /// What a written export says: the status line ("exported <file>
3318    /// [detail] [into <folder>]"), the family-preview note, and the toast —
3319    /// a labelled export's own (the PCB outputs name themselves: "Netlist:
3320    /// …"), a warning one carrying the bundle's warnings, or on desktop,
3321    /// where the chooser closes on the write and the status with it, the
3322    /// generic "exported … into …". Returns whether the dialog should STAY
3323    /// open: it does on warnings, so they are read before boards are ordered.
3324    fn say_written(&mut self, docs: &mut Documents, pending: &PendingSave, file: &str, folder: Option<&str>) -> bool {
3325        let into = folder.map(|folder| format!(" into {folder}")).unwrap_or_default();
3326        self.status = format!("exported {file}{}{into}", pending.detail);
3327        if pending.preview {
3328            self.say_previewed_export(docs);
3329        }
3330        use brep_render::engine_state::NoticeSeverity;
3331        let state = docs.engine_mut();
3332        match &pending.label {
3333            Some(label) if !pending.warnings.is_empty() => {
3334                let warnings = pending.warnings.join(" ");
3335                self.status = format!("{}. Before ordering: {warnings}", self.status);
3336                state.push_notice_as(NoticeSeverity::Warning, format!("{label}: {warnings}"));
3337                true
3338            }
3339            Some(label) => {
3340                state.push_notice_as(NoticeSeverity::Info, format!("{label}: {}", self.status));
3341                false
3342            }
3343            None => {
3344                if folder.is_some() {
3345                    state.push_notice_as(NoticeSeverity::Info, self.status.clone());
3346                }
3347                false
3348            }
3349        }
3350    }
3351
3352    /// A panel's export (the JavaScript editor's script, a plugin bundle, a
3353    /// PLM download): hand `request` to the user through the export door.
3354    /// Desktop: the Save-file chooser opens. Browser: the download, now.
3355    /// `Err` only when the download could not start.
3356    pub fn request_save_file(&mut self, docs: &mut Documents, store: &dyn ModelStore, request: SaveRequest) -> Result<(), String> {
3357        let pending = PendingSave { request, detail: String::new(), label: None, warnings: vec![], preview: false };
3358        match self.deliver(docs, store, pending) {
3359            Delivery::Failed => Err(std::mem::take(&mut self.status)),
3360            Delivery::Done | Delivery::Chooser => Ok(()),
3361        }
3362    }
3363
3364    /// The **Save-file** chooser ([`Mode::SaveFile`]): the explorer over the
3365    /// local files picks the FOLDER, the field the NAME, and Save writes the
3366    /// held export there. Shaped like Save As (the name field and buttons
3367    /// pinned under the browser), over the exports' own remembered folder,
3368    /// which starts in the models folder.
3369    fn show_save_file(&mut self, ctx: &egui::Context, docs: &mut Documents, store: &dyn ModelStore) {
3370        let Some(local) = store.local_files() else {
3371            self.save_file = None;
3372            self.open = false;
3373            return;
3374        };
3375        let title = self.save_file.as_ref().map(|pending| pending.request.title.clone()).unwrap_or_else(|| "Save file".into());
3376        let pending = self.save_file.is_some();
3377        // The explorer lists the files of the export's own kind beside the
3378        // folders, so a name to overwrite can be picked; a name without an
3379        // extension lists every file.
3380        let extension = std::path::Path::new(self.save_name.trim())
3381            .extension()
3382            .map(|extension| extension.to_string_lossy().into_owned());
3383        let extensions: Vec<&str> = extension.iter().map(String::as_str).collect();
3384        let mut do_save = false;
3385        let mut cancel = false;
3386        let modal = egui::Modal::new(egui::Id::new("brep-file-savefile")).show(ctx, |ui| {
3387            file_explorer::dialog_body(ui, |ui| {
3388                ui.heading(&title);
3389                ui.add_space(4.0);
3390                file_explorer::dialog_footer(ui, "savefile", |ui| {
3391                    ui.label("File name");
3392                    let field = ui.add(
3393                        egui::TextEdit::singleline(&mut self.save_name).margin(crate::viewport::touch::text_edit_margin(ui)).min_size(crate::viewport::touch::target_size(ui.ctx(), egui::Vec2::ZERO))
3394                            .hint_text("file name")
3395                            .desired_width(f32::INFINITY),
3396                    );
3397                    self.hit("savefile:name", &field);
3398                    if self.want_focus {
3399                        field.request_focus();
3400                        self.want_focus = false;
3401                    }
3402                    let enter = field.lost_focus() && ui.input(|i| i.key_pressed(egui::Key::Enter));
3403                    ui.add_space(6.0);
3404                    ui.horizontal(|ui| {
3405                        // Written already (the chooser stays up on a warning):
3406                        // nothing is left to save, only to read and close.
3407                        let save = ui.add_enabled(pending, egui::Button::new("Save"));
3408                        self.hit("save", &save);
3409                        if save.clicked() || (enter && pending) {
3410                            do_save = true;
3411                        }
3412                        let c = ui.button(if pending { "Cancel" } else { "Close" });
3413                        self.hit("cancel", &c);
3414                        if c.clicked() {
3415                            cancel = true;
3416                        }
3417                    });
3418                    if !self.status.is_empty() {
3419                        ui.add_space(4.0);
3420                        ui.weak(&self.status);
3421                    }
3422                });
3423                let options = FileExplorerOptions {
3424                    hit_prefix: "savefile",
3425                    empty_label: "(no files of this kind here)",
3426                    row_icon: "\u{1F5CE}",
3427                    current: None,
3428                    allow_delete: false,
3429                    allow_import: false,
3430                    import_label: "",
3431                    import_hit: "savefile:upload",
3432                    show_cancel: false,
3433                    confirm_label: None,
3434                    extensions: &extensions,
3435                };
3436                let output = self.explorer.show_store(ui, local, options);
3437                self.record_explorer_hits(&output.hits);
3438                // A row fills the name field (to overwrite that file); the
3439                // row's identity is its full path on desktop, so just its
3440                // name. One-shot, so it never clobbers a name typed after.
3441                if let Some(identity) = output.picked.or(output.activated) {
3442                    self.save_name = std::path::Path::new(&identity)
3443                        .file_name()
3444                        .map(|name| name.to_string_lossy().into_owned())
3445                        .unwrap_or(identity);
3446                    ui.ctx().request_repaint();
3447                }
3448            })
3449        });
3450        if do_save {
3451            if self.save_file(docs, store) {
3452                self.open = false;
3453            }
3454        } else if cancel || modal.should_close() {
3455            if self.save_file.take().is_some() {
3456                self.status = "export cancelled: nothing written".into();
3457            }
3458            self.open = false;
3459        }
3460    }
3461
3462    /// The Save-file chooser's Save: write the held export under the name
3463    /// field, where the explorer is, and remember that folder for the next
3464    /// export. `true` once written, so the chooser closes — unless the export
3465    /// carried warnings, which keep it open to be read (what the format
3466    /// dialog did before the chooser existed).
3467    pub(crate) fn save_file(&mut self, docs: &mut Documents, store: &dyn ModelStore) -> bool {
3468        let Some(local) = store.local_files() else {
3469            return false;
3470        };
3471        let name = self.save_name.trim().to_string();
3472        if name.is_empty() {
3473            self.status = "enter a file name".into();
3474            return false;
3475        }
3476        let Some(pending) = self.save_file.take() else {
3477            return false;
3478        };
3479        match local.browser_write_bytes(&name, pending.request.body.as_bytes()) {
3480            Ok(path) => {
3481                self.folders.insert(Purpose::Exports, local.browser_location());
3482                let path = std::path::Path::new(&path);
3483                let folder = path.parent().map(|folder| folder.display().to_string()).unwrap_or_else(|| local.browser_location());
3484                let file = path.file_name().map(|file| file.to_string_lossy().into_owned()).unwrap_or(name);
3485                !self.say_written(docs, &pending, &file, Some(&folder))
3486            }
3487            Err(e) => {
3488                self.status = format!("export failed: {e}");
3489                self.save_file = Some(pending);
3490                false
3491            }
3492        }
3493    }
3494
3495    /// Serialize the sheet-metal flat pattern in `format` (`"dxf"` | `"svg"`) and
3496    /// write it through the store under `<name>.<ext>`. Returns `true` on success
3497    /// (so the caller can close the modal). On failure the message goes to the
3498    /// status line AND is queued as a toast (the existing engine notice path), so
3499    /// a "no sheet-metal body in the part" error is surfaced prominently.
3500    fn export_flat_pattern_as(
3501        &mut self,
3502        docs: &mut Documents,
3503        store: &dyn ModelStore,
3504        format: &str,
3505    ) -> bool {
3506        let name = self.effective_name(docs);
3507        let state = docs.engine_mut();
3508        let (text, ext) = match format {
3509            "svg" => (state.export_flat_pattern_svg(), "svg"),
3510            _ => (state.export_flat_pattern_dxf(), "dxf"),
3511        };
3512        match text {
3513            Ok(contents) => {
3514                let request = SaveRequest::text("Export the flat pattern", format!("{name}.{ext}"), contents);
3515                let pending = PendingSave { request, detail: String::new(), label: None, warnings: vec![], preview: true };
3516                self.deliver(docs, store, pending) != Delivery::Failed
3517            }
3518            Err(e) => {
3519                self.status = format!("flat-pattern export failed: {e}");
3520                state.push_notice(format!("Flat pattern: {e}"));
3521                false
3522            }
3523        }
3524    }
3525
3526    /// Serialize the assembly BOM in `format` (`"csv"` | `"json"`) and write it
3527    /// through the store under `<name>.bom.<ext>` (a compound extension naming
3528    /// the content, like the `.nbrep` recipe). On failure the message goes
3529    /// to the status line AND is queued as a toast — the flat-pattern error
3530    /// pattern — so a componentless document reports loudly.
3531    fn export_bom_as(
3532        &mut self,
3533        docs: &mut Documents,
3534        store: &dyn ModelStore,
3535        format: &str,
3536    ) -> bool {
3537        let name = self.effective_name(docs);
3538        let state = docs.engine_mut();
3539        let (text, ext) = match format {
3540            "json" => (state.export_bom_json(), "bom.json"),
3541            _ => (state.export_bom_csv(), "bom.csv"),
3542        };
3543        match text {
3544            Ok(contents) => {
3545                let request = SaveRequest::text("Export the bill of materials", format!("{name}.{ext}"), contents);
3546                let pending = PendingSave { request, detail: String::new(), label: None, warnings: vec![], preview: false };
3547                self.deliver(docs, store, pending) != Delivery::Failed
3548            }
3549            Err(e) => {
3550                self.status = format!("BOM export failed: {e}");
3551                state.push_notice(format!("BOM export: {e}"));
3552                false
3553            }
3554        }
3555    }
3556
3557    /// Write one of the PCB's manufacturing outputs through the store under
3558    /// [`Output::file_name`](super::fabrication_export::Output::file_name).
3559    /// The bundle's warnings (design-rule findings, silkscreen characters
3560    /// the font lacks) keep the dialog OPEN with the warnings on its status
3561    /// line, and go to a toast: the files are complete, but the user should
3562    /// read why before ordering boards. Returns `true` to close the dialog.
3563    fn export_fabrication_as(
3564        &mut self,
3565        docs: &mut Documents,
3566        store: &dyn ModelStore,
3567        output: super::fabrication_export::Output,
3568    ) -> bool {
3569        use super::fabrication_export::{self as fab, Output};
3570        let name = self.effective_name(docs);
3571        let file = output.file_name(&name);
3572        let state = docs.engine_mut();
3573        let made = match output {
3574            Output::Bundle => fab::bundle(state, &name).map(|bundle| {
3575                let detail = format!(" ({} files)", bundle.files.len());
3576                (SaveBody::Bytes(bundle.zip()), detail, bundle.warnings)
3577            }),
3578            _ => fab::text(state, output, &name).map(|text| (SaveBody::Text(text), String::new(), vec![])),
3579        };
3580        match made {
3581            Ok((body, detail, warnings)) => {
3582                // The output names itself in its toast (Netlist, Pick & place,
3583                // BOM, Fabrication), and the chooser's status says WHERE the
3584                // files went: on desktop that is the folder the user chose.
3585                let request = SaveRequest { title: format!("Export: {}", output.label()), file_name: file, body };
3586                let pending = PendingSave { request, detail, label: Some(output.label().to_string()), warnings, preview: false };
3587                match self.deliver(docs, store, pending) {
3588                    Delivery::Done => !self.status.contains("Before ordering"),
3589                    Delivery::Chooser => true,
3590                    Delivery::Failed => {
3591                        docs.engine_mut().push_notice(format!("{} export: {}", output.label(), self.status));
3592                        false
3593                    }
3594                }
3595            }
3596            Err(e) => {
3597                self.status = format!("{} export failed: {e}", output.label());
3598                state.push_notice(format!("{} export: {e}", output.label()));
3599                false
3600            }
3601        }
3602    }
3603
3604    /// Route an imported STEP file: **probe first**. The probe IS the parse —
3605    /// it stashes the structure in the engine for the import to consume — so a
3606    /// structured file arms the choice modal and is imported on the click,
3607    /// never parsed a second time.
3608    ///
3609    /// Everything else goes straight to today's flat lane, unchanged:
3610    ///
3611    /// * `Ok(None)` — no product structure. A part file must NEVER see the
3612    ///   dialog, and its behaviour stays byte-for-byte what it was.
3613    /// * `Err` — text the Part 21 parser refuses. Handing it to the flat lane
3614    ///   keeps the failure wording exactly today's (the two share the same
3615    ///   `ISO-10303-21` guard), rather than inventing a second one.
3616    fn import_step(&mut self, state: &mut EngineState, name: &str, contents: &str) {
3617        // A previous file's armed choice cannot survive this one: the probe
3618        // below REPLACES the engine's stash on every outcome (including the
3619        // structureless one), so an app-side pending left standing would offer
3620        // a button whose parse is already gone. A probe still running for an
3621        // earlier upload is superseded the same way (its answer is ignored).
3622        self.pending_step_import = None;
3623        let id = state.submit_step_probe(contents);
3624        self.pending_step_probe = Some(PendingStepProbe {
3625            id,
3626            name: name.to_string(),
3627            text: contents.to_string(),
3628        });
3629        self.status = format!("reading \"{name}\"\u{2026}");
3630        // The synchronous Inline runner (tests, headless) has answered inside
3631        // the submit; a background runner answers on a later frame's poll.
3632        self.poll_step_probe(state);
3633    }
3634
3635    /// Resolve an answered STEP probe: structure arms the §3.9 choice, no
3636    /// structure (or a parse the flat lane will refuse with its own wording)
3637    /// takes the flat lane. A probe the engine no longer holds — a document
3638    /// switch or a cancelled run dropped it — is reported and forgotten.
3639    fn poll_step_probe(&mut self, state: &mut EngineState) {
3640        while let Some((id, outcome)) = state.take_step_probe() {
3641            let Some(pending) = self.pending_step_probe.as_ref() else {
3642                continue;
3643            };
3644            if pending.id != id {
3645                continue; // an earlier upload's answer; a newer probe replaced it
3646            }
3647            let PendingStepProbe { name, text, .. } = self.pending_step_probe.take().unwrap();
3648            match outcome {
3649                StepProbeOutcome::Structure(probe) => {
3650                    self.status = format!(
3651                        "\"{name}\" contains an assembly — {} part{}, {} instance{}",
3652                        probe.parts,
3653                        plural(probe.parts),
3654                        probe.instances,
3655                        plural(probe.instances)
3656                    );
3657                    self.pending_step_import = Some(PendingStepImport {
3658                        name,
3659                        text,
3660                        probe,
3661                        // Default = keep the tree (§3.9). Flattening is the escape
3662                        // hatch for a deep or pathological file, not the norm.
3663                        flatten: false,
3664                    });
3665                    // The routing site's `close_after_import` ran frames ago,
3666                    // while the probe was still out, and closed the modal: the
3667                    // choice is raised HERE, when the answer lands. (Under the
3668                    // synchronous Inline runner both run in the same call, and
3669                    // the second open is idempotent.)
3670                    self.open_modal(Mode::StepAssembly);
3671                }
3672                StepProbeOutcome::Flat | StepProbeOutcome::Failed(_) => {
3673                    self.import_step_flat(state, &name, &text);
3674                }
3675            }
3676        }
3677        if self.pending_step_probe.is_some() && !state.step_probes_pending() {
3678            // Nothing is running and no answer came: the engine dropped the
3679            // probe (document switch / cancel). Say so rather than reading
3680            // "reading…" forever.
3681            let pending = self.pending_step_probe.take().unwrap();
3682            self.status = format!("import of \"{}\" was cancelled", pending.name);
3683        }
3684    }
3685
3686    /// Append an imported STEP file to the model (an IMPORT3D feature), then treat
3687    /// the enlarged model as dirty (an import is an edit, not an Open — the model
3688    /// keeps its current name / save baseline). THE flat lane, reached from a
3689    /// structureless file, an unparseable one, and the dialog's "Import as
3690    /// bodies".
3691    fn import_step_flat(&mut self, state: &mut EngineState, name: &str, contents: &str) {
3692        match state.import_step_feature(contents) {
3693            // Framing is deferred to the engine (`pending_fit`): under the native
3694            // thread / wasm worker runner the body is not resident yet, so framing
3695            // here would frame the empty scene. See [`EngineState::pending_fit`].
3696            Ok(_) => self.status = format!("imported {name}"),
3697            Err(e) => self.status = format!("import failed: {e}"),
3698        }
3699    }
3700
3701    /// **Import as assembly** — consume the probe's stash into parts-library
3702    /// entries + one component per occurrence, in the engine's single batch.
3703    ///
3704    /// An `Err` here means the structured lane produced nothing, and the engine
3705    /// returns BEFORE it touches history when that happens — so the honest
3706    /// answer is the one A6's contract prescribes: re-run the flat import with
3707    /// the text this dialog is still holding, and say plainly that the assembly
3708    /// the user asked for came in as bodies. Silence there would leave them
3709    /// believing they have a structure tree that does not exist.
3710    fn step_assembly_import(&mut self, state: &mut EngineState, store: &dyn ModelStore) {
3711        let Some(pending) = self.pending_step_import.take() else {
3712            return;
3713        };
3714        // Read BEFORE the import: afterwards its own ACOMP features make the
3715        // answer unconditionally "yes" and the workbench switch never fires.
3716        let was_assembly = state.history_has_assembly();
3717        let doc_name = step_document_name(&pending.name);
3718        // The §3.9 checkbox. A depth-1 file imports identically either way, so
3719        // an un-shown box costs nothing.
3720        let opts = StepAssemblyImport {
3721            nested: !pending.flatten,
3722        };
3723        // Every unique part is written to the store as its own document, at the
3724        // browser's current location and under `{assembly}-{part}` — the same
3725        // `browser_write` door + "wherever the explorer is pointing" convention
3726        // the STEP parts-library import uses (`panels::step_parts`). So an
3727        // imported part is a part like any other: Open Part opens it, Update
3728        // Components tracks it, a part edit writes through to it.
3729        let mut sink = StorePartSink::new(store, &doc_name);
3730        let message = match state.import_probed_step_assembly(&doc_name, opts, &mut sink) {
3731            Ok(report) => {
3732                if !was_assembly {
3733                    self.enter_assembly_workbench(state);
3734                }
3735                // The store side effect is REPORTED, never silent: a 50-part
3736                // import writes 50 files the user did not individually ask for.
3737                for failure in &sink.failures {
3738                    state.push_notice(format!("part not saved — {failure}"));
3739                }
3740                let saved = sink.written;
3741                let base = assembly_import_message(&pending.name, &report);
3742                match (saved, sink.failures.len()) {
3743                    (0, _) if sink.deferred > 0 => format!(
3744                        "{base}; its {} part documents become PLM items when you save the assembly",
3745                        sink.deferred
3746                    ),
3747                    (0, _) => base,
3748                    (saved, 0) => format!("{base}; saved {saved} part file(s)"),
3749                    (saved, failed) => {
3750                        format!("{base}; saved {saved} part file(s), {failed} could not be saved")
3751                    }
3752                }
3753            }
3754            Err(error) => match state.import_step_feature(&pending.text) {
3755                Ok(_) => format!(
3756                    "imported {} as bodies — the assembly structure could not be built ({error})",
3757                    pending.name
3758                ),
3759                Err(flat) => format!("import failed: {flat}"),
3760            },
3761        };
3762        // The status line only renders inside an OPEN modal and this one closes
3763        // on the click, so the toast is the half the user actually sees.
3764        self.status = message.clone();
3765        state.push_notice(message);
3766    }
3767
3768    /// **Import as bodies** — today's flat lane, unchanged. Drops the probe's
3769    /// stash first: the user chose the text, so the parsed assembly (and every
3770    /// product's solids it is holding resident) has no consumer left.
3771    fn step_assembly_bodies(&mut self, state: &mut EngineState) {
3772        let Some(pending) = self.pending_step_import.take() else {
3773            return;
3774        };
3775        state.discard_probed_step_assembly();
3776        self.import_step_flat(state, &pending.name, &pending.text);
3777    }
3778
3779    /// **Cancel** — import nothing and drop the parse (§3.9).
3780    fn step_assembly_cancel(&mut self, state: &mut EngineState) {
3781        let Some(pending) = self.pending_step_import.take() else {
3782            return;
3783        };
3784        state.discard_probed_step_assembly();
3785        self.status = format!("import cancelled: {}", pending.name);
3786    }
3787
3788    /// Switch to the **Assembly** workbench so a freshly imported structure is
3789    /// actually reachable: the Assembly Structure tree and the Constraints panel
3790    /// are CLAIMED by that workbench, so an assembly imported under Modeling
3791    /// would land with its structure invisible.
3792    ///
3793    /// No-op when the active workbench already shows those panels: the
3794    /// predicate asks whether the BOM panel is visible, which is true for
3795    /// Assembly itself, for Wire harness (it lists the assembly panels too),
3796    /// and for "All", whose users must not be yanked out of it. Applied
3797    /// through the same settings seam the toolbar dropdown and the saved-
3798    /// workbench restore use, and like the restore NOT persisted to the settings
3799    /// blob: that blob stays the user's boot preference, and a document's
3800    /// workbench is session-scoped.
3801    fn enter_assembly_workbench(&mut self, state: &mut EngineState) {
3802        if crate::workbench::panel_visible(
3803            &state.settings.workbench,
3804            crate::workbench::assembly::BOM_PANEL_ID,
3805            &crate::workbench::ButtonState::of(state),
3806        ) {
3807            return;
3808        }
3809        let _ = state.apply_settings_json(
3810            &serde_json::json!({ "workbench": crate::workbench::assembly::ASSEMBLY.id })
3811                .to_string(),
3812        );
3813    }
3814
3815    /// Append an imported IGES file to the model (an IMPORT3D feature) — the
3816    /// IGES sibling of [`Self::import_step`].
3817    fn import_iges(&mut self, state: &mut EngineState, name: &str, contents: &str) {
3818        match state.import_iges_feature(contents) {
3819            // Framing deferred to the engine (`pending_fit`) — see `import_step`.
3820            Ok(_) => self.status = format!("imported {name}"),
3821            Err(e) => self.status = format!("import failed: {e}"),
3822        }
3823    }
3824
3825    fn stage_mesh_preview(
3826        &mut self,
3827        format: MeshImportFormat,
3828        name: String,
3829        contents: Vec<u8>,
3830    ) {
3831        self.pending_stl_import = Some((format, name, contents));
3832        self.status.clear();
3833    }
3834
3835    /// Selecting an STL or a 3MF starts a preview session; only its Accept
3836    /// action edits the destination document.
3837    pub fn take_stl_import(&mut self) -> Option<(MeshImportFormat, String, Vec<u8>)> {
3838        self.pending_stl_import.take()
3839    }
3840
3841    fn import_obj(&mut self, state: &mut EngineState, name: &str, contents: &[u8]) {
3842        match state.import_obj_bytes_feature(contents) {
3843            Ok(_) => self.status = format!("reconstructing {name} in background…"),
3844            Err(e) => self.status = format!("import failed: {e}"),
3845        }
3846    }
3847
3848    fn import_text(
3849        &mut self,
3850        state: &mut EngineState,
3851        name: &str,
3852        bytes: &[u8],
3853        importer: fn(&mut Self, &mut EngineState, &str, &str),
3854    ) {
3855        match std::str::from_utf8(bytes) {
3856            Ok(contents) => importer(self, state, name, contents),
3857            Err(_) => self.status = format!("import failed: {name} is not UTF-8 text"),
3858        }
3859    }
3860
3861    /// Load a model document's contents into a NEW TAB (roll to the last
3862    /// feature + zoom-to-fit), clean from the start. A document that fails to
3863    /// load leaves no tab behind — the engine it was loading into is dropped
3864    /// with its runner.
3865    fn load_document(&mut self, docs: &mut Documents, name: &str, contents: &str, store: &dyn ModelStore) {
3866        let plm = store.plm_client().is_some();
3867        let mut engine = docs.spawn_engine();
3868        match engine.load_model_and_fit(contents) {
3869            Ok(_) => {
3870                // The extension names the class on the file system; the
3871                // document field follows it, before `Document::new` takes the
3872                // clean baseline so a hand-renamed file does not open dirty.
3873                // On a PLM store the name is a revision key the explorer spells
3874                // with `.nbrep` whatever the part is, so the document's own
3875                // field is the class (the part's fixed `document_class`).
3876                if let Some(class) = DocumentClass::of_name(name).filter(|_| !plm) {
3877                    document_class::set_document_class(&mut engine, class);
3878                }
3879                // The document keeps the raw identity — the full path when a
3880                // native dialog picked the file, so plain Save writes back to
3881                // it; the name field shows only the bare display name.
3882                let mut doc = Document::new(engine);
3883                doc.set_name(Some(name.to_string()));
3884                doc.note_opened_from(contents);
3885                docs.open_document(doc);
3886                self.name_buf = model_display_name(name);
3887                self.status = format!("opened {name}");
3888                remember_document(store, name);
3889            }
3890            Err(e) => self.status = format!("open failed: {e}"),
3891        }
3892    }
3893
3894    // --- document classes: families, templates, members ------------------------
3895
3896    /// The session's PLM client and the revision key `identity` names, when
3897    /// the store IS the PLM and `identity` is one of its revisions.
3898    fn plm_revision(&self, store: &dyn ModelStore, identity: &str) -> Option<(std::rc::Rc<crate::plm::client::PlmClient>, String)> {
3899        let client = store.plm_client()?;
3900        let key = crate::plm::family::revision_key(identity)?;
3901        Some((client, key))
3902    }
3903
3904    /// Poll the PLM work in flight (S9) and act on what finished.
3905    fn poll_plm_work(&mut self, ctx: &egui::Context, docs: &mut Documents, store: &dyn ModelStore) {
3906        let Some(work) = self.plm_work.as_mut() else { return };
3907        ctx.request_repaint_after(std::time::Duration::from_millis(30));
3908        match work {
3909            PlmWork::Members(pending) => {
3910                let Some(result) = pending.poll() else { return };
3911                self.plm_work = None;
3912                let Some(pick) = self.pick_member.as_mut() else { return };
3913                pick.loading = false;
3914                match result {
3915                    Ok(members) => {
3916                        pick.rows = members
3917                            .into_iter()
3918                            .map(|member| MemberChoice {
3919                                why_not: member.not_placeable(),
3920                                exists: member.not_placeable().is_none(),
3921                                identity: member.key(),
3922                                description: member.name.clone(),
3923                                part_number: member.number,
3924                            })
3925                            .collect();
3926                    }
3927                    Err(error) => self.status = format!("the members could not be read: {error}"),
3928                }
3929            }
3930            PlmWork::Place(pending) => {
3931                let Some(result) = pending.poll() else { return };
3932                self.plm_work = None;
3933                match result {
3934                    Ok((key, text, number)) => {
3935                        self.insert_component_named(docs.engine_mut(), &key, &number, &text);
3936                        self.pick_member = None;
3937                        self.spin_out = None;
3938                        self.open = false;
3939                    }
3940                    Err(error) => self.status = error,
3941                }
3942            }
3943            PlmWork::OpenFamily(pending) => {
3944                let Some(result) = pending.poll() else { return };
3945                self.plm_work = None;
3946                match result {
3947                    Ok(key) => self.open_document(docs, store, &key),
3948                    Err(error) => self.status = format!("the family could not be opened: {error}"),
3949                }
3950            }
3951        }
3952    }
3953
3954    /// Route an Insert-component choice by its class. A FAMILY arms the member
3955    /// picker and a TEMPLATE the spin-out prompt; both return `true` (handled,
3956    /// the modal switches). A normal part returns `false` and is inserted as
3957    /// it always was. The class is the file's extension, or the document's own
3958    /// field when the name carries none (a browser upload's bare name).
3959    fn begin_class_insert(
3960        &mut self,
3961        store: &dyn ModelStore,
3962        identity: &str,
3963        contents: &str,
3964        assembly: Option<String>,
3965    ) -> bool {
3966        let document: serde_json::Value = serde_json::from_str(contents).unwrap_or_default();
3967        let class = DocumentClass::of_name(identity)
3968            .filter(|class| *class != DocumentClass::Normal)
3969            .unwrap_or_else(|| DocumentClass::of_document(&document));
3970        let file = crate::store::file_name_of(identity);
3971        match class {
3972            DocumentClass::Normal | DocumentClass::Instructions => false,
3973            DocumentClass::Family if self.plm_revision(store, identity).is_some() => {
3974                let (client, key) = self.plm_revision(store, identity).unwrap_or_else(|| unreachable!());
3975                let Some((part, _)) = crate::plm::family::key_ids(&key).map(|(p, r)| (p.to_string(), r.to_string())) else {
3976                    return false;
3977                };
3978                self.plm_work = Some(PlmWork::Members(crate::panels::plm_family::Pending::new(Box::pin(async move {
3979                    crate::plm::family::family_view(&client, &part).await.map(|view| view.members()).map_err(|e| e.to_string())
3980                }))));
3981                self.pick_member = Some(PickMember { family_file: file, rows: Vec::new(), plm: true, loading: true });
3982                self.status.clear();
3983                self.open_modal(Mode::PickMember);
3984                true
3985            }
3986            DocumentClass::Family => {
3987                let table = family_table::read_table(contents);
3988                let rows = table
3989                    .rows
3990                    .iter()
3991                    .map(|row| {
3992                        let identity = family_table::member_identity(Some(identity), &row.part_number);
3993                        let exists = identity.as_deref().is_some_and(|id| store.read(id).is_some());
3994                        MemberChoice {
3995                            part_number: row.part_number.clone(),
3996                            description: row.description.clone(),
3997                            identity,
3998                            exists,
3999                            why_not: None,
4000                        }
4001                    })
4002                    .collect();
4003                self.pick_member = Some(PickMember { family_file: file, rows, plm: false, loading: false });
4004                self.status.clear();
4005                self.open_modal(Mode::PickMember);
4006                true
4007            }
4008            DocumentClass::Template => {
4009                let inputs = template::inputs_of(&document);
4010                let values = inputs
4011                    .iter()
4012                    .map(|input| (input.name.clone(), template::default_value(&document, input)))
4013                    .collect();
4014                let stem = document_class::strip_class_extension(&file).to_string();
4015                let plm_template = self
4016                    .plm_revision(store, identity)
4017                    .and_then(|(_, key)| crate::plm::family::key_ids(&key).map(|(part, _)| part.to_string()));
4018                self.spin_out = Some(SpinOut {
4019                    template_file: file,
4020                    template: document,
4021                    inputs,
4022                    name: format!("{stem}-1"),
4023                    values,
4024                    assembly,
4025                    plm_template,
4026                    number: String::new(),
4027                });
4028                self.status.clear();
4029                self.open_modal(Mode::SpinOut);
4030                true
4031            }
4032        }
4033    }
4034
4035    /// The **member picker**: a family is never placed itself. Each table row
4036    /// is a button; one whose member file exists inserts that `.nbrep` as a
4037    /// normal component, one that was never generated says so.
4038    fn show_pick_member(&mut self, ctx: &egui::Context, state: &mut EngineState, store: &dyn ModelStore) {
4039        let Some(pick) = self.pick_member.take() else {
4040            self.open = false;
4041            return;
4042        };
4043        let mut chosen = None;
4044        let mut cancel = false;
4045        let modal = egui::Modal::new(egui::Id::new("brep-file-pick-member")).show(ctx, |ui| {
4046            ui.set_width(420.0);
4047            ui.heading(format!("Insert a member of {}", pick.family_file));
4048            ui.add_space(2.0);
4049            ui.weak("A family is never placed itself: choose the member to place.");
4050            ui.add_space(6.0);
4051            if pick.loading {
4052                ui.label("Reading the family's members from the PLM\u{2026}");
4053            } else if pick.rows.is_empty() {
4054                ui.label("This family's table has no rows yet.");
4055            }
4056            egui::ScrollArea::vertical().max_height(320.0).show(ui, |ui| {
4057                for (index, row) in pick.rows.iter().enumerate() {
4058                    ui.horizontal(|ui| {
4059                        let text = if row.description.trim().is_empty() {
4060                            row.part_number.clone()
4061                        } else {
4062                            format!("{} \u{2014} {}", row.part_number, row.description)
4063                        };
4064                        let button = ui.add_enabled(
4065                            row.exists,
4066                            egui::Button::new(text).min_size(egui::vec2(220.0, 0.0)),
4067                        );
4068                        self.hit(&format!("member:{}", row.part_number), &button);
4069                        if button.clicked() {
4070                            chosen = Some(index);
4071                        }
4072                        if let Some(why) = &row.why_not {
4073                            ui.weak(format!("({why})"));
4074                        } else if row.identity.is_none() {
4075                            ui.weak("(the part number is not a file name)");
4076                        } else if !row.exists {
4077                            ui.weak("(not generated yet: open the family and Generate)");
4078                        }
4079                    });
4080                }
4081            });
4082            ui.add_space(6.0);
4083            if !self.status.is_empty() {
4084                ui.weak(&self.status);
4085                ui.add_space(4.0);
4086            }
4087            let c = ui.button("Cancel");
4088            self.hit("member:cancel", &c);
4089            cancel = c.clicked();
4090        });
4091        let should_close = modal.should_close();
4092        if let (Some(index), true) = (chosen, pick.plm) {
4093            let row = &pick.rows[index];
4094            if let (Some(client), Some(key)) = (store.plm_client(), row.identity.clone()) {
4095                self.status = format!("reading {} from the PLM\u{2026}", row.part_number);
4096                self.plm_work = Some(PlmWork::Place(fetch_document(client, key, row.part_number.clone())));
4097            }
4098        } else if let Some(index) = chosen {
4099            let row = &pick.rows[index];
4100            let identity = row.identity.clone().unwrap_or_default();
4101            match store.read(&identity) {
4102                Some(contents) => {
4103                    self.insert_component_document(state, &identity, &contents);
4104                    self.open = false;
4105                    return;
4106                }
4107                None => self.status = format!("'{}' has not been generated yet ({identity})", row.part_number),
4108            }
4109        } else if cancel || should_close {
4110            self.status.clear();
4111            self.open = false;
4112            return;
4113        }
4114        self.pick_member = Some(pick);
4115    }
4116
4117    /// The **template spin-out** prompt: a template is never placed itself.
4118    /// The user names the new part and sets each marked input (a fixed list
4119    /// is offered as buttons; limits are checked as they type), then Create
4120    /// writes the specialised copy as `<name>.nbrep` beside the assembly and
4121    /// places it. A name that already exists is refused, never overwritten.
4122    fn show_spin_out(&mut self, ctx: &egui::Context, state: &mut EngineState, store: &dyn ModelStore) {
4123        let Some(mut spin) = self.spin_out.take() else {
4124            self.open = false;
4125            return;
4126        };
4127        let mut create = false;
4128        let mut cancel = false;
4129        let problems: Vec<Option<String>> = spin
4130            .inputs
4131            .iter()
4132            .map(|input| {
4133                let text = spin.values.get(&input.name).map(String::as_str).unwrap_or("");
4134                template::value_problem(&spin.template, input, text)
4135            })
4136            .collect();
4137        let name_problem = template::name_problem(&spin.name);
4138        let modal = egui::Modal::new(egui::Id::new("brep-file-spin-out")).show(ctx, |ui| {
4139            ui.set_width(440.0);
4140            ui.heading(format!("New part from {}", spin.template_file));
4141            ui.add_space(2.0);
4142            ui.weak("A template is never placed itself: this makes a new part from it and places that.");
4143            ui.add_space(6.0);
4144            egui::Grid::new("brep-spin-out-grid").num_columns(2).spacing([10.0, 6.0]).show(ui, |ui| {
4145                ui.label("New part name");
4146                let field = ui.add(egui::TextEdit::singleline(&mut spin.name).margin(crate::viewport::touch::text_edit_margin(ui)).min_size(crate::viewport::touch::target_size(ui.ctx(), egui::Vec2::ZERO)).desired_width(240.0));
4147                self.hit("spinout:name", &field);
4148                ui.end_row();
4149                if spin.plm_template.is_some() {
4150                    ui.label("Part number");
4151                    let field = ui.add(
4152                        egui::TextEdit::singleline(&mut spin.number).margin(crate::viewport::touch::text_edit_margin(ui)).min_size(crate::viewport::touch::target_size(ui.ctx(), egui::Vec2::ZERO))
4153                            .hint_text("empty: the part type numbers it")
4154                            .desired_width(240.0),
4155                    );
4156                    self.hit("spinout:number", &field);
4157                    ui.end_row();
4158                }
4159                for (input, problem) in spin.inputs.iter().zip(&problems) {
4160                    ui.label(input.shown());
4161                    ui.vertical(|ui| {
4162                        let value = spin.values.entry(input.name.clone()).or_default();
4163                        if input.choices.is_empty() {
4164                            let field = ui.add(egui::TextEdit::singleline(value).margin(crate::viewport::touch::text_edit_margin(ui)).min_size(crate::viewport::touch::target_size(ui.ctx(), egui::Vec2::ZERO)).desired_width(240.0));
4165                            self.hit(&format!("spinout:input:{}", input.name), &field);
4166                        } else {
4167                            // The input's choices as one dropdown: spinout:choice:<name>,
4168                            // its open entries spinout:choice:<name>#<choice>.
4169                            let trimmed: Vec<String> = input.choices.iter().map(|choice| choice.trim().to_string()).collect();
4170                            let choices: Vec<(String, &str, &str)> = trimmed.iter().map(|choice| (choice.clone(), choice.as_str(), choice.as_str())).collect();
4171                            let mut chosen = value.trim().to_string();
4172                            let (changed, _) = crate::form::enum_dropdown(ui, &format!("spinout:choice:{}", input.name), &choices, &mut chosen, None, Some(&mut self.hits));
4173                            if changed {
4174                                *value = chosen;
4175                            }
4176                        }
4177                        let limits = match (input.min, input.max) {
4178                            (Some(min), Some(max)) => format!("{min} to {max}"),
4179                            (Some(min), None) => format!("at least {min}"),
4180                            (None, Some(max)) => format!("at most {max}"),
4181                            (None, None) => String::new(),
4182                        };
4183                        if !limits.is_empty() {
4184                            ui.weak(limits);
4185                        }
4186                        if let Some(problem) = problem {
4187                            ui.colored_label(ui.visuals().warn_fg_color, problem);
4188                        }
4189                    });
4190                    ui.end_row();
4191                }
4192            });
4193            if spin.inputs.is_empty() {
4194                ui.weak("This template marks no inputs; the copy is the template as it is.");
4195            }
4196            ui.add_space(4.0);
4197            let file = crate::store::file_name_of(&spin_out_target(&spin));
4198            ui.weak(match &spin.assembly {
4199                _ if spin.plm_template.is_some() => {
4200                    "The PLM makes the new part from the template and bakes it; it is placed as its first revision.".to_string()
4201                }
4202                Some(assembly) => format!(
4203                    "Saved as {file}, in the folder of {}",
4204                    crate::store::file_name_of(assembly)
4205                ),
4206                None => format!("The assembly is not saved yet: the part goes to the models folder as {file}"),
4207            });
4208            if let Some(problem) = &name_problem {
4209                ui.colored_label(ui.visuals().warn_fg_color, problem);
4210            }
4211            if !self.status.is_empty() {
4212                ui.weak(&self.status);
4213            }
4214            ui.add_space(6.0);
4215            ui.horizontal(|ui| {
4216                let ok = name_problem.is_none() && problems.iter().all(Option::is_none) && self.plm_work.is_none();
4217                let c = ui.add_enabled(ok, egui::Button::new("Create and insert"));
4218                self.hit("spinout:create", &c);
4219                create = c.clicked();
4220                let x = ui.button("Cancel");
4221                self.hit("spinout:cancel", &x);
4222                cancel = x.clicked();
4223            });
4224        });
4225        let should_close = modal.should_close();
4226        if create {
4227            match self.create_spin_out(state, store, &spin) {
4228                Ok(()) => {
4229                    self.open = false;
4230                    return;
4231                }
4232                Err(error) => self.status = error,
4233            }
4234        } else if cancel || should_close {
4235            self.status.clear();
4236            self.open = false;
4237            return;
4238        }
4239        self.spin_out = Some(spin);
4240    }
4241
4242    /// Write the spun-out part and place it. `Err` is the status line.
4243    fn create_spin_out(&mut self, state: &mut EngineState, store: &dyn ModelStore, spin: &SpinOut) -> Result<(), String> {
4244        if let (Some(template_part), Some(client)) = (spin.plm_template.clone(), store.plm_client()) {
4245            // The server makes the part; it is placed when it answers. The
4246            // dialog stays open until then, saying so.
4247            let (name, number, values) = (spin.name.trim().to_string(), spin.number.trim().to_string(), spin.values.clone());
4248            self.plm_work = Some(PlmWork::Place(crate::panels::plm_family::Pending::new(Box::pin(async move {
4249                let spun = crate::plm::family::spin_out(&client, &template_part, &name, &number, &values)
4250                    .await
4251                    .map_err(|e| e.to_string())?;
4252                let bytes = client.get_document(&spun.key).await.map_err(|e| e.to_string())?;
4253                let text = bytes.map(|b| String::from_utf8_lossy(&b).into_owned()).ok_or_else(|| {
4254                    format!("{} was made, but has no document yet", spun.number)
4255                })?;
4256                Ok((spun.key, text, spun.number))
4257            }))));
4258            return Err("making the part on the PLM\u{2026}".into());
4259        }
4260        if let Some(problem) = template::name_problem(&spin.name) {
4261            return Err(problem);
4262        }
4263        let target = spin_out_target(spin);
4264        if store.read(&target).is_some() {
4265            return Err(format!("'{target}' already exists: choose another name"));
4266        }
4267        let copy = template::spin_out(&spin.template, &spin.template_file, &spin.values)?;
4268        let text = copy.to_string();
4269        store.write(&target, &text).map_err(|error| format!("could not save {target}: {error}"))?;
4270        self.save_generation += 1;
4271        self.insert_component_document(state, &target, &text);
4272        if spin.assembly.is_none() {
4273            self.status = format!(
4274                "{} — saved to the models folder as {target}, because the assembly is not saved yet",
4275                self.status
4276            );
4277        }
4278        Ok(())
4279    }
4280
4281    /// Arm the hand-edit prompt when the active tab is a family member. `true`
4282    /// when it was armed (the caller does not go on to write). Raise the
4283    /// hand-edit prompt the frame a family member first shows an unsaved
4284    /// change — the moment the user "starts to change it".
4285    fn watch_member_edit(&mut self, docs: &Documents) {
4286        let doc = docs.active();
4287        if !doc.dirty_marker() || self.member_ack.contains(&doc.id()) {
4288            return;
4289        }
4290        if let Some(name) = doc.name().map(str::to_string) {
4291            self.arm_member_prompt(docs, &name, false);
4292        }
4293    }
4294
4295    fn arm_member_prompt(&mut self, docs: &Documents, name: &str, from_save: bool) -> bool {
4296        let Some(source) = family_table::engine_family_source(docs.engine()) else {
4297            return false;
4298        };
4299        self.member_prompt = Some(MemberPrompt {
4300            doc_id: docs.active().id(),
4301            family_identity: sibling_identity(name, &source.family),
4302            source,
4303            from_save,
4304        });
4305        self.status.clear();
4306        self.open_modal(Mode::MemberEdit);
4307        true
4308    }
4309
4310    /// Undo the active tab back to its saved state (bounded), so a member the
4311    /// user declined to change is left exactly as generated.
4312    fn revert_member_edit(docs: &mut Documents) {
4313        for _ in 0..64 {
4314            if !docs.active().is_dirty() || !docs.engine().history.can_undo() {
4315                break;
4316            }
4317            docs.engine_mut().undo();
4318        }
4319    }
4320
4321    /// The assembly Save's ONE dialog about the documents it owns: a row per
4322    /// document that needs a decision, each ticked (yes) by default, so Enter
4323    /// saves everything. An unticked row is not written and its change stays
4324    /// in the assembly. Cancel saves nothing.
4325    fn show_owned_save(&mut self, ctx: &egui::Context, docs: &mut Documents, store: &dyn ModelStore) {
4326        let Some(mut prompt) = self.owned_prompt.take() else {
4327            self.open = false;
4328            return;
4329        };
4330        if docs.active_id() != prompt.plan.doc_id {
4331            self.status = "Save cancelled because the active document changed".into();
4332            self.open = false;
4333            return;
4334        }
4335        let mut choice = None;
4336        let modal = egui::Modal::new(egui::Id::new("brep-file-owned-save")).show(ctx, |ui| {
4337            ui.set_width(520.0);
4338            ui.heading("Save the parts this assembly changed");
4339            ui.add_space(4.0);
4340            ui.label("Saving writes these documents too. Untick any you do not want written; its change stays in the assembly.");
4341            ui.add_space(6.0);
4342            for (i, row) in prompt.plan.rows.iter_mut().enumerate() {
4343                if !row.asks() {
4344                    if let Some(crate::document::owned_save::Issue::Blocked(_)) = row.issue {
4345                        ui.weak(row.question());
4346                    }
4347                    continue;
4348                }
4349                let question = row.question();
4350                let tick = ui.checkbox(&mut row.proceed, question);
4351                self.hit(&format!("owned:row:{i}"), &tick);
4352                if row.issue == Some(crate::document::owned_save::Issue::Conflict) && row.proceed {
4353                    ui.indent(("owned-conflict", i), |ui| {
4354                        // Which copy to keep, as one dropdown: owned:choice:<row>, its
4355                        // open entries owned:choice:<row>#<variant>.
4356                        let labels: Vec<(String, String)> = row
4357                            .write
4358                            .variants
4359                            .iter()
4360                            .enumerate()
4361                            .map(|(j, (names, _))| {
4362                                let last = if row.edited_last == Some(j) { " (edited last)" } else { "" };
4363                                (j.to_string(), format!("keep the copy in {}{last}", names.join(" > ")))
4364                            })
4365                            .collect();
4366                        let choices: Vec<(usize, &str, &str)> = labels.iter().enumerate().map(|(j, (id, label))| (j, id.as_str(), label.as_str())).collect();
4367                        crate::form::enum_dropdown(ui, &format!("owned:choice:{i}"), &choices, &mut row.choice, None, Some(&mut self.hits));
4368                    });
4369                }
4370            }
4371            if prompt.plan.rows.iter().any(|row| matches!(row.issue, Some(crate::document::owned_save::Issue::CheckOut { .. }))) {
4372                ui.add_space(4.0);
4373                let remember = ui.checkbox(&mut self.checkout_remembered, "Check out without asking again in this session");
4374                self.hit("owned:remember", &remember);
4375            }
4376            for promotion in &prompt.plan.promotions {
4377                let part = promotion.names.last().map_or("", String::as_str);
4378                ui.weak(if promotion.recreates() {
4379                    format!("{part}: its file is missing — re-created as {}", promotion.new_key)
4380                } else {
4381                    format!("{part}: saved to a new part file {}", promotion.new_key)
4382                });
4383            }
4384            ui.add_space(8.0);
4385            ui.horizontal(|ui| {
4386                let save = ui.button("Save");
4387                self.hit("owned:save", &save);
4388                if save.clicked() || ui.input(|i| i.key_pressed(egui::Key::Enter)) {
4389                    choice = Some(true);
4390                }
4391                let cancel = ui.button("Cancel");
4392                self.hit("owned:cancel", &cancel);
4393                if cancel.clicked() {
4394                    choice = Some(false);
4395                }
4396            });
4397        });
4398        let choice = choice.or_else(|| modal.should_close().then_some(false));
4399        self.owned_state = serde_json::json!({"prompt": prompt.plan.to_json()});
4400        match choice {
4401            Some(true) => {
4402                self.open = false;
4403                let OwnedPrompt { plan, name, browser } = prompt;
4404                if browser {
4405                    self.commit_save_browser(docs, store, name, plan);
4406                } else {
4407                    self.commit_save(docs, store, name, plan);
4408                }
4409            }
4410            Some(false) => {
4411                self.status = "Save cancelled".into();
4412                self.owned_state = serde_json::Value::Null;
4413                self.open = false;
4414            }
4415            None => self.owned_prompt = Some(prompt),
4416        }
4417    }
4418
4419    /// "Also point these assemblies at the new file?" — one question for every
4420    /// open assembly that places the part just saved under a new name. Yes
4421    /// (the default, Enter) moves each of their copies to the new file as one
4422    /// undoable edit per assembly; No leaves them on the old file.
4423    fn show_retarget(&mut self, ctx: &egui::Context, docs: &mut Documents) {
4424        let Some(retarget) = self.retarget.as_ref() else {
4425            self.open = false;
4426            return;
4427        };
4428        let mut choice = None;
4429        let new_name = model_display_name(&retarget.new_key);
4430        let old_name = model_display_name(&retarget.old);
4431        let titles: Vec<String> = retarget.tabs.iter().map(|(_, title)| title.clone()).collect();
4432        egui::Modal::new(egui::Id::new("brep-file-retarget")).show(ctx, |ui| {
4433            ui.set_width(480.0);
4434            ui.heading("Point the assemblies at the new file?");
4435            ui.label(format!(
4436                "{} {} '{old_name}'. Point {} at '{new_name}' too?",
4437                titles.join(", "),
4438                if titles.len() == 1 { "places" } else { "place" },
4439                if titles.len() == 1 { "it" } else { "them" },
4440            ));
4441            ui.add_space(8.0);
4442            ui.horizontal(|ui| {
4443                let yes = ui.button(format!("Yes, use '{new_name}'"));
4444                self.hit("retarget:yes", &yes);
4445                if yes.clicked() || ui.input(|i| i.key_pressed(egui::Key::Enter)) {
4446                    choice = Some(true);
4447                }
4448                let no = ui.button(format!("No, keep '{old_name}'"));
4449                self.hit("retarget:no", &no);
4450                if no.clicked() || ui.input(|i| i.key_pressed(egui::Key::Escape)) {
4451                    choice = Some(false);
4452                }
4453            });
4454        });
4455        let Some(yes) = choice else { return };
4456        let Some(retarget) = self.retarget.take() else { return };
4457        self.open = false;
4458        if !yes {
4459            self.status = format!("kept the assemblies on '{old_name}'");
4460            return;
4461        }
4462        let mut moved = Vec::new();
4463        for (id, title) in &retarget.tabs {
4464            match docs.retarget_copies(*id, &retarget.old, &retarget.new_key, &retarget.text) {
4465                Ok(n) if n > 0 => moved.push(title.clone()),
4466                Ok(_) => {}
4467                Err(error) => docs.engine_mut().push_notice(format!("{title}: could not point it at '{new_name}': {error}")),
4468            }
4469        }
4470        self.status = format!("{} now {} '{new_name}' (unsaved)", moved.join(", "), if moved.len() == 1 { "uses" } else { "use" });
4471        docs.engine_mut().push_notice_as(brep_render::engine_state::NoticeSeverity::Info, self.status.clone());
4472    }
4473
4474    /// The **hand-edit prompt** on a family member: a member is the family's
4475    /// output, so a change belongs either in a NEW part or in the family.
4476    fn show_member_edit(&mut self, ctx: &egui::Context, docs: &mut Documents, store: &dyn ModelStore) {
4477        let Some(prompt) = self.member_prompt.take() else {
4478            self.open = false;
4479            return;
4480        };
4481        if docs.active().id() != prompt.doc_id {
4482            self.open = false;
4483            return;
4484        }
4485        let mut choice = None;
4486        let plm = docs.active().name().and_then(|name| self.plm_revision(store, name));
4487        let modal = egui::Modal::new(egui::Id::new("brep-file-member-edit")).show(ctx, |ui| {
4488            ui.set_width(440.0);
4489            ui.heading("This part is a family member");
4490            ui.add_space(4.0);
4491            ui.label(format!(
4492                "'{}' was generated from the family '{}'. A member is written by its family, not edited by hand.",
4493                prompt.source.part_number, prompt.source.family
4494            ));
4495            ui.add_space(4.0);
4496            ui.weak(if prompt.from_save {
4497                "Save it as a new part, or open the family and make the change there."
4498            } else {
4499                "Keep the change in a new part, or open the family and make it there."
4500            });
4501            if !self.status.is_empty() {
4502                ui.add_space(4.0);
4503                ui.weak(&self.status);
4504            }
4505            ui.add_space(8.0);
4506            ui.horizontal(|ui| {
4507                let a = ui.button("Save as a new part\u{2026}");
4508                self.hit("member-edit:saveas", &a);
4509                if a.clicked() {
4510                    choice = Some("saveas");
4511                }
4512                // On a PLM only someone who may edit the family is offered it.
4513                let offer_family = match &plm {
4514                    Some((client, _)) => client.me().is_some_and(|me| me.groups.iter().any(|g| g == "author" || g == "admin")),
4515                    None => true,
4516                };
4517                if offer_family {
4518                    let b = ui.button("Open the family");
4519                    self.hit("member-edit:family", &b);
4520                    if b.clicked() {
4521                        choice = Some("family");
4522                    }
4523                }
4524                let c = ui.button(if prompt.from_save { "Cancel" } else { "Cancel (undo the change)" });
4525                self.hit("member-edit:cancel", &c);
4526                if c.clicked() {
4527                    choice = Some("cancel");
4528                }
4529            });
4530        });
4531        let choice = choice.or_else(|| modal.should_close().then_some("cancel"));
4532        match choice {
4533            Some("saveas") => {
4534                // The tab may keep changing; Save As strips the stamp from
4535                // the copy it writes. Plain Save still asks. On a PLM the
4536                // Save As opens on "A new part".
4537                self.member_ack.insert(prompt.doc_id);
4538                self.member_new_part = store.plm_client().is_some();
4539                self.status.clear();
4540                self.dispatch(FileAction::SaveAs, docs, store);
4541            }
4542            Some("family") if plm.is_some() => {
4543                let (client, _) = plm.unwrap_or_else(|| unreachable!());
4544                let number = document_class::strip_class_extension(&prompt.source.family).to_string();
4545                Self::revert_member_edit(docs);
4546                self.status = format!("opening the family {number}\u{2026}");
4547                self.plm_work = Some(PlmWork::OpenFamily(crate::panels::plm_family::Pending::new(Box::pin(async move {
4548                    let head = crate::plm::family::part_head(&client, &number).await.map_err(|e| e.to_string())?;
4549                    // The newest revision, drafts included (D13).
4550                    let newest = head.revisions.last().ok_or_else(|| format!("the family {number} has no revision"))?;
4551                    Ok(crate::plm::identity::document_key(&head.id, &newest.id))
4552                }))));
4553                self.open = false;
4554            }
4555            Some("family") => {
4556                if store.read(&prompt.family_identity).is_none() {
4557                    self.status = format!("the family file is not there: {}", prompt.family_identity);
4558                    self.member_prompt = Some(prompt);
4559                    return;
4560                }
4561                Self::revert_member_edit(docs);
4562                self.open = false;
4563                self.open_document(docs, store, &prompt.family_identity);
4564            }
4565            Some(_) => {
4566                if !prompt.from_save {
4567                    Self::revert_member_edit(docs);
4568                }
4569                self.status.clear();
4570                self.open = false;
4571            }
4572            None => self.member_prompt = Some(prompt),
4573        }
4574    }
4575
4576    /// Run the family's Generate on the ACTIVE document (a family seed) and
4577    /// keep the report for the automation blob. The one door the automation
4578    /// command and the table editor's button share.
4579    /// Generate the active family. On a PLM store (the document is a
4580    /// `part/…/rev/…` revision and the session has a client) the server
4581    /// writes the members from the family's SAVED revision: a family with
4582    /// unsaved edits is refused unless `save` is set, which saves it first
4583    /// (plm-cad-integration-todo S9). Everywhere else this is the file-system
4584    /// Generate, and `save` is ignored.
4585    pub fn generate_active_family(
4586        &mut self,
4587        docs: &mut Documents,
4588        store: &dyn ModelStore,
4589        save: bool,
4590    ) -> Result<family_table::GenerateReport, String> {
4591        let plm_key = docs.active().name().and_then(crate::plm::family::revision_key);
4592        let report = match (store.plm_client(), plm_key) {
4593            (Some(client), Some(key)) => self.generate_on_plm(docs, store, &client, &key, save)?,
4594            _ => family_table::generate_family(store, docs.active().name(), &docs.engine().history_request_json()),
4595        };
4596        Ok(self.record_generate(report))
4597    }
4598
4599    /// The PLM half of [`Self::generate_active_family`]: blocks this thread
4600    /// while the members are built and the server answers (native; the web
4601    /// build's editor polls instead).
4602    #[cfg(not(target_arch = "wasm32"))]
4603    fn generate_on_plm(
4604        &mut self,
4605        docs: &mut Documents,
4606        store: &dyn ModelStore,
4607        client: &std::rc::Rc<crate::plm::client::PlmClient>,
4608        key: &str,
4609        save: bool,
4610    ) -> Result<family_table::GenerateReport, String> {
4611        let json = docs.tab_engine().history_request_json();
4612        if docs.active().is_dirty() {
4613            if !save {
4614                return Err(crate::plm::family::UNSAVED_FAMILY.into());
4615            }
4616            store.write(key, &json)?;
4617            docs.active_mut().mark_clean();
4618            self.save_generation += 1;
4619            // The write is behind: wait for it to land before the server reads it.
4620            let deadline = std::time::Instant::now() + std::time::Duration::from_secs(60);
4621            while store.pending_writes() > 0 {
4622                if std::time::Instant::now() > deadline {
4623                    return Err("the family's save did not reach the server within 60 s".into());
4624                }
4625                crate::plm::native::run_pending();
4626                std::thread::sleep(std::time::Duration::from_millis(5));
4627            }
4628            if let Some(error) = store.take_persistence_errors().into_iter().next() {
4629                return Err(format!("the family was not saved, so nothing was generated: {error}"));
4630            }
4631        }
4632        crate::plm::native::block_on_for(crate::plm::family::generate(client, key, &json), std::time::Duration::from_secs(300))
4633            .ok_or("the PLM server did not answer Generate within 5 minutes")?
4634            .map_err(|e| e.to_string())
4635    }
4636
4637    #[cfg(target_arch = "wasm32")]
4638    fn generate_on_plm(
4639        &mut self,
4640        _: &mut Documents,
4641        _: &dyn ModelStore,
4642        _: &std::rc::Rc<crate::plm::client::PlmClient>,
4643        _: &str,
4644        _: bool,
4645    ) -> Result<family_table::GenerateReport, String> {
4646        Err("in the browser, Generate a PLM family from the family table's Generate button".into())
4647    }
4648
4649    fn record_generate(&mut self, report: family_table::GenerateReport) -> family_table::GenerateReport {
4650        if report.rows.iter().any(|row| row.status() == "written") {
4651            self.save_generation += 1;
4652        }
4653        self.status = format!("generate: {}", report.summary());
4654        self.last_generate = Some(report.clone());
4655        report
4656    }
4657
4658    // --- verifier hooks (wasm only) -------------------------------------------
4659
4660    /// The published state for the headed verifier: current name, dirty flag,
4661    /// backend label, the stored-document list, and the modal's open/mode.
4662    pub fn file_state_json(&self, docs: &Documents, store: &dyn ModelStore) -> String {
4663        serde_json::json!({
4664            "name": docs.active().name(),
4665            "nameBuf": self.name_buf,
4666            "selected": self.explorer.selected(),
4667            "location": store.browser_location(),
4668            // The last folder of each purpose (Open/Save As/Insert, Import,
4669            // KiCad), which is where that modal opens next.
4670            "folders": Purpose::ALL
4671                .iter()
4672                .map(|purpose| (purpose.slug().to_string(), serde_json::json!(self.folders.get(purpose))))
4673                .collect::<serde_json::Map<_, _>>(),
4674            // The CACHED marker, not `is_dirty()`: this blob is published every
4675            // frame, and `is_dirty()` answers by serializing the whole document
4676            // — for a document whose history holds an imported model that is
4677            // hundreds of KB of JSON built and thrown away per frame, on the
4678            // thread that also has to answer the pointer. `Document::refresh_dirty_marker`
4679            // exists for exactly this and is refreshed once per frame; the tab
4680            // strip's dot already reads it, so the two cannot disagree.
4681            "dirty": docs.active().dirty_marker(),
4682            "backend": store.backend_label(),
4683            "interchange": store.supports_file_interchange(),
4684            "list": store.list(),
4685            "status": self.status,
4686            "ownedSave": self.owned_state,
4687            // What an assembly Save still has out after its status says
4688            // "saved": checkouts (the assembly's own included), new revisions, and the Save that names them.
4689            // 0 = the PLM holds everything this Save will write.
4690            "ownedInFlight": self.owned_checkouts.len()
4691                + self.owned_revisions.as_ref().map_or(0, |r| r.requests.len())
4692                + usize::from(self.owned_followup.is_some())
4693                + usize::from(self.owned_top.is_some()),
4694            "retarget": self.retarget.as_ref().map(|r| serde_json::json!({
4695                "from": r.old,
4696                "to": r.new_key,
4697                "tabs": r.tabs.iter().map(|(_, title)| title.clone()).collect::<Vec<_>>(),
4698            })),
4699            "open": self.open,
4700            "mode": match self.mode {
4701                Mode::CreateComponentPart => "createcomponentpart",
4702                Mode::Open => "open",
4703                Mode::SaveAs => "saveas",
4704                Mode::ConfirmClose => "confirmclose",
4705                Mode::Export => "export",
4706                Mode::Import => "import",
4707                Mode::FlatPattern => "flatpattern",
4708                Mode::InsertComponent => "insertcomponent",
4709                Mode::StepAssembly => "stepassembly",
4710                Mode::Kicad => "kicad",
4711                Mode::PickMember => "pickmember",
4712                Mode::SpinOut => "spinout",
4713                Mode::MemberEdit => "memberedit",
4714                Mode::OwnedSave => "ownedsave",
4715                Mode::Retarget => "retarget",
4716                Mode::PickFile => "pickfile",
4717                Mode::SaveFile => "savefile",
4718            },
4719            // The Save-file chooser's export: its title, the name field, the
4720            // folder the explorer is in (where Save writes) and the size.
4721            "saveFile": self.save_file.as_ref().map(|pending| serde_json::json!({
4722                "title": pending.request.title,
4723                "name": self.save_name,
4724                "folder": store.local_files().map(|local| local.browser_location()),
4725                "bytes": pending.request.body.as_bytes().len(),
4726            })),
4727            "pickFile": self.pick_request.as_ref().map(|(tag, title)| serde_json::json!({ "tag": tag, "title": title })),
4728            "picked": self.picked.as_ref().map(|(tag, file)| serde_json::json!({
4729                "tag": tag, "name": file.name, "path": file.path, "size": file.bytes.len(),
4730            })),
4731            // The active document's class (from its own `documentClass`
4732            // field) and, for a family member, its stamp.
4733            "class": document_class::document_class(docs.engine()).slug(),
4734            "saveClass": self.save_class.slug(),
4735            "plmSaveAs": self.plm_save_as.as_ref().map(|state| state.state_json()),
4736            "familySource": family_table::engine_family_source(docs.engine()).map(|source| serde_json::json!({
4737                "family": source.family,
4738                "partNumber": source.part_number,
4739                "valuesHash": source.values_hash,
4740                "historyHash": source.history_hash,
4741            })),
4742            // The armed member picker: each row, where its member is, and
4743            // whether Generate has written it.
4744            "pickMember": self.pick_member.as_ref().map(|pick| serde_json::json!({
4745                "family": pick.family_file,
4746                "plm": pick.plm,
4747                "loading": pick.loading,
4748                "rows": pick.rows.iter().map(|row| serde_json::json!({
4749                    "partNumber": row.part_number,
4750                    "identity": row.identity,
4751                    "exists": row.exists,
4752                    "whyNot": row.why_not,
4753                })).collect::<Vec<_>>(),
4754            })),
4755            "plmBusy": self.plm_work.is_some(),
4756            // What the PLM workspace browser (the Open dialog) says went wrong.
4757            "workspaceProblem": self.workspace.problem_text(),
4758            // The armed template spin-out: the answers so far, where the new
4759            // part will go, and what (if anything) blocks Create.
4760            "spinOut": self.spin_out.as_ref().map(|spin| serde_json::json!({
4761                "template": spin.template_file,
4762                "name": spin.name,
4763                "values": spin.values,
4764                "inputs": spin.inputs.iter().map(|input| input.name.clone()).collect::<Vec<_>>(),
4765                "target": spin_out_target(spin),
4766                "problems": spin.inputs.iter().filter_map(|input| template::value_problem(
4767                    &spin.template,
4768                    input,
4769                    spin.values.get(&input.name).map(String::as_str).unwrap_or(""),
4770                )).chain(template::name_problem(&spin.name)).collect::<Vec<_>>(),
4771            })),
4772            "memberPrompt": self.member_prompt.as_ref().map(|prompt| serde_json::json!({
4773                "partNumber": prompt.source.part_number,
4774                "family": prompt.source.family,
4775                "familyIdentity": prompt.family_identity,
4776                "fromSave": prompt.from_save,
4777            })),
4778            "generate": self.last_generate.as_ref().map(|report| serde_json::json!({
4779                "summary": report.summary(),
4780                "rows": report.to_json(),
4781            })),
4782            "kicad": (self.open && self.mode == Mode::Kicad).then(|| self.kicad.state_json()),
4783            // The armed §3.9 assembly choice: the file it belongs to and the
4784            // counts the prompt is showing, so the headed verifier can confirm
4785            // the dialog appeared with the numbers the import will deliver.
4786            // A `.step` upload whose structure probe is still running on the
4787            // background runner (the "reading…" state).
4788            "probing": self.pending_step_probe.as_ref().map(|pending| pending.name.clone()),
4789            "stepAssembly": self.pending_step_import.as_ref().map(|pending| {
4790                serde_json::json!({
4791                    "name": pending.name,
4792                    "parts": pending.probe.parts,
4793                    "instances": pending.probe.instances,
4794                    "nestedDepth": pending.probe.nested_depth,
4795                    // The §3.9 checkbox: shown only when there is a tree to
4796                    // flatten, and OFF by default (import keeps the tree).
4797                    "flatten": pending.flatten,
4798                })
4799            }),
4800        })
4801        .to_string()
4802    }
4803
4804    /// The published widget hit-rects (egui points) for the headed verifier.
4805    pub fn hits_json(&self) -> String {
4806        crate::automation::hit_rects::hits_json(&self.hits)
4807    }
4808
4809    /// Record a widget's screen rect for the headed verifier (wasm only; a no-op
4810    /// elsewhere so the dialog code reads the same on both targets).
4811    fn hit(&mut self, key: &str, resp: &egui::Response) {
4812        self.hits.insert(key.to_string(), resp.rect);
4813    }
4814
4815    fn record_explorer_hits(&mut self, hits: &[(String, egui::Rect)]) {
4816        self.hits.extend(hits.iter().cloned());
4817    }
4818}
4819
4820
4821/// A sheet's name as a file-name fragment: spaces and separators become
4822/// hyphens, so `Sheet 1` exports as `<document>-Sheet-1.svg` and cannot
4823/// reach the store as a path.
4824fn slug(name: &str) -> String {
4825    let cleaned: String = name
4826        .chars()
4827        .map(|c| if c.is_alphanumeric() { c } else { '-' })
4828        .collect();
4829    let trimmed = cleaned.trim_matches('-').to_string();
4830    if trimmed.is_empty() { "sheet".into() } else { trimmed }
4831}
4832/// The hit keys this panel publishes (see `automation::hit_keys`).
4833pub static HIT_KEYS: &[HitKeyDoc] = &[
4834    HitKeyDoc { panel: "file", prefix: "field:name", meaning: "the document name field", command: None },
4835    HitKeyDoc { panel: "file", prefix: "save", meaning: "confirm save / save-as", command: None },
4836    HitKeyDoc { panel: "file", prefix: "cancel", meaning: "close the modal", command: None },
4837    HitKeyDoc { panel: "file", prefix: "confirm:cancel", meaning: "keep the dirty document open", command: None },
4838    HitKeyDoc { panel: "file", prefix: "confirm:discard", meaning: "discard the dirty document", command: None },
4839    HitKeyDoc { panel: "file", prefix: "export:", meaning: "export in a format (export:step, export:iges, export:stl, export:obj, export:glb, export:3mf, export:json, export:bomcsv, export:bomjson, the open DRAWING SHEET as export:sheetsvg, every drawing sheet as ONE multi-page PDF as export:sheetpdf, and the same drawing set with the live 3D model as export:sheetpdf3d; for a document whose PCB board has parts, its manufacturing files: the Gerber/drill/pick-and-place/BOM zip as export:fabrication, the pick-and-place CSV as export:pickplace, the electronics BOM CSV as export:ecadbom; for manufacturing instructions, the printed PDF as export:instructionspdf, every step's PNG and SVG as one zip as export:instructionsimages, and the self-contained offline player as export:instructionshtml)", command: None },
4840    HitKeyDoc { panel: "file", prefix: "flat:", meaning: "export the flat pattern (flat:dxf, flat:svg)", command: None },
4841    HitKeyDoc { panel: "file", prefix: "savefile:", meaning: "the Save-file chooser a desktop export opens after its format click, for WHERE the file goes: savefile:name is the file name field, and its explorer over this machine's files publishes the explorer's key set under the `savefile` prefix (savefile:<file name> a row to overwrite, savefile:crumb:<i>, savefile:place:<label>, savefile:up, savefile:path-edit, …); `save` writes the file where the explorer is, `cancel` drops it. In the browser an export is a download and the chooser never opens", command: None },
4842    HitKeyDoc { panel: "file", prefix: "stepassembly:", meaning: "a STEP-with-structure import choice (assembly, bodies, flatten, cancel)", command: None },
4843    HitKeyDoc { panel: "file", prefix: "insert:lib:", meaning: "insert a parts-library entry (insert:lib:name)", command: None },
4844    HitKeyDoc { panel: "file", prefix: "filesystem:", meaning: "an entry of the file explorer", command: None },
4845    HitKeyDoc { panel: "file", prefix: "saveas:", meaning: "PLM Save As: saveas:destination (one dropdown; its open entries saveas:destination#new-revision, #new-part, #existing), destination fields (part, revision, name, number), save and cancel", command: None },
4846    HitKeyDoc { panel: "file", prefix: "saveas:class", meaning: "Save As's class dropdown: saveas:class, its open entries saveas:class#normal (.nbrep), saveas:class#family (.fbrep), saveas:class#template (.tbrep)", command: None },
4847    HitKeyDoc { panel: "file", prefix: "member:", meaning: "the member picker Insert component raises for a family: member:<part number> places that member (disabled until Generate has written it), member:cancel", command: None },
4848    HitKeyDoc { panel: "file", prefix: "spinout:", meaning: "the template spin-out prompt: spinout:name, spinout:input:<name> (a value field), spinout:choice:<name> (a fixed-list input's dropdown; its open entries spinout:choice:<name>#<value>), spinout:create, spinout:cancel", command: None },
4849    HitKeyDoc { panel: "file", prefix: "retarget:", meaning: "after a part is saved under a new name, point the open assemblies that place it at the new file (retarget:yes, the default) or keep them on the old one (retarget:no)", command: None },
4850    HitKeyDoc { panel: "file", prefix: "owned:", meaning: "the assembly Save's questions about the documents it owns: owned:save (save, every ticked row), owned:cancel (save nothing), owned:row:<i> (tick/untick row i), owned:choice:<i> (which copy of a document changed differently to keep, a dropdown; its open entries owned:choice:<i>#<j>), owned:remember (check out without asking again in this session)", command: None },
4851    HitKeyDoc { panel: "file", prefix: "member-edit:", meaning: "the hand-edit prompt on a family member: member-edit:saveas, member-edit:family (open the family), member-edit:cancel (undo the change)", command: None },
4852    HitKeyDoc { panel: "file", prefix: "pickfile:", meaning: "the file chooser (any file on this machine, for a panel's Add file…): the explorer's key set under the `pickfile` prefix — pickfile:<file name> (a row), pickfile:confirm, pickfile:path-edit-toggle, …", command: Some("file_pick") },
4853    HitKeyDoc { panel: "file", prefix: "kicad:", meaning: "the KiCad import: its explorer (kicad:<file name>, kicad:confirm, kicad:upload and the explorer's other keys), then kicad:symbol:<library:name>, kicad:next, kicad:footprint:<library:name>, kicad:use-footprint, kicad:no-footprint, kicad:search, kicad:field:footprints, kicad:field:models, kicad:apply-folders, kicad:import, kicad:cancel", command: None },
4854];
4855
4856/// Read `key`'s document from the PLM, for placing it.
4857fn fetch_document(
4858    client: std::rc::Rc<crate::plm::client::PlmClient>,
4859    key: String,
4860    number: String,
4861) -> crate::panels::plm_family::Pending<(String, String, String)> {
4862    crate::panels::plm_family::Pending::new(Box::pin(async move {
4863        let bytes = client.get_document(&key).await.map_err(|e| e.to_string())?;
4864        let text = bytes
4865            .map(|b| String::from_utf8_lossy(&b).into_owned())
4866            .ok_or_else(|| format!("{key} has no document yet"))?;
4867        Ok((key, text, number))
4868    }))
4869}
4870
4871/// Recent documents are global store identities, never document content.
4872use crate::store::RECENT_DOCUMENTS_KEY;
4873pub fn recent_documents(store: &dyn ModelStore) -> Vec<String> {
4874    store
4875        .read(RECENT_DOCUMENTS_KEY)
4876        .and_then(|s| serde_json::from_str(&s).ok())
4877        .unwrap_or_default()
4878}
4879fn remember_document(store: &dyn ModelStore, name: &str) {
4880    let mut recent = recent_documents(store);
4881    recent.retain(|item| item != name);
4882    recent.insert(0, name.into());
4883    recent.truncate(20);
4884    let _ = store.write(
4885        RECENT_DOCUMENTS_KEY,
4886        &serde_json::to_string(&recent).unwrap(),
4887    );
4888}
4889
4890#[derive(Default)]
4891pub struct FileMenuOutcome {
4892    pub action: Option<FileAction>,
4893    pub recent: Option<String>,
4894    /// The Plugins row under Settings was clicked: open the plugin manager.
4895    pub plugins: bool,
4896    /// The JavaScript row under Settings was clicked: open the editor.
4897    pub javascript: bool,
4898}
4899
4900/// The side of a recent row's thumbnail square, in points.
4901pub const RECENT_THUMB_SIDE: f32 = crate::plm::thumbnail::RECENT_SIDE;
4902
4903/// The neutral square a recent document without a picture yet shows: the
4904/// same size as a thumbnail, never a gap. Drawn through the SVG loader the
4905/// menu's artwork already uses.
4906const RECENT_PLACEHOLDER_SVG: &str = r##"<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 40 40"><rect x="1.5" y="1.5" width="37" height="37" rx="4" fill="#8a8a8a" fill-opacity="0.16" stroke="#8a8a8a" stroke-opacity="0.45"/></svg>"##;
4907
4908/// Shared File menu for both appearances: the workbench picker on the left,
4909/// the document commands on the right, the recent documents (with their
4910/// thumbnails) in one column under both. Workbench selection uses apply +
4911/// store.
4912pub fn file_menu(
4913    ui: &mut egui::Ui,
4914    state: &mut EngineState,
4915    store: &dyn ModelStore,
4916    settings_open: &mut bool,
4917    properties_open: &mut bool,
4918    hits: &mut HashMap<String, egui::Rect>,
4919) -> FileMenuOutcome {
4920    use crate::panels::toolbar_button;
4921    let mut outcome = FileMenuOutcome::default();
4922    let presentation = crate::workbench::file_presentation;
4923    egui_extras::install_image_loaders(ui.ctx());
4924    let menu = ui.menu_button("File", |ui| {
4925        // Plugin workbenches declare no artwork of their own; they share the
4926        // plugin manager's glyph.
4927        let plugin_glyph = presentation("workbench:manage:plugins").glyph;
4928        let dynamic = crate::workbench::owned_workbenches(state);
4929        let workbench_rows: Vec<(&str, &str)> = crate::workbench::WORKBENCHES
4930            .iter()
4931            .map(|w| (w.glyph, w.label))
4932            .chain(dynamic.iter().map(|w| (plugin_glyph, w.label.as_str())))
4933            .collect();
4934        let file_rows: Vec<_> = crate::workbench::FILE_COMMANDS
4935            .iter()
4936            .map(|p| (p.glyph, p.label()))
4937            .collect();
4938        // Fixed measured widths — one per column — avoid feeding the popup's
4939        // previous width back through full-width rows while egui settles its
4940        // area. The recent rows span both columns.
4941        let left_width = toolbar_button::menu_width_salted(ui, "workbench-measure", &workbench_rows);
4942        let right_width = toolbar_button::menu_width(ui, &file_rows);
4943        let gap = ui.spacing().item_spacing.x;
4944        let compact = crate::viewport::touch::compact(ui.ctx());
4945        let width = if compact { left_width.max(right_width).min((ui.ctx().content_rect().width() - 32.0).max(100.0)) }
4946            else { left_width + gap + right_width };
4947        let (left_width, right_width) = if compact { (width, width) } else { (left_width, right_width) };
4948        ui.set_width(width);
4949        let max_height = ui.ctx().content_rect().height() * 0.8;
4950        ui.set_max_height(max_height);
4951        // The whole body sits in one scroll area so a window too short for
4952        // the columns themselves still shows a menu that fits; on any taller
4953        // window the columns never scroll and only the recent list does, up
4954        // to the room left under the cap (`cap_bottom`).
4955        let cap_bottom = ui.max_rect().bottom();
4956        let body = egui::ScrollArea::vertical()
4957            .id_salt("file-menu-body")
4958            .auto_shrink([false, true])
4959            .max_height(max_height)
4960            .show(ui, |ui| {
4961                let columns = |ui: &mut egui::Ui| {
4962                    // LEFT: the workbench picker.
4963                    ui.vertical(|ui| {
4964                        ui.set_width(left_width);
4965                        ui.weak("Workbench");
4966                        let current = crate::workbench::resolved_id(state, &state.settings.workbench);
4967                        if current != state.settings.workbench {
4968                            ui.add(
4969                                egui::Label::new(
4970                                    egui::RichText::new(format!(
4971                                        "Unavailable workbench: {}; using Modeling",
4972                                        state.settings.workbench
4973                                    ))
4974                                    .weak(),
4975                                )
4976                                .wrap(),
4977                            );
4978                        }
4979                        let options = crate::workbench::WORKBENCHES
4980                            .iter()
4981                            .map(|w| (w.id, w.label, w.glyph))
4982                            .chain(dynamic.iter().map(|w| (w.id.as_str(), w.label.as_str(), plugin_glyph)));
4983                        for (id, label, glyph) in options {
4984                            let row = toolbar_button::menu_row(ui, glyph, label, current == id, left_width);
4985                            hits.insert(format!("workbench:item:{id}"), row.rect);
4986                            if row.clicked() {
4987                                let _ = state
4988                                    .apply_settings_json(&serde_json::json!({"workbench": id}).to_string());
4989                                let _ = store.write(crate::store::SETTINGS_KEY, &state.settings_json());
4990                                ui.close();
4991                            }
4992                        }
4993                    });
4994                    // RIGHT: everything else, in the order the one-column menu had.
4995                    ui.vertical(|ui| {
4996                        ui.set_width(right_width);
4997                        ui.weak("Document");
4998                        for (class, p) in DocumentClass::ALL
4999                            .into_iter()
5000                            .zip(crate::workbench::NEW_DOCUMENT_COMMANDS)
5001                        {
5002                            let row = toolbar_button::menu_row(ui, p.glyph, p.label(), false, right_width);
5003                            hits.insert(p.id.into(), row.rect);
5004                            if class == DocumentClass::Normal {
5005                                hits.insert("file:new".into(), row.rect);
5006                                hits.insert("file:newmenu".into(), row.rect);
5007                            }
5008                            if row.clicked() {
5009                                outcome.action = Some(FileAction::NewOfClass(class));
5010                                ui.close();
5011                            }
5012                        }
5013                        for (id, action) in [
5014                            ("file:open", FileAction::Open),
5015                            ("file:save", FileAction::Save),
5016                            ("file:saveas", FileAction::SaveAs),
5017                            ("file:import", FileAction::Import),
5018                            ("file:export", FileAction::Export),
5019                        ] {
5020                            let p = presentation(id);
5021                            let row = toolbar_button::menu_row(ui, p.glyph, p.label(), false, right_width);
5022                            hits.insert(id.into(), row.rect);
5023                            if row.clicked() {
5024                                outcome.action = Some(action);
5025                                ui.close();
5026                            }
5027                        }
5028                        let properties = toolbar_button::menu_row(
5029                            ui,
5030                            presentation("properties").glyph,
5031                            presentation("properties").label(),
5032                            *properties_open,
5033                            right_width,
5034                        );
5035                        hits.insert("properties".into(), properties.rect);
5036                        if properties.clicked() {
5037                            *properties_open = !*properties_open;
5038                            ui.close();
5039                        }
5040                        let settings = toolbar_button::menu_row(
5041                            ui,
5042                            presentation("settings").glyph,
5043                            presentation("settings").label(),
5044                            *settings_open,
5045                            right_width,
5046                        );
5047                        hits.insert("settings".into(), settings.rect);
5048                        if settings.clicked() {
5049                            *settings_open = !*settings_open;
5050                            ui.close();
5051                        }
5052                        // Plugin management sits under Settings: the plugin manager
5053                        // and the JavaScript editor, the same rows in both styles.
5054                        for (id, flag) in [
5055                            ("workbench:manage:plugins", &mut outcome.plugins),
5056                            ("workbench:manage:javascript", &mut outcome.javascript),
5057                        ] {
5058                            let row = toolbar_button::menu_row(
5059                                ui,
5060                                presentation(id).glyph,
5061                                presentation(id).label(),
5062                                false,
5063                                right_width,
5064                            );
5065                            hits.insert(id.into(), row.rect);
5066                            if row.clicked() {
5067                                *flag = true;
5068                                ui.close();
5069                            }
5070                        }
5071                    });
5072                };
5073                if compact { ui.vertical(columns); } else { ui.horizontal_top(columns); }
5074
5075                // BELOW BOTH: the recent documents, one column, a thumbnail each. The
5076                // list scrolls under the 80% cap; the columns above it never do.
5077                let recent = recent_documents(store);
5078                if !recent.is_empty() {
5079                    ui.separator();
5080                    let caption = ui.weak("Recent Documents");
5081                    hits.insert("file:recent".into(), caption.rect);
5082                    let thumbs = crate::plm::thumbnail::recent_textures(ui.ctx(), store);
5083                    let side = egui::Vec2::splat(RECENT_THUMB_SIDE);
5084                    let row_height = RECENT_THUMB_SIDE + 2.0 * ui.spacing().button_padding.y;
5085                    // The room left under the cap; at least two rows even when the
5086                    // columns already fill it (then the outer area scrolls).
5087                    let room = cap_bottom - ui.cursor().top();
5088                    let list_height = room.max(2.0 * row_height + ui.spacing().item_spacing.y);
5089                    egui::ScrollArea::vertical()
5090                        .id_salt("file-recent")
5091                        .auto_shrink([false, true])
5092                        .max_height(list_height)
5093                        .show(ui, |ui| {
5094                            for name in &recent {
5095                                let image = match thumbs.get(name) {
5096                                    Some((_, texture)) => egui::Image::from_texture((texture.id(), side)),
5097                                    None => egui::Image::new(egui::ImageSource::Bytes {
5098                                        uri: "bytes://brep-recent-thumb/placeholder.svg".into(),
5099                                        bytes: egui::load::Bytes::Static(RECENT_PLACEHOLDER_SVG.as_bytes()),
5100                                    }),
5101                                }
5102                                .fit_to_exact_size(side);
5103                                let row = ui
5104                                    .add(
5105                                        egui::Button::new((image, model_display_name(name)))
5106                                            .wrap_mode(egui::TextWrapMode::Truncate)
5107                                            .min_size(egui::vec2(width, row_height)),
5108                                    )
5109                                    .on_hover_text(name);
5110                                hits.insert(format!("file:recent:{name}"), row.rect);
5111                                let padding = ui.spacing().button_padding;
5112                                let thumb = egui::Rect::from_min_size(
5113                                    row.rect.min + egui::vec2(padding.x, (row.rect.height() - RECENT_THUMB_SIDE) / 2.0),
5114                                    side,
5115                                );
5116                                hits.insert(format!("file:recent:thumb:{name}"), thumb);
5117                                if row.clicked() {
5118                                    outcome.recent = Some(name.clone());
5119                                    ui.close();
5120                                }
5121                            }
5122                        });
5123                }
5124            });
5125        if compact { hits.insert("file:clip".into(), body.inner_rect); }
5126    });
5127    hits.insert("file:menu".into(), menu.response.rect);
5128    // Retain the historic menu-opener key for coordinate automation.
5129    hits.insert("workbench".into(), menu.response.rect);
5130    outcome
5131}
5132
5133
5134/// The library ids of the tools the open instructions use (the tools whose
5135/// pictures an export fetches).
5136pub fn linked_tool_ids(engine: &EngineState) -> Vec<String> {
5137    engine
5138        .instructions()
5139        .ok()
5140        .flatten()
5141        .map(|block| block.tools_list().into_iter().filter_map(|(tool, _)| tool.tool_id).collect())
5142        .unwrap_or_default()
5143}