2021-03-10 04:00:51 +00:00
|
|
|
use crate::{
|
2021-03-26 09:28:05 +00:00
|
|
|
geometry::{rect::RectF, vector::Vector2F},
|
2021-04-07 05:50:13 +00:00
|
|
|
json::{self, json, ToJson},
|
|
|
|
AfterLayoutContext, DebugContext, Element, ElementBox, Event, EventContext, LayoutContext,
|
|
|
|
PaintContext, SizeConstraint,
|
2021-03-10 04:00:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
pub struct Stack {
|
2021-03-22 02:54:23 +00:00
|
|
|
children: Vec<ElementBox>,
|
2021-03-10 04:00:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Stack {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Stack {
|
|
|
|
children: Vec::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Element for Stack {
|
2021-03-22 02:54:23 +00:00
|
|
|
type LayoutState = ();
|
|
|
|
type PaintState = ();
|
|
|
|
|
2021-03-10 04:00:51 +00:00
|
|
|
fn layout(
|
|
|
|
&mut self,
|
|
|
|
constraint: SizeConstraint,
|
|
|
|
ctx: &mut LayoutContext,
|
2021-03-22 02:54:23 +00:00
|
|
|
) -> (Vector2F, Self::LayoutState) {
|
2021-03-10 04:00:51 +00:00
|
|
|
let mut size = constraint.min;
|
|
|
|
for child in &mut self.children {
|
2021-03-22 02:54:23 +00:00
|
|
|
size = size.max(child.layout(constraint, ctx));
|
2021-03-10 04:00:51 +00:00
|
|
|
}
|
2021-03-22 02:54:23 +00:00
|
|
|
(size, ())
|
2021-03-10 04:00:51 +00:00
|
|
|
}
|
|
|
|
|
2021-03-22 02:54:23 +00:00
|
|
|
fn after_layout(
|
|
|
|
&mut self,
|
|
|
|
_: Vector2F,
|
|
|
|
_: &mut Self::LayoutState,
|
|
|
|
ctx: &mut AfterLayoutContext,
|
|
|
|
) {
|
2021-03-10 04:00:51 +00:00
|
|
|
for child in &mut self.children {
|
2021-03-22 02:54:23 +00:00
|
|
|
child.after_layout(ctx);
|
2021-03-10 04:00:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-22 02:54:23 +00:00
|
|
|
fn paint(
|
|
|
|
&mut self,
|
2021-03-26 09:28:05 +00:00
|
|
|
bounds: RectF,
|
2021-03-22 02:54:23 +00:00
|
|
|
_: &mut Self::LayoutState,
|
|
|
|
ctx: &mut PaintContext,
|
|
|
|
) -> Self::PaintState {
|
2021-03-10 04:00:51 +00:00
|
|
|
for child in &mut self.children {
|
2021-03-26 09:28:05 +00:00
|
|
|
ctx.scene.push_layer(None);
|
2021-03-22 02:54:23 +00:00
|
|
|
child.paint(bounds.origin(), ctx);
|
2021-03-22 17:55:53 +00:00
|
|
|
ctx.scene.pop_layer();
|
2021-03-10 04:00:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-22 02:54:23 +00:00
|
|
|
fn dispatch_event(
|
|
|
|
&mut self,
|
|
|
|
event: &Event,
|
2021-03-26 09:28:05 +00:00
|
|
|
_: RectF,
|
2021-03-22 02:54:23 +00:00
|
|
|
_: &mut Self::LayoutState,
|
|
|
|
_: &mut Self::PaintState,
|
|
|
|
ctx: &mut EventContext,
|
|
|
|
) -> bool {
|
|
|
|
for child in self.children.iter_mut().rev() {
|
|
|
|
if child.dispatch_event(event, ctx) {
|
2021-03-10 04:00:51 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
false
|
|
|
|
}
|
2021-04-07 05:50:13 +00:00
|
|
|
|
|
|
|
fn debug(
|
|
|
|
&self,
|
|
|
|
bounds: RectF,
|
|
|
|
_: &Self::LayoutState,
|
|
|
|
_: &Self::PaintState,
|
|
|
|
ctx: &DebugContext,
|
|
|
|
) -> json::Value {
|
|
|
|
json!({
|
|
|
|
"type": "Stack",
|
|
|
|
"bounds": bounds.to_json(),
|
|
|
|
"children": self.children.iter().map(|child| child.debug(ctx)).collect::<Vec<json::Value>>()
|
|
|
|
})
|
|
|
|
}
|
2021-03-10 04:00:51 +00:00
|
|
|
}
|
|
|
|
|
2021-03-22 02:54:23 +00:00
|
|
|
impl Extend<ElementBox> for Stack {
|
|
|
|
fn extend<T: IntoIterator<Item = ElementBox>>(&mut self, children: T) {
|
2021-03-10 04:00:51 +00:00
|
|
|
self.children.extend(children)
|
|
|
|
}
|
|
|
|
}
|