zed/crates/gpui/src/elements/stack.rs

106 lines
2.6 KiB
Rust
Raw Normal View History

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},
json::{self, json, ToJson},
2023-04-21 19:09:36 +00:00
AnyElement, Element, SceneBuilder, SizeConstraint, View, ViewContext,
2021-03-10 04:00:51 +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<AnyElement<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 {
Self::default()
2021-03-10 04:00:51 +00:00
}
}
impl<V: View> Element<V> for Stack<V> {
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>,
) -> (Vector2F, Self::LayoutState) {
2021-03-10 04:00:51 +00:00
let mut size = constraint.min;
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);
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
}
(size, ())
2021-03-10 04:00:51 +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,
visible_bounds: RectF,
_: &mut Self::LayoutState,
2023-04-10 23:27:47 +00:00
view: &mut V,
cx: &mut ViewContext<V>,
) -> 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);
});
2021-03-10 04:00:51 +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>,
) -> 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))
}
fn debug(
&self,
bounds: RectF,
_: &Self::LayoutState,
_: &Self::PaintState,
2023-04-10 23:27:47 +00:00
view: &V,
cx: &ViewContext<V>,
) -> 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-03-10 04:00:51 +00:00
}
impl<V: View> Extend<AnyElement<V>> for Stack<V> {
fn extend<T: IntoIterator<Item = AnyElement<V>>>(&mut self, children: T) {
2021-03-10 04:00:51 +00:00
self.children.extend(children)
}
}