use alloc::sync::Arc;
use crate::{ColorImage, mutex::Mutex};
type ScreenshotCallbackFn = dyn FnOnce(Arc<ColorImage>) + Send + 'static;
#[derive(Clone)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct ScreenshotCallback {
#[cfg_attr(feature = "serde", serde(skip))]
callback: Arc<Mutex<Option<Box<ScreenshotCallbackFn>>>>,
}
impl ScreenshotCallback {
pub fn new(callback: impl FnOnce(Arc<ColorImage>) + Send + 'static) -> Self {
Self {
callback: Arc::new(Mutex::new(Some(Box::new(callback)))),
}
}
pub fn complete(self, image: Arc<ColorImage>) {
let Some(callback) = self.callback.lock().take() else {
return;
};
callback(image);
}
}
impl core::fmt::Debug for ScreenshotCallback {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("ScreenshotCallback")
.field("called", &self.callback.lock().is_none())
.finish()
}
}
impl PartialEq for ScreenshotCallback {
fn eq(&self, other: &Self) -> bool {
Arc::ptr_eq(&self.callback, &other.callback)
}
}
impl Eq for ScreenshotCallback {}