diff --git a/Cargo.lock b/Cargo.lock index d6490337daecb788d3190a0aca7573b57f9391a1..4c31069461b3d8519477bd1745ff6ba66a27afa9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5119,7 +5119,6 @@ dependencies = [ "serde", "simplelog", "smallvec", - "taffy", "util", ] @@ -7435,7 +7434,7 @@ dependencies = [ [[package]] name = "taffy" version = "0.3.11" -source = "git+https://github.com/DioxusLabs/taffy?rev=dab541d6104d58e2e10ce90c4a1dad0b703160cd#dab541d6104d58e2e10ce90c4a1dad0b703160cd" +source = "git+https://github.com/DioxusLabs/taffy?rev=4fb530bdd71609bb1d3f76c6a8bde1ba82805d5e#4fb530bdd71609bb1d3f76c6a8bde1ba82805d5e" dependencies = [ "arrayvec 0.7.4", "grid", diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rs index 67279b1ba62551d0b41b7dc97a3d1e56eb841691..b51f7ae25541aad9df19c117552c48af50c5e396 100644 --- a/crates/editor/src/editor.rs +++ b/crates/editor/src/editor.rs @@ -4866,7 +4866,6 @@ impl Editor { if let Some(clipboard_selection) = clipboard_selections.get(ix) { let end_offset = start_offset + clipboard_selection.len; to_insert = &clipboard_text[start_offset..end_offset]; - dbg!(start_offset, end_offset, &clipboard_text, &to_insert); entire_line = clipboard_selection.is_entire_line; start_offset = end_offset + 1; original_indent_column = diff --git a/crates/gpui/Cargo.toml b/crates/gpui/Cargo.toml index 24397f619357a5454f333c21ef96e4ac369c6397..a77c9e2237bdc372f24c27fdec3dd991d51daef2 100644 --- a/crates/gpui/Cargo.toml +++ b/crates/gpui/Cargo.toml @@ -48,7 +48,7 @@ serde_derive.workspace = true serde_json.workspace = true smallvec.workspace = true smol.workspace = true -taffy = { git = "https://github.com/DioxusLabs/taffy", rev = "dab541d6104d58e2e10ce90c4a1dad0b703160cd", features = ["flexbox"] } +taffy = { git = "https://github.com/DioxusLabs/taffy", rev = "4fb530bdd71609bb1d3f76c6a8bde1ba82805d5e" } time.workspace = true tiny-skia = "0.5" usvg = { version = "0.14", features = [] } diff --git a/crates/gpui/playground/Cargo.toml b/crates/gpui/playground/Cargo.toml index 3e5a5e5606a5e3351104a9587f881ab3caf2fccc..d907d3d6b6f8ba6b37c404f33abeb23fd05d577f 100644 --- a/crates/gpui/playground/Cargo.toml +++ b/crates/gpui/playground/Cargo.toml @@ -19,7 +19,6 @@ refineable.workspace = true serde.workspace = true simplelog = "0.9" smallvec.workspace = true -taffy = { git = "https://github.com/DioxusLabs/taffy", rev = "dab541d6104d58e2e10ce90c4a1dad0b703160cd", features = ["flexbox"] } util = { path = "../../util" } [dev-dependencies] diff --git a/crates/gpui/playground/src/div.rs b/crates/gpui/playground/src/div.rs index 98cebfe4ea84bcb3c061fe504d4b8444e9c60340..a9bf89dc2aed9ff37573ab63ab737b5613410f44 100644 --- a/crates/gpui/playground/src/div.rs +++ b/crates/gpui/playground/src/div.rs @@ -6,7 +6,7 @@ use crate::{ style::{Style, StyleHelpers, Styleable}, }; use anyhow::Result; -use gpui::LayoutId; +use gpui::{LayoutId, RenderContext}; use refineable::{Refineable, RefinementCascade}; use smallvec::SmallVec; @@ -31,27 +31,46 @@ impl Element for Div { 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::>>()?; - let style = Style::from_refinement(&self.style_cascade().merged()); - cx.add_layout_node(style.clone(), (), children) + if pop_text_style { + cx.pop_text_style(); + } + + let layout = cx.add_layout_node(style, (), children.clone())?; + + dbg!(layout.id(), children); + Ok(layout) } fn paint(&mut self, view: &mut V, layout: &mut Layout, cx: &mut PaintContext) where Self: Sized, { - self.computed_style() - .paint_background(layout.bounds(cx), cx); + 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 + }); + style.paint_background(layout.bounds(cx), cx); self.interaction_handlers() .paint(layout.order(cx), layout.bounds(cx), cx); for child in &mut self.children { child.paint(view, cx); } + if pop_text_style { + cx.pop_text_style(); + } } } diff --git a/crates/gpui/playground/src/element.rs b/crates/gpui/playground/src/element.rs index f0b6df75b3af16536a4637437bccb5fb31fd1dc7..3de6ea2903de7d7626843afbb2b62eeaf1af3f1c 100644 --- a/crates/gpui/playground/src/element.rs +++ b/crates/gpui/playground/src/element.rs @@ -1,4 +1,4 @@ -use anyhow::{anyhow, Result}; +use anyhow::Result; use gpui::{geometry::rect::RectF, EngineLayout}; use smallvec::SmallVec; use std::marker::PhantomData; @@ -32,7 +32,7 @@ pub trait Element: 'static { where Self: 'static + Sized, { - AnyElement(Box::new(ElementState { + AnyElement(Box::new(StatefulElement { element: self, layout: None, })) @@ -40,19 +40,19 @@ pub trait Element: 'static { } /// Used to make ElementState into a trait object, so we can wrap it in AnyElement. -trait ElementStateObject { +trait AnyStatefulElement { fn layout(&mut self, view: &mut V, cx: &mut LayoutContext) -> Result; fn paint(&mut self, view: &mut V, cx: &mut PaintContext); } /// A wrapper around an element that stores its layout state. -struct ElementState> { +struct StatefulElement> { element: E, layout: Option>, } /// We blanket-implement the object-safe ElementStateObject interface to make ElementStates into trait objects -impl> ElementStateObject for ElementState { +impl> AnyStatefulElement for StatefulElement { fn layout(&mut self, view: &mut V, cx: &mut LayoutContext) -> Result { let layout = self.element.layout(view, cx)?; let layout_id = layout.id; @@ -63,14 +63,14 @@ impl> ElementStateObject for ElementState { fn paint(&mut self, view: &mut V, cx: &mut PaintContext) { let layout = self.layout.as_mut().expect("paint called before layout"); if layout.engine_layout.is_none() { - layout.engine_layout = cx.computed_layout(layout.id).log_err() + layout.engine_layout = dbg!(cx.computed_layout(dbg!(layout.id)).log_err()) } self.element.paint(view, layout, cx) } } /// A dynamic element. -pub struct AnyElement(Box>); +pub struct AnyElement(Box>); impl AnyElement { pub fn layout(&mut self, view: &mut V, cx: &mut LayoutContext) -> Result { @@ -99,6 +99,10 @@ impl Layout { } } + pub fn id(&self) -> LayoutId { + self.id + } + pub fn bounds(&mut self, cx: &mut PaintContext) -> RectF { self.engine_layout(cx).bounds } @@ -107,7 +111,7 @@ impl Layout { self.engine_layout(cx).order } - pub fn update(&mut self, update: F) -> Result + pub fn update(&mut self, update: F) -> T where F: FnOnce(&mut Self, &mut D) -> T, { @@ -118,7 +122,7 @@ impl Layout { self.element_data = Some(element_data); result }) - .ok_or_else(|| anyhow!("reentrant calls to Layout::update are not allowed")) + .expect("reentrant calls to Layout::update are not supported") } fn engine_layout(&mut self, cx: &mut PaintContext<'_, '_, '_, '_, V>) -> &mut EngineLayout { diff --git a/crates/gpui/playground/src/interactive.rs b/crates/gpui/playground/src/interactive.rs index 3afe928b3e6dad9f09abac49c768df106903b2dd..e91436499ca06ba1dce11d2ca1e8e82287a97b47 100644 --- a/crates/gpui/playground/src/interactive.rs +++ b/crates/gpui/playground/src/interactive.rs @@ -11,6 +11,7 @@ use crate::element::PaintContext; pub trait Interactive { fn interaction_handlers(&mut self) -> &mut InteractionHandlers; + // One line change. fn on_mouse_down( mut self, button: MouseButton, diff --git a/crates/gpui/playground/src/layout_context.rs b/crates/gpui/playground/src/layout_context.rs index 7c9f13e7f08519c13a7ac78c2f90bd628e8fd3a8..71bd3ac5a35163bdabef9dbfba8e9584e579e7b0 100644 --- a/crates/gpui/playground/src/layout_context.rs +++ b/crates/gpui/playground/src/layout_context.rs @@ -1,8 +1,7 @@ use anyhow::{anyhow, Result}; use derive_more::{Deref, DerefMut}; -pub use gpui::LayoutContext as LegacyLayoutContext; -use gpui::{RenderContext, ViewContext}; -pub use taffy::tree::NodeId; +use gpui::{geometry::Size, MeasureParams, RenderContext, ViewContext}; +pub use gpui::{taffy::tree::NodeId, LayoutContext as LegacyLayoutContext}; use crate::{element::Layout, style::Style}; @@ -51,4 +50,22 @@ impl<'a, 'b, 'c, 'd, V: 'static> LayoutContext<'a, 'b, 'c, 'd, V> { Ok(Layout::new(id, element_data)) } + + pub fn add_measured_layout_node( + &mut self, + style: Style, + element_data: D, + measure: F, + ) -> Result> + where + F: Fn(MeasureParams) -> Size + Sync + Send + 'static, + { + let rem_size = self.rem_pixels(); + let layout_id = self + .layout_engine() + .ok_or_else(|| anyhow!("no layout engine"))? + .add_measured_node(style.to_taffy(rem_size), measure)?; + + Ok(Layout::new(layout_id, element_data)) + } } diff --git a/crates/gpui/playground/src/paint_context.rs b/crates/gpui/playground/src/paint_context.rs index e3e8c2b1515b8806bed0f45a26b0958ce595b7b9..d49cbaf6beb7ca3517f82739fd71733e4d26691b 100644 --- a/crates/gpui/playground/src/paint_context.rs +++ b/crates/gpui/playground/src/paint_context.rs @@ -1,9 +1,11 @@ use anyhow::{anyhow, Result}; use derive_more::{Deref, DerefMut}; -use gpui::{scene::EventHandler, EngineLayout, EventContext, LayoutId, RenderContext, ViewContext}; -pub use gpui::{LayoutContext, PaintContext as LegacyPaintContext}; +pub use gpui::taffy::tree::NodeId; +use gpui::{ + scene::EventHandler, EngineLayout, EventContext, LayoutId, PaintContext as LegacyPaintContext, + RenderContext, ViewContext, +}; use std::{any::TypeId, rc::Rc}; -pub use taffy::tree::NodeId; #[derive(Deref, DerefMut)] pub struct PaintContext<'a, 'b, 'c, 'd, V> { diff --git a/crates/gpui/playground/src/playground.rs b/crates/gpui/playground/src/playground.rs index beca547e18e191de7062c13a322281c52919ea01..bd184ac621d2a8dfdc0c6e530c782365348b95ba 100644 --- a/crates/gpui/playground/src/playground.rs +++ b/crates/gpui/playground/src/playground.rs @@ -1,9 +1,6 @@ #![allow(dead_code, unused_variables)] use crate::{ - color::black, - components::button, - element::ParentElement, - style::{StyleHelpers, Styleable}, + color::black, element::ParentElement, style::StyleHelpers, themes::rose_pine::RosePinePalette, }; use element::Element; use gpui::{ @@ -51,64 +48,24 @@ fn main() { fn playground(theme: &ThemeColors) -> impl Element { use div::div; + let p = RosePinePalette::dawn(); div() .text_color(black()) .h_full() - .w_1_2() - .fill(theme.success(0.5)) - .hovered() - .fill(theme.error(0.5)) - .pressed() - .fill(theme.warning(0.5)) + .w_full() + .fill(p.rose) + .block() .child( div() - .h_6() - .w_6() - .absolute() - .bottom_0() - .fill(theme.success(0.)), + .block() + .fill(p.pine) + .child(div().block().fill(p.love).w_6().h_3()), ) .child( - button() - .label("Click me") - .data(1_usize) - .on_click(|_, data, _| { - dbg!(*data); - }), - ) - .child( - button() - .label("And me") - .data(2_usize) - .on_click(|_, data, _| { - dbg!(*data); - }), + div() + .block() + .fill(p.gold) + .child(div().block().fill(p.iris).w_3().h_3()), ) } - -// todo!() -// // column() -// // .size(auto()) -// // .fill(theme.base(0.5)) -// // .text_color(theme.text(0.5)) -// // .child(title_bar(theme)) -// // .child(stage(theme)) -// // .child(status_bar(theme)) -// } - -// fn title_bar(theme: &ThemeColors) -> impl Element { -// row() -// .fill(theme.base(0.2)) -// .justify(0.) -// .width(auto()) -// .child(text("Zed Playground")) -// } - -// fn stage(theme: &ThemeColors) -> impl Element { -// row().fill(theme.surface(0.9)) -// } - -// fn status_bar(theme: &ThemeColors) -> impl Element { -// row().fill(theme.surface(0.1)) -// } diff --git a/crates/gpui/playground/src/style.rs b/crates/gpui/playground/src/style.rs index 0decdb232f117bfbdfc6ce9ea11260bb7bca46c5..0d6a53e29606845e155b8de115296a4bc9373c9b 100644 --- a/crates/gpui/playground/src/style.rs +++ b/crates/gpui/playground/src/style.rs @@ -4,19 +4,20 @@ use crate::{ paint_context::PaintContext, pressable::{pressable, Pressable}, }; +pub use gpui::taffy::style::{ + AlignContent, AlignItems, AlignSelf, Display, FlexDirection, FlexWrap, JustifyContent, + Overflow, Position, +}; use gpui::{ fonts::TextStyleRefinement, geometry::{ rect::RectF, AbsoluteLength, DefiniteLength, Edges, EdgesRefinement, Length, Point, PointRefinement, Size, SizeRefinement, }, + taffy, }; use playground_macros::styleable_helpers; use refineable::{Refineable, RefinementCascade}; -pub use taffy::style::{ - AlignContent, AlignItems, AlignSelf, Display, FlexDirection, FlexWrap, JustifyContent, - Overflow, Position, -}; #[derive(Clone, Refineable)] pub struct Style { @@ -137,12 +138,23 @@ impl Style { }); } } + + pub fn text_style(&self) -> Option { + if let Some(color) = self.text_color { + Some(TextStyleRefinement { + color: Some(color.into()), + ..Default::default() + }) + } else { + None + } + } } impl Default for Style { fn default() -> Self { Style { - display: Display::DEFAULT, + display: Display::Block, overflow: Point { x: Overflow::Visible, y: Overflow::Visible, @@ -290,6 +302,14 @@ pub trait StyleHelpers: Styleable