This commit is contained in:
Nathan Sobo 2023-04-10 17:27:47 -06:00
parent 6638407ff9
commit 3de8fe0f87
21 changed files with 675 additions and 694 deletions

View file

@ -3,41 +3,41 @@ use std::ops::Range;
use crate::{
geometry::{rect::RectF, vector::Vector2F},
json::{self, json, ToJson},
window::MeasurementContext,
DebugContext, Element, ElementBox, LayoutContext, PaintContext, SizeConstraint,
Element, ElementBox, SceneBuilder, SizeConstraint, View, ViewContext,
};
/// Element which renders it's children in a stack on top of each other.
/// The first child determines the size of the others.
#[derive(Default)]
pub struct Stack {
children: Vec<ElementBox>,
pub struct Stack<V: View> {
children: Vec<ElementBox<V>>,
}
impl Stack {
impl<V: View> Stack<V> {
pub fn new() -> Self {
Self::default()
}
}
impl Element for Stack {
impl<V: View> Element<V> for Stack<V> {
type LayoutState = ();
type PaintState = ();
fn layout(
&mut self,
mut constraint: SizeConstraint,
cx: &mut LayoutContext,
view: &mut V,
cx: &mut ViewContext<V>,
) -> (Vector2F, Self::LayoutState) {
let mut size = constraint.min;
let mut children = self.children.iter_mut();
if let Some(bottom_child) = children.next() {
size = bottom_child.layout(constraint, cx);
size = bottom_child.layout(constraint, view, cx);
constraint = SizeConstraint::strict(size);
}
for child in children {
child.layout(constraint, cx);
child.layout(constraint, view, cx);
}
(size, ())
@ -45,14 +45,16 @@ impl Element for Stack {
fn paint(
&mut self,
scene: &mut SceneBuilder,
bounds: RectF,
visible_bounds: RectF,
_: &mut Self::LayoutState,
cx: &mut PaintContext,
view: &mut V,
cx: &mut ViewContext<V>,
) -> Self::PaintState {
for child in &mut self.children {
cx.paint_layer(None, |cx| {
child.paint(bounds.origin(), visible_bounds, cx);
child.paint(scene, bounds.origin(), visible_bounds, view, cx);
});
}
}
@ -64,12 +66,13 @@ impl Element for Stack {
_: RectF,
_: &Self::LayoutState,
_: &Self::PaintState,
cx: &MeasurementContext,
view: &V,
cx: &ViewContext<V>,
) -> Option<RectF> {
self.children
.iter()
.rev()
.find_map(|child| child.rect_for_text_range(range_utf16.clone(), cx))
.find_map(|child| child.rect_for_text_range(range_utf16.clone(), view, cx))
}
fn debug(
@ -77,18 +80,19 @@ impl Element for Stack {
bounds: RectF,
_: &Self::LayoutState,
_: &Self::PaintState,
cx: &DebugContext,
view: &V,
cx: &ViewContext<V>,
) -> json::Value {
json!({
"type": "Stack",
"bounds": bounds.to_json(),
"children": self.children.iter().map(|child| child.debug(cx)).collect::<Vec<json::Value>>()
"children": self.children.iter().map(|child| child.debug(view, cx)).collect::<Vec<json::Value>>()
})
}
}
impl Extend<ElementBox> for Stack {
fn extend<T: IntoIterator<Item = ElementBox>>(&mut self, children: T) {
impl<V: View> Extend<ElementBox<V>> for Stack<V> {
fn extend<T: IntoIterator<Item = ElementBox<V>>>(&mut self, children: T) {
self.children.extend(children)
}
}