Merge pull request #19241

Co-authored-by: autocraft-bot[bot] <354215646+autocraft-bot[bot]@users.noreply.github.com>
This commit is contained in:
davidmann4andautocraft-bot[bot] authored and autocraft-bot[bot] committed 2026-12-02 22:19:56 +00:00
commit cb081d3757
3 files changed
+101 -2

No files matched your search

@@ -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<Layer> {
fn create(s: &mut Session, p: &Value) -> Result<Value> {
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;
@@ -0,0 +1,69 @@
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");
}
}
+10 -1
View File
@@ -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<'_>) {