Checkpoint

This commit is contained in:
Nathan Sobo 2023-10-12 13:27:46 -06:00
parent 2044ccdc0b
commit ce8533f83b
11 changed files with 161 additions and 181 deletions

View file

@ -102,10 +102,10 @@ impl<T> core::ops::DerefMut for Lease<T> {
impl<T> Drop for Lease<T> { impl<T> Drop for Lease<T> {
fn drop(&mut self) { fn drop(&mut self) {
assert!( if self.entity.is_some() {
self.entity.is_none(), // We don't panic here, because other panics can cause us to drop the lease without ending it cleanly.
"Leases must be ended with EntityMap::end_lease" log::error!("Leases must be ended with EntityMap::end_lease")
); }
} }
} }

View file

@ -33,8 +33,8 @@ pub fn view<S, E>(
render: impl Fn(&mut S, &mut ViewContext<S>) -> E + Send + Sync + 'static, render: impl Fn(&mut S, &mut ViewContext<S>) -> E + Send + Sync + 'static,
) -> View<S> ) -> View<S>
where where
E: IntoAnyElement<S>,
S: 'static + Send + Sync, S: 'static + Send + Sync,
E: Element<ViewState = S>,
{ {
View { View {
state, state,
@ -86,6 +86,8 @@ impl<S: 'static + Send + Sync> Element for View<S> {
} }
} }
impl<S: Send + Sync + 'static> IdentifiedElement for View<S> {}
struct EraseViewState<ViewState: 'static + Send + Sync, ParentViewState> { struct EraseViewState<ViewState: 'static + Send + Sync, ParentViewState> {
view: View<ViewState>, view: View<ViewState>,
parent_view_state_type: PhantomData<ParentViewState>, parent_view_state_type: PhantomData<ParentViewState>,
@ -137,11 +139,9 @@ where
trait ViewObject: 'static + Send + Sync { trait ViewObject: 'static + Send + Sync {
fn entity_id(&self) -> EntityId; fn entity_id(&self) -> EntityId;
fn layout(&mut self, cx: &mut WindowContext) -> (LayoutId, AnyBox); fn layout(&mut self, cx: &mut WindowContext) -> (LayoutId, AnyBox);
fn paint(&mut self, bounds: Bounds<Pixels>, element: &mut dyn Any, cx: &mut WindowContext); fn paint(&mut self, bounds: Bounds<Pixels>, element: &mut AnyBox, cx: &mut WindowContext);
} }
impl<S: Send + Sync + 'static> IdentifiedElement for View<S> {}
impl<S: Send + Sync + 'static> ViewObject for View<S> { impl<S: Send + Sync + 'static> ViewObject for View<S> {
fn entity_id(&self) -> EntityId { fn entity_id(&self) -> EntityId {
self.state.id self.state.id
@ -158,7 +158,7 @@ impl<S: Send + Sync + 'static> ViewObject for View<S> {
}) })
} }
fn paint(&mut self, _: Bounds<Pixels>, element: &mut dyn Any, cx: &mut WindowContext) { fn paint(&mut self, _: Bounds<Pixels>, element: &mut AnyBox, cx: &mut WindowContext) {
cx.with_element_id(IdentifiedElement::element_id(self), |cx| { cx.with_element_id(IdentifiedElement::element_id(self), |cx| {
self.state.update(cx, |state, cx| { self.state.update(cx, |state, cx| {
let element = element.downcast_mut::<AnyElement<S>>().unwrap(); let element = element.downcast_mut::<AnyElement<S>>().unwrap();
@ -208,7 +208,7 @@ impl Element for AnyView {
element: &mut AnyBox, element: &mut AnyBox,
cx: &mut ViewContext<Self::ViewState>, cx: &mut ViewContext<Self::ViewState>,
) { ) {
self.view.lock().paint(bounds, element.as_mut(), cx) self.view.lock().paint(bounds, element, cx)
} }
} }

View file

@ -56,6 +56,7 @@ pub fn derive_element(input: TokenStream) -> TokenStream {
type ElementState = gpui3::AnyElement<#state_type>; type ElementState = gpui3::AnyElement<#state_type>;
fn element_id(&self) -> Option<gpui3::ElementId> { fn element_id(&self) -> Option<gpui3::ElementId> {
// todo!("What should element_id be here?")
None None
} }

View file

@ -7,7 +7,7 @@ use clap::ValueEnum;
use gpui3::AnyElement; use gpui3::AnyElement;
use strum::{EnumIter, EnumString, IntoEnumIterator}; use strum::{EnumIter, EnumString, IntoEnumIterator};
use ui::prelude::*; use ui::{prelude::*, AssistantPanelStory};
#[derive(Debug, PartialEq, Eq, Clone, Copy, strum::Display, EnumString, EnumIter)] #[derive(Debug, PartialEq, Eq, Clone, Copy, strum::Display, EnumString, EnumIter)]
#[strum(serialize_all = "snake_case")] #[strum(serialize_all = "snake_case")]
@ -90,7 +90,7 @@ impl ComponentStory {
Self::Toast => ui::ToastStory::new().into_any(), Self::Toast => ui::ToastStory::new().into_any(),
Self::Toolbar => ui::ToolbarStory::new().into_any(), Self::Toolbar => ui::ToolbarStory::new().into_any(),
Self::TrafficLights => ui::TrafficLightsStory::new().into_any(), Self::TrafficLights => ui::TrafficLightsStory::new().into_any(),
Self::Workspace => todo!(), Self::Workspace => ui::workspace_story(cx).into_any().into_any(),
} }
} }
} }

View file

@ -94,6 +94,7 @@ fn main() {
#[derive(Clone)] #[derive(Clone)]
pub struct StoryWrapper { pub struct StoryWrapper {
selector: StorySelector, selector: StorySelector,
// story:
theme: Theme, theme: Theme,
} }

View file

@ -33,12 +33,8 @@ impl<S: 'static + Send + Sync + Clone> AssistantPanel<S> {
pub scroll_state: ScrollState, pub scroll_state: ScrollState,
} }
Panel::new( Panel::new(self.scroll_state.clone())
self.scroll_state.clone(), .children(vec![div()
|_, payload| {
let payload = payload.downcast_ref::<PanelPayload>().unwrap();
vec![div()
.flex() .flex()
.flex_col() .flex_col()
.h_full() .h_full()
@ -75,15 +71,10 @@ impl<S: 'static + Send + Sync + Clone> AssistantPanel<S> {
.flex() .flex()
.flex_col() .flex_col()
.gap_3() .gap_3()
.overflow_y_scroll(payload.scroll_state.clone()) .overflow_y_scroll(self.scroll_state.clone())
.child(Label::new("Is this thing on?")), .child(Label::new("Is this thing on?")),
) )
.into_any()] .into_any()])
},
Box::new(PanelPayload {
scroll_state: self.scroll_state.clone(),
}),
)
.side(self.current_side) .side(self.current_side)
.width(AbsoluteLength::Rems(rems(32.))) .width(AbsoluteLength::Rems(rems(32.)))
} }
@ -110,7 +101,11 @@ mod stories {
} }
} }
fn render(&mut self, _view: &mut S, cx: &mut ViewContext<S>) -> impl Element<ViewState = S> { fn render(
&mut self,
_view: &mut S,
cx: &mut ViewContext<S>,
) -> impl Element<ViewState = S> {
Story::container(cx) Story::container(cx)
.child(Story::title_for::<_, AssistantPanel<S>>(cx)) .child(Story::title_for::<_, AssistantPanel<S>>(cx))
.child(Story::label(cx, "Default")) .child(Story::label(cx, "Default"))

View file

@ -20,7 +20,7 @@ impl<S: 'static + Send + Sync + Clone> ChatPanel<S> {
} }
} }
pub fn with_messages(mut self, messages: Vec<ChatMessage<S>>) -> Self { pub fn messages(mut self, messages: Vec<ChatMessage<S>>) -> Self {
self.messages = messages; self.messages = messages;
self self
} }
@ -130,21 +130,21 @@ mod stories {
} }
} }
fn render(&mut self, _view: &mut S, cx: &mut ViewContext<S>) -> impl Element<ViewState = S> { fn render(
&mut self,
_view: &mut S,
cx: &mut ViewContext<S>,
) -> impl Element<ViewState = S> {
Story::container(cx) Story::container(cx)
.child(Story::title_for::<_, ChatPanel<S>>(cx)) .child(Story::title_for::<_, ChatPanel<S>>(cx))
.child(Story::label(cx, "Default")) .child(Story::label(cx, "Default"))
.child(Panel::new( .child(
ScrollState::default(), Panel::new(ScrollState::default())
|_, _| vec![ChatPanel::new(ScrollState::default()).into_any()], .child(ChatPanel::new(ScrollState::default())),
Box::new(()), )
))
.child(Story::label(cx, "With Mesages")) .child(Story::label(cx, "With Mesages"))
.child(Panel::new( .child(Panel::new(ScrollState::default()).child(
ScrollState::default(), ChatPanel::new(ScrollState::default()).messages(vec![
|_, _| {
vec![ChatPanel::new(ScrollState::default())
.with_messages(vec![
ChatMessage::new( ChatMessage::new(
"osiewicz".to_string(), "osiewicz".to_string(),
"is this thing on?".to_string(), "is this thing on?".to_string(),
@ -159,10 +159,7 @@ mod stories {
.unwrap() .unwrap()
.naive_local(), .naive_local(),
), ),
]) ]),
.into_any()]
},
Box::new(()),
)) ))
} }
} }

