From 06701e78aae2e18afd2984193308e333d7680266 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Wed, 21 Jun 2023 11:44:32 +0200 Subject: [PATCH] WIP --- crates/ai/src/ai.rs | 4 +-- crates/ai/src/assistant.rs | 59 +++++++++++++++++++++++++++++++------- crates/theme/src/theme.rs | 9 ++++++ 3 files changed, 59 insertions(+), 13 deletions(-) diff --git a/crates/ai/src/ai.rs b/crates/ai/src/ai.rs index 728648a293..75aabe561c 100644 --- a/crates/ai/src/ai.rs +++ b/crates/ai/src/ai.rs @@ -34,7 +34,7 @@ struct SavedConversation { struct SavedConversationMetadata { title: String, path: PathBuf, - mtime: SystemTime, + mtime: chrono::DateTime, } impl SavedConversationMetadata { @@ -59,7 +59,7 @@ impl SavedConversationMetadata { conversations.push(Self { title: title.into_owned(), path, - mtime: metadata.mtime, + mtime: metadata.mtime.into(), }); } } diff --git a/crates/ai/src/assistant.rs b/crates/ai/src/assistant.rs index 785725ceaa..a21ab29fc0 100644 --- a/crates/ai/src/assistant.rs +++ b/crates/ai/src/assistant.rs @@ -104,9 +104,10 @@ pub enum AssistantPanelEvent { pub struct AssistantPanel { width: Option, height: Option, - active_conversation_index: usize, + active_conversation_index: Option, conversation_editors: Vec>, saved_conversations: Vec, + saved_conversations_list_state: UniformListState, zoomed: bool, has_focus: bool, api_key: Rc>>, @@ -153,9 +154,10 @@ impl AssistantPanel { }); let mut this = Self { - active_conversation_index: 0, + active_conversation_index: Default::default(), conversation_editors: Default::default(), saved_conversations, + saved_conversations_list_state: Default::default(), zoomed: false, has_focus: false, api_key: Rc::new(RefCell::new(None)), @@ -202,7 +204,7 @@ impl AssistantPanel { self.subscriptions .push(cx.subscribe(&editor, Self::handle_conversation_editor_event)); - self.active_conversation_index = self.conversation_editors.len(); + self.active_conversation_index = Some(self.conversation_editors.len()); self.conversation_editors.push(editor.clone()); cx.notify(); @@ -258,18 +260,43 @@ impl AssistantPanel { fn active_conversation_editor(&self) -> Option<&ViewHandle> { self.conversation_editors - .get(self.active_conversation_index) + .get(self.active_conversation_index?) } - fn render_hamburger_button( - &self, - style: &IconStyle, - cx: &ViewContext, - ) -> impl Element { + fn render_hamburger_button(style: &IconStyle) -> impl Element { Svg::for_style(style.icon.clone()) .contained() .with_style(style.container) } + + fn render_saved_conversation( + &mut self, + index: usize, + cx: &mut ViewContext, + ) -> impl Element { + MouseEventHandler::::new(index, cx, move |state, cx| { + let style = &theme::current(cx).assistant.saved_conversation; + let conversation = &self.saved_conversations[index]; + Flex::row() + .with_child( + Label::new( + conversation.mtime.format("%c").to_string(), + style.saved_at.text.clone(), + ) + .contained() + .with_style(style.saved_at.container), + ) + .with_child( + Label::new(conversation.title.clone(), style.title.text.clone()) + .contained() + .with_style(style.title.container), + ) + .contained() + .with_style(*style.container.style_for(state, false)) + }) + .with_cursor_style(CursorStyle::PointingHand) + .on_click(MouseButton::Left, |_, this, cx| {}) + } } fn build_api_key_editor(cx: &mut ViewContext) -> ViewHandle { @@ -318,7 +345,7 @@ impl View for AssistantPanel { Flex::column() .with_child( Flex::row() - .with_child(self.render_hamburger_button(&style.hamburger_button, cx)) + .with_child(Self::render_hamburger_button(&style.hamburger_button)) .contained() .with_style(theme.workspace.tab_bar.container) .constrained() @@ -327,7 +354,17 @@ impl View for AssistantPanel { .with_child(ChildView::new(editor, cx).flex(1., true)) .into_any() } else { - Empty::new().into_any() + UniformList::new( + self.saved_conversations_list_state.clone(), + self.saved_conversations.len(), + cx, + |this, range, items, cx| { + for ix in range { + items.push(this.render_saved_conversation(ix, cx).into_any()); + } + }, + ) + .into_any() } } diff --git a/crates/theme/src/theme.rs b/crates/theme/src/theme.rs index 38e66cbb4c..2ab12f1d96 100644 --- a/crates/theme/src/theme.rs +++ b/crates/theme/src/theme.rs @@ -1007,6 +1007,15 @@ pub struct AssistantStyle { pub error_icon: Icon, pub api_key_editor: FieldEditor, pub api_key_prompt: ContainedText, + pub saved_conversation: SavedConversation, +} + +#[derive(Clone, Deserialize, Default)] +pub struct SavedConversation { + #[serde(flatten)] + pub container: Interactive, + pub saved_at: ContainedText, + pub title: ContainedText, } #[derive(Clone, Deserialize, Default)]