From b23eaa591cd4501702991c0a9652f3cce0a8c542 Mon Sep 17 00:00:00 2001 From: Zhan Zhancheng Date: Mon, 23 Nov 2026 21:18:42 +0800 Subject: [PATCH 1/3] Dynamic dimension inline editor: claim pointer drag, select text on open, commit on focus loss --- crates/ui-egui/src/parametric.rs | 70 ++++-- crates/ui-egui/tests/it/dimcon_inline_edit.rs | 224 ++++++++++++++++++ crates/ui-egui/tests/it/main.rs | 1 + 3 files changed, 275 insertions(+), 20 deletions(-) create mode 100644 crates/ui-egui/tests/it/dimcon_inline_edit.rs diff --git a/crates/ui-egui/src/parametric.rs b/crates/ui-egui/src/parametric.rs index fb9edec5b..9e76d736f 100644 --- a/crates/ui-egui/src/parametric.rs +++ b/crates/ui-egui/src/parametric.rs @@ -60,14 +60,22 @@ fn dof_text(status: &str, dof: Option) -> Option<(String, Color32)> { } } +/// The dimensional constraint whose expression is being edited inline on the canvas. +#[derive(Clone)] +struct InlineEdit { + name: String, + original: String, + buf: String, + at: Pos2, +} + /// Per-drawing-revision cache of the constraint glyphs (inspect is not free on big drawings). #[derive(Default)] pub struct Cache { key: Option<(u64, u64, usize)>, glyphs: Vec, dof: Dof, - /// The dimensional constraint whose expression is being edited: (name, buffer, screen pos). - editing: Option<(String, String, Pos2)>, + editing: Option, } fn doc_key(app: &CadApp) -> Option<(u64, u64, usize)> { @@ -268,10 +276,13 @@ struct DimensionalContext<'a> { ui: &'a mut egui::Ui, tokens: Tokens, units: (i64, i64), + /// Clickable only at the Command prompt with no selection window open, so picks during a + /// command still reach the drawing (matches `viewcube::ucs_icon`). + interactive: bool, } fn draw_dimensional_constraints(frame: DimensionalContext<'_>) -> Option { - let DimensionalContext { doc: d, settings: set, glyphs, xf, painter, ui, tokens: t, units: (lu, lp) } = frame; + let DimensionalContext { doc: d, settings: set, glyphs, xf, painter, ui, tokens: t, units: (lu, lp), interactive } = frame; let mut act = None; // ----- dynamic (dimensional) constraints ----- let dim_c = Color32::from_rgb(0x9f, 0xb6, 0xd6); @@ -394,9 +405,15 @@ fn draw_dimensional_constraints(frame: DimensionalContext<'_>) -> Option { painter.rect_filled(r, 2.0, t.canvas.gamma_multiply(0.85)); lock(painter, Rect::from_min_size(r.min + vec2(3.0, (r.height() - 13.0) / 2.0), vec2(13.0, 13.0)), col); painter.galley(r.min + vec2(19.0, 2.0), galley, col); - let resp = ui.interact(r, ui.scope_id().with(("dimcon", c.id)), Sense::click()); - if resp.double_clicked() { - act = Some(Act::Edit(c.name.clone(), c.expr.clone(), r.left_center())); + // Sense drag as well as click so the canvas underneath (which uses `click_and_drag`) does + // not stay hovered over the label, start selection windows on click, or steal double-clicks. + let sense = if interactive { Sense::click_and_drag() } else { Sense::hover() }; + let mut resp = ui.interact(r, ui.scope_id().with(("dimcon", c.id)), sense); + if interactive { + resp = resp.on_hover_cursor(egui::CursorIcon::PointingHand); + if resp.double_clicked() { + act = Some(Act::Edit(c.name.clone(), c.expr.clone(), r.left_center())); + } } resp.on_hover_text(crate::tf!( "{kind} constraint {name} = {expression} (double-click to edit)", @@ -510,43 +527,56 @@ pub fn draw_overlay(app: &mut CadApp, ui: &mut egui::Ui, painter: &egui::Painter let set = &d.parametric.settings; let glyphs = &app.canvas.param.glyphs; let (lu, lp) = (d.header.i64("LUNITS", 2), d.header.i64("LUPREC", 4)); - let act = draw_dimensional_constraints(DimensionalContext { doc: d, settings: set, glyphs, xf, painter, ui, tokens: t, units: (lu, lp) }); + let interactive = app.session.running.is_none() && app.session.pending_window.is_none(); + let act = + draw_dimensional_constraints(DimensionalContext { doc: d, settings: set, glyphs, xf, painter, ui, tokens: t, units: (lu, lp), interactive }); draw_geometric_constraints(app, ui, painter, xf, t); let _ = act_take(app, act, ui); } fn act_take(app: &mut CadApp, act: Option, ui: &mut egui::Ui) -> Option<()> { + let te_id = egui::Id::unique("dimcon_edit_te"); if let Some(Act::Edit(name, expr, at)) = act { - app.canvas.param.editing = Some((name, expr, at)); + ui.ctx().data_mut(|d| d.remove::(te_id)); + app.canvas.param.editing = Some(InlineEdit { name, original: expr.clone(), buf: expr, at }); } // The inline expression editor. - let (name, mut buf, at) = app.canvas.param.editing.clone()?; + let mut edit = app.canvas.param.editing.clone()?; let mut done: Option = None; - egui::Area::new(egui::Id::unique("dimcon_edit")).order(egui::Order::Foreground).fixed_pos(at - vec2(0.0, 12.0)).show(ui.ctx(), |ui| { + egui::Area::new(egui::Id::unique("dimcon_edit")).order(egui::Order::Foreground).fixed_pos(edit.at - vec2(0.0, 12.0)).show(ui.ctx(), |ui| { egui::Frame::popup(ui.style()).show(ui, |ui| { ui.horizontal(|ui| { - let label = ui.label(format!("{name} =")); - let r = - ui.add(egui::TextEdit::singleline(&mut buf).desired_width(140.0).id(egui::Id::unique("dimcon_edit_te"))).labelled_by(label.id); - if !r.has_focus() && !r.lost_focus() { + let label = ui.label(format!("{} =", edit.name)); + let opening = egui::TextEdit::load_state(ui.ctx(), te_id).is_none(); + let r = ui.add(egui::TextEdit::singleline(&mut edit.buf).desired_width(140.0).id(te_id)).labelled_by(label.id); + if opening { r.request_focus(); - } - if r.lost_focus() { - done = Some(ui.input(|i| i.key_pressed(egui::Key::Enter))); + if let Some(mut state) = egui::TextEdit::load_state(ui.ctx(), te_id) { + let all = egui::text::CCursorRange::two(egui::text::CCursor::new(0), egui::text::CCursor::new(edit.buf.chars().count())); + state.cursor.set_char_range(Some(all)); + state.store(ui.ctx(), te_id); + } } if ui.input(|i| i.key_pressed(egui::Key::Escape)) { done = Some(false); + } else if r.lost_focus() { + let trimmed = edit.buf.trim(); + done = Some(!trimmed.is_empty() && trimmed != edit.original.trim()); } }); }); }); match done { Some(true) => { + ui.ctx().data_mut(|d| d.remove::(te_id)); app.canvas.param.editing = None; - let _ = app.run("parameters", json!({ "name": name, "expr": buf.trim() })); + let _ = app.run("parameters", json!({ "name": edit.name, "expr": edit.buf.trim() })); } - Some(false) => app.canvas.param.editing = None, - None => app.canvas.param.editing = Some((name, buf, at)), + Some(false) => { + ui.ctx().data_mut(|d| d.remove::(te_id)); + app.canvas.param.editing = None; + } + None => app.canvas.param.editing = Some(edit), } Some(()) } diff --git a/crates/ui-egui/tests/it/dimcon_inline_edit.rs b/crates/ui-egui/tests/it/dimcon_inline_edit.rs new file mode 100644 index 000000000..6afab527a --- /dev/null +++ b/crates/ui-egui/tests/it/dimcon_inline_edit.rs @@ -0,0 +1,224 @@ +//! Double-clicking a dynamic dimensional constraint on the canvas edits its expression inline: +//! clicks on the label stay off the canvas (no selection window, no text edit behind it), slight +//! pointer movement during the double-click still opens the box, the existing expression starts +//! selected so typing replaces it, and Enter or focus loss commits while Escape cancels. + +use cadcraft_engine::Session; +use cadcraft_engine::doc::{Drawing, EntityKind}; +use cadcraft_engine::geom::Vec2; +use cadcraft_ui_egui::{CadApp, Services}; +use egui::accesskit::Role; +use egui::{Event, Key, Modifiers, PointerButton, Pos2, pos2, vec2}; +use egui_kittest::Harness; +use egui_kittest::kittest::Queryable; +use serde_json::json; + +fn harness() -> Harness<'static, CadApp> { + let mut session = Session::empty(); + session.open_drawing(Drawing::new_imperial(), "Dimcon inline edit", None); + session.settings.osmode = 0; + session.execute("line", &json!({ "points": [[0, 0], [20, 0]] })).unwrap(); + let handle = session.doc().unwrap().model.iter().next().unwrap().handle.hex(); + session.execute("dchorizontal", &json!({ "h1": handle, "value": 20.0, "name": "d1" })).unwrap(); + session.execute("view.set", &json!({ "center": [10, 5], "height": 40 })).unwrap(); + let mut app = CadApp::new(session, Services::default()); + app.canvas.zoom_pending = false; + // 60 frames a second: the default step (0.25 s) spreads two clicks past the double-click delay. + let mut h = Harness::builder().with_size(vec2(1280.0, 760.0)).with_step_dt(1.0 / 60.0).build_ui_state( + |ui, app: &mut CadApp| { + app.logic(ui.ctx()); + app.ui(ui); + }, + app, + ); + h.run_steps(5); + h +} + +/// Screen centre of `d1`'s dynamic dimension label (26 pt above the horizontal line's midpoint). +fn pill_pos(h: &Harness<'_, CadApp>) -> Pos2 { + let xf = h.state().canvas.xf.unwrap(); + let d = h.state().session.doc().unwrap(); + let (a, b) = d + .model + .iter() + .find_map(|e| match &e.kind { + EntityKind::Line(l) => Some((l.a.xy(), l.b.xy())), + _ => None, + }) + .unwrap(); + let s1 = xf.to_screen(a); + let s2 = xf.to_screen(b); + pos2((s1.x + s2.x) * 0.5, s1.y.min(s2.y) - 26.0) +} + +fn click_at(h: &mut Harness<'_, CadApp>, pos: Pos2) { + h.event(Event::PointerMoved(pos)); + h.event(Event::PointerButton { pos, button: PointerButton::Primary, pressed: true, modifiers: Modifiers::NONE }); + h.run_steps(1); + h.event(Event::PointerButton { pos, button: PointerButton::Primary, pressed: false, modifiers: Modifiers::NONE }); + h.run_steps(1); +} + +fn double_click_at(h: &mut Harness<'_, CadApp>, pos: Pos2) { + h.event(Event::PointerMoved(pos)); + h.run_steps(2); + click_at(h, pos); + click_at(h, pos); + h.run_steps(2); +} + +fn d1_expr(h: &Harness<'_, CadApp>) -> String { + let d = h.state().session.doc().unwrap(); + d.constraints.iter().find(|c| c.name == "d1").unwrap().expr.clone() +} + +fn line_length(h: &Harness<'_, CadApp>) -> f64 { + let d = h.state().session.doc().unwrap(); + d.model + .iter() + .find_map(|e| match &e.kind { + EntityKind::Line(l) => Some(l.a.xy().dist(l.b.xy())), + _ => None, + }) + .unwrap() +} + +#[test] +fn double_click_replaces_expression_without_opening_selection_window() { + let mut h = harness(); + let pos = pill_pos(&h); + + // Hover and single-click on the label must not open a canvas selection window. + h.event(Event::PointerMoved(pos)); + h.run_steps(2); + click_at(&mut h, pos); + h.run_steps(2); + assert!(h.state().session.pending_window.is_none(), "clicking the label opened a selection window"); + + // Double-click opens the inline editor with the existing expression selected, so typing + // replaces it instead of appending to `20`. + double_click_at(&mut h, pos); + assert!(h.state().session.pending_window.is_none()); + assert!(h.get_by_role_and_label(Role::TextInput, "d1 =").is_focused(), "the inline expression editor has keyboard focus"); + h.event(Event::Text("30".into())); + h.run_steps(2); + assert!(h.state().cmd.buffer.is_empty(), "keys went to the command line instead of the inline box"); + h.key_press(Key::Enter); + h.run_steps(3); + assert_eq!(d1_expr(&h), "30"); + assert!((line_length(&h) - 30.0).abs() < 1e-6); + + // Wait past egui's double-click window so the next gesture is a fresh double-click, then + // re-open and verify the text box starts with the updated expression selected cleanly. + h.run_steps(25); + let pos2 = pill_pos(&h); + double_click_at(&mut h, pos2); + h.event(Event::Text("25".into())); + h.run_steps(2); + h.key_press(Key::Enter); + h.run_steps(3); + assert_eq!(d1_expr(&h), "25"); + assert!((line_length(&h) - 25.0).abs() < 1e-6); +} + +#[test] +fn double_click_with_slight_pointer_drift_opens_editor() { + let mut h = harness(); + let pos = pill_pos(&h); + let drifted = pos + vec2(2.0, 1.5); + h.event(Event::PointerMoved(pos)); + h.run_steps(2); + + // First click drifts 2.5 pt across multiple pointer moves while held (typical trackpad/mouse + // jitter); second click lands at the drifted position. + h.event(Event::PointerButton { pos, button: PointerButton::Primary, pressed: true, modifiers: Modifiers::NONE }); + for i in 1..=6 { + let t = i as f32 / 6.0; + h.event(Event::PointerMoved(pos + (drifted - pos) * t)); + } + h.event(Event::PointerButton { pos: drifted, button: PointerButton::Primary, pressed: false, modifiers: Modifiers::NONE }); + h.run_steps(1); + click_at(&mut h, drifted); + h.run_steps(2); + + assert!(h.state().session.pending_window.is_none()); + h.event(Event::Text("35".into())); + h.run_steps(2); + h.key_press(Key::Enter); + h.run_steps(3); + assert_eq!(d1_expr(&h), "35"); + assert!((line_length(&h) - 35.0).abs() < 1e-6); +} + +#[test] +fn double_click_on_pill_over_text_edits_constraint_not_text() { + let mut h = harness(); + let pos = pill_pos(&h); + let world = h.state().canvas.xf.unwrap().to_world(pos); + h.state_mut().run("text", json!({ "at": [world.x - 4.0, world.y - 1.2], "text": "LONG TEXT LABEL", "height": 2.5 })).unwrap(); + h.run_steps(3); + + double_click_at(&mut h, pos); + assert_eq!(h.state().session.running.as_ref().map(|r| r.id.as_str()), None, "TEXTEDIT stole the double-click"); + h.event(Event::Text("28".into())); + h.run_steps(2); + h.key_press(Key::Enter); + h.run_steps(3); + assert_eq!(d1_expr(&h), "28"); +} + +#[test] +fn clicking_outside_commits_changed_expression_and_escape_cancels() { + let mut h = harness(); + let pos = pill_pos(&h); + + double_click_at(&mut h, pos); + h.event(Event::Text("42".into())); + h.run_steps(2); + // Click empty canvas away from the popup to finish editing without Enter (matches Parameters Manager). + let outside = h.state().canvas.xf.unwrap().to_screen(Vec2::new(10.0, -10.0)); + click_at(&mut h, outside); + h.run_steps(3); + assert_eq!(d1_expr(&h), "42"); + + // Dismiss any selection window opened by the outside click, then re-open and cancel with Escape. + h.key_press(Key::Escape); + h.run_steps(2); + let pos2 = pill_pos(&h); + double_click_at(&mut h, pos2); + h.event(Event::Text("99".into())); + h.run_steps(2); + h.key_press(Key::Escape); + h.run_steps(3); + assert_eq!(d1_expr(&h), "42"); +} + +#[test] +fn pending_inline_edit_survives_window_blur() { + let mut h = harness(); + let pos = pill_pos(&h); + double_click_at(&mut h, pos); + h.event(Event::Text("32".into())); + h.run_steps(2); + h.event(Event::WindowFocused(false)); + h.run_steps(2); + h.event(Event::WindowFocused(true)); + h.run_steps(2); + h.key_press(Key::Enter); + h.run_steps(3); + assert_eq!(d1_expr(&h), "32"); +} + +#[test] +fn clicking_pill_during_command_passes_point_to_drawing() { + let mut h = harness(); + let pos = pill_pos(&h); + h.state_mut().start("line"); + h.run_steps(2); + assert_eq!(h.state().session.current_prompt().unwrap().message, "Specify first point"); + + click_at(&mut h, pos); + h.run_steps(2); + assert_eq!(h.state().session.current_prompt().unwrap().message, "Specify next point"); +} diff --git a/crates/ui-egui/tests/it/main.rs b/crates/ui-egui/tests/it/main.rs index 7ff34c0ca..819ea782f 100644 --- a/crates/ui-egui/tests/it/main.rs +++ b/crates/ui-egui/tests/it/main.rs @@ -8,6 +8,7 @@ mod control_cmdline; mod control_cmdline_key; mod control_methods; mod dialog_dispatch; +mod dimcon_inline_edit; mod dimension_menu; mod dimstyle_dialog; mod docking_workflows; From 34777543d9ec4f9a6b2de6c5289fd27bb5905ef1 Mon Sep 17 00:00:00 2001 From: Zhan Zhancheng Date: Tue, 24 Nov 2026 08:00:12 +0800 Subject: [PATCH 2/3] ui-egui: guard inline dimcon edit across drawings and LISP prompts --- crates/ui-egui/src/parametric.rs | 20 ++++-- crates/ui-egui/tests/it/dimcon_inline_edit.rs | 63 +++++++++++++++++++ 2 files changed, 78 insertions(+), 5 deletions(-) diff --git a/crates/ui-egui/src/parametric.rs b/crates/ui-egui/src/parametric.rs index 9e76d736f..51aa656d7 100644 --- a/crates/ui-egui/src/parametric.rs +++ b/crates/ui-egui/src/parametric.rs @@ -63,6 +63,8 @@ fn dof_text(status: &str, dof: Option) -> Option<(String, Color32)> { /// The dimensional constraint whose expression is being edited inline on the canvas. #[derive(Clone)] struct InlineEdit { + /// Active drawing (`DocState::uid`) when editing started; switching drawings drops the edit. + uid: u64, name: String, original: String, buf: String, @@ -276,8 +278,8 @@ struct DimensionalContext<'a> { ui: &'a mut egui::Ui, tokens: Tokens, units: (i64, i64), - /// Clickable only at the Command prompt with no selection window open, so picks during a - /// command still reach the drawing (matches `viewcube::ucs_icon`). + /// Clickable only when idle (no active command or LISP prompt, and no selection window open), + /// so picks during a prompt still reach the drawing (matches `viewcube::ucs_icon`). interactive: bool, } @@ -520,14 +522,16 @@ pub fn draw_overlay(app: &mut CadApp, ui: &mut egui::Ui, painter: &egui::Painter refresh(app); let Ok(d) = app.session.doc() else { return }; if d.constraints.is_empty() { - app.canvas.param.editing = None; + if app.canvas.param.editing.take().is_some() { + ui.ctx().data_mut(|d| d.remove::(egui::Id::unique("dimcon_edit_te"))); + } return; } let t = Tokens::get(); let set = &d.parametric.settings; let glyphs = &app.canvas.param.glyphs; let (lu, lp) = (d.header.i64("LUNITS", 2), d.header.i64("LUPREC", 4)); - let interactive = app.session.running.is_none() && app.session.pending_window.is_none(); + let interactive = !app.session.prompting() && app.session.pending_window.is_none(); let act = draw_dimensional_constraints(DimensionalContext { doc: d, settings: set, glyphs, xf, painter, ui, tokens: t, units: (lu, lp), interactive }); draw_geometric_constraints(app, ui, painter, xf, t); @@ -536,12 +540,18 @@ pub fn draw_overlay(app: &mut CadApp, ui: &mut egui::Ui, painter: &egui::Painter fn act_take(app: &mut CadApp, act: Option, ui: &mut egui::Ui) -> Option<()> { let te_id = egui::Id::unique("dimcon_edit_te"); + let uid = app.session.state().ok()?.uid; if let Some(Act::Edit(name, expr, at)) = act { ui.ctx().data_mut(|d| d.remove::(te_id)); - app.canvas.param.editing = Some(InlineEdit { name, original: expr.clone(), buf: expr, at }); + app.canvas.param.editing = Some(InlineEdit { uid, name, original: expr.clone(), buf: expr, at }); } // The inline expression editor. let mut edit = app.canvas.param.editing.clone()?; + if edit.uid != uid { + ui.ctx().data_mut(|d| d.remove::(te_id)); + app.canvas.param.editing = None; + return None; + } let mut done: Option = None; egui::Area::new(egui::Id::unique("dimcon_edit")).order(egui::Order::Foreground).fixed_pos(edit.at - vec2(0.0, 12.0)).show(ui.ctx(), |ui| { egui::Frame::popup(ui.style()).show(ui, |ui| { diff --git a/crates/ui-egui/tests/it/dimcon_inline_edit.rs b/crates/ui-egui/tests/it/dimcon_inline_edit.rs index 6afab527a..5d32778e0 100644 --- a/crates/ui-egui/tests/it/dimcon_inline_edit.rs +++ b/crates/ui-egui/tests/it/dimcon_inline_edit.rs @@ -222,3 +222,66 @@ fn clicking_pill_during_command_passes_point_to_drawing() { h.run_steps(2); assert_eq!(h.state().session.current_prompt().unwrap().message, "Specify next point"); } + +#[test] +fn clicking_pill_during_lisp_prompt_passes_point_to_drawing() { + let mut h = harness(); + let pos = pill_pos(&h); + h.state_mut().session.cmdline("(setq p (getpoint \"\nPick: \"))").unwrap(); + h.run_steps(2); + assert!(h.state().session.prompting()); + + click_at(&mut h, pos); + h.run_steps(2); + assert!(!h.state().session.lisp_running(), "the click on the label answered the LISP prompt"); + assert!(h.state().session.lisp_var("P").as_point().is_some(), "the LISP prompt received a point"); +} + +#[test] +fn switching_drawing_tabs_drops_pending_edit_without_writing_to_other_drawing() { + let mut session = Session::empty(); + let add_d1 = |session: &mut Session, title: &str, length: f64| { + session.open_drawing(Drawing::new_imperial(), title, None); + session.settings.osmode = 0; + session.execute("line", &json!({ "points": [[0, 0], [length, 0]] })).unwrap(); + let handle = session.doc().unwrap().model.iter().next().unwrap().handle.hex(); + session.execute("dchorizontal", &json!({ "h1": handle, "value": length, "name": "d1" })).unwrap(); + session.execute("view.set", &json!({ "center": [length / 2.0, 5.0], "height": 40 })).unwrap(); + }; + add_d1(&mut session, "First drawing", 20.0); + let first = session.active; + add_d1(&mut session, "Second drawing", 10.0); + let second = session.active; + session.execute("document.switch", &json!({ "index": first })).unwrap(); + + let mut app = CadApp::new(session, Services::default()); + app.canvas.zoom_pending = false; + let mut h = Harness::builder().with_size(vec2(1280.0, 760.0)).with_step_dt(1.0 / 60.0).build_ui_state( + |ui, app: &mut CadApp| { + app.logic(ui.ctx()); + app.ui(ui); + }, + app, + ); + h.run_steps(5); + + let pos = pill_pos(&h); + double_click_at(&mut h, pos); + h.event(Event::Text("99".into())); + h.run_steps(2); + + // Click the other drawing's tab: switching drawings before the canvas draws must drop the + // pending inline edit instead of committing `99` to the other drawing's `d1`. + h.get_by_label("Second drawing").click(); + h.run_steps(3); + + assert_eq!(h.state().session.active, second); + assert!(h.query_by_role_and_label(Role::TextInput, "d1 =").is_none(), "inline editor closed on tab switch"); + assert_eq!(d1_expr(&h), "10", "the second drawing's d1 was not overwritten"); + assert!((line_length(&h) - 10.0).abs() < 1e-6); + + h.get_by_label("First drawing").click(); + h.run_steps(3); + assert_eq!(h.state().session.active, first); + assert_eq!(d1_expr(&h), "20", "the first drawing's pending edit was dropped"); +} From 1d28c3ef327d09d09f9360123ef0afe0dc50a3ce Mon Sep 17 00:00:00 2001 From: Zhan Zhancheng Date: Tue, 24 Nov 2026 09:08:41 +0800 Subject: [PATCH 3/3] ui-egui: prevent inline dimcon Enter/Escape from leaking to command line --- crates/ui-egui/src/canvas.rs | 10 +++- crates/ui-egui/src/parametric.rs | 16 +++++++ crates/ui-egui/tests/it/dimcon_inline_edit.rs | 47 +++++++++++++++++++ 3 files changed, 71 insertions(+), 2 deletions(-) diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rs index 8aa940ea0..51f494d22 100644 --- a/crates/ui-egui/src/canvas.rs +++ b/crates/ui-egui/src/canvas.rs @@ -557,8 +557,12 @@ fn paint_pointer_overlay(frame: &mut CanvasPaint<'_>, paper_rect: Option, draw_viewcube(app, ui, rect); } viewport_label(painter, rect); - // Typing and Escape reach the command line even when its overlay is hidden. - crate::cmdline::keyboard(app, ui.ctx()); + // Typing and Escape reach the command line even when its overlay is hidden, unless the + // inline constraint editor already handled this frame's keys (Enter would otherwise repeat + // `last_command`, e.g. immediately undoing the edit after Cmd+Z). + if !crate::parametric::take_keyboard(app) { + crate::cmdline::keyboard(app, ui.ctx()); + } // Command line overlay. if app.ui.show_command_line { crate::cmdline::show(app, ui, full); @@ -1201,6 +1205,8 @@ fn paint_canvas(frame: CanvasPaint<'_>) { }; let p = painter.with_clip_rect(clip); crate::parametric::draw_overlay(app, ui, &p, &xf); + } else { + crate::parametric::dismiss_inline(app, ui.ctx()); } // Grips on selected objects (when idle or editing grips). let grip_edit = app.session.running.as_ref().is_some_and(|r| r.id == "grip"); diff --git a/crates/ui-egui/src/parametric.rs b/crates/ui-egui/src/parametric.rs index 51aa656d7..ff569257b 100644 --- a/crates/ui-egui/src/parametric.rs +++ b/crates/ui-egui/src/parametric.rs @@ -78,6 +78,9 @@ pub struct Cache { glyphs: Vec, dof: Dof, editing: Option, + /// True on the frame the inline editor rendered, so `cmdline::keyboard` later in the same frame + /// does not also consume the Enter or Escape that just closed it (repeating `last_command`, e.g. Undo). + took_keyboard: bool, } fn doc_key(app: &CadApp) -> Option<(u64, u64, usize)> { @@ -518,7 +521,19 @@ fn draw_geometric_constraints(app: &CadApp, ui: &mut egui::Ui, painter: &egui::P } } +pub fn take_keyboard(app: &mut CadApp) -> bool { + std::mem::take(&mut app.canvas.param.took_keyboard) +} + +pub fn dismiss_inline(app: &mut CadApp, ctx: &egui::Context) { + if app.canvas.param.editing.take().is_some() { + ctx.data_mut(|d| d.remove::(egui::Id::unique("dimcon_edit_te"))); + } + app.canvas.param.took_keyboard = false; +} + pub fn draw_overlay(app: &mut CadApp, ui: &mut egui::Ui, painter: &egui::Painter, xf: &Xf) { + app.canvas.param.took_keyboard = false; refresh(app); let Ok(d) = app.session.doc() else { return }; if d.constraints.is_empty() { @@ -552,6 +567,7 @@ fn act_take(app: &mut CadApp, act: Option, ui: &mut egui::Ui) -> Option<()> app.canvas.param.editing = None; return None; } + app.canvas.param.took_keyboard = true; let mut done: Option = None; egui::Area::new(egui::Id::unique("dimcon_edit")).order(egui::Order::Foreground).fixed_pos(edit.at - vec2(0.0, 12.0)).show(ui.ctx(), |ui| { egui::Frame::popup(ui.style()).show(ui, |ui| { diff --git a/crates/ui-egui/tests/it/dimcon_inline_edit.rs b/crates/ui-egui/tests/it/dimcon_inline_edit.rs index 5d32778e0..e8981ee76 100644 --- a/crates/ui-egui/tests/it/dimcon_inline_edit.rs +++ b/crates/ui-egui/tests/it/dimcon_inline_edit.rs @@ -285,3 +285,50 @@ fn switching_drawing_tabs_drops_pending_edit_without_writing_to_other_drawing() assert_eq!(h.state().session.active, first); assert_eq!(d1_expr(&h), "20", "the first drawing's pending edit was dropped"); } + +#[test] +fn enter_to_commit_does_not_repeat_last_command() { + let mut h = harness(); + let pos = pill_pos(&h); + + // Edit once, then Undo via `u` (sets `last_command = Some("u")`, matching Cmd+Z). + double_click_at(&mut h, pos); + h.event(Event::Text("30".into())); + h.run_steps(2); + h.key_press(Key::Enter); + h.run_steps(3); + assert_eq!(d1_expr(&h), "30"); + + h.state_mut().start("u"); + h.run_steps(3); + assert_eq!(d1_expr(&h), "20"); + assert_eq!(h.state().session.last_command.as_deref(), Some("u")); + + // Committing with Enter must not leak into `cmdline::keyboard` in the same frame and repeat Undo. + h.run_steps(25); + let pos2 = pill_pos(&h); + double_click_at(&mut h, pos2); + h.event(Event::Text("35".into())); + h.run_steps(2); + h.key_press(Key::Enter); + h.run_steps(3); + assert_eq!(d1_expr(&h), "35", "Enter must not repeat Undo and revert the edit"); + assert!((line_length(&h) - 35.0).abs() < 1e-6); + + // Same when `last_command` is an interactive drawing command (LINE): Enter must not start it. + h.state_mut().start("line"); + h.run_steps(2); + h.key_press(Key::Escape); + h.run_steps(2); + assert_eq!(h.state().session.last_command.as_deref(), Some("line")); + + h.run_steps(25); + let pos3 = pill_pos(&h); + double_click_at(&mut h, pos3); + h.event(Event::Text("40".into())); + h.run_steps(2); + h.key_press(Key::Enter); + h.run_steps(3); + assert_eq!(d1_expr(&h), "40"); + assert!(h.state().session.running.is_none(), "Enter must not repeat LINE"); +}