diff --git a/crates/algo/src/gradient_dither_16bit_tests.rs b/crates/algo/src/gradient_dither_16bit_tests.rs index 64736f455f..3cee7c6fca 100644 --- a/crates/algo/src/gradient_dither_16bit_tests.rs +++ b/crates/algo/src/gradient_dither_16bit_tests.rs @@ -5,7 +5,7 @@ use photocraft_color::{ColorMode, PixelFormat, SampleType}; use photocraft_raster::Surface; -use super::{BlendMode, GradientShape, Rect, paint_gradient}; +use super::{BlendMode, FillMode, GradientShape, Rect, paint_gradient}; /// A black → white Linear gradient over 800 × 600, from (100, 300) to (700, 300), in `fmt`. fn painted(fmt: PixelFormat, dither: bool) -> Surface { @@ -20,7 +20,7 @@ fn painted(fmt: PixelFormat, dither: bool) -> Surface { &stops, false, 1.0, - BlendMode::Normal, + FillMode::Blend(BlendMode::Normal), dither, None, false, diff --git a/crates/algo/src/paint.rs b/crates/algo/src/paint.rs index 8ae73b48a7..39e4770117 100644 --- a/crates/algo/src/paint.rs +++ b/crates/algo/src/paint.rs @@ -170,7 +170,8 @@ fn behind(d: [f32; 4], src: [f32; 4], k: f32) -> [f32; 4] { [mix(0), mix(1), mix(2), alpha] } -/// Gradient tool over `area` (the selection bounds or canvas). +/// Gradient tool over `area` (the selection bounds or canvas), composited by `mode` (a blend +/// mode, or Behind, #12011). #[allow(clippy::too_many_arguments)] pub fn paint_gradient( s: &mut Surface, @@ -181,7 +182,7 @@ pub fn paint_gradient( stops: &[(f32, [f32; 4])], reverse: bool, opacity: f32, - blend_mode: BlendMode, + mode: FillMode, dither: bool, selection: Option<&Surface>, lock_transparency: bool, @@ -196,7 +197,7 @@ pub fn paint_gradient( composite_area( s, area, - FillMode::Blend(blend_mode), + mode, |x, y| opacity * selection.map_or(1.0, |m| m.sample_channel(x, y, 0)), |x, y| { // Sampled one pixel up and left of the pixel centre, where the reference application samples (#12018). @@ -408,7 +409,7 @@ mod tests { &[(0.0, [0.0, 0.0, 0.0, 1.0]), (1.0, [1.0, 1.0, 1.0, 1.0])], false, 1.0, - BlendMode::Normal, + FillMode::Blend(BlendMode::Normal), false, Some(&sel), false, @@ -427,7 +428,20 @@ mod tests { let a = Rect::new(0, 0, 4, 1); s.fill_rect(a, &[1.0, 1.0, 1.0, 0.5]); let black = [0.0, 0.0, 0.0, 1.0]; - paint_gradient(&mut s, a, (0.0, 0.0), (4.0, 0.0), GradientShape::Linear, &[(0.0, black), (1.0, black)], false, 0.5, mode, false, None, true); + paint_gradient( + &mut s, + a, + (0.0, 0.0), + (4.0, 0.0), + GradientShape::Linear, + &[(0.0, black), (1.0, black)], + false, + 0.5, + FillMode::Blend(mode), + false, + None, + true, + ); let p = s.rgba(2, 0); assert!((p[0] - 0.5).abs() < 0.01 && (p[3] - 0.5).abs() < 0.01, "{fmt:?} {mode:?} {p:?}"); } diff --git a/crates/algo/src/paint_gradient_sample_tests.rs b/crates/algo/src/paint_gradient_sample_tests.rs index 2b30439ba4..9cc2d91092 100644 --- a/crates/algo/src/paint_gradient_sample_tests.rs +++ b/crates/algo/src/paint_gradient_sample_tests.rs @@ -7,7 +7,7 @@ use photocraft_color::PixelFormat; use photocraft_raster::Surface; -use super::{BlendMode, GradientShape, Rect, paint_gradient}; +use super::{BlendMode, FillMode, GradientShape, Rect, paint_gradient}; const LINEAR: [f32; 11] = [0.0, 2.0, 17.0, 42.0, 70.0, 99.0, 129.0, 162.0, 196.0, 228.0, 255.0]; @@ -18,7 +18,7 @@ fn ramp() -> Vec<(f32, [f32; 4])> { /// An 800 × 600 RGB8 gradient from `from` to `to`, dither off. fn painted(shape: GradientShape, from: (f32, f32), to: (f32, f32)) -> Surface { let mut s = Surface::new(PixelFormat::RGBA8); - paint_gradient(&mut s, Rect::new(0, 0, 800, 600), from, to, shape, &ramp(), false, 1.0, BlendMode::Normal, false, None, false); + paint_gradient(&mut s, Rect::new(0, 0, 800, 600), from, to, shape, &ramp(), false, 1.0, FillMode::Blend(BlendMode::Normal), false, None, false); s } diff --git a/crates/engine-paint/src/cmd.rs b/crates/engine-paint/src/cmd.rs index f2bd6b0c6b..757472f0ae 100644 --- a/crates/engine-paint/src/cmd.rs +++ b/crates/engine-paint/src/cmd.rs @@ -16,6 +16,8 @@ pub mod fill_key_cmds; #[cfg(test)] mod gradient_alpha_method_tests; #[cfg(test)] +mod gradient_behind_mode_tests; +#[cfg(test)] mod gradient_mask_history_name_tests; pub mod paint_cmds; pub mod pick_cmds; diff --git a/crates/engine-paint/src/cmd/gradient_behind_mode_tests.rs b/crates/engine-paint/src/cmd/gradient_behind_mode_tests.rs new file mode 100644 index 0000000000..fa79172f8d --- /dev/null +++ b/crates/engine-paint/src/cmd/gradient_behind_mode_tests.rs @@ -0,0 +1,68 @@ +//! #12011: the Classic gradient's Behind mode paints only where the layer is transparent. +//! Reference: a layer opaque red at x 0-399 and transparent at x 400-799, the default black to +//! white gradient dragged (100, 300) to (700, 300) in Behind: red stays at x 50 and 399, greys +//! 99 / 179 / 255 at x 400 / 550 / 750. + +use serde_json::{Value, json}; + +use crate::Session; + +/// An 800 x 600 document whose active layer is red on its left half and transparent on its right. +fn red_half() -> Session { + let mut s = crate::session(); + s.execute("file.new", json!({"width": 800, "height": 600, "background": "transparent"})).unwrap(); + s.execute("select.rect", json!({"x": 0, "y": 0, "width": 400, "height": 600})).unwrap(); + s.execute("edit.fill", json!({"color": "#ff0000"})).unwrap(); + s.execute("select.deselect", json!({})).unwrap(); + s +} + +fn drag(s: &mut Session, extra: Value) -> crate::Result { + let mut p = json!({"from": [100, 300], "to": [700, 300], "dither": false}); + for (k, v) in extra.as_object().unwrap() { + p[k] = v.clone(); + } + s.execute("paint.gradient", p) +} + +fn rgba_at(s: &Session, x: i32) -> [f32; 4] { + let d = s.active().unwrap(); + let l = d.doc.layer(d.active_layer.unwrap()).unwrap(); + let photocraft_doc::LayerContent::Raster(surface) = &l.content else { panic!("a pixel layer") }; + let p = surface.rgba(x, 300); + p.map(|c| c * 255.0) +} + +#[test] +fn behind_keeps_the_red_and_fills_only_the_transparent_half() { + let mut s = red_half(); + // Smooth is the reference's interpolation; the Gradient tool's method is #12004. + drag(&mut s, json!({"mode": "behind", "method": "smooth"})).unwrap(); + for x in [50, 399] { + assert_eq!(rgba_at(&s, x), [255.0, 0.0, 0.0, 255.0], "x={x}: the opaque red stays"); + } + for (x, want) in [(400, 99.0), (550, 179.0), (750, 255.0)] { + let p = rgba_at(&s, x); + assert!(p[..3].iter().all(|c| (c - want).abs() <= 1.0) && p[3] == 255.0, "x={x}: {p:?}, want grey {want}"); + } +} + +#[test] +fn an_omitted_mode_stays_normal_and_paints_over_the_red() { + let mut s = red_half(); + drag(&mut s, json!({})).unwrap(); + let p = rgba_at(&s, 50); + assert!(p[0] < 1.0 && p[1] < 1.0 && p[2] < 1.0, "Normal covers the red with the gradient's black: {p:?}"); +} + +#[test] +fn an_unknown_mode_is_an_error_naming_it_and_paints_nothing() { + // `clear` too: the Classic gradient's Mode list has no Clear. + for mode in ["behnd", "clear"] { + let mut s = red_half(); + let err = drag(&mut s, json!({"mode": mode})).expect_err(mode).to_string(); + assert!(err.contains(mode), "{mode}: {err}"); + assert_eq!(rgba_at(&s, 50), [255.0, 0.0, 0.0, 255.0], "{mode}: the layer is unchanged"); + assert_eq!(rgba_at(&s, 550)[3], 0.0, "{mode}: nothing is painted"); + } +} diff --git a/crates/engine-paint/src/cmd/paint_cmds.rs b/crates/engine-paint/src/cmd/paint_cmds.rs index 8475f941d8..97aa7d7a78 100644 --- a/crates/engine-paint/src/cmd/paint_cmds.rs +++ b/crates/engine-paint/src/cmd/paint_cmds.rs @@ -41,18 +41,20 @@ fn opacity(p: &Value, cmd: &str) -> Result { Ok(crate::eraser_cmds::number(p, cmd, "opacity", 100.0, 1.0..=100.0)? as f32 / 100.0) } -/// The Paint Bucket's `mode`: a blend mode (not Pass Through, a group's), `behind` or `clear`. -fn fill_mode(p: &Value) -> Result { - let bad = |msg: String| EngineError::BadParams { cmd: "paint.bucket".into(), msg }; +/// The `mode` of `cmd`: a blend mode (not Pass Through, a group's), `behind`, or `clear` where +/// `clear` is true (the Paint Bucket's; the Classic gradient's list has no Clear, #12011). +fn fill_mode(p: &Value, cmd: &str, clear: bool) -> Result { + let bad = |msg: String| EngineError::BadParams { cmd: cmd.into(), msg }; match p.get("mode") { None | Some(Value::Null) => Ok(FillMode::Blend(photocraft_color::BlendMode::Normal)), - Some(Value::String(m)) if m.eq_ignore_ascii_case("clear") => Ok(FillMode::Clear), + Some(Value::String(m)) if clear && m.eq_ignore_ascii_case("clear") => Ok(FillMode::Clear), Some(Value::String(m)) if m.eq_ignore_ascii_case("behind") => Ok(FillMode::Behind), Some(Value::String(m)) => parse_blend(m) .filter(|m| *m != photocraft_color::BlendMode::PassThrough) .map(FillMode::Blend) .ok_or_else(|| bad(format!("unknown blend mode `{m}`"))), - Some(v) => Err(bad(format!("`mode` must be a blend mode name, `behind` or `clear`, not {v}"))), + Some(v) if clear => Err(bad(format!("`mode` must be a blend mode name, `behind` or `clear`, not {v}"))), + Some(v) => Err(bad(format!("`mode` must be a blend mode name or `behind`, not {v}"))), } } @@ -65,7 +67,7 @@ fn bucket(s: &mut Session, p: &Value) -> Result { let (x, y) = (f(p, "x", 0.0).floor() as i32, f(p, "y", 0.0).floor() as i32); let (tol, contiguous, aa) = (f(p, "tolerance", 32.0), b(p, "contiguous", true), b(p, "antiAlias", true)); let opacity = opacity(p, "paint.bucket")?; - let mode = fill_mode(p)?; + let mode = fill_mode(p, "paint.bucket", true)?; let all = b(p, "sampleAllLayers", false) && crate::channel_cmds::target_of(p) == crate::channel_cmds::Target::Pixels; // Before the region, the target and the document copy: a Bitmap click changes nothing. if s.active().is_some_and(|d| d.doc.mode == photocraft_color::ColorMode::Bitmap) { @@ -197,14 +199,14 @@ fn gradient(s: &mut Session, p: &Value) -> Result { }; let reverse = b(p, "reverse", false); let opacity = opacity(p, "paint.gradient")?; - let blend = p.get("mode").and_then(Value::as_str).and_then(parse_blend).unwrap_or(photocraft_color::BlendMode::Normal); + let mode = fill_mode(p, "paint.gradient", false)?; let dither = b(p, "dither", true); // Photoshop dithers gradients by default (reduces banding). let layer = if p.get("layer").is_some() { Some(crate::commands::layer_param(s, p)?) } else { s.active().and_then(|st| st.active_layer) }; s.edit(step_name(p), |doc, _| { let sel = doc.selection.clone(); let area = sel.as_ref().map(|m| m.content_bounds()).filter(|r| !r.is_empty()).unwrap_or_else(|| doc.bounds()).intersect(&doc.bounds()); let (surf, lock) = crate::channel_cmds::target_surface(doc, layer, p)?; - paint_gradient(surf, area, from, to, shape, &stops, reverse, opacity, blend, dither, sel.as_ref(), lock); + paint_gradient(surf, area, from, to, shape, &stops, reverse, opacity, mode, dither, sel.as_ref(), lock); Ok(()) })?; Ok(Value::Null) @@ -300,7 +302,7 @@ pub fn specs() -> Vec { label: "Gradient", menu: &[], shortcut: None, - params: r##"{"from":[x,y],"to":[x,y],"style":"linear|radial|angle|reflected|diamond"="linear","colors":["#rrggbb",…]? (evenly spaced),"gradient":preset name?,"stops":[[t,"#rrggbb"|"foreground"|"background"],…]?,"transparency":[[t,0..100],…]? (default: the current gradient, see gradient.presets.select),"method":"classic|perceptual|linear|smooth"? (Photoshop's interpolation; omitted: straight stops),"smoothness":0..100=100,"reverse":bool=false,"dither":bool=true,"opacity":1..100=100,"mode":"normal|multiply|…"="normal","layer":id? (default: the active layer; a layer name also works),"target":"pixels"|"mask"|"filterMask"|"quickMask"|{"channel":i}=Channels panel target}"##, + params: r##"{"from":[x,y],"to":[x,y],"style":"linear|radial|angle|reflected|diamond"="linear","colors":["#rrggbb",…]? (evenly spaced),"gradient":preset name?,"stops":[[t,"#rrggbb"|"foreground"|"background"],…]?,"transparency":[[t,0..100],…]? (default: the current gradient, see gradient.presets.select),"method":"classic|perceptual|linear|smooth"? (Photoshop's interpolation; omitted: straight stops),"smoothness":0..100=100,"reverse":bool=false,"dither":bool=true,"opacity":1..100=100,"mode":"normal|multiply|…|behind (paints only transparency)"="normal","layer":id? (default: the active layer; a layer name also works),"target":"pixels"|"mask"|"filterMask"|"quickMask"|{"channel":i}=Channels panel target}"##, enabled: crate::cmd::has_paintable, run: gradient, journal: true, diff --git a/crates/ui-egui/src/brush_mode_menu.rs b/crates/ui-egui/src/brush_mode_menu.rs index 414144d122..b9f0abb6f2 100644 --- a/crates/ui-egui/src/brush_mode_menu.rs +++ b/crates/ui-egui/src/brush_mode_menu.rs @@ -179,6 +179,8 @@ pub(crate) fn current(app: &PhotocraftApp, tool: Tool) -> Option { Item::Blend(crate::blend_keys::brush_tool_mode(tool, brush.mode)) } } + // This menu has no Behind row for the Gradient, so no row is checked rather than Normal (#12011). + Tool::Gradient if o.gradient_behind => Item::Behind, Tool::Gradient => Item::Blend(o.gradient_blend_mode), Tool::PaintBucket if o.bucket_clear => Item::Clear, // This menu has no Behind row for the bucket yet, so no row is checked rather than Normal. diff --git a/crates/ui-egui/src/control/dispatch.rs b/crates/ui-egui/src/control/dispatch.rs index f7ec98e064..1f0fca1bce 100644 --- a/crates/ui-egui/src/control/dispatch.rs +++ b/crates/ui-egui/src/control/dispatch.rs @@ -541,6 +541,7 @@ impl ToolSet { let gradient_before = app.ui.tool_options.clone(); if let Some(mode) = self.gradient_blend { app.ui.tool_options.gradient_blend_mode = mode; + app.ui.tool_options.gradient_behind = false; } if let Some(v) = &self.stroke_trace { app.stroke_trace.set(v); diff --git a/crates/ui-egui/src/gradient_behind_mode_bar_tests.rs b/crates/ui-egui/src/gradient_behind_mode_bar_tests.rs new file mode 100644 index 0000000000..faabe98590 --- /dev/null +++ b/crates/ui-egui/src/gradient_behind_mode_bar_tests.rs @@ -0,0 +1,60 @@ +//! #12011: the Classic gradient's Mode dropdown lists Behind third, after Normal and Dissolve, and +//! no Clear; Behind is greyed on the Background, and a Classic drag in Behind paints only the +//! layer's transparency. + +use photocraft_doc::BlendMode; +use serde_json::json; + +use crate::PhotocraftApp; +use crate::canvas::ToolEvent; +use crate::options_bar_mode_wheel_tests::{harness, mode_dropdown, wheel}; +use crate::state::Tool; + +#[test] +fn the_classic_gradient_mode_dropdown_has_behind_after_dissolve_and_no_clear() { + let mut h = harness(Tool::Gradient); + h.state_mut().ui.tool_options.gradient_classic = true; + h.state_mut().run("layer.new.layer", json!({})).unwrap(); + h.run_steps(4); + let at = mode_dropdown(&h).center(); + wheel(&mut h, at, -1.0); + assert_eq!(h.state().ui.tool_options.gradient_blend_mode, BlendMode::Dissolve); + wheel(&mut h, at, -1.0); + assert!(h.state().ui.tool_options.gradient_behind, "Behind follows Dissolve"); + wheel(&mut h, at, -1.0); + assert!(!h.state().ui.tool_options.gradient_behind, "the next entry leaves Behind"); + assert_eq!(h.state().ui.tool_options.gradient_blend_mode, BlendMode::Darken, "Darken follows Behind: no Clear between"); +} + +#[test] +fn behind_is_greyed_on_the_background() { + let mut h = harness(Tool::Gradient); + h.state_mut().ui.tool_options.gradient_classic = true; + h.run_steps(4); + let at = mode_dropdown(&h).center(); + wheel(&mut h, at, -1.0); + wheel(&mut h, at, -1.0); + assert!(!h.state().ui.tool_options.gradient_behind, "the wheel steps past the greyed Behind"); + assert_eq!(h.state().ui.tool_options.gradient_blend_mode, BlendMode::Darken); +} + +#[test] +fn a_classic_drag_in_behind_keeps_the_layers_pixels() { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.run("file.new", json!({"width": 200, "height": 120, "background": "transparent"})).unwrap(); + app.run("select.rect", json!({"x": 0, "y": 0, "width": 100, "height": 120})).unwrap(); + app.run("edit.fill", json!({"color": "#ff0000"})).unwrap(); + app.run("select.deselect", json!({})).unwrap(); + app.ui.tool = Tool::Gradient; + app.ui.tool_options.gradient_classic = true; + app.ui.tool_options.gradient_behind = true; + let m = egui::Modifiers::NONE; + crate::canvas::tool_event(&mut app, ToolEvent::Down { x: 20.0, y: 60.0, pressure: 1.0 }, m); + crate::canvas::tool_event(&mut app, ToolEvent::Move { x: 100.0, y: 60.0, pressure: 1.0 }, m); + crate::canvas::tool_event(&mut app, ToolEvent::Move { x: 180.0, y: 60.0, pressure: 1.0 }, m); + crate::canvas::tool_event(&mut app, ToolEvent::Up { x: 180.0, y: 60.0 }, m); + let st = app.session.active().unwrap(); + let s = st.doc.layer(st.active_layer.unwrap()).unwrap().surface().unwrap(); + assert_eq!(s.rgba(40, 60), [1.0, 0.0, 0.0, 1.0], "the red under the gradient's dark end stays"); + assert!(s.rgba(150, 60)[3] > 0.99, "the transparent half takes the gradient"); +} diff --git a/crates/ui-egui/src/input/blend_keys.rs b/crates/ui-egui/src/input/blend_keys.rs index caf9ed75ba..01fe48aad0 100644 --- a/crates/ui-egui/src/input/blend_keys.rs +++ b/crates/ui-egui/src/input/blend_keys.rs @@ -159,6 +159,8 @@ fn set_behind(app: &mut PhotocraftApp) { pub(crate) fn set_gradient_mode(app: &mut PhotocraftApp, mode: BlendMode) { let before = app.ui.tool_options.clone(); app.ui.tool_options.gradient_blend_mode = mode; + // Any blend mode leaves Behind (#12011), as the Paint Bucket's does. + app.ui.tool_options.gradient_behind = false; // Live mode: a selected gradient fill layer follows, as with the dropdown. crate::gradient_ui::options_changed(app, &before); } diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index 2e4a1071db..ac4dd35e78 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -1035,6 +1035,9 @@ mod options_bar_lead_centred_tests; #[cfg(test)] mod options_bar_mode_wheel_tests; +#[cfg(test)] +mod gradient_behind_mode_bar_tests; + #[cfg(test)] mod options_bar_percent_arrow_repeat_tests; diff --git a/crates/ui-egui/src/state/tool_options.rs b/crates/ui-egui/src/state/tool_options.rs index a65c2e7e5b..98ea38ade8 100644 --- a/crates/ui-egui/src/state/tool_options.rs +++ b/crates/ui-egui/src/state/tool_options.rs @@ -62,6 +62,10 @@ pub struct ToolOptions { pub gradient_classic: bool, /// Blend mode used by both live and classic gradient drags. pub gradient_blend_mode: photocraft_color::BlendMode, + /// Classic gradient › Mode: Behind, which paints only the pixels' transparency (#12011). + /// Picking it sets `gradient_blend_mode` to Normal, as the Paint Bucket's Behind does; picking + /// any other mode leaves it. + pub gradient_behind: bool, pub fill_opacity: f32, /// Paint Bucket fill source: false = Foreground colour, true = Pattern (Patterns panel selection). pub bucket_fill_pattern: bool, @@ -493,6 +497,7 @@ impl Default for ToolOptions { gradient_dither: true, gradient_classic: false, gradient_blend_mode: photocraft_color::BlendMode::Normal, + gradient_behind: false, fill_opacity: 100.0, bucket_fill_pattern: false, bucket_blend_mode: photocraft_color::BlendMode::Normal, diff --git a/crates/ui-egui/src/tools/gradient_ui/bar.rs b/crates/ui-egui/src/tools/gradient_ui/bar.rs index 2a389f1be8..d9a1e0cc26 100644 --- a/crates/ui-egui/src/tools/gradient_ui/bar.rs +++ b/crates/ui-egui/src/tools/gradient_ui/bar.rs @@ -3,7 +3,7 @@ use craft::draw::ui::gradient::{GradientFillWords, GradientStyle, GradientSwatch}; use craft::ui::choice::{Checkbox, Segmented}; -use craft::ui::contract::{Bind, Entry, LabelKind, Props as _, Show as _, ShowContainer as _, SideLabel, bind_tracked}; +use craft::ui::contract::{Bind, Entry, LabelKind, Props as _, Show as _, ShowContainer as _, SideLabel}; use craft::ui::display::Divider; use craft::ui::form::Field; use craft::ui::patterns::blend_keys::{BlendKeyEntry, blend_entries}; @@ -17,17 +17,24 @@ use super::{fill_layer_active, options_changed, stops}; use crate::state::Tool; use crate::{PhotocraftApp, panel_docking}; -/// The blend modes the Classic gradient lists, with a rule above each group. -fn blend_modes() -> Vec> { - static CACHE: crate::i18n::LangCache<(), Vec>> = crate::i18n::LangCache::new(); - CACHE.get_or_build((), || { - let keys: Vec> = crate::blend_keys::tool_modes(Tool::Gradient) +/// The modes the Classic gradient lists, with a rule above each group: `None` is Behind, third +/// after Normal and Dissolve, and greyed where `behind_enabled` is false (the Background, as the +/// Paint Bucket's). The list has no Clear (#12011). +fn blend_modes(behind_enabled: bool) -> Vec>> { + static CACHE: crate::i18n::LangCache>>> = crate::i18n::LangCache::new(); + CACHE.get_or_build(behind_enabled, || { + let mut keys: Vec>> = crate::blend_keys::tool_modes(Tool::Gradient) .unwrap_or_default() .into_iter() .flatten() - .map(|mode| crate::panels::blend_entry(mode, mode)) + .map(|mode| crate::panels::blend_entry(Some(mode), mode)) .collect(); - blend_entries(&keys, |entry| Label::from(tl!(entry.key.label()).to_string())) + let at = keys.iter().position(|k| k.key == Some(BlendMode::Dissolve)).map_or(keys.len(), |i| i + 1); + keys.insert(at, BlendKeyEntry::behind(None)); + blend_entries(&keys, |entry| Label::from(tl!(entry.key.map_or("Behind", BlendMode::label)).to_string())) + .into_iter() + .map(|entry| if entry.value() == Some(&None) { entry.enabled(behind_enabled) } else { entry }) + .collect() }) } @@ -48,7 +55,7 @@ pub(crate) fn bar(app: &mut PhotocraftApp, ui: &mut egui::Ui) { let words = Env::of(ui).words::(); let styles: Vec> = GradientStyle::ALL.iter().map(|style| Entry::new(*style, style.label(&words)).icon(style.icon())).collect(); let modes = [Entry::new(false, tl!("Gradient").to_owned()), Entry::new(true, tl!("Classic gradient").to_owned())]; - let blends = blend_modes(); + let blends = blend_modes(crate::brush_mode_menu::enabled(app, crate::brush_mode_menu::Item::Behind)); let open_editor; { let mut classic = app.ui.tool_options.gradient_classic && !locked_live; @@ -67,10 +74,18 @@ pub(crate) fn bar(app: &mut PhotocraftApp, ui: &mut egui::Ui) { if classic { let fill_opacity = app.ui.tool_options.fill_opacity; { - let mut blend = bind_tracked!(app.ui.tool_options => gradient_blend_mode); + let o = &app.ui.tool_options; + let shown = if o.gradient_behind { None } else { Some(o.gradient_blend_mode) }; + let mut mode = shown; labelled(ui, tl!("Mode"), |ui| { - Select::new(tl!("Blend Mode").to_owned(), blend.reborrowed(), &blends).show(ui); + Select::new(tl!("Blend Mode").to_owned(), Bind::bound(&mut mode), &blends).show(ui); }); + if mode != shown { + let o = &mut app.ui.tool_options; + o.gradient_behind = mode.is_none(); + // Behind names no blend: the stored mode reads Normal while it is chosen, as the Paint Bucket's. + o.gradient_blend_mode = mode.unwrap_or(BlendMode::Normal); + } } labelled(ui, tl!("Opacity"), |ui| { let field = diff --git a/crates/ui-egui/src/tools/gradient_ui/tool.rs b/crates/ui-egui/src/tools/gradient_ui/tool.rs index a865dff70c..19b88fe7e2 100644 --- a/crates/ui-egui/src/tools/gradient_ui/tool.rs +++ b/crates/ui-egui/src/tools/gradient_ui/tool.rs @@ -87,9 +87,10 @@ fn commit(app: &mut PhotocraftApp, from: [f32; 2], to: [f32; 2], redraw: Option< return; } let o = app.ui.tool_options.clone(); + let mode = if o.gradient_behind { "behind" } else { o.gradient_blend_mode.label() }; let params = json!({ "from": from, "to": to, "style": o.gradient_style, "reverse": o.gradient_reverse, "dither": o.gradient_dither, - "opacity": o.fill_opacity, "mode": o.gradient_blend_mode.label(), "target": crate::canvas::paint_target(app), + "opacity": o.fill_opacity, "mode": mode, "target": crate::canvas::paint_target(app), }); let result = app.run("paint.gradient", params); report(app, result); diff --git a/crates/ui-egui/src/tools/tool_reset.rs b/crates/ui-egui/src/tools/tool_reset.rs index f40ccc6c38..8821d1c18c 100644 --- a/crates/ui-egui/src/tools/tool_reset.rs +++ b/crates/ui-egui/src/tools/tool_reset.rs @@ -235,7 +235,7 @@ pub fn option_keys(tool: Tool) -> &'static [&'static str] { Tool::BackgroundEraser => reset!(bg_sampling, bg_limits, bg_tolerance, bg_protect_fg, bg_tolerance_control), Tool::ColorReplacement => reset!(cr_mode, cr_sampling, cr_limits, cr_tolerance, cr_anti_alias), Tool::MagicEraser => reset!(tolerance, anti_alias, contiguous, magic_eraser_opacity), - Tool::Gradient => reset!(gradient_style, gradient_reverse, gradient_dither, gradient_classic, gradient_blend_mode, fill_opacity), + Tool::Gradient => reset!(gradient_style, gradient_reverse, gradient_dither, gradient_classic, gradient_blend_mode, gradient_behind, fill_opacity), Tool::PaintBucket => reset!(bucket_fill_pattern, bucket_blend_mode, bucket_clear, bucket_behind, fill_opacity, tolerance, anti_alias, contiguous), Tool::Type | Tool::VerticalType | Tool::HorizontalTypeMask | Tool::VerticalTypeMask => { reset!(type_font, type_style, type_size, type_aa, type_align, type_direction, type_style_defaults)