Skip to content

Commit

Permalink
Merge pull request #238 from moxin-org/fix-chat-text-inputs
Browse files Browse the repository at this point in the history
Fix Chat section text inputs focus issues
  • Loading branch information
jmbejar authored Aug 29, 2024
2 parents f61c2d7 + f81a643 commit 3ec84a3
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
11 changes: 10 additions & 1 deletion src/chat/chat_history.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::chat_history_card::ChatHistoryCardWidgetRefExt;
use super::chat_history_card::{ChatHistoryCardAction, ChatHistoryCardWidgetRefExt};
use crate::data::store::Store;
use makepad_widgets::*;

Expand Down Expand Up @@ -122,6 +122,15 @@ 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.
let widget_uid = self.widget_uid();
cx.widget_action(
widget_uid,
&scope.path,
ChatHistoryCardAction::ChatSelected,
);

self.redraw(cx);
}

Expand Down
4 changes: 2 additions & 2 deletions src/chat/chat_history_card.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ impl WidgetMatchEvent for ChatHistoryCard {
cx.widget_action(
widget_uid,
&scope.path,
ChatHistoryCardAction::ChatSelected(self.chat_id),
ChatHistoryCardAction::ChatSelected,
);
let store = scope.data.get_mut::<Store>().unwrap();
store.chats.set_current_chat(self.chat_id);
Expand Down Expand Up @@ -475,7 +475,7 @@ impl ChatHistoryCardRef {
#[derive(Clone, DefaultNone, Eq, Hash, PartialEq, Debug)]
pub enum ChatHistoryCardAction {
None,
ChatSelected(ChatID),
ChatSelected,
ActivateTitleEdition(ChatID),
MenuClosed(ChatID),
DeleteChatOptionSelected(ChatID),
Expand Down
22 changes: 19 additions & 3 deletions src/chat/chat_panel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,9 @@ pub struct ChatPanel {

#[rust]
portal_list_end_reached: bool,

#[rust(false)]
focus_on_prompt_input_pending: bool,
}

impl Widget for ChatPanel {
Expand Down Expand Up @@ -486,6 +489,17 @@ impl Widget for ChatPanel {
fn draw_walk(&mut self, cx: &mut Cx2d, scope: &mut Scope, walk: Walk) -> DrawStep {
self.update_view(cx, scope);

// We need to make sure we're drawing this widget in order to focus on the prompt input
// Otherwise, when navigating from another section this command would happen before the widget is drawn
// (not having any effect).
if self.focus_on_prompt_input_pending {
self.focus_on_prompt_input_pending = false;
let prompt_input = self.text_input(id!(main_prompt_input.prompt));
prompt_input.set_text("");
prompt_input.set_cursor(0, 0);
prompt_input.set_key_focus(cx);
}

while let Some(view_item) = self.view.draw_walk(cx, scope, walk).step() {
if let Some(mut list) = view_item.as_portal_list().borrow_mut() {
self.draw_messages(cx, scope, &mut list);
Expand All @@ -505,8 +519,9 @@ impl WidgetMatchEvent for ChatPanel {
.iter()
.filter_map(|action| action.as_widget_action())
{
if let ChatHistoryCardAction::ChatSelected(_) = action.cast() {
if let ChatHistoryCardAction::ChatSelected = action.cast() {
self.reset_scroll_messages(&store);
self.focus_on_prompt_input_pending = true;
self.redraw(cx);
}

Expand All @@ -517,13 +532,16 @@ impl WidgetMatchEvent for ChatPanel {
chat.borrow_mut().last_used_file_id = Some(downloaded_file.file.id.clone());
chat.borrow().save();
}

self.focus_on_prompt_input_pending = true;
self.redraw(cx)
}

match action.cast() {
ChatAction::Start(file_id) => {
if let Some(file) = store.downloads.get_file(&file_id) {
store.chats.create_empty_chat_and_load_file(file);
self.focus_on_prompt_input_pending = true;
}
}
_ => {}
Expand Down Expand Up @@ -658,8 +676,6 @@ impl ChatPanel {
) {
let prompt_input = self.text_input(id!(main_prompt_input.prompt));

prompt_input.set_key_focus(cx);

let enabled = match mode {
PromptInputMode::Enabled => !prompt_input.text().is_empty(),
PromptInputMode::Disabled => false,
Expand Down

0 comments on commit 3ec84a3

Please sign in to comment.