From 69a90d9a1bad1e8ac77e72d2c6901a0bdaf22918 Mon Sep 17 00:00:00 2001 From: Franco Profeti <7684329+noxware@users.noreply.github.com> Date: Thu, 5 Dec 2024 09:36:31 -0300 Subject: [PATCH] Fix chat switching (#322) * fix chat switching issues by reacting to the propety change directly * remove not longer necessary action reaction * remove ChatSelected action as not longer necessary * restore a suspecious line just in case --- src/chat/chat_history.rs | 6 +---- src/chat/chat_history_card.rs | 2 -- src/chat/chat_panel.rs | 42 ++++++++++++++++++++--------------- 3 files changed, 25 insertions(+), 25 deletions(-) diff --git a/src/chat/chat_history.rs b/src/chat/chat_history.rs index fc6a5670..c4ceb10c 100644 --- a/src/chat/chat_history.rs +++ b/src/chat/chat_history.rs @@ -1,4 +1,4 @@ -use super::chat_history_card::{ChatHistoryCardAction, ChatHistoryCardWidgetRefExt}; +use super::chat_history_card::ChatHistoryCardWidgetRefExt; use crate::chat::entity_button::EntityButtonWidgetRefExt; use crate::data::chats::chat::ChatID; use crate::data::store::Store; @@ -185,10 +185,6 @@ impl WidgetMatchEvent for ChatHistory { if self.button(id!(new_chat_button)).clicked(&actions) { store.chats.create_empty_chat(); - - // Make sure text input is focused and other necessary setup happens. - cx.action(ChatHistoryCardAction::ChatSelected); - self.redraw(cx); } } diff --git a/src/chat/chat_history_card.rs b/src/chat/chat_history_card.rs index 8924e63b..eebbdc85 100644 --- a/src/chat/chat_history_card.rs +++ b/src/chat/chat_history_card.rs @@ -413,7 +413,6 @@ impl WidgetMatchEvent for ChatHistoryCard { if let Some(fe) = self.view(id!(content)).finger_down(actions) { if fe.tap_count == 1 { - cx.action(ChatHistoryCardAction::ChatSelected); let store = scope.data.get_mut::().unwrap(); store.chats.set_current_chat(self.chat_id); self.redraw(cx); @@ -578,7 +577,6 @@ impl ChatHistoryCardRef { #[derive(Clone, DefaultNone, Eq, Hash, PartialEq, Debug)] pub enum ChatHistoryCardAction { None, - ChatSelected, ActivateTitleEdition(ChatID), MenuClosed(ChatID), DeleteChatOptionSelected(ChatID), diff --git a/src/chat/chat_panel.rs b/src/chat/chat_panel.rs index 0dc54ab0..e49f93ad 100644 --- a/src/chat/chat_panel.rs +++ b/src/chat/chat_panel.rs @@ -17,8 +17,8 @@ use crate::{ }; use super::{ - chat_history_card::ChatHistoryCardAction, model_selector_list::ModelSelectorListAction, - prompt_input::PromptInputWidgetExt, shared::ChatAgentAvatarWidgetRefExt, + model_selector_list::ModelSelectorListAction, prompt_input::PromptInputWidgetExt, + shared::ChatAgentAvatarWidgetRefExt, }; live_design! { @@ -394,15 +394,25 @@ pub struct ChatPanel { #[rust(false)] focus_on_prompt_input_pending: bool, + + #[rust] + current_chat_id: Option, } impl Widget for ChatPanel { fn handle_event(&mut self, cx: &mut Cx, event: &Event, scope: &mut Scope) { + let store = scope.data.get_mut::().unwrap(); + + if store.chats.get_current_chat_id() != self.current_chat_id { + self.current_chat_id = store.chats.get_current_chat_id(); + self.reset_scroll_messages(store); + self.redraw(cx); + } + self.view.handle_event(cx, event, scope); self.widget_match_event(cx, event, scope); - let store = scope.data.get_mut::().unwrap(); - self.update_state(store); + self.update_state(scope); } fn draw_walk(&mut self, cx: &mut Cx2d, scope: &mut Scope, walk: Walk) -> DrawStep { @@ -484,12 +494,6 @@ impl WidgetMatchEvent for ChatPanel { } for action in actions.iter() { - if let ChatHistoryCardAction::ChatSelected = action.cast() { - self.reset_scroll_messages(&store); - self.focus_on_prompt_input_pending = true; - self.redraw(cx); - } - if let ModelSelectorListAction::AddedOrDeletedModel = action.cast() { self.redraw(cx); } @@ -517,7 +521,7 @@ impl WidgetMatchEvent for ChatPanel { } ChatLineAction::Edit(id, updated, regenerate) => { if regenerate { - self.send_message(cx, store, updated, Some(id)); + self.send_message(cx, scope, updated, Some(id)); return; } else { store.edit_chat_message(id, updated); @@ -565,7 +569,9 @@ impl WidgetMatchEvent for ChatPanel { } impl ChatPanel { - fn update_state(&mut self, store: &mut Store) { + fn update_state(&mut self, scope: &mut Scope) { + let store = scope.data.get_mut::().unwrap(); + let chat_entity = store .chats .get_current_chat() @@ -739,20 +745,18 @@ impl ChatPanel { .button(id!(main_prompt_input.prompt_send_button)) .clicked(&actions) { - let store = scope.data.get_mut::().unwrap(); - self.send_message(cx, store, prompt_input.text(), None); + self.send_message(cx, scope, prompt_input.text(), None); } if let Some(prompt) = prompt_input.returned(actions) { - let store = scope.data.get_mut::().unwrap(); - self.send_message(cx, store, prompt, None); + self.send_message(cx, scope, prompt, None); } } fn send_message( &mut self, cx: &mut Cx, - store: &mut Store, + scope: &mut Scope, prompt: String, regenerate_from: Option, ) { @@ -762,7 +766,7 @@ impl ChatPanel { } // Let's confirm we're in an appropriate state to send a message - self.update_state(store); + self.update_state(scope); if matches!( self.state, State::ModelSelectedWithChat { @@ -772,6 +776,8 @@ impl ChatPanel { .. } | State::ModelSelectedWithEmptyChat { is_loading: false } ) { + let store = scope.data.get_mut::().unwrap(); + if let Some(entity_selected) = &self .prompt_input(id!(main_prompt_input)) .borrow()