use std::cell::{Cell, RefCell};
use std::collections::HashMap;
use std::rc::Rc;
use gpui::{App, Bounds, Global, Pixels, SharedString, Window};
const EPSILON: f32 = 0.5;
#[derive(Default)]
struct Measurements(RefCell<HashMap<SharedString, Rc<Cell<Bounds<Pixels>>>>>);
impl Global for Measurements {}
pub(crate) fn cell(id: &SharedString, cx: &mut App) -> Rc<Cell<Bounds<Pixels>>> {
if !cx.has_global::<Measurements>() {
cx.set_global(Measurements::default());
}
let mut cells = cx.global::<Measurements>().0.borrow_mut();
Rc::clone(cells.entry(id.clone()).or_default())
}
pub(crate) fn record(cell: &Rc<Cell<Bounds<Pixels>>>, bounds: Bounds<Pixels>, window: &mut Window) {
let previous = cell.get();
let moved = |a: Pixels, b: Pixels| (f32::from(a) - f32::from(b)).abs() > EPSILON;
if moved(previous.size.width, bounds.size.width)
|| moved(previous.size.height, bounds.size.height)
|| moved(previous.origin.x, bounds.origin.x)
|| moved(previous.origin.y, bounds.origin.y)
{
cell.set(bounds);
window.request_animation_frame();
}
}