diff --git a/crates/ui-egui/src/tools/move_lock.rs b/crates/ui-egui/src/tools/move_lock.rs index 9cf16a9af6..2e79028b66 100644 --- a/crates/ui-egui/src/tools/move_lock.rs +++ b/crates/ui-egui/src/tools/move_lock.rs @@ -11,6 +11,8 @@ use crate::state::Tool; pub const MESSAGE: &str = "Could not use the move tool because the layer is locked."; /// The alert of a move refused only because a layer linked to the selection is locked (#18139), in our own words. pub const LINKED_MESSAGE: &str = "Could not use the move tool because linked layers are locked."; +/// The alert of a move refused only because a moving group holds a locked layer (#18138), in our own words. +pub const GROUP_MESSAGE: &str = "Could not use the move tool because the group contains a locked layer."; /// The selected pixels can't float: only a fresh cut can be refused, so a floating piece is never /// locked. The selection family's ⌘-drag of pixels asks this. @@ -20,8 +22,14 @@ 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 layers: any of them locked in place refuses the whole move. +/// 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 { + targets_locked(app) || group_child_locked(app) +} + +/// A layer the move takes is locked in place itself or through its groups. +fn targets_locked(app: &PhotocraftApp) -> bool { let Some(st) = app.session.active() else { return false }; photocraft_engine::layer_multi_cmds::move_targets(&st.doc, st.selected_layers()).iter().any(|id| { let locks = st.doc.effective_locks(*id); @@ -29,6 +37,27 @@ fn layers_locked(app: &PhotocraftApp) -> bool { }) } +/// A group the move takes holds a layer with Lock position or Lock all (#18138). Only the group's own children count: +/// a locked layer two groups deep and a child with only a pixel lock are not measured and keep moving along. +fn group_child_locked(app: &PhotocraftApp) -> bool { + let Some(st) = app.session.active() else { return false }; + photocraft_engine::layer_multi_cmds::move_targets(&st.doc, st.selected_layers()).iter().any(|id| { + let Some(photocraft_doc::LayerContent::Group(group)) = st.doc.layer(*id).map(|l| &l.content) else { return false }; + group.children.iter().any(|child| child.locks.position || child.locks.all) + }) +} + +/// The move of the layers is refused only because a moving group holds a locked layer: no layer of the selection is +/// locked in place itself, so the alert says [`GROUP_MESSAGE`]. The arrow-key nudge asks this before it moves. +pub fn refused_for_group_child(app: &PhotocraftApp) -> bool { + let Some(st) = app.session.active() else { return false }; + let own_locked = st.selected_layers().iter().any(|id| { + let locks = st.doc.effective_locks(*id); + locks.position || locks.all + }); + !own_locked && !targets_locked(app) && group_child_locked(app) +} + /// Would a press with the Move tool in effect be a move that fails because of a lock? /// `standing_in` is true when the Move tool stands in for another tool's ⌘ press. pub fn blocked(app: &PhotocraftApp, standing_in: bool) -> bool { @@ -62,13 +91,24 @@ fn linked_layers_locked(app: &PhotocraftApp) -> bool { !own_locked && !pixels_refused && linked.any(|id| st.doc.effective_locks(id).all) } -/// Opens the message as an alert: [`LINKED_MESSAGE`] when only a linked layer's lock refuses the move, [`MESSAGE`] -/// otherwise. +/// 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 message = if linked_layers_locked(app) { tl!(LINKED_MESSAGE) } else { tl!(MESSAGE) }; + let pixels_refused = app.session.active().is_some_and(|st| st.doc.selection.is_some()) && pixels_locked(app); + let message = if !pixels_refused && refused_for_group_child(app) { + tl!(GROUP_MESSAGE) + } else if linked_layers_locked(app) { + tl!(LINKED_MESSAGE) + } else { + tl!(MESSAGE) + }; crate::dialogs::alerts::alert(app, message); } #[cfg(test)] #[path = "move_lock_linked_text_tests.rs"] mod move_lock_linked_text_tests; + +#[cfg(test)] +#[path = "move_lock_group_child_tests.rs"] +mod move_lock_group_child_tests; diff --git a/crates/ui-egui/src/tools/move_lock_group_child_tests.rs b/crates/ui-egui/src/tools/move_lock_group_child_tests.rs new file mode 100644 index 0000000000..7381b9a2f5 --- /dev/null +++ b/crates/ui-egui/src/tools/move_lock_group_child_tests.rs @@ -0,0 +1,129 @@ +//! #18138: a Move of a group that holds a layer with Lock position or Lock all is refused, by a drag and by an arrow +//! nudge, with an alert of its own that says the group contains a locked layer. A group that is all-locked itself, or +//! the Background in the selection, keeps the "layer is locked" alert. + +use egui::Modifiers; +use photocraft_doc::{Group, Layer, LayerContent, LayerId}; +use serde_json::json; + +use super::{GROUP_MESSAGE, MESSAGE}; +use crate::PhotocraftApp; +use crate::state::Tool; + +/// A document with a Background and a group G of two layers, G1 and G2, the Move tool picked and G selected. +fn group_of_two() -> (Box, LayerId, LayerId) { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.run("file.new", json!({"width": 80, "height": 60})).unwrap(); + app.sync_views(); + app.ui.tool = Tool::Move; + app.ui.tool_options.move_auto_select = false; + app.ui.extras.snap = false; + let new_layer = |app: &mut PhotocraftApp| { + app.run("layer.new.layer", json!({})).unwrap(); + app.session.active().unwrap().active_layer.unwrap() + }; + let (g1, g2) = (new_layer(&mut app), new_layer(&mut app)); + app.session + .edit("group", |doc, _| { + let children = vec![doc.remove(g1).unwrap(), doc.remove(g2).unwrap()]; + doc.layers.push(Layer::new("G", LayerContent::Group(Group { children, expanded: true, artboard: None, frame_stroke: None }))); + Ok(()) + }) + .unwrap(); + let group = app.session.active().unwrap().doc.layers.last().unwrap().id; + app.run("layer.select", json!({"layer": group})).unwrap(); + (app, group, g2) +} + +fn set_locks(app: &mut PhotocraftApp, id: LayerId, f: impl Fn(&mut photocraft_doc::Locks)) { + app.session.edit("lock", |doc, _| Ok(f(&mut doc.layer_mut(id).unwrap().locks))).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 drag across the canvas and the alerts it opens. +fn drag_alerts(app: &mut PhotocraftApp) -> 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: 40.0, y: 30.0, pressure: 1.0 }, Modifiers::NONE); + tool_event(app, ToolEvent::Up { x: 40.0, y: 30.0 }, Modifiers::NONE); + alerts(app) +} + +/// A → press with the Move tool and the alerts it opens. +fn nudge_alerts(app: &mut PhotocraftApp) -> Vec { + let mods = craft::keymap::Mods::from_egui(&Modifiers::NONE, craft::keymap::Platform::current()); + assert!(crate::move_mods::arrow_key(app, egui::vec2(1.0, 0.0), mods, 0.0, false)); + alerts(app) +} + +fn steps(app: &PhotocraftApp) -> usize { + app.session.active().unwrap().history.past_len() +} + +#[test] +fn a_group_with_a_locked_layer_inside_refuses_a_drag_and_a_nudge_with_its_own_alert() { + for gesture in [drag_alerts as fn(&mut PhotocraftApp) -> Vec, nudge_alerts] { + let (mut app, _, g2) = group_of_two(); + let before = steps(&app); + assert!(gesture(&mut app).is_empty(), "with nothing locked the group moves"); + assert!(steps(&app) > before, "the unlocked group moved"); + for all in [false, true] { + set_locks(&mut app, g2, |l| (l.position, l.all) = (!all, all)); + let before = steps(&app); + assert_eq!(gesture(&mut app), [GROUP_MESSAGE], "Lock all: {all}"); + assert_eq!(steps(&app), before, "nothing moved, Lock all: {all}"); + } + } + assert_ne!(GROUP_MESSAGE, MESSAGE, "a text of its own"); +} + +#[test] +fn an_all_locked_group_or_the_background_keeps_the_layer_is_locked_alert() { + let (mut app, group, g2) = group_of_two(); + set_locks(&mut app, g2, |l| l.position = true); + set_locks(&mut app, group, |l| l.all = true); + assert_eq!(drag_alerts(&mut app), [MESSAGE]); + + let (mut app, _, g2) = group_of_two(); + set_locks(&mut app, g2, |l| l.position = true); + let background = app.session.active().unwrap().doc.background().unwrap().id; + app.run("layer.select", json!({"layer": background, "mode": "add"})).unwrap(); + assert_eq!(drag_alerts(&mut app), [MESSAGE]); +} + +/// Not measured, so they keep moving along as before: a child with only a pixel lock, and a locked layer two groups deep. +#[test] +fn unmeasured_locks_inside_the_group_keep_moving() { + let (mut app, _, g2) = group_of_two(); + set_locks(&mut app, g2, |l| l.pixels = true); + let before = steps(&app); + assert!(drag_alerts(&mut app).is_empty()); + assert!(steps(&app) > before); + + let (mut app, group, g2) = group_of_two(); + app.session + .edit("nest", |doc, _| { + let mut inner = doc.remove(g2).unwrap(); + inner.locks.position = true; + let LayerContent::Group(g) = &mut doc.layer_mut(group).unwrap().content else { unreachable!() }; + g.children.push(Layer::new("inner", LayerContent::Group(Group { children: vec![inner], expanded: true, artboard: None, frame_stroke: None }))); + Ok(()) + }) + .unwrap(); + let before = steps(&app); + assert!(drag_alerts(&mut app).is_empty()); + assert!(steps(&app) > before); +} diff --git a/crates/ui-egui/src/tools/move_mods.rs b/crates/ui-egui/src/tools/move_mods.rs index 1eadbd54f6..38937109d4 100644 --- a/crates/ui-egui/src/tools/move_mods.rs +++ b/crates/ui-egui/src/tools/move_mods.rs @@ -184,6 +184,11 @@ pub fn nudge(app: &mut PhotocraftApp, dx: f64, dy: f64, duplicate_first: bool) { crate::move_ui::float_selected(app, duplicate_first, dx, dy); return; } + // A group holding a locked layer: nothing moves, the alert says why (#18138). + if crate::move_lock::refused_for_group_child(app) { + crate::move_lock::prompt(app); + return; + } let from = if duplicate_first { duplicate(app) } else { None }; let revision = app.session.active().map(|st| st.revision); let moved = if duplicate_first {