use std::sync::Arc;
use dioxus_core::AttributeValue;
use dioxus_hooks::{use_memo, use_reactive, Dependency};
use dioxus_signals::{Memo, Readable};
use freya_node_state::{CanvasReference, CanvasRunner, CustomAttributeValues};
#[derive(PartialEq, Clone)]
pub struct UseCanvas {
runner: Memo<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.read().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(use_reactive(dependencies, move |dependencies| {
UseCanvasRunner(Arc::new(renderer_cb(dependencies)))
}));
UseCanvas { runner }
}