From 0ff5767a21b20857d4b6216acaa993db6f7484b5 Mon Sep 17 00:00:00 2001 From: yoriswind Date: Wed, 2 Dec 2026 08:11:46 -0800 Subject: [PATCH 1/2] Refuse Gradient fill creation in unsupported color modes --- .../engine-core/src/cmd/gradient_fill_cmds.rs | 23 ++++- .../cmd/gradient_fill_mode_refusal_tests.rs | 86 +++++++++++++++++++ crates/ui-egui/src/tools/gradient_ui/tool.rs | 11 ++- 3 files changed, 118 insertions(+), 2 deletions(-) create mode 100644 crates/engine-core/src/cmd/gradient_fill_mode_refusal_tests.rs diff --git a/crates/engine-core/src/cmd/gradient_fill_cmds.rs b/crates/engine-core/src/cmd/gradient_fill_cmds.rs index 91f00d261e2..80e750d1c8d 100644 --- a/crates/engine-core/src/cmd/gradient_fill_cmds.rs +++ b/crates/engine-core/src/cmd/gradient_fill_cmds.rs @@ -14,7 +14,7 @@ use crate::presets::PresetsExt; use photocraft_color::Color; use photocraft_compose::gradient_fill as gf; -use photocraft_doc::{Document, Fill, GradientStyle, Layer, LayerContent, LayerId}; +use photocraft_doc::{ColorMode, Document, Fill, GradientStyle, Layer, LayerContent, LayerId}; use photocraft_geom::Rect; use serde_json::{Value, json}; @@ -30,6 +30,20 @@ pub const SET: &str = "gradient.fill.set"; pub const STOP: &str = "gradient.fill.stop"; pub const GET: &str = "gradient.fill.get"; +/// The refusal shared by Gradient tool releases and direct Gradient Fill creation. +pub fn create_mode_refusal(mode: ColorMode) -> Option<&'static str> { + match mode { + ColorMode::Indexed => { + Some("Could not use the gradient tool because it does not work with Indexed Color images (convert image to RGB Color mode to edit).") + } + ColorMode::Bitmap => Some("Could not use the gradient tool because it does not work with Bitmap images (convert image to Grayscale mode to edit)."), + ColorMode::Multichannel => { + Some("Could not use the gradient tool because it does not work with Multichannel images (convert image to RGB Color mode to edit).") + } + _ => None, + } +} + /// Largest coordinate accepted for a start/end point (pixels). const MAX_COORD: f32 = 1.0e7; @@ -342,6 +356,9 @@ pub fn new_layer(s: &Session, doc: &Document, p: &Value) -> Result { fn create(s: &mut Session, p: &Value) -> Result { let layer = { let d = s.active().ok_or(EngineError::NoDocument)?; + if let Some(message) = create_mode_refusal(d.doc.mode) { + return Err(EngineError::Other(message.into())); + } new_layer(s, &d.doc, p)? }; let id = s.edit("New Gradient Fill Layer", |doc, active| { @@ -646,3 +663,7 @@ mod tests; #[cfg(test)] #[path = "gradient_fill_selection_tests.rs"] mod gradient_fill_selection_tests; + +#[cfg(test)] +#[path = "gradient_fill_mode_refusal_tests.rs"] +mod gradient_fill_mode_refusal_tests; diff --git a/crates/engine-core/src/cmd/gradient_fill_mode_refusal_tests.rs b/crates/engine-core/src/cmd/gradient_fill_mode_refusal_tests.rs new file mode 100644 index 00000000000..e1d709f9672 --- /dev/null +++ b/crates/engine-core/src/cmd/gradient_fill_mode_refusal_tests.rs @@ -0,0 +1,86 @@ +use photocraft_color::{Color, ColorMode, SampleType}; +use photocraft_doc::{Document, Fill, LayerContent, LayerId, Size}; +use serde_json::{Value, json}; + +use super::CREATE; +use crate::{EngineError, Session}; + +fn session(mode: ColorMode, depth: SampleType) -> Session { + let mut session = crate::session(); + let doc = Document::with_background("Gradient mode", Size::new(64, 64), mode, depth, Color::rgb(1.0, 1.0, 1.0)); + session.add_document(doc, None); + session +} + +fn drag() -> Value { + json!({"from": [8, 8], "to": [56, 56]}) +} + +#[test] +fn unsupported_modes_refuse_without_changing_the_document() { + let cases = [ + ( + ColorMode::Indexed, + "Could not use the gradient tool because it does not work with Indexed Color images (convert image to RGB Color mode to edit).", + ), + ( + ColorMode::Bitmap, + "Could not use the gradient tool because it does not work with Bitmap images (convert image to Grayscale mode to edit).", + ), + ( + ColorMode::Multichannel, + "Could not use the gradient tool because it does not work with Multichannel images (convert image to RGB Color mode to edit).", + ), + ]; + + for (mode, message) in cases { + let mut session = session(mode, SampleType::U8); + session.execute("select.all", json!({})).unwrap(); + let state = session.active().unwrap(); + let canvas = state.doc.bounds(); + let selection = state.doc.selection.as_ref().unwrap().read_region(canvas); + let layers = state.doc.layers.len(); + let active = state.active_layer; + let steps = state.history.past_len(); + let dirty = state.is_dirty(); + + assert_eq!(session.execute(CREATE, drag()).unwrap_err(), EngineError::Other(message.into()), "{mode:?}"); + + let state = session.active().unwrap(); + assert_eq!(state.doc.layers.len(), layers, "{mode:?}: no layer added"); + assert_eq!(state.active_layer, active, "{mode:?}: active layer unchanged"); + assert_eq!(state.doc.selection.as_ref().unwrap().read_region(canvas), selection, "{mode:?}: selection unchanged"); + assert_eq!(state.history.past_len(), steps, "{mode:?}: no history state added"); + assert_eq!(state.is_dirty(), dirty, "{mode:?}: dirty state unchanged"); + } +} + +#[test] +fn other_modes_keep_creating_gradient_fill_layers() { + use ColorMode::{Cmyk, Duotone, Grayscale, Lab, Rgb}; + use SampleType::{F32, U8, U16}; + + let cases = [ + (Rgb, U8), + (Rgb, U16), + (Rgb, F32), + (Grayscale, U8), + (Grayscale, U16), + (Grayscale, F32), + (Cmyk, U8), + (Cmyk, U16), + (Lab, U8), + (Lab, U16), + (Duotone, U8), + ]; + + for (mode, depth) in cases { + let mut session = session(mode, depth); + let steps = session.active().unwrap().history.past_len(); + let result = session.execute(CREATE, drag()).unwrap_or_else(|error| panic!("{mode:?}/{depth:?}: {error}")); + let id = LayerId(result["layer"].as_u64().unwrap()); + let state = session.active().unwrap(); + assert!(matches!(&state.doc.layer(id).unwrap().content, LayerContent::Fill(Fill::Gradient { .. })), "{mode:?}/{depth:?}: gradient fill created"); + assert_eq!(state.history.past_len(), steps + 1, "{mode:?}/{depth:?}: one history state"); + } +} diff --git a/crates/ui-egui/src/tools/gradient_ui/tool.rs b/crates/ui-egui/src/tools/gradient_ui/tool.rs index 079918dbdf6..b85b6b82988 100644 --- a/crates/ui-egui/src/tools/gradient_ui/tool.rs +++ b/crates/ui-egui/src/tools/gradient_ui/tool.rs @@ -179,7 +179,16 @@ impl CanvasTool for GradientTool<'_> { if screen(from).distance(screen(to)) < MIN_DRAG { return; } - self.scope.with_app(|app| commit(app, from, to, redraw)); + self.scope.with_app(|app| { + if !app.ui.tool_options.gradient_classic + && crate::canvas::paint_target(app).as_str() == Some("pixels") + && let Some(message) = app.session.active().and_then(|state| cmds::create_mode_refusal(state.doc.mode)) + { + let _ = crate::dialogs::alerts::alert(app, tl!(message)); + return; + } + commit(app, from, to, redraw); + }); } fn cancel(&mut self, _reason: CancelReason, _cx: &mut ToolContext<'_>) { From b342bf0d45b0f57f4b0edaeb35305511bd25d61a Mon Sep 17 00:00:00 2001 From: yoriswind Date: Wed, 2 Dec 2026 08:28:13 -0800 Subject: [PATCH 2/2] Format the Gradient color-mode engine fixtures --- .../cmd/gradient_fill_mode_refusal_tests.rs | 25 +++---------------- 1 file changed, 4 insertions(+), 21 deletions(-) diff --git a/crates/engine-core/src/cmd/gradient_fill_mode_refusal_tests.rs b/crates/engine-core/src/cmd/gradient_fill_mode_refusal_tests.rs index e1d709f9672..7cfaeb381c0 100644 --- a/crates/engine-core/src/cmd/gradient_fill_mode_refusal_tests.rs +++ b/crates/engine-core/src/cmd/gradient_fill_mode_refusal_tests.rs @@ -19,14 +19,8 @@ fn drag() -> Value { #[test] fn unsupported_modes_refuse_without_changing_the_document() { let cases = [ - ( - ColorMode::Indexed, - "Could not use the gradient tool because it does not work with Indexed Color images (convert image to RGB Color mode to edit).", - ), - ( - ColorMode::Bitmap, - "Could not use the gradient tool because it does not work with Bitmap images (convert image to Grayscale mode to edit).", - ), + (ColorMode::Indexed, "Could not use the gradient tool because it does not work with Indexed Color images (convert image to RGB Color mode to edit)."), + (ColorMode::Bitmap, "Could not use the gradient tool because it does not work with Bitmap images (convert image to Grayscale mode to edit)."), ( ColorMode::Multichannel, "Could not use the gradient tool because it does not work with Multichannel images (convert image to RGB Color mode to edit).", @@ -60,19 +54,8 @@ fn other_modes_keep_creating_gradient_fill_layers() { use ColorMode::{Cmyk, Duotone, Grayscale, Lab, Rgb}; use SampleType::{F32, U8, U16}; - let cases = [ - (Rgb, U8), - (Rgb, U16), - (Rgb, F32), - (Grayscale, U8), - (Grayscale, U16), - (Grayscale, F32), - (Cmyk, U8), - (Cmyk, U16), - (Lab, U8), - (Lab, U16), - (Duotone, U8), - ]; + let cases = + [(Rgb, U8), (Rgb, U16), (Rgb, F32), (Grayscale, U8), (Grayscale, U16), (Grayscale, F32), (Cmyk, U8), (Cmyk, U16), (Lab, U8), (Lab, U16), (Duotone, U8)]; for (mode, depth) in cases { let mut session = session(mode, depth);