Introduce Refinement trait and derive macro

This commit is contained in:
Nathan Sobo 2023-08-18 01:03:46 -06:00
parent 19ccb19c96
commit 9b74dc196e
22 changed files with 6164 additions and 244 deletions

View file

@ -1,12 +1,13 @@
use crate::{
adapter::Adapter,
color::Hsla,
style::{Display, ElementStyle, Fill, Overflow, Position},
hoverable::Hoverable,
style::{Display, Fill, Overflow, Position, StyleRefinement},
};
use anyhow::Result;
pub use gpui::LayoutContext;
use gpui::{
geometry::{DefinedLength, Length},
geometry::{DefinedLength, Length, PointRefinement},
platform::{MouseButton, MouseButtonEvent},
EngineLayout, EventContext, RenderContext, ViewContext,
};
@ -26,7 +27,7 @@ pub struct Layout<'a, E: ?Sized> {
}
pub struct ElementMetadata<V> {
pub style: ElementStyle,
pub style: StyleRefinement,
pub handlers: Vec<EventHandler<V>>,
}
@ -49,7 +50,7 @@ impl<V> Clone for EventHandler<V> {
impl<V> Default for ElementMetadata<V> {
fn default() -> Self {
Self {
style: ElementStyle::default(),
style: StyleRefinement::default(),
handlers: Vec::new(),
}
}
@ -58,11 +59,17 @@ impl<V> Default for ElementMetadata<V> {
pub trait Element<V: 'static>: 'static {
type Layout: 'static;
fn style_mut(&mut self) -> &mut ElementStyle;
fn declared_style(&mut self) -> &mut StyleRefinement;
fn computed_style(&mut self) -> &StyleRefinement {
self.declared_style()
}
fn handlers_mut(&mut self) -> &mut Vec<EventHandler<V>>;
fn layout(&mut self, view: &mut V, cx: &mut LayoutContext<V>)
-> Result<(NodeId, Self::Layout)>;
fn paint<'a>(
&mut self,
layout: Layout<Self::Layout>,
@ -208,7 +215,7 @@ pub trait Element<V: 'static>: 'static {
where
Self: Sized,
{
self.style_mut().display = Display::Block;
self.declared_style().display = Some(Display::Block);
self
}
@ -216,7 +223,7 @@ pub trait Element<V: 'static>: 'static {
where
Self: Sized,
{
self.style_mut().display = Display::Flex;
self.declared_style().display = Some(Display::Flex);
self
}
@ -224,7 +231,7 @@ pub trait Element<V: 'static>: 'static {
where
Self: Sized,
{
self.style_mut().display = Display::Grid;
self.declared_style().display = Some(Display::Grid);
self
}
@ -234,8 +241,10 @@ pub trait Element<V: 'static>: 'static {
where
Self: Sized,
{
self.style_mut().overflow.x = Overflow::Visible;
self.style_mut().overflow.y = Overflow::Visible;
self.declared_style().overflow = PointRefinement {
x: Some(Overflow::Visible),
y: Some(Overflow::Visible),
};
self
}
@ -243,8 +252,10 @@ pub trait Element<V: 'static>: 'static {
where
Self: Sized,
{
self.style_mut().overflow.x = Overflow::Hidden;
self.style_mut().overflow.y = Overflow::Hidden;
self.declared_style().overflow = PointRefinement {
x: Some(Overflow::Hidden),
y: Some(Overflow::Hidden),
};
self
}
@ -252,8 +263,10 @@ pub trait Element<V: 'static>: 'static {
where
Self: Sized,
{
self.style_mut().overflow.x = Overflow::Scroll;
self.style_mut().overflow.y = Overflow::Scroll;
self.declared_style().overflow = PointRefinement {
x: Some(Overflow::Scroll),
y: Some(Overflow::Scroll),
};
self
}
@ -261,7 +274,7 @@ pub trait Element<V: 'static>: 'static {
where
Self: Sized,
{
self.style_mut().overflow.x = Overflow::Visible;
self.declared_style().overflow.x = Some(Overflow::Visible);
self
}
@ -269,7 +282,7 @@ pub trait Element<V: 'static>: 'static {
where
Self: Sized,
{
self.style_mut().overflow.x = Overflow::Hidden;
self.declared_style().overflow.x = Some(Overflow::Hidden);
self
}
@ -277,7 +290,7 @@ pub trait Element<V: 'static>: 'static {
where
Self: Sized,
{
self.style_mut().overflow.x = Overflow::Scroll;
self.declared_style().overflow.x = Some(Overflow::Scroll);
self
}
@ -285,7 +298,7 @@ pub trait Element<V: 'static>: 'static {
where
Self: Sized,
{
self.style_mut().overflow.y = Overflow::Visible;
self.declared_style().overflow.y = Some(Overflow::Visible);
self
}
@ -293,7 +306,7 @@ pub trait Element<V: 'static>: 'static {
where
Self: Sized,
{
self.style_mut().overflow.y = Overflow::Hidden;
self.declared_style().overflow.y = Some(Overflow::Hidden);
self
}
@ -301,7 +314,7 @@ pub trait Element<V: 'static>: 'static {
where
Self: Sized,
{
self.style_mut().overflow.y = Overflow::Scroll;
self.declared_style().overflow.y = Some(Overflow::Scroll);
self
}
@ -311,7 +324,7 @@ pub trait Element<V: 'static>: 'static {
where
Self: Sized,
{
self.style_mut().position = Position::Relative;
self.declared_style().position = Some(Position::Relative);
self
}
@ -319,7 +332,7 @@ pub trait Element<V: 'static>: 'static {
where
Self: Sized,
{
self.style_mut().position = Position::Absolute;
self.declared_style().position = Some(Position::Absolute);
self
}
@ -329,10 +342,11 @@ pub trait Element<V: 'static>: 'static {
where
Self: Sized,
{
self.style_mut().inset.top = length;
self.style_mut().inset.right = length;
self.style_mut().inset.bottom = length;
self.style_mut().inset.left = length;
let inset = &mut self.declared_style().inset;
inset.top = Some(length);
inset.right = Some(length);
inset.bottom = Some(length);
inset.left = Some(length);
self
}
@ -340,7 +354,7 @@ pub trait Element<V: 'static>: 'static {
where
Self: Sized,
{
self.style_mut().size.width = width.into();
self.declared_style().size.width = Some(width.into());
self
}
@ -348,7 +362,7 @@ pub trait Element<V: 'static>: 'static {
where
Self: Sized,
{
self.style_mut().size.width = Length::Auto;
self.declared_style().size.width = Some(Length::Auto);
self
}
@ -357,7 +371,7 @@ pub trait Element<V: 'static>: 'static {
where
Self: Sized,
{
self.style_mut().size.width = length;
self.declared_style().size.width = Some(length);
self
}
@ -366,7 +380,7 @@ pub trait Element<V: 'static>: 'static {
where
Self: Sized,
{
self.style_mut().min_size.width = length;
self.declared_style().min_size.width = Some(length);
self
}
@ -374,7 +388,7 @@ pub trait Element<V: 'static>: 'static {
where
Self: Sized,
{
self.style_mut().size.height = height.into();
self.declared_style().size.height = Some(height.into());
self
}
@ -382,7 +396,7 @@ pub trait Element<V: 'static>: 'static {
where
Self: Sized,
{
self.style_mut().size.height = Length::Auto;
self.declared_style().size.height = Some(Length::Auto);
self
}
@ -391,7 +405,7 @@ pub trait Element<V: 'static>: 'static {
where
Self: Sized,
{
self.style_mut().size.height = height;
self.declared_style().size.height = Some(height);
self
}
@ -400,23 +414,22 @@ pub trait Element<V: 'static>: 'static {
where
Self: Sized,
{
self.style_mut().min_size.height = length;
self.declared_style().min_size.height = Some(length);
self
}
fn hoverable(self) -> Hoverable<V, Self>
where
Self: Sized,
{
Hoverable::new(self)
}
fn fill(mut self, fill: impl Into<Fill>) -> Self
where
Self: Sized,
{
self.style_mut().fill = Some(fill.into());
self
}
fn hover_fill(mut self, fill: impl Into<Fill>) -> Self
where
Self: Sized,
{
self.style_mut().hover_fill = Some(fill.into());
self.declared_style().fill = Some(fill.into());
self
}
@ -424,14 +437,14 @@ pub trait Element<V: 'static>: 'static {
where
Self: Sized,
{
self.style_mut().text_color = Some(color.into());
self.declared_style().text_color = Some(color.into());
self
}
}
// Object-safe counterpart of Element used by AnyElement to store elements as trait objects.
trait ElementObject<V> {
fn style_mut(&mut self) -> &mut ElementStyle;
fn style(&mut self) -> &mut StyleRefinement;
fn handlers_mut(&mut self) -> &mut Vec<EventHandler<V>>;
fn layout(&mut self, view: &mut V, cx: &mut LayoutContext<V>)
-> Result<(NodeId, Box<dyn Any>)>;
@ -444,8 +457,8 @@ trait ElementObject<V> {
}
impl<V: 'static, E: Element<V>> ElementObject<V> for E {
fn style_mut(&mut self) -> &mut ElementStyle {
Element::style_mut(self)
fn style(&mut self) -> &mut StyleRefinement {
Element::declared_style(self)
}
fn handlers_mut(&mut self) -> &mut Vec<EventHandler<V>> {
@ -498,11 +511,9 @@ impl<V: 'static> AnyElement<V> {
}
pub fn push_text_style(&mut self, cx: &mut impl RenderContext) -> bool {
let text_style = self.element.style_mut().text_style();
let text_style = self.element.style().text_style();
if let Some(text_style) = text_style {
let mut current_text_style = cx.text_style();
text_style.apply(&mut current_text_style);
cx.push_text_style(current_text_style);
cx.push_text_style(cx.text_style().refine(text_style));
true
} else {
false
@ -524,20 +535,17 @@ impl<V: 'static> AnyElement<V> {
from_element: element_layout.as_mut(),
};
let fill_color = self
.element
.style_mut()
.fill
.as_ref()
.and_then(Fill::color)
.map(Into::into);
let style = self.element.style();
cx.scene.push_quad(gpui::scene::Quad {
bounds: layout.from_engine.bounds,
background: fill_color,
border: Default::default(),
corner_radii: Default::default(),
});
let fill_color = style.fill.as_ref().and_then(|fill| fill.color());
if let Some(fill_color) = fill_color {
cx.scene.push_quad(gpui::scene::Quad {
bounds: layout.from_engine.bounds,
background: Some(fill_color.into()),
border: Default::default(),
corner_radii: Default::default(),
});
}
for event_handler in self.element.handlers_mut().iter().cloned() {
let EngineLayout { order, bounds } = layout.from_engine;
@ -574,8 +582,8 @@ impl<V: 'static> AnyElement<V> {
impl<V: 'static> Element<V> for AnyElement<V> {
type Layout = ();
fn style_mut(&mut self) -> &mut ElementStyle {
self.element.style_mut()
fn declared_style(&mut self) -> &mut StyleRefinement {
self.element.style()
}
fn handlers_mut(&mut self) -> &mut Vec<EventHandler<V>> {

View file

@ -2,23 +2,24 @@ use crate::{
element::{
AnyElement, Element, EventHandler, IntoElement, Layout, LayoutContext, NodeId, PaintContext,
},
style::ElementStyle,
style::{Style, StyleRefinement},
};
use anyhow::{anyhow, Result};
use gpui::LayoutNodeId;
use playground_macros::IntoElement;
use refineable::Refineable;
#[derive(IntoElement)]
#[element_crate = "crate"]
pub struct Frame<V: 'static> {
style: ElementStyle,
style: StyleRefinement,
handlers: Vec<EventHandler<V>>,
children: Vec<AnyElement<V>>,
}
pub fn frame<V>() -> Frame<V> {
Frame {
style: ElementStyle::default(),
style: StyleRefinement::default(),
handlers: Vec::new(),
children: Vec::new(),
}
@ -27,7 +28,7 @@ pub fn frame<V>() -> Frame<V> {
impl<V: 'static> Element<V> for Frame<V> {
type Layout = ();
fn style_mut(&mut self) -> &mut ElementStyle {
fn declared_style(&mut self) -> &mut StyleRefinement {
&mut self.style
}
@ -47,10 +48,11 @@ impl<V: 'static> Element<V> for Frame<V> {
.collect::<Result<Vec<LayoutNodeId>>>()?;
let rem_size = cx.rem_pixels();
let style = Style::default().refine(&self.style);
let node_id = cx
.layout_engine()
.ok_or_else(|| anyhow!("no layout engine"))?
.add_node(self.style.to_taffy(rem_size), child_layout_node_ids)?;
.add_node(style.to_taffy(rem_size), child_layout_node_ids)?;
Ok((node_id, ()))
}

View file

@ -0,0 +1,80 @@
use std::{cell::Cell, marker::PhantomData, rc::Rc};
use gpui::{
geometry::{rect::RectF, vector::Vector2F},
scene::MouseMove,
EngineLayout,
};
use crate::{
element::Element,
style::{Style, StyleRefinement},
};
pub struct Hoverable<V, E> {
hover_style: StyleRefinement,
computed_style: Option<Style>,
view_type: PhantomData<V>,
child: E,
}
impl<V, E> Hoverable<V, E> {
pub fn new(child: E) -> Self {
Self {
hover_style: StyleRefinement::default(),
computed_style: None,
view_type: PhantomData,
child,
}
}
}
impl<V: 'static, E: Element<V>> Element<V> for Hoverable<V, E> {
type Layout = E::Layout;
fn declared_style(&mut self) -> &mut StyleRefinement {
&mut self.hover_style
}
fn computed_style(&mut self) -> &StyleRefinement {
todo!()
}
fn handlers_mut(&mut self) -> &mut Vec<crate::element::EventHandler<V>> {
self.child.handlers_mut()
}
fn layout(
&mut self,
view: &mut V,
cx: &mut gpui::LayoutContext<V>,
) -> anyhow::Result<(taffy::tree::NodeId, Self::Layout)> {
self.child.layout(view, cx)
}
fn paint<'a>(
&mut self,
layout: crate::element::Layout<Self::Layout>,
view: &mut V,
cx: &mut crate::element::PaintContext<V>,
) -> anyhow::Result<()> {
let EngineLayout { bounds, order } = layout.from_engine;
let window_bounds = RectF::new(Vector2F::zero(), cx.window_size());
let was_hovered = Rc::new(Cell::new(false));
self.child.paint(layout, view, cx)?;
cx.draw_interactive_region(
order,
window_bounds,
false,
move |view, event: &MouseMove, cx| {
let is_hovered = bounds.contains_point(cx.mouse_position());
if is_hovered != was_hovered.get() {
was_hovered.set(is_hovered);
cx.repaint();
}
},
);
Ok(())
}
}

View file

@ -18,6 +18,7 @@ mod color;
mod components;
mod element;
mod frame;
mod hoverable;
mod paint_context;
mod style;
mod text;

View file

@ -1,17 +1,24 @@
use crate::color::Hsla;
use gpui::geometry::{DefinedLength, Edges, Length, Point, Size};
use gpui::{
fonts::TextStyleRefinement,
geometry::{
DefinedLength, Edges, EdgesRefinement, Length, Point, PointRefinement, Size, SizeRefinement,
},
};
use refineable::Refineable;
pub use taffy::style::{
AlignContent, AlignItems, AlignSelf, Display, FlexDirection, FlexWrap, JustifyContent,
Overflow, Position,
};
#[derive(Clone)]
pub struct ElementStyle {
#[derive(Clone, Refineable)]
pub struct Style {
/// What layout strategy should be used?
pub display: Display,
// Overflow properties
/// How children overflowing their container should affect layout
#[refineable]
pub overflow: Point<Overflow>,
/// How much space (in points) should be reserved for the scrollbars of `Overflow::Scroll` and `Overflow::Auto` nodes.
pub scrollbar_width: f32,
@ -20,24 +27,31 @@ pub struct ElementStyle {
/// What should the `position` value of this struct use as a base offset?
pub position: Position,
/// How should the position of this element be tweaked relative to the layout defined?
#[refineable]
pub inset: Edges<Length>,
// Size properies
/// Sets the initial size of the item
#[refineable]
pub size: Size<Length>,
/// Controls the minimum size of the item
#[refineable]
pub min_size: Size<Length>,
/// Controls the maximum size of the item
#[refineable]
pub max_size: Size<Length>,
/// Sets the preferred aspect ratio for the item. The ratio is calculated as width divided by height.
pub aspect_ratio: Option<f32>,
// Spacing Properties
/// How large should the margin be on each side?
#[refineable]
pub margin: Edges<Length>,
/// How large should the padding be on each side?
#[refineable]
pub padding: Edges<DefinedLength>,
/// How large should the border be on each side?
#[refineable]
pub border: Edges<DefinedLength>,
// Alignment properties
@ -50,6 +64,7 @@ pub struct ElementStyle {
/// How should contained within this item be aligned in the main/inline axis
pub justify_content: Option<JustifyContent>,
/// How large should the gaps between items in a flex container be?
#[refineable]
pub gap: Size<DefinedLength>,
// Flexbox properies
@ -66,14 +81,12 @@ pub struct ElementStyle {
/// The fill color of this element
pub fill: Option<Fill>,
/// The fill color of this element when hovered
pub hover_fill: Option<Fill>,
/// The color of text within this element. Cascades to children unless overridden.
pub text_color: Option<Hsla>,
}
impl ElementStyle {
pub const DEFAULT: ElementStyle = ElementStyle {
impl Style {
pub const DEFAULT: Style = Style {
display: Display::DEFAULT,
overflow: Point {
x: Overflow::Visible,
@ -102,7 +115,6 @@ impl ElementStyle {
flex_shrink: 1.0,
flex_basis: Length::Auto,
fill: None,
hover_fill: None,
text_color: None,
};
@ -137,21 +149,20 @@ impl ElementStyle {
..Default::default() // Ignore grid properties for now
}
}
}
pub fn text_style(&self) -> Option<OptionalTextStyle> {
if self.text_color.is_some() {
Some(OptionalTextStyle {
color: self.text_color,
})
} else {
None
}
impl Default for Style {
fn default() -> Self {
Self::DEFAULT.clone()
}
}
impl Default for ElementStyle {
fn default() -> Self {
Self::DEFAULT.clone()
impl StyleRefinement {
pub fn text_style(&self) -> Option<TextStyleRefinement> {
self.text_color.map(|color| TextStyleRefinement {
color: Some(color.into()),
..Default::default()
})
}
}

View file

@ -1,6 +1,10 @@
use crate::element::{Element, ElementMetadata, EventHandler, IntoElement};
use crate::{
element::{Element, ElementMetadata, EventHandler, IntoElement},
style::Style,
};
use gpui::{geometry::Size, text_layout::LineLayout, RenderContext};
use parking_lot::Mutex;
use refineable::Refineable;
use std::sync::Arc;
impl<V: 'static, S: Into<ArcCow<'static, str>>> IntoElement<V> for S {
@ -22,7 +26,7 @@ pub struct Text<V> {
impl<V: 'static> Element<V> for Text<V> {
type Layout = Arc<Mutex<Option<TextLayout>>>;
fn style_mut(&mut self) -> &mut crate::style::ElementStyle {
fn declared_style(&mut self) -> &mut crate::style::StyleRefinement {
&mut self.metadata.style
}
@ -39,7 +43,8 @@ impl<V: 'static> Element<V> for Text<V> {
let text = self.text.clone();
let layout = Arc::new(Mutex::new(None));
let node_id = layout_engine.add_measured_node(self.metadata.style.to_taffy(rem_size), {
let style: Style = Style::default().refine(&self.metadata.style);
let node_id = layout_engine.add_measured_node(style.to_taffy(rem_size), {
let layout = layout.clone();
move |params| {
let line_layout = fonts.layout_line(