From 6db3ac0cf2a1be0bf4e2af9f1e7dcc25f484c6d7 Mon Sep 17 00:00:00 2001 From: qvest-ssels <214787280+qvest-ssels@users.noreply.github.com> Date: Sat, 28 Nov 2026 17:23:18 +0100 Subject: [PATCH] Layers: a layer mask dropped on a masked layer asks Replace Layer Mask? (#16787) Dragging or Alt-dragging a layer mask thumbnail onto a layer that already has a layer mask was refused. It now opens the alert "Replace Layer Mask?" with Yes (default), No and an unticked "Don't show again", as in the reference application. Yes moves (or with Alt copies) the mask in one undo step through layer.layerMask.moveTo with replace; No changes nothing; a ticked box replaces without asking next time. Groups and adjustment layers with a mask still refuse the drop (not measured). --- crates/ui-egui/src/dialogs.rs | 5 +- .../src/dialogs/replace_layer_mask_prompt.rs | 89 ++++++++ crates/ui-egui/src/panels.rs | 3 + crates/ui-egui/src/panels/layers_events.rs | 9 +- .../src/panels/layers_replace_mask_tests.rs | 207 ++++++++++++++++++ crates/ui-egui/src/panels/layers_tree.rs | 6 +- 6 files changed, 315 insertions(+), 4 deletions(-) create mode 100644 crates/ui-egui/src/dialogs/replace_layer_mask_prompt.rs create mode 100644 crates/ui-egui/src/panels/layers_replace_mask_tests.rs diff --git a/crates/ui-egui/src/dialogs.rs b/crates/ui-egui/src/dialogs.rs index 4840e265e9..04a32a74ae 100644 --- a/crates/ui-egui/src/dialogs.rs +++ b/crates/ui-egui/src/dialogs.rs @@ -445,6 +445,7 @@ pub(crate) const PROMPTS: &[PromptRow] = &[ disk_change_prompt::answer, rasterize_prompt::answer, live_shape_prompt::answer, + replace_layer_mask_prompt::answer, assign_profile_layers_alert::answer, preset_group_prompt::answer, open_prompt::answer_prompt, @@ -669,13 +670,14 @@ const ENGINE_READ_SHARED: &[&str] = &[photocraft_engine::atn_cmds::CAF_WORKSPACE /// The "Don't show again" entries (`{"skip": true}`), by key, as their prompt modules read them. Told apart by key, not /// by the shape of the value. -fn dont_show_again_keys() -> [&'static str; 5] { +fn dont_show_again_keys() -> [&'static str; 6] { [ delete_layer_prompt::PREF, live_shape_prompt::PREF, assign_profile_layers_alert::PREF, crate::analysis::note_alerts::DELETE_NOTE_PREF, crate::retouch_ui::CAM_NO_PIXELS_PREF, + replace_layer_mask_prompt::PREF, ] } @@ -847,6 +849,7 @@ mod paths_export_document_bounds_tests; pub(crate) mod pattern_fill_ui; pub mod preset_group_prompt; pub mod rasterize_prompt; +pub(crate) mod replace_layer_mask_prompt; #[cfg(test)] mod rotate_canvas_layout_tests; pub(crate) mod rotate_canvas_ui; diff --git a/crates/ui-egui/src/dialogs/replace_layer_mask_prompt.rs b/crates/ui-egui/src/dialogs/replace_layer_mask_prompt.rs new file mode 100644 index 0000000000..ff395b3218 --- /dev/null +++ b/crates/ui-egui/src/dialogs/replace_layer_mask_prompt.rs @@ -0,0 +1,89 @@ +//! A layer mask dragged (or Alt-dragged) in the Layers panel onto a layer that already has a layer +//! mask asks "Replace Layer Mask?" first, as in Photoshop (#16787): Yes (the default) moves the mask +//! there, or copies it with Alt, replacing the mask in one undo step; No changes nothing. A ticked +//! "Don't show again" (kept with the other dialog preferences under [`PREF`]) makes later drops +//! replace without asking. +//! +//! Groups and adjustment layers that have a mask still refuse the drop: what Photoshop does there was +//! not measured. + +use craft::ui::dialog::{PromptAnswer, PromptChoice, StandardPrompt}; +use photocraft_doc::{Layer, LayerContent, LayerId}; +use serde_json::{Value, json}; + +use super::alerts; +use crate::PhotocraftApp; + +/// Its "Don't show again", under `Preferences::dialogs`. +pub const PREF: &str = "layer.layerMask.replace"; + +const COMMAND: &str = "layer.layerMask.moveTo"; + +/// What the question asked about: the mask of `from` going to `to` (a copy when `copy`), in this +/// `revision` of `document`. An edit or another document meanwhile makes the answer replace nothing. +pub(crate) struct ReplaceMask { + document: u64, + revision: u64, + from: LayerId, + to: LayerId, + copy: bool, +} + +/// Whether a layer mask dropped on `target` replaces its mask after the question: it has a layer +/// mask and is neither a group nor an adjustment layer. +pub(crate) fn asks_before_replacing(target: &Layer) -> bool { + target.mask.is_some() && !matches!(target.content, LayerContent::Group(_) | LayerContent::Adjustment(_)) +} + +/// The `layer.layerMask.moveTo` step that moves (or with `copy` copies) the mask of `from` onto +/// `to`, replacing a mask there when `replace`. +pub(crate) fn move_action(from: LayerId, to: LayerId, copy: bool, replace: bool) -> (String, Value) { + let mut params = json!({"layer": from.0, "to": to.0, "copy": copy}); + if replace { + params["replace"] = json!(true); + } + (COMMAND.into(), params) +} + +/// A layer mask dropped from `from` onto `to`, which already has one. `Some` step to run at once when +/// "Don't show again" was ticked before; otherwise opens the question and returns `None`. +pub(crate) fn ask(app: &mut PhotocraftApp, from: LayerId, to: LayerId, copy: bool) -> Option<(String, Value)> { + if alerts::dismissed(app, PREF) { + return Some(move_action(from, to, copy, true)); + } + let (document, revision) = app.session.active().map(|st| (st.doc.id.0, st.revision))?; + let prompt = StandardPrompt::custom(tl!("Replace Layer Mask?").to_owned()) + .title(tl!("PhotoCraft").to_owned()) + .button(PromptChoice::Proceed, tl!("Yes").to_owned()) + .button(PromptChoice::Cancel, tl!("No").to_owned()) + .dont_show_again() + .tag(ReplaceMask { document, revision, from, to, copy }); + // An `Err` is another question already open: the drop waits for nothing and changes nothing. + let _ = alerts::open_prompt(app, prompt); + None +} + +/// The `PROMPTS` entry: Yes replaces the mask in one step and keeps a ticked "Don't show again"; No +/// changes nothing. `false` for another family's answer. +pub(crate) fn answer(app: &mut PhotocraftApp, _ctx: &egui::Context, answer: &mut PromptAnswer) -> bool { + let Some(ask) = answer.take_tag::() else { return false }; + if answer.choice != PromptChoice::Proceed { + return true; + } + match confirm(app, &ask) { + Ok(_) if answer.skip => alerts::remember_skip(app, PREF), + Ok(_) => {} + Err(error) => alerts::report_error(app, error), + } + true +} + +/// Runs the replacement when the document is still the one, in the revision, the question was asked in. +fn confirm(app: &mut PhotocraftApp, ask: &ReplaceMask) -> Result { + let current = app.session.active().map(|st| (st.doc.id.0, st.revision)); + if current != Some((ask.document, ask.revision)) { + return Err(tl!("The document changed. Drag the mask again to replace it.").into()); + } + let (command, params) = move_action(ask.from, ask.to, ask.copy, true); + app.run(&command, params) +} diff --git a/crates/ui-egui/src/panels.rs b/crates/ui-egui/src/panels.rs index bea440a6d5..2f0be13816 100644 --- a/crates/ui-egui/src/panels.rs +++ b/crates/ui-egui/src/panels.rs @@ -172,3 +172,6 @@ mod mask_properties_tests; #[cfg(test)] mod history_panel_select_tests; + +#[cfg(test)] +mod layers_replace_mask_tests; diff --git a/crates/ui-egui/src/panels/layers_events.rs b/crates/ui-egui/src/panels/layers_events.rs index 8d38e64b10..cbb293a327 100644 --- a/crates/ui-egui/src/panels/layers_events.rs +++ b/crates/ui-egui/src/panels/layers_events.rs @@ -262,7 +262,14 @@ fn map_event(app: &mut PhotocraftApp, ctx: &egui::Context, event: LayersEvent { - actions.push(("layer.layerMask.moveTo".into(), json!({"layer": mv.layer.0, "to": mv.to.0, "copy": mv.copy}))); + use crate::dialogs::replace_layer_mask_prompt as replace; + let target = doc.layer(mv.to); + if target.is_some_and(replace::asks_before_replacing) { + // Asks "Replace Layer Mask?" first, unless "Don't show again" was ticked (#16787). + actions.extend(replace::ask(app, mv.layer, mv.to, mv.copy)); + } else if target.is_some_and(|l| l.mask.is_none()) { + actions.push(replace::move_action(mv.layer, mv.to, mv.copy, false)); + } } _ => {} } diff --git a/crates/ui-egui/src/panels/layers_replace_mask_tests.rs b/crates/ui-egui/src/panels/layers_replace_mask_tests.rs new file mode 100644 index 0000000000..9d1160b789 --- /dev/null +++ b/crates/ui-egui/src/panels/layers_replace_mask_tests.rs @@ -0,0 +1,207 @@ +//! #16787: a layer mask dragged (or Alt-dragged) onto a layer that already has a layer mask asks +//! "Replace Layer Mask?" with Yes (the default), No and an unticked "Don't show again", as in the +//! reference application, instead of refusing the drop. Yes moves (with Alt copies) the mask in one +//! undo step, No changes nothing, a ticked "Don't show again" replaces at once next time. A drop on +//! the mask's own layer, a vector mask's drop, and a drop on a group or adjustment layer that has a +//! mask (not measured) are still refused. + +use craft::layers::ui::tree::LayerPart; +use craft::ui::dialog::{DialogId, StackDialog as _, StandardPrompt}; +use egui::Modifiers; +use egui_kittest::{Harness, kittest::Queryable}; +use photocraft_doc::LayerMask; +use photocraft_engine::Session; +use serde_json::json; + +use crate::PhotocraftApp; +use crate::dialogs::alerts; +use crate::dialogs::replace_layer_mask_prompt::{PREF, ReplaceMask}; +use crate::layers_panel_nodes::{drag, layer_id, name_point, thumb}; + +type H = Harness<'static, Box>; + +fn harness(s: Session) -> H { + let mut h = Harness::builder().with_size(egui::vec2(1440.0, 900.0)).with_step_dt(1.0 / 60.0).with_max_steps(64).build_eframe(|cc| { + PhotocraftApp::setup_context(&cc.egui_ctx, Default::default()); + PhotocraftApp::with_context(cc.egui_ctx.clone(), s, crate::Services::default()) + }); + let ctx = h.ctx.clone(); + let (r, _) = crate::control::ControlRequest::new("ui.set", json!({"dock": {"collapsed": ["color", "properties", "history", "navigator"]}})); + crate::control::handle(h.state_mut(), &ctx, &r); + h.run_steps(8); + h +} + +/// 64 x 48 with "Source" (a mask revealing 8,8 24x16) under "Target" (a mask hiding all). +fn masked_pair() -> Session { + let mut s = Session::new(); + s.execute("file.new", json!({"width": 64, "height": 48})).unwrap(); + s.execute("layer.new.layer", json!({"name": "Source"})).unwrap(); + s.execute("select.rect", json!({"x": 8, "y": 8, "width": 24, "height": 16})).unwrap(); + s.execute("layer.layerMask.revealSelection", json!({})).unwrap(); + s.execute("layer.new.layer", json!({"name": "Target"})).unwrap(); + s.execute("layer.layerMask.hideAll", json!({})).unwrap(); + s +} + +fn mask(h: &H, name: &str) -> Option { + h.state().session.active().unwrap().doc.layer(layer_id(h.state(), name)).unwrap().mask.clone() +} + +fn steps(h: &H) -> usize { + h.state().session.active().unwrap().history.past_len() +} + +fn drag_mask(h: &mut H, from: &str, to: &str, mods: Modifiers) { + let at = thumb(h, from, LayerPart::LayerMask).center(); + let target = name_point(h, to); + drag(h, at, target, mods); +} + +fn question(h: &H) -> Option { + alerts::prompt_tagged::(h.state()) +} + +/// Presses the question's default button, ticking "Don't show again" when `skip`. +fn answer_default(h: &mut H, id: DialogId, skip: bool) { + let ctx = h.ctx.clone(); + if skip { + h.state_mut().framework.dialogs.set_json_field(&ctx, id, "skip", json!(true)).unwrap(); + } + crate::dialogs::confirm(h.state_mut(), &ctx, id).unwrap(); + h.run_steps(3); +} + +fn answer_no(h: &mut H, id: DialogId) { + let ctx = h.ctx.clone(); + h.state_mut().framework.dialogs.set_json_field(&ctx, id, "choice", json!("cancel")).unwrap(); + crate::dialogs::confirm(h.state_mut(), &ctx, id).unwrap(); + h.run_steps(3); +} + +#[test] +fn a_drop_on_a_masked_layer_asks_replace_layer_mask_with_yes_no_and_an_unticked_box() { + for mods in [Modifiers::NONE, Modifiers::ALT] { + let mut h = harness(masked_pair()); + let (source, target, n) = (mask(&h, "Source"), mask(&h, "Target"), steps(&h)); + drag_mask(&mut h, "Source", "Target", mods); + assert!(question(&h).is_some(), "{mods:?}: the drop asks"); + let prompt = h.state().framework.dialogs.get::().unwrap(); + assert_eq!(prompt.json_fields().unwrap()["skip"], json!(false), "{mods:?}: Don't show again starts unticked"); + h.run_steps(3); + for label in ["Replace Layer Mask?", "Yes", "No", "Don't show again"] { + assert!(h.query_by_label(label).is_some(), "{mods:?}: the alert shows {label:?}"); + } + assert_eq!((mask(&h, "Source"), mask(&h, "Target"), steps(&h)), (source, target, n), "{mods:?}: nothing changes before the answer"); + } +} + +#[test] +fn no_leaves_both_layers_and_the_history_alone() { + for mods in [Modifiers::NONE, Modifiers::ALT] { + let mut h = harness(masked_pair()); + let (source, target, n) = (mask(&h, "Source"), mask(&h, "Target"), steps(&h)); + drag_mask(&mut h, "Source", "Target", mods); + let id = question(&h).expect("the question"); + answer_no(&mut h, id); + assert!(question(&h).is_none()); + assert_eq!((mask(&h, "Source"), mask(&h, "Target"), steps(&h)), (source, target, n), "{mods:?}"); + } +} + +#[test] +fn yes_the_default_moves_the_mask_in_one_undo_step() { + let mut h = harness(masked_pair()); + let (source, target, n) = (mask(&h, "Source"), mask(&h, "Target"), steps(&h)); + assert_ne!(source, target); + drag_mask(&mut h, "Source", "Target", Modifiers::NONE); + let id = question(&h).expect("the question"); + answer_default(&mut h, id, false); + assert_eq!(mask(&h, "Source"), None, "the mask left its layer"); + assert_eq!(mask(&h, "Target").map(|m| m.surface), source.clone().map(|m| m.surface), "and replaced the target's"); + assert_eq!(steps(&h), n + 1, "one step"); + assert!(h.state_mut().session.undo()); + assert_eq!((mask(&h, "Source"), mask(&h, "Target")), (source, target), "one undo puts both back"); +} + +#[test] +fn yes_after_an_alt_drag_copies_the_mask_in_one_undo_step() { + let mut h = harness(masked_pair()); + let (source, target, n) = (mask(&h, "Source"), mask(&h, "Target"), steps(&h)); + drag_mask(&mut h, "Source", "Target", Modifiers::ALT); + let id = question(&h).expect("the question"); + answer_default(&mut h, id, false); + assert_eq!(mask(&h, "Source"), source, "the original stays"); + assert_eq!(mask(&h, "Target").map(|m| m.surface), source.clone().map(|m| m.surface), "a copy replaced the target's"); + assert_eq!(steps(&h), n + 1, "one step"); + assert!(h.state_mut().session.undo()); + assert_eq!((mask(&h, "Source"), mask(&h, "Target")), (source, target)); +} + +#[test] +fn a_ticked_dont_show_again_replaces_at_once_next_time() { + let mut h = harness(masked_pair()); + let source = mask(&h, "Source"); + drag_mask(&mut h, "Source", "Target", Modifiers::ALT); + let id = question(&h).expect("the question"); + answer_default(&mut h, id, true); + assert!(alerts::dismissed(h.state(), PREF), "the box is remembered"); + // Give Target its own mask again, then drop once more: no question, the mask is replaced. + let target = layer_id(h.state(), "Target").0; + h.state_mut().run("layer.layerMask.delete", json!({"layer": target})).unwrap(); + h.state_mut().run("layer.layerMask.hideAll", json!({"layer": target})).unwrap(); + h.run_steps(3); + let n = steps(&h); + drag_mask(&mut h, "Source", "Target", Modifiers::NONE); + assert!(question(&h).is_none(), "no question"); + assert_eq!(mask(&h, "Source"), None, "the mask moved"); + assert_eq!(mask(&h, "Target").map(|m| m.surface), source.map(|m| m.surface)); + assert_eq!(steps(&h), n + 1, "in one step"); +} + +#[test] +fn an_edit_before_yes_replaces_nothing() { + let mut h = harness(masked_pair()); + drag_mask(&mut h, "Source", "Target", Modifiers::NONE); + let id = question(&h).expect("the question"); + let target = layer_id(h.state(), "Target").0; + h.state_mut().run("layer.layerMask.enabled", json!({"layer": target, "enabled": false})).unwrap(); + let (source, masked) = (mask(&h, "Source"), mask(&h, "Target")); + answer_default(&mut h, id, false); + assert_eq!((mask(&h, "Source"), mask(&h, "Target")), (source, masked), "the stale answer changes nothing"); + assert!(h.state().ui.status_error, "and says why"); +} + +#[test] +fn a_drop_on_a_layer_without_a_mask_still_moves_without_asking() { + let mut s = masked_pair(); + s.execute("layer.new.layer", json!({"name": "Plain"})).unwrap(); + let mut h = harness(s); + let (source, n) = (mask(&h, "Source"), steps(&h)); + drag_mask(&mut h, "Source", "Plain", Modifiers::NONE); + assert!(question(&h).is_none()); + assert_eq!(mask(&h, "Plain"), source); + assert_eq!(steps(&h), n + 1); +} + +#[test] +fn the_own_layer_a_vector_mask_a_masked_group_and_a_masked_adjustment_still_refuse() { + let mut s = masked_pair(); + s.execute("layer.vectorMask.revealAll", json!({})).unwrap(); + s.execute("layer.newAdjustmentLayer.brightnessContrast", json!({"name": "Tone", "brightness": 12})).unwrap(); + s.execute("layer.new.group", json!({"name": "Folder"})).unwrap(); + s.execute("layer.layerMask.hideAll", json!({})).unwrap(); + let mut h = harness(s); + assert!(mask(&h, "Tone").is_some() && mask(&h, "Folder").is_some(), "both have a mask"); + let snapshot = |h: &H| ["Source", "Target", "Tone", "Folder"].map(|name| mask(h, name)); + let (before, n) = (snapshot(&h), steps(&h)); + for to in ["Source", "Tone", "Folder"] { + drag_mask(&mut h, "Source", to, Modifiers::NONE); + assert!(question(&h).is_none(), "onto {to}: no question"); + } + let vector = thumb(&h, "Target", LayerPart::VectorMask).center(); + let source = name_point(&h, "Source"); + drag(&mut h, vector, source, Modifiers::NONE); + assert!(question(&h).is_none(), "a vector mask: no question"); + assert_eq!((snapshot(&h), steps(&h)), (before, n), "nothing changed"); +} diff --git a/crates/ui-egui/src/panels/layers_tree.rs b/crates/ui-egui/src/panels/layers_tree.rs index 1b30713394..2b0c657d6d 100644 --- a/crates/ui-egui/src/panels/layers_tree.rs +++ b/crates/ui-egui/src/panels/layers_tree.rs @@ -550,8 +550,10 @@ impl LayerTree for PhotoLayers { } fn accepts_mask_drop(&self, from: &LayerId, part: LayerPart, to: &LayerId) -> bool { - // The engine moves layer masks only. - part == LayerPart::LayerMask && from != to && self.layer(to).is_some_and(|l| l.mask.is_none()) + // The engine moves layer masks only. A layer that has one asks "Replace Layer Mask?" first (#16787). + part == LayerPart::LayerMask + && from != to + && self.layer(to).is_some_and(|l| l.mask.is_none() || crate::dialogs::replace_layer_mask_prompt::asks_before_replacing(l)) } fn label_colors_in_menu(&self, target: &MenuTarget) -> bool {