use std::sync::Arc;
use dioxus_core::AttributeValue;
use dioxus_signals::{use_memo_with_dependencies, Dependency, Readable};
use freya_node_state::{CanvasReference, CanvasRunner, CustomAttributeValues};
#[derive(PartialEq, Clone)]
pub struct UseCanvas {
runner: UseCanvasRunner,
}
#[derive(Clone)]
pub struct UseCanvasRunner(pub Arc<Box<CanvasRunner>>);
impl PartialEq for UseCanvasRunner {
fn eq(&self, other: &Self) -> bool {
Arc::ptr_eq(&self.0, &other.0)
}
}
impl UseCanvas {
pub fn attribute(&self) -> AttributeValue {
AttributeValue::any_value(CustomAttributeValues::Canvas(CanvasReference {
runner: self.runner.0.clone(),
}))
}
}
pub fn use_canvas<D: Dependency>(
dependencies: D,
renderer_cb: impl Fn(D::Out) -> Box<CanvasRunner> + 'static,
) -> UseCanvas
where
D::Out: 'static,
{
let runner = use_memo_with_dependencies(dependencies, move |dependencies| {
UseCanvasRunner(Arc::new(renderer_cb(dependencies)))
});
UseCanvas {
runner: runner.peek().clone(),
}
}