Skip to content

LightCraft: An open-source non-destructive photo editor written by scratch in pure Rust

LR-CROP-STRAIGHTEN: Esc in the Angle field keeps the old angle

Nathaniel Ledfordd92ec902c89a
diff --git a/crates/ui-egui/src/panels/right.rs b/crates/ui-egui/src/panels/right.rs
index 91d7f181d..8e7da9ef1 100644
--- a/crates/ui-egui/src/panels/right.rs
+++ b/crates/ui-egui/src/panels/right.rs
@@ -190,6 +190,7 @@ fn crop(app: &mut LightcraftApp, ui: &mut egui::Ui, id: PhotoId) {
ui.horizontal(|ui| {
ui.label(crate::i18n::tr("Angle"));
let mut angle = d.crop.geometry.angle;
+ let escape = ui.input(|i| i.key_pressed(egui::Key::Escape));
let r = ui.add(
egui::DragValue::new(&mut angle)
.range(spec.min..=spec.max)
@@ -208,7 +209,14 @@ fn crop(app: &mut LightcraftApp, ui: &mut egui::Ui, id: PhotoId) {
}
// egui reports a change while text is typed even when the value isn't updated yet:
// apply only a new angle
- if r.changed() && angle.is_finite() && angle != d.crop.geometry.angle {
+ // Esc keeps the old angle, but egui applies the typed text on the frame after the
+ // one where Esc took the focus away: remember the Esc for that frame
+ let cancel = egui::Id::new("cropAngleFieldEscaped");
+ let escaped = ui.data_mut(|m| m.remove_temp::<bool>(cancel)).unwrap_or(false);
+ if escape && r.lost_focus() {
+ ui.data_mut(|m| m.insert_temp(cancel, true));
+ }
+ if r.changed() && !escaped && angle.is_finite() && angle != d.crop.geometry.angle {
let _ = app.run("crop.straighten", json!({"angle": (angle.clamp(spec.min, spec.max) * 100.0).round() / 100.0}));
}
if r.drag_stopped() {
diff --git a/crates/ui-egui/src/tests_crop_rotate.rs b/crates/ui-egui/src/tests_crop_rotate.rs
index 75a74bfc4..b7dfc07cf 100644
--- a/crates/ui-egui/src/tests_crop_rotate.rs
+++ b/crates/ui-egui/src/tests_crop_rotate.rs
@@ -239,3 +239,47 @@ fn placing_the_readout_never_panics() {
let r = readout_rect(egui::pos2(f32::NAN, 5.0), size, egui::Rect::from_min_size(egui::pos2(0.0, 0.0), egui::vec2(100.0, 100.0)));
assert_eq!(r.size(), size);
}
+
+/// Esc in the Angle field gives up the typed angle and keeps the old one, as in the slider's value.
+#[test]
+fn escape_in_the_angle_field_keeps_the_angle() {
+ let mut h = crop_tool();
+ let r = h.request("ui.clickWidget", json!({"id": "cropAngleField"}), T);
+ assert_eq!(r["ok"], true, "{r}");
+ h.settle(SETTLE);
+ for key in ["a", "Backspace"] {
+ let r = h.request("ui.key", json!({"key": key, "cmd": key == "a"}), T);
+ assert_eq!(r["ok"], true, "{r}");
+ }
+ let r = h.request("ui.text", json!({"text": "7"}), T);
+ assert_eq!(r["ok"], true, "{r}");
+ h.settle(SETTLE);
+ let (before, undo) = (angle(&h), h.app.session.undo.len());
+ let r = h.request("ui.key", json!({"key": "Escape"}), T);
+ assert_eq!(r["ok"], true, "{r}");
+ h.settle(SETTLE);
+ assert_eq!((angle(&h), h.app.session.undo.len()), (before, undo), "Esc applied nothing");
+ assert_eq!(h.app.ui.right, crate::state::RightPanel::Crop, "and only left the field");
+}
+
+/// Leaving the Angle field another way than Esc (Tab here) applies the typed angle, as Return does.
+#[test]
+fn leaving_the_angle_field_applies_the_angle() {
+ let mut h = crop_tool();
+ let r = h.request("ui.clickWidget", json!({"id": "cropAngleField"}), T);
+ assert_eq!(r["ok"], true, "{r}");
+ h.settle(SETTLE);
+ for key in ["a", "Backspace"] {
+ let r = h.request("ui.key", json!({"key": key, "cmd": key == "a"}), T);
+ assert_eq!(r["ok"], true, "{r}");
+ }
+ let r = h.request("ui.text", json!({"text": "4.5"}), T);
+ assert_eq!(r["ok"], true, "{r}");
+ h.settle(SETTLE);
+ let undo = h.app.session.undo.len();
+ let r = h.request("ui.key", json!({"key": "Tab"}), T);
+ assert_eq!(r["ok"], true, "{r}");
+ h.settle(SETTLE);
+ assert!((angle(&h) - 4.5).abs() < 1e-9, "applied on leaving: {}", angle(&h));
+ assert_eq!(h.app.session.undo.len(), undo + 1);
+}