Resize rooms pane via dragging
This commit is contained in:
parent
a50ecaee68
commit
d68ec217a8
1 changed files with 33 additions and 12 deletions
|
|
@ -1,10 +1,11 @@
|
|||
use std::io::Stdout;
|
||||
|
||||
use crossterm::event::{Event, EventStream, KeyCode, KeyEvent, MouseEvent};
|
||||
use crossterm::event::{Event, EventStream, KeyCode, KeyEvent, MouseEvent, MouseEventKind};
|
||||
use futures::StreamExt;
|
||||
use tokio::sync::mpsc::{self, UnboundedReceiver, UnboundedSender};
|
||||
use tui::backend::CrosstermBackend;
|
||||
use tui::widgets::Paragraph;
|
||||
use tui::layout::{Constraint, Direction, Layout};
|
||||
use tui::widgets::{Block, Borders, Paragraph};
|
||||
use tui::{Frame, Terminal};
|
||||
|
||||
pub type Backend = CrosstermBackend<Stdout>;
|
||||
|
|
@ -22,7 +23,8 @@ enum EventHandleResult {
|
|||
|
||||
pub struct Ui {
|
||||
event_tx: UnboundedSender<UiEvent>,
|
||||
rooms_width: i32,
|
||||
rooms_width: u16,
|
||||
rooms_dragging: bool,
|
||||
log: Vec<String>,
|
||||
}
|
||||
|
||||
|
|
@ -31,6 +33,7 @@ impl Ui {
|
|||
Self {
|
||||
event_tx,
|
||||
rooms_width: 24,
|
||||
rooms_dragging: false,
|
||||
log: vec!["Hello world!".to_string()],
|
||||
}
|
||||
}
|
||||
|
|
@ -117,26 +120,44 @@ impl Ui {
|
|||
}
|
||||
|
||||
async fn handle_mouse_event(&mut self, event: MouseEvent) -> anyhow::Result<EventHandleResult> {
|
||||
Ok(match event.kind {
|
||||
// MouseEventKind::Down(_) => todo!(),
|
||||
// MouseEventKind::Up(_) => todo!(),
|
||||
// MouseEventKind::Drag(_) => todo!(),
|
||||
match event.kind {
|
||||
MouseEventKind::Down(_) if event.column == self.rooms_width => {
|
||||
self.rooms_dragging = true;
|
||||
}
|
||||
MouseEventKind::Up(_) => {
|
||||
self.rooms_dragging = false;
|
||||
}
|
||||
MouseEventKind::Drag(_) if self.rooms_dragging => {
|
||||
self.rooms_width = event.column;
|
||||
}
|
||||
// MouseEventKind::Moved => todo!(),
|
||||
// MouseEventKind::ScrollDown => todo!(),
|
||||
// MouseEventKind::ScrollUp => todo!(),
|
||||
_ => EventHandleResult::Continue,
|
||||
})
|
||||
_ => {}
|
||||
}
|
||||
Ok(EventHandleResult::Continue)
|
||||
}
|
||||
|
||||
async fn render(&mut self, frame: &mut Frame<'_, Backend>) -> anyhow::Result<()> {
|
||||
let scroll = if self.log.len() as u16 > frame.size().height {
|
||||
self.log.len() as u16 - frame.size().height
|
||||
let outer = Layout::default()
|
||||
.direction(Direction::Horizontal)
|
||||
.constraints([
|
||||
Constraint::Length(self.rooms_width),
|
||||
Constraint::Length(1),
|
||||
Constraint::Min(0),
|
||||
])
|
||||
.split(frame.size());
|
||||
|
||||
frame.render_widget(Block::default().borders(Borders::RIGHT), outer[1]);
|
||||
|
||||
let scroll = if self.log.len() as u16 > outer[2].height {
|
||||
self.log.len() as u16 - outer[2].height
|
||||
} else {
|
||||
0
|
||||
};
|
||||
frame.render_widget(
|
||||
Paragraph::new(self.log.join("\n")).scroll((scroll, 0)),
|
||||
frame.size(),
|
||||
outer[2],
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue