Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

Merge PR #7894: Slice Select: Delete deletes the selected slice, not the layer (#7862)

aiongg8d989c457199
diff --cc crates/ui-egui/src/slice_ui.rs
index 32945c81b,7847ef28e..78e19f4be
--- a/crates/ui-egui/src/slice_ui.rs
+++ b/crates/ui-egui/src/slice_ui.rs
@@@ -409,100 -356,60 +442,154 @@@ mod tests
assert!(doc.slices.list.iter().any(|s| s.name == "footer"));
}
+ /// #7865: with View › Lock Slices on, Slice Select doesn't select (or move) a slice, and the
+ /// Slice tool doesn't start one; both say why, as Photoshop's error does.
+ #[test]
+ fn locked_slices_cannot_be_selected() {
+ let mut app = app();
+ let m = egui::Modifiers::NONE;
+ app.ui.tool = Tool::Slice;
+ pointer(&mut app, ToolEvent::Down { x: 10.0, y: 10.0, pressure: 1.0 }, m);
+ pointer(&mut app, ToolEvent::Up { x: 40.0, y: 30.0 }, m);
+ let rect = app.session.active().unwrap().doc.slices.list[0].rect;
+ app.ui.slices.selected = None;
+ app.run("view.lockSlices", json!({"on": true})).unwrap();
+ app.ui.tool = Tool::SliceSelect;
+ pointer(&mut app, ToolEvent::Down { x: 20.0, y: 20.0, pressure: 1.0 }, m);
+ pointer(&mut app, ToolEvent::Move { x: 30.0, y: 30.0, pressure: 1.0 }, m);
+ pointer(&mut app, ToolEvent::Up { x: 30.0, y: 30.0 }, m);
+ assert_eq!(app.ui.slices.selected, None, "not selected");
+ assert_eq!(app.session.active().unwrap().doc.slices.list[0].rect, rect, "not moved");
+ assert!(app.ui.status_error && app.ui.status.contains("locked"), "{}", app.ui.status);
+ // The Slice tool says so too.
+ app.ui.status_error = false;
+ app.ui.tool = Tool::Slice;
+ pointer(&mut app, ToolEvent::Down { x: 50.0, y: 50.0, pressure: 1.0 }, m);
+ pointer(&mut app, ToolEvent::Up { x: 80.0, y: 70.0 }, m);
+ assert_eq!(app.session.active().unwrap().doc.slices.list.len(), 1);
+ assert!(app.ui.status_error && app.ui.status.contains("locked"), "{}", app.ui.status);
+ // Unlocked again, Slice Select selects it.
+ app.run("view.lockSlices", json!({"on": false})).unwrap();
+ app.ui.tool = Tool::SliceSelect;
+ pointer(&mut app, ToolEvent::Down { x: 20.0, y: 20.0, pressure: 1.0 }, m);
+ pointer(&mut app, ToolEvent::Up { x: 20.0, y: 20.0 }, m);
+ assert!(app.ui.slices.selected.is_some());
+ }
+
+ #[test]
+ fn dragging_a_piece_moves_the_whole_covered_slice() {
+ // #7770: the slice under a later one is drawn as pieces; dragging one moves all of it.
+ let mut app = app();
+ app.run("slice.new", json!({"rect": [0, 0, 60, 60]})).unwrap();
+ app.run("slice.new", json!({"rect": [40, 40, 20, 20]})).unwrap();
+ app.ui.tool = Tool::SliceSelect;
+ let m = egui::Modifiers::NONE;
+ pointer(&mut app, ToolEvent::Down { x: 10.0, y: 50.0, pressure: 1.0 }, m);
+ pointer(&mut app, ToolEvent::Up { x: 15.0, y: 50.0 }, m);
+ let doc = app.session.active().unwrap().doc.clone();
+ assert_eq!(doc.slices.list[0].rect, photocraft_geom::Rect::new(5, 0, 65, 60));
+ }
+
+ #[test]
+ fn handles_resize_the_edges_they_hang_off() {
+ let r = photocraft_geom::Rect::new(10, 10, 40, 30);
+ assert_eq!(handle_at(r, [10.5, 9.0], 2.0), Some([-1, -1]));
+ assert_eq!(handle_at(r, [25.0, 30.0], 2.0), Some([0, 1]));
+ assert_eq!(handle_at(r, [40.0, 20.0], 2.0), Some([1, 0]));
+ assert_eq!(handle_at(r, [25.0, 20.0], 2.0), None, "the middle is no handle");
+ assert_eq!(resized(r, [1, 0], 5, 9), photocraft_geom::Rect::new(10, 10, 45, 30), "the right edge only");
+ assert_eq!(resized(r, [-1, 1], -4, 6), photocraft_geom::Rect::new(6, 10, 40, 36));
+ assert_eq!(resized(r, [-1, 0], 50, 0), photocraft_geom::Rect::new(40, 10, 60, 30), "dragged past the other edge");
+ assert_eq!(resized(r, [1, 0], -30, 0), photocraft_geom::Rect::new(10, 10, 11, 30), "never empty");
+ }
+
+ /// #7863: with Slice Select, dragging a handle of the selected slice resizes it; the middle
+ /// still moves it, and locked slices keep their size.
+ #[test]
+ fn slice_select_handles_resize_the_slice() {
+ let mut app = app();
+ let m = egui::Modifiers::NONE;
+ app.ui.tool = Tool::Slice;
+ pointer(&mut app, ToolEvent::Down { x: 10.0, y: 10.0, pressure: 1.0 }, m);
+ pointer(&mut app, ToolEvent::Up { x: 40.0, y: 30.0 }, m);
+ app.ui.tool = Tool::SliceSelect;
+ let rect = |app: &PhotocraftApp| app.session.active().unwrap().doc.slices.list[0].rect;
+ // The bottom-right corner.
+ pointer(&mut app, ToolEvent::Down { x: 40.0, y: 30.0, pressure: 1.0 }, m);
+ assert!(matches!(app.ui.slices.drag, Some(SliceDrag::Resize { handle: [1, 1], .. })));
+ pointer(&mut app, ToolEvent::Move { x: 50.0, y: 45.0, pressure: 1.0 }, m);
+ pointer(&mut app, ToolEvent::Up { x: 50.0, y: 45.0 }, m);
+ assert_eq!(rect(&app), photocraft_geom::Rect::new(10, 10, 50, 45));
+ assert!(app.ui.slices.selected.is_some(), "still selected");
+ // The left edge's middle.
+ pointer(&mut app, ToolEvent::Down { x: 10.0, y: 27.0, pressure: 1.0 }, m);
+ pointer(&mut app, ToolEvent::Up { x: 4.0, y: 60.0 }, m);
+ assert_eq!(rect(&app), photocraft_geom::Rect::new(4, 10, 50, 45), "only the left edge moves");
+ // Inside, away from the handles, it still moves.
+ pointer(&mut app, ToolEvent::Down { x: 30.0, y: 20.0, pressure: 1.0 }, m);
+ pointer(&mut app, ToolEvent::Up { x: 32.0, y: 21.0 }, m);
+ assert_eq!(rect(&app), photocraft_geom::Rect::new(6, 11, 52, 46));
+ // Locked: the handle doesn't resize.
+ app.run("view.lockSlices", json!({"on": true})).unwrap();
+ pointer(&mut app, ToolEvent::Down { x: 52.0, y: 46.0, pressure: 1.0 }, m);
+ pointer(&mut app, ToolEvent::Up { x: 70.0, y: 70.0 }, m);
+ assert_eq!(rect(&app), photocraft_geom::Rect::new(6, 11, 52, 46));
+ }
+
+ /// Delete through the app's shortcut handling, as the canvas gets it.
+ fn press(app: &mut PhotocraftApp, key: egui::Key) {
+ let ctx = egui::Context::default();
+ let m = egui::Modifiers::NONE;
+ let raw = egui::RawInput {
+ events: vec![egui::Event::ModifiersChanged(m), egui::Event::Key { key, physical_key: None, pressed: true, repeat: false, modifiers: m }],
+ ..Default::default()
+ };
+ ctx.begin_pass(raw);
+ crate::shortcuts::handle(app, &ctx);
+ ctx.end_pass().textures_delta.clear();
+ }
+
+ /// #7862: with Slice Select, Delete (or Backspace) deletes the selected slice, never the
+ /// layer: in a one-layer document that used to fail with "a document must keep at least one
+ /// layer". Locked slices refuse; an auto slice has nothing to delete.
+ #[test]
+ fn delete_deletes_the_selected_slice_not_the_layer() {
+ let m = egui::Modifiers::NONE;
+ for key in [egui::Key::Delete, egui::Key::Backspace] {
+ let mut app = app();
+ let layers = app.session.active().unwrap().doc.layers.len();
+ app.ui.tool = Tool::Slice;
+ pointer(&mut app, ToolEvent::Down { x: 10.0, y: 10.0, pressure: 1.0 }, m);
+ pointer(&mut app, ToolEvent::Up { x: 40.0, y: 30.0 }, m);
+ app.ui.tool = Tool::SliceSelect;
+ pointer(&mut app, ToolEvent::Down { x: 20.0, y: 20.0, pressure: 1.0 }, m);
+ pointer(&mut app, ToolEvent::Up { x: 20.0, y: 20.0 }, m);
+ assert!(app.ui.slices.selected.is_some());
+
+ // Locked: the slice stays, and says why.
+ app.run("view.lockSlices", json!({"on": true})).unwrap();
+ press(&mut app, key);
+ assert_eq!(app.session.active().unwrap().doc.slices.list.len(), 1, "{key:?}: locked");
+ assert!(app.ui.status_error && app.ui.status.contains("locked"), "{key:?}: {}", app.ui.status);
+ app.run("view.lockSlices", json!({"on": false})).unwrap();
+ app.ui.status_error = false;
+
+ press(&mut app, key);
+ let doc = app.session.active().unwrap().doc.clone();
+ assert!(doc.slices.list.is_empty(), "{key:?}: the slice is deleted");
+ assert_eq!(doc.layers.len(), layers, "{key:?}: the layer stays");
+ assert!(!app.ui.status_error, "{key:?}: {}", app.ui.status);
+ assert_eq!(app.ui.slices.selected, None);
+
+ // An auto slice: nothing to delete, and still not the layer.
+ pointer(&mut app, ToolEvent::Down { x: 90.0, y: 75.0, pressure: 1.0 }, m);
+ pointer(&mut app, ToolEvent::Up { x: 90.0, y: 75.0 }, m);
+ press(&mut app, key);
+ assert_eq!(app.session.active().unwrap().doc.layers.len(), layers, "{key:?}: auto slice");
+ assert!(!app.ui.status_error, "{key:?}: {}", app.ui.status);
+ }
+ }
+
#[test]
fn locked_slices_ignore_gestures() {
let mut app = app();