use std::collections::HashMap;
use std::rc::Rc;
use crate::conversation::{ConversationItemId, Revision};
use crate::view::generation::Generation;
use ratatui::text::Line;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub(super) struct RenderShape {
pub(super) width: u16,
pub(super) padding: u16,
pub(super) theme: Generation,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub(super) struct RenderKey {
pub(super) item_id: ConversationItemId,
pub(super) revision: Revision,
pub(super) shape: RenderShape,
pub(super) spinner: Option<usize>,
}
#[derive(Default)]
pub(super) struct RenderCache {
pub(super) current: HashMap<RenderKey, Rc<[Line<'static>]>>,
pub(super) frame: HashMap<RenderKey, Rc<[Line<'static>]>>,
}
impl RenderCache {
pub(super) fn get_or_insert_with(
&mut self,
key: RenderKey,
build: impl FnOnce() -> Rc<[Line<'static>]>,
) -> (Rc<[Line<'static>]>, bool) {
if let Some(lines) = self.frame.remove(&key).or_else(|| self.current.remove(&key)) {
self.frame.insert(key, Rc::clone(&lines));
return (lines, false);
}
let lines = build();
self.frame.insert(key, Rc::clone(&lines));
(lines, true)
}
pub(super) fn clear(&mut self) {
self.current.clear();
self.frame.clear();
}
}