use egui::Ui;
use super::{Plot, PlotResponse, PlotState};
use crisol_ui_kit::Icons;
pub struct PlotGrid<'a> {
columns: usize,
plots: Vec<Plot<'a>>,
cell_height: f32,
spacing: f32,
}
impl<'a> PlotGrid<'a> {
pub fn new(columns: usize) -> Self {
Self {
columns: columns.max(1),
plots: Vec::new(),
cell_height: 240.0,
spacing: 8.0,
}
}
pub fn plot(mut self, plot: Plot<'a>) -> Self {
self.plots.push(plot);
self
}
pub fn cell_height(mut self, height: f32) -> Self {
self.cell_height = height.max(80.0);
self
}
pub fn spacing(mut self, spacing: f32) -> Self {
self.spacing = spacing.max(0.0);
self
}
pub fn show(self, ui: &mut Ui, icons: &mut Icons, state: &mut PlotState) -> Vec<PlotResponse> {
let mut responses = Vec::with_capacity(self.plots.len());
let width = ((ui.available_width() - self.spacing * (self.columns.saturating_sub(1) as f32))
/ self.columns as f32)
.max(120.0);
for row in self.plots.chunks(self.columns) {
ui.horizontal(|ui| {
for plot in row {
let response = plot
.clone()
.width(width)
.height(self.cell_height)
.show(ui, icons, state);
responses.push(response);
}
});
ui.add_space(self.spacing);
}
responses
}
}