Skip to main content

brep_app/panels/
pick_bar.rs

1//! The FOCUSED PICK bar. A form field's reference pick normally keeps its
2//! dialog up, the field focused in its pane, while the user clicks in the view
3//! (`EngineState::ref_select_in_form`). On a phone that dialog and the side
4//! panels cover the view, so nothing can be picked: on a touch or narrow
5//! window (`crate::viewport::touch::narrow`) the shell instead hides ALL of
6//! its chrome — toolbar and ribbon, workbench strip, status bar, the docks and
7//! the dialog in them, the action overlay — and draws the viewport alone, with
8//! this bar over it: what is being picked, the running picks (each with an ✕),
9//! the selection filter's one button, Finish and Cancel. Finish puts the picks
10//! in the field and Cancel leaves it as it was; either brings the whole UI
11//! back. Picking itself is unchanged — the same filter, the same per-field
12//! validation, a list field taking picks until Finish and a single field
13//! finishing on its first pick as it does on desktop.
14//!
15//! The bar CLAIMS the pick every frame it draws (`ref_select_claim`): the
16//! shell finishes an in-form pick no form claimed, and the form is not drawn
17//! while the bar is up.
18
19use crate::automation::hit_keys::HitKeyDoc;
20use brep_render::engine_state::EngineState;
21use eframe::egui;
22use std::collections::HashMap;
23
24/// How the shell lays a frame out around a reference pick.
25#[derive(Debug, Clone, Copy, PartialEq, Eq)]
26pub enum PickLayout {
27    /// No pick is running.
28    Idle,
29    /// A form field's pick with its dialog up (desktop).
30    InForm,
31    /// A form field's pick on a touch or narrow window: the dock is hidden
32    /// and this bar is the picker.
33    Focused,
34    /// A card-based pick (a connection point's seat, a spline anchor's port):
35    /// the dock is hidden and the mode bar's card is the picker.
36    Modal,
37}
38
39impl PickLayout {
40    pub fn of(active: bool, in_form: bool, narrow: bool) -> Self {
41        match (active, in_form, narrow) {
42            (false, _, _) => Self::Idle,
43            (true, false, _) => Self::Modal,
44            (true, true, false) => Self::InForm,
45            (true, true, true) => Self::Focused,
46        }
47    }
48
49    /// Whether the dock (side panels, the dialog among them) is drawn.
50    pub fn shows_dock(self) -> bool {
51        matches!(self, Self::Idle | Self::InForm)
52    }
53}
54
55/// What a click on the bar asks for.
56#[derive(Debug, Clone, Copy, PartialEq, Eq)]
57enum Action {
58    Finish,
59    Cancel,
60    Remove(usize),
61}
62
63#[derive(Default)]
64pub struct PickBar {
65    hits: HashMap<String, egui::Rect>,
66    /// `(label, picks so far)` as last drawn; `None` while the bar is down.
67    shown: Option<(String, usize)>,
68}
69
70impl PickBar {
71    pub fn new() -> Self {
72        Self::default()
73    }
74
75    /// Draw the bar across the top of `view` (the viewport rect) and apply
76    /// what it was asked for. Called by the shell after the viewport, every
77    /// frame of a [`PickLayout::Focused`] pick.
78    pub fn show(
79        &mut self,
80        ctx: &egui::Context,
81        view: egui::Rect,
82        state: &mut EngineState,
83        filter: &mut crate::panels::selection::SelectionPanel,
84    ) {
85        self.hits.clear();
86        self.shown = None;
87        let Some(field) = state.ref_select_field() else {
88            return;
89        };
90        state.ref_select_claim();
91        let label = state.ref_select_label();
92        let hint = if state.ref_select_is_sheet() {
93            "Tap an anchor marked on the sheet to pick; drag to pan."
94        } else if field.multiple {
95            "Tap in the view to add picks; drag to orbit. Finish when done."
96        } else {
97            "Tap in the view to pick; drag to orbit."
98        };
99        let width = (view.width() - 16.0).max(120.0);
100        let mut action = None;
101        egui::Area::new(egui::Id::new("brep-pick-bar"))
102            .order(egui::Order::Foreground)
103            .fixed_pos(view.left_top() + egui::vec2(8.0, 8.0))
104            .movable(false)
105            .show(ctx, |ui| {
106                let frame = egui::Frame::popup(ui.style()).show(ui, |ui| {
107                    ui.set_width(width - 16.0);
108                    ui.horizontal_wrapped(|ui| {
109                        ui.label(egui::RichText::new(format!("Select: {label}")).strong());
110                        ui.label(egui::RichText::new(format!("{} picked", field.names.len())).weak());
111                    });
112                    ui.label(egui::RichText::new(hint).weak().small());
113                    // Finish / Cancel come BEFORE the picks: a long list must
114                    // never push the way out of the picker off the screen. The
115                    // status bar's filter button rides here, the bar being
116                    // hidden with the rest of the chrome.
117                    ui.horizontal_wrapped(|ui| {
118                        let finish = ui.button("Finish").on_hover_text("Put the picks in the field and return to the dialog");
119                        self.hits.insert("finish".into(), finish.rect);
120                        if finish.clicked() {
121                            action = Some(Action::Finish);
122                        }
123                        let cancel = ui.button("Cancel").on_hover_text("Return to the dialog with the field unchanged");
124                        self.hits.insert("cancel".into(), cancel.rect);
125                        if cancel.clicked() {
126                            action = Some(Action::Cancel);
127                        }
128                        filter.show_filter_bar(ui, state);
129                    });
130                    if !field.names.is_empty() {
131                        egui::ScrollArea::vertical().id_salt("pick-bar-picks").max_height(96.0).show(ui, |ui| {
132                            ui.horizontal_wrapped(|ui| {
133                                for (i, name) in field.names.iter().enumerate() {
134                                    let chip = ui.add(egui::Button::new(format!("{name}  \u{2716}")).small())
135                                        .on_hover_text("Remove this pick");
136                                    self.hits.insert(format!("x{i}"), chip.rect);
137                                    if chip.clicked() {
138                                        action = Some(Action::Remove(i));
139                                    }
140                                }
141                            });
142                        });
143                    }
144                });
145                self.hits.insert("bar".into(), frame.response.rect);
146            });
147        self.shown = Some((label, field.names.len()));
148        match action {
149            Some(Action::Finish) => state.finish_ref_select(),
150            Some(Action::Cancel) => state.cancel_ref_select(),
151            Some(Action::Remove(i)) => state.ref_select_remove(i),
152            None => {}
153        }
154        if action.is_some() {
155            ctx.request_repaint();
156        }
157    }
158
159    /// For a frame the bar is not drawn: no rect of it may stay published.
160    pub fn clear_hits(&mut self) {
161        self.hits.clear();
162        self.shown = None;
163    }
164
165    pub fn hits_json(&self) -> String {
166        crate::automation::hit_rects::hits_json(&self.hits)
167    }
168
169    /// The `__brepPickBar` verifier global: `{shown, label, count}`.
170    pub fn state_json(&self) -> String {
171        serde_json::json!({
172            "shown": self.shown.is_some(),
173            "label": self.shown.as_ref().map(|(label, _)| label),
174            "count": self.shown.as_ref().map(|(_, count)| count),
175        })
176        .to_string()
177    }
178}
179
180/// The hit keys this panel publishes (see `automation::hit_keys`).
181pub static HIT_KEYS: &[HitKeyDoc] = &[
182    HitKeyDoc { panel: "pickbar", prefix: "bar", meaning: "the focused pick bar a touch or narrow window shows over the full-window view while a dialog's reference field picks (every other piece of chrome is hidden; the filter button rides in the bar)", command: None },
183    HitKeyDoc { panel: "pickbar", prefix: "finish", meaning: "put the picks in the field and return to the dialog", command: None },
184    HitKeyDoc { panel: "pickbar", prefix: "cancel", meaning: "return to the dialog with the field unchanged (Escape does the same)", command: None },
185    HitKeyDoc { panel: "pickbar", prefix: "x", meaning: "remove the i-th running pick (xi)", command: None },
186];
187