Sketch: a quick double Esc finishes the sketch (#3950)
* Sketch: a quick double Esc finishes the sketch One Esc still puts the tool down, then clears the selection, and with nothing left to cancel keeps the sketch open (#1804). A second press within 0.4 s finishes the sketch, keeping it, the same as Finish Sketch, also when the first press cancelled a tool. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Sketch: a double Esc takes away a blank sketch; only Esc twice in a row counts A quick double Esc on a sketch the user added nothing to removes it in one undo step, the view and tab going back as on Finish, so no empty sketch is left behind. A sketch with anything in it is still finished and kept. The double press counts only for two Esc presses in a row with no tool or dialog left after the first: any other key in between resets it, and while a tool is still out (its first Esc only ended a chain) the press puts it down as before. Tests that press Esc on its own now wait past the window. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -428,6 +428,8 @@ pub struct SolveApp {
|
||||
pub last_command: Option<(String, String)>,
|
||||
/// Esc was already acted on this frame (by the shortcuts), so later handlers skip it.
|
||||
pub esc_handled: bool,
|
||||
/// When Esc was last pressed (input time): a second press soon after finishes the sketch.
|
||||
pub last_escape: Option<f64>,
|
||||
pub status: Option<(String, f64, bool)>,
|
||||
pub quit_requested: bool,
|
||||
/// Autosave into the recovery folder (the desktop app turns it on).
|
||||
@@ -556,6 +558,7 @@ impl SolveApp {
|
||||
apply_gate: None,
|
||||
last_command: None,
|
||||
esc_handled: false,
|
||||
last_escape: None,
|
||||
status: None,
|
||||
quit_requested: false,
|
||||
autosave: None,
|
||||
|
||||
@@ -43,6 +43,18 @@ pub fn has_geometry(sketch: &solidcraft_engine::sketch::Sketch) -> bool {
|
||||
!sketch.curves.is_empty() || !sketch.wires.is_empty()
|
||||
}
|
||||
|
||||
/// Nothing the user added: no curve, 3D curve, constraint or dimension, pattern, projection,
|
||||
/// text or offset, and no point but the fixed origin.
|
||||
pub fn is_blank(sketch: &solidcraft_engine::sketch::Sketch) -> bool {
|
||||
!has_geometry(sketch)
|
||||
&& sketch.constraints.is_empty()
|
||||
&& sketch.patterns.is_empty()
|
||||
&& sketch.links.is_empty()
|
||||
&& sketch.offsets.is_empty()
|
||||
&& sketch.freeform_offsets.is_empty()
|
||||
&& sketch.points.iter().all(|p| p.id == "origin")
|
||||
}
|
||||
|
||||
/// The degrees-of-freedom line: "No geometry yet" (an empty sketch only has its fixed origin),
|
||||
/// "Fully constrained" or "N degrees of freedom".
|
||||
pub fn dof_text(dof: usize, ok: bool, has_geometry: bool) -> String {
|
||||
@@ -951,4 +963,84 @@ mod tests {
|
||||
h.settle();
|
||||
assert_ne!(line(&h), "No geometry yet", "a drawn rectangle is geometry");
|
||||
}
|
||||
|
||||
/// One Esc cancels the tool and keeps the sketch open; two quick presses finish it (the
|
||||
/// sketch is kept and listed in the timeline), also when the first press cancelled a tool.
|
||||
#[test]
|
||||
fn escape_cancels_then_a_quick_second_escape_finishes_the_sketch() {
|
||||
let apart = |h: &mut crate::scenario::Harness| h.frames(60);
|
||||
let mut h = crate::scenario::Harness::new();
|
||||
h.app.run("sketch.create", json!({"plane":"XY"})).unwrap();
|
||||
h.app.run("sketch.rectangle.two_point", json!({"p0":[0,0],"p1":[40,30]})).unwrap();
|
||||
h.settle();
|
||||
h.step(&json!({"start":"sketch.line"})).unwrap();
|
||||
assert!(h.app.tool.is_some());
|
||||
h.step(&json!({"key":"Escape"})).unwrap();
|
||||
assert!(h.app.tool.is_none(), "one Esc puts the tool down");
|
||||
apart(&mut h);
|
||||
h.step(&json!({"key":"Escape"})).unwrap();
|
||||
apart(&mut h);
|
||||
assert!(h.app.session.active_sketch.is_some(), "single presses keep the sketch open");
|
||||
|
||||
// A quick double press finishes and keeps the sketch.
|
||||
h.step(&json!({"key":"Escape"})).unwrap();
|
||||
h.step(&json!({"key":"Escape"})).unwrap();
|
||||
h.settle();
|
||||
assert!(h.app.session.active_sketch.is_none(), "a double Esc finishes the sketch");
|
||||
assert_eq!(h.app.ui.tab, "MODEL");
|
||||
assert!(crate::scenario::handle_at("timeline:Sketch1").is_some(), "the sketch is kept");
|
||||
let sketch = h.app.session.execute("sketch.inspect", &json!({"sketch": 1})).unwrap();
|
||||
assert_eq!(sketch["curves"].as_array().map(Vec::len), Some(4), "{sketch}");
|
||||
|
||||
// With a tool active: the first press cancels it, the second finishes.
|
||||
h.step(&json!({"click": {"handle": "timeline:Sketch1"}, "double": true})).unwrap();
|
||||
h.settle();
|
||||
assert!(h.app.session.active_sketch.is_some());
|
||||
apart(&mut h);
|
||||
h.step(&json!({"start":"sketch.circle.center"})).unwrap();
|
||||
assert!(h.app.tool.is_some());
|
||||
h.step(&json!({"key":"Escape"})).unwrap();
|
||||
h.step(&json!({"key":"Escape"})).unwrap();
|
||||
h.settle();
|
||||
assert!(h.app.tool.is_none() && h.app.session.active_sketch.is_none());
|
||||
assert!(crate::scenario::handle_at("timeline:Sketch1").is_some());
|
||||
// Outside a sketch a double Esc only clears, as before.
|
||||
h.step(&json!({"key":"Escape"})).unwrap();
|
||||
h.step(&json!({"key":"Escape"})).unwrap();
|
||||
assert!(h.app.session.active_sketch.is_none());
|
||||
}
|
||||
|
||||
/// A double Esc on a sketch left blank takes it away (no empty sketch in the browser or the
|
||||
/// timeline, one undo step) and puts the view back; one line is enough to keep it.
|
||||
#[test]
|
||||
fn a_quick_double_escape_discards_a_blank_sketch_and_keeps_one_with_a_line() {
|
||||
let mut h = crate::scenario::Harness::new();
|
||||
h.call("ui.view", json!({"view": "iso"}));
|
||||
h.frames(3);
|
||||
let undo = h.app.session.undo.len();
|
||||
h.step(&json!({"start":"sketch.create"})).unwrap();
|
||||
h.step(&json!({"click": {"plane": "XY"}})).unwrap();
|
||||
h.settle();
|
||||
assert!(h.app.session.active_sketch.is_some());
|
||||
h.step(&json!({"key":"Escape"})).unwrap();
|
||||
h.step(&json!({"key":"Escape"})).unwrap();
|
||||
h.settle();
|
||||
crate::scenario::check(&mut h, &json!({"sketching": false, "camera": {"yaw_deg": -45, "pitch_deg": 35}})).unwrap();
|
||||
assert_eq!(h.app.ui.tab, "MODEL");
|
||||
assert!(h.app.session.doc.features.is_empty(), "the blank sketch is gone");
|
||||
assert!(crate::scenario::handles("timeline:").iter().all(|(n, _)| !n.contains("Sketch")));
|
||||
assert!(crate::scenario::handle_at("row:Sketch1").is_none());
|
||||
assert!(h.app.session.undo.len() <= undo + 2, "creating and discarding are at most two steps");
|
||||
|
||||
h.step(&json!({"start":"sketch.create"})).unwrap();
|
||||
h.step(&json!({"click": {"plane": "XY"}})).unwrap();
|
||||
h.app.run("sketch.line", json!({"points": [[0, 0], [30, 10]]})).unwrap();
|
||||
h.settle();
|
||||
h.step(&json!({"key":"Escape"})).unwrap();
|
||||
h.step(&json!({"key":"Escape"})).unwrap();
|
||||
h.settle();
|
||||
assert!(h.app.session.active_sketch.is_none());
|
||||
assert_eq!(h.app.session.doc.features.len(), 1, "a sketch with a line is kept");
|
||||
assert!(crate::scenario::handles("timeline:").iter().any(|(n, _)| n.contains("Sketch")));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -67,9 +67,12 @@ fn a_sketch_key_with_nothing_selected_asks_for_the_plane_then_starts_the_tool()
|
||||
assert!(h.app.dialog.is_none());
|
||||
}
|
||||
|
||||
/// #1804: Escape puts down the sketch tool. Another Escape does not leave the sketch.
|
||||
/// #1804: Escape puts down the sketch tool. Another Escape, on its own, does not leave the
|
||||
/// sketch; only a quick second press does (and finishes it, keeping it).
|
||||
#[test]
|
||||
fn a_second_escape_stays_in_the_sketch() {
|
||||
// Presses further apart than the double-press window.
|
||||
let pause = |h: &mut Harness| h.frames((crate::toolbar::ESCAPE_TWICE_S * 60.0) as usize + 6);
|
||||
let mut h = Harness::new();
|
||||
h.app.run("sketch.create", json!({"plane":"XY"})).unwrap();
|
||||
let sketch = h.app.session.active_sketch;
|
||||
@@ -79,15 +82,21 @@ fn a_second_escape_stays_in_the_sketch() {
|
||||
press(&mut h, "Escape");
|
||||
assert!(tool(&h).is_none(), "the first Escape puts the tool down");
|
||||
assert_eq!(h.app.session.active_sketch, sketch);
|
||||
pause(&mut h);
|
||||
press(&mut h, "Escape");
|
||||
assert_eq!(h.app.session.active_sketch, sketch, "a second Escape stays in the sketch");
|
||||
h.app.run("sketch.line", json!({"points":[[0,0],[10,0]]})).unwrap();
|
||||
h.app.session.selection = vec![Sel::SketchCurve { id: "l1".into() }];
|
||||
pause(&mut h);
|
||||
press(&mut h, "Escape");
|
||||
assert!(h.app.session.selection.is_empty(), "Escape still clears a selection");
|
||||
assert_eq!(h.app.session.active_sketch, sketch);
|
||||
pause(&mut h);
|
||||
press(&mut h, "Escape");
|
||||
assert_eq!(h.app.session.active_sketch, sketch, "Escape after clearing the selection stays in the sketch");
|
||||
press(&mut h, "Escape");
|
||||
assert!(h.app.session.active_sketch.is_none(), "a quick second Escape finishes the sketch");
|
||||
assert_eq!(h.app.session.doc.features.len(), 1, "and keeps it");
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -1783,6 +1783,9 @@ 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;
|
||||
|
||||
/// Global keyboard shortcuts (when no text field has focus).
|
||||
pub fn shortcuts(app: &mut SolveApp, ctx: &egui::Context) {
|
||||
if app.file_load.busy() {
|
||||
@@ -1811,6 +1814,10 @@ pub fn shortcuts(app: &mut SolveApp, ctx: &egui::Context) {
|
||||
}
|
||||
for k in keys {
|
||||
use egui::Key;
|
||||
// Only Esc pressed twice in a row finishes a sketch: any other key in between resets it.
|
||||
if k != Key::Escape {
|
||||
app.last_escape = None;
|
||||
}
|
||||
if shown && crate::navigation::view_key(app, ctx, k, mods) {
|
||||
continue;
|
||||
}
|
||||
@@ -1841,6 +1848,29 @@ pub fn shortcuts(app: &mut SolveApp, ctx: &egui::Context) {
|
||||
match k {
|
||||
Key::Escape => {
|
||||
app.esc_handled = true;
|
||||
// Two quick presses in a sketch finish it (keeping it, like Finish Sketch);
|
||||
// the first one has already cancelled whatever was active.
|
||||
let now = ctx.input(|i| i.time);
|
||||
let again = app.last_escape.is_some_and(|t| now - t <= ESCAPE_TWICE_S);
|
||||
app.last_escape = Some(now);
|
||||
// Not while a tool is still out (its first Esc only ended a chain): this press
|
||||
// puts it down, and the next quick one finishes.
|
||||
if again
|
||||
&& app.tool.is_none()
|
||||
&& app.dialog.is_none()
|
||||
&& let Some(sketch) = app.session.active_sketch
|
||||
{
|
||||
app.last_escape = None;
|
||||
// A sketch left blank is taken away instead, in one undo step, so no empty
|
||||
// sketch stays behind.
|
||||
let blank = app.session.doc.sketch(sketch).is_ok_and(crate::sketch_palette::is_blank);
|
||||
let depth = app.session.undo.len();
|
||||
app.finish_sketch();
|
||||
if blank && app.session.active_sketch.is_none() && app.run("timeline.delete", json!({ "feature": sketch.to_string() })).is_ok() {
|
||||
app.session.undo.truncate(depth + 1);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if crate::sketch_text_move::cancel_drag(app) {
|
||||
continue;
|
||||
}
|
||||
@@ -1854,8 +1884,8 @@ pub fn shortcuts(app: &mut SolveApp, ctx: &egui::Context) {
|
||||
app.tree.picked_joints.clear();
|
||||
let _ = app.run("select.clear", json!({}));
|
||||
}
|
||||
// An open sketch with nothing left to cancel stays open (#1804). Finish Sketch
|
||||
// is what leaves it, so a second Escape after putting a tool down is harmless.
|
||||
// A single Esc with nothing left to cancel keeps the sketch open (#1804); a quick
|
||||
// second press finishes it (above).
|
||||
}
|
||||
Key::S if !mods.shift && !mods.alt => {
|
||||
app.start("ui.palette");
|
||||
|
||||
Reference in New Issue
Block a user