use crate::hit_test::{NodeGeometry, SimpleNodeGeometry};
use crate::state::GeometryCache;
use std::cell::RefCell;
use std::rc::Rc;
pub struct GeometryTracker<N = SimpleNodeGeometry> {
cache: Rc<RefCell<GeometryCache<N>>>,
}
impl<N> Default for GeometryTracker<N> {
fn default() -> Self {
Self::new()
}
}
impl<N> GeometryTracker<N> {
pub fn new() -> Self {
Self {
cache: Rc::new(RefCell::new(GeometryCache::new())),
}
}
pub fn with_cache(cache: Rc<RefCell<GeometryCache<N>>>) -> Self {
Self { cache }
}
pub fn cache(&self) -> Rc<RefCell<GeometryCache<N>>> {
self.cache.clone()
}
}
impl<N> GeometryTracker<N>
where
N: NodeGeometry + Copy,
{
pub fn pin_position_callback(&self) -> impl Fn(i32, i32, i32, f32, f32) + Clone {
let cache = self.cache.clone();
move |pin_id, node_id, pin_type, rel_x, rel_y| {
cache
.borrow_mut()
.handle_pin_report(pin_id, node_id, pin_type, rel_x, rel_y);
}
}
}
impl GeometryTracker<SimpleNodeGeometry> {
pub fn node_rect_callback(&self) -> impl Fn(i32, f32, f32, f32, f32) + Clone {
let cache = self.cache.clone();
move |id, x, y, width, height| {
cache
.borrow_mut()
.handle_node_rect_report(id, x, y, width, height);
}
}
}
impl<N> GeometryTracker<N>
where
N: NodeGeometry + Copy,
{
pub fn node_rect_callback_with<F>(&self, convert: F) -> impl Fn(i32, f32, f32, f32, f32) + Clone
where
F: Fn(i32, f32, f32, f32, f32) -> N + Clone + 'static,
{
let cache = self.cache.clone();
move |id, x, y, width, height| {
let node = convert(id, x, y, width, height);
cache.borrow_mut().node_rects.insert(id, node);
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::hit_test::SimpleNodeGeometry;
#[test]
fn test_tracker_new_creates_empty_cache() {
let tracker: GeometryTracker<SimpleNodeGeometry> = GeometryTracker::new();
let cache = tracker.cache();
assert!(cache.borrow().node_rects.is_empty());
assert!(cache.borrow().pin_positions.is_empty());
}
#[test]
fn test_tracker_default_creates_empty_cache() {
let tracker: GeometryTracker<SimpleNodeGeometry> = GeometryTracker::default();
let cache = tracker.cache();
assert!(cache.borrow().node_rects.is_empty());
}
#[test]
fn test_tracker_with_existing_cache() {
let cache = Rc::new(RefCell::new(GeometryCache::<SimpleNodeGeometry>::new()));
cache
.borrow_mut()
.update_node_rect(1, 10.0, 20.0, 100.0, 50.0);
let tracker = GeometryTracker::with_cache(cache.clone());
assert!(tracker.cache().borrow().node_rects.contains_key(&1));
}
#[test]
fn test_node_rect_callback_updates_cache() {
let tracker = GeometryTracker::new();
let callback = tracker.node_rect_callback();
callback(1, 10.0, 20.0, 100.0, 50.0);
let cache = tracker.cache();
let node = cache.borrow().node_rects.get(&1).copied();
assert!(node.is_some());
let node = node.unwrap();
assert_eq!(node.id, 1);
assert_eq!(node.x, 10.0);
assert_eq!(node.y, 20.0);
assert_eq!(node.width, 100.0);
assert_eq!(node.height, 50.0);
}
#[test]
fn test_pin_position_callback_updates_cache() {
let tracker: GeometryTracker<SimpleNodeGeometry> = GeometryTracker::new();
let callback = tracker.pin_position_callback();
callback(1001, 1, 2, 50.0, 25.0);
let cache = tracker.cache();
let pin = cache.borrow().pin_positions.get(&1001).copied();
assert!(pin.is_some());
let pin = pin.unwrap();
assert_eq!(pin.node_id, 1);
assert_eq!(pin.pin_type, 2);
assert_eq!(pin.rel_x, 50.0);
assert_eq!(pin.rel_y, 25.0);
}
#[test]
fn test_callbacks_share_same_cache() {
let tracker = GeometryTracker::new();
let node_cb = tracker.node_rect_callback();
let pin_cb = tracker.pin_position_callback();
node_cb(1, 0.0, 0.0, 100.0, 50.0);
pin_cb(1001, 1, 2, 50.0, 25.0);
let cache = tracker.cache();
assert_eq!(cache.borrow().node_rects.len(), 1);
assert_eq!(cache.borrow().pin_positions.len(), 1);
}
#[test]
fn test_multiple_cache_clones_share_data() {
let tracker = GeometryTracker::new();
let cache1 = tracker.cache();
let cache2 = tracker.cache();
cache1
.borrow_mut()
.update_node_rect(1, 0.0, 0.0, 100.0, 50.0);
assert!(cache2.borrow().node_rects.contains_key(&1));
}
#[test]
fn test_callback_is_clone() {
let tracker = GeometryTracker::new();
let cb1 = tracker.node_rect_callback();
let cb2 = cb1.clone();
cb1(1, 0.0, 0.0, 100.0, 50.0);
cb2(2, 100.0, 0.0, 100.0, 50.0);
let cache = tracker.cache();
assert_eq!(cache.borrow().node_rects.len(), 2);
}
#[test]
fn test_custom_node_geometry() {
#[derive(Clone, Copy)]
struct CustomNode {
id: i32,
x: f32,
y: f32,
w: f32,
h: f32,
extra: bool,
}
impl NodeGeometry for CustomNode {
fn id(&self) -> i32 {
self.id
}
fn rect(&self) -> (f32, f32, f32, f32) {
(self.x, self.y, self.w, self.h)
}
}
let tracker = GeometryTracker::<CustomNode>::new();
let callback = tracker.node_rect_callback_with(|id, x, y, w, h| CustomNode {
id,
x,
y,
w,
h,
extra: true,
});
callback(1, 10.0, 20.0, 100.0, 50.0);
let cache = tracker.cache();
let node = cache.borrow().node_rects.get(&1).copied();
assert!(node.is_some());
let node = node.unwrap();
assert_eq!(node.id, 1);
assert!(node.extra);
}
}