use crate::core::layout;
use crate::core::mouse;
use crate::core::overlay;
use crate::core::renderer;
use crate::core::widget::{Operation, Tree};
use crate::core::{
Clipboard, Element, Event, Layout, Length, Rectangle, Shell, Size, Vector,
Widget,
};
#[allow(missing_debug_implementations)]
pub struct Stack<'a, Message, Theme = crate::Theme, Renderer = crate::Renderer>
{
width: Length,
height: Length,
children: Vec<Element<'a, Message, Theme, Renderer>>,
}
impl<'a, Message, Theme, Renderer> Stack<'a, Message, Theme, Renderer>
where
Renderer: crate::core::Renderer,
{
pub fn new() -> Self {
Self::from_vec(Vec::new())
}
pub fn with_capacity(capacity: usize) -> Self {
Self::from_vec(Vec::with_capacity(capacity))
}
pub fn with_children(
children: impl IntoIterator<Item = Element<'a, Message, Theme, Renderer>>,
) -> Self {
let iterator = children.into_iter();
Self::with_capacity(iterator.size_hint().0).extend(iterator)
}
pub fn from_vec(
children: Vec<Element<'a, Message, Theme, Renderer>>,
) -> Self {
Self {
width: Length::Shrink,
height: Length::Shrink,
children,
}
}
pub fn width(mut self, width: impl Into<Length>) -> Self {
self.width = width.into();
self
}
pub fn height(mut self, height: impl Into<Length>) -> Self {
self.height = height.into();
self
}
pub fn push(
mut self,
child: impl Into<Element<'a, Message, Theme, Renderer>>,
) -> Self {
let child = child.into();
if self.children.is_empty() {
let child_size = child.as_widget().size_hint();
self.width = self.width.enclose(child_size.width);
self.height = self.height.enclose(child_size.height);
}
self.children.push(child);
self
}
pub fn push_maybe(
self,
child: Option<impl Into<Element<'a, Message, Theme, Renderer>>>,
) -> Self {
if let Some(child) = child {
self.push(child)
} else {
self
}
}
pub fn extend(
self,
children: impl IntoIterator<Item = Element<'a, Message, Theme, Renderer>>,
) -> Self {
children.into_iter().fold(self, Self::push)
}
}
impl<'a, Message, Renderer> Default for Stack<'a, Message, Renderer>
where
Renderer: crate::core::Renderer,
{
fn default() -> Self {
Self::new()
}
}
impl<'a, Message, Theme, Renderer> Widget<Message, Theme, Renderer>
for Stack<'a, Message, Theme, Renderer>
where
Renderer: crate::core::Renderer,
{
fn children(&self) -> Vec<Tree> {
self.children.iter().map(Tree::new).collect()
}
fn diff(&self, tree: &mut Tree) {
tree.diff_children(&self.children);
}
fn size(&self) -> Size<Length> {
Size {
width: self.width,
height: self.height,
}
}
fn layout(
&self,
tree: &mut Tree,
renderer: &Renderer,
limits: &layout::Limits,
) -> layout::Node {
let limits = limits.width(self.width).height(self.height);
if self.children.is_empty() {
return layout::Node::new(limits.resolve(
self.width,
self.height,
Size::ZERO,
));
}
let base = self.children[0].as_widget().layout(
&mut tree.children[0],
renderer,
&limits,
);
let size = limits.resolve(self.width, self.height, base.size());
let limits = layout::Limits::new(Size::ZERO, size);
let nodes = std::iter::once(base)
.chain(self.children[1..].iter().zip(&mut tree.children[1..]).map(
|(layer, tree)| {
let node =
layer.as_widget().layout(tree, renderer, &limits);
node
},
))
.collect();
layout::Node::with_children(size, nodes)
}
fn operate(
&self,
tree: &mut Tree,
layout: Layout<'_>,
renderer: &Renderer,
operation: &mut dyn Operation,
) {
operation.container(None, layout.bounds(), &mut |operation| {
self.children
.iter()
.zip(&mut tree.children)
.zip(layout.children())
.for_each(|((child, state), layout)| {
child
.as_widget()
.operate(state, layout, renderer, operation);
});
});
}
fn update(
&mut self,
tree: &mut Tree,
event: Event,
layout: Layout<'_>,
mut cursor: mouse::Cursor,
renderer: &Renderer,
clipboard: &mut dyn Clipboard,
shell: &mut Shell<'_, Message>,
viewport: &Rectangle,
) {
let is_over = cursor.is_over(layout.bounds());
for ((child, state), layout) in self
.children
.iter_mut()
.rev()
.zip(tree.children.iter_mut().rev())
.zip(layout.children().rev())
{
child.as_widget_mut().update(
state,
event.clone(),
layout,
cursor,
renderer,
clipboard,
shell,
viewport,
);
if is_over && cursor != mouse::Cursor::Unavailable {
let interaction = child.as_widget().mouse_interaction(
state, layout, cursor, viewport, renderer,
);
if interaction != mouse::Interaction::None {
cursor = mouse::Cursor::Unavailable;
}
}
if shell.is_event_captured() {
return;
}
}
}
fn mouse_interaction(
&self,
tree: &Tree,
layout: Layout<'_>,
cursor: mouse::Cursor,
viewport: &Rectangle,
renderer: &Renderer,
) -> mouse::Interaction {
self.children
.iter()
.rev()
.zip(tree.children.iter().rev())
.zip(layout.children().rev())
.map(|((child, state), layout)| {
child.as_widget().mouse_interaction(
state, layout, cursor, viewport, renderer,
)
})
.find(|&interaction| interaction != mouse::Interaction::None)
.unwrap_or_default()
}
fn draw(
&self,
tree: &Tree,
renderer: &mut Renderer,
theme: &Theme,
style: &renderer::Style,
layout: Layout<'_>,
cursor: mouse::Cursor,
viewport: &Rectangle,
) {
if let Some(clipped_viewport) = layout.bounds().intersection(viewport) {
let layers_below = if cursor.is_over(layout.bounds()) {
self.children
.iter()
.rev()
.zip(tree.children.iter().rev())
.zip(layout.children().rev())
.position(|((layer, state), layout)| {
let interaction = layer.as_widget().mouse_interaction(
state, layout, cursor, viewport, renderer,
);
interaction != mouse::Interaction::None
})
.map(|i| self.children.len() - i - 1)
.unwrap_or_default()
} else {
0
};
let mut layers = self
.children
.iter()
.zip(&tree.children)
.zip(layout.children())
.enumerate();
let layers = layers.by_ref();
let mut draw_layer =
|i,
layer: &Element<'a, Message, Theme, Renderer>,
state,
layout,
cursor| {
if i > 0 {
renderer.with_layer(clipped_viewport, |renderer| {
layer.as_widget().draw(
state,
renderer,
theme,
style,
layout,
cursor,
&clipped_viewport,
);
});
} else {
layer.as_widget().draw(
state,
renderer,
theme,
style,
layout,
cursor,
&clipped_viewport,
);
}
};
for (i, ((layer, state), layout)) in layers.take(layers_below) {
draw_layer(i, layer, state, layout, mouse::Cursor::Unavailable);
}
for (i, ((layer, state), layout)) in layers {
draw_layer(i, layer, state, layout, cursor);
}
}
}
fn overlay<'b>(
&'b mut self,
tree: &'b mut Tree,
layout: Layout<'_>,
renderer: &Renderer,
translation: Vector,
) -> Option<overlay::Element<'b, Message, Theme, Renderer>> {
overlay::from_children(
&mut self.children,
tree,
layout,
renderer,
translation,
)
}
}
impl<'a, Message, Theme, Renderer> From<Stack<'a, Message, Theme, Renderer>>
for Element<'a, Message, Theme, Renderer>
where
Message: 'a,
Theme: 'a,
Renderer: crate::core::Renderer + 'a,
{
fn from(stack: Stack<'a, Message, Theme, Renderer>) -> Self {
Self::new(stack)
}
}