Color Replacement: refuse an unpaintable target at admission, before a floating selection drops

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
David MannandClaude Opus 5.5 committed 2026-12-01 11:57:31 +01:00
1 parent 07dc59dd75
commit 4674495026
2 files changed
+22 -4

No files matched your search

+8 -4
View File
@@ -55,8 +55,9 @@ fn shown(doc: &Document, id: LayerId) -> bool {
doc.path_of(id).is_some_and(|path| (1..=path.len()).all(|n| path.get(..n).and_then(|p| doc.layer_at(p)).is_some_and(|l| l.visible)))
}
/// Why command `id` with `params` must not run: its target layer is hidden. `None` for other
/// commands, channel targets, and visible layers.
/// Why command `id` with `params` must not run: its target layer is hidden, or a Color Replacement
/// target it can't recolour ([`color_replacement_target`]). `None` for other commands, channel
/// targets, and visible layers.
pub fn refusal(s: &Session, id: &str, params: &Value) -> Option<&'static str> {
if !GATED.contains(&id) {
return None;
@@ -66,8 +67,11 @@ pub fn refusal(s: &Session, id: &str, params: &Value) -> Option<&'static str> {
return None;
}
// The Color Replacement tool's target refusals come first: a hidden layer's mask gets them (#17782).
if id == "paint.colorReplacement" && color_replacement_target(s, &params).is_some() {
return None;
// Admission returns them, so a refused stroke leaves a floating selection floating, with no History step.
if id == "paint.colorReplacement"
&& let Some(why) = color_replacement_target(s, &params)
{
return Some(why);
}
let st = s.active()?;
if id == "layer.translate" && matches!(crate::commands::explicit_layer(s, &params), Ok(None)) && st.selected_layers().len() > 1 {
@@ -93,3 +93,17 @@ fn every_colour_channel_is_the_composite_and_paints() {
fn a_cmyk_channel_keeps_painting() {
paints(&mut session_in(ColorMode::Cmyk), stroke(Some(json!({"color": 0}))));
}
/// A refused target is refused before a floating selection drops: the piece stays floating, and
/// neither the pixels nor History change.
#[test]
fn a_refused_target_leaves_a_floating_selection_floating() {
let mut s = session();
s.execute("select.all", json!({})).unwrap();
s.execute("select.float", json!({"dx": 8, "dy": 0, "copy": true})).unwrap();
let before = snapshot(&s);
let err = s.execute("paint.colorReplacement", stroke(Some(json!({"color": 0})))).unwrap_err().to_string();
assert_eq!(err, COLOR_REPLACEMENT_MASK);
assert!(snapshot(&s) == before, "nothing dropped or painted, no History state");
assert!(crate::float_cmds::floating(s.active().unwrap()).is_some(), "the piece still floats");
}