Preserve surviving channel targets and visibility on deletion (#3887)

Co-authored-by: jonathnwxh <[email protected]>
Co-authored-by: Codex <[email protected]>
This commit is contained in:
authored and GitHub committed 2026-10-12 09:32:37 -04:00
1 parent 76fa06dbd5
commit 4010630c98
2 files changed
+54

No files matched your search

+4
View File
@@ -1126,11 +1126,15 @@ fn delete_channel(s: &mut Session, p: &Value) -> Result<Value> {
_ => 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);
+50
View File
@@ -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();