diff --git a/crates/doc/src/lib.rs b/crates/doc/src/lib.rs index 153fe6bd2ff..ca77a9a2f88 100644 --- a/crates/doc/src/lib.rs +++ b/crates/doc/src/lib.rs @@ -830,6 +830,28 @@ impl Document { self.layer_at_mut(&path) } + /// A [`Layer::link_group`] id no layer uses yet: one above the highest stored id, or, when a + /// loaded file already holds `u64::MAX` (#961), the smallest free id from 1. Never wraps, so a + /// new link can't join an unrelated group. `None` only if every id is taken. + pub fn unused_link_group(&self) -> Option { + let mut used: Vec = self.walk().iter().filter_map(|(_, _, l)| l.link_group).collect(); + used.sort_unstable(); + used.dedup(); + if let Some(next) = used.last().map_or(Some(1), |g| g.checked_add(1)) { + return Some(next); + } + let mut free = 1u64; + for g in used { + if g > free { + break; + } + if g == free { + free = free.checked_add(1)?; + } + } + Some(free) + } + pub fn layer_at(&self, path: &[usize]) -> Option<&Layer> { let (first, rest) = path.split_first()?; let mut cur = self.layers.get(*first)?; @@ -925,6 +947,29 @@ mod tests { Document::with_background("t", Size::new(100, 50), ColorMode::Rgb, SampleType::U8, Color::WHITE) } + #[test] + fn unused_link_group_never_wraps() { + let mut d = doc(); + assert_eq!(d.unused_link_group(), Some(1)); + let with = |d: &mut Document, groups: &[u64]| { + d.layers.truncate(1); + for g in groups { + let mut l = Layer::raster("l", PixelFormat::RGBA8); + l.link_group = Some(*g); + d.layers.push(l); + } + }; + with(&mut d, &[3, 7]); + assert_eq!(d.unused_link_group(), Some(8)); + with(&mut d, &[u64::MAX - 1]); + assert_eq!(d.unused_link_group(), Some(u64::MAX)); + // A stored maximal id would wrap to 0 (#961): take the smallest free id instead. + with(&mut d, &[u64::MAX]); + assert_eq!(d.unused_link_group(), Some(1)); + with(&mut d, &[0, 1, 2, 4, u64::MAX]); + assert_eq!(d.unused_link_group(), Some(3)); + } + #[test] fn new_document_has_locked_background() { let d = doc(); diff --git a/crates/engine/src/layer_multi_cmds.rs b/crates/engine/src/layer_multi_cmds.rs index 08e5b1f878b..115a6c1cb68 100644 --- a/crates/engine/src/layer_multi_cmds.rs +++ b/crates/engine/src/layer_multi_cmds.rs @@ -537,13 +537,12 @@ fn link_layers(s: &mut Session) -> Result { // Already one link group (or a single linked layer): unlink, like Photoshop's toggle. let first = groups.first().copied().flatten(); let unlink = first.is_some() && groups.iter().all(|g| *g == first); - // A fresh id: one past the largest, or (a stored document already uses u64::MAX) the - // smallest unused one, never an existing group that would join unrelated layers. - let used: std::collections::HashSet = doc.walk().iter().filter_map(|(_, _, l)| l.link_group).collect(); - let next = used.iter().max().map_or(Some(1), |m| m.checked_add(1)).or_else(|| (1..=u64::MAX).find(|g| !used.contains(g))).unwrap_or(1); + // A loaded file can store any id up to u64::MAX, so the new id must not be a bare + // max + 1 (#961). Unlinking needs no new id at all. + let next = if unlink { None } else { Some(doc.unused_link_group().ok_or_else(|| bad("layer.linkLayers", "no free link group id is left"))?) }; for id in &sel { let l = doc.layer_mut(*id).ok_or(EngineError::NoLayer(*id))?; - l.link_group = if unlink { None } else { Some(next) }; + l.link_group = next; } // A group left with a single member is no longer a link. let mut counts = std::collections::HashMap::::new(); @@ -552,7 +551,8 @@ fn link_layers(s: &mut Session) -> Result { *counts.entry(g).or_default() += 1; } } - let lonely: Vec = doc.walk().iter().filter(|(_, _, l)| l.link_group.is_some_and(|g| counts[&g] < 2)).map(|(_, _, l)| l.id).collect(); + let lonely: Vec = + doc.walk().iter().filter(|(_, _, l)| l.link_group.is_some_and(|g| counts.get(&g).is_some_and(|&c| c < 2))).map(|(_, _, l)| l.id).collect(); for id in lonely { if let Some(l) = doc.layer_mut(id) { l.link_group = None; @@ -1375,6 +1375,111 @@ mod tests { } } + /// Plain layers carrying stored link groups, as a loaded `.pcraft` or PSD can hold (#961). + fn stored_groups(s: &mut Session, groups: &[Option]) -> Vec { + let ids: Vec = groups.iter().map(|_| rect_layer(s, Rect::new(0, 0, 4, 4))).collect(); + s.edit("load groups", |doc, _| { + for (id, g) in ids.iter().zip(groups) { + doc.layer_mut(*id).unwrap().link_group = *g; + } + Ok(()) + }) + .unwrap(); + ids + } + + fn group(s: &Session, id: LayerId) -> Option { + doc(s).layer(id).unwrap().link_group + } + + /// Links `ids`, then checks they share one group no other layer uses (#961). + fn link_fresh(s: &mut Session, ids: &[LayerId]) -> u64 { + select_all(s, ids); + let past = s.active().unwrap().history.past_len(); + assert_eq!(s.execute("layer.linkLayers", json!({})).unwrap()["linked"], true); + assert_eq!(s.active().unwrap().history.past_len(), past + 1); + let g = group(s, ids[0]).expect("linked"); + assert!(ids.iter().all(|id| group(s, *id) == Some(g))); + let members = doc(s).walk().iter().filter(|(_, _, l)| l.link_group == Some(g)).count(); + assert_eq!(members, ids.len(), "group {g} is shared with unselected layers"); + g + } + + #[test] + fn link_layers_beside_a_maximal_stored_group() { + // The issue's repro: one layer stored at u64::MAX, link it with an unlinked one. + let mut s = session(8); + let ids = stored_groups(&mut s, &[Some(u64::MAX), None]); + assert_ne!(link_fresh(&mut s, &ids), u64::MAX); + // Unlink and relink the pair: with no groups left, numbering starts over at 1. + assert_eq!(s.execute("layer.linkLayers", json!({})).unwrap()["linked"], false); + assert!(ids.iter().all(|id| group(&s, *id).is_none())); + assert_eq!(link_fresh(&mut s, &ids), 1); + + // Two other layers linked beside an untouched maximal pair. + let mut s = session(8); + let ids = stored_groups(&mut s, &[Some(u64::MAX), Some(u64::MAX), None, None]); + let g = link_fresh(&mut s, &ids[2..]); + assert_ne!(g, u64::MAX); + assert_eq!((group(&s, ids[0]), group(&s, ids[1])), (Some(u64::MAX), Some(u64::MAX))); + } + + #[test] + fn unlink_a_maximal_stored_group() { + let mut s = session(8); + let ids = stored_groups(&mut s, &[Some(u64::MAX), Some(u64::MAX), None]); + select_all(&mut s, &ids[..2]); + assert_eq!(s.execute("layer.linkLayers", json!({})).unwrap()["linked"], false); + assert!(ids.iter().all(|id| group(&s, *id).is_none())); + s.undo(); + assert_eq!((group(&s, ids[0]), group(&s, ids[1])), (Some(u64::MAX), Some(u64::MAX))); + // Relinking all three gets a working group. + link_fresh(&mut s, &ids); + } + + #[test] + fn link_layers_when_every_group_is_huge() { + // Unselected groups at the top of the range, including the 0 a wrapped id would join. + let top = [u64::MAX, u64::MAX - 1, u64::MAX - 2, 0, 1, 2]; + let stored: Vec> = top.iter().flat_map(|g| [Some(*g), Some(*g)]).chain([None, None, None, None]).collect(); + let mut s = session(8); + let ids = stored_groups(&mut s, &stored); + let n = top.len() * 2; + let g = link_fresh(&mut s, &ids[n..n + 2]); + assert!(!top.contains(&g), "{g} collides with a stored group"); + // A second link gets yet another group, and existing groups never change. + let h = link_fresh(&mut s, &ids[n + 2..]); + assert!(!top.contains(&h) && h != g); + for (i, t) in top.iter().enumerate() { + assert_eq!((group(&s, ids[2 * i]), group(&s, ids[2 * i + 1])), (Some(*t), Some(*t))); + } + // Moving a newly linked layer leaves the group-0 pair alone. + assert_eq!(with_links(doc(&s), &[ids[n]]), vec![ids[n], ids[n + 1]]); + } + + #[test] + fn link_layers_near_the_maximum() { + // max + 1 still fits: the new group is u64::MAX itself. + let mut s = session(8); + let ids = stored_groups(&mut s, &[Some(u64::MAX - 1), Some(u64::MAX - 1), None, None, None, None]); + assert_eq!(link_fresh(&mut s, &ids[2..4]), u64::MAX); + // The next link no longer fits above the maximum and takes a free id instead. + let g = link_fresh(&mut s, &ids[4..]); + assert!(g != u64::MAX && g != u64::MAX - 1); + } + + #[test] + fn link_layers_assigns_working_groups() { + // Control: ordinary documents keep getting max + 1, starting at 1. + let mut s = session(8); + let ids = stored_groups(&mut s, &[None, None, None, None]); + assert_eq!(link_fresh(&mut s, &ids[..2]), 1); + assert_eq!(link_fresh(&mut s, &ids[2..]), 2); + assert_eq!(s.execute("layer.linkLayers", json!({})).unwrap()["linked"], false); + assert_eq!((group(&s, ids[2]), group(&s, ids[3])), (None, None)); + assert_eq!((group(&s, ids[0]), group(&s, ids[1])), (Some(1), Some(1))); + } + #[test] fn selection_pruned_after_delete_and_undo() { let mut s = session(8);