use std::{
cell::RefCell,
rc::{Rc, Weak},
};
use crate::{ScrollHandle, ScrollHandleState};
thread_local! {
#[allow(clippy::type_complexity)]
static QUIET_SCROLLS_TO_BOTTOM: RefCell<Vec<(Weak<RefCell<ScrollHandleState>>, u64)>> =
const { RefCell::new(Vec::new()) };
}
pub(crate) fn note_scroll_to_bottom(handle: &ScrollHandle, state: &ScrollHandleState) {
if state.scroll_to_bottom {
return;
}
let there = state
.offset
.try_borrow()
.is_ok_and(|offset| offset.y <= -state.max_offset.y);
if there {
QUIET_SCROLLS_TO_BOTTOM.with_borrow_mut(|quiet| {
quiet.push((Rc::downgrade(&handle.0), state.version.get()));
});
} else {
state.version.bump();
}
}
pub(crate) fn drop_quiet_scrolls_to_bottom() {
if QUIET_SCROLLS_TO_BOTTOM.with_borrow(|quiet| quiet.is_empty()) {
return;
}
let quiet = QUIET_SCROLLS_TO_BOTTOM.with_borrow_mut(std::mem::take);
for (handle, version) in quiet {
if let Some(handle) = handle.upgrade()
&& let Ok(mut state) = handle.try_borrow_mut()
&& state.version.get() == version
{
state.scroll_to_bottom = false;
}
}
}