From 30979caf255f56f117073164c82a39c6faf17721 Mon Sep 17 00:00:00 2001 From: Marshall Bowers Date: Thu, 12 Oct 2023 15:37:50 -0400 Subject: [PATCH] Use new children approach for `Pane`s and `Toast`s --- crates/ui2/src/components/panes.rs | 26 ++++++++------- crates/ui2/src/components/terminal.rs | 29 +++++++++-------- crates/ui2/src/components/toast.rs | 39 +++++++++++++--------- crates/ui2/src/components/workspace.rs | 45 +++++++------------------- 4 files changed, 64 insertions(+), 75 deletions(-) diff --git a/crates/ui2/src/components/panes.rs b/crates/ui2/src/components/panes.rs index ffb0f0491fa34f7abfb954b43bafdbbcb35479c1..f4d168fee3e65352bce4d2f97e30bfa8faf593bb 100644 --- a/crates/ui2/src/components/panes.rs +++ b/crates/ui2/src/components/panes.rs @@ -1,6 +1,7 @@ use std::marker::PhantomData; -use gpui3::{hsla, Hsla, Length, Size}; +use gpui3::{hsla, AnyElement, Hsla, Length, Size}; +use smallvec::SmallVec; use crate::prelude::*; use crate::theme; @@ -18,17 +19,11 @@ pub struct Pane { scroll_state: ScrollState, size: Size, fill: Hsla, - children: HackyChildren, - payload: HackyChildrenPayload, + children: SmallVec<[AnyElement; 2]>, } impl Pane { - pub fn new( - scroll_state: ScrollState, - size: Size, - children: HackyChildren, - payload: HackyChildrenPayload, - ) -> Self { + pub fn new(scroll_state: ScrollState, size: Size) -> Self { // Fill is only here for debugging purposes, remove before release let system_color = SystemColor::new(); @@ -38,8 +33,7 @@ impl Pane { size, fill: hsla(0.3, 0.3, 0.3, 1.), // fill: system_color.transparent, - children, - payload, + children: SmallVec::new(), } } @@ -58,7 +52,15 @@ impl Pane { .w(self.size.width) .h(self.size.height) .overflow_y_scroll(self.scroll_state.clone()) - .children_any((self.children)(cx, self.payload.as_ref())) + .children(self.children.drain(..)) + } +} + +impl ParentElement for Pane { + type State = S; + + fn children_mut(&mut self) -> &mut SmallVec<[AnyElement; 2]> { + &mut self.children } } diff --git a/crates/ui2/src/components/terminal.rs b/crates/ui2/src/components/terminal.rs index 08b47e70ad04442046f7a547d8271ea439d2cc5d..58db6d75cbe8c50542407104996c86b7b231e0d6 100644 --- a/crates/ui2/src/components/terminal.rs +++ b/crates/ui2/src/components/terminal.rs @@ -72,19 +72,16 @@ impl Terminal { ), ) // Terminal Pane. - .child(Pane::new( - ScrollState::default(), - Size { - width: relative(1.).into(), - height: rems(36.).into(), - }, - |_, payload| { - let theme = payload.downcast_ref::>().unwrap(); - - vec![crate::static_data::terminal_buffer(&theme).into_any()] - }, - Box::new(theme), - )) + .child( + Pane::new( + ScrollState::default(), + Size { + width: relative(1.).into(), + height: rems(36.).into(), + }, + ) + .child(crate::static_data::terminal_buffer(&theme)), + ) } } @@ -109,7 +106,11 @@ mod stories { } } - fn render(&mut self, _view: &mut S, cx: &mut ViewContext) -> impl Element { + fn render( + &mut self, + _view: &mut S, + cx: &mut ViewContext, + ) -> impl Element { Story::container(cx) .child(Story::title_for::<_, Terminal>(cx)) .child(Story::label(cx, "Default")) diff --git a/crates/ui2/src/components/toast.rs b/crates/ui2/src/components/toast.rs index 385b4a928c2edae5bfefe19daf9c18c0881aad14..fdba2fda6ed7805261ef63e373fdfef8f58b2276 100644 --- a/crates/ui2/src/components/toast.rs +++ b/crates/ui2/src/components/toast.rs @@ -1,3 +1,8 @@ +use std::marker::PhantomData; + +use gpui3::AnyElement; +use smallvec::SmallVec; + use crate::prelude::*; #[derive(Default, Debug, PartialEq, Eq, Clone, Copy)] @@ -24,20 +29,14 @@ pub enum ToastVariant { #[derive(Element)] pub struct Toast { origin: ToastOrigin, - children: HackyChildren, - payload: HackyChildrenPayload, + children: SmallVec<[AnyElement; 2]>, } impl Toast { - pub fn new( - origin: ToastOrigin, - children: HackyChildren, - payload: HackyChildrenPayload, - ) -> Self { + pub fn new(origin: ToastOrigin) -> Self { Self { origin, - children, - payload, + children: SmallVec::new(), } } @@ -62,7 +61,15 @@ impl Toast { .rounded_md() .fill(color.elevated_surface) .max_w_64() - .children_any((self.children)(cx, self.payload.as_ref())) + .children(self.children.drain(..)) + } +} + +impl ParentElement for Toast { + type State = S; + + fn children_mut(&mut self) -> &mut SmallVec<[AnyElement; 2]> { + &mut self.children } } @@ -89,15 +96,15 @@ mod stories { } } - fn render(&mut self, _view: &mut S, cx: &mut ViewContext) -> impl Element { + fn render( + &mut self, + _view: &mut S, + cx: &mut ViewContext, + ) -> impl Element { Story::container(cx) .child(Story::title_for::<_, Toast>(cx)) .child(Story::label(cx, "Default")) - .child(Toast::new( - ToastOrigin::Bottom, - |_, _| vec![Label::new("label").into_any()], - Box::new(()), - )) + .child(Toast::new(ToastOrigin::Bottom).child(Label::new("label"))) } } } diff --git a/crates/ui2/src/components/workspace.rs b/crates/ui2/src/components/workspace.rs index 2a46fdde1f8f572dcd6202db1ec61ccb36e2fe76..8650de3b8bfc9cfd5f341f5c5ff13169820d41c1 100644 --- a/crates/ui2/src/components/workspace.rs +++ b/crates/ui2/src/components/workspace.rs @@ -172,25 +172,18 @@ impl Workspace { width: relative(1.).into(), height: temp_size, }, - |_, payload| { - let theme = payload.downcast_ref::>().unwrap(); - - vec![EditorPane::new(hello_world_rust_editor_with_status_example( - &theme, - )) - .into_any()] - }, - Box::new(theme.clone()), - ), + ) + .child(EditorPane::new( + hello_world_rust_editor_with_status_example(&theme), + )), Pane::new( ScrollState::default(), Size { width: relative(1.).into(), height: temp_size, }, - |_, _| vec![Terminal::new().into_any()], - Box::new(()), - ), + ) + .child(Terminal::new()), ], SplitDirection::Vertical, ), @@ -201,16 +194,10 @@ impl Workspace { width: relative(1.).into(), height: relative(1.).into(), }, - |_, payload| { - let theme = payload.downcast_ref::>().unwrap(); - - vec![EditorPane::new(hello_world_rust_editor_with_status_example( - &theme, - )) - .into_any()] - }, - Box::new(theme.clone()), - )], + ) + .child(EditorPane::new( + hello_world_rust_editor_with_status_example(&theme), + ))], SplitDirection::Vertical, ), ], @@ -326,16 +313,8 @@ impl Workspace { ) .filter(|_| workspace_state.is_language_selector_open()), ) - .child(Toast::new( - ToastOrigin::Bottom, - |_, _| vec![Label::new("A toast").into_any()], - Box::new(()), - )) - .child(Toast::new( - ToastOrigin::BottomRight, - |_, _| vec![Label::new("Another toast").into_any()], - Box::new(()), - )) + .child(Toast::new(ToastOrigin::Bottom).child(Label::new("A toast"))) + .child(Toast::new(ToastOrigin::BottomRight).child(Label::new("Another toast"))) } }