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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
//! Reactive counter example using Signal
//!
//! This demonstrates how to use the reactive system (Signal, Computed, Effect)
//! with the current widget API.
//!
//! Run with: cargo run --example reactive_counter
use revue::prelude::*;
/// A counter widget that uses Signal for reactive state
struct ReactiveCounter {
/// Reactive counter value
count: Signal<i32>,
/// Computed doubled value
doubled: Computed<i32>,
/// Computed status message
status: Computed<String>,
}
impl ReactiveCounter {
fn new() -> Self {
// Create reactive signal
let count = signal(0);
// Create computed value that doubles the count
let count_clone = count.clone();
let doubled = computed(move || count_clone.get() * 2);
// Create computed status message based on count
let count_clone2 = count.clone();
let status = computed(move || {
let value = count_clone2.get();
if value > 0 {
format!("Positive: {}", value)
} else if value < 0 {
format!("Negative: {}", value)
} else {
"Zero".to_string()
}
});
// Set up effect to log changes (optional)
let count_clone3 = count.clone();
effect(move || {
let value = count_clone3.get();
println!("Counter changed to: {}", value);
});
Self {
count,
doubled,
status,
}
}
fn increment(&mut self) {
self.count.update(|v| *v += 1);
}
fn decrement(&mut self) {
self.count.update(|v| *v -= 1);
}
fn reset(&mut self) {
self.count.set(0);
}
fn handle_key(&mut self, key: &Key) -> bool {
match key {
Key::Up | Key::Char('k') | Key::Char('+') => {
self.increment();
true
}
Key::Down | Key::Char('j') | Key::Char('-') => {
self.decrement();
true
}
Key::Char('r') => {
self.reset();
true
}
_ => false,
}
}
}
impl View for ReactiveCounter {
fn render(&self, ctx: &mut RenderContext) {
// Get reactive values - these are cached and only recompute when dependencies change!
let count = self.count.get();
let doubled = self.doubled.get();
let status = self.status.get();
let color = if count > 0 {
Color::GREEN
} else if count < 0 {
Color::RED
} else {
Color::WHITE
};
let view = vstack()
.gap(1)
.child(
Border::panel().title("đ Reactive Counter").child(
vstack()
.gap(1)
.child(
Text::new(format!("Count: {}", count))
.fg(color)
.bold()
.align(Alignment::Center),
)
.child(
Text::new(format!("Doubled: {}", doubled))
.fg(Color::CYAN)
.align(Alignment::Center),
)
.child(
Text::new(format!("Status: {}", status))
.fg(Color::YELLOW)
.align(Alignment::Center),
),
),
)
.child(
Border::single().title("Controls").child(
vstack()
.child(
hstack()
.gap(2)
.child(Text::muted("[+/-/â/â]"))
.child(Text::new("Increment/Decrement")),
)
.child(
hstack()
.gap(2)
.child(Text::muted("[r]"))
.child(Text::new("Reset")),
)
.child(
hstack()
.gap(2)
.child(Text::muted("[q]"))
.child(Text::new("Quit")),
),
),
)
.child(
Border::rounded().title("âšī¸ How It Works").child(
vstack()
.child(Text::success("â count is a Signal<i32>"))
.child(Text::success("â doubled is a Computed value"))
.child(Text::success("â status is computed based on count"))
.child(Text::info("â Computed values auto-update!"))
.child(Text::info("â No manual recalculation needed!")),
),
);
view.render(ctx);
}
fn meta(&self) -> WidgetMeta {
WidgetMeta::new("ReactiveCounter")
}
}
fn main() -> Result<()> {
println!("đ Reactive Counter Example");
println!("This example demonstrates Signal, Computed, and Effect.\n");
let mut app = App::builder().build();
let counter = ReactiveCounter::new();
app.run(counter, |event, counter, _app| match event {
Event::Key(key_event) => counter.handle_key(&key_event.key),
_ => false,
})
}