use std::cell::RefCell;
thread_local! {
static WATCH: RefCell<Option<Box<dyn Fn()>>> = const { RefCell::new(None) };
}
pub fn watch_progress(progressed: impl Fn() + 'static) -> ProgressWatch {
let previous = WATCH.with(|watch| watch.replace(Some(Box::new(progressed))));
ProgressWatch {
previous,
thread: std::marker::PhantomData,
}
}
pub struct ProgressWatch {
previous: Option<Box<dyn Fn()>>,
thread: std::marker::PhantomData<*const ()>,
}
impl Drop for ProgressWatch {
fn drop(&mut self) {
let previous = self.previous.take();
WATCH.with(|watch| {
watch.replace(previous);
});
}
}
#[cfg(feature = "animated-image")]
pub(crate) fn progressed() {
WATCH.with(|watch| {
if let Some(progressed) = watch.borrow().as_ref() {
progressed();
}
});
}