Skip to content

Commit a05877f

Browse files
kinowerclaude
andcommitted
Finish a crop gesture wherever it can be cut short; tighten crop checks
- Session::exit_crop() is the one way out of crop mode (Enter/Escape, the toggle, a click elsewhere, a selection that no longer fits): it finishes a crop drag still in progress as its own "Crop" undo step first. - set_tool and Undo/Redo also finish a crop drag first, so later pointer events can't carry it on and Undo takes back exactly that gesture. - Crop drags store crop fractions clamped as the renderer draws them. - picture.crop with keepFrame rejects a crop that leaves nothing showing. - picture.cropMode is enabled only with exactly one picture selected. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent a9f2598 commit a05877f

6 files changed

Lines changed: 86 additions & 9 deletions

File tree

‎crates/engine/src/cmd/crop.rs‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ pub fn specs() -> Vec<CommandSpec> {
2727
["Picture Format", "Size", "Crop"],
2828
None,
2929
"{on?: bool (default: toggle), id?} — show the crop handles on one selected picture: drag them to crop, drag the picture to move it inside, its round handles to scale it; Enter, Escape or a click elsewhere ends",
30-
has_picture,
30+
has_one_picture,
3131
crop_mode
3232
),
3333
cmd!(
@@ -57,6 +57,13 @@ pub fn has_picture(s: &Session) -> std::result::Result<(), String> {
5757
if any { Ok(()) } else { Err("select a picture".into()) }
5858
}
5959

60+
/// Crop mode works on one picture: with anything else selected too it is off.
61+
pub fn has_one_picture(s: &Session) -> std::result::Result<(), String> {
62+
has_selection(s)?;
63+
let one = s.active().is_some_and(|d| matches!(d.selected_shapes().as_slice(), [sh] if matches!(sh.kind, ShapeKind::Picture { .. })));
64+
if one { Ok(()) } else { Err("select one picture".into()) }
65+
}
66+
6067
/// The whole image in the box's local points (`w`×`h` box, `crop` = left, top, right, bottom).
6168
pub fn image_rect(w: f64, h: f64, crop: [f64; 4]) -> Option<Rect> {
6269
let [l, t, r, b] = crop;
@@ -197,7 +204,7 @@ fn crop_target(s: &Session, p: &Value, cmd: &str) -> Result<ShapeId> {
197204
fn crop_mode(s: &mut Session, p: &Value) -> Result<Value> {
198205
let on = p.get("on").and_then(Value::as_bool).unwrap_or(s.tool.crop.is_none());
199206
if !on {
200-
s.tool.crop = None;
207+
s.exit_crop();
201208
return Ok(json!({"cropping": Value::Null}));
202209
}
203210
let id = crop_target(s, p, "picture.cropMode")?;

‎crates/engine/src/cmd/edit.rs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ pub fn specs() -> Vec<CommandSpec> {
4444
}
4545

4646
fn restore(s: &mut Session, redo: bool) -> Result<Value> {
47+
// A crop gesture in progress becomes its own step first, so Undo takes exactly that back.
48+
s.end_crop_drag();
4749
let st = s.doc_mut()?;
4850
let (from, to) = if redo { (&mut st.history.redo, &mut st.history.undo) } else { (&mut st.history.undo, &mut st.history.redo) };
4951
let Some(e) = from.pop() else { return ok() };

‎crates/engine/src/cmd/shape.rs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1063,6 +1063,10 @@ fn crop(s: &mut Session, p: &Value) -> Result<Value> {
10631063
*slot = v.clamp(-10.0, 0.95);
10641064
}
10651065
}
1066+
// Keep at least a sliver showing, as without keepFrame.
1067+
if fill.crop[0] + fill.crop[2] > 0.99 || fill.crop[1] + fill.crop[3] > 0.99 {
1068+
return Err(bad("picture.crop", "the crop leaves nothing of the picture"));
1069+
}
10661070
}
10671071
Ok(())
10681072
})

‎crates/engine/src/keys.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ impl Session {
5959
}
6060
self.settle_crop();
6161
if self.tool.crop.is_some() && matches!(k, "enter" | "return" | "escape") {
62-
self.tool.crop = None;
62+
self.exit_crop();
6363
return Ok(json!({"handled": true, "command": "picture.cropMode"}));
6464
}
6565
if self.tool.edit_points.is_some() && matches!(k, "enter" | "return" | "escape") {

‎crates/engine/src/tests.rs‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4296,6 +4296,61 @@ fn a_crop_drag_ends_when_closing_another_presentation_changes_the_active_one() {
42964296
assert!(near(crop_state(&s, id).1, [0.0, 0.0, 0.375, 0.0]));
42974297
}
42984298

4299+
#[test]
4300+
fn every_way_out_of_a_crop_gesture_leaves_it_as_one_undo_step() {
4301+
let mut s = session();
4302+
let rect = s.execute("shape.insert", &json!({"preset": "rect", "rect": [400, 300, 50, 50]})).unwrap()["id"].as_u64().unwrap();
4303+
let id = cropped_picture(&mut s);
4304+
let ev = |kind, x, y| PointerEvent { kind, x, y, mods: Mods::default(), tol: 3.0, pressure: None };
4305+
let undos = |s: &Session| s.doc().unwrap().history.undo.len();
4306+
let ended = |s: &Session| !s.tool.dragging() && s.doc().unwrap().interaction.is_none();
4307+
// Selecting another shape mid-gesture: crop mode ends and the gesture is its own step.
4308+
s.execute("picture.cropMode", &json!({"id": id})).unwrap();
4309+
let n = undos(&s);
4310+
s.pointer(ev(PointerKind::Down, 300.0, 175.0)).unwrap();
4311+
s.pointer(ev(PointerKind::Drag, 250.0, 175.0)).unwrap();
4312+
s.execute("edit.select", &json!({"ids": [rect]})).unwrap();
4313+
assert!(ended(&s) && s.tool.crop.is_none());
4314+
assert_eq!(undos(&s), n + 1);
4315+
s.pointer(ev(PointerKind::Drag, 200.0, 175.0)).unwrap();
4316+
s.pointer(ev(PointerKind::Up, 200.0, 175.0)).unwrap();
4317+
assert!(near(crop_state(&s, id).1, [0.0, 0.0, 0.25, 0.0]), "later events change nothing");
4318+
assert_eq!(undos(&s), n + 1);
4319+
// Another tool mid-gesture: the same, and crop mode stays on.
4320+
s.execute("edit.select", &json!({"ids": [id]})).unwrap();
4321+
s.execute("picture.cropMode", &json!({})).unwrap();
4322+
s.pointer(ev(PointerKind::Down, 250.0, 175.0)).unwrap();
4323+
s.pointer(ev(PointerKind::Drag, 225.0, 175.0)).unwrap();
4324+
s.set_tool(ToolKind::Select);
4325+
assert!(ended(&s));
4326+
assert_eq!(undos(&s), n + 2);
4327+
assert!(near(crop_state(&s, id).1, [0.0, 0.0, 0.375, 0.0]));
4328+
// Undo mid-gesture takes back exactly that gesture; Redo brings it back.
4329+
s.pointer(ev(PointerKind::Down, 225.0, 175.0)).unwrap();
4330+
s.pointer(ev(PointerKind::Drag, 200.0, 175.0)).unwrap();
4331+
s.execute("edit.undo", &json!({})).unwrap();
4332+
assert!(ended(&s));
4333+
assert_eq!(undos(&s), n + 2);
4334+
assert!(near(crop_state(&s, id).1, [0.0, 0.0, 0.375, 0.0]));
4335+
s.pointer(ev(PointerKind::Drag, 150.0, 175.0)).unwrap();
4336+
s.pointer(ev(PointerKind::Up, 150.0, 175.0)).unwrap();
4337+
assert!(near(crop_state(&s, id).1, [0.0, 0.0, 0.375, 0.0]));
4338+
s.execute("edit.redo", &json!({})).unwrap();
4339+
assert!(near(crop_state(&s, id).1, [0.0, 0.0, 0.5, 0.0]));
4340+
// Panning the picture off the box stores the crop the renderer draws.
4341+
assert_eq!(s.tool.crop, Some(deckcraft_model::ShapeId(id as _)));
4342+
s.pointer(ev(PointerKind::Down, 150.0, 175.0)).unwrap();
4343+
s.pointer(ev(PointerKind::Drag, 550.0, 175.0)).unwrap();
4344+
s.pointer(ev(PointerKind::Up, 550.0, 175.0)).unwrap();
4345+
let c = crop_state(&s, id).1;
4346+
assert!(c.iter().all(|v| (-10.0..=0.99).contains(v)) && c[2] == 0.99, "{c:?}");
4347+
// keepFrame can't crop the whole picture away either.
4348+
assert!(s.execute("picture.crop", &json!({"ids": [id], "left": 0.6, "right": 0.6, "keepFrame": true})).is_err());
4349+
// Crop mode needs exactly one picture selected.
4350+
s.execute("edit.select", &json!({"ids": [id, rect]})).unwrap();
4351+
assert!(s.execute("picture.cropMode", &json!({})).is_err());
4352+
}
4353+
42994354
#[test]
43004355
fn crop_aspect_fill_and_fit() {
43014356
let mut s = session();

‎crates/engine/src/tools.rs‎

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -400,13 +400,20 @@ impl Session {
400400
/// same picture keeps crop mode on.
401401
pub(crate) fn settle_crop(&mut self) {
402402
if self.tool.crop.is_some() && self.active().is_none_or(|st| self.tool.cropping(st).is_none()) {
403-
self.tool.crop = None;
403+
self.exit_crop();
404404
}
405405
}
406406

407-
/// Finish a crop gesture in the presentation it began in, as releasing the pointer does,
408-
/// before another presentation becomes active or this one closes: one undo step there, and
409-
/// later pointer events can't carry it into another presentation.
407+
/// Leave crop mode. A crop gesture still in progress is finished first, as its own undo step.
408+
/// Every way out of crop mode comes through here.
409+
pub(crate) fn exit_crop(&mut self) {
410+
self.end_crop_drag();
411+
self.tool.crop = None;
412+
}
413+
414+
/// Finish a crop gesture in the presentation it began in, as releasing the pointer does:
415+
/// one undo step there, and later pointer events can't carry it on. Called wherever the
416+
/// gesture can be cut short (leaving crop mode, Undo/Redo, another tool, another presentation).
410417
pub(crate) fn end_crop_drag(&mut self) {
411418
if matches!(self.tool.drag, Some(Drag::Crop { .. })) {
412419
self.tool.drag = None;
@@ -415,6 +422,7 @@ impl Session {
415422
}
416423

417424
pub fn set_tool(&mut self, kind: ToolKind) {
425+
self.end_crop_drag();
418426
self.tool.kind = kind;
419427
self.tool.drag = None;
420428
self.tool.preview = None;
@@ -671,7 +679,7 @@ impl Session {
671679
self.tool.drag = Some(Drag::Crop { id: cid, part, orig, img, start: p });
672680
return Ok(json!({"hit": serde_json::to_value(&hit).unwrap_or_default()}));
673681
}
674-
self.tool.crop = None;
682+
self.exit_crop();
675683
}
676684
// Edit Points: drag a vertex, Ctrl-click one to delete it or the outline to add one;
677685
// a click anywhere else leaves Edit Points.
@@ -904,7 +912,8 @@ impl Session {
904912
(orig, cmd::crop::crop_of(orig.w, orig.h, img + d))
905913
}
906914
};
907-
if let Some(c) = nc {
915+
// Stored as the renderer draws it, so the next gesture starts from what is shown.
916+
if let Some(c) = nc.map(|c| c.map(|v| v.clamp(-10.0, 0.99))) {
908917
cmd::crop::store(self, "crop", &[(id, Some(nx), c)])?;
909918
}
910919
}

0 commit comments

Comments
 (0)