Implement basic layer merging for graphics::layer::Stack

This commit is contained in:
Héctor Ramón Jiménez 2025-08-16 23:15:20 +02:00
parent 95769fcd59
commit d3e9547079
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
6 changed files with 122 additions and 17 deletions

View file

@ -9,6 +9,9 @@ pub trait Layer: Default {
/// Creates a new [`Layer`] with the given bounds.
fn with_bounds(bounds: Rectangle) -> Self;
/// Returns the current bounds of the [`Layer`].
fn bounds(&self) -> Rectangle;
/// Flushes and settles any pending group of primitives in the [`Layer`].
///
/// This will be called when a [`Layer`] is finished. It allows layers to efficiently
@ -20,6 +23,21 @@ pub trait Layer: Default {
/// Clears all the layers contents and resets its bounds.
fn reset(&mut self);
/// Returns the level of the [`Layer`].
///
/// The level is the lowest "sublayer" index inside of a [`Layer`].
///
/// A [`Layer`] may draw multiple primitive types in a certain order.
/// The level represents the lowest index of the primitive types it
/// contains.
///
/// Two layers A and B can therefore be merged if they have the same bounds,
/// and the level of A is lower or equal than the level of B.
fn level(&self) -> usize;
/// Merges a [`Layer`] with the current one.
fn merge(&mut self, _layer: &mut Self);
}
/// A stack of layers used for drawing.
@ -82,7 +100,19 @@ impl<T: Layer> Stack<T> {
pub fn pop_clip(&mut self) {
self.flush();
self.current = self.previous.pop().unwrap();
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];
if previous_layer.level() <= current_layer.level()
&& previous_layer.bounds() == current_layer.bounds()
{
previous_layer.merge(current_layer);
}
self.current = previous;
}
/// Pushes a new [`Transformation`] in the [`Stack`].

View file

@ -17,8 +17,8 @@ pub struct Layer {
pub bounds: Rectangle,
pub quads: Vec<(Quad, Background)>,
pub primitives: Vec<Item<Primitive>>,
pub text: Vec<Item<Text>>,
pub images: Vec<Image>,
pub text: Vec<Item<Text>>,
}
impl Layer {
@ -284,6 +284,10 @@ impl graphics::Layer for Layer {
}
}
fn bounds(&self) -> Rectangle {
self.bounds
}
fn flush(&mut self) {}
fn resize(&mut self, bounds: Rectangle) {
@ -298,6 +302,29 @@ impl graphics::Layer for Layer {
self.text.clear();
self.images.clear();
}
fn level(&self) -> usize {
if !self.text.is_empty() {
return 3;
}
if !self.images.is_empty() {
return 2;
}
if !self.primitives.is_empty() {
return 1;
}
0
}
fn merge(&mut self, layer: &mut Self) {
self.quads.append(&mut layer.quads);
self.primitives.append(&mut layer.primitives);
self.text.append(&mut layer.text);
self.images.append(&mut layer.images);
}
}
#[derive(Debug, Clone)]

View file

@ -11,4 +11,6 @@ impl Batch {
pub fn is_empty(&self) -> bool {
true
}
pub fn append(&mut self, _batch: &mut Self) {}
}

View file

@ -268,6 +268,10 @@ impl graphics::Layer for Layer {
}
}
fn bounds(&self) -> Rectangle {
self.bounds
}
fn flush(&mut self) {
self.flush_meshes();
self.flush_text();
@ -288,6 +292,34 @@ impl graphics::Layer for Layer {
self.pending_meshes.clear();
self.pending_text.clear();
}
fn level(&self) -> usize {
if !self.text.is_empty() {
return 4;
}
if !self.images.is_empty() {
return 3;
}
if !self.primitives.is_empty() {
return 2;
}
if !self.triangles.is_empty() {
return 1;
}
0
}
fn merge(&mut self, layer: &mut Self) {
self.quads.append(&mut layer.quads);
self.triangles.append(&mut layer.triangles);
self.primitives.append(&mut layer.primitives);
self.images.append(&mut layer.images);
self.text.append(&mut layer.text);
}
}
impl Default for Layer {

View file

@ -304,6 +304,12 @@ impl Batch {
self.gradients.clear();
self.order.clear();
}
pub fn append(&mut self, batch: &mut Batch) {
self.solids.append(&mut batch.solids);
self.gradients.append(&mut batch.gradients);
self.order.append(&mut batch.order);
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq)]

View file

@ -23,6 +23,7 @@ pub struct Stack<'a, Message, Theme = crate::Theme, Renderer = crate::Renderer>
width: Length,
height: Length,
children: Vec<Element<'a, Message, Theme, Renderer>>,
clip: bool,
}
impl<'a, Message, Theme, Renderer> Stack<'a, Message, Theme, Renderer>
@ -62,6 +63,7 @@ where
width: Length::Shrink,
height: Length::Shrink,
children,
clip: false,
}
}
@ -114,6 +116,16 @@ where
) -> Self {
children.into_iter().fold(self, Self::push)
}
/// Sets whether the [`Stack`] should clip overflowing content.
///
/// It has a slight performance overhead during presentation.
///
/// By default, it is set to `false`.
pub fn clip(mut self, clip: bool) -> Self {
self.clip = clip;
self
}
}
impl<Message, Renderer> Default for Stack<'_, Message, Renderer>
@ -277,6 +289,12 @@ where
viewport: &Rectangle,
) {
if let Some(clipped_viewport) = layout.bounds().intersection(viewport) {
let viewport = if self.clip {
&clipped_viewport
} else {
viewport
};
let layers_below = if cursor.is_over(layout.bounds()) {
self.children
.iter()
@ -312,26 +330,16 @@ where
layout,
cursor| {
if i > 0 {
renderer.with_layer(clipped_viewport, |renderer| {
renderer.with_layer(*viewport, |renderer| {
layer.as_widget().draw(
state,
renderer,
theme,
style,
layout,
cursor,
&clipped_viewport,
state, renderer, theme, style, layout, cursor,
viewport,
);
});
} else {
layer.as_widget().draw(
state,
renderer,
theme,
style,
layout,
cursor,
&clipped_viewport,
state, renderer, theme, style, layout, cursor,
viewport,
);
}
};