2022-07-21 15:35:40 +00:00
|
|
|
use std::ops::Range;
|
|
|
|
|
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},
|
2023-04-10 23:27:47 +00:00
|
|
|
Element, ElementBox, SceneBuilder, SizeConstraint, View, ViewContext,
|
2021-03-10 04:00:51 +00:00
|
|
|
};
|
|
|
|
|
2022-10-25 06:47:43 +00:00
|
|
|
/// Element which renders it's children in a stack on top of each other.
|
|
|
|
/// The first child determines the size of the others.
|
2023-04-10 23:27:47 +00:00
|
|
|
pub struct Stack<V: View> {
|
|
|
|
children: Vec<ElementBox<V>>,
|
2021-03-10 04:00:51 +00:00
|
|
|
}
|
|
|
|
|
2023-04-12 00:21:56 +00:00
|
|
|
impl<V: View> Default for Stack<V> {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
children: Vec::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-10 23:27:47 +00:00
|
|
|
impl<V: View> Stack<V> {
|
2021-03-10 04:00:51 +00:00
|
|
|
pub fn new() -> Self {
|
2022-08-10 21:39:24 +00:00
|
|
|
Self::default()
|
2021-03-10 04:00:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-10 23:27:47 +00:00
|
|
|
impl<V: View> Element<V> for Stack<V> {
|
2021-03-22 02:54:23 +00:00
|
|
|
type LayoutState = ();
|
|
|
|
type PaintState = ();
|
|
|
|
|
2021-03-10 04:00:51 +00:00
|
|
|
fn layout(
|
|
|
|
&mut self,
|
2022-10-23 04:32:07 +00:00
|
|
|
mut constraint: SizeConstraint,
|
2023-04-10 23:27:47 +00:00
|
|
|
view: &mut V,
|
|
|
|
cx: &mut ViewContext<V>,
|
2021-03-22 02:54:23 +00:00
|
|
|
) -> (Vector2F, Self::LayoutState) {
|
2021-03-10 04:00:51 +00:00
|
|
|
let mut size = constraint.min;
|
2022-10-25 06:47:43 +00:00
|
|
|
let mut children = self.children.iter_mut();
|
|
|
|
if let Some(bottom_child) = children.next() {
|
2023-04-10 23:27:47 +00:00
|
|
|
size = bottom_child.layout(constraint, view, cx);
|
2022-10-25 06:47:43 +00:00
|
|
|
constraint = SizeConstraint::strict(size);
|
|
|
|
}
|
|
|
|
|
|
|
|
for child in children {
|
2023-04-10 23:27:47 +00:00
|
|
|
child.layout(constraint, view, cx);
|
2021-03-10 04:00:51 +00:00
|
|
|
}
|
2022-10-25 06:47:43 +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 paint(
|
|
|
|
&mut self,
|
2023-04-10 23:27:47 +00:00
|
|
|
scene: &mut SceneBuilder,
|
2021-03-26 09:28:05 +00:00
|
|
|
bounds: RectF,
|
2021-09-02 09:42:23 +00:00
|
|
|
visible_bounds: RectF,
|
2021-03-22 02:54:23 +00:00
|
|
|
_: &mut Self::LayoutState,
|
2023-04-10 23:27:47 +00:00
|
|
|
view: &mut V,
|
|
|
|
cx: &mut ViewContext<V>,
|
2021-03-22 02:54:23 +00:00
|
|
|
) -> Self::PaintState {
|
2021-03-10 04:00:51 +00:00
|
|
|
for child in &mut self.children {
|
2023-04-12 00:21:56 +00:00
|
|
|
scene.paint_layer(None, |scene| {
|
2023-04-10 23:27:47 +00:00
|
|
|
child.paint(scene, bounds.origin(), visible_bounds, view, cx);
|
2022-10-21 19:09:14 +00:00
|
|
|
});
|
2021-03-10 04:00:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-21 15:35:40 +00:00
|
|
|
fn rect_for_text_range(
|
|
|
|
&self,
|
|
|
|
range_utf16: Range<usize>,
|
|
|
|
_: RectF,
|
|
|
|
_: RectF,
|
|
|
|
_: &Self::LayoutState,
|
|
|
|
_: &Self::PaintState,
|
2023-04-10 23:27:47 +00:00
|
|
|
view: &V,
|
|
|
|
cx: &ViewContext<V>,
|
2022-07-21 15:35:40 +00:00
|
|
|
) -> Option<RectF> {
|
|
|
|
self.children
|
|
|
|
.iter()
|
|
|
|
.rev()
|
2023-04-10 23:27:47 +00:00
|
|
|
.find_map(|child| child.rect_for_text_range(range_utf16.clone(), view, cx))
|
2022-07-21 15:35:40 +00:00
|
|
|
}
|
|
|
|
|
2021-04-07 05:50:13 +00:00
|
|
|
fn debug(
|
|
|
|
&self,
|
|
|
|
bounds: RectF,
|
|
|
|
_: &Self::LayoutState,
|
|
|
|
_: &Self::PaintState,
|
2023-04-10 23:27:47 +00:00
|
|
|
view: &V,
|
|
|
|
cx: &ViewContext<V>,
|
2021-04-07 05:50:13 +00:00
|
|
|
) -> json::Value {
|
|
|
|
json!({
|
|
|
|
"type": "Stack",
|
|
|
|
"bounds": bounds.to_json(),
|
2023-04-10 23:27:47 +00:00
|
|
|
"children": self.children.iter().map(|child| child.debug(view, cx)).collect::<Vec<json::Value>>()
|
2021-04-07 05:50:13 +00:00
|
|
|
})
|
|
|
|
}
|
2021-03-10 04:00:51 +00:00
|
|
|
}
|
|
|
|
|
2023-04-10 23:27:47 +00:00
|
|
|
impl<V: View> Extend<ElementBox<V>> for Stack<V> {
|
|
|
|
fn extend<T: IntoIterator<Item = ElementBox<V>>>(&mut self, children: T) {
|
2021-03-10 04:00:51 +00:00
|
|
|
self.children.extend(children)
|
|
|
|
}
|
|
|
|
}
|