use std::collections::BTreeMap;
#[derive(Debug, Clone)]
pub(crate) struct ExpandedRowsCache {
expanded_rows: BTreeMap<u64, u64>,
valid_for: egui::Id,
}
impl Default for ExpandedRowsCache {
fn default() -> Self {
Self {
expanded_rows: BTreeMap::default(),
valid_for: egui::Id::new(""),
}
}
}
impl ExpandedRowsCache {
fn validate_id(&mut self, id: egui::Id) {
if id != self.valid_for {
self.valid_for = id;
self.expanded_rows = BTreeMap::default();
}
}
}
pub(crate) struct ExpandedRows<'a> {
row_height: f32,
cache: &'a mut ExpandedRowsCache,
egui_ctx: egui::Context,
id: egui::Id,
}
impl<'a> ExpandedRows<'a> {
pub(crate) fn new(
egui_ctx: egui::Context,
id: egui::Id,
cache: &'a mut ExpandedRowsCache,
row_height: f32,
) -> Self {
cache.validate_id(id);
Self {
row_height,
cache,
egui_ctx,
id,
}
}
pub(crate) fn row_top_offset(&self, row_nr: u64) -> f32 {
self.cache
.expanded_rows
.range(0..row_nr)
.map(|(expanded_row_nr, additional_lines)| {
self.egui_ctx.animate_value_with_time(
self.row_id(*expanded_row_nr),
*additional_lines as f32 * self.row_height,
self.egui_ctx.style().animation_time,
)
})
.sum::<f32>()
+ row_nr as f32 * self.row_height
}
pub(crate) fn is_row_odd(&self, row_nr: u64) -> bool {
let total_lines = self
.cache
.expanded_rows
.range(0..row_nr)
.map(|(_, additional_lines)| *additional_lines)
.sum::<u64>()
+ row_nr;
total_lines % 2 == 1
}
pub(crate) fn additional_lines_for_row(&self, row_nr: u64) -> u64 {
self.cache.expanded_rows.get(&row_nr).copied().unwrap_or(0)
}
pub(crate) fn set_additional_lines_for_row(&mut self, row_nr: u64, additional_lines: u64) {
if !self.cache.expanded_rows.contains_key(&row_nr) {
self.egui_ctx.animate_value_with_time(
self.row_id(row_nr),
0.0,
self.egui_ctx.style().animation_time,
);
}
self.cache.expanded_rows.insert(row_nr, additional_lines);
}
pub(crate) fn remove_additional_lines_for_row(&mut self, row_nr: u64) {
self.set_additional_lines_for_row(row_nr, 0);
}
#[inline]
fn row_id(&self, row_nr: u64) -> egui::Id {
self.id.with(row_nr)
}
}