use crate::{ element::{AnyElement, Element, Layout, ParentElement}, interactive::{InteractionHandlers, Interactive}, layout_context::LayoutContext, paint_context::PaintContext, style::{Style, StyleHelpers, StyleRefinement, Styleable}, }; use anyhow::Result; use gpui::LayoutId; use smallvec::SmallVec; pub struct Div { style: StyleRefinement, handlers: InteractionHandlers, children: SmallVec<[AnyElement; 2]>, } pub fn div() -> Div { Div { style: Default::default(), handlers: Default::default(), children: Default::default(), } } impl Element for Div { type Layout = (); fn layout(&mut self, view: &mut V, cx: &mut LayoutContext) -> Result> where Self: Sized, { let children = self .children .iter_mut() .map(|child| child.layout(view, cx)) .collect::>>()?; cx.add_layout_node(self.style(), (), children) } fn paint(&mut self, view: &mut V, layout: &mut Layout, cx: &mut PaintContext) where Self: Sized, { let style = self.style(); style.paint_background::(layout, cx); for child in &mut self.children { child.paint(view, cx); } } } impl Styleable for Div { type Style = Style; fn declared_style(&mut self) -> &mut StyleRefinement { &mut self.style } } impl StyleHelpers for Div {} impl Interactive for Div { fn interaction_handlers(&mut self) -> &mut InteractionHandlers { &mut self.handlers } } impl ParentElement for Div { fn children_mut(&mut self) -> &mut SmallVec<[AnyElement; 2]> { &mut self.children } } #[test] fn test() { // let elt = div().w_auto(); } // trait Element { // type Style; // fn layout() // } // trait Stylable: Element { // type Style; // fn with_style(self, style: Self::Style) -> Self; // } // pub struct HoverStyle { // default: S, // hovered: S, // } // struct Hover> { // child: C, // style: HoverStyle, // } // impl> Hover { // fn new(child: C, style: HoverStyle) -> Self { // Self { child, style } // } // }