feat(example): debounce, top right corner performance info and update on resize
This commit is contained in:
+67
-23
@@ -1,6 +1,10 @@
|
|||||||
|
use std::time::{Duration, Instant};
|
||||||
|
|
||||||
use crossterm::event::{Event, KeyCode, MouseEventKind};
|
use crossterm::event::{Event, KeyCode, MouseEventKind};
|
||||||
use ratatui::backend::{Backend, CrosstermBackend};
|
use ratatui::backend::{Backend, CrosstermBackend};
|
||||||
|
use ratatui::layout::Rect;
|
||||||
use ratatui::style::{Color, Modifier, Style};
|
use ratatui::style::{Color, Modifier, Style};
|
||||||
|
use ratatui::text::Span;
|
||||||
use ratatui::widgets::{Block, Scrollbar, ScrollbarOrientation};
|
use ratatui::widgets::{Block, Scrollbar, ScrollbarOrientation};
|
||||||
use ratatui::{Frame, Terminal};
|
use ratatui::{Frame, Terminal};
|
||||||
use tui_tree_widget::{Tree, TreeItem, TreeState};
|
use tui_tree_widget::{Tree, TreeItem, TreeState};
|
||||||
@@ -133,32 +137,72 @@ fn main() -> std::io::Result<()> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn run_app<B: Backend>(terminal: &mut Terminal<B>, mut app: App) -> std::io::Result<()> {
|
fn run_app<B: Backend>(terminal: &mut Terminal<B>, mut app: App) -> std::io::Result<()> {
|
||||||
|
const DEBOUNCE: Duration = Duration::from_millis(20); // 50 FPS
|
||||||
|
|
||||||
|
let before = Instant::now();
|
||||||
terminal.draw(|frame| app.draw(frame))?;
|
terminal.draw(|frame| app.draw(frame))?;
|
||||||
|
let mut last_render_took = before.elapsed();
|
||||||
|
|
||||||
|
let mut debounce: Option<Instant> = None;
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
let update = match crossterm::event::read()? {
|
let timeout = debounce.map_or(DEBOUNCE, |start| DEBOUNCE.saturating_sub(start.elapsed()));
|
||||||
Event::Key(key) => match key.code {
|
if crossterm::event::poll(timeout)? {
|
||||||
KeyCode::Char('q') => return Ok(()),
|
let update = match crossterm::event::read()? {
|
||||||
KeyCode::Char('\n' | ' ') => app.state.toggle_selected(),
|
Event::Key(key) => match key.code {
|
||||||
KeyCode::Left => app.state.key_left(),
|
KeyCode::Char('q') => return Ok(()),
|
||||||
KeyCode::Right => app.state.key_right(),
|
KeyCode::Char('\n' | ' ') => app.state.toggle_selected(),
|
||||||
KeyCode::Down => app.state.key_down(),
|
KeyCode::Left => app.state.key_left(),
|
||||||
KeyCode::Up => app.state.key_up(),
|
KeyCode::Right => app.state.key_right(),
|
||||||
KeyCode::Esc => app.state.select(Vec::new()),
|
KeyCode::Down => app.state.key_down(),
|
||||||
KeyCode::Home => app.state.select_first(),
|
KeyCode::Up => app.state.key_up(),
|
||||||
KeyCode::End => app.state.select_last(),
|
KeyCode::Esc => app.state.select(Vec::new()),
|
||||||
KeyCode::PageDown => app.state.scroll_down(3),
|
KeyCode::Home => app.state.select_first(),
|
||||||
KeyCode::PageUp => app.state.scroll_up(3),
|
KeyCode::End => app.state.select_last(),
|
||||||
|
KeyCode::PageDown => app.state.scroll_down(3),
|
||||||
|
KeyCode::PageUp => app.state.scroll_up(3),
|
||||||
|
_ => false,
|
||||||
|
},
|
||||||
|
Event::Mouse(mouse) => match mouse.kind {
|
||||||
|
MouseEventKind::ScrollDown => app.state.scroll_down(1),
|
||||||
|
MouseEventKind::ScrollUp => app.state.scroll_up(1),
|
||||||
|
_ => false,
|
||||||
|
},
|
||||||
|
Event::Resize(_, _) => true,
|
||||||
_ => false,
|
_ => false,
|
||||||
},
|
};
|
||||||
Event::Mouse(mouse) => match mouse.kind {
|
if update {
|
||||||
MouseEventKind::ScrollDown => app.state.scroll_down(1),
|
debounce.get_or_insert_with(Instant::now);
|
||||||
MouseEventKind::ScrollUp => app.state.scroll_up(1),
|
}
|
||||||
_ => false,
|
}
|
||||||
},
|
if debounce.is_some_and(|debounce| debounce.elapsed() > DEBOUNCE) {
|
||||||
_ => false,
|
let before = Instant::now();
|
||||||
};
|
terminal.draw(|frame| {
|
||||||
if update {
|
app.draw(frame);
|
||||||
terminal.draw(|frame| app.draw(frame))?;
|
|
||||||
|
// Performance info in top right corner
|
||||||
|
{
|
||||||
|
let text = format!(
|
||||||
|
" {} {last_render_took:?} {:.1} FPS",
|
||||||
|
frame.count(),
|
||||||
|
1.0 / last_render_took.as_secs_f64()
|
||||||
|
);
|
||||||
|
#[allow(clippy::cast_possible_truncation)]
|
||||||
|
let area = Rect {
|
||||||
|
y: 0,
|
||||||
|
height: 1,
|
||||||
|
x: frame.size().width.saturating_sub(text.len() as u16),
|
||||||
|
width: text.len() as u16,
|
||||||
|
};
|
||||||
|
frame.render_widget(
|
||||||
|
Span::styled(text, Style::new().fg(Color::Black).bg(Color::Gray)),
|
||||||
|
area,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
})?;
|
||||||
|
last_render_took = before.elapsed();
|
||||||
|
|
||||||
|
debounce = None;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user