Move cursor in room list

This commit is contained in:
Joscha 2022-06-27 09:30:41 +02:00
parent e9e3b6e21c
commit 32e8616ed7
2 changed files with 35 additions and 5 deletions

View file

@ -192,7 +192,7 @@ impl Ui {
}
match self.mode {
Mode::Main => self.rooms.handle_key_event(terminal, event).await,
Mode::Main => self.rooms.handle_key_event(terminal, size, event).await,
Mode::Log => self.log_chat.handle_navigation(terminal, size, event).await,
}

View file

@ -1,7 +1,7 @@
use std::collections::{HashMap, HashSet};
use crossterm::event::KeyEvent;
use toss::frame::{Frame, Pos};
use crossterm::event::{KeyCode, KeyEvent};
use toss::frame::{Frame, Pos, Size};
use toss::terminal::Terminal;
use crate::chat::Chat;
@ -138,7 +138,37 @@ impl Rooms {
}
}
pub async fn handle_key_event(&mut self, terminal: &mut Terminal, event: KeyEvent) {
// TODO
pub async fn handle_key_event(&mut self, terminal: &mut Terminal, size: Size, event: KeyEvent) {
if let Some(focus) = &self.focus {
if event.code == KeyCode::Esc {
self.focus = None;
}
} else {
let rooms = self.rooms().await;
self.make_consistent(&rooms, size.height.into());
match event.code {
KeyCode::Enter => {
if let Some(cursor) = self.cursor {
if let Some(room) = rooms.get(cursor.index) {
self.focus = Some(room.clone());
}
}
}
KeyCode::Char('j') => {
if let Some(cursor) = &mut self.cursor {
cursor.index = cursor.index.saturating_add(1);
cursor.line += 1;
}
}
KeyCode::Char('k') => {
if let Some(cursor) = &mut self.cursor {
cursor.index = cursor.index.saturating_sub(1);
cursor.line -= 1;
}
}
_ => {}
}
}
}
}