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

86 lines
2 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},
presenter::MeasurementContext,
DebugContext, Element, ElementBox, LayoutContext, PaintContext, SizeConstraint,
2021-03-10 04:00:51 +00:00
};
#[derive(Default)]
2021-03-10 04:00:51 +00:00
pub struct Stack {
children: Vec<ElementBox>,
2021-03-10 04:00:51 +00:00
}
impl Stack {
pub fn new() -> Self {
Self::default()
2021-03-10 04:00:51 +00:00
}
}
impl Element for Stack {
type LayoutState = ();
type PaintState = ();
2021-03-10 04:00:51 +00:00
fn layout(
&mut self,
constraint: SizeConstraint,
cx: &mut LayoutContext,
) -> (Vector2F, Self::LayoutState) {
2021-03-10 04:00:51 +00:00
let mut size = constraint.min;
for child in &mut self.children {
size = size.max(child.layout(constraint, cx));
2021-03-10 04:00:51 +00:00
}
(size, ())
2021-03-10 04:00:51 +00:00
}
fn paint(
&mut self,
2021-03-26 09:28:05 +00:00
bounds: RectF,
visible_bounds: RectF,
_: &mut Self::LayoutState,
cx: &mut PaintContext,
) -> Self::PaintState {
2021-03-10 04:00:51 +00:00
for child in &mut self.children {
cx.scene.push_layer(None);
child.paint(bounds.origin(), visible_bounds, cx);
cx.scene.pop_layer();
2021-03-10 04:00:51 +00:00
}
}
fn rect_for_text_range(
&self,
range_utf16: Range<usize>,
_: RectF,
_: RectF,
_: &Self::LayoutState,
_: &Self::PaintState,
cx: &MeasurementContext,
) -> Option<RectF> {
self.children
.iter()
.rev()
.find_map(|child| child.rect_for_text_range(range_utf16.clone(), cx))
}
fn debug(
&self,
bounds: RectF,
_: &Self::LayoutState,
_: &Self::PaintState,
cx: &DebugContext,
) -> json::Value {
json!({
"type": "Stack",
"bounds": bounds.to_json(),
"children": self.children.iter().map(|child| child.debug(cx)).collect::<Vec<json::Value>>()
})
}
2021-03-10 04:00:51 +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)
}
}