This commit is contained in:
Nathan Sobo 2023-08-12 10:00:08 -06:00
parent 4b4b949972
commit 0d31d6dac5
4 changed files with 35 additions and 41 deletions

View file

@ -18,8 +18,8 @@ use gpui::{
use length::{Length, Rems};
use log::warn;
use optional_struct::*;
use smallvec::SmallVec;
use std::{any::Any, borrow::Cow, f32, ops::Range, sync::Arc};
use taffy::prelude::Style as TaffyStyle;
use util::ResultExt;
use crate::color::{Hsla, Rgba};
@ -28,6 +28,7 @@ use self::length::rems;
pub struct Frame<V> {
style: FrameStyle,
metadata: FrameMetadata,
children: Vec<AnyElement<V>>,
id: Option<Cow<'static, str>>,
before_paint: Option<Box<dyn FnMut(RectF, &mut FrameLayout, &mut PaintContext<V>)>>,
@ -49,6 +50,7 @@ impl<V> Default for Frame<V> {
fn default() -> Self {
Self {
style: Default::default(),
metadata: Default::default(),
children: Default::default(),
id: None,
before_paint: None,
@ -76,6 +78,25 @@ impl<V: 'static> Element<V> for Frame<V> {
}
}
let mut child_node_ids = SmallVec::<[_; 2]>::new();
for child in &mut self.children {
child.layout(constraint, view, cx);
child_node_ids.extend(
child
.metadata::<FrameMetadata>()
.and_then(|meta| meta.layout_node_id),
);
}
self.metadata.layout_node_id = cx
.layout_engine()
.new_with_children(self.style.layout.clone(), child_node_ids.as_slice())
.log_err();
if pushed_text_style {
cx.pop_text_style();
}
(todo!(), todo!())
}
@ -196,7 +217,7 @@ impl<V: 'static> Element<V> for Frame<V> {
}
fn metadata(&self) -> Option<&dyn Any> {
Some(&self.style)
Some(&self.metadata)
}
}
@ -295,6 +316,11 @@ struct Interactive<Style> {
disabled: Style,
}
#[derive(Default)]
pub struct FrameMetadata {
layout_node_id: Option<taffy::tree::NodeId>,
}
#[derive(Clone, Default)]
pub struct FrameStyle {
text: OptionalTextStyle,
@ -303,7 +329,7 @@ pub struct FrameStyle {
borders: Borders,
corner_radius: f32,
shadows: Vec<Shadow>,
layout: TaffyStyle,
layout: taffy::style::Style,
}
#[optional_struct]
@ -1309,28 +1335,6 @@ impl Vector2FExt for Vector2F {
}
}
trait ElementExt<V: 'static> {
fn margin_left(self, margin_left: impl Into<Length>) -> Frame<V>
where
Self: Element<V> + Sized,
{
column().child(self).margin_left(margin_left)
}
}
impl<V, E> ElementExt<V> for E
where
V: 'static,
E: Element<V>,
{
fn margin_left(self, margin_left: impl Into<Length>) -> Frame<V>
where
Self: Sized,
{
column().child(self).margin_left(margin_left)
}
}
pub fn view<F, E>(mut render: F) -> ViewFn
where
F: 'static + FnMut(&mut ViewContext<ViewFn>) -> E,