use std::hash::{DefaultHasher, Hash, Hasher};
use gpui::{App, ElementId, Entity, Path, Pixels, Point, Window};
#[derive(Default)]
pub struct PathCache {
key: Option<u64>,
path: Option<Path>,
}
impl PathCache {
pub fn get(
&mut self,
key: u64,
origin: Point<Pixels>,
build: impl FnOnce() -> Option<Path>,
) -> Option<Path> {
let mut hasher = DefaultHasher::new();
key.hash(&mut hasher);
origin.x.as_f32().to_bits().hash(&mut hasher);
origin.y.as_f32().to_bits().hash(&mut hasher);
let key = hasher.finish();
if self.key != Some(key) {
self.path = build();
self.key = Some(key);
}
self.path.clone()
}
pub fn is_warm(&self) -> bool {
self.key.is_some()
}
}
#[derive(Default)]
pub struct PathCaches {
slots: Vec<PathCache>,
}
impl PathCaches {
pub fn for_paint(key: impl Into<ElementId>, window: &mut Window, cx: &mut App) -> Entity<Self> {
window.use_keyed_state(key, cx, |_, _| Self::default())
}
pub fn slot(&mut self, index: usize) -> &mut PathCache {
if self.slots.len() <= index {
self.slots.resize_with(index + 1, PathCache::default);
}
&mut self.slots[index]
}
pub fn slot_pair(&mut self, index: usize) -> (&mut PathCache, &mut PathCache) {
let first = 2 * index;
if self.slots.len() <= first + 1 {
self.slots.resize_with(first + 2, PathCache::default);
}
let (head, tail) = self.slots.split_at_mut(first + 1);
(&mut head[first], &mut tail[0])
}
}
pub struct ShapeKey(DefaultHasher);
impl ShapeKey {
pub fn new(extra: impl Hash) -> Self {
let mut hasher = DefaultHasher::new();
extra.hash(&mut hasher);
Self(hasher)
}
pub fn point(&mut self, point: Point<Pixels>) -> &mut Self {
point.x.as_f32().to_bits().hash(&mut self.0);
point.y.as_f32().to_bits().hash(&mut self.0);
self
}
pub fn f32(&mut self, value: f32) -> &mut Self {
value.to_bits().hash(&mut self.0);
self
}
pub fn finish(&self) -> u64 {
self.0.finish()
}
}
#[cfg(test)]
mod tests {
use super::*;
use gpui::{PathBuilder, point, px};
fn diagonal() -> Option<Path> {
let mut builder = PathBuilder::stroke(px(2.));
builder.move_to(point(px(0.), px(0.)));
builder.line_to(point(px(10.), px(10.)));
builder.build().ok()
}
#[test]
fn builds_once_per_key_and_origin() {
let mut cache = PathCache::default();
let mut builds = 0;
let origin = point(px(100.), px(50.));
cache.get(1, origin, || {
builds += 1;
diagonal()
});
assert!(
cache
.get(1, origin, || {
builds += 1;
diagonal()
})
.is_some()
);
assert_eq!(builds, 1);
cache.get(1, point(px(200.), px(50.)), || {
builds += 1;
diagonal()
});
assert_eq!(builds, 2);
cache.get(2, point(px(200.), px(50.)), || {
builds += 1;
diagonal()
});
assert_eq!(builds, 3);
}
#[test]
fn keys_follow_points_and_extras() {
let a = ShapeKey::new(("linear", 1.0f32.to_bits()))
.point(point(px(1.), px(2.)))
.finish();
let same = ShapeKey::new(("linear", 1.0f32.to_bits()))
.point(point(px(1.), px(2.)))
.finish();
let moved = ShapeKey::new(("linear", 1.0f32.to_bits()))
.point(point(px(1.), px(3.)))
.finish();
let thicker = ShapeKey::new(("linear", 2.0f32.to_bits()))
.point(point(px(1.), px(2.)))
.finish();
assert_eq!(a, same);
assert_ne!(a, moved);
assert_ne!(a, thicker);
}
}