use super::*;
pub fn with_context<F, R>(ctx: Context, operation: F) -> R
where
F: FnOnce() -> R,
{
let previous: Option<Context> =
ACTIVE_CONTEXT.with(|slot: &RefCell<Option<Context>>| slot.borrow_mut().replace(ctx));
let _guard: ContextGuard = ContextGuard::new(previous);
operation()
}
pub fn with_current_context<F, R>(operation: F) -> R
where
F: FnOnce(&Context) -> R,
{
ACTIVE_CONTEXT.with(|slot: &RefCell<Option<Context>>| {
let borrowed: Ref<'_, Option<Context>> = slot.borrow();
if let Some(ctx) = borrowed.as_ref() {
operation(ctx)
} else {
drop(borrowed);
let fallback: Context = Context::new(SceneHandle::new_tree(), PlatformKind::Headless);
operation(&fallback)
}
})
}
#[must_use]
pub fn current_context() -> Context {
ACTIVE_CONTEXT.with(|slot: &RefCell<Option<Context>>| {
slot.borrow()
.clone()
.unwrap_or_else(|| Context::new(SceneHandle::new_tree(), PlatformKind::Headless))
})
}
#[must_use]
pub fn has_context() -> bool {
ACTIVE_CONTEXT.with(|slot: &RefCell<Option<Context>>| slot.borrow().is_some())
}
#[must_use]
pub fn current_platform() -> PlatformKind {
with_current_context(|ctx: &Context| ctx.get_platform())
}
pub fn mount_reactive_text<F>(style: Style, producer: F) -> NodeId
where
F: FnMut() -> String + 'static,
{
let handle: SceneHandle = with_current_context(|ctx: &Context| ctx.get_scene().clone());
let node_id: NodeId = handle.with_write(
|tree: &mut SceneTree| tree.create_text(String::new(), style),
NodeId::default(),
);
let (scope, _): (Scope, Effect) = Scope::run(|_: Scope| handle.bind_text(node_id, producer));
handle.mutate(move |tree: &mut SceneTree| {
tree.attach_scope(node_id, scope);
});
node_id
}
pub fn bind_style_current<F>(node_id: NodeId, producer: F)
where
F: FnMut() -> Style + 'static,
{
let handle: SceneHandle = with_current_context(|ctx: &Context| ctx.get_scene().clone());
let (scope, _): (Scope, Effect) = Scope::run(|_: Scope| handle.bind_style(node_id, producer));
handle.mutate(move |tree: &mut SceneTree| {
tree.attach_scope(node_id, scope);
});
}
pub fn bind_children_current<F>(host: NodeId, builder: F)
where
F: FnMut() -> Vec<NodeId> + 'static,
{
let mut ctx: Context = current_context();
ctx.bind_children(host, builder);
}
#[must_use]
pub fn into_text<T>(value: T) -> String
where
T: Into<String>,
{
value.into()
}
pub fn register_frame_wakeup(hook: fn()) {
FRAME_WAKEUP_HOOK.store(hook as *mut (), Ordering::Release);
if FRAME_NEEDED.load(Ordering::Acquire) {
hook();
}
}
pub fn clear_frame_wakeup() {
FRAME_WAKEUP_HOOK.store(null_mut(), Ordering::Release);
}
#[inline(always)]
pub fn notify_frame_needed() {
if !FRAME_NEEDED.load(Ordering::Relaxed) && !FRAME_NEEDED.swap(true, Ordering::AcqRel) {
let raw: *mut () = FRAME_WAKEUP_HOOK.load(Ordering::Acquire);
if !raw.is_null() {
let hook: fn() = unsafe { transmute::<*mut (), fn()>(raw) };
hook();
}
}
}
#[must_use]
#[inline(always)]
pub fn take_frame_needed() -> bool {
FRAME_NEEDED.swap(false, Ordering::AcqRel)
}
#[must_use]
#[inline(always)]
pub fn is_frame_needed() -> bool {
FRAME_NEEDED.load(Ordering::Acquire)
}
#[must_use]
#[inline(always)]
pub(crate) fn is_scene_borrowed() -> bool {
SCENE_BORROW_DEPTH
.try_with(|depth: &Cell<u32>| depth.get() > 0)
.unwrap_or(false)
}
#[inline(always)]
pub(crate) fn defer_effect_flush_while_borrowed() {
let _: Result<(), _> = DEFERRED_EFFECT_FLUSH.try_with(|flag: &Cell<bool>| {
flag.set(true);
});
notify_frame_needed();
}
#[must_use]
#[inline(always)]
pub(crate) fn has_deferred_effect_flush() -> bool {
DEFERRED_EFFECT_FLUSH.try_with(Cell::get).unwrap_or(false)
}
#[must_use]
#[inline(always)]
pub(crate) fn take_deferred_effect_flush() -> bool {
DEFERRED_EFFECT_FLUSH
.try_with(|flag: &Cell<bool>| flag.replace(false))
.unwrap_or(false)
}
pub(crate) fn reset_scene_borrow_state() {
let _: Result<(), _> = SCENE_BORROW_DEPTH.try_with(|depth: &Cell<u32>| {
depth.set(0);
});
let _: Result<(), _> = DEFERRED_EFFECT_FLUSH.try_with(|flag: &Cell<bool>| {
flag.set(false);
});
FRAME_NEEDED.store(false, Ordering::Release);
}