From 65c7765c070cbb03450f8b562cdca3412d2debf2 Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Thu, 5 Oct 2023 21:02:26 -0600 Subject: [PATCH] Checkpoint --- crates/gpui3/src/element.rs | 23 +++++++------- crates/gpui3/src/elements/div.rs | 22 ++++++-------- crates/gpui3/src/elements/img.rs | 12 +++----- crates/gpui3/src/elements/stateless.rs | 6 ++-- crates/gpui3/src/elements/svg.rs | 6 ++-- crates/gpui3/src/elements/text.rs | 7 ++--- crates/gpui3/src/style.rs | 2 +- crates/gpui3/src/taffy.rs | 42 +++++++++++--------------- crates/gpui3/src/text_system/line.rs | 20 ++++-------- crates/gpui3/src/view.rs | 17 +++++++---- crates/gpui3/src/window.rs | 24 +++++---------- crates/storybook2/src/theme.rs | 6 ++-- 12 files changed, 80 insertions(+), 107 deletions(-) diff --git a/crates/gpui3/src/element.rs b/crates/gpui3/src/element.rs index c145a9725b..c430ce3b5b 100644 --- a/crates/gpui3/src/element.rs +++ b/crates/gpui3/src/element.rs @@ -1,4 +1,6 @@ -use super::{Layout, LayoutId, Pixels, Point, Result, ViewContext}; +use crate::Bounds; + +use super::{LayoutId, Pixels, Point, Result, ViewContext}; pub(crate) use smallvec::SmallVec; pub trait Element: 'static { @@ -13,7 +15,7 @@ pub trait Element: 'static { fn paint( &mut self, - layout: Layout, + bounds: Bounds, state: &mut Self::State, frame_state: &mut Self::FrameState, cx: &mut ViewContext, @@ -65,7 +67,7 @@ enum ElementRenderPhase { frame_state: S, }, Painted { - layout: Layout, + bounds: Bounds, frame_state: S, }, } @@ -105,24 +107,23 @@ impl ElementObject for RenderedElement { layout_id, mut frame_state, } => { - let mut layout = cx.layout(layout_id)?.clone(); - offset.map(|offset| layout.bounds.origin += offset); - self.element - .paint(layout.clone(), state, &mut frame_state, cx)?; + let mut bounds = cx.layout_bounds(layout_id)?.clone(); + offset.map(|offset| bounds.origin += offset); + self.element.paint(bounds, state, &mut frame_state, cx)?; ElementRenderPhase::Painted { - layout, + bounds, frame_state, } } ElementRenderPhase::Painted { - layout, + bounds, mut frame_state, } => { self.element - .paint(layout.clone(), state, &mut frame_state, cx)?; + .paint(bounds.clone(), state, &mut frame_state, cx)?; ElementRenderPhase::Painted { - layout, + bounds, frame_state, } } diff --git a/crates/gpui3/src/elements/div.rs b/crates/gpui3/src/elements/div.rs index ba006b13ee..f577d9b5c6 100644 --- a/crates/gpui3/src/elements/div.rs +++ b/crates/gpui3/src/elements/div.rs @@ -1,6 +1,6 @@ use crate::{ - AnyElement, Bounds, Element, Layout, LayoutId, Overflow, ParentElement, Pixels, Point, - Refineable, RefinementCascade, Result, Style, StyleHelpers, Styled, ViewContext, + AnyElement, Bounds, Element, LayoutId, Overflow, ParentElement, Pixels, Point, Refineable, + RefinementCascade, Result, Style, StyleHelpers, Styled, ViewContext, }; use parking_lot::Mutex; use smallvec::SmallVec; @@ -40,31 +40,28 @@ impl Element for Div { fn paint( &mut self, - layout: Layout, + bounds: Bounds, state: &mut S, child_layouts: &mut Self::FrameState, cx: &mut ViewContext, ) -> Result<()> { - let Layout { order, bounds } = layout; - let style = self.computed_style(); - cx.stack(0, |cx| style.paint(order, bounds, cx)); + cx.stack(0, |cx| style.paint(bounds, cx)); let overflow = &style.overflow; style.apply_text_style(cx, |cx| { cx.stack(1, |cx| { - style.apply_overflow(layout.bounds, cx, |cx| { - self.paint_children(overflow, state, cx) - }) + style.apply_overflow(bounds, cx, |cx| self.paint_children(overflow, state, cx)) }) })?; - self.handle_scroll(order, bounds, style.overflow.clone(), child_layouts, cx); + self.handle_scroll(bounds, style.overflow.clone(), child_layouts, cx); // todo!("enable inspector") // if cx.is_inspector_enabled() { // self.paint_inspector(parent_origin, layout, cx); // } // + Ok(()) } } @@ -139,7 +136,6 @@ impl Div { fn handle_scroll( &mut self, - _order: u32, bounds: Bounds, overflow: Point, child_layout_ids: &[LayoutId], @@ -148,8 +144,8 @@ impl Div { if overflow.y == Overflow::Scroll || overflow.x == Overflow::Scroll { let mut scroll_max = Point::default(); for child_layout_id in child_layout_ids { - if let Some(child_layout) = cx.layout(*child_layout_id).log_err() { - scroll_max = scroll_max.max(&child_layout.bounds.lower_right()); + if let Some(child_bounds) = cx.layout_bounds(*child_layout_id).log_err() { + scroll_max = scroll_max.max(&child_bounds.lower_right()); } } scroll_max -= bounds.size; diff --git a/crates/gpui3/src/elements/img.rs b/crates/gpui3/src/elements/img.rs index 6b6ddc185f..68aac46446 100644 --- a/crates/gpui3/src/elements/img.rs +++ b/crates/gpui3/src/elements/img.rs @@ -1,6 +1,6 @@ use crate::{ - BorrowWindow, Element, Layout, LayoutId, Result, SharedString, Style, StyleHelpers, Styled, - ViewContext, + BorrowWindow, Bounds, Element, LayoutId, Pixels, Result, SharedString, Style, StyleHelpers, + Styled, ViewContext, }; use futures::FutureExt; use refineable::RefinementCascade; @@ -54,16 +54,14 @@ impl Element for Img { fn paint( &mut self, - layout: Layout, + bounds: Bounds, _: &mut Self::State, _: &mut Self::FrameState, cx: &mut ViewContext, ) -> Result<()> { let style = self.computed_style(); - let order = layout.order; - let bounds = layout.bounds; - style.paint(order, bounds, cx); + style.paint(bounds, cx); if let Some(uri) = self.uri.clone() { let image_future = cx.image_cache.get(uri); @@ -74,7 +72,7 @@ impl Element for Img { { let corner_radii = style.corner_radii.to_pixels(bounds.size, cx.rem_size()); cx.stack(1, |cx| { - cx.paint_image(bounds, corner_radii, order, data, self.grayscale) + cx.paint_image(bounds, corner_radii, data, self.grayscale) })?; } else { cx.spawn(|_, mut cx| async move { diff --git a/crates/gpui3/src/elements/stateless.rs b/crates/gpui3/src/elements/stateless.rs index 9726685365..b1cd31a146 100644 --- a/crates/gpui3/src/elements/stateless.rs +++ b/crates/gpui3/src/elements/stateless.rs @@ -1,4 +1,4 @@ -use crate::Element; +use crate::{Bounds, Element, Pixels}; use std::marker::PhantomData; pub struct Stateless, S> { @@ -20,11 +20,11 @@ impl, S: Send + Sync + 'static> Element for Stateless, _: &mut Self::State, frame_state: &mut Self::FrameState, cx: &mut crate::ViewContext, ) -> anyhow::Result<()> { - cx.erase_state(|cx| self.element.paint(layout, &mut (), frame_state, cx)) + cx.erase_state(|cx| self.element.paint(bounds, &mut (), frame_state, cx)) } } diff --git a/crates/gpui3/src/elements/svg.rs b/crates/gpui3/src/elements/svg.rs index dbb4ffb155..4af620ec06 100644 --- a/crates/gpui3/src/elements/svg.rs +++ b/crates/gpui3/src/elements/svg.rs @@ -1,4 +1,4 @@ -use crate::{Element, Layout, LayoutId, Result, SharedString, Style, StyleHelpers, Styled}; +use crate::{Bounds, Element, LayoutId, Pixels, Result, SharedString, Style, StyleHelpers, Styled}; use refineable::RefinementCascade; use std::marker::PhantomData; @@ -41,7 +41,7 @@ impl Element for Svg { fn paint( &mut self, - layout: Layout, + bounds: Bounds, _: &mut Self::State, _: &mut Self::FrameState, cx: &mut crate::ViewContext, @@ -51,7 +51,7 @@ impl Element for Svg { { let fill_color = self.computed_style().fill.and_then(|fill| fill.color()); if let Some((path, fill_color)) = self.path.as_ref().zip(fill_color) { - cx.paint_svg(layout.bounds, layout.order, path.clone(), fill_color)?; + cx.paint_svg(bounds, path.clone(), fill_color)?; } Ok(()) } diff --git a/crates/gpui3/src/elements/text.rs b/crates/gpui3/src/elements/text.rs index 77390e79a4..6959cc709b 100644 --- a/crates/gpui3/src/elements/text.rs +++ b/crates/gpui3/src/elements/text.rs @@ -1,5 +1,5 @@ use crate::{ - AnyElement, Element, IntoAnyElement, Layout, LayoutId, Line, Pixels, Result, Size, ViewContext, + AnyElement, Bounds, Element, IntoAnyElement, LayoutId, Line, Pixels, Result, Size, ViewContext, }; use parking_lot::Mutex; use std::{marker::PhantomData, sync::Arc}; @@ -82,7 +82,7 @@ impl Element for Text { fn paint<'a>( &mut self, - layout: Layout, + bounds: Bounds, _: &mut Self::State, frame_state: &mut Self::FrameState, cx: &mut ViewContext, @@ -99,8 +99,7 @@ impl Element for Text { } // todo!("We haven't added visible bounds to the new element system yet, so this is a placeholder."); - let visible_bounds = layout.bounds; - line.paint(&layout, visible_bounds, line_height, cx)?; + line.paint(bounds, bounds, line_height, cx)?; Ok(()) } diff --git a/crates/gpui3/src/style.rs b/crates/gpui3/src/style.rs index 50d9260e1f..2b310d146f 100644 --- a/crates/gpui3/src/style.rs +++ b/crates/gpui3/src/style.rs @@ -241,7 +241,7 @@ impl Style { } /// Paints the background of an element styled with this style. - pub fn paint(&self, order: u32, bounds: Bounds, cx: &mut ViewContext) { + pub fn paint(&self, bounds: Bounds, cx: &mut ViewContext) { let rem_size = cx.rem_size(); let scale = cx.scale_factor(); diff --git a/crates/gpui3/src/taffy.rs b/crates/gpui3/src/taffy.rs index 3aa53a11cd..3045e06923 100644 --- a/crates/gpui3/src/taffy.rs +++ b/crates/gpui3/src/taffy.rs @@ -1,6 +1,5 @@ use super::{ - AbsoluteLength, Bounds, DefiniteLength, Edges, Layout, Length, Pixels, Point, Result, Size, - Style, + AbsoluteLength, Bounds, DefiniteLength, Edges, Length, Pixels, Point, Result, Size, Style, }; use collections::HashMap; use std::fmt::Debug; @@ -14,7 +13,7 @@ use taffy::{ pub struct TaffyLayoutEngine { taffy: Taffy, children_to_parents: HashMap, - absolute_layouts: HashMap, + absolute_layout_bounds: HashMap>, } impl TaffyLayoutEngine { @@ -22,7 +21,7 @@ impl TaffyLayoutEngine { TaffyLayoutEngine { taffy: Taffy::new(), children_to_parents: HashMap::default(), - absolute_layouts: HashMap::default(), + absolute_layout_bounds: HashMap::default(), } } @@ -76,19 +75,24 @@ impl TaffyLayoutEngine { Ok(()) } - pub fn layout(&mut self, id: LayoutId) -> Result { - if let Some(layout) = self.absolute_layouts.get(&id).cloned() { + pub fn layout_bounds(&mut self, id: LayoutId) -> Result> { + if let Some(layout) = self.absolute_layout_bounds.get(&id).cloned() { return Ok(layout); } - let mut relative_layout: Layout = self.taffy.layout(id.into()).map(Into::into)?; - if let Some(parent_id) = self.children_to_parents.get(&id).copied() { - let parent_layout = self.layout(parent_id)?; - relative_layout.bounds.origin += parent_layout.bounds.origin; - } - self.absolute_layouts.insert(id, relative_layout.clone()); + let layout = self.taffy.layout(id.into())?; + let mut bounds = Bounds { + origin: layout.location.into(), + size: layout.size.into(), + }; - Ok(relative_layout) + if let Some(parent_id) = self.children_to_parents.get(&id).copied() { + let parent_bounds = self.layout_bounds(parent_id)?; + bounds.origin += parent_bounds.origin; + } + self.absolute_layout_bounds.insert(id, bounds); + + Ok(bounds) } } @@ -369,15 +373,3 @@ impl From for AvailableSpace { AvailableSpace::Definite(pixels) } } - -impl From<&taffy::tree::Layout> for Layout { - fn from(layout: &taffy::tree::Layout) -> Self { - Layout { - order: layout.order, - bounds: Bounds { - origin: layout.location.into(), - size: layout.size.into(), - }, - } - } -} diff --git a/crates/gpui3/src/text_system/line.rs b/crates/gpui3/src/text_system/line.rs index 10b10d041e..be04247137 100644 --- a/crates/gpui3/src/text_system/line.rs +++ b/crates/gpui3/src/text_system/line.rs @@ -1,6 +1,6 @@ use crate::{ - black, point, px, Bounds, FontId, Hsla, Layout, Pixels, Point, RunStyle, ShapedBoundary, - ShapedLine, ShapedRun, UnderlineStyle, WindowContext, + black, point, px, Bounds, FontId, Hsla, Pixels, Point, RunStyle, ShapedBoundary, ShapedLine, + ShapedRun, UnderlineStyle, WindowContext, }; use anyhow::Result; use smallvec::SmallVec; @@ -90,15 +90,14 @@ impl Line { } } - // todo! pub fn paint( &self, - layout: &Layout, - visible_bounds: Bounds, + bounds: Bounds, + visible_bounds: Bounds, // todo!("use clipping") line_height: Pixels, cx: &mut WindowContext, ) -> Result<()> { - let origin = layout.bounds.origin; + let origin = bounds.origin; let padding_top = (line_height - self.layout.ascent - self.layout.descent) / 2.; let baseline_offset = point(px(0.), padding_top + self.layout.ascent); @@ -159,17 +158,10 @@ impl Line { } if glyph.is_emoji { - cx.paint_emoji( - glyph_origin, - layout.order, - run.font_id, - glyph.id, - self.layout.font_size, - )?; + cx.paint_emoji(glyph_origin, run.font_id, glyph.id, self.layout.font_size)?; } else { cx.paint_glyph( glyph_origin, - layout.order, run.font_id, glyph.id, self.layout.font_size, diff --git a/crates/gpui3/src/view.rs b/crates/gpui3/src/view.rs index 5c7f7a766d..b6f4af92c6 100644 --- a/crates/gpui3/src/view.rs +++ b/crates/gpui3/src/view.rs @@ -1,7 +1,7 @@ use parking_lot::Mutex; use crate::{ - AnyElement, Element, Handle, IntoAnyElement, Layout, LayoutId, Result, ViewContext, + AnyElement, Bounds, Element, Handle, IntoAnyElement, LayoutId, Pixels, Result, ViewContext, WindowContext, }; use std::{any::Any, marker::PhantomData, sync::Arc}; @@ -67,7 +67,7 @@ impl Element for View { fn paint( &mut self, - _: Layout, + _: Bounds, _: &mut Self::State, element: &mut Self::FrameState, cx: &mut ViewContext, @@ -81,7 +81,7 @@ trait ViewObject: Send + 'static { fn layout(&mut self, cx: &mut WindowContext) -> Result<(LayoutId, Box)>; fn paint( &mut self, - layout: Layout, + bounds: Bounds, element: &mut dyn Any, cx: &mut WindowContext, ) -> Result<()>; @@ -97,7 +97,12 @@ impl ViewObject for View { }) } - fn paint(&mut self, _: Layout, element: &mut dyn Any, cx: &mut WindowContext) -> Result<()> { + fn paint( + &mut self, + _: Bounds, + element: &mut dyn Any, + cx: &mut WindowContext, + ) -> Result<()> { self.state.update(cx, |state, cx| { let element = element.downcast_mut::>().unwrap(); element.paint(state, None, cx) @@ -124,12 +129,12 @@ impl Element for AnyView { fn paint( &mut self, - layout: Layout, + bounds: Bounds, _: &mut (), element: &mut Box, cx: &mut ViewContext, ) -> Result<()> { - self.view.lock().paint(layout, element.as_mut(), cx) + self.view.lock().paint(bounds, element.as_mut(), cx) } } diff --git a/crates/gpui3/src/window.rs b/crates/gpui3/src/window.rs index 7952c8c85c..3d5d237ee5 100644 --- a/crates/gpui3/src/window.rs +++ b/crates/gpui3/src/window.rs @@ -224,11 +224,11 @@ impl<'a, 'w> WindowContext<'a, 'w> { .request_measured_layout(style, rem_size, measure) } - pub fn layout(&mut self, layout_id: LayoutId) -> Result { + pub fn layout_bounds(&mut self, layout_id: LayoutId) -> Result> { Ok(self .window .layout_engine - .layout(layout_id) + .layout_bounds(layout_id) .map(Into::into)?) } @@ -262,7 +262,6 @@ impl<'a, 'w> WindowContext<'a, 'w> { pub fn paint_glyph( &mut self, origin: Point, - order: u32, font_id: FontId, glyph_id: GlyphId, font_size: Pixels, @@ -302,7 +301,7 @@ impl<'a, 'w> WindowContext<'a, 'w> { self.window.scene.insert( layer_id, MonochromeSprite { - order, + order: 0, bounds, content_mask, color, @@ -316,7 +315,6 @@ impl<'a, 'w> WindowContext<'a, 'w> { pub fn paint_emoji( &mut self, origin: Point, - order: u32, font_id: FontId, glyph_id: GlyphId, font_size: Pixels, @@ -352,7 +350,7 @@ impl<'a, 'w> WindowContext<'a, 'w> { self.window.scene.insert( layer_id, PolychromeSprite { - order, + order: 0, bounds, corner_radii: Default::default(), content_mask, @@ -367,7 +365,6 @@ impl<'a, 'w> WindowContext<'a, 'w> { pub fn paint_svg( &mut self, bounds: Bounds, - order: u32, path: SharedString, color: Hsla, ) -> Result<()> { @@ -394,7 +391,7 @@ impl<'a, 'w> WindowContext<'a, 'w> { self.window.scene.insert( layer_id, MonochromeSprite { - order, + order: 0, bounds, content_mask, color, @@ -409,7 +406,6 @@ impl<'a, 'w> WindowContext<'a, 'w> { &mut self, bounds: Bounds, corner_radii: Corners, - order: u32, data: Arc, grayscale: bool, ) -> Result<()> { @@ -430,7 +426,7 @@ impl<'a, 'w> WindowContext<'a, 'w> { self.window.scene.insert( order, PolychromeSprite { - order: 0, // Used in in Scene::batches. 0 has no meaning. + order: 0, bounds, content_mask, corner_radii, @@ -452,7 +448,7 @@ impl<'a, 'w> WindowContext<'a, 'w> { cx.window .layout_engine .compute_layout(root_layout_id, available_space)?; - let layout = cx.window.layout_engine.layout(root_layout_id)?; + let layout = cx.window.layout_engine.layout_bounds(root_layout_id)?; root_view.paint(layout, &mut (), &mut frame_state, cx)?; cx.window.root_view = Some(root_view); @@ -786,9 +782,3 @@ pub struct AnyWindowHandle { pub(crate) id: WindowId, state_type: TypeId, } - -#[derive(Clone)] -pub struct Layout { - pub order: u32, - pub bounds: Bounds, -} diff --git a/crates/storybook2/src/theme.rs b/crates/storybook2/src/theme.rs index b28a0f289c..498d626d76 100644 --- a/crates/storybook2/src/theme.rs +++ b/crates/storybook2/src/theme.rs @@ -1,5 +1,5 @@ use gpui3::{ - BorrowAppContext, Element, Hsla, Layout, LayoutId, Result, ViewContext, WindowContext, + BorrowAppContext, Bounds, Element, Hsla, LayoutId, Pixels, Result, ViewContext, WindowContext, }; use serde::{de::Visitor, Deserialize, Deserializer}; use std::{collections::HashMap, fmt}; @@ -160,7 +160,7 @@ impl Element for Themed { fn paint( &mut self, - layout: Layout, + bounds: Bounds, state: &mut Self::State, frame_state: &mut Self::FrameState, cx: &mut ViewContext, @@ -169,7 +169,7 @@ impl Element for Themed { Self: Sized, { cx.with_state(self.theme.clone(), |cx| { - self.child.paint(layout, state, frame_state, cx) + self.child.paint(bounds, state, frame_state, cx) }) } }