use std::cell::RefCell;
use std::rc::Rc;
use auralis_signal::subscription::{subscribe_to_dyn, SubscriptionHandle};
use auralis_signal::Signal;
use crate::core::dirty_registry;
use crate::core::element::{DirtyFlags, Element, ElementId};
use crate::style::Color;
pub fn store_subscription(eid: ElementId, handle: SubscriptionHandle) {
crate::core::element::with_ct_mut(|ct| {
ct.lc.entry(eid).or_default().subscriptions.push(handle);
});
}
pub fn subscribe_owned<T: 'static>(
eid: ElementId,
signal: &Signal<T>,
callback: impl Fn() + 'static,
) {
store_subscription(eid, subscribe_to_dyn(signal, Box::new(callback)));
}
pub fn bind_dirty<T: Clone + 'static>(
element: &Element,
signal: &Signal<T>,
app: std::rc::Weak<crate::core::app_context::AppContext>,
) -> T {
let dirty = element.dirty.clone();
let eid = element.id();
subscribe_owned(eid, signal, move || {
dirty.set(dirty.get() | DirtyFlags::REPAINT);
if let Some(app) = app.upgrade() {
app.register_dirty(eid, DirtyFlags::REPAINT);
app.bump_subtree_gen(eid);
}
});
signal.read()
}
pub fn bind_dirty_reposition<T: Clone + 'static>(
element: &Element,
signal: &Signal<T>,
app: std::rc::Weak<crate::core::app_context::AppContext>,
) -> T {
let dirty = element.dirty.clone();
let eid = element.id();
subscribe_owned(eid, signal, move || {
dirty.set(dirty.get() | DirtyFlags::REPOSITION);
if let Some(app) = app.upgrade() {
app.register_dirty(eid, DirtyFlags::REPOSITION);
app.bump_subtree_gen(eid);
}
});
signal.read()
}
pub fn bind_dirty_measure<T: Clone + 'static>(
element: &Element,
signal: &Signal<T>,
app: std::rc::Weak<crate::core::app_context::AppContext>,
) -> T {
let dirty = element.dirty.clone();
let eid = element.id();
subscribe_owned(eid, signal, move || {
dirty.set(dirty.get() | DirtyFlags::MEASURE);
if let Some(app) = app.upgrade() {
app.register_dirty(eid, DirtyFlags::MEASURE);
app.bump_subtree_gen(eid);
}
});
signal.read()
}
#[allow(dead_code)]
pub(crate) fn bind_label(
element_id: crate::core::ElementId,
dirty: Rc<std::cell::Cell<DirtyFlags>>,
signal: &Signal<String>,
app: std::rc::Weak<crate::core::app_context::AppContext>,
) -> String {
let d = dirty.clone();
let eid = element_id;
subscribe_owned(eid, signal, move || {
d.set(d.get() | DirtyFlags::REPAINT);
if let Some(app) = app.upgrade() {
app.register_dirty(eid, DirtyFlags::REPAINT);
app.bump_subtree_gen(eid);
}
});
signal.read()
}
#[allow(dead_code)]
pub(crate) fn bind_visible(
element_id: crate::core::ElementId,
dirty: Rc<std::cell::Cell<DirtyFlags>>,
signal: &Signal<bool>,
app: std::rc::Weak<crate::core::app_context::AppContext>,
) -> bool {
let d = dirty.clone();
let eid = element_id;
subscribe_owned(eid, signal, move || {
d.set(d.get() | DirtyFlags::REPAINT);
if let Some(app) = app.upgrade() {
app.register_dirty(eid, DirtyFlags::REPAINT);
app.bump_subtree_gen(eid);
}
});
signal.read()
}
#[allow(dead_code)]
pub(crate) fn bind_color(
element_id: crate::core::ElementId,
dirty: Rc<std::cell::Cell<DirtyFlags>>,
signal: &Signal<Color>,
app: std::rc::Weak<crate::core::app_context::AppContext>,
) -> Color {
let d = dirty.clone();
let eid = element_id;
subscribe_owned(eid, signal, move || {
d.set(d.get() | DirtyFlags::REPAINT);
if let Some(app) = app.upgrade() {
app.register_dirty(eid, DirtyFlags::REPAINT);
app.bump_subtree_gen(eid);
}
});
signal.read()
}
#[allow(dead_code)]
pub(crate) fn bind_f32(
element_id: crate::core::ElementId,
dirty: Rc<std::cell::Cell<DirtyFlags>>,
signal: &Signal<f32>,
app: std::rc::Weak<crate::core::app_context::AppContext>,
) -> f32 {
let d = dirty.clone();
let eid = element_id;
subscribe_owned(eid, signal, move || {
d.set(d.get() | DirtyFlags::REPAINT);
if let Some(app) = app.upgrade() {
app.register_dirty(eid, DirtyFlags::REPAINT);
app.bump_subtree_gen(eid);
}
});
signal.read()
}
pub(crate) fn bind_label_lazy(
label_cell: Rc<std::cell::Cell<String>>,
text_gen_cell: Rc<std::cell::Cell<u64>>,
dirty: Rc<std::cell::Cell<DirtyFlags>>,
eid: crate::core::ElementId,
signal: &Signal<String>,
app: std::rc::Weak<crate::core::app_context::AppContext>,
lazy_font_params: Option<Rc<crate::core::element::LazyFontParams>>,
measured_width: Option<Rc<std::cell::Cell<f32>>>,
) -> String {
let sig = signal.clone();
subscribe_owned(eid, signal, move || {
let old_gen = text_gen_cell.get();
let new_val_str = {
let v = sig.read();
label_cell.set(v.clone());
text_gen_cell.set(old_gen.wrapping_add(1));
v
};
if let (Some(ref lfp), Some(ref mw)) = (&lazy_font_params, &measured_width) {
let new_w = crate::render::text::measure_text_width(
&new_val_str,
lfp.font_size,
lfp.font_weight,
lfp.font_family.clone(),
)
.max(lfp.font_size * 2.0);
let old_w = mw.get();
if (old_w - new_w).abs() > 0.5 {
mw.set(new_w);
crate::core::element::with_ct_mut(|ct| {
ct.layout.entry(eid).or_default().preferred_width = Some(new_w);
});
}
}
vgen!(
"[VGEN:LAZY_BIND] eid={:?} text_generation: {} -> {}",
eid,
old_gen,
old_gen.wrapping_add(1),
);
let flags = DirtyFlags::REPAINT | DirtyFlags::MEASURE;
dirty.set(dirty.get() | flags);
if let Some(app) = app.upgrade() {
app.register_dirty(eid, flags);
app.bump_subtree_gen(eid);
}
});
signal.read()
}
type CleanupList = Rc<RefCell<Vec<Box<dyn FnOnce()>>>>;
thread_local! {
static PENDING_CLEANUPS: RefCell<Option<CleanupList>> = RefCell::new(None);
static IMPLICIT_DIRTY_LEVEL: RefCell<DirtyFlags> = const { RefCell::new(DirtyFlags::REPAINT) };
}
pub fn set_implicit_dirty(level: DirtyFlags) {
IMPLICIT_DIRTY_LEVEL.with(|l| *l.borrow_mut() = level);
}
pub fn observe_element<'e>(
element: &'e Element,
app: std::rc::Weak<crate::core::app_context::AppContext>,
) -> impl Drop + 'e {
let dirty = element.dirty.clone();
let eid = element.id();
let dirty_cb: Rc<dyn Fn()> = Rc::new(move || {
let level = IMPLICIT_DIRTY_LEVEL.with(|l| *l.borrow());
dirty.set(dirty.get() | level);
if let Some(app) = app.upgrade() {
app.register_dirty(eid, level);
app.bump_subtree_gen(eid);
}
});
let cleanups: CleanupList = Rc::new(RefCell::new(Vec::new()));
PENDING_CLEANUPS.with(|c| *c.borrow_mut() = Some(cleanups.clone()));
let on_sub: Rc<dyn Fn(usize, Box<dyn FnOnce()>)> = Rc::new({
let cleanups = cleanups.clone();
move |_addr, cleanup| {
cleanups.borrow_mut().push(cleanup);
}
});
let guard = auralis_signal::install_observer(dirty_cb, on_sub);
ObserveScope { _guard: guard }
}
struct ObserveScope {
_guard: auralis_signal::ObserverGuard,
}
impl Drop for ObserveScope {
fn drop(&mut self) {}
}
pub fn apply_observed_subscriptions(element: &mut Element) {
PENDING_CLEANUPS.with(|c| {
if let Some(cleanups) = c.borrow_mut().take() {
let eid = element.id();
let cleanups = std::mem::take(&mut *cleanups.borrow_mut());
crate::core::element::with_ct_mut(|ct| {
let lc = ct.lc.entry(eid).or_default();
for cleanup in cleanups {
lc.subscriptions
.push(SubscriptionHandle::from_cleanup(cleanup));
}
});
}
});
}
pub(crate) fn force_refresh_label(eid: ElementId) {
crate::core::element::with_ct_mut(|ct| {
if let Some(tc) = ct.text.get_mut(&eid) {
let old = tc.text_generation.get();
tc.text_generation.set(old.wrapping_add(1));
vgen!(
"[VGEN:FORCE_REFRESH] eid={:?} text_generation: {} -> {}",
eid,
old,
old.wrapping_add(1)
);
}
});
dirty_registry::mark_dirty(eid, DirtyFlags::REPAINT);
dirty_registry::register_dirty(eid, DirtyFlags::REPAINT);
dirty_registry::bump_subtree_gen(eid);
}
#[cfg(feature = "devtools")]
pub(crate) fn read_current_signal_addr() -> usize {
auralis_signal::CURRENT_NOTIFYING_SIGNAL.with(|c| c.get())
}