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