diff --git a/crates/ui-egui/src/tools/move_lock.rs b/crates/ui-egui/src/tools/move_lock.rs index aee5297bab..12fd1d2a69 100644 --- a/crates/ui-egui/src/tools/move_lock.rs +++ b/crates/ui-egui/src/tools/move_lock.rs @@ -9,6 +9,8 @@ use crate::PhotocraftApp; 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 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. @@ -43,7 +45,29 @@ pub fn blocked(app: &PhotocraftApp, standing_in: bool) -> bool { !floating && st.doc.selection.is_some() && pixels_locked(app) || layers_locked(app) } -/// Opens the message as an alert. -pub fn prompt(app: &mut PhotocraftApp) { - crate::dialogs::alerts::alert(app, tl!(MESSAGE)); +/// The move is refused only because of a layer linked to the selection: no layer of the selection is locked in +/// place, its selected pixels could float, and a linked layer that moves along has Lock all (#18139). A linked layer +/// with only Lock position is not measured and is not counted. +fn linked_layers_locked(app: &PhotocraftApp) -> bool { + let Some(st) = app.session.active() else { return false }; + let selected = st.selected_layers(); + let targets = photocraft_engine::layer_multi_cmds::move_targets(&st.doc, selected); + let (own, linked): (Vec<_>, Vec<_>) = targets.into_iter().partition(|id| selected.contains(id)); + let own_locked = own.iter().any(|id| { + let locks = st.doc.effective_locks(*id); + locks.position || locks.all + }); + let pixels_refused = st.doc.selection.is_some() && pixels_locked(app); + !own_locked && !pixels_refused && linked.iter().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. +pub fn prompt(app: &mut PhotocraftApp) { + let message = 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; diff --git a/crates/ui-egui/src/tools/move_lock_linked_text_tests.rs b/crates/ui-egui/src/tools/move_lock_linked_text_tests.rs new file mode 100644 index 0000000000..4a37a64a1d --- /dev/null +++ b/crates/ui-egui/src/tools/move_lock_linked_text_tests.rs @@ -0,0 +1,106 @@ +//! #18139: a Move-tool drag refused only because a layer linked to the selection is locked says that linked layers +//! are locked, in an alert of its own. A locked layer of the selection, the Background in it, and a linked partner +//! with only Lock position keep the "layer is locked" alert. + +use egui::Modifiers; +use photocraft_doc::LayerId; +use serde_json::json; + +use super::{LINKED_MESSAGE, MESSAGE}; +use crate::PhotocraftApp; +use crate::state::Tool; + +/// A document with a Background and two linked layers above it, the Move tool picked: the upper layer, which is +/// the selection, and its linked partner. +fn linked_pair() -> (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 (partner, selected) = (new_layer(&mut app), new_layer(&mut app)); + app.session + .edit("link", |doc, _| { + for id in [selected, partner] { + doc.layer_mut(id).unwrap().link_group = Some(1); + } + Ok(()) + }) + .unwrap(); + (app, selected, partner) +} + +/// Lock all on `id`, or with `position_only` just Lock position. +fn lock(app: &mut PhotocraftApp, id: LayerId, position_only: bool) { + app.session + .edit("lock", |doc, _| { + let locks = &mut doc.layer_mut(id).unwrap().locks; + (locks.position, locks.all) = (true, !position_only); + Ok(()) + }) + .unwrap(); +} + +/// A Move-tool drag across the canvas: the messages of the alerts it leaves open, which are then closed. +fn drag_alerts(app: &mut PhotocraftApp) -> Vec { + use craft::ui::dialog::StackDialog; + + 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: 20.0, pressure: 1.0 }, Modifiers::NONE); + tool_event(app, ToolEvent::Up { x: 40.0, y: 20.0 }, Modifiers::NONE); + 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 +} + +fn steps(app: &PhotocraftApp) -> usize { + app.session.active().unwrap().history.past_len() +} + +#[test] +fn a_move_refused_by_a_locked_linked_partner_says_that_linked_layers_are_locked() { + let (mut app, _, partner) = linked_pair(); + assert!(drag_alerts(&mut app).is_empty(), "with nothing locked the pair moves"); + lock(&mut app, partner, false); + let before = steps(&app); + assert_eq!(drag_alerts(&mut app), [LINKED_MESSAGE]); + assert_eq!(steps(&app), before, "nothing moved"); + assert_ne!(LINKED_MESSAGE, MESSAGE, "a text of its own"); +} + +#[test] +fn a_locked_layer_of_the_selection_keeps_the_layer_is_locked_alert() { + // The selected layer is locked itself, whatever its partner is. + for position_only in [false, true] { + let (mut app, selected, partner) = linked_pair(); + lock(&mut app, partner, false); + lock(&mut app, selected, position_only); + assert_eq!(drag_alerts(&mut app), [MESSAGE], "position only: {position_only}"); + } + // The Background is in the selection. + let (mut app, _, partner) = linked_pair(); + lock(&mut app, partner, false); + 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 it keeps the alert it had. +#[test] +fn a_linked_partner_with_only_lock_position_keeps_the_layer_is_locked_alert() { + let (mut app, _, partner) = linked_pair(); + lock(&mut app, partner, true); + assert_eq!(drag_alerts(&mut app), [MESSAGE]); +}