mirror of
https://github.com/apognu/tuigreet.git
synced 2024-09-11 07:25:29 +03:00
Trying to only render when necessary to limit CPU usage (#113).
This commit is contained in:
parent
04d7a655b8
commit
158a98e85d
@ -4,13 +4,11 @@ use crossterm::event::{Event as TermEvent, EventStream, KeyEvent};
|
||||
use futures::{future::FutureExt, StreamExt};
|
||||
use tokio::sync::mpsc;
|
||||
|
||||
const TICK_RATE: u64 = 150;
|
||||
const FRAME_RATE: f64 = 60.0;
|
||||
const FRAME_RATE: f64 = 2.0;
|
||||
|
||||
pub enum Event {
|
||||
Key(KeyEvent),
|
||||
Render,
|
||||
Tick,
|
||||
}
|
||||
|
||||
pub struct Events {
|
||||
@ -24,10 +22,8 @@ impl Events {
|
||||
tokio::task::spawn(async move {
|
||||
let mut stream = EventStream::new();
|
||||
let mut render_interval = tokio::time::interval(Duration::from_secs_f64(1.0 / FRAME_RATE));
|
||||
let mut tick_interval = tokio::time::interval(Duration::from_millis(TICK_RATE));
|
||||
|
||||
loop {
|
||||
let tick = tick_interval.tick();
|
||||
let render = render_interval.tick();
|
||||
let event = stream.next().fuse();
|
||||
|
||||
@ -35,11 +31,11 @@ impl Events {
|
||||
event = event => {
|
||||
if let Some(Ok(TermEvent::Key(event))) = event {
|
||||
let _ = tx.send(Event::Key(event)).await;
|
||||
let _ = tx.send(Event::Render).await;
|
||||
}
|
||||
}
|
||||
|
||||
_ = render => { let _ = tx.send(Event::Render).await; },
|
||||
_ = tick => { let _ = tx.send(Event::Tick).await; },
|
||||
}
|
||||
}
|
||||
});
|
||||
|
@ -96,7 +96,7 @@ async fn run() -> Result<(), Box<dyn Error>> {
|
||||
match events.next().await {
|
||||
Some(Event::Render) => ui::draw(greeter.clone(), &mut terminal).await?,
|
||||
Some(Event::Key(key)) => keyboard::handle(greeter.clone(), key, ipc.clone()).await?,
|
||||
Some(Event::Tick) | None => {}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user