Skip to content

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

Move drag reveals a larger-than-canvas layer's off-canvas pixels (#526)

sommerper2051c0caf4c7
diff --git a/crates/ui-egui/src/move_ui.rs b/crates/ui-egui/src/move_ui.rs
index 0441b3b56..f6f0cc9ed 100644
--- a/crates/ui-egui/src/move_ui.rs
+++ b/crates/ui-egui/src/move_ui.rs
@@ -19,8 +19,10 @@ pub(crate) struct MovePreview {
revision: u64,
/// Layers that move ([`photocraft_engine::layer_multi_cmds::move_targets`]).
ids: Vec<LayerId>,
- /// What moving them can change at offset (0, 0) (`None` = anything).
+ /// What moving them can change at offset (0, 0), not clipped to the canvas: a layer larger
+ /// than the canvas brings its pixels from beyond the edge into view (`None` = anything).
bounds: Option<Rect>,
+ canvas: Rect,
/// Offsets shown so far, by preview key (`BASE + index`).
offsets: Vec<(i32, i32)>,
/// The document at the latest offset.
@@ -40,7 +42,7 @@ impl MovePreview {
}
fn area(&self, d: (i32, i32)) -> Option<Rect> {
let b = self.bounds?;
- Some(if b.is_empty() { b } else { b.translate(d.0, d.1).inflate(1) })
+ Some(if b.is_empty() { b } else { b.translate(d.0, d.1).inflate(1).intersect(&self.canvas) })
}
}
@@ -72,8 +74,10 @@ pub(crate) fn display_doc(app: &mut PhotocraftApp, idx: usize) -> Option<(Arc<Do
if ids.is_empty() {
return None;
}
- let bounds = photocraft_engine::layer_multi_cmds::layers_damage(&doc, &doc, &ids);
- app.move_preview = Some(MovePreview { doc: doc_id, revision, ids, bounds, offsets: Vec::new(), shown: None });
+ let all = Rect::new(i32::MIN / 2, i32::MIN / 2, i32::MAX / 2, i32::MAX / 2);
+ let bounds = ids.iter().try_fold(Rect::EMPTY, |acc, id| Some(acc.union(&photocraft_compose::change_bounds(doc.layer(*id)?, all)?)));
+ let canvas = doc.bounds();
+ app.move_preview = Some(MovePreview { doc: doc_id, revision, ids, bounds, canvas, offsets: Vec::new(), shown: None });
}
let p = app.move_preview.as_mut()?;
if offset == (0, 0) && p.offsets.is_empty() {
@@ -142,3 +146,41 @@ pub(crate) fn finish(app: &mut PhotocraftApp, dx: f64, dy: f64) {
let Some(p) = shown else { return };
crate::canvas::shown_as_document(app, p.doc, is_preview_key);
}
+
+#[cfg(test)]
+mod tests {
+ use photocraft_geom::Rect;
+ use serde_json::json;
+
+ use crate::PhotocraftApp;
+ use crate::canvas::ToolEvent;
+ use crate::state::Tool;
+
+ #[test]
+ fn dragging_a_layer_larger_than_the_canvas_redraws_what_comes_in_from_beyond_the_edge() {
+ let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
+ app.session.execute("file.new", json!({"width": 64, "height": 64})).unwrap();
+ app.sync_views();
+ app.session.execute("layer.new.layer", json!({})).unwrap();
+ app.session
+ .edit("paint", |doc, a| {
+ doc.layer_mut(a.unwrap()).unwrap().surface_mut().unwrap().fill_rect(Rect::new(-32, -32, 96, 96), &[1.0, 0.0, 0.0, 1.0]);
+ Ok(())
+ })
+ .unwrap();
+ 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: 32.0, y: 32.0, pressure: 1.0 }, m);
+ let mut keys = Vec::new();
+ for x in [42.0, 52.0] {
+ crate::canvas::tool_event(&mut app, ToolEvent::Move { x, y: 32.0, pressure: 1.0 }, m);
+ keys.push(super::display_doc(&mut app, 0).unwrap().1);
+ }
+ let st = app.session.active().unwrap();
+ // From +10 to +20 the strip x 0..10 shows pixels that were off the canvas a frame ago.
+ let r = super::damage(&app, st.doc.id, st.revision, keys[0], keys[1]).unwrap();
+ assert_eq!(r, Rect::new(0, 0, 64, 64));
+ }
+}