Skip to main content

euv_core/scene/tree/
fn.rs

1use super::*;
2
3/// Runs `operation` with `ctx` bound as the active ambient `Context`, restoring any previous context on exit.
4///
5/// # Arguments
6///
7/// - `Context` - Framework context to bind during `operation`.
8/// - `F` - Closure executing declarative UI construction (`view!` or components).
9///
10/// # Returns
11///
12/// - `R` - Value returned by `operation`.
13pub fn with_context<F, R>(ctx: Context, operation: F) -> R
14where
15    F: FnOnce() -> R,
16{
17    let previous: Option<Context> =
18        ACTIVE_CONTEXT.with(|slot: &RefCell<Option<Context>>| slot.borrow_mut().replace(ctx));
19    let _guard: ContextGuard = ContextGuard::new(previous);
20    operation()
21}
22
23/// Returns a cloned handle to the active ambient `Context`.
24///
25/// # Returns
26///
27/// - `Context` - Currently bound ambient framework context.
28#[must_use]
29pub fn current_context() -> Context {
30    ACTIVE_CONTEXT.with(|slot: &RefCell<Option<Context>>| {
31        slot.borrow()
32            .clone()
33            .unwrap_or_else(|| Context::new(SceneHandle::new_tree(), PlatformKind::Headless))
34    })
35}
36
37/// Returns `true` when an ambient `Context` is currently bound on this thread.
38///
39/// # Returns
40///
41/// - `bool` - Whether `with_context` or `App::mount` is active.
42#[must_use]
43pub fn has_context() -> bool {
44    ACTIVE_CONTEXT.with(|slot: &RefCell<Option<Context>>| slot.borrow().is_some())
45}
46
47/// Returns the active `PlatformKind` from the ambient `Context`.
48///
49/// # Returns
50///
51/// - `PlatformKind` - Execution platform kind.
52#[must_use]
53pub fn current_platform() -> PlatformKind {
54    current_context().get_platform()
55}
56
57/// Converts any string-like value into an owned `String` for declarative `view!` macro expansion.
58///
59/// # Arguments
60///
61/// - `T` - String-like text value implementing `Into<String>`.
62///
63/// # Returns
64///
65/// - `String` - Owned UTF-8 text string.
66#[must_use]
67pub fn into_text<T>(value: T) -> String
68where
69    T: Into<String>,
70{
71    value.into()
72}