Skip to main content

brep_app/panels/
selection.rs

1//! Selection panel — the **selection filter** (which entity kinds a viewport
2//! click may pick), rendered as a row or compact popup in the shell's bottom
3//! STATUS BAR (not a side-panel section). Follows the panel pattern: a small
4//! state struct + a `show_status_bar(&mut self, ui, state)` the bottom bar calls
5//! once per frame in modeling mode; `EngineState` stays the single brain (it owns
6//! the filter + the selection), borrowed in.
7//!
8//! * **Filter** — a leading "All" tristate CHECKBOX followed by a real CHECKBOX
9//!   per kind (COMPONENT / SOLID / FACE / EDGE / VERTEX / PLANE; COMPONENT =
10//!   promote a pick on assembly-component geometry to the whole component;
11//!   PLANE = the construction datum/plane cards, pickable like any face). The
12//!   engine honors the filter in
13//!   `select_top_at` (via the planes-aware `pick_top_at` with the enabled
14//!   kinds), so a plain click grabs only an
15//!   allowed kind — a FACE-only filter selects a face, a SOLID-only filter the
16//!   owning solid, a PLANE-only filter the construction plane under the cursor. Defaults to ALL kinds enabled (everything under the cursor is
17//!   pickable, highest-priority kind wins). Reference-selection mode temporarily
18//!   constrains it to the active field's allowed kinds — while that picker is
19//!   active the row is LOCKED (greyed + non-interactive) so a click can't
20//!   overwrite the constraint. The model state is the engine's `selection_filter`;
21//!   this panel just reads/writes it.
22//!
23//! The quick actions on the current selection (Clear / Hide / Edit-owning-feature
24//! + the feature-from-selection actions) live in the dedicated
25//! [`crate::panels::context_bar`] (the engine-native successor to the old app's
26//! floating selection action bar).
27//!
28//! The panel owns only the per-frame `hits` map (widget screen rects) the headed
29//! verifier reads to drive real clicks, exactly like the toolbar/history panels.
30
31use crate::automation::hit_keys::HitKeyDoc;
32use brep_render::engine_state::{EngineState, SelectionFilter};
33use crate::icon_text::IconTextUi as _;
34use eframe::egui;
35use std::collections::HashMap;
36
37/// The pickable kinds, in the order the filter row draws them: the geometry
38/// kinds coarsest-first, then the CONSTRUCTION kind last. SKETCH sits beside
39/// SOLID because a committed sketch is drawn as a sheet solid and picks as one;
40/// the two lanes split that single pick kind so a sketch can be made pickable
41/// (or not) independently of real bodies. It governs WHOLE sketches — a sketch's
42/// face and its drawn segments stay under Face and Edge.
43/// `(key, label)`: the `key` is the engine kind name + the `hits` map key
44/// suffix; the `label` is the checkbox caption. COMPONENT is the promotion
45/// kind: on, a pick landing on assembly-component geometry selects the WHOLE
46/// component; off, the click reaches the sub-entity kinds. PLANE is the
47/// construction kind: the drawn datum/plane cards, pickable like any face (they
48/// rank right after faces in the pick list, so a plane under geometry is
49/// reachable through the pick-list popup).
50const KINDS: [(&str, &str); 7] = [
51    ("COMPONENT", "Component"),
52    ("SOLID", "Solid"),
53    ("SKETCH", "Sketch"),
54    ("FACE", "Face"),
55    ("EDGE", "Edge"),
56    ("VERTEX", "Vertex"),
57    ("PLANE", "Plane"),
58];
59
60/// The selection panel's own state: the per-frame map of egui widget screen
61/// rects, published to JS for the headed verifier to drive real clicks. Rebuilt
62/// each frame (there is no DOM — egui is drawn on the canvas).
63#[derive(Default)]
64pub struct SelectionPanel {
65    hits: HashMap<String, egui::Rect>,
66    /// The compact filter button's caption as last drawn, `None` while the
67    /// full row is drawn instead (published as `__brepFilterButton`).
68    button: Option<String>,
69}
70
71impl SelectionPanel {
72    pub fn new() -> Self {
73        Self::default()
74    }
75
76    /// Draw the selection filter as a row or compact popup in the shell's
77    /// bottom STATUS BAR: a leading "All" tristate checkbox + one CHECKBOX per
78    /// pickable kind (all reflecting the LIVE engine `selection_filter`). Rebuilds
79    /// `hits` as it draws. Called by the bottom bar in modeling mode.
80    pub fn show_status_bar(&mut self, ui: &mut egui::Ui, state: &mut EngineState) {
81        self.hits.clear();
82        self.button = None;
83        // A loading label can leave only a sliver of the status bar. Never
84        // wrap the filter into that sliver: a popup does not consume viewport
85        // height and remains usable with touch-sized controls. The whole
86        // filter is then ONE button whose caption says what is pickable.
87        if crate::viewport::touch::enabled(ui.ctx()) || ui.available_width() < crate::viewport::touch::NARROW_WIDTH {
88            let caption = button_caption(&state.selection_filter(), state.ref_select_active());
89            self.button = Some(caption.clone());
90            let menu = egui::containers::menu::MenuButton::new(caption)
91                .config(egui::containers::menu::MenuConfig::new()
92                    .close_behavior(egui::PopupCloseBehavior::CloseOnClickOutside))
93                .ui(ui, |ui| {
94                    ui.set_width(240.0_f32.min(ui.ctx().content_rect().width() - 24.0));
95                    egui::ScrollArea::vertical()
96                        .max_height((ui.ctx().content_rect().height() - 100.0).max(88.0))
97                        .show(ui, |ui| self.show_filters(ui, state));
98                });
99            self.hits.insert("filters:toggle".into(), menu.0.rect);
100        } else {
101            ui.horizontal_wrapped(|ui| self.show_filters(ui, state));
102        }
103    }
104
105    fn show_filters(&mut self, ui: &mut egui::Ui, state: &mut EngineState) {
106        // While the reference-selection picker is active the engine
107        // temporarily CONSTRAINS the filter to the active field's allowed
108        // kinds (see `begin_ref_select`). Letting a bottom-bar click edit it
109        // then would clobber that constraint, so LOCK the row: it still shows
110        // the constrained kinds (greyed) but is non-interactive until the
111        // picker finishes/cancels and the filter is restored.
112        let locked = state.ref_select_active();
113        if locked {
114            ui.icon_label(egui::RichText::new("\u{1f512} Reference selection — filter locked").weak())
115                .on_hover_text(
116                    "The pick filter is set by the field being referenced. \
117                     Finish the pick (Done on its field, or Escape) to change it.",
118                );
119        } else {
120            ui.label("Pickable:");
121        }
122
123        // `add_enabled_ui(false, …)` greys the widgets AND makes them
124        // non-interactive, so no `.changed()`/`.clicked()` fires while locked
125        // — the constrained filter cannot be overwritten. Rects are still laid
126        // out and published so the verifier and layout stay consistent.
127        ui.add_enabled_ui(!locked, |ui| {
128            let mut filter = state.selection_filter();
129            let before = filter;
130
131            // Leading "All" TRISTATE CHECKBOX (mirrors the Scene tree's group
132            // checkbox idiom): checked when EVERY kind is on, the indeterminate
133            // dash when some-but-not-all are on, unchecked when none are.
134            // Toggling it applies the toggle-all semantic — all-on → clear all;
135            // partial or none → set all on — which is exactly the checkbox's
136            // post-click value. Published under `filter:ALL` for the verifier.
137            let all_on = KINDS.iter().all(|(k, _)| filter.get(k));
138            let any_on = KINDS.iter().any(|(k, _)| filter.get(k));
139            let mut all_checked = all_on;
140            let resp = ui.add(
141                egui::Checkbox::new(&mut all_checked, "All").indeterminate(any_on && !all_on),
142            );
143            self.hits.insert("filter:ALL".into(), resp.rect);
144            if resp.changed() {
145                let target = toggle_all_target(&filter);
146                for (kind, _) in KINDS {
147                    filter.set(kind, target);
148                }
149            }
150
151            // One CHECKBOX per kind, reflecting the live filter. A `.changed()`
152            // checkbox updates the working copy; it is written back once below.
153            for (kind, label) in KINDS {
154                let mut on = filter.get(kind);
155                let resp = ui.checkbox(&mut on, label);
156                self.hits.insert(format!("filter:{kind}"), resp.rect);
157                if resp.changed() {
158                    filter.set(kind, on);
159                }
160            }
161
162            if filter != before {
163                state.set_selection_filter(filter);
164            }
165        });
166    }
167
168    /// Drop the published widget rects. The bottom bar calls this in sketch mode
169    /// (when the filter row is NOT drawn) so the verifier never sees stale
170    /// last-modeling-frame rects for widgets that are no longer on screen.
171    pub fn clear_hits(&mut self) {
172        self.hits.clear();
173        self.button = None;
174    }
175
176    /// The `__brepFilterButton` verifier global: `{shown, label}` — whether
177    /// the filter is collapsed to its one button, and the button's caption.
178    pub fn button_json(&self) -> String {
179        serde_json::json!({ "shown": self.button.is_some(), "label": self.button }).to_string()
180    }
181
182    /// The published widget hit-rects (egui points) for the headed verifier —
183    /// `filter:COMPONENT|SOLID|FACE|EDGE|VERTEX|PLANE` (the per-kind checkboxes)
184    /// + `filter:ALL` (the leading "All" tristate checkbox).
185    pub fn hits_json(&self) -> String {
186        crate::automation::hit_rects::hits_json(&self.hits)
187    }
188}
189
190/// The compact filter button's caption: what a click may pick, so the state
191/// shows without opening the popup. "All" and "None" for the two ends, the
192/// kind names for one or two, a count beyond that; a reference pick's
193/// constraint is marked locked (the popup cannot change it).
194fn button_caption(filter: &SelectionFilter, locked: bool) -> String {
195    let on: Vec<&str> = KINDS.iter().filter(|(k, _)| filter.get(k)).map(|(_, label)| *label).collect();
196    let what = match on.len() {
197        0 => "None".to_string(),
198        n if n == KINDS.len() => "All".to_string(),
199        1 | 2 => on.join(", "),
200        n => format!("{n} of {}", KINDS.len()),
201    };
202    if locked { format!("Filter (locked): {what}") } else { format!("Filter: {what}") }
203}
204
205/// The "toggle all" target value: if EVERY pickable kind is currently on, the
206/// "All" checkbox turns them all OFF; otherwise (some-but-not-all on, or none on)
207/// it turns them all ON. Returns the value to write to every kind.
208fn toggle_all_target(filter: &SelectionFilter) -> bool {
209    !KINDS.iter().all(|(k, _)| filter.get(k))
210}
211
212
213/// The hit keys this panel publishes (see `automation::hit_keys`).
214pub static HIT_KEYS: &[HitKeyDoc] = &[
215    HitKeyDoc { panel: "selection", prefix: "filters:toggle", meaning: "show or hide pickable filters when the status bar is compact", command: None },
216    HitKeyDoc { panel: "selection", prefix: "filter:", meaning: "toggle which entity kinds a click may pick (filter:ALL, filter:kind)", command: None },
217];