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
//! Example demonstrating reactive widget properties and input events.
//!
//! This example shows:
//! - Reactive text content that updates automatically
//! - State layer API for hover/pressed states with ripple effects
//! - Scroll event handling
//! - Borders and gradient backgrounds
//! - Direct signal updates from callbacks and background threads
use std::thread;
use std::time::Duration;
use guido::prelude::*;
fn main() {
App::new().run(|app| {
// Create signals for reactive state
let count = create_signal(0i32);
let scroll_offset = create_signal(0.0f32);
// Spawn a background thread that increments count every 2 seconds
// Use .writer() to get a Send-able handle for the background thread
let count_w = count.writer();
thread::spawn(move || {
loop {
thread::sleep(Duration::from_secs(2));
count_w.update(|c| *c += 1);
}
});
// Run the app
app.add_surface(
SurfaceConfig::new()
.height(32)
.anchor(Anchor::TOP | Anchor::LEFT | Anchor::RIGHT)
.background_color(Color::rgb(0.1, 0.1, 0.15)),
move || {
container()
.layout(
Flex::row()
.spacing(8.0)
.main_alignment(MainAlignment::SpaceBetween),
)
.child(
// Clickable container with state layer - click to increment count
container()
.padding(8.0)
.background(Color::rgb(0.2, 0.2, 0.3))
.corners(4.0)
.when_hovered(|s| s.lighter(0.1))
.when_pressed(|s| s.ripple())
.on_click(move || {
count.update(|c| *c += 10);
})
.child(
text(move || format!("Count: {} (click me!)", count.get()))
.color(Color::WHITE),
),
)
.child(
// Scrollable container with hover state
container()
.padding(8.0)
.background(Color::rgb(0.2, 0.3, 0.2))
.corners(4.0)
.when_hovered(|s| s.lighter(0.05))
.on_scroll(move |_dx, dy, _source| {
scroll_offset.update(|offset| {
*offset += dy;
});
})
.child(
text(move || format!("Scroll: {:.0}px", scroll_offset.get()))
.color(Color::WHITE),
),
)
.child(
// Container with border and hover state
container()
.padding(8.0)
.background(Color::rgb(0.15, 0.15, 0.2))
.border(2.0, Color::rgb(0.4, 0.6, 0.8))
.when_hovered(|s| s.lighter(0.1))
.child(text("With border").color(Color::WHITE)),
)
.child(
// Container with gradient and hover state
container()
.padding(8.0)
.gradient(LinearGradient::horizontal(
Color::rgb(0.3, 0.1, 0.4),
Color::rgb(0.1, 0.3, 0.5),
))
.corners(4.0)
.when_hovered(|s| s.lighter(0.1))
.child(text("Gradient!").color(Color::WHITE)),
)
},
);
});
}