diff --git a/crates/engine-core/src/cmd/float_cmds.rs b/crates/engine-core/src/cmd/float_cmds.rs index 9f46d14eab..a98e1c483f 100644 --- a/crates/engine-core/src/cmd/float_cmds.rs +++ b/crates/engine-core/src/cmd/float_cmds.rs @@ -312,6 +312,13 @@ pub fn locked_for_float(doc: &Document, l: &photocraft_doc::Layer) -> bool { locks.all || (locks.position && !doc.is_background(l.id)) } +/// Do `l`'s locks stop a Move-tool drag of its selected pixels? Those of [`locked_for_float`], and Lock image pixels +/// too: dragging selected pixels needs pixel access (#18131). `select.float` itself, and with it the arrow-key nudge +/// of the selected pixels, keeps [`locked_for_float`]: that case is not measured. +pub fn locked_for_pixel_drag(doc: &Document, l: &photocraft_doc::Layer) -> bool { + locked_for_float(doc, l) || doc.effective_locks(l.id).pixels +} + fn float(s: &mut Session, p: &Value) -> Result { let (dx, dy) = (int_param(p, "dx"), int_param(p, "dy")); let copy = p.get("copy").and_then(Value::as_bool).unwrap_or(false); @@ -462,3 +469,7 @@ pub fn specs() -> Vec { #[cfg(test)] mod tests; + +#[cfg(test)] +#[path = "move_lock_pixels_selection_tests.rs"] +mod move_lock_pixels_selection_tests; diff --git a/crates/engine-core/src/cmd/move_lock_pixels_selection_tests.rs b/crates/engine-core/src/cmd/move_lock_pixels_selection_tests.rs new file mode 100644 index 0000000000..ea1213f77d --- /dev/null +++ b/crates/engine-core/src/cmd/move_lock_pixels_selection_tests.rs @@ -0,0 +1,59 @@ +//! #18131: Lock image pixels refuses a Move-tool drag of the selected pixels (`locked_for_pixel_drag`), while +//! `select.float`, which the arrow-key nudge runs, keeps its meaning (not measured). + +use photocraft_doc::Locks; +use serde_json::json; + +use super::*; + +/// A transparent document with one layer and a selection; `locks` on that layer, or on a group holding it. +fn locked(locks: Locks, on_group: bool) -> (Session, LayerId) { + let mut s = crate::session(); + s.execute("file.new", json!({"width": 40, "height": 30, "background": "transparent"})).unwrap(); + let id = s.active().unwrap().active_layer.unwrap(); + s.edit("lock", |doc, _| { + if on_group { + let layer = doc.remove(id).unwrap(); + let mut group = photocraft_doc::Layer::new( + "G", + LayerContent::Group(photocraft_doc::Group { children: vec![layer], expanded: true, artboard: None, frame_stroke: None }), + ); + group.locks = locks; + doc.layers.push(group); + } else { + doc.layer_mut(id).unwrap().locks = locks; + } + Ok(()) + }) + .unwrap(); + s.execute("select.rect", json!({"x": 5, "y": 5, "width": 10, "height": 10})).unwrap(); + (s, id) +} + +fn drag_refused(s: &Session, id: LayerId) -> bool { + let doc = &s.active().unwrap().doc; + locked_for_pixel_drag(doc, doc.layer(id).unwrap()) +} + +#[test] +fn lock_image_pixels_refuses_the_drag_but_not_select_float() { + for on_group in [false, true] { + let (s, id) = locked(Locks { pixels: true, ..Locks::default() }, on_group); + assert!(drag_refused(&s, id), "on the group: {on_group}"); + assert!(s.is_enabled("select.float"), "the nudge's float keeps working, on the group: {on_group}"); + } +} + +#[test] +fn lock_transparent_pixels_or_no_lock_lets_the_drag_through() { + for locks in [Locks::default(), Locks { transparency: true, ..Locks::default() }] { + let (s, id) = locked(locks, false); + assert!(!drag_refused(&s, id), "{locks:?}"); + } +} + +#[test] +fn every_lock_that_stops_a_float_also_stops_the_drag() { + let (s, id) = locked(Locks { all: true, ..Locks::default() }, false); + assert!(!s.is_enabled("select.float") && drag_refused(&s, id)); +} diff --git a/crates/ui-egui/src/tools/move_lock.rs b/crates/ui-egui/src/tools/move_lock.rs index 2e79028b66..f053cc471e 100644 --- a/crates/ui-egui/src/tools/move_lock.rs +++ b/crates/ui-egui/src/tools/move_lock.rs @@ -22,6 +22,15 @@ pub fn pixels_locked(app: &PhotocraftApp) -> bool { && st.active_layer.and_then(|id| st.doc.layer(id)).is_some_and(|l| photocraft_engine::float_cmds::locked_for_float(&st.doc, l)) } +/// The Move tool's drag would cut the selected pixels of a layer whose locks refuse it, Lock image pixels included +/// (`photocraft_engine::float_cmds::locked_for_pixel_drag`, #18131). The arrow-key nudge doesn't ask this. +fn pixel_drag_locked(app: &PhotocraftApp) -> bool { + let Some(st) = app.session.active() else { return false }; + crate::move_ui::moves_selected_pixels_with(app, Tool::Move) + && photocraft_engine::float_cmds::floating(st).is_none() + && st.active_layer.and_then(|id| st.doc.layer(id)).is_some_and(|l| photocraft_engine::float_cmds::locked_for_pixel_drag(&st.doc, l)) +} + /// The layers: any of them locked in place refuses the whole move, and so does a moving group with a locked layer /// inside it ([`group_child_locked`]). fn layers_locked(app: &PhotocraftApp) -> bool { @@ -68,7 +77,7 @@ pub fn blocked(app: &PhotocraftApp, standing_in: bool) -> bool { // `moves_selected_pixels_with` is false for a layer whose pixels can't float: then the Move tool // moves the layer, which its position lock refuses too. if crate::move_ui::moves_selected_pixels_with(app, Tool::Move) { - return pixels_locked(app); + return pixel_drag_locked(app); } let floating = photocraft_engine::float_cmds::floating(st).is_some(); !floating && st.doc.selection.is_some() && pixels_locked(app) || layers_locked(app) @@ -85,7 +94,7 @@ fn linked_layers_locked(app: &PhotocraftApp) -> bool { let locks = st.doc.effective_locks(*id); locks.position || locks.all }); - let pixels_refused = st.doc.selection.is_some() && pixels_locked(app); + let pixels_refused = (st.doc.selection.is_some() && pixels_locked(app)) || pixel_drag_locked(app); let targets = photocraft_engine::layer_multi_cmds::move_targets(&st.doc, selected); let mut linked = targets.into_iter().filter(|id| !selected.contains(id)); !own_locked && !pixels_refused && linked.any(|id| st.doc.effective_locks(id).all) @@ -94,7 +103,7 @@ fn linked_layers_locked(app: &PhotocraftApp) -> bool { /// Opens the message as an alert: [`GROUP_MESSAGE`] when only a locked layer inside a moving group refuses the move, /// [`LINKED_MESSAGE`] when only a linked layer's lock does, [`MESSAGE`] otherwise. pub fn prompt(app: &mut PhotocraftApp) { - let pixels_refused = app.session.active().is_some_and(|st| st.doc.selection.is_some()) && pixels_locked(app); + let pixels_refused = (app.session.active().is_some_and(|st| st.doc.selection.is_some()) && pixels_locked(app)) || pixel_drag_locked(app); let message = if !pixels_refused && refused_for_group_child(app) { tl!(GROUP_MESSAGE) } else if linked_layers_locked(app) { @@ -112,3 +121,7 @@ mod move_lock_linked_text_tests; #[cfg(test)] #[path = "move_lock_group_child_tests.rs"] mod move_lock_group_child_tests; + +#[cfg(test)] +#[path = "move_lock_pixels_selection_tests.rs"] +mod move_lock_pixels_selection_tests; diff --git a/crates/ui-egui/src/tools/move_lock_pixels_selection_tests.rs b/crates/ui-egui/src/tools/move_lock_pixels_selection_tests.rs new file mode 100644 index 0000000000..6b2eb53a66 --- /dev/null +++ b/crates/ui-egui/src/tools/move_lock_pixels_selection_tests.rs @@ -0,0 +1,128 @@ +//! #18131: with a selection, a Move-tool drag on a layer with Lock image pixels is refused with the "layer is locked" +//! alert and changes nothing. Without a selection that layer still moves as a whole; Lock transparent pixels with a +//! selection and the arrow-key nudge of the selected pixels are not measured and keep moving the pixels. + +use egui::Modifiers; +use photocraft_doc::LayerId; +use photocraft_geom::Rect; +use serde_json::json; + +use super::MESSAGE; +use crate::PhotocraftApp; +use crate::state::Tool; + +/// An 80×60 transparent document with one layer painted over (10..30)², (10..30)² selected, the Move tool picked +/// with Auto-Select and snapping off. +fn painted() -> (Box, LayerId) { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.run("file.new", json!({"width": 80, "height": 60, "background": "transparent"})).unwrap(); + app.sync_views(); + app.ui.tool = Tool::Move; + app.ui.tool_options.move_auto_select = false; + app.ui.extras.snap = false; + let id = app.session.active().unwrap().active_layer.unwrap(); + app.session + .edit("paint", |doc, _| { + doc.layer_mut(id).unwrap().surface_mut().unwrap().fill_rect(Rect::new(10, 10, 30, 30), &[1.0, 0.0, 0.0, 1.0]); + Ok(()) + }) + .unwrap(); + app.run("select.rect", json!({"x": 10, "y": 10, "width": 20, "height": 20})).unwrap(); + (app, id) +} + +fn set_locks(app: &mut PhotocraftApp, id: LayerId, f: impl Fn(&mut photocraft_doc::Locks)) { + app.session + .edit("lock", |doc, _| { + f(&mut doc.layer_mut(id).unwrap().locks); + Ok(()) + }) + .unwrap(); +} + +/// The messages of the alerts left open, which are then closed. +fn alerts(app: &mut PhotocraftApp) -> Vec { + use craft::ui::dialog::StackDialog; + let ctx = app.ctx().clone(); + let dialogs = &mut app.framework.dialogs; + let ids: Vec<_> = dialogs.stack().ids().collect(); + let alert = |id| dialogs.get_by_id::(id)?.json_fields()?.get("message")?.as_str().map(String::from); + let messages = ids.iter().copied().filter_map(alert).collect(); + for id in ids { + dialogs.cancel_by_id(&ctx, id); + } + messages +} + +/// A Move-tool press inside the selection, a release at `to`, and the alerts it opens. +fn drag_to(app: &mut PhotocraftApp, to: [f64; 2]) -> Vec { + use crate::canvas::{ToolEvent, tool_event}; + tool_event(app, ToolEvent::Down { x: 20.0, y: 20.0, pressure: 1.0 }, Modifiers::NONE); + tool_event(app, ToolEvent::Move { x: to[0], y: to[1], pressure: 1.0 }, Modifiers::NONE); + tool_event(app, ToolEvent::Up { x: to[0], y: to[1] }, Modifiers::NONE); + alerts(app) +} + +fn steps(app: &PhotocraftApp) -> usize { + app.session.active().unwrap().history.past_len() +} + +fn bounds(app: &PhotocraftApp, id: LayerId) -> Option<[f64; 4]> { + crate::snap_ui::tree_rect(&app.session.active().unwrap().doc, id) +} + +fn alpha(app: &PhotocraftApp, id: LayerId, x: i32, y: i32) -> f32 { + app.session.active().unwrap().doc.layer(id).unwrap().surface().unwrap().rgba(x, y)[3] +} + +fn floating_offset(app: &PhotocraftApp) -> Option<(i32, i32)> { + photocraft_engine::float_cmds::floating(app.session.active().unwrap()).map(|f| f.offset) +} + +#[test] +fn a_drag_of_the_selection_on_a_pixel_locked_layer_is_refused_with_the_alert() { + let (mut app, id) = painted(); + set_locks(&mut app, id, |l| l.pixels = true); + let (before, rect) = (steps(&app), bounds(&app, id)); + assert_eq!(drag_to(&mut app, [40.0, 30.0]), [MESSAGE]); + assert_eq!(floating_offset(&app), None, "nothing was cut"); + assert_eq!(steps(&app), before, "no history step"); + assert_eq!(bounds(&app, id), rect, "the layer's bounds stay"); + assert!(alpha(&app, id, 12, 12) > 0.0 && alpha(&app, id, 35, 25) == 0.0, "the pixels stay where they were"); +} + +#[test] +fn a_plain_click_on_a_pixel_locked_layer_says_nothing() { + let (mut app, id) = painted(); + set_locks(&mut app, id, |l| l.pixels = true); + assert!(drag_to(&mut app, [20.0, 20.0]).is_empty()); +} + +#[test] +fn a_pixel_locked_layer_without_a_selection_still_moves_as_a_whole() { + let (mut app, id) = painted(); + set_locks(&mut app, id, |l| l.pixels = true); + app.run("select.deselect", json!({})).unwrap(); + let (before, rect) = (steps(&app), bounds(&app, id).unwrap()); + assert!(drag_to(&mut app, [40.0, 30.0]).is_empty()); + assert!(steps(&app) > before); + assert_eq!(app.session.active().unwrap().history.undo_label(), Some("Move")); + let moved = bounds(&app, id).unwrap(); + assert!(moved[0] > rect[0] && moved[1] > rect[1], "moved right and down: {rect:?} -> {moved:?}"); +} + +/// Not measured, so they keep today's behaviour: the selected pixels float and move. +#[test] +fn transparency_lock_with_a_selection_and_the_arrow_nudge_keep_moving_the_pixels() { + let (mut app, id) = painted(); + set_locks(&mut app, id, |l| l.transparency = true); + assert!(drag_to(&mut app, [40.0, 30.0]).is_empty()); + assert!(floating_offset(&app).is_some_and(|o| o.0 > 0 && o.1 > 0), "the selected pixels move"); + + let (mut app, id) = painted(); + set_locks(&mut app, id, |l| l.pixels = true); + let mods = craft::keymap::Mods::from_egui(&Modifiers::NONE, craft::keymap::Platform::current()); + assert!(crate::move_mods::arrow_key(&mut app, egui::vec2(1.0, 0.0), mods, 0.0, false)); + assert!(alerts(&mut app).is_empty()); + assert!(floating_offset(&app).is_some_and(|o| o.0 > 0), "the nudge floats the selected pixels as before"); +}