View file

@ -62,7 +62,11 @@ mod stories {
} }
} }
fn render(&mut self, _view: &mut S, cx: &mut ViewContext<S>) -> impl Element<ViewState = S> { fn render(
&mut self,
_view: &mut S,
cx: &mut ViewContext<S>,
) -> impl Element<ViewState = S> {
let theme = theme(cx); let theme = theme(cx);
Story::container(cx) Story::container(cx)

View file

@ -1,6 +1,7 @@
use std::marker::PhantomData; use std::marker::PhantomData;
use gpui3::AbsoluteLength; use gpui3::{AbsoluteLength, AnyElement};
use smallvec::SmallVec;
use crate::{prelude::*, theme}; use crate::{prelude::*, theme};
use crate::{token, v_stack}; use crate::{token, v_stack};
@ -47,16 +48,11 @@ pub struct Panel<S: 'static + Send + Sync> {
allowed_sides: PanelAllowedSides, allowed_sides: PanelAllowedSides,
initial_width: AbsoluteLength, initial_width: AbsoluteLength,
width: Option<AbsoluteLength>, width: Option<AbsoluteLength>,
children: HackyChildren<S>, children: SmallVec<[AnyElement<S>; 2]>,
payload: HackyChildrenPayload,
} }
impl<S: 'static + Send + Sync> Panel<S> { impl<S: 'static + Send + Sync> Panel<S> {
pub fn new( pub fn new(scroll_state: ScrollState) -> Self {
scroll_state: ScrollState,
children: HackyChildren<S>,
payload: HackyChildrenPayload,
) -> Self {
let token = token(); let token = token();
Self { Self {
@ -66,8 +62,7 @@ impl<S: 'static + Send + Sync> Panel<S> {
allowed_sides: PanelAllowedSides::default(), allowed_sides: PanelAllowedSides::default(),
initial_width: token.default_panel_size, initial_width: token.default_panel_size,
width: None, width: None,
children, children: SmallVec::new(),
payload,
} }
} }
@ -140,7 +135,15 @@ impl<S: 'static + Send + Sync> Panel<S> {
} }
} }
panel_base.children_any((self.children)(cx, self.payload.as_ref())) panel_base.children(self.children.drain(..))
}
}
impl<S: 'static + Send + Sync> ParentElement for Panel<S> {
type State = S;
fn children_mut(&mut self) -> &mut SmallVec<[AnyElement<Self::State>; 2]> {
&mut self.children
} }
} }
@ -165,20 +168,21 @@ mod stories {
} }
} }
fn render(&mut self, _view: &mut S, cx: &mut ViewContext<S>) -> impl Element<ViewState = S> { fn render(
&mut self,
_view: &mut S,
cx: &mut ViewContext<S>,
) -> impl Element<ViewState = S> {
Story::container(cx) Story::container(cx)
.child(Story::title_for::<_, Panel<S>>(cx)) .child(Story::title_for::<_, Panel<S>>(cx))
.child(Story::label(cx, "Default")) .child(Story::label(cx, "Default"))
.child(Panel::new( .child(
ScrollState::default(), Panel::new(ScrollState::default()).child(
|_, _| { div()
vec![div()
.overflow_y_scroll(ScrollState::default()) .overflow_y_scroll(ScrollState::default())
.children((0..100).map(|ix| Label::new(format!("Item {}", ix + 1)))) .children((0..100).map(|ix| Label::new(format!("Item {}", ix + 1)))),
.into_any()] ),
}, )
Box::new(()),
))
} }
} }
} }

