tui_temp_fork/widgets/canvas/
rectangle.rs

1use crate::layout::Rect;
2use crate::style::Color;
3use crate::widgets::canvas::{Line, Shape};
4use itertools::Itertools;
5
6/// Shape to draw a rectangle from a `Rect` with the given color
7pub struct Rectangle {
8    pub rect: Rect,
9    pub color: Color,
10}
11
12impl<'a> Shape<'a> for Rectangle {
13    fn color(&self) -> Color {
14        self.color
15    }
16
17    fn points(&'a self) -> Box<dyn Iterator<Item = (f64, f64)> + 'a> {
18        let left_line = Line {
19            x1: f64::from(self.rect.x),
20            y1: f64::from(self.rect.y),
21            x2: f64::from(self.rect.x),
22            y2: f64::from(self.rect.y + self.rect.height),
23            color: self.color,
24        };
25        let top_line = Line {
26            x1: f64::from(self.rect.x),
27            y1: f64::from(self.rect.y + self.rect.height),
28            x2: f64::from(self.rect.x + self.rect.width),
29            y2: f64::from(self.rect.y + self.rect.height),
30            color: self.color,
31        };
32        let right_line = Line {
33            x1: f64::from(self.rect.x + self.rect.width),
34            y1: f64::from(self.rect.y),
35            x2: f64::from(self.rect.x + self.rect.width),
36            y2: f64::from(self.rect.y + self.rect.height),
37            color: self.color,
38        };
39        let bottom_line = Line {
40            x1: f64::from(self.rect.x),
41            y1: f64::from(self.rect.y),
42            x2: f64::from(self.rect.x + self.rect.width),
43            y2: f64::from(self.rect.y),
44            color: self.color,
45        };
46        Box::new(
47            left_line.into_iter().merge(
48                top_line
49                    .into_iter()
50                    .merge(right_line.into_iter().merge(bottom_line.into_iter())),
51            ),
52        )
53    }
54}