Split playground into gpui2 and storybook
This commit is contained in:
parent
ee11be98e5
commit
3b5ee59273
41 changed files with 175 additions and 2210 deletions
116
crates/gpui2/src/elements/div.rs
Normal file
116
crates/gpui2/src/elements/div.rs
Normal file
|
@ -0,0 +1,116 @@
|
|||
use crate::{
|
||||
element::{AnyElement, Element, IntoElement, Layout, ParentElement},
|
||||
layout_context::LayoutContext,
|
||||
paint_context::PaintContext,
|
||||
style::{Style, StyleHelpers, Styleable},
|
||||
InteractionHandlers, Interactive,
|
||||
};
|
||||
use anyhow::Result;
|
||||
use gpui::{LayoutId, RenderContext};
|
||||
use refineable::{Refineable, RefinementCascade};
|
||||
use smallvec::SmallVec;
|
||||
|
||||
pub struct Div<V: 'static> {
|
||||
styles: RefinementCascade<Style>,
|
||||
handlers: InteractionHandlers<V>,
|
||||
children: SmallVec<[AnyElement<V>; 2]>,
|
||||
}
|
||||
|
||||
pub fn div<V>() -> Div<V> {
|
||||
Div {
|
||||
styles: Default::default(),
|
||||
handlers: Default::default(),
|
||||
children: Default::default(),
|
||||
}
|
||||
}
|
||||
|
||||
impl<V: 'static> Element<V> for Div<V> {
|
||||
type PaintState = ();
|
||||
|
||||
fn layout(
|
||||
&mut self,
|
||||
view: &mut V,
|
||||
cx: &mut LayoutContext<V>,
|
||||
) -> Result<(LayoutId, Self::PaintState)>
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
let style = self.computed_style();
|
||||
let pop_text_style = style.text_style().map_or(false, |style| {
|
||||
cx.push_text_style(cx.text_style().clone().refined(&style));
|
||||
true
|
||||
});
|
||||
|
||||
let children = self
|
||||
.children
|
||||
.iter_mut()
|
||||
.map(|child| child.layout(view, cx))
|
||||
.collect::<Result<Vec<LayoutId>>>()?;
|
||||
|
||||
if pop_text_style {
|
||||
cx.pop_text_style();
|
||||
}
|
||||
|
||||
Ok((cx.add_layout_node(style, children)?, ()))
|
||||
}
|
||||
|
||||
fn paint(
|
||||
&mut self,
|
||||
view: &mut V,
|
||||
layout: &Layout,
|
||||
_: &mut Self::PaintState,
|
||||
cx: &mut PaintContext<V>,
|
||||
) where
|
||||
Self: Sized,
|
||||
{
|
||||
let style = &self.computed_style();
|
||||
let pop_text_style = style.text_style().map_or(false, |style| {
|
||||
let style = cx.text_style().clone().refined(&style);
|
||||
cx.push_text_style(style);
|
||||
true
|
||||
});
|
||||
style.paint_background(layout.bounds, cx);
|
||||
self.interaction_handlers()
|
||||
.paint(layout.order, layout.bounds, cx);
|
||||
for child in &mut self.children {
|
||||
child.paint(view, layout.bounds.origin(), cx);
|
||||
}
|
||||
if pop_text_style {
|
||||
cx.pop_text_style();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<V> Styleable for Div<V> {
|
||||
type Style = Style;
|
||||
|
||||
fn style_cascade(&mut self) -> &mut RefinementCascade<Self::Style> {
|
||||
&mut self.styles
|
||||
}
|
||||
|
||||
fn declared_style(&mut self) -> &mut <Self::Style as Refineable>::Refinement {
|
||||
self.styles.base()
|
||||
}
|
||||
}
|
||||
|
||||
impl<V> StyleHelpers for Div<V> {}
|
||||
|
||||
impl<V> Interactive<V> for Div<V> {
|
||||
fn interaction_handlers(&mut self) -> &mut InteractionHandlers<V> {
|
||||
&mut self.handlers
|
||||
}
|
||||
}
|
||||
|
||||
impl<V: 'static> ParentElement<V> for Div<V> {
|
||||
fn children_mut(&mut self) -> &mut SmallVec<[AnyElement<V>; 2]> {
|
||||
&mut self.children
|
||||
}
|
||||
}
|
||||
|
||||
impl<V: 'static> IntoElement<V> for Div<V> {
|
||||
type Element = Self;
|
||||
|
||||
fn into_element(self) -> Self::Element {
|
||||
self
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue