Skip to content

Commit

Permalink
Trying to only render when necessary to limit CPU usage (#113).
Browse files Browse the repository at this point in the history
  • Loading branch information
apognu committed Nov 4, 2023
1 parent 04d7a65 commit 158a98e
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 7 deletions.
8 changes: 2 additions & 6 deletions src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -24,22 +22,20 @@ 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();

tokio::select! {
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; },
}
}
});
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {}
_ => {}
}
}
}
Expand Down

0 comments on commit 158a98e

Please sign in to comment.