diff --git a/crates/engine/src/cmd/edit_menu_cmds.rs b/crates/engine/src/cmd/edit_menu_cmds.rs index f7603d2908e..cef435e46af 100644 --- a/crates/engine/src/cmd/edit_menu_cmds.rs +++ b/crates/engine/src/cmd/edit_menu_cmds.rs @@ -74,9 +74,17 @@ pub(crate) fn after_command(s: &mut Session, id: &str, params: &Value) { } let Some(st) = s.active() else { return }; let label = st.history.undo_label().unwrap_or(id).to_string(); + let prev = st.history.state(st.history.past_len().wrapping_sub(1)); + let prev = prev.as_deref(); + let surf_eq = |a: Option<&Surface>, b: Option<&Surface>| match (a, b) { + (Some(a), Some(b)) => a.format() == b.format() && a.default_pixel() == b.default_pixel() && crate_fingerprint(a) == crate_fingerprint(b), + (None, None) => true, + _ => false, + }; // An alpha channel or the Quick Mask took the edit instead of the layer when the command ran // on it (#7382): its explicit `target`, or the one the Channels panel gave it. Only commands - // routed to channels edit them; the others edit the layer whatever `target` says. + // routed to channels edit them, and some routed ones (Edit › Stroke, the Magic and Background + // Erasers) still edit the layer whatever `target` says, so the channel must have changed. let run = crate::channel_cmds::inject_target(s, id, params.clone()); let channel = match crate::channel_cmds::target_of(&run) { _ if !crate::channel_cmds::routed(id) => None, @@ -84,21 +92,23 @@ pub(crate) fn after_command(s: &mut Session, id: &str, params: &Value) { Target::QuickMask if st.doc.quick_mask.is_some() => Some(FadeTarget::QuickMask), _ => None, }; + fn channel_surface<'a>(doc: &'a Document, target: &FadeTarget) -> Option<&'a Surface> { + match target { + FadeTarget::Channel(i) => doc.channels.get(*i).map(|c| &c.surface), + _ => doc.quick_mask.as_ref().map(|c| &c.surface), + } + } + let channel = channel.filter(|target| prev.is_none_or(|prev| !surf_eq(channel_surface(&st.doc, target), channel_surface(prev, target)))); if let Some(target) = channel { s.edit_state.fade = Some(FadeSource { doc: st.doc.id, revision: st.revision, target, label }); return; } let Some(layer) = st.active_layer else { return }; let mask = st.doc.layer(layer).is_some_and(|l| l.mask.is_some()) - && st.history.state(st.history.past_len().wrapping_sub(1)).is_some_and(|prev| { + && prev.is_some_and(|prev| { // The mask changed and the pixels didn't: the command painted the mask. let now = st.doc.layer(layer); let was = prev.layer(layer); - let surf_eq = |a: Option<&Surface>, b: Option<&Surface>| match (a, b) { - (Some(a), Some(b)) => a.format() == b.format() && a.default_pixel() == b.default_pixel() && crate_fingerprint(a) == crate_fingerprint(b), - (None, None) => true, - _ => false, - }; surf_eq(now.and_then(|l| l.surface()), was.and_then(|l| l.surface())) && !surf_eq(now.and_then(|l| l.mask.as_ref().map(|m| &m.surface)), was.and_then(|l| l.mask.as_ref().map(|m| &m.surface))) }); diff --git a/crates/engine/src/cmd/edit_menu_cmds/tests.rs b/crates/engine/src/cmd/edit_menu_cmds/tests.rs index 21d667ecce4..03cd97679f9 100644 --- a/crates/engine/src/cmd/edit_menu_cmds/tests.rs +++ b/crates/engine/src/cmd/edit_menu_cmds/tests.rs @@ -143,6 +143,36 @@ fn fade_follows_the_target_the_command_ran_on() { assert_eq!(s.active().unwrap().doc.channels[0].surface.pixel(5, 5)[0], 0.0, "the channel is left alone"); } +/// Edit › Stroke and the Magic Eraser follow the Channels panel's target but still edit the layer, +/// so with an alpha channel targeted Fade fades the layer they changed, not the untouched channel. +#[test] +fn fade_after_a_routed_command_that_edited_the_layer_fades_the_layer() { + for depth in [8, 16, 32] { + for command in ["edit.stroke", "paint.magicEraser"] { + let mut s = session(depth); + s.execute("layer.new.layer", json!({})).unwrap(); + s.execute("edit.fill", json!({"color": "#ffffff"})).unwrap(); + s.execute("channel.new", json!({"fill": "black"})).unwrap(); + s.execute("channel.target", json!({"channel": 0})).unwrap(); + let (x, y) = if command == "edit.stroke" { + s.execute("select.rect", json!({"x": 10, "y": 10, "width": 10, "height": 10})).unwrap(); + s.execute(command, json!({"width": 2, "color": "#000000", "location": "inside"})).unwrap(); + assert_eq!(px(&s, 10, 15)[0], 0.0, "{depth} {command}: the layer was stroked"); + (10, 15) + } else { + s.execute(command, json!({"x": 5, "y": 5, "antiAlias": false})).unwrap(); + assert_eq!(px(&s, 5, 5)[3], 0.0, "{depth} {command}: the layer was erased"); + (5, 5) + }; + assert!(matches!(s.edit_state.fade.as_ref().map(|f| f.target), Some(FadeTarget::Pixels(_))), "{depth} {command}"); + s.execute("edit.fade", json!({"opacity": 50})).unwrap(); + let k = if command == "edit.stroke" { 0 } else { 3 }; + assert!((px(&s, x, y)[k] - 0.5).abs() < 0.01, "{depth} {command}: the layer faded {:?}", px(&s, x, y)); + assert_eq!(s.active().unwrap().doc.channels[0].surface.pixel(x, y)[0], 0.0, "{depth} {command}: the channel is left alone"); + } + } +} + /// A command that only edits layers (Auto Tone) fades the layer, even given a channel `target`. #[test] fn fade_ignores_a_target_the_command_doesnt_take() { diff --git a/crates/engine/src/color_channels/tone.rs b/crates/engine/src/color_channels/tone.rs index 23100f31acc..b8114a17912 100644 --- a/crates/engine/src/color_channels/tone.rs +++ b/crates/engine/src/color_channels/tone.rs @@ -206,7 +206,8 @@ fn output(tone: f32, adjusted: f32, weight: f32, component: &Component, fmt: Pix let value = if component.lab_invert { tone + (256.0 / 255.0 - tone.clamp(0.0, 1.0) - tone) * weight } else { - photocraft_color::convert::rgb_to_gray([tone + (adjusted - tone) * weight; 3]) + // As `pixels::mix_through_selection`: `o·(1−k) + a·k`, exact at full coverage (#9483). + photocraft_color::convert::rgb_to_gray([tone * (1.0 - weight) + adjusted * weight; 3]) }; let mut encoded = [0; 4]; write_sample(&mut encoded, fmt.sample, 0, value); diff --git a/crates/ui-egui/src/app/commands.rs b/crates/ui-egui/src/app/commands.rs index 5645693e4b6..7ff12ccbbd2 100644 --- a/crates/ui-egui/src/app/commands.rs +++ b/crates/ui-egui/src/app/commands.rs @@ -236,6 +236,7 @@ impl PhotocraftApp { // Adding a mask selects its thumbnail: the next brush or footer Delete targets it. self.ui.mask_target = true; self.ui.vector_mask_target = false; + self.target_mask_over_colour_rows(); } if result.is_ok() && creates_active_vector_mask { // Likewise a new vector mask (#4728): Properties shows its Density and Feather, and a diff --git a/crates/ui-egui/src/app/targets.rs b/crates/ui-egui/src/app/targets.rs index 6e5c14473e5..1d40cdd570e 100644 --- a/crates/ui-egui/src/app/targets.rs +++ b/crates/ui-egui/src/app/targets.rs @@ -109,6 +109,22 @@ impl PhotocraftApp { self.sync_mask_targets(); } + /// Targeting a pixel layer's mask (its thumbnail, or a mask just added to it) takes the + /// Channels panel off colour rows left selected and onto the mask, as in Photoshop, so tools, + /// menu commands and the engine all edit the mask. A view change only: no command, no step. + pub(crate) fn target_mask_over_colour_rows(&mut self) { + use photocraft_engine::channel_cmds::ChannelTarget; + let Some(st) = self.session.active_mut() else { return }; + let raster_with_mask = + st.active_layer.and_then(|id| st.doc.layer(id)).is_some_and(|l| l.mask.is_some() && matches!(l.content, photocraft_doc::LayerContent::Raster(_))); + if matches!(st.channel_view.target, ChannelTarget::Color(_)) && photocraft_engine::mask_view_cmds::current(st).is_none() && raster_with_mask { + let v = &mut st.channel_view; + v.target = ChannelTarget::LayerMask; + v.selected_colors.clear(); + v.color_hidden.clear(); + } + } + /// Viewing a layer mask (#196) targets it; a vector-mask target needs a vector mask on the /// active layer (a shape layer's path is its content, not a mask). Targeting a mask or the /// pixels brings back that target's foreground/background pair, as in Photoshop (#2166). diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rs index 7fb5fba768c..33394450704 100644 --- a/crates/ui-egui/src/canvas.rs +++ b/crates/ui-egui/src/canvas.rs @@ -2688,6 +2688,99 @@ mod multi_channel_ui_tests { } } } + /// The colour rows stay selected when another layer becomes active. An adjustment layer has no + /// pixels, so the Brush paints its mask, as it does without colour rows selected. + #[test] + fn colour_rows_left_selected_still_paint_an_adjustment_layers_mask() { + for colors in [vec![0], vec![0, 2]] { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.run("file.new", json!({"width":64,"height":64})).unwrap(); + app.run("channel.target", json!({"colors":colors})).unwrap(); + app.run("layer.newAdjustmentLayer.levels", json!({})).unwrap(); + app.sync_views(); + assert_eq!(paint_target(&app), json!("mask"), "{colors:?}"); + app.run("tools.setColors", json!({"foreground":[0.0,0.0,0.0,1.0]})).unwrap(); + app.run("tools.setBrush", json!({"brush":{"size":12,"hardness":1.0,"smoothing":{"amount":0.0}}})).unwrap(); + app.ui.tool = Tool::Brush; + let st = app.session.active().unwrap(); + let layer = st.active_layer.unwrap(); + let steps = st.history.past_len(); + tool_event(&mut app, ToolEvent::Down { x: 12.0, y: 32.0, pressure: 1.0 }, egui::Modifiers::NONE); + tool_event(&mut app, ToolEvent::Move { x: 32.0, y: 32.0, pressure: 1.0 }, egui::Modifiers::NONE); + tool_event(&mut app, ToolEvent::Up { x: 32.0, y: 32.0 }, egui::Modifiers::NONE); + let st = app.session.active().unwrap(); + assert_eq!(st.history.past_len(), steps + 1, "{colors:?}: one stroke"); + assert!(st.doc.layer(layer).unwrap().mask.as_ref().unwrap().surface.sample_channel(28, 32, 0) < 0.1, "{colors:?}: the mask was painted"); + } + } + + /// Targeting a layer mask with colour rows selected (its thumbnail, or adding the mask) moves + /// the Channels panel onto the mask, as in Photoshop: tools, dialogs and menu commands then + /// all edit the mask. Several rows still stop the Move tool on a layer that needs + /// rasterizing, which paints only after the prompt. + #[test] + fn targeting_a_mask_over_colour_rows_edits_the_mask_everywhere() { + let ctx = egui::Context::default(); + let set = |app: &mut PhotocraftApp, p: serde_json::Value| { + let (req, _) = crate::control::ControlRequest::new("ui.set", p); + assert!(matches!(crate::control::handle(app, &ctx, &req), crate::control::Outcome::Done(v) if v["ok"] != false)); + }; + for colors in [vec![0], vec![0, 2]] { + for add in [false, true] { + let mut app = self::app(); + if !add { + app.run("layer.layerMask.revealAll", json!({})).unwrap(); + set(&mut app, json!({"maskTarget": false})); + } + app.run("channel.target", json!({"colors":colors})).unwrap(); + app.sync_views(); + assert!(paint_target(&app).get("colors").is_some(), "{colors:?}: the rows are the target first"); + let journal = app.session.journal.len(); + if add { + app.run("layer.layerMask.revealAll", json!({})).unwrap(); + } else { + set(&mut app, json!({"maskTarget": true})); + } + app.sync_views(); + assert_eq!(app.session.journal.len(), journal + usize::from(add), "{colors:?} add={add}: no extra recorded step"); + assert_eq!(paint_target(&app), json!("mask"), "{colors:?} add={add}"); + assert!(app.layer_mask_targeted(), "{colors:?} add={add}"); + assert_eq!(app.with_mask_target("filter.blur.gaussianBlur", json!({}))["target"], "mask"); + assert!(crate::distort_ui::colour_set_target(&app).is_null()); + assert!(!multiple_color_target(&app)); + // Clicking a colour row again takes the rows back. + app.run("channel.target", json!({"colors":colors})).unwrap(); + app.sync_views(); + assert!(paint_target(&app).get("colors").is_some(), "{colors:?} add={add}: the rows again"); + } + } + // A mask added to another layer, or a fill layer's mask, leaves the rows targeted. + let mut app = self::app(); + let active = app.session.active().unwrap().active_layer.unwrap(); + app.run("layer.new.layer", json!({})).unwrap(); + let other = app.session.active().unwrap().active_layer.unwrap(); + app.run("layer.select", json!({"layer": active.0})).unwrap(); + app.run("layer.layerMask.revealAll", json!({})).unwrap(); + app.run("channel.target", json!({"colors":[0,2]})).unwrap(); + app.run("layer.layerMask.revealAll", json!({"layer": other.0})).unwrap(); + app.sync_views(); + assert_eq!(paint_target(&app), json!({"colors":[0,2]}), "a mask on another layer"); + app.run("layer.newFillLayer.solidColor", json!({})).unwrap(); + app.run("channel.target", json!({"colors":[0,2]})).unwrap(); + set(&mut app, json!({"maskTarget": true})); + app.sync_views(); + assert!(matches!(app.session.active().unwrap().channel_view.target, photocraft_engine::channel_cmds::ChannelTarget::Color(_)), "a fill layer"); + let mut app = self::app(); + app.run("layer.smartObjects.convertToSmartObject", json!({})).unwrap(); + app.run("channel.target", json!({"colors":[0,2]})).unwrap(); + app.sync_views(); + assert_eq!(paint_target(&app), json!("pixels")); + assert!(crate::rasterize_prompt::needed(&app).is_some()); + assert!(multiple_color_target(&app)); + let refused = crate::liquify_ui::open(&mut app, &ctx).unwrap_err(); + assert!(refused.contains("Rasterize"), "{refused}"); + } + #[test] fn subset_does_not_block_guides_or_vector_path_transforms() { let mut app = app(); diff --git a/crates/ui-egui/src/canvas/strokes.rs b/crates/ui-egui/src/canvas/strokes.rs index 1538ff975fa..c7c6d4b643a 100644 --- a/crates/ui-egui/src/canvas/strokes.rs +++ b/crates/ui-egui/src/canvas/strokes.rs @@ -705,7 +705,11 @@ pub fn paint_target(app: &PhotocraftApp) -> serde_json::Value { if let Some(view) = photocraft_engine::mask_view_cmds::current(st) { return json!(if view.filter { "filterMask" } else { "mask" }); } - if let Some(colors) = photocraft_engine::color_channels::selected(&app.session) { + // A colour set edits a raster layer's own pixels. The rows stay selected across layer + // switches, so an adjustment layer's mask and a layer that needs rasterizing first + // keep the layer's own target below. + let raster = st.active_layer.and_then(|id| st.doc.layer(id)).is_some_and(|l| matches!(l.content, photocraft_doc::LayerContent::Raster(_))); + if raster && let Some(colors) = photocraft_engine::color_channels::selected(&app.session) { return colors.params(); } } @@ -723,6 +727,10 @@ pub fn paint_target(app: &PhotocraftApp) -> serde_json::Value { } /// Colour rows can remain selected behind a viewed mask; restrictions follow what is edited. +/// Several rows stop Move and transforms on any layer, which the engine refuses there too. pub(crate) fn multiple_color_target(app: &PhotocraftApp) -> bool { - paint_target(app).get("colors").and_then(serde_json::Value::as_array).is_some_and(|colors| colors.len() > 1) + let Some(st) = app.session.active() else { return false }; + matches!(st.channel_view.target, photocraft_engine::channel_cmds::ChannelTarget::Color(_)) + && photocraft_engine::mask_view_cmds::current(st).is_none() + && photocraft_engine::color_channels::selected(&app.session).is_some_and(|colors| colors.len() > 1) } diff --git a/crates/ui-egui/src/control/dispatch.rs b/crates/ui-egui/src/control/dispatch.rs index c2600f064a3..2151eb75a7c 100644 --- a/crates/ui-egui/src/control/dispatch.rs +++ b/crates/ui-egui/src/control/dispatch.rs @@ -586,6 +586,9 @@ impl PanelSet { app.ui.mask_target = m; app.ui.properties_layer = None; app.ui.vector_mask_target &= !m; + if m { + app.target_mask_over_colour_rows(); + } app.sync_mask_targets(); } if let Some(open) = self.updates_open { diff --git a/crates/ui-egui/src/dialogs/distort_ui.rs b/crates/ui-egui/src/dialogs/distort_ui.rs index 0118b5f1082..aa5c8fe4d9d 100644 --- a/crates/ui-egui/src/dialogs/distort_ui.rs +++ b/crates/ui-egui/src/dialogs/distort_ui.rs @@ -71,15 +71,43 @@ pub(crate) fn active_pixels(app: &PhotocraftApp) -> Result<(LayerId, Surface, bo } } +/// The colour set full-window filters capture, or Null when none is targeted: the command then +/// routes a targeted mask, filter mask, alpha channel or Quick Mask itself, as without colour sets. +pub(crate) fn colour_set_target(app: &PhotocraftApp) -> Value { + let target = crate::canvas::paint_target(app); + if target.get("colors").is_some() { target } else { Value::Null } +} + +/// `params` with the captured `target`, if any (see [`colour_set_target`]). +pub(crate) fn with_filter_target(mut params: Value, target: &Value) -> Value { + if !target.is_null() + && let Some(o) = params.as_object_mut() + { + o.insert("target".into(), target.clone()); + } + params +} + /// Full-window filters capture their layer target; scalar planes use ordinary filter dialogs. +/// Without a colour set they open on the layer's pixels or smart object, as before colour sets. pub(crate) fn active_filter_target(app: &PhotocraftApp) -> Result<(LayerId, Surface, Value), String> { + let target = colour_set_target(app); + if target.is_null() { + // Colour rows left selected on a smart object: the engine refuses the command, so refuse + // before any work is done in the workspace. + if let Some(st) = app.session.active() + && matches!(st.channel_view.target, photocraft_engine::channel_cmds::ChannelTarget::Color(_)) + && photocraft_engine::mask_view_cmds::current(st).is_none() + && st.active_layer.and_then(|id| st.doc.layer(id)).is_some_and(|l| matches!(l.content, LayerContent::Smart(_))) + { + return Err("Rasterize the layer before editing its colour channels".into()); + } + let (layer, surface, _) = active_pixels(app)?; + return Ok((layer, surface, Value::Null)); + } let st = app.session.active().ok_or("no document")?; let layer = st.active_layer.ok_or("no active layer")?; - let target = crate::canvas::paint_target(app); let params = serde_json::json!({"target":target}); - if target.as_str() != Some("pixels") && target.get("colors").is_none() { - return Err("This filter workspace requires layer pixels or native colour channels".into()); - } if photocraft_engine::color_channels::explicit(¶ms, photocraft_engine::channel_cmds::color_count(&st.doc)).map_err(|e| e.to_string())?.is_some() && !matches!(st.doc.layer(layer).map(|l| &l.content), Some(LayerContent::Raster(_))) { diff --git a/crates/ui-egui/src/dialogs/gallery_ui.rs b/crates/ui-egui/src/dialogs/gallery_ui.rs index aa699ade8c6..62be21930c9 100644 --- a/crates/ui-egui/src/dialogs/gallery_ui.rs +++ b/crates/ui-egui/src/dialogs/gallery_ui.rs @@ -118,7 +118,9 @@ impl GalleryDialog { /// The command params for the current stack. pub fn params(&self) -> Value { - json!({"effects": self.effects.iter().map(EffectLayer::to_json).collect::>(), "foreground": self.foreground, "background": self.background, "target":self.target}) + let p = + json!({"effects": self.effects.iter().map(EffectLayer::to_json).collect::>(), "foreground": self.foreground, "background": self.background}); + crate::distort_ui::with_filter_target(p, &self.target) } /// The progress (`0.0..=1.0`) of a preview still rendering. @@ -365,7 +367,7 @@ fn open_on( let sel = effects.len() - 1; let open_cats = std::array::from_fn(|i| effects[sel].filter.category() == GALLERY_CATEGORIES[i]); let history = EditHistory::new(&effects); - let target = if smart_filter.is_some() { json!("pixels") } else { crate::canvas::paint_target(app) }; + let target = if smart_filter.is_some() { json!("pixels") } else { crate::distort_ui::colour_set_target(app) }; app.distort.gallery = Some(GalleryDialog { document: st.doc.id, layer, @@ -635,7 +637,8 @@ fn update_thumbs(d: &mut GalleryDialog, ctx: &egui::Context, budget: usize) { ctx.request_repaint(); return; } - let p = json!({"effects": [{"filter": f.key()}], "foreground": d.foreground, "background": d.background,"target":d.target}); + let p = + crate::distort_ui::with_filter_target(json!({"effects": [{"filter": f.key()}], "foreground": d.foreground, "background": d.background}), &d.target); let out = render(&d.thumb_src, &p); let b = d.thumb_src.content_bounds(); let img = image(&out, b.x1.max(1) as usize, b.y1.max(1) as usize); diff --git a/crates/ui-egui/src/dialogs/liquify_ui.rs b/crates/ui-egui/src/dialogs/liquify_ui.rs index 7f10e03d207..464acb51e21 100644 --- a/crates/ui-egui/src/dialogs/liquify_ui.rs +++ b/crates/ui-egui/src/dialogs/liquify_ui.rs @@ -606,7 +606,7 @@ fn open_on( let canvas = st.doc.bounds(); let name = st.doc.layer(layer).map(|l| l.name.clone()).unwrap_or_default(); let proxy = ProxyImage::new(&surf, canvas, PROXY_SIDE); - let target = if smart_filter.is_some() { json!("pixels") } else { crate::canvas::paint_target(app) }; + let target = if smart_filter.is_some() { json!("pixels") } else { crate::distort_ui::colour_set_target(app) }; let colors = photocraft_engine::color_channels::explicit(&json!({"target":target}), surf.format().mode.color_channels()).map_err(|e| e.to_string())?; let native_proxy = colors.map(|_| crate::gallery_ui::reduce(&surf, canvas, proxy.scale as u32)); let out = vec![[0u8; 4]; proxy.w * proxy.h]; @@ -689,7 +689,7 @@ pub fn commit(app: &mut PhotocraftApp) -> Result<(), String> { if d.strokes.is_empty() { return Ok(()); } - let p = json!({"strokes": d.strokes, "meshSize": d.cell, "layer": d.layer.0,"target":d.target}); + let p = crate::distort_ui::with_filter_target(json!({"strokes": d.strokes, "meshSize": d.cell, "layer": d.layer.0}), &d.target); app.run("filter.liquify", p).map(|_| ()) } diff --git a/crates/ui-egui/src/dialogs/liquify_ui/tests.rs b/crates/ui-egui/src/dialogs/liquify_ui/tests.rs index 557115ff3e1..7d5548ed99e 100644 --- a/crates/ui-egui/src/dialogs/liquify_ui/tests.rs +++ b/crates/ui-egui/src/dialogs/liquify_ui/tests.rs @@ -897,3 +897,83 @@ fn liquify_that_cannot_be_applied_reports_the_error() { assert_eq!(app.session.active().unwrap().history.past_len(), steps); assert_eq!(r, Err(app.ui.status.clone())); } + +/// Liquify, Adaptive Wide Angle and the Filter Gallery open on the layer when no colour set is +/// targeted, as before colour sets: with its mask, an alpha channel or the Quick Mask targeted. +/// OK sends the command no `target`, so it routes the targeted surface exactly as it did then. +#[test] +fn workspaces_open_on_the_layer_with_a_mask_channel_or_quick_mask_targeted() { + let setup = |case: &str| { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default()); + app.run("file.new", json!({"width": 64, "height": 48})).unwrap(); + app.run("layer.new.layer", json!({})).unwrap(); + app.run("edit.fill", json!({"contents": "color", "color": "#ff0000"})).unwrap(); + app.run("select.rect", json!({"x": 20, "y": 16, "width": 16, "height": 16})).unwrap(); + app.run("edit.fill", json!({"contents": "color", "color": "#0000ff"})).unwrap(); + app.run("select.deselect", json!({})).unwrap(); + match case { + "mask" => drop(app.run("layer.layerMask.revealAll", json!({})).unwrap()), + "alpha" => { + app.run("channel.new", json!({"fill": "black"})).unwrap(); + app.run("channel.target", json!({"channel": 0})).unwrap(); + } + _ => drop(app.run("select.editInQuickMaskMode", json!({"on": true})).unwrap()), + } + app.sync_views(); + app + }; + let surfaces = |app: &PhotocraftApp| { + let st = app.session.active().unwrap(); + let l = st.doc.layer(st.active_layer.unwrap()).unwrap(); + ( + l.surface().cloned(), + l.mask.as_ref().map(|m| m.surface.clone()), + st.doc.channels.iter().map(|c| c.surface.clone()).collect::>(), + st.doc.quick_mask.as_ref().map(|c| c.surface.clone()), + ) + }; + let ctx = egui::Context::default(); + for case in ["mask", "alpha", "quickMask"] { + let mut app = setup(case); + assert!(crate::canvas::paint_target(&app).get("colors").is_none(), "{case}"); + crate::wide_angle_ui::open(&mut app, &ctx).unwrap_or_else(|e| panic!("{case}: Adaptive Wide Angle: {e}")); + crate::gallery_ui::open(&mut app).unwrap_or_else(|e| panic!("{case}: Filter Gallery: {e}")); + open(&mut app, &ctx).unwrap_or_else(|e| panic!("{case}: Liquify: {e}")); + let d = app.distort.liquify.as_mut().unwrap(); + let stroke = LiquifyStroke { points: vec![vec![20.0, 24.0, 1.0], vec![36.0, 24.0, 1.0]], ..LiquifyStroke::new(LiquifyTool::ForwardWarp, 30.0) }; + d.strokes.push(stroke.clone()); + let cell = d.cell; + commit(&mut app).unwrap(); + let mut before = setup(case); + let layer = before.session.active().unwrap().active_layer.unwrap(); + before.run("filter.liquify", json!({"strokes": [stroke], "meshSize": cell, "layer": layer.0})).unwrap(); + assert!(surfaces(&app) == surfaces(&before), "{case}: OK edits what the command without a target edits"); + assert!(surfaces(&app) != surfaces(&setup(case)), "{case}: the stroke changed something"); + } +} + +/// A smart object's targeted filter mask: the workspaces open on the smart object, and Liquify's +/// OK adds a smart filter, as the filter mask doesn't take it. +#[test] +fn workspaces_open_on_a_smart_object_with_its_filter_mask_targeted() { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default()); + app.run("file.new", json!({"width": 64, "height": 48})).unwrap(); + app.run("layer.new.layer", json!({})).unwrap(); + app.run("edit.fill", json!({"contents": "color", "color": "#ff0000"})).unwrap(); + app.run("layer.smartObjects.convertToSmartObject", json!({})).unwrap(); + app.run("filter.blur.gaussianBlur", json!({"radius": 2})).unwrap(); + let layer = app.session.active().unwrap().active_layer.unwrap(); + app.ui.filter_mask_target = Some(layer.0); + app.sync_views(); + assert!(app.filter_mask_targeted()); + let ctx = egui::Context::default(); + crate::wide_angle_ui::open(&mut app, &ctx).unwrap(); + crate::gallery_ui::open(&mut app).unwrap(); + open(&mut app, &ctx).unwrap(); + let d = app.distort.liquify.as_mut().unwrap(); + d.strokes.push(LiquifyStroke { points: vec![vec![20.0, 24.0, 1.0], vec![36.0, 24.0, 1.0]], ..LiquifyStroke::new(LiquifyTool::ForwardWarp, 30.0) }); + commit(&mut app).unwrap(); + let st = app.session.active().unwrap(); + let Some(photocraft_doc::LayerContent::Smart(sm)) = st.doc.layer(layer).map(|l| &l.content) else { panic!("not a smart object") }; + assert_eq!(sm.smart_filters.iter().map(|f| f.command.as_str()).collect::>(), ["filter.blur.gaussianBlur", photocraft_engine::distort_cmds::LIQUIFY]); +} diff --git a/crates/ui-egui/src/dialogs/wide_angle_ui.rs b/crates/ui-egui/src/dialogs/wide_angle_ui.rs index 01014e926dc..b979f71cf44 100644 --- a/crates/ui-egui/src/dialogs/wide_angle_ui.rs +++ b/crates/ui-egui/src/dialogs/wide_angle_ui.rs @@ -110,8 +110,7 @@ impl WideAngleDialog { fn command_params(&self) -> Value { let mut v = serde_json::to_value(&self.params).unwrap_or(json!({})); v["layer"] = json!(self.layer.0); - v["target"] = self.target.clone(); - v + crate::distort_ui::with_filter_target(v, &self.target) } } @@ -149,7 +148,7 @@ pub fn open(app: &mut PhotocraftApp, ctx: &egui::Context) -> Result<(), String> frame, scale: 1.0 / k as f64, proxy: px, - target: crate::canvas::paint_target(app), + target: crate::distort_ui::colour_set_target(app), pw, ph, src_tex: None, diff --git a/crates/ui-egui/src/panels/layers.rs b/crates/ui-egui/src/panels/layers.rs index d83c1cbbb41..0d3b55e0767 100644 --- a/crates/ui-egui/src/panels/layers.rs +++ b/crates/ui-egui/src/panels/layers.rs @@ -515,6 +515,9 @@ pub(super) fn layers(app: &mut PhotocraftApp, ui: &mut egui::Ui) { app.ui.mask_target = p.as_bool().unwrap_or(false); app.ui.vector_mask_target = false; app.ui.filter_mask_target = None; + if app.ui.mask_target { + app.target_mask_over_colour_rows(); + } app.sync_mask_targets(); continue; }