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