View file

@ -78,15 +78,18 @@ mod stories {
} }
} }
fn render(&mut self, _view: &mut S, cx: &mut ViewContext<S>) -> impl Element<ViewState = S> { fn render(
&mut self,
_view: &mut S,
cx: &mut ViewContext<S>,
) -> impl Element<ViewState = S> {
Story::container(cx) Story::container(cx)
.child(Story::title_for::<_, ProjectPanel<S>>(cx)) .child(Story::title_for::<_, ProjectPanel<S>>(cx))
.child(Story::label(cx, "Default")) .child(Story::label(cx, "Default"))
.child(Panel::new( .child(
ScrollState::default(), Panel::new(ScrollState::default())
|_, _| vec![ProjectPanel::new(ScrollState::default()).into_any()], .child(ProjectPanel::new(ScrollState::default())),
Box::new(()), )
))
} }
} }
} }

View file

@ -244,26 +244,16 @@ impl Workspace {
.border_color(theme.lowest.base.default.border) .border_color(theme.lowest.base.default.border)
.children( .children(
Some( Some(
Panel::new( Panel::new(self.left_panel_scroll_state.clone())
self.left_panel_scroll_state.clone(), .side(PanelSide::Left)
|_, payload| { .child(ProjectPanel::new(ScrollState::default())),
vec![ProjectPanel::new(ScrollState::default()).into_any()]
},
Box::new(()),
)
.side(PanelSide::Left),
) )
.filter(|_| workspace_state.is_project_panel_open()), .filter(|_| workspace_state.is_project_panel_open()),
) )
.children( .children(
Some( Some(
Panel::new( Panel::new(self.left_panel_scroll_state.clone())
self.left_panel_scroll_state.clone(), .child(CollabPanel::new(ScrollState::default()))
|_, payload| {
vec![CollabPanel::new(ScrollState::default()).into_any()]
},
Box::new(()),
)
.side(PanelSide::Left), .side(PanelSide::Left),
) )
.filter(|_| workspace_state.is_collab_panel_open()), .filter(|_| workspace_state.is_collab_panel_open()),
@ -285,11 +275,8 @@ impl Workspace {
) )
.children( .children(
Some( Some(
Panel::new( Panel::new(self.bottom_panel_scroll_state.clone())
self.bottom_panel_scroll_state.clone(), .child(Terminal::new())
|_, _| vec![Terminal::new().into_any()],
Box::new(()),
)
.allowed_sides(PanelAllowedSides::BottomOnly) .allowed_sides(PanelAllowedSides::BottomOnly)
.side(PanelSide::Bottom), .side(PanelSide::Bottom),
) )
@ -298,44 +285,32 @@ impl Workspace {
) )
.children( .children(
Some( Some(
Panel::new( Panel::new(self.right_panel_scroll_state.clone())
self.right_panel_scroll_state.clone(), .side(PanelSide::Right)
|_, payload| { .child(ChatPanel::new(ScrollState::default()).messages(vec![
vec![ChatPanel::new(ScrollState::default())
.with_messages(vec![
ChatMessage::new( ChatMessage::new(
"osiewicz".to_string(), "osiewicz".to_string(),
"is this thing on?".to_string(), "is this thing on?".to_string(),
DateTime::parse_from_rfc3339( DateTime::parse_from_rfc3339("2023-09-27T15:40:52.707Z")
"2023-09-27T15:40:52.707Z",
)
.unwrap() .unwrap()
.naive_local(), .naive_local(),
), ),
ChatMessage::new( ChatMessage::new(
"maxdeviant".to_string(), "maxdeviant".to_string(),
"Reading you loud and clear!".to_string(), "Reading you loud and clear!".to_string(),
DateTime::parse_from_rfc3339( DateTime::parse_from_rfc3339("2023-09-28T15:40:52.707Z")
"2023-09-28T15:40:52.707Z",
)
.unwrap() .unwrap()
.naive_local(), .naive_local(),
), ),
]) ])),
.into_any()]
},
Box::new(()),
)
.side(PanelSide::Right),
) )
.filter(|_| workspace_state.is_chat_panel_open()), .filter(|_| workspace_state.is_chat_panel_open()),
) )
.children( .children(
Some(Panel::new( Some(
self.right_panel_scroll_state.clone(), Panel::new(self.right_panel_scroll_state.clone())
|_, _| vec![AssistantPanel::new().into_any()], .child(AssistantPanel::new()),
Box::new(()), )
))
.filter(|_| workspace_state.is_assistant_panel_open()), .filter(|_| workspace_state.is_assistant_panel_open()),
), ),
) )