From c3b1264c05feec23a25cd3012181dc9158fe68eb Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Fri, 8 Sep 2023 14:09:25 -0600 Subject: [PATCH] Checkpoint --- crates/gpui2/src/adapter.rs | 4 ++-- crates/gpui2/src/element.rs | 10 +++++----- crates/gpui2/src/elements/div.rs | 5 ++--- crates/gpui2/src/elements/hoverable.rs | 4 ++-- crates/gpui2/src/elements/img.rs | 8 +++++--- crates/gpui2/src/elements/pressable.rs | 4 ++-- crates/gpui2/src/elements/svg.rs | 2 +- crates/gpui2/src/elements/text.rs | 4 ++-- crates/gpui2/src/gpui2.rs | 4 ++-- .../gpui2/src/{layout_context.rs => view_context.rs} | 10 +++++----- crates/gpui2_macros/src/derive_element.rs | 2 +- crates/storybook/src/components.rs | 9 ++++++--- crates/storybook/src/storybook.rs | 2 +- crates/storybook/src/theme.rs | 5 ++--- 14 files changed, 38 insertions(+), 35 deletions(-) rename crates/gpui2/src/{layout_context.rs => view_context.rs} (76%) diff --git a/crates/gpui2/src/adapter.rs b/crates/gpui2/src/adapter.rs index 6aa89385af4e9f22297264080ab3761e3365acef..d1bc7ac290e3b9ac44eb3e54e8951cbf4a0aba29 100644 --- a/crates/gpui2/src/adapter.rs +++ b/crates/gpui2/src/adapter.rs @@ -1,4 +1,4 @@ -use crate::{layout_context::LayoutContext, paint_context::PaintContext}; +use crate::{paint_context::PaintContext, ViewContext}; use gpui::{geometry::rect::RectF, LayoutEngine, LayoutId}; use util::ResultExt; @@ -17,7 +17,7 @@ impl gpui::Element for AdapterElement { ) -> (gpui::geometry::vector::Vector2F, Self::LayoutState) { cx.push_layout_engine(LayoutEngine::new()); - let mut cx = LayoutContext::new(cx); + let mut cx = ViewContext::new(cx); let layout_id = self.0.layout(view, &mut cx).log_err(); if let Some(layout_id) = layout_id { cx.layout_engine() diff --git a/crates/gpui2/src/element.rs b/crates/gpui2/src/element.rs index 3f376f978f21475e958c3dabb57a796ac67ff00d..d2a8efee830518aa695ca2c6bc8bb8e6a886de4b 100644 --- a/crates/gpui2/src/element.rs +++ b/crates/gpui2/src/element.rs @@ -1,5 +1,5 @@ -pub use crate::layout_context::LayoutContext; pub use crate::paint_context::PaintContext; +pub use crate::ViewContext; use anyhow::Result; use gpui::geometry::vector::Vector2F; pub use gpui::{Layout, LayoutId}; @@ -11,7 +11,7 @@ pub trait Element: 'static + IntoElement { fn layout( &mut self, view: &mut V, - cx: &mut LayoutContext, + cx: &mut ViewContext, ) -> Result<(LayoutId, Self::PaintState)> where Self: Sized; @@ -39,7 +39,7 @@ pub trait Element: 'static + IntoElement { /// Used to make ElementState into a trait object, so we can wrap it in AnyElement. trait AnyStatefulElement { - fn layout(&mut self, view: &mut V, cx: &mut LayoutContext) -> Result; + fn layout(&mut self, view: &mut V, cx: &mut ViewContext) -> Result; fn paint(&mut self, view: &mut V, parent_origin: Vector2F, cx: &mut PaintContext); } @@ -86,7 +86,7 @@ impl> Default for ElementPhase { /// We blanket-implement the object-safe ElementStateObject interface to make ElementStates into trait objects impl> AnyStatefulElement for StatefulElement { - fn layout(&mut self, view: &mut V, cx: &mut LayoutContext) -> Result { + fn layout(&mut self, view: &mut V, cx: &mut ViewContext) -> Result { let result; self.phase = match self.element.layout(view, cx) { Ok((layout_id, paint_state)) => { @@ -145,7 +145,7 @@ impl> AnyStatefulElement for StatefulElement { pub struct AnyElement(Box>); impl AnyElement { - pub fn layout(&mut self, view: &mut V, cx: &mut LayoutContext) -> Result { + pub fn layout(&mut self, view: &mut V, cx: &mut ViewContext) -> Result { self.0.layout(view, cx) } diff --git a/crates/gpui2/src/elements/div.rs b/crates/gpui2/src/elements/div.rs index 162383642a865f719fa32719fce587f377dcc59a..f32b22434a87a2db3601af0477d4ae07ab15c379 100644 --- a/crates/gpui2/src/elements/div.rs +++ b/crates/gpui2/src/elements/div.rs @@ -3,10 +3,9 @@ use std::{cell::Cell, rc::Rc}; use crate::{ element::{AnyElement, Element, IntoElement, Layout, ParentElement}, hsla, - layout_context::LayoutContext, paint_context::PaintContext, style::{CornerRadii, Overflow, Style, StyleHelpers, Styleable}, - InteractionHandlers, Interactive, + InteractionHandlers, Interactive, ViewContext, }; use anyhow::Result; use gpui::{ @@ -41,7 +40,7 @@ impl Element for Div { fn layout( &mut self, view: &mut V, - cx: &mut LayoutContext, + cx: &mut ViewContext, ) -> Result<(LayoutId, Self::PaintState)> where Self: Sized, diff --git a/crates/gpui2/src/elements/hoverable.rs b/crates/gpui2/src/elements/hoverable.rs index 89a8b663ee8bc112ab95a77e395da4b08790589c..ba8109038fdaa05bfb05467c7b9009b280b4c87b 100644 --- a/crates/gpui2/src/elements/hoverable.rs +++ b/crates/gpui2/src/elements/hoverable.rs @@ -1,9 +1,9 @@ use crate::{ element::{AnyElement, Element, IntoElement, Layout, ParentElement}, interactive::{InteractionHandlers, Interactive}, - layout_context::LayoutContext, paint_context::PaintContext, style::{Style, StyleHelpers, Styleable}, + ViewContext, }; use anyhow::Result; use gpui::{geometry::vector::Vector2F, platform::MouseMovedEvent, LayoutId}; @@ -45,7 +45,7 @@ impl + Styleable> Element for Hoverable { fn layout( &mut self, view: &mut V, - cx: &mut LayoutContext, + cx: &mut ViewContext, ) -> Result<(LayoutId, Self::PaintState)> where Self: Sized, diff --git a/crates/gpui2/src/elements/img.rs b/crates/gpui2/src/elements/img.rs index 8f9620ce2a06788ed2d02430bcb41eb0831601f6..a4934c54155eb649c4b975980b90289832add82e 100644 --- a/crates/gpui2/src/elements/img.rs +++ b/crates/gpui2/src/elements/img.rs @@ -1,6 +1,8 @@ use crate as gpui2; -use crate::style::{StyleHelpers, Styleable}; -use crate::{style::Style, Element}; +use crate::{ + style::{Style, StyleHelpers, Styleable}, + Element, +}; use futures::FutureExt; use gpui::geometry::vector::Vector2F; use gpui::scene; @@ -35,7 +37,7 @@ impl Element for Img { fn layout( &mut self, _: &mut V, - cx: &mut crate::LayoutContext, + cx: &mut crate::ViewContext, ) -> anyhow::Result<(gpui::LayoutId, Self::PaintState)> where Self: Sized, diff --git a/crates/gpui2/src/elements/pressable.rs b/crates/gpui2/src/elements/pressable.rs index db6652fad5e5dc3212c38b18394db212eee9d0ad..1b696e7ef6030c78337f56fcd3657b4c1e14bcaf 100644 --- a/crates/gpui2/src/elements/pressable.rs +++ b/crates/gpui2/src/elements/pressable.rs @@ -1,9 +1,9 @@ use crate::{ element::{AnyElement, Element, IntoElement, Layout, ParentElement}, interactive::{InteractionHandlers, Interactive}, - layout_context::LayoutContext, paint_context::PaintContext, style::{Style, StyleHelpers, Styleable}, + ViewContext, }; use anyhow::Result; use gpui::{geometry::vector::Vector2F, platform::MouseButtonEvent, LayoutId}; @@ -45,7 +45,7 @@ impl + Styleable> Element for Pressable { fn layout( &mut self, view: &mut V, - cx: &mut LayoutContext, + cx: &mut ViewContext, ) -> Result<(LayoutId, Self::PaintState)> where Self: Sized, diff --git a/crates/gpui2/src/elements/svg.rs b/crates/gpui2/src/elements/svg.rs index 216725b6d6c4b0a36bf40bef65177daace17fcd5..ebf06178aa8c174c63fc65c7adc6e7c694f7ac0e 100644 --- a/crates/gpui2/src/elements/svg.rs +++ b/crates/gpui2/src/elements/svg.rs @@ -34,7 +34,7 @@ impl Element for Svg { fn layout( &mut self, _: &mut V, - cx: &mut crate::LayoutContext, + cx: &mut crate::ViewContext, ) -> anyhow::Result<(LayoutId, Self::PaintState)> where Self: Sized, diff --git a/crates/gpui2/src/elements/text.rs b/crates/gpui2/src/elements/text.rs index c0e2996d16f62fe173550296577eca3098086ba9..97b911201acd4897d5e4c92594de23eb6564f354 100644 --- a/crates/gpui2/src/elements/text.rs +++ b/crates/gpui2/src/elements/text.rs @@ -1,7 +1,7 @@ use crate::{ element::{Element, IntoElement, Layout}, - layout_context::LayoutContext, paint_context::PaintContext, + ViewContext, }; use anyhow::Result; use gpui::{ @@ -31,7 +31,7 @@ impl Element for Text { fn layout( &mut self, _view: &mut V, - cx: &mut LayoutContext, + cx: &mut ViewContext, ) -> Result<(LayoutId, Self::PaintState)> { let fonts = cx.platform().fonts(); let text_style = cx.text_style(); diff --git a/crates/gpui2/src/gpui2.rs b/crates/gpui2/src/gpui2.rs index c92d22ba61772e06a65e0c67a8a117495a1ece08..11d5279ec134336751d8b91eb466fb17548c560c 100644 --- a/crates/gpui2/src/gpui2.rs +++ b/crates/gpui2/src/gpui2.rs @@ -3,10 +3,10 @@ pub mod color; pub mod element; pub mod elements; pub mod interactive; -pub mod layout_context; pub mod paint_context; pub mod style; pub mod view; +pub mod view_context; pub use color::*; pub use element::{AnyElement, Element, IntoElement, Layout, ParentElement}; @@ -17,7 +17,7 @@ pub use geometry::{ pub use gpui::*; pub use gpui2_macros::{Element, *}; pub use interactive::*; -pub use layout_context::LayoutContext; pub use platform::{Platform, WindowBounds, WindowOptions}; pub use util::arc_cow::ArcCow; pub use view::*; +pub use view_context::ViewContext; diff --git a/crates/gpui2/src/layout_context.rs b/crates/gpui2/src/view_context.rs similarity index 76% rename from crates/gpui2/src/layout_context.rs rename to crates/gpui2/src/view_context.rs index 0249246514e015ab14a515963c10035c1742f2b6..43d8093240d6cecfa6d8616d25701f23e15e6f57 100644 --- a/crates/gpui2/src/layout_context.rs +++ b/crates/gpui2/src/view_context.rs @@ -2,17 +2,17 @@ use crate::{element::LayoutId, style::Style}; use anyhow::{anyhow, Result}; use derive_more::{Deref, DerefMut}; use gpui::{geometry::Size, MeasureParams}; -pub use gpui::{taffy::tree::NodeId, LayoutContext as LegacyLayoutContext}; +pub use gpui::{taffy::tree::NodeId, ViewContext as LegacyViewContext}; #[derive(Deref, DerefMut)] -pub struct LayoutContext<'a, 'b, 'c, 'd, V> { +pub struct ViewContext<'a, 'b, 'c, V> { #[deref] #[deref_mut] - pub(crate) legacy_cx: &'d mut LegacyLayoutContext<'a, 'b, 'c, V>, + pub(crate) legacy_cx: &'c mut LegacyViewContext<'a, 'b, V>, } -impl<'a, 'b, 'c, 'd, V: 'static> LayoutContext<'a, 'b, 'c, 'd, V> { - pub fn new(legacy_cx: &'d mut LegacyLayoutContext<'a, 'b, 'c, V>) -> Self { +impl<'a, 'b, 'c, V: 'static> ViewContext<'a, 'b, 'c, V> { + pub fn new(legacy_cx: &'c mut LegacyViewContext<'a, 'b, V>) -> Self { Self { legacy_cx } } diff --git a/crates/gpui2_macros/src/derive_element.rs b/crates/gpui2_macros/src/derive_element.rs index ca94112a020726324e0908e7c6b8f0375b68d750..a769437676e5cd93957852ba62887518d773d17f 100644 --- a/crates/gpui2_macros/src/derive_element.rs +++ b/crates/gpui2_macros/src/derive_element.rs @@ -67,7 +67,7 @@ pub fn derive_element(input: TokenStream) -> TokenStream { fn layout( &mut self, view: &mut V, - cx: &mut gpui2::element::LayoutContext, + cx: &mut gpui2::ViewContext, ) -> anyhow::Result<(gpui2::element::LayoutId, Self::PaintState)> { let mut rendered_element = self.render(view, cx).into_element().into_any(); let layout_id = rendered_element.layout(view, cx)?; diff --git a/crates/storybook/src/components.rs b/crates/storybook/src/components.rs index c24f9d672b88fb59773096bc30502da64d3cfd91..1aafefc1a6a0a89728f64b6c6299e8c68ef1cc20 100644 --- a/crates/storybook/src/components.rs +++ b/crates/storybook/src/components.rs @@ -1,11 +1,11 @@ use gpui2::{ elements::div, interactive::Interactive, platform::MouseButton, style::StyleHelpers, ArcCow, - Element, IntoElement, ParentElement, ViewContext, + Element, EventContext, IntoElement, ParentElement, ViewContext, }; use std::{marker::PhantomData, rc::Rc}; struct ButtonHandlers { - click: Option)>>, + click: Option)>>, } impl Default for ButtonHandlers { @@ -59,7 +59,10 @@ impl Button { self } - pub fn on_click(mut self, handler: impl Fn(&mut V, &D, &mut ViewContext) + 'static) -> Self { + pub fn on_click( + mut self, + handler: impl Fn(&mut V, &D, &mut EventContext) + 'static, + ) -> Self { self.handlers.click = Some(Rc::new(handler)); self } diff --git a/crates/storybook/src/storybook.rs b/crates/storybook/src/storybook.rs index cbfc9681f6e892f2ccdbf58b6c3a7e61c5e801d2..04e1038988fc640d54a2d1d289319af7c832d7c2 100644 --- a/crates/storybook/src/storybook.rs +++ b/crates/storybook/src/storybook.rs @@ -41,7 +41,7 @@ fn main() { |cx| { view(|cx| { cx.enable_inspector(); - storybook(cx) + storybook(&mut ViewContext::new(cx)) }) }, ); diff --git a/crates/storybook/src/theme.rs b/crates/storybook/src/theme.rs index fbcb8e37a8cdad16d0e96ab6c46054f711a4debe..45327e1ffcf6ca781413590ab652e3525909320b 100644 --- a/crates/storybook/src/theme.rs +++ b/crates/storybook/src/theme.rs @@ -1,8 +1,7 @@ use gpui2::{ color::Hsla, element::{Element, PaintContext}, - layout_context::LayoutContext, - serde_json, AppContext, IntoElement, Vector2F, WindowContext, + serde_json, AppContext, IntoElement, Vector2F, ViewContext, WindowContext, }; use serde::{de::Visitor, Deserialize, Deserializer}; use std::{collections::HashMap, fmt, marker::PhantomData}; @@ -146,7 +145,7 @@ impl> Element for Themed { fn layout( &mut self, view: &mut V, - cx: &mut LayoutContext, + cx: &mut ViewContext, ) -> anyhow::Result<(gpui2::LayoutId, Self::PaintState)> where Self: Sized,