PhotoCraft: An open-source, photo editor written by scratch in pure Rust
fix(move): no trail when dragging a shape past the canvas edge (#3946) (#4069)
Alon9d37e1518bdf
diff --git a/crates/ui-egui/src/move_ui.rs b/crates/ui-egui/src/move_ui.rsindex 5c2f2e6c6..fb49b0247 100644--- a/crates/ui-egui/src/move_ui.rs+++ b/crates/ui-egui/src/move_ui.rs@@ -127,6 +127,7 @@ pub(crate) fn display_doc(app: &mut PhotocraftApp, idx: usize) -> Option<(Arc<Dolet bounds = ids.iter().try_fold(Rect::EMPTY, |acc, id| Some(acc.union(&photocraft_compose::change_bounds(doc.layer(*id)?, all)?)));let canvas = doc.bounds();let moving = with_descendants(&doc, &ids);+ let bounds = bounds.map(|b| moving.iter().filter_map(|id| shape_reach(doc.layer(*id)?, canvas)).fold(b, |acc, r| acc.union(&r)));app.move_preview = Some(MovePreview { doc: doc_id, revision, ids, moving, bounds, canvas, offsets: Vec::new(), shown: None, floating: false });}let p = app.move_preview.as_mut()?;@@ -153,6 +154,15 @@ pub(crate) fn display_doc(app: &mut PhotocraftApp, idx: usize) -> Option<(Arc<DoSome((p.shown.clone()?, p.key()))}+/// Where shape layer `l` paints, effects included, not cut at the canvas as its rendered pixels+/// are: a drag brings the rest of it into view (#3946). `None` for other layers.+fn shape_reach(l: &photocraft_doc::Layer, canvas: Rect) -> Option<Rect> {+ let photocraft_doc::LayerContent::Shape(sh) = &l.content else { return None };+ let b = photocraft_vector::CompiledShape::new(sh, photocraft_vector::DEFAULT_TOLERANCE, canvas).bounds()?;+ let m = if photocraft_compose::effects::has_effects(l) { photocraft_compose::effects::margin(l) } else { 0 };+ Some(b.inflate(m))+}+/// `ids` and every layer inside them (groups move their contents).fn with_descendants(doc: &Document, ids: &[LayerId]) -> Vec<LayerId> {let walk = doc.walk();@@ -299,6 +309,37 @@ mod tests {assert_eq!(r, Rect::new(0, 0, 64, 64));}+ /// A shape reaching past the canvas edge renders cut at the canvas, but its path goes on: a+ /// drag frame must redraw where the rest of it was, or the cut edge stays behind (#3946).+ #[test]+ fn dragging_a_shape_past_the_canvas_edge_leaves_no_trail() {+ let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());+ app.session.execute("file.new", json!({"width": 64, "height": 64, "background": "transparent"})).unwrap();+ app.sync_views();+ let id = app.session.execute("shape.create", json!({"kind": "rect", "rect": [40, 16, 50, 32], "fill": "#ff0000"})).unwrap()["layer"].as_u64().unwrap();+ let id = photocraft_doc::LayerId(id);+ app.ui.tool = Tool::Move;+ app.ui.extras.snap = false;+ app.ui.view.show.smart_guides = false;+ let m = egui::Modifiers::NONE;+ crate::canvas::tool_event(&mut app, ToolEvent::Down { x: 50.0, y: 32.0, pressure: 1.0 }, m);+ let mut frames = Vec::new();+ for x in [40.0, 10.0] {+ crate::canvas::tool_event(&mut app, ToolEvent::Move { x, y: 32.0, pressure: 1.0 }, m);+ frames.push(super::display_doc(&mut app, 0).unwrap());+ }+ let st = app.session.active().unwrap();+ let r = super::damage(&app, st.doc.id, st.revision, frames[0].1, frames[1].1).unwrap();+ let alpha = |d: &photocraft_doc::Document, x, y| d.layer(id).and_then(|l| l.surface()).map_or(0.0, |s| s.pixel(x, y)[3]);+ for y in 0..64 {+ for x in 0..64 {+ if alpha(&frames[0].0, x, y) != alpha(&frames[1].0, x, y) {+ assert!(r.contains(x, y), "({x}, {y}) changed outside the redrawn {r:?}");+ }+ }+ }+ }+/// Dragging with an unlinked mask targeted moves the mask alone, shown live and committed;/// with the mask linked (or the pixels targeted) the layer moves.#[test]