diff --git a/crates/ui-egui/src/dialogs_assembly_tests.rs b/crates/ui-egui/src/dialogs_assembly_tests.rs index ba14837a0..e78310bd0 100644 --- a/crates/ui-egui/src/dialogs_assembly_tests.rs +++ b/crates/ui-egui/src/dialogs_assembly_tests.rs @@ -450,6 +450,59 @@ fn drive_slider_previews_intermediate_positions_before_release() { assert_eq!(h.app.session.doc, before, "cancel leaves the joint at its original angle"); } +/// Dragging the slider rewrites the value box, which keeps its focus (a drag is not a click) and +/// its selection: the rewritten text must not open the parameter suggestions over the slider. +#[test] +fn slider_drag_does_not_open_suggestions_over_the_slider() { + let mut h = crate::scenario::Harness::new(); + h.app = two_boxes(); + for name in ["depth", "drop"] { + h.app.run("parameters.add", json!({"name": name, "expression": "5 mm"})).unwrap(); + } + let (a, b) = (occ(&h.app, "A"), occ(&h.app, "B")); + h.app + .run( + "joint.create", + json!({"type": "revolute", "a": {"occurrence": a, "face": [10, 10, 10]}, + "b": {"occurrence": b, "face": [40, 10, 0]}, "limits": [[-90, 90]]}), + ) + .unwrap(); + h.app.start("joint.drive"); + h.frames(4); + let editor = egui::Id::new("sc_drive_value"); + let bounds = h + .output + .accesskit_update + .as_ref() + .unwrap() + .nodes + .iter() + .find_map(|(_, n)| (n.role() == egui::accesskit::Role::Slider).then(|| n.bounds()).flatten()) + .expect("drive slider"); + let at = |fraction: f64| egui::pos2((bounds.x0 + fraction * (bounds.x1 - bounds.x0)) as f32, ((bounds.y0 + bounds.y1) / 2.0) as f32); + h.app.synthetic.extend([ + egui::Event::PointerMoved(at(0.3)), + egui::Event::PointerButton { pos: at(0.3), button: egui::PointerButton::Primary, pressed: true, modifiers: Default::default() }, + ]); + // Past 0 deg the selection left from "0 deg" ends after the "d" of "6.0 deg": a word. + for step in 0..=10 { + h.app.synthetic.push(egui::Event::PointerMoved(at(0.3 + 0.023 * step as f64))); + h.frames(2); + assert!(!crate::expression_complete::showing(&h.ctx, editor), "suggestions opened during the drag (step {step})"); + } + h.app.synthetic.push(egui::Event::PointerButton { + pos: at(0.53), + button: egui::PointerButton::Primary, + pressed: false, + modifiers: Default::default(), + }); + h.frames(3); + assert!(h.ctx.memory(|m| m.has_focus(editor)), "the value box kept its focus through the drag"); + assert!(!crate::expression_complete::showing(&h.ctx, editor), "suggestions opened after the drag"); + let Some(Dialog { kind: Kind::Assembly(Asm::Drive { values, .. }), .. }) = h.app.dialog.as_ref() else { panic!("drive dialog") }; + assert_eq!(values[0].get(3..5), Some(" d"), "the caret ends after a \"d\": {values:?}"); +} + /// Named joint origins are drawn where their component is: a drive of the occurrence, or a free /// move not captured yet, carries them along. #[test] diff --git a/crates/ui-egui/src/expression_complete.rs b/crates/ui-egui/src/expression_complete.rs index db52278c9..08d1a1074 100644 --- a/crates/ui-egui/src/expression_complete.rs +++ b/crates/ui-egui/src/expression_complete.rs @@ -95,6 +95,13 @@ pub(crate) fn begin_frame(ctx: &egui::Context) { ctx.data_mut(|d| d.insert_temp(popup_id(), popup)); } +/// Whether `editor`'s suggestions were drawn in the last frame. +#[cfg(test)] +pub(crate) fn showing(ctx: &egui::Context, editor: egui::Id) -> bool { + ctx.data(|d| d.get_temp::(popup_id())) + .is_some_and(|p| p.editor == editor && p.count > 0 && !p.dismissed && p.frame.saturating_add(1) >= ctx.cumulative_frame_nr()) +} + pub(crate) fn complete(ui: &mut egui::Ui, resp: &egui::Response, text: &mut String) -> bool { let ctx = ui.ctx().clone(); // A press on a suggestion takes the focus from the editor before the list can see it @@ -117,8 +124,11 @@ pub(crate) fn complete(ui: &mut egui::Ui, resp: &egui::Response, text: &mut Stri let state = egui::TextEdit::load_state(&ctx, resp.id); let cursor = state.as_ref().and_then(|s| s.cursor.char_range()).map(|r| r.primary.index.0).unwrap_or_else(|| text.chars().count()); let (start, word) = word_before(text, cursor); - let previous = ctx.data(|d| d.get_temp::(popup_id())); - let mut popup = previous.filter(|p| p.editor == resp.id && p.word == word && p.cursor == cursor).unwrap_or(Popup { + let previous = ctx.data(|d| d.get_temp::(popup_id())).filter(|p| p.editor == resp.id); + // Dismissed suggestions stay closed until this box is typed in: another control rewriting + // its text (a slider dragged while the box keeps the focus) or a moved caret is no reason. + let dismissed = previous.as_ref().is_some_and(|p| p.dismissed) && !resp.changed(); + let mut popup = previous.filter(|p| p.word == word && p.cursor == cursor).unwrap_or(Popup { editor: resp.id, frame: 0, word, @@ -126,15 +136,21 @@ pub(crate) fn complete(ui: &mut egui::Ui, resp: &egui::Response, text: &mut Stri selected: 0, count: 0, accept: false, - dismissed: false, + dismissed, explicit: false, rows: Vec::new(), }); + // Never over a control being dragged: the drag dismisses them. + if ctx.dragged_id().is_some_and(|id| id != resp.id) { + popup.dismissed = true; + } let list = suggestions_for(&ctx, &popup.word, popup.explicit); popup.frame = ctx.cumulative_frame_nr(); popup.count = list.len(); popup.selected = popup.selected.min(list.len().saturating_sub(1)); if list.is_empty() || popup.dismissed { + // Nothing drawn: a press where the rows were is not on one. + popup.rows.clear(); ctx.data_mut(|d| d.insert_temp(popup_id(), popup)); return false; } @@ -337,6 +353,23 @@ mod tests { assert!(e.leaked.contains(&Key::Enter)); } + /// Text another control writes into the box (a slider drag) leaves dismissed suggestions + /// closed, though they would offer something for the new word; typing opens them again. + #[test] + fn dismissed_suggestions_stay_closed_until_the_box_is_typed_in() { + let mut e = Editor::new("wid"); + assert!(showing(&e.ctx, e.id)); + e.key(Key::Escape); + assert!(!showing(&e.ctx, e.id)); + e.text = "wir".into(); + e.frame(Vec::new()); + assert!(!suggestions(&e.ctx, "wir").is_empty()); + assert!(!showing(&e.ctx, e.id), "a rewritten value does not reopen them"); + e.key(Key::Backspace); + assert_eq!(e.text, "wi"); + assert!(showing(&e.ctx, e.id), "typing does"); + } + #[test] fn absent_popup_preserves_editor_and_application_keys() { let mut e = Editor::new("123");