Skip to main content

brep_app/panels/
scene.rs

1//! Scene panel — the engine-native **Scene tree** ("Scene Manager"), the second
2//! sidebar tree in the design reference. Built on the SAME reusable [`tree`] node
3//! helper (connector lines + `[+]`/`[-]` collapse boxes) the history panel uses,
4//! so the two trees read as one system.
5//!
6//! # What it draws
7//!
8//! Above the tree sits a **type-visibility button row** (`Faces` / `Edges` /
9//! `Vertices`, plus `Sketches` / `Planes` when the scene has them): each button
10//! hides/shows EVERY object of that type SCENE-WIDE in one click — the missing
11//! bulk complement to the per-solid group tristate below. The toggle follows the
12//! same rule as the group checkbox + selection-filter toggle-all: if EVERY object
13//! of that type is currently visible → hide them all; otherwise (some or all
14//! hidden) → show them all. There is deliberately no `Solids` button — the Scene
15//! ROOT checkbox already toggles every solid.
16//!
17//! A file-tree of the engine's display scene:
18//!   * `[-] Scene <☑>` — the root; its checkbox toggles ALL solids' visibility.
19//!   * per solid `[-] <Name> <☑>` — the checkbox toggles that solid's visibility
20//!     through the engine ([`EngineState::set_visible`]); expands to
21//!   * `[+] Faces <☑>`, `[+] Edges <☑>`, `[+] Vertices <☑>` — each expands to the
22//!     individual entities BY KERNEL NAME (vertices by index/position).
23//!
24//! # Selection sync (both ways)
25//!
26//! Clicking an entity row drives the engine's name-based SELECTION
27//! ([`EngineState::select_by_name`] / [`select_vertex_by_position`](brep_render::engine_state::EngineState::select_vertex_by_position)) so it
28//! highlights (emphasis) in the viewport; and the engine's CURRENT selection
29//! (`state.emphasis`) bolds the matching tree row — the same emphasis the
30//! viewport reads, so a viewport pick lights up the tree and vice-versa.
31//!
32//! # Hover sync (row → viewport)
33//!
34//! MOUSING OVER a row highlights that entity in the 3D view exactly as mousing
35//! over it in the viewport does: the row feeds the SAME `emphasis` hover buckets
36//! the viewport's `hover_at` fills, through the name-based twins
37//! ([`EngineState::hover_by_name`] / [`hover_vertex_by_position`](brep_render::engine_state::EngineState::hover_vertex_by_position)) — so there is
38//! one hover highlight, one style, one clear. It lights exactly the rows whose
39//! CLICK selects one 3D entity (solids, committed sketches, datums/planes, and
40//! the face/edge/vertex leaves); the Scene root and the Faces/Edges/Vertices
41//! GROUP nodes are not things in the 3D view, so they stay unlit. The pointer is
42//! off the viewport while it is over a row, so the engine's one-frame
43//! [`EngineState::take_scene_tree_hover`] flag makes the viewport's hover pass
44//! yield instead of clearing the row's highlight.
45//!
46//! # This panel OWNS NO model state
47//!
48//! The scene + selection live in the engine ([`EngineState`], the single source
49//! of truth). The panel holds only transient UI state: which nodes are expanded
50//! and the per-frame `hits` map (widget screen rects) the headed verifier reads.
51//! Each frame it snapshots the scene into owned rows FIRST, draws from that, and
52//! applies at most one deferred engine mutation after the draw loop (so no borrow
53//! of `state` is held across a `&mut` call — the history panel's pattern).
54//!
55//! # Two deliberate reshapes (functional-over-1:1, per the design doc)
56//!
57//! * The visibility checkbox is drawn in the row's RIGHT slot: the shared tree
58//!   widget reserves the left columns for the collapse box + connector + glyph,
59//!   and (per the constraints) it is reused verbatim, not modified. Functionally
60//!   identical to the reference's left checkbox.
61//! * **Per-entity visibility (live):** the engine now hides individual faces /
62//!   edges / vertices and whole groups ([`EngineState::set_entity_visible`] /
63//!   [`EngineState::set_group_visible`]). So each Faces/Edges/Vertices group
64//!   checkbox is a live TRISTATE (all / some / none of that kind shown) and each
65//!   entity leaf carries its own live checkbox — the render pass skips a hidden
66//!   entity's triangles / segments / point. The whole-solid + whole-scene
67//!   checkboxes still compose: a hidden solid draws nothing; re-showing it keeps
68//!   any per-entity hides intact.
69
70use crate::automation::hit_keys::HitKeyDoc;
71use crate::panels::toolbar_button;
72use crate::panels::tree::{self, TreeRow};
73use brep_render::engine_state::EngineState;
74use brep_render::style::OrderedNames;
75use brep_render::visibility::{EntityKind as VisKind, GroupState};
76use eframe::egui;
77use std::collections::{HashMap, HashSet};
78
79/// One entity leaf's identity — how a click maps to an engine selection call.
80#[derive(Clone)]
81enum EntityKind {
82    /// Face, selected by kernel name (empty = unnamed → not selectable).
83    Face(String),
84    /// Edge, selected by kernel name (empty = unnamed → not selectable).
85    Edge(String),
86    /// Vertex, selected by owning-solid + world position (no kernel name).
87    Vertex([f64; 3]),
88}
89
90/// One row under a Faces/Edges/Vertices group — a display label, its selection
91/// identity, whether it is currently in the engine selection, and whether it is
92/// currently VISIBLE in the engine (its live per-entity checkbox state).
93#[derive(Clone)]
94struct Entity {
95    label: String,
96    kind: EntityKind,
97    selected: bool,
98    visible: bool,
99}
100
101/// One solid's owned snapshot for the frame (decoupled from `state.scene` so the
102/// draw loop can issue deferred `&mut state` mutations afterwards).
103struct SolidRow {
104    name: String,
105    visible: bool,
106    selected: bool,
107    faces: Vec<Entity>,
108    edges: Vec<Entity>,
109    vertices: Vec<Entity>,
110}
111
112/// A deferred engine mutation, collected during the draw and applied once after
113/// the loop (one per frame — the history panel's pattern).
114enum Action {
115    SetVisible(String, bool),
116    SetAllVisible(bool),
117    /// Hide/show one entity: `(solid, kind, index-in-kind-list, visible)`.
118    SetEntityVisible(String, VisKind, usize, bool),
119    /// Hide/show a whole group: `(solid, kind, visible)`.
120    SetGroupVisible(String, VisKind, bool),
121    /// Hide/show one group KIND across EVERY solid in the scene: `(kind, visible)`.
122    SetAllGroupVisible(VisKind, bool),
123    /// Hide/show EVERY committed sketch's overlay: `(visible)`.
124    SetAllSketchVisible(bool),
125    /// Hide/show EVERY construction datum/plane: `(visible)`.
126    SetAllDatumVisible(bool),
127    Select(&'static str, String),
128    SelectVertex(String, [f64; 3]),
129    /// Show/hide a committed sketch's persistent overlay: `(feature-id, visible)`.
130    SetSketchVisible(String, bool),
131    /// Show/hide a construction datum/plane's plane: `(frame-name, visible)`.
132    SetDatumVisible(String, bool),
133    /// Select a construction datum/plane by frame NAME (a row click).
134    SelectDatum(String),
135}
136
137/// The row the pointer is over this frame — the hover twin of [`Action`]'s
138/// select arms, and deliberately the SAME targets: a row hover previews exactly
139/// what that row's click would select. Collected during the draw, applied once
140/// after it (like [`Action`], so no borrow of `state` is held across the loop).
141enum Hover {
142    /// Solid / committed sketch / face / edge / datum — by kernel (or frame) NAME.
143    Named(&'static str, String),
144    /// A vertex: owning solid + world position (vertices carry no kernel name).
145    Vertex(String, [f64; 3]),
146}
147
148/// The Scene tree panel's transient UI state (the scene + selection live in the
149/// engine).
150#[derive(Default)]
151pub struct ScenePanel {
152    /// Per-frame egui widget screen rects, published to JS for the headed
153    /// verifier. Rebuilt every frame.
154    hits: HashMap<String, egui::Rect>,
155    /// The Scene ROOT is collapsed (absent/false = open — it defaults open).
156    root_collapsed: bool,
157    /// Solids explicitly COLLAPSED, by name (absent = open — solids default open,
158    /// matching the reference showing a solid's Faces/Edges/Vertices).
159    collapsed_solids: HashSet<String>,
160    /// Faces/Edges/Vertices group nodes explicitly EXPANDED, keyed
161    /// `"<solid>/<group>"` (absent = collapsed — groups default collapsed `[+]`).
162    expanded_groups: HashSet<String>,
163    /// The `Planes & Datums` group node is collapsed (absent/false = open — it
164    /// defaults open so construction datums are visible in the tree).
165    datums_collapsed: bool,
166}
167
168impl ScenePanel {
169    pub fn new() -> Self {
170        Self::default()
171    }
172
173    /// Draw the Scene tree. Snapshots the scene + current selection into owned
174    /// rows, draws them via the shared [`tree`] node helper, then applies at most
175    /// one deferred engine mutation.
176    pub fn show(&mut self, ui: &mut egui::Ui, state: &mut EngineState) {
177        self.hits.clear();
178
179        // --- snapshot scene + selection (owned) so we can mutate after drawing --
180        let sel_solids = state.emphasis.selected_solids.clone();
181        let sel_faces = state.emphasis.selected_faces.clone();
182        let sel_edges = state.emphasis.selected_edges.clone();
183        let sel_vertices = state.emphasis.selected_vertices.clone();
184        let solids = snapshot(state, &sel_solids, &sel_faces, &sel_edges, &sel_vertices);
185        // Committed sketches (id, visible) — listed under the solids in the tree.
186        let sketches = state.committed_sketches();
187        // Construction datums/planes (name, visible) + which are selected — listed as
188        // the tree's LAST group.
189        let sel_datums = state.emphasis.selected_datums.clone();
190        let datums = state.construction_datums();
191
192        // Tight, tree-like row spacing so connector verticals read continuously.
193        ui.spacing_mut().item_spacing.y = 2.0;
194
195        let mut action: Option<Action> = None;
196        // The row under the pointer (at most one — rows don't overlap).
197        let mut hover: Option<Hover> = None;
198
199        // --- TYPE-VISIBILITY button row (scene-wide, ABOVE the tree) -----------
200        // One button per display TYPE; each hides/shows EVERY object of that type
201        // across the whole scene. Same toggle rule as the per-solid group tristate
202        // + the selection-filter toggle-all: all-visible → hide all; otherwise
203        // (some or all hidden) → show all. Buttons show a "pressed" (selected) look
204        // when all of that type are currently visible. There is no `Solids` button
205        // — the Scene root checkbox already toggles every solid.
206        ui.horizontal(|ui| {
207            let has_solids = !solids.is_empty();
208
209            // Faces / Edges / Vertices — scene-wide across ALL solids. All-visible
210            // means every solid reports GroupState::All for that kind.
211            let groups: [(VisKind, &str, bool); 3] = [
212                (
213                    VisKind::Face,
214                    "Faces",
215                    has_solids
216                        && solids
217                            .iter()
218                            .all(|s| matches!(group_state(&s.faces), GroupState::All)),
219                ),
220                (
221                    VisKind::Edge,
222                    "Edges",
223                    has_solids
224                        && solids
225                            .iter()
226                            .all(|s| matches!(group_state(&s.edges), GroupState::All)),
227                ),
228                (
229                    VisKind::Vertex,
230                    "Vertices",
231                    has_solids
232                        && solids
233                            .iter()
234                            .all(|s| matches!(group_state(&s.vertices), GroupState::All)),
235                ),
236            ];
237            for (kind, label, all_visible) in groups {
238                let tip = if all_visible {
239                    format!("Hide all {}", label.to_lowercase())
240                } else {
241                    format!("Show all {}", label.to_lowercase())
242                };
243                // Disabled (greyed + non-interactive) with no solids, matching the
244                // Scene root checkbox's `add_enabled(!solids.is_empty(), …)` spirit.
245                let resp = ui
246                    .add_enabled_ui(has_solids, |ui| {
247                        toolbar_button::toggle(ui, all_visible, label, &tip)
248                    })
249                    .inner;
250                self.hits.insert(format!("typevis:{label}"), resp.rect);
251                if resp.clicked() {
252                    action = Some(Action::SetAllGroupVisible(kind, !all_visible));
253                }
254            }
255
256            // Sketches — shown only when the scene has committed sketches (mirrors
257            // `render_sketches` being conditional). All-visible = every sketch shown.
258            if !sketches.is_empty() {
259                let all_visible = sketches.iter().all(|(_, v)| *v);
260                let tip = if all_visible {
261                    "Hide all sketches"
262                } else {
263                    "Show all sketches"
264                };
265                let resp = toolbar_button::toggle(ui, all_visible, "Sketches", tip);
266                self.hits.insert("typevis:Sketches".into(), resp.rect);
267                if resp.clicked() {
268                    action = Some(Action::SetAllSketchVisible(!all_visible));
269                }
270            }
271
272            // Planes & Datums — shown only when the scene has construction datums
273            // (mirrors `render_datums` being conditional). Hit key is the literal
274            // `typevis:Datums` the verifier drives; the button LABEL reads "Planes".
275            if !datums.is_empty() {
276                let all_visible = datums.iter().all(|(_, v)| *v);
277                let tip = if all_visible {
278                    "Hide all planes & datums"
279                } else {
280                    "Show all planes & datums"
281                };
282                let resp = toolbar_button::toggle(ui, all_visible, "Planes", tip);
283                self.hits.insert("typevis:Datums".into(), resp.rect);
284                if resp.clicked() {
285                    action = Some(Action::SetAllDatumVisible(!all_visible));
286                }
287            }
288        });
289
290        // --- ROOT: `[-] Scene  ☑` (visibility toggles every solid) ------------
291        let root_open = !self.root_collapsed;
292        let all_visible = !solids.is_empty() && solids.iter().all(|s| s.visible);
293        let mut root_vis = all_visible;
294        let mut root_vis_rect = egui::Rect::NOTHING;
295        let mut root_vis_clicked = false;
296        let root_resp = tree::node(
297            ui,
298            TreeRow {
299                guides: &[],
300                is_last: true,
301                expandable: true,
302                expanded: root_open,
303                root: true,
304                glyph: None,
305                label: "Scene",
306                selected: false,
307                highlighted: false,
308                draggable: false,
309                tint: None,
310            },
311            |ui| {
312                let cb = ui.add_enabled(!solids.is_empty(), egui::Checkbox::new(&mut root_vis, ""));
313                root_vis_rect = cb.rect;
314                root_vis_clicked = cb.clicked();
315            },
316        );
317        self.hits.insert("box:__scene".into(), root_resp.box_rect);
318        self.hits.insert("vis:__scene".into(), root_vis_rect);
319        if root_vis_clicked {
320            action = Some(Action::SetAllVisible(root_vis));
321        }
322        if root_resp.toggled || root_resp.clicked() {
323            self.root_collapsed = !self.root_collapsed;
324        }
325
326        if solids.is_empty() && sketches.is_empty() && datums.is_empty() {
327            let g = tree::child_guides(&[], true);
328            tree::node(ui, TreeRow::leaf(&g, true, "(scene is empty)"), |_| {});
329        }
330
331        if root_open {
332            // Top-level children under the root, in order: solids, then each committed
333            // sketch as its OWN top-level row (no group wrapper — a committed sketch is
334            // a scene solid, listed like the solids), then the `Planes & Datums` group
335            // (the last child), so nothing "below" is marked last while a later child
336            // still follows.
337            let has_sketches = !sketches.is_empty();
338            let has_datums = !datums.is_empty();
339            let n = solids.len();
340            for (si, solid) in solids.iter().enumerate() {
341                let is_last = !has_sketches && !has_datums && si + 1 == n;
342                self.render_solid(ui, solid, is_last, &mut action, &mut hover);
343            }
344            let m = sketches.len();
345            for (i, (id, visible)) in sketches.iter().enumerate() {
346                let is_last = !has_datums && i + 1 == m;
347                let selected = sel_solids.contains(id);
348                self.render_sketch_row(ui, id, *visible, selected, is_last, &mut action, &mut hover);
349            }
350            if has_datums {
351                self.render_datums(ui, &datums, &sel_datums, &mut action, &mut hover);
352            }
353        }
354
355        // --- apply the one deferred engine mutation ---------------------------
356        match action {
357            Some(Action::SetVisible(name, v)) => {
358                state.set_visible(&name, v);
359            }
360            Some(Action::SetAllVisible(v)) => {
361                for s in &solids {
362                    state.set_visible(&s.name, v);
363                }
364            }
365            Some(Action::SetEntityVisible(name, kind, index, v)) => {
366                state.set_entity_visible(&name, kind, index, v);
367            }
368            Some(Action::SetGroupVisible(name, kind, v)) => {
369                state.set_group_visible(&name, kind, v);
370            }
371            Some(Action::SetAllGroupVisible(kind, v)) => {
372                for s in &solids {
373                    state.set_group_visible(&s.name, kind, v);
374                }
375            }
376            Some(Action::SetAllSketchVisible(v)) => {
377                for (id, _) in &sketches {
378                    state.set_sketch_visible(id, v);
379                }
380            }
381            Some(Action::SetAllDatumVisible(v)) => {
382                for (name, _) in &datums {
383                    state.set_datum_visible(name, v);
384                }
385            }
386            Some(Action::Select(kind, name)) => {
387                state.select_by_name(kind, &name);
388            }
389            Some(Action::SelectVertex(solid, pos)) => {
390                state.select_vertex_by_position(&solid, pos);
391            }
392            Some(Action::SetSketchVisible(id, v)) => {
393                state.set_sketch_visible(&id, v);
394            }
395            Some(Action::SetDatumVisible(name, v)) => {
396                state.set_datum_visible(&name, v);
397            }
398            Some(Action::SelectDatum(name)) => {
399                state.select_datum(&name);
400            }
401            None => {}
402        }
403
404        // --- apply the row HOVER (row → viewport highlight) --------------------
405        // Independent of the click above (hovering and clicking legitimately land
406        // on the same frame) and applied EVERY frame: the engine dedupes a held
407        // hover and re-arms the one-frame flag the viewport's hover pass yields to,
408        // and `scene_tree_hover_end` is a no-op once it has cleared what the tree
409        // lit (it never touches a hover the viewport set).
410        let hover_changed = match &hover {
411            Some(Hover::Named(kind, name)) => state.hover_by_name(kind, name),
412            Some(Hover::Vertex(solid, position)) => {
413                state.hover_vertex_by_position(solid, *position)
414            }
415            None => state.scene_tree_hover_end(),
416        };
417        if hover_changed {
418            // The viewport tile may have drawn (and consumed `state.dirty`) BEFORE
419            // this pane in the dock, so without this the new highlight would wait
420            // for the next pointer event to reach the screen.
421            ui.ctx().request_repaint();
422        }
423
424        // --- verifier hooks (wasm only): scene listing + widget hit-rects ------
425        // Published from the panel (not the shared shell) so the headed verifier
426        // can assert the tree contents / visibility and drive real clicks, without
427        // touching `app.rs`'s shared publish block.
428        if crate::automation::registry::enabled() {
429            crate::automation::registry::publish("__brepScene", "scene tree solids with faces, edges, vertices", &state.scene_entities_json());
430            crate::automation::registry::publish("__brepSketches", "committed sketches", &state.sketch_entities_json());
431            crate::automation::registry::publish("__brepDatums", "datum entities", &state.datum_entities_json());
432            crate::automation::registry::publish("__brepSceneVis", "scene visibility map", &state.scene_visibility_json());
433            self.publish_hits();
434        }
435    }
436
437    /// One committed sketch as a TOP-LEVEL leaf row (no group wrapper): a
438    /// visibility checkbox wired to
439    /// [`EngineState::set_sketch_visible`](brep_render::engine_state::EngineState::set_sketch_visible)
440    /// and a label that SELECTS the sketch's sheet solid on click (a committed
441    /// sketch is a scene solid, dim-cyan / `is_sketch`-styled in the viewport, kept
442    /// OUT of the plain-solid rows by the [`snapshot`] filter so it lists exactly
443    /// once). Rendered under the solids, before the `Planes & Datums` group;
444    /// `is_last` is set only when it is the final top-level child.
445    fn render_sketch_row(
446        &mut self,
447        ui: &mut egui::Ui,
448        id: &str,
449        visible: bool,
450        selected: bool,
451        is_last: bool,
452        action: &mut Option<Action>,
453        hover: &mut Option<Hover>,
454    ) {
455        let mut vis = visible;
456        let mut vis_rect = egui::Rect::NOTHING;
457        let mut vis_clicked = false;
458        let resp = tree::node(
459            ui,
460            TreeRow::leaf(&[], is_last, id).selected(selected),
461            |ui| {
462                let cb = ui.add(egui::Checkbox::new(&mut vis, ""));
463                vis_rect = cb.rect;
464                vis_clicked = cb.clicked();
465            },
466        );
467        self.hits.insert(format!("vis:sketch/{id}"), vis_rect);
468        self.hits.insert(format!("sel:sketch/{id}"), resp.label.rect);
469        if vis_clicked {
470            *action = Some(Action::SetSketchVisible(id.to_string(), vis));
471        } else if resp.clicked() {
472            // A committed sketch is a scene solid — select it like any solid.
473            *action = Some(Action::Select("solid", id.to_string()));
474        }
475        if resp.label.hovered() {
476            *hover = Some(Hover::Named("solid", id.to_string()));
477        }
478    }
479
480    /// The `Planes & Datums` group node + (when open) one row per construction
481    /// datum/plane frame, each with a visibility checkbox wired to
482    /// [`EngineState::set_datum_visible`](brep_render::engine_state::EngineState::set_datum_visible)
483    /// and a label that selects the datum
484    /// ([`EngineState::select_datum`](brep_render::engine_state::EngineState::select_datum))
485    /// on click. Rendered as the Scene root's LAST child (only when there is at
486    /// least one construction datum).
487    fn render_datums(
488        &mut self,
489        ui: &mut egui::Ui,
490        datums: &[(String, bool)],
491        sel_datums: &OrderedNames,
492        action: &mut Option<Action>,
493        hover: &mut Option<Hover>,
494    ) {
495        let open = !self.datums_collapsed;
496        let resp = tree::node(
497            ui,
498            TreeRow::branch(&[], true, open, "Planes & Datums"),
499            |ui| {
500                ui.add_space(6.0);
501                ui.label(egui::RichText::new(format!("{}", datums.len())).weak());
502            },
503        );
504        self.hits.insert("box:__datums".into(), resp.box_rect);
505        if resp.toggled || resp.clicked() {
506            self.datums_collapsed = !self.datums_collapsed;
507        }
508        if !open {
509            return;
510        }
511
512        let base = tree::child_guides(&[], true);
513        let m = datums.len();
514        for (i, (name, visible)) in datums.iter().enumerate() {
515            let last = i + 1 == m;
516            let selected = sel_datums.contains(name);
517            let mut vis = *visible;
518            let mut vis_rect = egui::Rect::NOTHING;
519            let mut vis_clicked = false;
520            let resp = tree::node(
521                ui,
522                TreeRow::leaf(&base, last, name).selected(selected),
523                |ui| {
524                    let cb = ui.add(egui::Checkbox::new(&mut vis, ""));
525                    vis_rect = cb.rect;
526                    vis_clicked = cb.clicked();
527                },
528            );
529            self.hits.insert(format!("vis:datum/{name}"), vis_rect);
530            self.hits.insert(format!("sel:datum/{name}"), resp.label.rect);
531            if vis_clicked {
532                *action = Some(Action::SetDatumVisible(name.clone(), vis));
533            } else if resp.clicked() {
534                *action = Some(Action::SelectDatum(name.clone()));
535            }
536            if resp.label.hovered() {
537                *hover = Some(Hover::Named("datum", name.clone()));
538            }
539        }
540    }
541
542    /// One solid node + (when open) its Faces / Edges / Vertices groups.
543    fn render_solid(
544        &mut self,
545        ui: &mut egui::Ui,
546        solid: &SolidRow,
547        is_last: bool,
548        action: &mut Option<Action>,
549        hover: &mut Option<Hover>,
550    ) {
551        let name = &solid.name;
552        let open = !self.collapsed_solids.contains(name);
553
554        let mut vis = solid.visible;
555        let mut vis_rect = egui::Rect::NOTHING;
556        let mut vis_clicked = false;
557        let resp = tree::node(
558            ui,
559            TreeRow::branch(&[], is_last, open, name).selected(solid.selected),
560            |ui| {
561                let cb = ui.add(egui::Checkbox::new(&mut vis, ""));
562                vis_rect = cb.rect;
563                vis_clicked = cb.clicked();
564            },
565        );
566        self.hits.insert(format!("box:{name}"), resp.box_rect);
567        self.hits.insert(format!("vis:{name}"), vis_rect);
568        self.hits.insert(format!("sel:{name}"), resp.label.rect);
569
570        if vis_clicked {
571            *action = Some(Action::SetVisible(name.clone(), vis));
572        }
573        if resp.toggled {
574            if open {
575                self.collapsed_solids.insert(name.clone());
576            } else {
577                self.collapsed_solids.remove(name);
578            }
579        }
580        if resp.clicked() {
581            *action = Some(Action::Select("solid", name.clone()));
582        }
583        if resp.label.hovered() {
584            *hover = Some(Hover::Named("solid", name.clone()));
585        }
586
587        if open {
588            let base = tree::child_guides(&[], is_last);
589            self.render_group(ui, name, &base, false, "Faces", VisKind::Face, &solid.faces, action, hover);
590            self.render_group(ui, name, &base, false, "Edges", VisKind::Edge, &solid.edges, action, hover);
591            self.render_group(ui, name, &base, true, "Vertices", VisKind::Vertex, &solid.vertices, action, hover);
592        }
593    }
594
595    /// One Faces/Edges/Vertices group node + (when open) its entity leaves. The
596    /// group checkbox is a live TRISTATE that hides/shows every entity of `kind`;
597    /// each leaf carries its own live checkbox that hides just that entity.
598    #[allow(clippy::too_many_arguments)]
599    fn render_group(
600        &mut self,
601        ui: &mut egui::Ui,
602        solid_name: &str,
603        base: &[bool],
604        is_last: bool,
605        group: &str,
606        kind: VisKind,
607        entities: &[Entity],
608        action: &mut Option<Action>,
609        hover: &mut Option<Hover>,
610    ) {
611        let key = format!("{solid_name}/{group}");
612        let open = self.expanded_groups.contains(&key);
613
614        // Tristate over the group's entities (empty group reads All → checked).
615        let state = group_state(entities);
616        let mut checked = matches!(state, GroupState::All);
617        let indeterminate = matches!(state, GroupState::Partial);
618        let mut vis_rect = egui::Rect::NOTHING;
619        let mut vis_clicked = false;
620        let resp = tree::node(
621            ui,
622            TreeRow::branch(base, is_last, open, group),
623            |ui| {
624                // right-to-left: the tristate group checkbox (rightmost), then count.
625                let cb = ui.add(
626                    egui::Checkbox::new(&mut checked, "").indeterminate(indeterminate),
627                );
628                vis_rect = cb.rect;
629                vis_clicked = cb.clicked();
630                ui.add_space(6.0);
631                ui.label(egui::RichText::new(format!("{}", entities.len())).weak());
632            },
633        );
634        self.hits.insert(format!("box:{key}"), resp.box_rect);
635        self.hits.insert(format!("vis:{key}"), vis_rect);
636        if vis_clicked {
637            // Standard tristate: All → hide all; None/Partial → show all.
638            let want_visible = !matches!(state, GroupState::All);
639            *action = Some(Action::SetGroupVisible(solid_name.to_string(), kind, want_visible));
640        }
641        if resp.toggled || resp.clicked() {
642            if open {
643                self.expanded_groups.remove(&key);
644            } else {
645                self.expanded_groups.insert(key.clone());
646            }
647        }
648
649        if !open {
650            return;
651        }
652        let gg = tree::child_guides(base, is_last);
653        if entities.is_empty() {
654            tree::node(ui, TreeRow::leaf(&gg, true, "(none)"), |_| {});
655            return;
656        }
657        let m = entities.len();
658        for (ei, e) in entities.iter().enumerate() {
659            let last = ei + 1 == m;
660            let mut vis = e.visible;
661            let mut ev_rect = egui::Rect::NOTHING;
662            let mut ev_clicked = false;
663            let resp = tree::node(
664                ui,
665                TreeRow::leaf(&gg, last, &e.label).selected(e.selected),
666                |ui| {
667                    let cb = ui.add(egui::Checkbox::new(&mut vis, ""));
668                    ev_rect = cb.rect;
669                    ev_clicked = cb.clicked();
670                },
671            );
672            self.hits.insert(format!("sel:{key}/{ei}"), resp.label.rect);
673            self.hits.insert(format!("vis:{key}/{ei}"), ev_rect);
674            if ev_clicked {
675                *action = Some(Action::SetEntityVisible(solid_name.to_string(), kind, ei, vis));
676            } else if resp.clicked() {
677                *action = Some(match &e.kind {
678                    EntityKind::Face(n) => Action::Select("face", n.clone()),
679                    EntityKind::Edge(n) => Action::Select("edge", n.clone()),
680                    EntityKind::Vertex(p) => Action::SelectVertex(solid_name.to_string(), *p),
681                });
682            }
683            if resp.label.hovered() {
684                // An UNNAMED face/edge has nothing to highlight (it is not
685                // selectable either) — leaving `hover` unset clears instead, so a
686                // neighbouring row's highlight never lingers under the pointer.
687                *hover = match &e.kind {
688                    EntityKind::Face(n) if !n.is_empty() => {
689                        Some(Hover::Named("face", n.clone()))
690                    }
691                    EntityKind::Edge(n) if !n.is_empty() => {
692                        Some(Hover::Named("edge", n.clone()))
693                    }
694                    EntityKind::Vertex(p) => {
695                        Some(Hover::Vertex(solid_name.to_string(), *p))
696                    }
697                    _ => None,
698                };
699            }
700        }
701    }
702
703    /// The published widget hit-rects (egui points) for the headed verifier.
704    pub fn hits_json(&self) -> String {
705        crate::automation::hit_rects::hits_json(&self.hits)
706    }
707
708    /// Drop the published rects: the dock calls this for a pane it did not
709    /// draw this frame (`DockState::ui`), whose widgets are not on screen.
710    pub fn clear_hits(&mut self) {
711        self.hits.clear();
712        // This panel publishes its rects from `show`, which did not run.
713        if crate::automation::registry::enabled() {
714            self.publish_hits();
715        }
716    }
717
718    fn publish_hits(&self) {
719        crate::automation::registry::publish("__brepSceneHit", "scene tree widget rects (box:, sel:, vis:, typevis:)", &self.hits_json());
720    }
721}
722
723/// The tristate for a group of entity rows, computed from their live per-entity
724/// `visible` flags (the same the engine would report). An empty group reads
725/// [`GroupState::All`] — nothing to hide, so the checkbox shows checked.
726fn group_state(entities: &[Entity]) -> GroupState {
727    if entities.is_empty() {
728        return GroupState::All;
729    }
730    let visible = entities.iter().filter(|e| e.visible).count();
731    if visible == entities.len() {
732        GroupState::All
733    } else if visible == 0 {
734        GroupState::None
735    } else {
736        GroupState::Partial
737    }
738}
739
740/// Snapshot `state.scene` into owned rows, precomputing each entity's `selected`
741/// flag against the current engine selection (cloned in by the caller) and its
742/// live per-entity `visible` flag. Face / edge labels fall back to `Face i` /
743/// `Edge i` when the kernel left them unnamed; unnamed entities are then not
744/// name-selectable (the click is a no-op) but still hideable (by index).
745fn snapshot(
746    state: &EngineState,
747    sel_solids: &OrderedNames,
748    sel_faces: &OrderedNames,
749    sel_edges: &OrderedNames,
750    sel_vertices: &[brep_render::style::VertexRef],
751) -> Vec<SolidRow> {
752    // Vertex positions are set exactly from the same source; a tiny tolerance
753    // guards float round-trips.
754    const TOL: f64 = 1e-6;
755    state
756        .scene
757        .solids()
758        .iter()
759        // Committed-sketch SHEETS are scene solids too, but they list as their OWN
760        // top-level sketch rows (`render_sketch_row`, checkbox wired to
761        // `set_sketch_visible`) — never as plain solid rows, so they are dropped here
762        // and never counted by the root / type-visibility toggles.
763        .filter(|s| !s.is_sketch)
764        .map(|s| {
765            let faces = s
766                .faces
767                .iter()
768                .enumerate()
769                .map(|(i, f)| Entity {
770                    label: if f.name.is_empty() {
771                        format!("Face {i}")
772                    } else {
773                        f.name.clone()
774                    },
775                    selected: !f.name.is_empty() && sel_faces.contains(&f.name),
776                    visible: s.visibility.is_face_visible(i),
777                    kind: EntityKind::Face(f.name.clone()),
778                })
779                .collect();
780            // AUX edges are display-only (a board's copper outline, thousands
781            // of them) and are not entities. They trail the real edges, so
782            // stopping at the first keeps a row's position its edge's index,
783            // which is what the visibility checkbox hides by.
784            let edges = s
785                .edges
786                .iter()
787                .enumerate()
788                .take_while(|(_, e)| !e.aux)
789                .map(|(i, e)| Entity {
790                    label: if e.name.is_empty() {
791                        format!("Edge {i}")
792                    } else {
793                        e.name.clone()
794                    },
795                    selected: !e.name.is_empty() && sel_edges.contains(&e.name),
796                    visible: s.visibility.is_edge_visible(i),
797                    kind: EntityKind::Edge(e.name.clone()),
798                })
799                .collect();
800            let vertices = s
801                .vertices
802                .iter()
803                .enumerate()
804                .map(|(i, v)| Entity {
805                    label: format!("Vertex {i}"),
806                    selected: sel_vertices.iter().any(|r| {
807                        r.solid == s.name
808                            && (r.position[0] - v.position[0]).abs() <= TOL
809                            && (r.position[1] - v.position[1]).abs() <= TOL
810                            && (r.position[2] - v.position[2]).abs() <= TOL
811                    }),
812                    visible: s.visibility.is_vertex_visible(i),
813                    kind: EntityKind::Vertex(v.position),
814                })
815                .collect();
816            SolidRow {
817                name: s.name.clone(),
818                visible: s.visible,
819                selected: sel_solids.contains(&s.name),
820                faces,
821                edges,
822                vertices,
823            }
824        })
825        .collect()
826}
827
828
829/// The hit keys this panel publishes (see `automation::hit_keys`).
830pub static HIT_KEYS: &[HitKeyDoc] = &[
831    HitKeyDoc { panel: "scene", prefix: "box:", meaning: "expand/collapse a tree node (box:name, box:__scene, box:__datums)", command: None },
832    HitKeyDoc { panel: "scene", prefix: "sel:", meaning: "select a tree row (sel:name, sel:name/ei, sel:sketch/id, sel:datum/name)", command: None },
833    HitKeyDoc { panel: "scene", prefix: "vis:", meaning: "toggle a row's visibility (vis:name, vis:name/ei, vis:__scene)", command: None },
834    HitKeyDoc { panel: "scene", prefix: "typevis:", meaning: "toggle visibility of a whole kind (typevis:label)", command: None },
835];