alpha2_extensions/
progress.rs1use std::cell::RefCell;
6
7thread_local! {
8 static WATCH: RefCell<Option<Box<dyn Fn()>>> = const { RefCell::new(None) };
9}
10
11pub fn watch_progress(progressed: impl Fn() + 'static) -> ProgressWatch {
13 let previous = WATCH.with(|watch| watch.replace(Some(Box::new(progressed))));
14 ProgressWatch {
15 previous,
16 thread: std::marker::PhantomData,
17 }
18}
19
20pub struct ProgressWatch {
22 previous: Option<Box<dyn Fn()>>,
23 thread: std::marker::PhantomData<*const ()>,
25}
26
27impl Drop for ProgressWatch {
28 fn drop(&mut self) {
29 let previous = self.previous.take();
30 WATCH.with(|watch| {
31 watch.replace(previous);
32 });
33 }
34}
35
36#[cfg(feature = "animated-image")]
37pub(crate) fn progressed() {
38 WATCH.with(|watch| {
39 if let Some(progressed) = watch.borrow().as_ref() {
40 progressed();
41 }
42 });
43}