SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.
Clicking a parameter suggestion inserts it (#3257) (#3260)
phanoobe1daf0a8b662
diff --git a/crates/ui-egui/src/expression_complete.rs b/crates/ui-egui/src/expression_complete.rsindex 0ef4f7de6b..db52278c97 100644--- a/crates/ui-egui/src/expression_complete.rs+++ b/crates/ui-egui/src/expression_complete.rs@@ -17,6 +17,8 @@ struct Popup {accept: bool,dismissed: bool,explicit: bool,+ /// Where each suggestion was drawn, for a press on one (see [`complete`]).+ rows: Vec<egui::Rect>,}fn popup_id() -> egui::Id {@@ -95,7 +97,21 @@ pub(crate) fn begin_frame(ctx: &egui::Context) {pub(crate) fn complete(ui: &mut egui::Ui, resp: &egui::Response, text: &mut String) -> bool {let ctx = ui.ctx().clone();- if !ctx.memory(|m| m.has_focus(resp.id)) {+ // A press on a suggestion takes the focus from the editor before the list can see it+ // clicked, and the editor then gives up what was typed (#3257). The press picks the+ // suggestion under it, and the editor gets its focus back below.+ let pressed_row = ctx+ .input(|i| i.pointer.primary_pressed().then(|| i.pointer.interact_pos()).flatten())+ .and_then(|at| ctx.data(|d| d.get_temp::<Popup>(popup_id())).filter(|p| p.editor == resp.id)?.rows.iter().position(|r| r.contains(at)));+ // Its release is a click away from the editor, which gives up the focus again: take it back,+ // and don't let the list take that release as a click on another row.+ let picked_by_press = resp.id.with("completion_picked_by_press");+ let released_after_press = ctx.data(|d| d.get_temp::<bool>(picked_by_press)).is_some() && ctx.input(|i| i.pointer.any_released());+ if released_after_press {+ ctx.data_mut(|d| d.remove::<bool>(picked_by_press));+ resp.request_focus();+ }+ if !ctx.memory(|m| m.has_focus(resp.id)) && pressed_row.is_none() {return false;}let state = egui::TextEdit::load_state(&ctx, resp.id);@@ -112,6 +128,7 @@ pub(crate) fn complete(ui: &mut egui::Ui, resp: &egui::Response, text: &mut Striaccept: false,dismissed: false,explicit: false,+ rows: Vec::new(),});let list = suggestions_for(&ctx, &popup.word, popup.explicit);popup.frame = ctx.cumulative_frame_nr();@@ -121,7 +138,11 @@ pub(crate) fn complete(ui: &mut egui::Ui, resp: &egui::Response, text: &mut Strictx.data_mut(|d| d.insert_temp(popup_id(), popup));return false;}- let mut pick = if popup.accept { list.get(popup.selected).map(|s| s.text.clone()) } else { None };+ let mut pick = match pressed_row {+ Some(row) => list.get(row).map(|s| s.text.clone()),+ None if popup.accept => list.get(popup.selected).map(|s| s.text.clone()),+ None => None,+ };popup.accept = false;let t = Tokens::get();let options: Vec<_> = list@@ -143,16 +164,20 @@ pub(crate) fn complete(ui: &mut egui::Ui, resp: &egui::Response, text: &mut Stri},}).collect();+ let list_id = resp.id.with("completions");if let Some(index) = ui_kit::popup_list(&ctx,- resp.id.with("completions"),+ list_id,resp.rect,popup.selected,&options,crate::i18n::tr_label("↑ ↓ choose · Enter / Tab complete · Esc dismiss"),- ) {+ ) && pick.is_none()+ && !released_after_press+ {pick = list.get(index).map(|suggestion| suggestion.text.clone());}+ popup.rows = ctx.data(|d| d.get_temp::<Vec<egui::Rect>>(list_id.with("rows"))).unwrap_or_default();ctx.data_mut(|d| d.insert_temp(popup_id(), popup));let Some(pick) = pick else { return false };let chars: Vec<char> = text.chars().collect();@@ -163,6 +188,9 @@ pub(crate) fn complete(ui: &mut egui::Ui, resp: &egui::Response, text: &mut Stristate.cursor.set_char_range(Some(egui::text::CCursorRange::one(egui::text::CCursor::new(start + pick.chars().count()))));state.store(&ctx, resp.id);resp.request_focus();+ if pressed_row.is_some() {+ ctx.data_mut(|d| d.insert_temp(picked_by_press, true));+ }// Do not leave the accepted popup owning Enter on the next frame.ctx.data_mut(|d| d.remove::<Popup>(popup_id()));true@@ -206,7 +234,7 @@ mod tests {fn frame(&mut self, events: Vec<egui::Event>) {self.accepted = false;self.leaked.clear();- let _ = self.ctx.run_ui(+ let mut output = self.ctx.run_ui(egui::RawInput {screen_rect: Some(egui::Rect::from_min_size(egui::Pos2::ZERO, egui::vec2(600.0, 400.0))),events,@@ -229,6 +257,8 @@ mod tests {});},);+ // Nothing paints these frames: drop their texture updates on purpose.+ output.textures_delta.clear();}fn key(&mut self, key: Key) {@@ -266,6 +296,26 @@ mod tests {assert!(e.ctx.memory(|m| m.has_focus(e.id)));}+ /// #3257: a click on a suggestion inserts it, though the press takes the focus from the+ /// editor first; the editor keeps its focus and its caret goes after the inserted name.+ #[test]+ fn clicking_a_suggestion_inserts_it() {+ let mut e = Editor::new("2 * wi");+ let list = suggestions(&e.ctx, "wi");+ let rows = e.ctx.data(|d| d.get_temp::<Popup>(popup_id())).unwrap().rows;+ assert_eq!(rows.len(), list.len());+ let at = rows[1].center();+ let button =+ |pressed| egui::Event::PointerButton { pos: at, button: egui::PointerButton::Primary, pressed, modifiers: egui::Modifiers::NONE };+ e.frame(vec![egui::Event::PointerMoved(at)]);+ e.frame(vec![button(true)]);+ assert!(e.accepted, "the press picked the suggestion under it");+ e.frame(vec![button(false)]);+ assert_eq!(e.text, format!("2 * {}", list[1].text));+ assert!(e.ctx.memory(|m| m.has_focus(e.id)), "the editor keeps the focus");+ assert_eq!(e.cursor(), e.text.chars().count());+ }+#[test]fn tab_accepts_selection_filtering_resets_it_and_escape_dismisses() {let mut e = Editor::new("wi");diff --git a/crates/ui-egui/src/sketch_dims_tests.rs b/crates/ui-egui/src/sketch_dims_tests.rsindex 5f6033c93e..b0fd1d543e 100644--- a/crates/ui-egui/src/sketch_dims_tests.rs+++ b/crates/ui-egui/src/sketch_dims_tests.rs@@ -128,6 +128,32 @@ fn forward_tab_completes_expression_then_cycles_and_shift_tab_moves_back() {assert!((sketch.profiles[0].area - 120.0).abs() < 1e-6);}+/// #3257: clicking a parameter suggestion under a line's length box puts the name in the box,+/// which keeps the focus; the click places nothing, and Enter draws the line that long.+#[test]+fn clicking_a_parameter_suggestion_fills_the_length_box() {+ let mut h = drawing("sketch.line");+ h.app.run("parameters.add", json!({"name": "width", "expression": "24 mm"})).unwrap();+ h.frames(2);+ type_value(&mut h, "wid");+ h.frames(1);+ h.step(&json!({"click": {"handle": "dialog:suggestion:0"}})).unwrap();+ h.frames(1);+ assert_focus(&h, 0);+ assert_eq!(h.app.tool.as_ref().unwrap().dims[0].value, "width");+ let sketch = |h: &Harness| h.app.session.world_state().sketch(h.app.session.active_sketch.unwrap()).unwrap().sketch.curves.len();+ assert_eq!(sketch(&h), 0, "the click on the list placed no point");+ h.step(&json!({"key": "Enter"})).unwrap();+ let state = h.app.session.world_state();+ let sk = &state.sketch(h.app.session.active_sketch.unwrap()).unwrap().sketch;+ assert_eq!(sk.curves.len(), 1);+ let length = sk.shape(0).map(|s| match s {+ solidcraft_engine::sketch::Shape::Line { a, b } => a.dist(b),+ _ => 0.0,+ });+ assert!(length.is_some_and(|l| (l - 24.0).abs() < 1e-6), "{length:?}");+}+#[test]fn inch_live_lengths_use_design_units_and_typed_four_digits_commit_in_inches() {let mut h = Harness::new();diff --git a/crates/ui-egui/src/ui_kit/choice.rs b/crates/ui-egui/src/ui_kit/choice.rsindex 7d9ca3558e..bb518beb85 100644--- a/crates/ui-egui/src/ui_kit/choice.rs+++ b/crates/ui-egui/src/ui_kit/choice.rs@@ -348,7 +348,9 @@ pub(crate) struct PopupOption<'a> {pub color: egui::Color32,}-/// Focus-neutral suggestion popup. The owner handles navigation and completion semantics.+/// Focus-neutral suggestion popup. The owner handles navigation and completion semantics. Its+/// rows take no focus, and where they were drawn is kept under `id.with("rows")`, so an owner+/// can act on a press before the click lands (#3257)./// Tokens: space_2, form_label_width, control_height, text_dim, scope control/font tokens.pub(crate) fn popup_list(ctx: &egui::Context,@@ -360,6 +362,7 @@ pub(crate) fn popup_list() -> Option<usize> {let t = crate::theme::Tokens::get();let mut chosen = None;+ let mut rows = Vec::with_capacity(options.len());let area = crate::frame::area(id).order(egui::Order::Tooltip).fixed_pos(anchor.left_bottom() + egui::vec2(0.0, t.space_2)).show(ctx, |ui| {egui::Frame::popup(ui.style()).fill(t.control_fill)@@ -370,7 +373,8 @@ pub(crate) fn popup_list(super::scope(ui, |ui| {ui.set_width(anchor.width().max(t.form_card_width - t.space_16 * 2.0 - t.space_2));for (index, option) in options.iter().enumerate() {- let (rect, response) = ui.allocate_exact_size(vec2(ui.available_width(), t.control_height), egui::Sense::click());+ let (rect, response) = ui.allocate_exact_size(vec2(ui.available_width(), t.control_height), egui::Sense::CLICK);+ rows.push(rect);let picked = index == selected;if picked || response.hovered() {ui.painter().rect_filled(rect, t.control_radius, if picked { t.selected_bg } else { t.hover });@@ -413,6 +417,7 @@ pub(crate) fn popup_list(});});crate::scenario::publish_rect("dialog:suggestions", area.response.rect);+ ctx.data_mut(|d| d.insert_temp(id.with("rows"), rows));chosen}