hyle_dioxus/context.rs
1use std::sync::Arc;
2
3use dioxus_hooks::{try_use_context, use_context};
4
5use hyle::Blueprint;
6
7use crate::types::HyleComponents;
8
9/// Hyle configuration stored in Dioxus context.
10///
11/// Provide this at your root component:
12/// ```rust,ignore
13/// use_context_provider(|| HyleConfig { blueprint: Arc::new(my_blueprint()) });
14/// ```
15///
16/// The blueprint is wrapped in `Arc` so that cloning the context value (which
17/// Dioxus does on every `use_context` call) stays cheap.
18#[derive(Clone)]
19pub struct HyleConfig {
20 pub blueprint: Arc<Blueprint>,
21}
22
23/// Read the `HyleConfig` from Dioxus context.
24///
25/// Panics if `HyleConfig` has not been provided by an ancestor component.
26pub(crate) fn use_hyle_config() -> HyleConfig {
27 use_context::<HyleConfig>()
28}
29
30/// Read `HyleComponents` from Dioxus context, if provided.
31///
32/// Returns `None` when no ancestor has called
33/// `use_context_provider(|| HyleComponents { .. })`.
34pub fn use_hyle_components() -> Option<HyleComponents> {
35 try_use_context::<HyleComponents>()
36}
37
38/// Convenience re-export so callers don't need to import `use_context_provider` directly.
39#[allow(unused_imports)]
40pub use dioxus_hooks::use_context_provider;