From 4010630c98084b9e296dbc845c477a0bfeb662ef Mon Sep 17 00:00:00 2001 From: jonathanwxh-cell Date: Mon, 12 Oct 2026 21:32:37 +0800 Subject: [PATCH] Preserve surviving channel targets and visibility on deletion (#3887) Co-authored-by: jonathnwxh <258340642+jonathanwxh-cell@users.noreply.github.com> Co-authored-by: Codex --- crates/engine/src/channel_cmds.rs | 4 ++ crates/engine/src/channel_cmds/tests.rs | 50 +++++++++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/crates/engine/src/channel_cmds.rs b/crates/engine/src/channel_cmds.rs index 39ce9c8250..63f8f5b626 100644 --- a/crates/engine/src/channel_cmds.rs +++ b/crates/engine/src/channel_cmds.rs @@ -1126,11 +1126,15 @@ fn delete_channel(s: &mut Session, p: &Value) -> Result { _ => return Err(bad(cmd, "missing `channel`")), }, }; + // `edit` validates the view against the shorter channel list before we can remap it. + // Keep the original indices and eyes so a surviving final channel is not discarded. + let view = s.active().ok_or(EngineError::NoDocument)?.channel_view.clone(); s.edit("Delete Channel", |doc, _| { doc.channels.remove(i); Ok(()) })?; if let Some(st) = s.active_mut() { + st.channel_view = view; let v = &mut st.channel_view; if i < v.alpha_visible.len() { v.alpha_visible.remove(i); diff --git a/crates/engine/src/channel_cmds/tests.rs b/crates/engine/src/channel_cmds/tests.rs index 17727dc8ad..d39dda70b7 100644 --- a/crates/engine/src/channel_cmds/tests.rs +++ b/crates/engine/src/channel_cmds/tests.rs @@ -168,6 +168,56 @@ fn channel_management_and_view_state() { assert_eq!(doc(&s).channels.len(), 3); } +#[test] +fn deleting_an_earlier_channel_keeps_the_last_channel_targeted() { + for depth in [8, 16, 32] { + let mut s = session_depth(depth); + s.execute("channel.new", json!({"name": "Remove", "fill": "black"})).unwrap(); + s.execute("channel.new", json!({"name": "Keep", "fill": "white"})).unwrap(); + let layer_before = px(&s, 5, 5); + s.execute("channel.delete", json!({"channel": "Remove"})).unwrap(); + assert_eq!(doc(&s).channels[0].name, "Keep"); + let view = &s.active().unwrap().channel_view; + assert_eq!(view.target, ChannelTarget::Alpha(0), "the surviving target follows its new index"); + assert!(view.alpha_shown(0)); + assert_eq!(view.visible_colors(3), 0, "keep the channel-only view"); + + s.execute("image.adjustments.invert", json!({})).unwrap(); + assert_eq!(chan(&s, 0, 5, 5), 0.0, "implicit pixel edits still target Keep"); + assert_eq!(px(&s, 5, 5), layer_before, "the layer must not be edited instead"); + assert!(s.undo()); + assert_eq!(chan(&s, 0, 5, 5), 1.0); + assert_eq!(px(&s, 5, 5), layer_before); + assert!(s.redo()); + assert_eq!(chan(&s, 0, 5, 5), 0.0); + assert_eq!(px(&s, 5, 5), layer_before); + } +} + +#[test] +fn deleting_a_channel_remaps_all_surviving_eyes_and_targets() { + for target_index in 0..3 { + let mut s = session(); + for name in ["First", "Middle", "Last"] { + s.execute("channel.new", json!({"name": name})).unwrap(); + } + s.execute("channel.target", json!({"channel": target_index})).unwrap(); + for (channel, visible) in [false, false, true].into_iter().enumerate() { + s.execute("channel.setVisible", json!({"channel": channel, "visible": visible})).unwrap(); + } + s.execute("channel.delete", json!({"channel": "Middle"})).unwrap(); + let view = &s.active().unwrap().channel_view; + let expected = match target_index { + 0 => ChannelTarget::Alpha(0), + 1 => ChannelTarget::Composite, + _ => ChannelTarget::Alpha(1), + }; + assert_eq!(view.target, expected, "old target {target_index}"); + assert!(!view.alpha_shown(0)); + assert!(view.alpha_shown(1), "Last keeps its eye when Middle is removed"); + } +} + #[test] fn every_colour_channel_can_hide_while_alpha_stays() { let mut s = session();