use crate::core::{ObjectId, Rect};
use crate::layout::Layout;
#[derive(Debug)]
pub struct ChartLayout {
chart_id: Option<ObjectId>,
}
impl ChartLayout {
pub fn new(chart_id: ObjectId) -> Self {
Self { chart_id: Some(chart_id) }
}
}
impl Layout for ChartLayout {
fn add_widget(&mut self, widget_id: ObjectId, _stretch: u32) {
self.chart_id = Some(widget_id);
}
fn remove_widget(&mut self, widget_id: ObjectId) {
if self.chart_id == Some(widget_id) {
self.chart_id = None;
}
}
fn update(&self, rect: Rect, widgets: &mut dyn FnMut(ObjectId, Rect)) {
if let Some(id) = self.chart_id {
widgets(id, rect);
}
}
fn child_ids(&self) -> Vec<ObjectId> {
self.chart_id.into_iter().collect()
}
fn has_child(&self, id: ObjectId) -> bool {
self.chart_id == Some(id)
}
fn clear(&mut self) {
self.chart_id = None;
}
fn as_any(&self) -> &dyn std::any::Any {
self
}
fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
self
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::core::Rect;
#[test]
fn chart_layout_manages_single_child() {
let mut layout = ChartLayout::new(1);
assert!(layout.has_child(1));
assert_eq!(layout.child_ids(), vec![1]);
layout.add_widget(2, 0);
assert!(!layout.has_child(1));
assert!(layout.has_child(2));
}
#[test]
fn chart_layout_remove_widget() {
let mut layout = ChartLayout::new(42);
layout.remove_widget(42);
assert!(!layout.has_child(42));
assert!(layout.child_ids().is_empty());
}
#[test]
fn chart_layout_update_fills_rect() {
let layout = ChartLayout::new(99);
let mut results = Vec::new();
layout.update(Rect::new(10, 20, 300, 200), &mut |id, rect| {
results.push((id, rect));
});
assert_eq!(results, vec![(99, Rect::new(10, 20, 300, 200))]);
}
#[test]
fn chart_layout_clear() {
let mut layout = ChartLayout::new(7);
assert!(layout.has_child(7));
layout.clear();
assert!(!layout.has_child(7));
assert!(layout.child_ids().is_empty());
}
#[test]
fn chart_layout_update_empty_does_nothing() {
let layout = ChartLayout { chart_id: None };
let mut called = false;
layout.update(Rect::new(0, 0, 100, 100), &mut |_id, _rect| {
called = true;
});
assert!(!called);
}
}