diff --git a/crates/ui-egui/src/sketch_edit_tests.rs b/crates/ui-egui/src/sketch_edit_tests.rs index 1b56c954e..5617c07d1 100644 --- a/crates/ui-egui/src/sketch_edit_tests.rs +++ b/crates/ui-egui/src/sketch_edit_tests.rs @@ -286,6 +286,32 @@ mod view_corner { assert!(h.app.session.doc.features.is_empty(), "a new sketch goes with what was drawn in it"); } + /// With a line half drawn, the cross takes the same steps as Esc: the placed point, then the + /// tool, and only then the sketch, which keeps what was drawn until that last click. + #[test] + fn the_cross_cancels_an_unfinished_tool_before_the_sketch() { + let mut h = sketching(); + h.step(&json!({"start": "sketch.line"})).unwrap(); + assert!(h.app.tool.is_some(), "the line tool is out"); + h.step(&json!({"click": {"sketch": [10, 20]}})).unwrap(); + assert_eq!(h.app.tool.as_ref().map(|tool| tool.pts.len()), Some(1), "the line's first point is down"); + let cross = |h: &mut Harness| { + h.step(&json!({"click": {"handle": "view:confirm:cancel"}})).unwrap(); + h.settle(); + }; + + cross(&mut h); + assert_eq!(h.app.tool.as_ref().map(|tool| tool.pts.len()), Some(0), "the first click takes the point back"); + assert!(h.app.session.active_sketch.is_some()); + cross(&mut h); + assert!(h.app.tool.is_none(), "the second puts the tool down"); + assert!(h.app.session.active_sketch.is_some(), "and keeps the sketch open"); + assert_eq!(scenario::count_of("view:confirm:ok:enabled"), Some(1.0)); + cross(&mut h); + assert_eq!(h.app.session.active_sketch, None, "with nothing unfinished it cancels the sketch"); + assert!(h.app.session.doc.features.is_empty()); + } + #[test] fn a_refused_cancel_shows_its_error_and_keeps_the_sketch() { let mut h = Harness::new(); diff --git a/crates/ui-egui/src/toolbar.rs b/crates/ui-egui/src/toolbar.rs index 3d534d2fb..222801dae 100644 --- a/crates/ui-egui/src/toolbar.rs +++ b/crates/ui-egui/src/toolbar.rs @@ -2916,6 +2916,33 @@ pub(crate) fn selection_filter(app: &mut SolveApp, ui: &mut egui::Ui) { /// Two Esc presses within this many seconds finish the open sketch. pub const ESCAPE_TWICE_S: f64 = 0.4; +/// Whether `cancel_unfinished` has something to cancel. +pub(crate) fn unfinished(app: &SolveApp) -> bool { + app.tool.is_some() + || crate::select_other::is_open(app) + || app.menu.joint_move_warning.is_some() + || app.viewport.joint_handles.is_some() + || app.viewport.text_drag.is_some() + || crate::occurrence_drag::active(app) +} + +/// Cancels what one Esc cancels before it reaches the dialog, the selection or the sketch: +/// a drag, the Select Other list, or the active tool's points and then the tool. The view's +/// corner Cancel (#6709) takes this step first too. Returns whether anything was cancelled. +pub(crate) fn cancel_unfinished(app: &mut SolveApp) -> bool { + if crate::joint_handles::cancel(app) || crate::sketch_text_move::cancel_drag(app) || crate::occurrence_drag::cancel(app) { + return true; + } + if crate::select_other::is_open(app) { + crate::select_other::close(app); + } else if app.tool.is_some() { + crate::tools::finish(app); + } else { + return false; + } + true +} + /// Global keyboard shortcuts (when no text field has focus). pub fn shortcuts(app: &mut SolveApp, ctx: &egui::Context) { crate::viewport_status::reconcile(app, ctx); @@ -3111,14 +3138,10 @@ pub fn shortcuts(app: &mut SolveApp, ctx: &egui::Context) { } continue; } - if crate::joint_handles::cancel(app) || crate::sketch_text_move::cancel_drag(app) || crate::occurrence_drag::cancel(app) { + if cancel_unfinished(app) { continue; } - if crate::select_other::is_open(app) { - crate::select_other::close(app); - } else if app.tool.is_some() { - crate::tools::finish(app); - } else if app.dialog.is_some() || app.pending_feature_edit.is_some() { + if app.dialog.is_some() || app.pending_feature_edit.is_some() { crate::dialogs::cancel(app); } else if crate::viewport_status::enabled(app) && crate::viewport_status::escape(app, repeat) { app.last_escape = None; diff --git a/crates/ui-egui/src/view_confirm.rs b/crates/ui-egui/src/view_confirm.rs index 38f8e7042..32c73b704 100644 --- a/crates/ui-egui/src/view_confirm.rs +++ b/crates/ui-egui/src/view_confirm.rs @@ -1,7 +1,8 @@ //! OK and Cancel in the view's corner (Preferences › Interface, #6709). While a dialog is open, //! a check and a cross at the top right of the view do what the dialog's own OK and Cancel do, //! and the check greys out whenever the dialog's OK does. While a sketch is open without a -//! dialog, they are Finish Sketch and Cancel Sketch, as on the ribbon. The axis view moves down +//! dialog, they are Finish Sketch and Cancel Sketch, as on the ribbon, except that while a tool +//! is out the cross first cancels it, as one Esc does. The axis view moves down //! below the pair while it is shown (`viewport_layout::Layout::with_content`), and the pair is //! one of the shell's covered rects, so a click on it never picks or navigates. @@ -80,7 +81,10 @@ pub(crate) fn show(app: &mut SolveApp, ctx: &egui::Context) { Answers::Sketch => { let command = |id: &str| solidcraft_engine::find_command(id).map(|spec| (spec.label, spec.info(&app.session).enabled)); let (finish, finish_enabled) = command("sketch.finish").unwrap_or(("Finish Sketch", false)); - let (cancel, cancel_enabled) = command("sketch.cancel").unwrap_or(("Cancel Sketch", false)); + // While a tool or a drag is unfinished, the cross cancels that first, as one Esc does; + // the next click cancels the sketch. + let (cancel, cancel_enabled) = + if crate::toolbar::unfinished(app) { ("Cancel", true) } else { command("sketch.cancel").unwrap_or(("Cancel Sketch", false)) }; [ Choice { action: Action::Ok, key: "view:confirm:ok", icon: "finish", label: finish, enabled: finish_enabled }, Choice { action: Action::Cancel, key: "view:confirm:cancel", icon: "sketch_cancel", label: cancel, enabled: cancel_enabled }, @@ -116,7 +120,8 @@ pub(crate) fn show(app: &mut SolveApp, ctx: &egui::Context) { match (answers, clicked) { (Answers::Dialog, Some(action)) => app.viewport.corner = Some(action), (Answers::Sketch, Some(Action::Ok)) => app.start_pinned("sketch.finish", None), - (Answers::Sketch, Some(Action::Cancel)) => app.start_pinned("sketch.cancel", None), + // The guard cancels an unfinished tool first; only with nothing to cancel does the sketch go. + (Answers::Sketch, Some(Action::Cancel)) if !crate::toolbar::cancel_unfinished(app) => app.start_pinned("sketch.cancel", None), _ => {} } } diff --git a/crates/ui-egui/tests/scenarios/view_confirm_corner.json b/crates/ui-egui/tests/scenarios/view_confirm_corner.json index 44899e881..b8f45c7fe 100644 --- a/crates/ui-egui/tests/scenarios/view_confirm_corner.json +++ b/crates/ui-egui/tests/scenarios/view_confirm_corner.json @@ -19,13 +19,15 @@ {"click": {"handle": "view:confirm:ok"}}, {"until": {"dialog": null}}, {"expect": {"bodies": 1, "features": 2, "errors": 0}}, - {"note": "A new sketch, left with the cross: it goes, with what was drawn in it."}, + {"note": "A new sketch, left with the cross: like Esc, it first ends the line's chain, then puts the tool down, and only then the sketch goes, with what was drawn in it."}, {"start": "sketch.create"}, {"click": {"plane": "XY"}}, {"start": "sketch.line"}, {"click": {"sketch": [50, 0]}}, {"click": {"sketch": [70, 10]}}, - {"key": "Escape"}, + {"click": {"handle": "view:confirm:cancel"}}, + {"click": {"handle": "view:confirm:cancel"}}, + {"expect": {"sketching": true, "sketch_curves": {"own": 1}}}, {"click": {"handle": "view:confirm:cancel"}}, {"expect": {"sketching": false, "features": 2, "errors": 0}} ]