use crate::{ChartChange, ChartModel, ChartWindow, SeriesId, SeriesView};
use teksilo_core::color_prop::ColorProp;
use teksilo_core::{ObserverHandle, Signal};
#[derive(Debug)]
pub enum ChartSource<T: 'static> {
Model(ChartModel<T>),
Window(ChartWindow<T>),
}
impl<T: 'static> Clone for ChartSource<T> {
fn clone(&self) -> Self {
match self {
Self::Model(model) => Self::Model(model.clone()),
Self::Window(window) => Self::Window(window.clone()),
}
}
}
impl<T: 'static> From<ChartModel<T>> for ChartSource<T> {
fn from(model: ChartModel<T>) -> Self {
Self::Model(model)
}
}
impl<T: 'static> From<ChartWindow<T>> for ChartSource<T> {
fn from(window: ChartWindow<T>) -> Self {
Self::Window(window)
}
}
impl<T: 'static> ChartSource<T> {
fn source(&self) -> ChartModel<T> {
match self {
Self::Model(model) => model.clone(),
Self::Window(window) => window.source(),
}
}
pub fn with_all_series<R>(&self, f: impl FnOnce(&[SeriesView<'_, T>]) -> R) -> R {
match self {
Self::Model(model) => model.with_all_series(f),
Self::Window(window) => window.with_all_series(f),
}
}
pub fn with_series_view<R>(
&self,
series: SeriesId,
f: impl FnOnce(SeriesView<'_, T>) -> R,
) -> Option<R> {
match self {
Self::Model(model) => model.with_series_view(series, f),
Self::Window(window) => window.with_series_view(series, f),
}
}
pub fn with_series<R>(
&self,
series: SeriesId,
f: impl FnOnce(&str, Option<&ColorProp>, bool) -> R,
) -> Option<R> {
self.source().with_series(series, f)
}
pub fn set_series_visible(&self, series: SeriesId, visible: bool) {
self.source().set_series_visible(series, visible);
}
pub fn series_ids(&self) -> Vec<SeriesId> {
self.source().series_ids()
}
pub fn series_count(&self) -> usize {
self.source().series_count()
}
pub fn series_id_at(&self, index: usize) -> Option<SeriesId> {
self.source().series_id_at(index)
}
pub fn only_series(&self) -> Option<SeriesId> {
self.source().only_series()
}
pub fn point_count(&self, series: SeriesId) -> usize {
self.with_series_view(series, |view| view.points.len())
.unwrap_or(0)
}
pub fn structure_version(&self) -> Signal<u64> {
match self {
Self::Model(model) => model.structure_version(),
Self::Window(window) => window.structure_version(),
}
}
pub fn style_version(&self) -> Signal<u64> {
match self {
Self::Model(model) => model.style_version(),
Self::Window(window) => window.style_version(),
}
}
pub fn observe_changes(&self, f: impl Fn(&ChartChange) + 'static) -> ObserverHandle {
match self {
Self::Model(model) => model.observe_changes(f),
Self::Window(window) => window.observe_changes(f),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn window_borrows_points_and_tracks_structure_separately_from_style() {
struct NonClone;
let model = ChartModel::new();
let series = model.add_series("sensor");
for value in 0..4 {
model.push_point(series, NonClone, value as f32);
}
let window = ChartWindow::new(model.clone(), 2);
let input = ChartSource::from(window.clone());
model.with_series_view(series, |original| {
input.with_series_view(series, |view| {
assert!(std::ptr::eq(&original.points[2], &view.points[0]));
assert_eq!(view.points.len(), 2);
});
});
let version = input.structure_version().get();
window.set_window_size(1);
assert!(input.structure_version().get() > version);
assert_eq!(input.point_count(series), 1);
let version = input.structure_version().get();
model.set_series_pattern(series, crate::SeriesPattern::Dotted);
assert_eq!(input.structure_version().get(), version);
assert!(input.style_version().get() > 0);
input.set_series_visible(series, false);
assert_eq!(
model.with_series(series, |_, _, visible| visible),
Some(false)
);
model.remove_series(series);
assert_eq!(input.series_count(), 0);
assert!(input.with_series_view(series, |_| ()).is_none());
}
}