PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Keep transform controls with moved selected pixels (#3402) (#3436)
prekabrekie762c784fb85
diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rsindex fbac6ce43..6778becf8 100644--- a/crates/ui-egui/src/canvas.rs+++ b/crates/ui-egui/src/canvas.rs@@ -3957,6 +3957,8 @@ fn transform_controls_rect(app: &PhotocraftApp, xf: &ViewXform) -> Option<Rect>return None;}let b = photocraft_engine::transform_cmds::transform_bounds(&st.doc, l);+ let (dx, dy) = crate::move_ui::floating_offset(app).unwrap_or((0, 0));+ let b = b.translate(dx, dy);if b.is_empty() {return None;}@@ -6578,6 +6580,25 @@ mod transform_controls_tests {assert_eq!(t.rect, [20.0, 30.0, 100.0, 70.0]);}+ #[test]+ fn move_transform_controls_follow_floating_selected_pixels() {+ let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());+ app.run("file.new", json!({"width": 400, "height": 300})).unwrap();+ app.run("layer.new.layer", json!({})).unwrap();+ app.run("select.rect", json!({"x": 100, "y": 100, "width": 100, "height": 60})).unwrap();+ app.run("edit.fill", json!({"color": "#ff0000"})).unwrap();+ app.ui.tool = Tool::Move;+ app.ui.tool_options.move_show_transform = true;+ assert!(crate::move_ui::float_selected(&mut app, false, 100.0, 50.0));+ let xf =+ ViewXform { rect: Rect::from_min_size(Pos2::ZERO, vec2(400.0, 300.0)), zoom: 1.0, center: [200.0, 150.0], flip: false, rotation: 0.0, aspect: 1.0 };+ let r = transform_controls_rect(&app, &xf).unwrap();+ assert_eq!(r, Rect::from_min_max(pos2(200.0, 150.0), pos2(300.0, 210.0)));+ let ctx = egui::Context::default();+ assert!(begin_transform_controls_at(&mut app, &ctx, &xf, r.right_bottom()));+ assert_eq!(app.ui.transform.as_ref().unwrap().rect, [200.0, 150.0, 300.0, 210.0]);+ }+/// #2585: Free Transform on a position-locked layer is refused before the box opens, with/// Photoshop's alert, from ⌘T and from the Move tool's transform controls alike. It used to/// open, preview every drag and fail only at commit.diff --git a/crates/ui-egui/src/transform_tool.rs b/crates/ui-egui/src/transform_tool.rsindex 7e4eef019..2806e2d5d 100644--- a/crates/ui-egui/src/transform_tool.rs+++ b/crates/ui-egui/src/transform_tool.rs@@ -163,6 +163,9 @@ pub fn begin(app: &mut PhotocraftApp, ctx: &egui::Context) -> Result<(), String>if let Some((params, path)) = crate::vector_ui::free_transform_path(app) {return begin_path(app, ctx, params, path);}+ if app.session.active().is_some_and(|st| photocraft_engine::float_cmds::floating(st).is_some()) {+ app.run("select.drop", json!({}))?;+ }let target = crate::canvas::paint_target(app);let st = app.session.active().ok_or("no document")?;let (doc, revision) = (st.doc.clone(), st.revision);diff --git a/crates/ui-egui/src/transform_undo_tests.rs b/crates/ui-egui/src/transform_undo_tests.rsindex 360770231..d9ffdf9c6 100644--- a/crates/ui-egui/src/transform_undo_tests.rs+++ b/crates/ui-egui/src/transform_undo_tests.rs@@ -100,6 +100,19 @@ fn close(a: [[f64; 2]; 4], b: [[f64; 2]; 4]) -> bool {a.iter().flatten().zip(b.iter().flatten()).all(|(x, y)| (x - y).abs() < 1e-6)}+#[test]+fn free_transform_starts_where_the_selected_pixels_were_moved() {+ let mut h = harness();+ h.state_mut().run("select.rect", json!({"x": 100, "y": 100, "width": 100, "height": 60})).unwrap();+ drag(&mut h, [150.0, 130.0], [250.0, 180.0]);+ assert_eq!(photocraft_engine::float_cmds::floating(h.state().session.active().unwrap()).unwrap().offset, (100, 50));+ begin(&mut h);+ assert_eq!(quad(&h), [[200.0, 150.0], [300.0, 150.0], [300.0, 210.0], [200.0, 210.0]]);+ let st = h.state().session.active().unwrap();+ assert!(photocraft_engine::float_cmds::floating(st).is_none());+ assert_eq!(st.doc.selection.as_ref().unwrap().content_bounds(), photocraft_geom::Rect::new(200, 150, 300, 210));+}+/// Scale ×2, then down again, then ⌘Z: back to the ×2 box, not the fill before the transform.#[test]fn undo_steps_back_through_the_transform_not_the_document() {