Implement overlay ordering for Image::float

This commit is contained in:
Héctor Ramón Jiménez 2025-04-25 11:20:16 +02:00
parent 7f1dcec391
commit e64c58d032
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
8 changed files with 416 additions and 143 deletions

View file

@ -101,6 +101,16 @@ where
) -> Option<Element<'a, Message, Theme, Renderer>> {
None
}
/// The index of the overlay.
///
/// Overlays with a higher index will be rendered on top of overlays with
/// a lower index.
///
/// By default, it returns `1.0`.
fn index(&self) -> f32 {
1.0
}
}
/// Returns a [`Group`] of overlay [`Element`] children.

View file

@ -23,6 +23,18 @@ where
Self { overlay }
}
/// Returns a reference to the [`Overlay`] of the [`Element`],
pub fn as_overlay(&self) -> &dyn Overlay<Message, Theme, Renderer> {
self.overlay.as_ref()
}
/// Returns a mutable reference to the [`Overlay`] of the [`Element`],
pub fn as_overlay_mut(
&mut self,
) -> &mut dyn Overlay<Message, Theme, Renderer> {
self.overlay.as_mut()
}
/// Applies a transformation to the produced message of the [`Element`].
pub fn map<B>(
self,
@ -38,82 +50,6 @@ where
overlay: Box::new(Map::new(self.overlay, f)),
}
}
/// Computes the layout of the [`Element`] in the given bounds.
pub fn layout(
&mut self,
renderer: &Renderer,
bounds: Size,
) -> layout::Node {
self.overlay.layout(renderer, bounds)
}
/// Processes a runtime [`Event`].
pub fn update(
&mut self,
event: &Event,
layout: Layout<'_>,
cursor: mouse::Cursor,
renderer: &Renderer,
clipboard: &mut dyn Clipboard,
shell: &mut Shell<'_, Message>,
) {
self.overlay
.update(event, layout, cursor, renderer, clipboard, shell);
}
/// Returns the current [`mouse::Interaction`] of the [`Element`].
pub fn mouse_interaction(
&self,
layout: Layout<'_>,
cursor: mouse::Cursor,
viewport: &Rectangle,
renderer: &Renderer,
) -> mouse::Interaction {
self.overlay
.mouse_interaction(layout, cursor, viewport, renderer)
}
/// Draws the [`Element`] and its children using the given [`Layout`].
pub fn draw(
&self,
renderer: &mut Renderer,
theme: &Theme,
style: &renderer::Style,
layout: Layout<'_>,
cursor: mouse::Cursor,
) {
self.overlay.draw(renderer, theme, style, layout, cursor);
}
/// Applies a [`widget::Operation`] to the [`Element`].
pub fn operate(
&mut self,
layout: Layout<'_>,
renderer: &Renderer,
operation: &mut dyn widget::Operation,
) {
self.overlay.operate(layout, renderer, operation);
}
/// Returns true if the cursor is over the [`Element`].
pub fn is_over(
&self,
layout: Layout<'_>,
renderer: &Renderer,
cursor_position: Point,
) -> bool {
self.overlay.is_over(layout, renderer, cursor_position)
}
/// Returns the nested overlay of the [`Element`], if there is any.
pub fn overlay<'b>(
&'b mut self,
layout: Layout<'_>,
renderer: &Renderer,
) -> Option<Element<'b, Message, Theme, Renderer>> {
self.overlay.overlay(layout, renderer)
}
}
struct Map<'a, A, B, Theme, Renderer> {

View file

@ -25,8 +25,17 @@ where
/// Creates a [`Group`] with the given elements.
pub fn with_children(
children: Vec<overlay::Element<'a, Message, Theme, Renderer>>,
mut children: Vec<overlay::Element<'a, Message, Theme, Renderer>>,
) -> Self {
use std::cmp;
children.sort_unstable_by(|a, b| {
a.as_overlay()
.index()
.partial_cmp(&b.as_overlay().index())
.unwrap_or(cmp::Ordering::Equal)
});
Group { children }
}
@ -67,7 +76,7 @@ where
bounds,
self.children
.iter_mut()
.map(|child| child.layout(renderer, bounds))
.map(|child| child.as_overlay_mut().layout(renderer, bounds))
.collect(),
)
}
@ -82,7 +91,9 @@ where
shell: &mut Shell<'_, Message>,
) {
for (child, layout) in self.children.iter_mut().zip(layout.children()) {
child.update(event, layout, cursor, renderer, clipboard, shell);
child
.as_overlay_mut()
.update(event, layout, cursor, renderer, clipboard, shell);
}
}
@ -95,7 +106,9 @@ where
cursor: mouse::Cursor,
) {
for (child, layout) in self.children.iter().zip(layout.children()) {
child.draw(renderer, theme, style, layout, cursor);
child
.as_overlay()
.draw(renderer, theme, style, layout, cursor);
}
}
@ -110,7 +123,9 @@ where
.iter()
.zip(layout.children())
.map(|(child, layout)| {
child.mouse_interaction(layout, cursor, viewport, renderer)
child
.as_overlay()
.mouse_interaction(layout, cursor, viewport, renderer)
})
.max()
.unwrap_or_default()
@ -125,7 +140,7 @@ where
operation.container(None, layout.bounds(), &mut |operation| {
self.children.iter_mut().zip(layout.children()).for_each(
|(child, layout)| {
child.operate(layout, renderer, operation);
child.as_overlay_mut().operate(layout, renderer, operation);
},
);
});
@ -141,7 +156,9 @@ where
.iter()
.zip(layout.children())
.any(|(child, layout)| {
child.is_over(layout, renderer, cursor_position)
child
.as_overlay()
.is_over(layout, renderer, cursor_position)
})
}
@ -154,11 +171,20 @@ where
.children
.iter_mut()
.zip(layout.children())
.filter_map(|(child, layout)| child.overlay(layout, renderer))
.filter_map(|(child, layout)| {
child.as_overlay_mut().overlay(layout, renderer)
})
.collect::<Vec<_>>();
(!children.is_empty()).then(|| Group::with_children(children).overlay())
}
fn index(&self) -> f32 {
self.children
.first()
.map(|child| child.as_overlay().index())
.unwrap_or(1.0)
}
}
impl<'a, Message, Theme, Renderer> From<Group<'a, Message, Theme, Renderer>>