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
//! Example demonstrating the WidgetRef API for reactive widget bounds.
//!
//! Attaches a WidgetRef to a container and displays its surface-relative
//! position and size in real time via a reactive text widget.
use guido::prelude::*;
fn main() {
App::new().run(|app| {
let module_ref = create_widget_ref();
app.add_surface(
SurfaceConfig::new()
.height(64)
.anchor(Anchor::TOP | Anchor::LEFT | Anchor::RIGHT)
.background_color(Color::rgb(0.1, 0.1, 0.15)),
move || {
container()
.layout(Flex::row().spacing(16.0).center())
.child(
// Spacer
container().width(100.0),
)
.child(
// The measured module
container()
.widget_ref(module_ref)
.padding([8.0, 16.0])
.background(Color::rgb(0.25, 0.25, 0.35))
.corners(6.0)
.child(text("Measured Module").color(Color::WHITE)),
)
.child(
// Display the bounds reactively
container()
.padding([8.0, 16.0])
.background(Color::rgb(0.15, 0.2, 0.15))
.corners(6.0)
.child(
text(move || {
let r = module_ref.rect().get();
format!(
"Bounds: x={:.0} y={:.0} w={:.0} h={:.0}",
r.x, r.y, r.width, r.height
)
})
.color(Color::WHITE),
),
)
},
);
});
}