use std::cell::RefCell;
use std::collections::HashMap;
use gpui::{App, Global, ScrollStrategy, SharedString, UniformListScrollHandle};
use crate::foundation::Ident;
#[derive(Default)]
struct ScrollHandles(RefCell<HashMap<SharedString, UniformListScrollHandle>>);
impl Global for ScrollHandles {}
pub(crate) fn scroll_handle(ident: &Ident, cx: &mut App) -> UniformListScrollHandle {
if !cx.has_global::<ScrollHandles>() {
cx.set_global(ScrollHandles::default());
}
let mut handles = cx.global::<ScrollHandles>().0.borrow_mut();
handles.entry(ident.semantic_id()).or_default().clone()
}
pub fn scroll_to_row(ident: &Ident, index: usize, cx: &mut App) {
scroll_handle(ident, cx).scroll_to_item(index, ScrollStrategy::Bottom);
}
pub fn reveal_row(ident: &Ident, index: usize, cx: &mut App) {
scroll_handle(ident, cx).scroll_to_item(index, ScrollStrategy::Nearest);
}