From f4d8763d2b0676df01ba0145496344f9544eca93 Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Sat, 19 Aug 2023 23:16:20 -0600 Subject: [PATCH] WIP --- crates/gpui/playground/src/adapter.rs | 124 ++-- crates/gpui/playground/src/div.rs | 167 +---- crates/gpui/playground/src/element.rs | 639 ++++--------------- crates/gpui/playground/src/frame.rs | 84 --- crates/gpui/playground/src/hoverable.rs | 149 ++--- crates/gpui/playground/src/layout_context.rs | 2 +- crates/gpui/playground/src/playground.rs | 1 - crates/gpui/playground/src/style.rs | 27 +- crates/gpui/playground/src/text.rs | 36 +- crates/gpui/playground/src/view.rs | 2 +- 10 files changed, 275 insertions(+), 956 deletions(-) delete mode 100644 crates/gpui/playground/src/frame.rs diff --git a/crates/gpui/playground/src/adapter.rs b/crates/gpui/playground/src/adapter.rs index 452bce6f72..bb3a417ae7 100644 --- a/crates/gpui/playground/src/adapter.rs +++ b/crates/gpui/playground/src/adapter.rs @@ -1,70 +1,70 @@ -use crate::element::{LayoutContext, PaintContext}; -use gpui::{geometry::rect::RectF, LayoutEngine}; -use util::ResultExt; +// use crate::element::{LayoutContext, PaintContext}; +// use gpui::{geometry::rect::RectF, LayoutEngine}; +// use util::ResultExt; -use crate::element::AnyElement; +// use crate::element::AnyElement; -pub struct Adapter(pub(crate) AnyElement); +// pub struct Adapter(pub(crate) AnyElement); -impl gpui::Element for Adapter { - type LayoutState = Option; - type PaintState = (); +// impl gpui::Element for Adapter { +// type LayoutState = Option; +// type PaintState = (); - fn layout( - &mut self, - constraint: gpui::SizeConstraint, - view: &mut V, - cx: &mut LayoutContext, - ) -> (gpui::geometry::vector::Vector2F, Self::LayoutState) { - cx.push_layout_engine(LayoutEngine::new()); - let node = self.0.layout(view, cx).log_err(); +// fn layout( +// &mut self, +// constraint: gpui::SizeConstraint, +// view: &mut V, +// cx: &mut LayoutContext, +// ) -> (gpui::geometry::vector::Vector2F, Self::LayoutState) { +// cx.push_layout_engine(LayoutEngine::new()); +// let node = self.0.layout(view, cx).log_err(); - if let Some(node) = node { - let layout_engine = cx.layout_engine().unwrap(); - layout_engine.compute_layout(node, constraint.max).log_err(); - } - let layout_engine = cx.pop_layout_engine(); - debug_assert!(layout_engine.is_some()); - (constraint.max, layout_engine) - } +// if let Some(node) = node { +// let layout_engine = cx.layout_engine().unwrap(); +// layout_engine.compute_layout(node, constraint.max).log_err(); +// } +// let layout_engine = cx.pop_layout_engine(); +// debug_assert!(layout_engine.is_some()); +// (constraint.max, layout_engine) +// } - fn paint( - &mut self, - scene: &mut gpui::SceneBuilder, - bounds: RectF, - visible_bounds: RectF, - layout_engine: &mut Option, - view: &mut V, - legacy_cx: &mut gpui::PaintContext, - ) -> Self::PaintState { - legacy_cx.push_layout_engine(layout_engine.take().unwrap()); - let mut cx = PaintContext::new(legacy_cx, scene); - self.0.paint(view, &mut cx).log_err(); - *layout_engine = legacy_cx.pop_layout_engine(); - debug_assert!(layout_engine.is_some()); - } +// fn paint( +// &mut self, +// scene: &mut gpui::SceneBuilder, +// bounds: RectF, +// visible_bounds: RectF, +// layout_engine: &mut Option, +// view: &mut V, +// legacy_cx: &mut gpui::PaintContext, +// ) -> Self::PaintState { +// legacy_cx.push_layout_engine(layout_engine.take().unwrap()); +// let mut cx = PaintContext::new(legacy_cx, scene); +// self.0.paint(view, &mut cx).log_err(); +// *layout_engine = legacy_cx.pop_layout_engine(); +// debug_assert!(layout_engine.is_some()); +// } - fn rect_for_text_range( - &self, - range_utf16: std::ops::Range, - bounds: RectF, - visible_bounds: RectF, - layout: &Self::LayoutState, - paint: &Self::PaintState, - view: &V, - cx: &gpui::ViewContext, - ) -> Option { - todo!("implement before merging to main") - } +// fn rect_for_text_range( +// &self, +// range_utf16: std::ops::Range, +// bounds: RectF, +// visible_bounds: RectF, +// layout: &Self::LayoutState, +// paint: &Self::PaintState, +// view: &V, +// cx: &gpui::ViewContext, +// ) -> Option { +// todo!("implement before merging to main") +// } - fn debug( - &self, - bounds: RectF, - layout: &Self::LayoutState, - paint: &Self::PaintState, - view: &V, - cx: &gpui::ViewContext, - ) -> gpui::serde_json::Value { - todo!("implement before merging to main") - } -} +// fn debug( +// &self, +// bounds: RectF, +// layout: &Self::LayoutState, +// paint: &Self::PaintState, +// view: &V, +// cx: &gpui::ViewContext, +// ) -> gpui::serde_json::Value { +// todo!("implement before merging to main") +// } +// } diff --git a/crates/gpui/playground/src/div.rs b/crates/gpui/playground/src/div.rs index 98a5e74dfb..57d334506c 100644 --- a/crates/gpui/playground/src/div.rs +++ b/crates/gpui/playground/src/div.rs @@ -1,104 +1,13 @@ -use std::cell::Cell; -use std::{marker::PhantomData, rc::Rc}; - -use crate::element::{AnyElement, PaintContext}; -use crate::layout_context::LayoutContext; -use crate::style::{Style, StyleRefinement}; +use crate::{ + element::{AnyElement, Element, Layout}, + layout_context::LayoutContext, + paint_context::PaintContext, + style::{Style, StyleRefinement, Styleable}, +}; use anyhow::Result; -use derive_more::{Deref, DerefMut}; -use gpui::EngineLayout; -use gpui::{geometry::rect::RectF, platform::MouseMovedEvent, EventContext}; -use playground_macros::styleable_helpers; -use refineable::Refineable; +use gpui::{platform::MouseMovedEvent, EventContext, LayoutId}; use smallvec::SmallVec; -use util::ResultExt; - -type LayoutId = gpui::LayoutId; - -#[derive(Deref, DerefMut)] -pub struct Layout { - id: LayoutId, - engine_layout: Option, - #[deref] - #[deref_mut] - element_data: D, - view_type: PhantomData, -} - -impl Layout { - pub fn new(id: LayoutId, engine_layout: Option, element_data: D) -> Self { - Self { - id, - engine_layout, - element_data, - view_type: PhantomData, - } - } - - pub fn bounds(&mut self, cx: &mut PaintContext) -> RectF { - self.engine_layout(cx).bounds - } - - pub fn order(&mut self, cx: &mut PaintContext) -> u32 { - self.engine_layout(cx).order - } - - fn engine_layout(&mut self, cx: &mut PaintContext<'_, '_, '_, '_, V>) -> &mut EngineLayout { - self.engine_layout - .get_or_insert_with(|| cx.computed_layout(self.id).log_err().unwrap_or_default()) - } -} - -pub trait Element { - type Layout; - - fn layout( - &mut self, - view: &mut V, - cx: &mut LayoutContext, - ) -> Result> - where - Self: Sized; - - fn paint( - &mut self, - view: &mut V, - layout: &mut Layout, - cx: &mut PaintContext, - ) where - Self: Sized; - - /// ## Helpers - - fn hoverable(self) -> Hoverable - where - Self: Styleable + Sized, - { - hoverable(self) - } -} - -pub trait Styleable { - type Style: refineable::Refineable; - - fn declared_style(&mut self) -> &mut playground::style::StyleRefinement; - - fn style(&mut self) -> playground::style::Style { - let mut style = playground::style::Style::default(); - style.refine(self.declared_style()); - style - } -} - -// Tailwind-style helpers methods that take and return mut self -// -// Example: -// // Sets the padding to 0.5rem, just like class="p-2" in Tailwind. -// fn p_2(mut self) -> Self where Self: Sized; -use crate as playground; // Macro invocation references this crate as playground. -pub trait StyleHelpers: Styleable