From 3623a9ca5edb0f7e39cf173514e50c8b25b3b0f3 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Tue, 15 Aug 2023 15:26:02 -0700 Subject: [PATCH] Simplify Component implementation Co-authored-by: Mikayla --- crates/gpui/examples/components.rs | 74 +++++++++++++----------------- 1 file changed, 32 insertions(+), 42 deletions(-) diff --git a/crates/gpui/examples/components.rs b/crates/gpui/examples/components.rs index 50ace5eee049a9a650185c39f59a07d8d915f773..09a732c6c1b640ebdead04d960ebc83fae3f15a3 100644 --- a/crates/gpui/examples/components.rs +++ b/crates/gpui/examples/components.rs @@ -1,6 +1,6 @@ use button_component::Button; -use component::AdaptComponent; +use component::Component; use gpui::{ color::Color, elements::{ContainerStyle, Flex, Label, ParentElement}, @@ -14,6 +14,8 @@ use simplelog::SimpleLogger; use theme::Toggleable; use toggleable_button::ToggleableButton; +// cargo run -p gpui --example components + fn main() { SimpleLogger::init(LevelFilter::Info, Default::default()).expect("could not initialize logger"); @@ -155,14 +157,8 @@ mod toggleable_button { } } - impl Component for ToggleableButton { - type View = V; - - fn render( - self, - v: &mut Self::View, - cx: &mut gpui::ViewContext, - ) -> gpui::AnyElement { + impl Component for ToggleableButton { + fn render(self, v: &mut V, cx: &mut gpui::ViewContext) -> gpui::AnyElement { let button = if let Some(style) = self.style { self.button.with_style(*style.style_for(self.active)) } else { @@ -219,10 +215,8 @@ mod button_component { } } - impl Component for Button { - type View = V; - - fn render(self, _: &mut Self::View, cx: &mut ViewContext) -> AnyElement { + impl Component for Button { + fn render(self, _: &mut V, cx: &mut ViewContext) -> AnyElement { let click_handler = self.click_handler; let result = MouseEventHandler::new_dynamic(self.tag, 0, cx, |_, _| { @@ -250,45 +244,41 @@ mod component { use gpui::{AnyElement, Element, View, ViewContext}; use pathfinder_geometry::vector::Vector2F; - // Public API: - pub trait Component { - type View: View; + pub trait Component { + fn render(self, v: &mut V, cx: &mut ViewContext) -> AnyElement; - fn render( - self, - v: &mut Self::View, - cx: &mut ViewContext, - ) -> AnyElement; + fn into_element(self) -> ComponentAdapter + where + Self: Sized, + { + ComponentAdapter::new(self) + } } - pub struct ComponentAdapter { + pub struct ComponentAdapter { component: Option, + phantom: std::marker::PhantomData, } - impl ComponentAdapter { + impl ComponentAdapter { pub fn new(e: E) -> Self { - Self { component: Some(e) } - } - } - - pub trait AdaptComponent: Sized { - fn into_element(self) -> ComponentAdapter { - ComponentAdapter::new(self) + Self { + component: Some(e), + phantom: std::marker::PhantomData, + } } } - impl AdaptComponent for C {} - - impl Element for ComponentAdapter { - type LayoutState = AnyElement; + impl + 'static> Element for ComponentAdapter { + type LayoutState = AnyElement; type PaintState = (); fn layout( &mut self, constraint: gpui::SizeConstraint, - view: &mut C::View, - cx: &mut gpui::LayoutContext, + view: &mut V, + cx: &mut gpui::LayoutContext, ) -> (Vector2F, Self::LayoutState) { let component = self.component.take().unwrap(); let mut element = component.render(view, cx.view_context()); @@ -302,8 +292,8 @@ mod component { bounds: gpui::geometry::rect::RectF, visible_bounds: gpui::geometry::rect::RectF, layout: &mut Self::LayoutState, - view: &mut C::View, - cx: &mut gpui::PaintContext, + view: &mut V, + cx: &mut gpui::PaintContext, ) -> Self::PaintState { layout.paint(scene, bounds.origin(), visible_bounds, view, cx) } @@ -315,8 +305,8 @@ mod component { _: gpui::geometry::rect::RectF, _: &Self::LayoutState, _: &Self::PaintState, - _: &C::View, - _: &ViewContext, + _: &V, + _: &ViewContext, ) -> Option { todo!() } @@ -326,8 +316,8 @@ mod component { _: gpui::geometry::rect::RectF, _: &Self::LayoutState, _: &Self::PaintState, - _: &C::View, - _: &ViewContext, + _: &V, + _: &ViewContext, ) -> serde_json::Value { todo!() }