Skip to content

Commit

Permalink
Refactor chat input code in ChatPanel
Browse files Browse the repository at this point in the history
  • Loading branch information
jmbejar committed Jul 24, 2024
1 parent 0d75af9 commit 1056e1a
Showing 1 changed file with 61 additions and 97 deletions.
158 changes: 61 additions & 97 deletions src/chat/chat_panel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,15 @@ enum State {
},
}

enum PromptInputMode {
Enabled,
Disabled,
}
enum PromptInputButton {
Send,
Stop,
}

#[derive(Live, LiveHook, Widget)]
pub struct ChatPanel {
#[deref]
Expand Down Expand Up @@ -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) {
Expand All @@ -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
Expand All @@ -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) {
Expand Down

0 comments on commit 1056e1a

Please sign in to comment.