diff --git a/crates/engine-core/src/cmd/layer_multi_cmds.rs b/crates/engine-core/src/cmd/layer_multi_cmds.rs index 7c88592d9b..e06fb798d0 100644 --- a/crates/engine-core/src/cmd/layer_multi_cmds.rs +++ b/crates/engine-core/src/cmd/layer_multi_cmds.rs @@ -157,7 +157,7 @@ pub fn specs() -> Vec { "Group from Layers…", &["Layer", "New"], None, - r##"{"layer":id?,"name":text?,"color":"none|red|orange|yellow|green|seafoam|blue|indigo|magenta|fuchsia|violet|gray"="none","mode":str="passThrough" (blend mode),"opacity":0..100=100 (percent)} (no layer: every selected layer; the new group starts collapsed)"##, + r##"{"layer":id?,"name":text?,"color":"none|red|orange|yellow|green|seafoam|blue|indigo|magenta|fuchsia|violet|gray"="none","mode":str="passThrough" (blend mode),"opacity":0..100=100 (percent)} (no layer: every selected layer; the new group starts expanded with one target and collapsed with multiple targets)"##, structure::can_group_from_layers, structure::group_from_layers ), @@ -192,3 +192,6 @@ mod group_from_layers_clipping_tests; #[cfg(test)] mod merge_layers_history_match_tests; + +#[cfg(test)] +mod group_from_layers_one_layer_expanded_tests; diff --git a/crates/engine-core/src/cmd/layer_multi_cmds/group_from_layers_one_layer_expanded_tests.rs b/crates/engine-core/src/cmd/layer_multi_cmds/group_from_layers_one_layer_expanded_tests.rs new file mode 100644 index 0000000000..31dfdb6cb0 --- /dev/null +++ b/crates/engine-core/src/cmd/layer_multi_cmds/group_from_layers_one_layer_expanded_tests.rs @@ -0,0 +1,104 @@ +use super::*; + +const GROUP_FROM_LAYERS: &str = "layer.new.groupFromLayers"; + +fn session() -> Session { + let mut s = crate::session(); + s.execute("file.new", json!({"width": 32, "height": 32})).unwrap(); + s +} + +fn create(s: &mut Session, command: &str, name: &str) -> LayerId { + let reply = s.execute(command, json!({"name": name})).unwrap(); + LayerId(reply["layer"].as_u64().unwrap()) +} + +fn inspect_layer(layers: &Value, id: LayerId) -> Option<&Value> { + layers.as_array()?.iter().find_map(|layer| if layer["id"] == id.0 { Some(layer) } else { inspect_layer(&layer["children"], id) }) +} + +fn assert_group(s: &mut Session, group: LayerId, expanded: bool, children: &[LayerId]) { + let state = s.execute("document.inspect", json!({})).unwrap(); + let inspected = inspect_layer(&state["layers"], group).expect("new group is in the layer tree"); + assert_eq!(inspected["expanded"], expanded); + let ids: Vec<_> = inspected["children"].as_array().unwrap().iter().rev().map(|layer| LayerId(layer["id"].as_u64().unwrap())).collect(); + assert_eq!(ids, children); +} + +#[test] +fn grouping_one_layer_or_group_starts_expanded_in_a_single_history_step() { + for command in ["layer.new.layer", "layer.new.group"] { + let mut s = session(); + let child = create(&mut s, command, "Child"); + let before = s.active().unwrap().history.past_len(); + let group = create(&mut s, GROUP_FROM_LAYERS, "Wrapper"); + assert_group(&mut s, group, true, &[child]); + assert_eq!(s.active().unwrap().history.past_len(), before + 1); + s.execute("edit.undo", json!({})).unwrap(); + let doc = &s.active().unwrap().doc; + assert!(doc.layer(group).is_none()); + assert!(doc.layers.iter().any(|layer| layer.id == child)); + } +} + +#[test] +fn grouping_one_nested_layer_starts_expanded_with_either_parent_lock_state() { + for locked in [false, true] { + let mut s = session(); + let child = create(&mut s, "layer.new.layer", "Child"); + let parent = create(&mut s, "layer.new.group", "Parent"); + s.execute("layer.moveTo", json!({"layer": child.0, "target": parent.0, "position": "into"})).unwrap(); + s.execute("layer.select", json!({"layer": parent.0})).unwrap(); + s.execute("layer.lockLayers", json!({"all": locked})).unwrap(); + s.execute("layer.select", json!({"layer": child.0})).unwrap(); + + let group = create(&mut s, GROUP_FROM_LAYERS, "Inner"); + assert_group(&mut s, group, true, &[child]); + let doc = &s.active().unwrap().doc; + assert_eq!(doc.layers.index().parent(group), Some(parent)); + assert_eq!(doc.layer(parent).unwrap().locks.all, locked); + s.execute("edit.undo", json!({})).unwrap(); + assert_eq!(s.active().unwrap().doc.layers.index().parent(child), Some(parent)); + } +} + +#[test] +fn a_multi_selection_starts_collapsed_and_an_explicit_target_starts_expanded() { + let mut s = session(); + let a = create(&mut s, "layer.new.layer", "A"); + let b = create(&mut s, "layer.new.layer", "B"); + s.execute("layer.select", json!({"layers": [a.0, b.0]})).unwrap(); + let group = create(&mut s, GROUP_FROM_LAYERS, "Pair"); + assert_group(&mut s, group, false, &[a, b]); + s.execute("edit.undo", json!({})).unwrap(); + + s.execute("layer.select", json!({"layers": [a.0, b.0]})).unwrap(); + let reply = s.execute(GROUP_FROM_LAYERS, json!({"layer": a.0})).unwrap(); + let explicit = LayerId(reply["layer"].as_u64().unwrap()); + assert_group(&mut s, explicit, true, &[a]); + + s.execute("layer.select", json!({"layers": [explicit.0, a.0]})).unwrap(); + let wrapper = create(&mut s, GROUP_FROM_LAYERS, "Ancestor and Child"); + assert_group(&mut s, wrapper, false, &[explicit]); +} + +#[test] +fn ordinary_group_layers_still_starts_collapsed_for_one_layer() { + let mut s = session(); + let child = create(&mut s, "layer.new.layer", "Child"); + let group = create(&mut s, "layer.groupLayers", "Wrapper"); + assert_group(&mut s, group, false, &[child]); +} + +#[test] +fn one_selected_base_starts_expanded_with_its_clipped_layer() { + let mut s = session(); + let base = create(&mut s, "layer.new.layer", "Base"); + let clipped = create(&mut s, "layer.new.layer", "Clipped"); + s.execute("layer.setProps", json!({"layer": clipped.0, "clipped": true})).unwrap(); + s.execute("layer.select", json!({"layer": base.0})).unwrap(); + + let group = create(&mut s, GROUP_FROM_LAYERS, "Wrapper"); + assert_group(&mut s, group, true, &[base, clipped]); + assert!(s.active().unwrap().doc.layer(clipped).unwrap().clipped); +} diff --git a/crates/engine-core/src/cmd/layer_multi_cmds/structure.rs b/crates/engine-core/src/cmd/layer_multi_cmds/structure.rs index 63ab05661f..ab63541aaa 100644 --- a/crates/engine-core/src/cmd/layer_multi_cmds/structure.rs +++ b/crates/engine-core/src/cmd/layer_multi_cmds/structure.rs @@ -122,9 +122,7 @@ pub(super) fn can_group_from_layers(s: &Session) -> std::result::Result<(), Stri Ok(()) } -/// Group from Layers: [`group_layers`], refused with the Background among the layers it would -/// move, also when the `layer` param names it (#14709). Group Layers (⌘G) is not measured and -/// keeps grouping it. +/// Group from Layers opens a group made from one target and refuses to group the Background (#14709). pub(super) fn group_from_layers(s: &mut Session, p: &Value) -> Result { let ids = match crate::commands::explicit_layer(s, p)? { Some(id) => vec![id], @@ -134,17 +132,21 @@ pub(super) fn group_from_layers(s: &mut Session, p: &Value) -> Result { if ids.iter().any(|id| doc.is_background(*id)) { return Err(EngineError::Other("the Background layer can't go into a group".into())); } - group_layers(s, p) + group_layers_with(s, p, &ids, ids.len() == 1) } -/// Group Layers (⌘G) / Group from Layers: the explicit layer, or every selected layer, moves -/// into a new, collapsed group placed where the top-most of them was. Bottom-to-top order is -/// preserved. The New Group dialog's colour, mode and opacity are set in the same history step. +/// Group Layers (⌘G) moves the explicit layer or the selection into a new, collapsed group. pub fn group_layers(s: &mut Session, p: &Value) -> Result { let ids = match crate::commands::explicit_layer(s, p)? { Some(id) => vec![id], None => selected(s), }; + group_layers_with(s, p, &ids, false) +} + +/// Creates the group at the topmost target, preserving layer order and applying the dialog's +/// options in the same history step. +fn group_layers_with(s: &mut Session, p: &Value, ids: &[LayerId], expanded: bool) -> Result { if ids.is_empty() { return Err(EngineError::Other("no layer selected".into())); } @@ -153,7 +155,7 @@ pub fn group_layers(s: &mut Session, p: &Value) -> Result { // A base takes the layers clipped to it into the group, still clipped, whether they were // selected or not; a clipped layer that goes in without its base is released, as in the // reference application (#14713). - let mut ids = top_level(doc, &ids); + let mut ids = top_level(doc, ids); let bases: Vec = ids.clone(); for id in bases { ids.extend(crate::clip_group::clipped_to(doc, id)); @@ -170,10 +172,9 @@ pub fn group_layers(s: &mut Session, p: &Value) -> Result { } let top = *ids.last().ok_or(EngineError::NoLayer(LayerId(0)))?; let name = opts.name.clone().unwrap_or_else(|| doc.next_layer_name("Group")); - // Photoshop creates the group collapsed, so the panel shows one row in place of the layers. let mut group = Layer::group(name, vec![]); if let LayerContent::Group(g) = &mut group.content { - g.expanded = false; + g.expanded = expanded; } let gid = doc.insert_above(Some(top), group); let mut children = Vec::with_capacity(ids.len()); diff --git a/crates/ui-egui/src/canvas/paint_tool.rs b/crates/ui-egui/src/canvas/paint_tool.rs index d3ce9b3552..0d7ba055f5 100644 --- a/crates/ui-egui/src/canvas/paint_tool.rs +++ b/crates/ui-egui/src/canvas/paint_tool.rs @@ -373,7 +373,7 @@ impl CanvasTool for PaintTool<'_> { scope.with_app(|app| { crate::brush_cursor::sync_tip(app); // The readout request lasts one pass: a pass without pointer input repeats it. - app.brush_resize.hud(cx); + app.brush_resize.hud(app, cx); let Some(stroke) = state.stroke.as_ref() else { return }; let stale_mixer = stroke.tool == Tool::MixerBrush && app.live_stroke.is_some() && app.session.active_index().is_none_or(|idx| live_stroke(app, idx).is_none()); diff --git a/crates/ui-egui/src/tools/brush_resize.rs b/crates/ui-egui/src/tools/brush_resize.rs index d5c19b96fd..ec6dfff9e4 100644 --- a/crates/ui-egui/src/tools/brush_resize.rs +++ b/crates/ui-egui/src/tools/brush_resize.rs @@ -29,6 +29,10 @@ const HARDNESS_TRAVEL: f32 = 400.0; const OPACITY_TRAVEL: f32 = 200.0; /// The overlay alpha of the preview tint. const TINT_ALPHA: u8 = 110; +/// The Background Eraser's third readout row: the reference shows "Opacity : 50 %" there, the +/// tool's Tolerance after Reset Tool and not an opacity the tool has (#18053). Unmeasured for other +/// Tolerance values, so the row stays at 50 until they are (#18053). +const BACKGROUND_ERASER_READOUT_OPACITY: f64 = 50.0; /// The brush resize drag of the painting tools, kept in `PhotocraftApp::brush_resize`. The painting tool takes it /// out of the app for each call, so its methods get the app as an argument. @@ -98,7 +102,7 @@ impl PaintResize { return false; } self.key += 1; - self.hud(cx); + self.hud(app, cx); true } @@ -108,7 +112,7 @@ impl PaintResize { let Some(space) = cx.space() else { return }; let step = self.resize.track(&space.view, pointer.pos); self.apply(app, step); - self.hud(cx); + self.hud(app, cx); } /// The button came up: the last values stay. @@ -126,13 +130,20 @@ impl PaintResize { /// Asks for the diameter and level readout beside the pointer while a resize runs. `hud`, not `readout`: Show /// Transformation Values never hides it. The request lasts one pass, so the tool repeats it every pass. - pub(crate) fn hud(&self, cx: &mut ToolContext<'_>) { + /// The Background Eraser gets a third row, Opacity (#18053). + pub(crate) fn hud(&self, app: &PhotocraftApp, cx: &mut ToolContext<'_>) { if !self.resize.active() { return; } let words = cx.words::(); - let rows = self.resize.readout_rows(&cx.units(), &words); + let mut rows = self.resize.readout_rows(&cx.units(), &words); + // The reference's third row is there beside a Hardness row; with "Vary Round Brush Hardness on HUD" off the + // level row is already Opacity, and a second one would repeat it. + if app.ui.tool == Tool::BackgroundEraser && varies_hardness(app) { + let opacity = cx.units().percent(BACKGROUND_ERASER_READOUT_OPACITY); + rows.push((words.opacity.clone(), opacity)); + } cx.hud(rows); } @@ -175,6 +186,10 @@ impl PaintResize { #[cfg(test)] mod tests; +#[cfg(test)] +#[path = "brush_resize_background_eraser_opacity_row_tests.rs"] +mod background_eraser_opacity_row_tests; + #[cfg(test)] #[path = "../brush_resize_hardness_rate_tests.rs"] mod brush_resize_hardness_rate_tests; diff --git a/crates/ui-egui/src/tools/brush_resize_background_eraser_opacity_row_tests.rs b/crates/ui-egui/src/tools/brush_resize_background_eraser_opacity_row_tests.rs new file mode 100644 index 0000000000..5551976baf --- /dev/null +++ b/crates/ui-egui/src/tools/brush_resize_background_eraser_opacity_row_tests.rs @@ -0,0 +1,116 @@ +//! #18053: the Background Eraser's Ctrl+Alt+drag (Alt+right-drag) readout has a third row, +//! "Opacity : 50 %", after Diameter and Hardness, as the reference application does. The row does +//! not follow the drag or the tool's Tolerance (not measured), and the gesture still changes only +//! the diameter and the hardness, erases nothing and adds no history state. + +use serde_json::json; + +use super::*; + +use crate::canvas::{ToolEvent, testing}; + +/// A 200 x 100 white document at 100 % zoom with `tool` active and a 40 px, `hardness`, 100 % opaque +/// brush. The window is made a Mac's: off macOS the host reports Ctrl as the command key, and the +/// Ctrl+Alt chord needs Ctrl its own key. +fn app(tool: Tool, hardness: f32) -> Box { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default()); + app.run("file.new", json!({"width": 200, "height": 100, "background": "white"})).unwrap(); + app.run("tools.setBrush", json!({"brush": {"size": 40, "hardness": hardness, "opacity": 1.0}})).unwrap(); + app.ui.tool = tool; + app.sync_views(); + testing::on_a_mac(&app); + app +} + +fn chord() -> egui::Modifiers { + egui::Modifiers { alt: true, ctrl: true, ..Default::default() } +} + +/// The rows the readout asked for at `ev`, as (label, value) pairs. +fn readout(app: &mut PhotocraftApp, ev: ToolEvent) -> Vec<(String, String)> { + crate::canvas::tool_event_response(app, ev, chord()) + .readout + .map(|r| r.rows.into_iter().map(|(label, value)| (label.as_str().to_owned(), value)).collect()) + .unwrap_or_default() +} + +fn values(rows: &[(String, String)]) -> Vec<&str> { + rows.iter().map(|(_, value)| value.as_str()).collect() +} + +/// Done when 1: after Reset Tool's brush, three rows in the order Diameter, Hardness, Opacity, and +/// the Opacity row reads 50 %. +#[test] +fn the_background_eraser_readout_has_a_fifty_percent_opacity_row() { + let mut a = app(Tool::BackgroundEraser, 1.0); + let got = readout(&mut a, ToolEvent::Down { x: 100.0, y: 50.0, pressure: 1.0 }); + assert_eq!( + got, + vec![("Diameter:".to_owned(), "40 px".to_owned()), ("Hardness:".to_owned(), "100%".to_owned()), ("Opacity:".to_owned(), "50%".to_owned()),], + "three rows, in the reference's order" + ); + readout(&mut a, ToolEvent::Up { x: 100.0, y: 50.0 }); +} + +/// Done when 2: horizontal and vertical movement leave the Opacity row at 50 %, the diameter and +/// the hardness following their own axes. +#[test] +fn the_opacity_row_stays_at_fifty_percent_through_the_drag() { + let mut a = app(Tool::BackgroundEraser, 0.5); + readout(&mut a, ToolEvent::Down { x: 100.0, y: 50.0, pressure: 1.0 }); + let right = readout(&mut a, ToolEvent::Move { x: 130.0, y: 50.0, pressure: 1.0 }); + assert_eq!(values(&right), vec!["100 px", "50%", "50%"], "rightward: a wider tip, the same rows"); + // 400 screen points take the hardness from 0 % to 100 %, so 20 points down add 5 %. + let down = readout(&mut a, ToolEvent::Move { x: 130.0, y: 70.0, pressure: 1.0 }); + assert_eq!(values(&down), vec!["100 px", "55%", "50%"], "downward: a softer tip, the opacity row unchanged"); + let up = readout(&mut a, ToolEvent::Move { x: 130.0, y: 30.0, pressure: 1.0 }); + assert_eq!(values(&up), vec!["100 px", "45%", "50%"], "upward: harder, the opacity row unchanged"); + readout(&mut a, ToolEvent::Up { x: 130.0, y: 30.0 }); +} + +/// Done when 3: the row stays at 50 % for another Tolerance. The 50 is the Tolerance after Reset +/// Tool, but the reference's row was not measured for other values, so it does not follow it yet. +#[test] +fn another_tolerance_leaves_the_row_at_fifty_percent() { + let mut a = app(Tool::BackgroundEraser, 1.0); + a.ui.tool_options.bg_tolerance = 85.0; + let got = readout(&mut a, ToolEvent::Down { x: 100.0, y: 50.0, pressure: 1.0 }); + assert_eq!(values(&got), vec!["40 px", "100%", "50%"], "Tolerance 85 %: the row still reads 50 %"); + readout(&mut a, ToolEvent::Up { x: 100.0, y: 50.0 }); +} + +/// Done when 2: the gesture changes only the diameter and the hardness, erases nothing and adds no +/// history state. +#[test] +fn the_gesture_erases_nothing_and_adds_no_history_state() { + let mut a = app(Tool::BackgroundEraser, 0.5); + let history = a.session.active().unwrap().history.entries().len(); + readout(&mut a, ToolEvent::Down { x: 100.0, y: 50.0, pressure: 1.0 }); + readout(&mut a, ToolEvent::Move { x: 130.0, y: 70.0, pressure: 1.0 }); + readout(&mut a, ToolEvent::Up { x: 130.0, y: 70.0 }); + + assert_ne!(a.session.tools.brush.size, 40.0, "the diameter changed"); + assert!(a.session.tools.brush.hardness > 0.5, "the hardness changed"); + assert_eq!(a.session.tools.brush.opacity, 1.0, "the brush's own opacity did not"); + let st = a.session.active().unwrap(); + assert_eq!(st.history.entries().len(), history, "no history state"); + let surface = st.doc.layer(st.active_layer.unwrap()).unwrap().surface().unwrap(); + assert_eq!(surface.pixel(100, 50)[3], 1.0, "the canvas was not erased"); +} + +/// Done when 1: every other painting tool keeps the framework's two rows, and with "Vary Round +/// Brush Hardness on HUD" off the Background Eraser's second row is already Opacity, so no third row +/// repeats it. +#[test] +fn only_the_background_eraser_with_the_hardness_row_gets_the_third_row() { + let mut pencil = app(Tool::Pencil, 1.0); + let got = readout(&mut pencil, ToolEvent::Down { x: 100.0, y: 50.0, pressure: 1.0 }); + assert_eq!(values(&got), vec!["40 px", "100%"], "the Pencil keeps two rows"); + readout(&mut pencil, ToolEvent::Up { x: 100.0, y: 50.0 }); + + let mut eraser = app(Tool::BackgroundEraser, 1.0); + eraser.run("prefs.set", json!({"path": "tools.varyRoundBrushHardnessOnHud", "value": false})).unwrap(); + let got = readout(&mut eraser, ToolEvent::Down { x: 100.0, y: 50.0, pressure: 1.0 }); + assert_eq!(values(&got), vec!["40 px", "100%"], "the level row is Opacity, and there is no second one"); + readout(&mut eraser, ToolEvent::Up { x: 100.0, y: 50.0 }); +}