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
// SPDX-License-Identifier: MIT
//! Spacer primitive for reserving empty space in rlvgl-ui layouts.
//!
//! [`Spacer`] is intentionally non-rendering and non-interactive. It exists so
//! layout code can name intentional gaps using the same widget contract as
//! visible controls.
use rlvgl_core::{
event::Event,
renderer::Renderer,
widget::{Rect, Widget},
};
/// Empty widget used to reserve layout space.
pub struct Spacer {
bounds: Rect,
}
impl Spacer {
/// Create a spacer with explicit bounds.
pub fn new(bounds: Rect) -> Self {
Self { bounds }
}
/// Create an origin-based spacer with only a width.
pub fn width(width: i32) -> Self {
Self::new(Rect {
x: 0,
y: 0,
width,
height: 0,
})
}
/// Create an origin-based spacer with only a height.
pub fn height(height: i32) -> Self {
Self::new(Rect {
x: 0,
y: 0,
width: 0,
height,
})
}
/// Create an origin-based square spacer.
pub fn square(size: i32) -> Self {
Self::new(Rect {
x: 0,
y: 0,
width: size,
height: size,
})
}
}
impl Widget for Spacer {
fn bounds(&self) -> Rect {
self.bounds
}
fn set_bounds(&mut self, bounds: Rect) {
self.bounds = bounds;
}
fn draw(&self, _renderer: &mut dyn Renderer) {}
fn handle_event(&mut self, _event: &Event) -> bool {
false
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn spacer_constructors_create_expected_bounds() {
assert_eq!(
Spacer::width(12).bounds(),
Rect {
x: 0,
y: 0,
width: 12,
height: 0,
}
);
assert_eq!(Spacer::height(7).bounds().height, 7);
assert_eq!(Spacer::square(5).bounds().width, 5);
assert_eq!(Spacer::square(5).bounds().height, 5);
}
#[test]
fn spacer_adopts_layout_bounds() {
let mut spacer = Spacer::height(4);
spacer.set_bounds(Rect {
x: 1,
y: 2,
width: 3,
height: 4,
});
assert_eq!(
spacer.bounds(),
Rect {
x: 1,
y: 2,
width: 3,
height: 4,
}
);
}
}