1use tui::{
2 layout::Rect,
3};
4use treemap::{MapItem, Mappable, Rect as TmRect, TreemapLayout as TmLayout};
5
6#[derive(Debug)]
7pub struct TreeMapLayout {
8 sizes: Vec<f64>,
9 height_multiplier: f64,
10}
12
13impl TreeMapLayout {
14 pub fn new(sizes: Vec<f64>) -> Self {
15 Self {
16 sizes,
17 height_multiplier: 1.0,
18 }
19 }
20
21 pub fn with_height_multiplier(mut self, height_multiplier: f64) -> Self {
22 self.height_multiplier = height_multiplier;
23 self
24 }
25
26 pub fn split(&self, area: Rect) -> Vec<Rect> {
27 let mut items: Vec<_> = self.sizes.iter().map(|inner| MapItem::with_size(*inner)).collect();
28 TmLayout::new().layout_items(&mut items, TmRect::from_points(area.x as f64, area.y as f64 * self.height_multiplier, area.width as f64, area.height as f64 * self.height_multiplier));
29 items.into_iter().map(|ea| {
30 let bounds = ea.bounds();
31 Rect {
32 x: bounds.x.round() as u16,
33 y: (bounds.y / self.height_multiplier).round() as u16,
34 width: bounds.w.round() as u16,
35 height: (bounds.h / self.height_multiplier).round() as u16,
36 }
37 }).collect()
38 }
39}