From 4d93837094a98fad97ccc387aef97de83733b8d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Sun, 17 Aug 2025 09:31:46 +0200 Subject: [PATCH] Merge only contiguous layers in `layer::Stack` --- graphics/src/layer.rs | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/graphics/src/layer.rs b/graphics/src/layer.rs index dbab0192..de5fa522 100644 --- a/graphics/src/layer.rs +++ b/graphics/src/layer.rs @@ -105,14 +105,18 @@ impl Stack { let previous = self.previous.pop().unwrap(); - let (head, tail) = self.layers.split_at_mut(previous + 1); - let previous_layer = &mut head[previous]; - let current_layer = &mut tail[self.current - previous - 1]; + // Try to merge contiguous layers + if previous + 1 == self.current { + let (head, tail) = self.layers.split_at_mut(previous + 1); + let previous_layer = &mut head[previous]; + let current_layer = &mut tail[0]; - if previous_layer.end() <= current_layer.start() - && previous_layer.bounds() == current_layer.bounds() - { - previous_layer.merge(current_layer); + if previous_layer.end() <= current_layer.start() + && previous_layer.bounds() == current_layer.bounds() + { + previous_layer.merge(current_layer); + self.active_count -= 1; + } } self.current = previous;