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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
//! Spacer component - configurable empty space.
use crate::{Cell, Color, Component, Event, Rect, Size, Surface};
/// A spacer component that occupies configurable space.
pub struct Spacer {
width: u16,
height: u16,
/// If true, the spacer expands to fill available space.
flex: bool,
bg_color: Option<Color>,
dirty: bool,
}
impl Spacer {
/// Create a spacer with fixed dimensions.
pub fn new(width: u16, height: u16) -> Self {
Self {
width,
height,
flex: false,
bg_color: None,
dirty: true,
}
}
/// Create a horizontal spacer (1 row tall).
pub fn horizontal(width: u16) -> Self {
Self::new(width, 1)
}
/// Create a vertical spacer (1 column wide).
pub fn vertical(height: u16) -> Self {
Self::new(1, height)
}
/// Create a flexible spacer that fills available space.
pub fn flex() -> Self {
Self {
width: 0,
height: 0,
flex: true,
bg_color: None,
dirty: true,
}
}
pub fn with_bg(mut self, color: Color) -> Self {
self.bg_color = Some(color);
self
}
pub fn set_size(&mut self, width: u16, height: u16) {
self.width = width;
self.height = height;
self.dirty = true;
}
}
impl Component for Spacer {
fn name(&self) -> &str {
"Spacer"
}
fn request_render(&mut self) {
self.dirty = true;
}
fn is_dirty(&self) -> bool {
self.dirty
}
fn clear_dirty(&mut self) {
self.dirty = false;
}
fn handle_event(&mut self, _event: &Event) -> bool {
false
}
fn render(&mut self, surface: &mut Surface, area: Rect) {
if let Some(bg) = self.bg_color {
for r in area.y..area.y + area.height {
for c in area.x..area.x + area.width {
surface.set(r, c, Cell::new(' ').with_bg(bg));
}
}
}
// If no bg color, we simply leave the area untouched (transparent)
}
fn min_size(&self) -> Size {
if self.flex {
Size {
width: 0,
height: 0,
}
} else {
Size {
width: self.width,
height: self.height,
}
}
}
fn desired_size(&self) -> Option<Size> {
if self.flex {
// Request a large size to fill available space
Some(Size {
width: 1000,
height: 1000,
})
} else {
Some(Size {
width: self.width,
height: self.height,
})
}
}
}