1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
use le::Layout;
use crate::{
le::layout::{Rect, Vec2},
terminal::Buffer,
Widget, WidgetType,
};
pub struct FixedSize<'a> {
pub widget: WidgetType<'a>,
pub width: Option<usize>,
pub height: Option<usize>,
}
impl<'a> FixedSize<'a> {
pub fn new(
widget: WidgetType<'a>,
width: Option<usize>,
height: Option<usize>,
) -> Box<Self> {
Box::new(Self {
widget,
width,
height,
})
}
}
impl Layout for FixedSize<'_> {
fn width_for_height(&self, height: usize) -> usize {
match self.width {
Some(width) => width,
None => self.widget.width_for_height(height),
}
}
fn height_for_width(&self, width: usize) -> usize {
match self.height {
Some(height) => height,
None => self.widget.height_for_width(width),
}
}
fn prefered_size(&self) -> Vec2 {
let widget_size = self.widget.prefered_size();
Vec2::new(
self.width.unwrap_or(widget_size.x),
self.height.unwrap_or(widget_size.y),
)
}
}
impl Widget for FixedSize<'_> {
fn render<'a>(&self, rect: Rect, buffer: &mut Buffer) {
let mut rect = rect;
match (self.width, self.height) {
(Some(width), Some(height)) => {
rect.size =
Vec2::new(width.min(rect.size.x), height.min(rect.size.y));
}
(Some(width), None) => {
rect.size.x = width;
rect.size.y = self.widget.height_for_width(width);
}
(None, Some(height)) => {
rect.size.x = self.widget.width_for_height(height);
rect.size.y = height;
}
(None, None) => (),
}
self.widget.render(rect, buffer);
}
}