Skip to content

DesignCraft: An open-source page and layout design editor written by scratch in pure Rust

Escape cancels a value typed over an automatic one

moppeeerbabb86c3ae92
diff --git a/crates/ui-egui/src/chrome.rs b/crates/ui-egui/src/chrome.rs
index cd937bc47..851808a25 100644
--- a/crates/ui-egui/src/chrome.rs
+++ b/crates/ui-egui/src/chrome.rs
@@ -1383,6 +1383,18 @@ mod tests {
let st = h.state().app.session.doc().unwrap();
let sid = designcraft_doc::StoryId(f["story"].as_u64().unwrap());
assert_eq!(st.doc.story(sid).unwrap().format_after(0).over.leading, Some(designcraft_doc::Leading::Points(87.6)));
+ // Escape cancels: the same typing, then Escape, leaves the leading automatic.
+ h.state_mut().app.run("type.char", serde_json::json!({"attrs": {"leading": {"kind": "auto"}}})).unwrap();
+ h.run_steps(3);
+ let auto = h.state().app.session.doc().unwrap().doc.story(sid).unwrap().format_after(0).over.leading;
+ let lead = field(&h, "(87.6 pt)");
+ press(&mut h, lead.center());
+ h.run_steps(2);
+ h.event(egui::Event::Key { key: egui::Key::A, physical_key: None, pressed: true, repeat: false, modifiers: egui::Modifiers::COMMAND });
+ h.event(egui::Event::Text("87.6".into()));
+ h.key_press(egui::Key::Escape);
+ h.run_steps(3);
+ assert_eq!(h.state().app.session.doc().unwrap().doc.story(sid).unwrap().format_after(0).over.leading, auto, "Escape leaves it automatic");
}
#[test]
diff --git a/crates/ui-egui/src/widgets.rs b/crates/ui-egui/src/widgets.rs
index 0d6596114..a6e0aa64f 100644
--- a/crates/ui-egui/src/widgets.rs
+++ b/crates/ui-egui/src/widgets.rs
@@ -228,8 +228,9 @@ impl<'a> NumField<'a> {
}
if r.lost_focus() {
// An automatic value (in parentheses) typed over commits even when it's the same
- // number: typing "14.4 pt" over "(14.4 pt)" makes it a fixed value.
- let typed_over_auto = self.parens && !buf.trim_start().starts_with('(');
+ // number: typing "14.4 pt" over "(14.4 pt)" makes it a fixed value. Escape cancels it.
+ let escaped = ui.input(|i| i.key_pressed(egui::Key::Escape));
+ let typed_over_auto = self.parens && !escaped && !buf.trim_start().starts_with('(');
if let Some(v) = self.parse(&buf).map(clamp)
&& (typed_over_auto || self.value.is_none_or(|old| (old - v).abs() > 1e-9))
{