diff --git a/crates/ui-egui/src/color_replacement_blend_keys_tests.rs b/crates/ui-egui/src/color_replacement_blend_keys_tests.rs new file mode 100644 index 0000000000..3acfeca7af --- /dev/null +++ b/crates/ui-egui/src/color_replacement_blend_keys_tests.rs @@ -0,0 +1,101 @@ +//! #11928: with the Color Replacement tool, Shift+Alt+letter sets the active layer's blend mode +//! (one History step each) and the tool's own Mode stays. Its Shift+plus/minus and the other tools +//! that ignore the keys keep today's behaviour. + +use egui::{Key, Modifiers}; +use serde_json::json; + +use super::*; + +const SHIFT_ALT: Modifiers = Modifiers { alt: true, ctrl: false, shift: true, mac_cmd: false, command: false }; + +fn app(tool: Tool) -> PhotocraftApp { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default()); + app.run("file.new", json!({"width": 32, "height": 32})).unwrap(); + app.run("layer.new.layer", json!({})).unwrap(); + app.ui.tool = tool; + app +} + +fn key(app: &mut PhotocraftApp, ctx: &egui::Context, key: Key, modifiers: Modifiers) { + let raw = egui::RawInput { + events: vec![egui::Event::ModifiersChanged(modifiers), egui::Event::Key { key, physical_key: None, pressed: true, repeat: false, modifiers }], + ..Default::default() + }; + ctx.begin_pass(raw); + crate::shortcuts::handle(app, ctx); + ctx.end_pass().textures_delta.clear(); +} + +fn layer_blend(app: &PhotocraftApp) -> BlendMode { + let st = app.session.active().unwrap(); + st.doc.layer(st.active_layer.unwrap()).unwrap().blend +} + +fn steps(app: &PhotocraftApp) -> usize { + app.session.active().unwrap().history.past_len() +} + +#[test] +fn shift_alt_letters_set_the_active_layer() { + let ctx = egui::Context::default(); + let mut app = app(Tool::ColorReplacement); + let tool_options = app.ui.tool_options.clone(); + for (letter, mode) in [(Key::M, BlendMode::Multiply), (Key::N, BlendMode::Normal), (Key::Y, BlendMode::Luminosity), (Key::U, BlendMode::Hue)] { + let n = steps(&app); + key(&mut app, &ctx, letter, SHIFT_ALT); + assert_eq!(layer_blend(&app), mode, "{letter:?}"); + assert_eq!(steps(&app), n + 1, "{letter:?}: one History step"); + } + assert_eq!(app.ui.tool_options, tool_options, "the tool's own Mode and options stay"); +} + +#[test] +fn shift_plus_minus_with_color_replacement_still_do_nothing() { + let ctx = egui::Context::default(); + let mut app = app(Tool::ColorReplacement); + let n = steps(&app); + for k in [Key::Plus, Key::Minus] { + key(&mut app, &ctx, k, Modifiers::SHIFT); + } + assert_eq!((layer_blend(&app), steps(&app)), (BlendMode::Normal, n), "not measured: today's behaviour"); +} + +#[test] +fn the_other_tools_that_ignore_the_keys_still_do() { + let ctx = egui::Context::default(); + for tool in [Tool::SpotHealing, Tool::Blur, Tool::Sharpen, Tool::Smudge, Tool::ArtHistoryBrush, Tool::Dodge, Tool::Burn, Tool::Sponge] { + let mut app = app(tool); + let n = steps(&app); + key(&mut app, &ctx, Key::M, SHIFT_ALT); + assert_eq!((layer_blend(&app), steps(&app)), (BlendMode::Normal, n), "{tool:?}"); + } +} + +#[test] +fn letters_outside_the_measured_four_still_do_nothing() { + // Not measured: Shift+Alt+S (Screen), C (Color), O (Overlay) keep today's behaviour. + let ctx = egui::Context::default(); + let mut app = app(Tool::ColorReplacement); + let n = steps(&app); + for letter in [Key::S, Key::C, Key::O] { + key(&mut app, &ctx, letter, SHIFT_ALT); + assert_eq!((layer_blend(&app), steps(&app)), (BlendMode::Normal, n), "{letter:?}"); + } +} + +#[test] +fn several_selected_layers_still_do_nothing() { + // Not measured: with more than one layer selected the letters keep today's behaviour. + let ctx = egui::Context::default(); + let mut app = app(Tool::ColorReplacement); + let first = app.session.active().unwrap().active_layer.unwrap(); + app.run("layer.new.layer", json!({})).unwrap(); + app.run("layer.select", json!({"layer": first.0, "mode": "add"})).unwrap(); + assert_eq!(app.session.active().unwrap().selected_layers().len(), 2); + let n = steps(&app); + key(&mut app, &ctx, Key::M, SHIFT_ALT); + let st = app.session.active().unwrap(); + let blends: Vec = st.selected_layers().into_iter().map(|id| st.doc.layer(id).unwrap().blend).collect(); + assert_eq!((blends, steps(&app)), (vec![BlendMode::Normal; 2], n)); +} diff --git a/crates/ui-egui/src/input/blend_keys.rs b/crates/ui-egui/src/input/blend_keys.rs index 9ea13d6a19..fec117dac2 100644 --- a/crates/ui-egui/src/input/blend_keys.rs +++ b/crates/ui-egui/src/input/blend_keys.rs @@ -2,7 +2,10 @@ //! letter picks one (⇧⌥M Multiply, ⇧⌥S Screen…). With a painting tool that has a Mode of its own //! (`tool_modes`: Brush, Pencil, Paint Bucket, Gradient, the stamps, History Brush, Healing) they //! set that tool's mode within its menu and never the layer's. With a tool that has no blend mode -//! (Move, the selection tools, the Erasers…) they set every selected layer's mode. +//! (Move, the selection tools, the Erasers…) they set every selected layer's mode. With the Color +//! Replacement tool the ⇧⌥ letters for Normal, Multiply, Hue and Luminosity set the active layer's +//! mode too and its own Mode stays, as measured in the reference application (#11928); its other +//! letters, the letters with several layers selected, and ⇧+ / ⇧− do nothing. //! The step list wraps at either end. Each press is one call of an engine command //! (`tools.setBrush`, `layer.setProps` with every target layer), so it is one undo step. @@ -149,6 +152,10 @@ enum Target { /// nothing, and never reach the layer. Nothing, Layers, + /// The ⇧⌥ letters for these modes set the active layer's mode while it is the only one + /// selected; the other letters, several selected layers and ⇧+ / ⇧− do nothing (the Color + /// Replacement tool, #11928: only these letters on one layer were measured). + LayerLetters(&'static [BlendMode]), } fn target(tool: Tool) -> Target { @@ -156,7 +163,9 @@ fn target(tool: Tool) -> Target { return Target::Tool(modes); } match tool { - Tool::SpotHealing | Tool::ColorReplacement | Tool::Blur | Tool::Sharpen | Tool::Smudge | Tool::ArtHistoryBrush => Target::Nothing, + Tool::SpotHealing | Tool::Blur | Tool::Sharpen | Tool::Smudge | Tool::ArtHistoryBrush => Target::Nothing, + // Its ⇧⌥ letters set the active layer's mode and leave the tool's own Mode (#11928). + Tool::ColorReplacement => Target::LayerLetters(&[BlendMode::Normal, BlendMode::Multiply, BlendMode::Hue, BlendMode::Luminosity]), // Photoshop's ⇧⌥S/M/H pick the Dodge and Burn Range and ⇧⌥D/S the Sponge's mode: never // the layer's. Tool::Dodge | Tool::Burn | Tool::Sponge => Target::Nothing, @@ -251,7 +260,7 @@ pub fn press(app: &mut PhotocraftApp, step: i32) { set_tool_mode(app, mode); } } - Target::Nothing => {} + Target::Nothing | Target::LayerLetters(_) => {} Target::Layers => { let Some(st) = app.session.active() else { return }; let Some(layer) = st.active_layer.and_then(|id| st.doc.layer(id)) else { return }; @@ -298,6 +307,13 @@ pub fn pick_mode(app: &mut PhotocraftApp, pick: Pick) { set_layers_mode(app, m); } } + Target::LayerLetters(modes) => { + // Only one selected layer was measured; several keep today's (nothing). + let one = app.session.active().is_some_and(|st| st.selected_layers().len() == 1); + if let Some(m) = want.filter(|m| one && modes.contains(m)) { + set_layers_mode(app, m); + } + } _ => {} } } @@ -309,6 +325,10 @@ mod tests; #[path = "../brush_behind_key_tests.rs"] mod brush_behind_key_tests; +#[cfg(test)] +#[path = "../color_replacement_blend_keys_tests.rs"] +mod color_replacement_blend_keys_tests; + #[cfg(test)] #[path = "../brush_blend_keys_behind_tests.rs"] mod brush_blend_keys_behind_tests;