Skip to content

Commit

Permalink
implement autoscroll
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaszKielar committed Sep 9, 2024
1 parent 23b4231 commit f608ad0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 16 deletions.
16 changes: 8 additions & 8 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ impl App {
conversation.id,
)
.await?;
self.chat.push(user_message.clone());
self.chat.push_message(user_message.clone());
self.inference_tx.send(user_message).await?;
self.prompt.clear();
}
Expand All @@ -140,7 +140,7 @@ impl App {
self.chat.load_messages(conversation.id).await?;
}
}
AppFocus::Messages => self.chat.down(),
AppFocus::Messages => self.chat.scroll_down(),
AppFocus::Prompt => {
self.prompt.handle_input(key_event);
}
Expand All @@ -152,7 +152,7 @@ impl App {
self.chat.load_messages(conversation.id).await?;
}
}
AppFocus::Messages => self.chat.up(),
AppFocus::Messages => self.chat.scroll_up(),
AppFocus::Prompt => {
self.prompt.handle_input(key_event);
}
Expand All @@ -175,23 +175,23 @@ impl App {
}

async fn handle_inference_event(&mut self, message: Message) -> AppResult<()> {
self.chat.push(message);
self.chat.push_message(message);

Ok(())
}

async fn handle_inference_stream_event(&mut self, message: Message) -> AppResult<()> {
if let Some(last_message) = self.chat.last() {
if let Some(last_message) = self.chat.get_last_message() {
if let Some(conversation) = self.conversations.currently_selected() {
if conversation.id.eq(&message.conversation_id) {
match last_message.role {
Role::Assistant => {
self.chat.pop();
self.chat.push(message);
self.chat.pop_message();
self.chat.push_message(message);
}
Role::System => {}
Role::User => {
self.chat.push(message);
self.chat.push_message(message);
}
}
}
Expand Down
17 changes: 9 additions & 8 deletions src/chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,29 +25,29 @@ impl Chat {
pub fn reset(&mut self) {
self.messages = vec![];
self.vertical_scroll = 0;
self.vertical_scrollbar_state =
self.vertical_scrollbar_state.position(self.vertical_scroll);
self.vertical_scrollbar_state.first();
}

pub fn push(&mut self, message: Message) {
pub fn push_message(&mut self, message: Message) {
self.messages.push(message);
self.scroll_down();
}

pub fn pop(&mut self) {
pub fn pop_message(&mut self) {
self.messages.pop();
}

pub fn last(&self) -> Option<&Message> {
pub fn get_last_message(&self) -> Option<&Message> {
self.messages.last()
}

pub fn up(&mut self) {
pub fn scroll_up(&mut self) {
self.vertical_scroll = self.vertical_scroll.saturating_sub(1);
self.vertical_scrollbar_state =
self.vertical_scrollbar_state.position(self.vertical_scroll);
}

pub fn down(&mut self) {
pub fn scroll_down(&mut self) {
self.vertical_scroll = {
let next_position = self.vertical_scroll.saturating_add(1);
if next_position > self.vertical_scrollbar_content_length {
Expand All @@ -69,7 +69,8 @@ impl Chat {
Ok(())
}

pub fn as_list_widget<F, T>(&self, f: F) -> List<'static>
#[allow(dead_code)]
fn as_list_widget<F, T>(&self, f: F) -> List<'static>
where
F: Fn(&Message) -> T,
T: Into<ListItem<'static>>,
Expand Down

0 comments on commit f608ad0

Please sign in to comment.