Skip to content

Commit

Permalink
implement chat autoscroll
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaszKielar committed Sep 10, 2024
1 parent d01c1f7 commit 931c4bc
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
22 changes: 19 additions & 3 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ pub struct App {
pub conversations: Conversations,
pub prompt: Prompt,
focus: AppFocus,
event_tx: UnboundedSender<Event>,
inference_tx: Sender<Message>,
running: bool,
sqlite: SqlitePool,
Expand All @@ -65,6 +66,7 @@ impl App {
conversations: Conversations::new(sqlite.clone()),
prompt: Default::default(),
focus: Default::default(),
event_tx: event_tx.clone(),
inference_tx,
running: true,
sqlite: sqlite.clone(),
Expand Down Expand Up @@ -132,12 +134,14 @@ impl App {
}
KeyCode::Down => match self.current_focus() {
AppFocus::Conversation => {
// 1. get index of conversation
// 2. get messages for conversation
// 3. mutate state of app by assigning messages to proper attr
self.conversations.down();
if let Some(conversation) = self.conversations.currently_selected() {
self.chat.load_messages(conversation.id).await?;
// I could've call self.chat.scroll_to_bottom, but at the time of reseting chat
// I had lost all information about scrollbar
// I'll get it next time my UI recalculates scrollbar's params and updates self.chat state
// We know that event we send below will happen after that, therefore it's safe to do it
self.event_tx.send(Event::ChatBottomScroll)?;
}
}
AppFocus::Messages => self.chat.scroll_down(),
Expand All @@ -150,6 +154,11 @@ impl App {
self.conversations.up();
if let Some(conversation) = self.conversations.currently_selected() {
self.chat.load_messages(conversation.id).await?;
// I could've call self.chat.scroll_to_bottom, but at the time of reseting chat
// I had lost all information about scrollbar
// I'll get it next time my UI recalculates scrollbar's params and updates self.chat state
// We know that event we send below will happen after that, therefore it's safe to do it
self.event_tx.send(Event::ChatBottomScroll)?;
}
}
AppFocus::Messages => self.chat.scroll_up(),
Expand Down Expand Up @@ -201,6 +210,12 @@ impl App {
Ok(())
}

async fn handle_chat_bottom_scroll_event(&mut self) -> AppResult<()> {
self.chat.scroll_to_bottom();

Ok(())
}

pub async fn handle_events(&mut self, event: Event) -> AppResult<()> {
match event {
Event::TerminalTick => Ok(()),
Expand All @@ -211,6 +226,7 @@ impl App {
Event::Inference(message, InferenceType::NonStreaming) => {
self.handle_inference_event(message).await
}
Event::ChatBottomScroll => self.handle_chat_bottom_scroll_event().await,
}
}
}
1 change: 1 addition & 0 deletions src/chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ impl Chat {
pub fn reset(&mut self) {
self.messages = vec![];
self.vertical_scroll = 0;
self.vertical_scrollbar_content_length = 0;
self.vertical_scrollbar_state.first();
}

Expand Down
1 change: 1 addition & 0 deletions src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub enum Event {
TerminalTick,
Key(KeyEvent),
Inference(Message, InferenceType),
ChatBottomScroll,
}

#[allow(dead_code)]
Expand Down

0 comments on commit 931c4bc

Please sign in to comment.