diff --git a/src/chat/chat_panel.rs b/src/chat/chat_panel.rs index 8285543e..7459b378 100644 --- a/src/chat/chat_panel.rs +++ b/src/chat/chat_panel.rs @@ -426,6 +426,15 @@ enum State { }, } +enum PromptInputMode { + Enabled, + Disabled, +} +enum PromptInputButton { + Send, + Stop, +} + #[derive(Live, LiveHook, Widget)] pub struct ChatPanel { #[deref] @@ -607,124 +616,79 @@ impl ChatPanel { is_streaming: false, .. } => { - self.enable_or_disable_prompt_buttons(cx); - self.show_prompt_send_button(cx); + self.activate_prompt_input(cx, PromptInputMode::Enabled, PromptInputButton::Send); } State::ModelSelectedWithChat { is_streaming: true, .. } => { - let prompt_input = self.text_input(id!(main_prompt_input.prompt)); - prompt_input.apply_over( - cx, - live! { - draw_text: { prompt_enabled: 0.0 } - }, - ); - self.show_prompt_input_stop_button(cx); + self.activate_prompt_input(cx, PromptInputMode::Disabled, PromptInputButton::Stop); } State::ModelLoading => { - self.show_prompt_send_button(cx); - self.disable_prompt_buttons(cx); - - let prompt_input = self.text_input(id!(main_prompt_input.prompt)); - prompt_input.apply_over( - cx, - live! { - draw_text: { prompt_enabled: 0.0 } - }, - ); + self.activate_prompt_input(cx, PromptInputMode::Disabled, PromptInputButton::Send); } _ => {} } } - fn enable_or_disable_prompt_buttons(&mut self, cx: &mut Cx) { + fn activate_prompt_input( + &mut self, + cx: &mut Cx, + mode: PromptInputMode, + button: PromptInputButton, + ) { let prompt_input = self.text_input(id!(main_prompt_input.prompt)); - let enable = if !prompt_input.text().is_empty() { - 1.0 - } else { - 0.0 - }; - prompt_input.apply_over( - cx, - live! { - draw_text: { prompt_enabled: (enable) } - }, - ); - } - - fn show_prompt_send_button(&mut self, cx: &mut Cx) { - self.button(id!(main_prompt_input.prompt_send_button)) - .set_visible(true); - self.button(id!(main_prompt_input.prompt_stop_button)) - .set_visible(false); + let enabled = match mode { + PromptInputMode::Enabled => !prompt_input.text().is_empty(), + PromptInputMode::Disabled => false, + }; - let prompt_input = self.text_input(id!(main_prompt_input.prompt)); - if !prompt_input.text().is_empty() { - self.enable_prompt_buttons(cx); + let (button_color, prompt_enabled) = if enabled { + (vec3(0.0, 0.0, 0.0), 1.0) } else { - self.disable_prompt_buttons(cx); - } - } - - fn show_prompt_input_stop_button(&mut self, cx: &mut Cx) { - self.button(id!(main_prompt_input.prompt_send_button)) - .set_visible(false); - self.button(id!(main_prompt_input.prompt_stop_button)) - .set_visible(true); - - self.enable_prompt_buttons(cx); - } - - fn enable_prompt_buttons(&mut self, cx: &mut Cx) { - let enabled_color = vec3(0.0, 0.0, 0.0); - let send_button = self.button(id!(main_prompt_input.prompt_send_button)); - send_button.set_enabled(true); - send_button.apply_over( - cx, - live! { - draw_bg: { - color: (enabled_color) - } - }, - ); + // The color code is #D0D5DD + (vec3(0.816, 0.835, 0.867), 0.0) + }; - let stop_button = self.button(id!(main_prompt_input.prompt_stop_button)); - stop_button.set_enabled(true); - stop_button.apply_over( + prompt_input.apply_over( cx, live! { - draw_bg: { - color: (enabled_color) - } + draw_text: { prompt_enabled: (prompt_enabled) } }, ); - } - fn disable_prompt_buttons(&mut self, cx: &mut Cx) { - let disabled_color = vec3(0.816, 0.835, 0.867); // #D0D5DD let send_button = self.button(id!(main_prompt_input.prompt_send_button)); - send_button.set_enabled(false); - send_button.apply_over( - cx, - live! { - draw_bg: { - color: (disabled_color) - } - }, - ); - let stop_button = self.button(id!(main_prompt_input.prompt_stop_button)); - stop_button.set_enabled(false); - stop_button.apply_over( - cx, - live! { - draw_bg: { - color: (disabled_color) - } - }, - ); + match button { + PromptInputButton::Send => { + // The send button is enabled or not based on the prompt input + send_button.set_visible(true); + send_button.set_enabled(enabled); + send_button.apply_over( + cx, + live! { + draw_bg: { + color: (button_color) + } + }, + ); + stop_button.set_visible(false); + } + PromptInputButton::Stop => { + // The stop button is always enabled, when visible + stop_button.set_visible(true); + stop_button.set_enabled(true); + stop_button.apply_over( + cx, + live! { + draw_bg: { + color: #x000 + } + }, + ); + send_button.set_visible(false); + } + } } fn scroll_messages_to_bottom(&mut self, cx: &mut Cx) { @@ -747,7 +711,7 @@ impl ChatPanel { fn handle_prompt_input_actions(&mut self, cx: &mut Cx, actions: &Actions, scope: &mut Scope) { let prompt_input = self.text_input(id!(main_prompt_input.prompt)); if let Some(_text) = prompt_input.changed(actions) { - self.update_prompt_input(cx); + self.redraw(cx); } if self @@ -773,10 +737,10 @@ impl ChatPanel { let prompt_input = self.text_input(id!(main_prompt_input.prompt)); prompt_input.set_text_and_redraw(cx, ""); prompt_input.set_cursor(0, 0); - self.update_prompt_input(cx); // Scroll to the bottom when the message is sent self.scroll_messages_to_bottom(cx); + self.redraw(cx); } fn update_view(&mut self, cx: &mut Cx2d, scope: &mut Scope) {