use std::{
cell::RefCell,
rc::Rc,
sync::atomic::{AtomicUsize, Ordering},
};
use crate::{Composer, ComposerCore};
thread_local! {
static CURRENT_COMPOSER: RefCell<Option<Rc<ComposerCore>>> = const { RefCell::new(None) };
}
static INSTALLED_COMPOSERS: AtomicUsize = AtomicUsize::new(0);
fn any_composer_installed() -> bool {
INSTALLED_COMPOSERS.load(Ordering::Relaxed) != 0
}
#[must_use = "ComposerScopeGuard restores the previous composer on drop"]
pub struct ComposerScopeGuard {
previous: Option<Rc<ComposerCore>>,
}
impl Drop for ComposerScopeGuard {
fn drop(&mut self) {
replace_current(self.previous.take());
}
}
pub fn enter(composer: &Composer) -> ComposerScopeGuard {
ComposerScopeGuard {
previous: replace_current(Some(composer.clone_core())),
}
}
fn replace_current(composer: Option<Rc<ComposerCore>>) -> Option<Rc<ComposerCore>> {
let installs = composer.is_some();
let previous = CURRENT_COMPOSER.with(|current| current.replace(composer));
match (previous.is_some(), installs) {
(false, true) => {
INSTALLED_COMPOSERS.fetch_add(1, Ordering::Relaxed);
}
(true, false) => {
INSTALLED_COMPOSERS.fetch_sub(1, Ordering::Relaxed);
}
_ => {}
}
previous
}
pub(crate) fn without_composer<R>(f: impl FnOnce() -> R) -> R {
let _suspended = ComposerScopeGuard {
previous: replace_current(None),
};
f()
}
pub fn with_composer<R>(f: impl FnOnce(&Composer) -> R) -> R {
CURRENT_COMPOSER.with(|current| {
let core = current
.borrow()
.as_ref()
.expect("with_composer: no active composer")
.clone();
let composer = Composer::from_core(core);
f(&composer)
})
}
pub(crate) fn with_current_core<R>(f: impl FnOnce(&ComposerCore) -> R) -> Option<R> {
if !any_composer_installed() {
return None;
}
CURRENT_COMPOSER.with(|current| current.borrow().as_deref().map(f))
}
pub fn current_composer() -> Option<Composer> {
if !any_composer_installed() {
return None;
}
CURRENT_COMPOSER.with(|current| {
let core = current.borrow().as_ref()?.clone();
Some(Composer::from_core(core))
})
}
pub fn note_nested_slots_host(host: &std::rc::Rc<crate::SlotsHost>) {
let Some(composer) = current_composer() else {
return;
};
let holder = composer.active_slots_host();
if std::rc::Rc::ptr_eq(&holder, host) {
return;
}
holder.note_nested_host(host);
}
pub fn try_with_composer<R>(f: impl FnOnce(&Composer) -> R) -> Option<R> {
current_composer().map(|composer| f(&composer))
}