Skip to content

Commit

Permalink
fix: fix event freeze while animations playing
Browse files Browse the repository at this point in the history
  • Loading branch information
Beastwick18 committed Jun 6, 2024
1 parent 53a86ce commit a69320c
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 27 deletions.
51 changes: 33 additions & 18 deletions src/app.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
use std::{error::Error, fmt::Display, sync::Arc, time::Instant};
use std::{
error::Error,
fmt::Display,
sync::Arc,
time::{Duration, Instant},
};

use crossterm::event::{Event, KeyCode, KeyEvent, KeyEventKind};
use indexmap::IndexMap;
Expand Down Expand Up @@ -51,6 +56,9 @@ use crate::util::term;

pub static APP_NAME: &str = "nyaa";

// To ensure that other events will get a chance to be received
static ANIMATE_SLEEP_MILLIS: u64 = 5;

#[derive(PartialEq, Clone)]
pub enum LoadType {
Sourcing,
Expand Down Expand Up @@ -228,6 +236,9 @@ impl App {
) -> Result<(), Box<dyn Error>> {
let ctx = &mut Context::default();

let timer = tokio::time::sleep(Duration::from_millis(ANIMATE_SLEEP_MILLIS));
tokio::pin!(timer);

let (tx_res, mut rx_res) =
mpsc::channel::<Result<SourceResults, Box<dyn Error + Send + Sync>>>(32);
let (tx_evt, mut rx_evt) = mpsc::channel::<Event>(100);
Expand Down Expand Up @@ -365,15 +376,29 @@ impl App {
loop {
tokio::select! {
biased;
Some(evt) = rx_evt.recv() => {
Some(Event::Key(key)) = rx_evt.recv() => {
let evt = Event::Key(key);
#[cfg(unix)]
self.on::<B, TEST>(&evt, ctx, terminal);
#[cfg(not(unix))]
self.on::<B, TEST>(&evt, ctx);

last_time = None;
break;
},
() = &mut timer, if self.widgets.notification.is_animating() => {
timer.as_mut().reset(tokio::time::Instant::now() + Duration::from_millis(ANIMATE_SLEEP_MILLIS));
if let Ok(size) = terminal.size() {
let now = Instant::now();
ctx.deltatime = last_time.map(|l| (now - l).as_secs_f64()).unwrap_or(0.0);
last_time = Some(now);

if self.widgets.notification.update(ctx.deltatime, size) {
break;
}
} else {
break;
}
},
Some(rt) = rx_res.recv() => {
match rt {
Ok(SourceResults::Results(rt)) => {
Expand All @@ -394,7 +419,6 @@ impl App {
}
ctx.load_type = None;
last_load_abort = None;
last_time = None;
break;
},
Some(dl) = rx_dl.recv() => {
Expand All @@ -413,24 +437,15 @@ impl App {
}
break;
}
_ = async{}, if self.widgets.notification.is_animating() => {
if let Ok(size) = terminal.size() {
let now = Instant::now();
ctx.deltatime = (now - last_time.unwrap_or(now)).as_secs_f64();
last_time = Some(now);

if self.widgets.notification.update(ctx.deltatime, size) {
break;
}
} else {
break;
}
},
// _ = async{}, if matches!(terminal.size().map(|s| self.widgets.notification.update(last_time.map(|l| (Instant::now() - l).as_secs_f64()).unwrap_or(0.), s)), Ok(true)) => {
else => {
break;
return Err("All channels closed".into());
}
};
}
if !self.widgets.notification.is_animating() {
last_time = None;
}
}
Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion src/widget/batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ impl super::Widget for BatchWidget {
area.height as usize,
3,
num_items,
1,
ctx.config.scroll_padding,
self.table.state.offset_mut(),
);

Expand Down
4 changes: 2 additions & 2 deletions src/widget/notifications.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,10 @@ impl NotificationWidget {
}

pub fn update(&mut self, deltatime: f64, area: Rect) -> bool {
self.notifs.retain(|n| !n.is_done());
self.notifs.iter_mut().fold(false, |acc, x| {
let res = x.update(deltatime, area);
x.is_done() || res || acc
res || acc
})
}
}
Expand All @@ -118,7 +119,6 @@ impl Widget for NotificationWidget {
}
})
}
self.notifs.retain(|n| !n.is_done());

for n in self.notifs.iter_mut() {
n.draw(f, ctx, area);
Expand Down
14 changes: 8 additions & 6 deletions src/widget/results.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use super::{border_block, centered_rect, Corner, VirtualStatefulTable};
pub struct ResultsWidget {
pub table: VirtualStatefulTable,
control_space: bool,
draw_count: u64,
}

impl ResultsWidget {
Expand Down Expand Up @@ -57,6 +58,7 @@ impl Default for ResultsWidget {
ResultsWidget {
table: VirtualStatefulTable::new(),
control_space: false,
draw_count: 0,
}
}
}
Expand Down Expand Up @@ -172,12 +174,12 @@ impl super::Widget for ResultsWidget {
}
}

// if let Some((bl, area)) =
// TitlePosition::BottomLeft.try_widget(format!("{} draws", self.draw_count), area, false)
// {
// f.render_widget(bl, area);
// self.draw_count += 1;
// }
if let Some((bl, area)) =
Corner::BottomLeft.try_title(format!("{} draws", self.draw_count), area, false)
{
f.render_widget(bl, area);
self.draw_count += 1;
}
}

fn handle_event(&mut self, ctx: &mut Context, e: &Event) {
Expand Down

0 comments on commit a69320c

Please sign in to comment.