Skip to content

Commit 1056e1a

Browse files
committed
Refactor chat input code in ChatPanel
1 parent 0d75af9 commit 1056e1a

File tree

1 file changed

+61
-97
lines changed

1 file changed

+61
-97
lines changed

src/chat/chat_panel.rs

+61-97
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,15 @@ enum State {
426426
},
427427
}
428428

429+
enum PromptInputMode {
430+
Enabled,
431+
Disabled,
432+
}
433+
enum PromptInputButton {
434+
Send,
435+
Stop,
436+
}
437+
429438
#[derive(Live, LiveHook, Widget)]
430439
pub struct ChatPanel {
431440
#[deref]
@@ -607,124 +616,79 @@ impl ChatPanel {
607616
is_streaming: false,
608617
..
609618
} => {
610-
self.enable_or_disable_prompt_buttons(cx);
611-
self.show_prompt_send_button(cx);
619+
self.activate_prompt_input(cx, PromptInputMode::Enabled, PromptInputButton::Send);
612620
}
613621
State::ModelSelectedWithChat {
614622
is_streaming: true, ..
615623
} => {
616-
let prompt_input = self.text_input(id!(main_prompt_input.prompt));
617-
prompt_input.apply_over(
618-
cx,
619-
live! {
620-
draw_text: { prompt_enabled: 0.0 }
621-
},
622-
);
623-
self.show_prompt_input_stop_button(cx);
624+
self.activate_prompt_input(cx, PromptInputMode::Disabled, PromptInputButton::Stop);
624625
}
625626
State::ModelLoading => {
626-
self.show_prompt_send_button(cx);
627-
self.disable_prompt_buttons(cx);
628-
629-
let prompt_input = self.text_input(id!(main_prompt_input.prompt));
630-
prompt_input.apply_over(
631-
cx,
632-
live! {
633-
draw_text: { prompt_enabled: 0.0 }
634-
},
635-
);
627+
self.activate_prompt_input(cx, PromptInputMode::Disabled, PromptInputButton::Send);
636628
}
637629
_ => {}
638630
}
639631
}
640632

641-
fn enable_or_disable_prompt_buttons(&mut self, cx: &mut Cx) {
633+
fn activate_prompt_input(
634+
&mut self,
635+
cx: &mut Cx,
636+
mode: PromptInputMode,
637+
button: PromptInputButton,
638+
) {
642639
let prompt_input = self.text_input(id!(main_prompt_input.prompt));
643-
let enable = if !prompt_input.text().is_empty() {
644-
1.0
645-
} else {
646-
0.0
647-
};
648640

649-
prompt_input.apply_over(
650-
cx,
651-
live! {
652-
draw_text: { prompt_enabled: (enable) }
653-
},
654-
);
655-
}
656-
657-
fn show_prompt_send_button(&mut self, cx: &mut Cx) {
658-
self.button(id!(main_prompt_input.prompt_send_button))
659-
.set_visible(true);
660-
self.button(id!(main_prompt_input.prompt_stop_button))
661-
.set_visible(false);
641+
let enabled = match mode {
642+
PromptInputMode::Enabled => !prompt_input.text().is_empty(),
643+
PromptInputMode::Disabled => false,
644+
};
662645

663-
let prompt_input = self.text_input(id!(main_prompt_input.prompt));
664-
if !prompt_input.text().is_empty() {
665-
self.enable_prompt_buttons(cx);
646+
let (button_color, prompt_enabled) = if enabled {
647+
(vec3(0.0, 0.0, 0.0), 1.0)
666648
} else {
667-
self.disable_prompt_buttons(cx);
668-
}
669-
}
670-
671-
fn show_prompt_input_stop_button(&mut self, cx: &mut Cx) {
672-
self.button(id!(main_prompt_input.prompt_send_button))
673-
.set_visible(false);
674-
self.button(id!(main_prompt_input.prompt_stop_button))
675-
.set_visible(true);
676-
677-
self.enable_prompt_buttons(cx);
678-
}
679-
680-
fn enable_prompt_buttons(&mut self, cx: &mut Cx) {
681-
let enabled_color = vec3(0.0, 0.0, 0.0);
682-
let send_button = self.button(id!(main_prompt_input.prompt_send_button));
683-
send_button.set_enabled(true);
684-
send_button.apply_over(
685-
cx,
686-
live! {
687-
draw_bg: {
688-
color: (enabled_color)
689-
}
690-
},
691-
);
649+
// The color code is #D0D5DD
650+
(vec3(0.816, 0.835, 0.867), 0.0)
651+
};
692652

693-
let stop_button = self.button(id!(main_prompt_input.prompt_stop_button));
694-
stop_button.set_enabled(true);
695-
stop_button.apply_over(
653+
prompt_input.apply_over(
696654
cx,
697655
live! {
698-
draw_bg: {
699-
color: (enabled_color)
700-
}
656+
draw_text: { prompt_enabled: (prompt_enabled) }
701657
},
702658
);
703-
}
704659

705-
fn disable_prompt_buttons(&mut self, cx: &mut Cx) {
706-
let disabled_color = vec3(0.816, 0.835, 0.867); // #D0D5DD
707660
let send_button = self.button(id!(main_prompt_input.prompt_send_button));
708-
send_button.set_enabled(false);
709-
send_button.apply_over(
710-
cx,
711-
live! {
712-
draw_bg: {
713-
color: (disabled_color)
714-
}
715-
},
716-
);
717-
718661
let stop_button = self.button(id!(main_prompt_input.prompt_stop_button));
719-
stop_button.set_enabled(false);
720-
stop_button.apply_over(
721-
cx,
722-
live! {
723-
draw_bg: {
724-
color: (disabled_color)
725-
}
726-
},
727-
);
662+
match button {
663+
PromptInputButton::Send => {
664+
// The send button is enabled or not based on the prompt input
665+
send_button.set_visible(true);
666+
send_button.set_enabled(enabled);
667+
send_button.apply_over(
668+
cx,
669+
live! {
670+
draw_bg: {
671+
color: (button_color)
672+
}
673+
},
674+
);
675+
stop_button.set_visible(false);
676+
}
677+
PromptInputButton::Stop => {
678+
// The stop button is always enabled, when visible
679+
stop_button.set_visible(true);
680+
stop_button.set_enabled(true);
681+
stop_button.apply_over(
682+
cx,
683+
live! {
684+
draw_bg: {
685+
color: #x000
686+
}
687+
},
688+
);
689+
send_button.set_visible(false);
690+
}
691+
}
728692
}
729693

730694
fn scroll_messages_to_bottom(&mut self, cx: &mut Cx) {
@@ -747,7 +711,7 @@ impl ChatPanel {
747711
fn handle_prompt_input_actions(&mut self, cx: &mut Cx, actions: &Actions, scope: &mut Scope) {
748712
let prompt_input = self.text_input(id!(main_prompt_input.prompt));
749713
if let Some(_text) = prompt_input.changed(actions) {
750-
self.update_prompt_input(cx);
714+
self.redraw(cx);
751715
}
752716

753717
if self
@@ -773,10 +737,10 @@ impl ChatPanel {
773737
let prompt_input = self.text_input(id!(main_prompt_input.prompt));
774738
prompt_input.set_text_and_redraw(cx, "");
775739
prompt_input.set_cursor(0, 0);
776-
self.update_prompt_input(cx);
777740

778741
// Scroll to the bottom when the message is sent
779742
self.scroll_messages_to_bottom(cx);
743+
self.redraw(cx);
780744
}
781745

782746
fn update_view(&mut self, cx: &mut Cx2d, scope: &mut Scope) {

0 commit comments

Comments
 (0)