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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
//! Status indicator with animated states
//!
//! Visual indicator showing agent/system state with optional pulse/blink animations.
//!
//! # Example
//! ```ignore
//! // Factory methods (recommended)
//! StatusIndicator::active().show(ui);
//! StatusIndicator::error().show(ui);
//!
//! // With label (inline)
//! ui.horizontal(|ui| {
//! StatusIndicator::active().size(8.0).show(ui);
//! ui.label("Connected");
//! });
//!
//! // TEA style
//! StatusIndicator::active()
//! .label("Online")
//! .show_with(ctx, || Msg::StatusClicked);
//! ```
use crate::Theme;
use egui::{Color32, Response, Sense, Ui, Vec2};
use egui_cha::ViewCtx;
/// Status state with predefined colors and animations
#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub enum Status {
/// Gray, no animation - system offline/disconnected
#[default]
Offline,
/// Dim green, no animation - connected but idle
Idle,
/// Bright green, pulse animation - actively working
Active,
/// Blue, pulse animation - processing/busy
Busy,
/// Yellow, slow blink - needs attention
Warning,
/// Red, fast blink - error state
Error,
}
/// Animation type for status indicator
#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub enum Animation {
/// No animation (static)
#[default]
None,
/// Smooth breathing effect (0.5 - 1.0 speed recommended)
Pulse { speed: f32 },
/// On/off toggle effect (2.0 - 4.0 speed recommended)
Blink { speed: f32 },
}
/// Visual status indicator with optional animation
pub struct StatusIndicator {
status: Status,
size: Option<f32>,
label: Option<String>,
custom_color: Option<Color32>,
custom_animation: Option<Animation>,
}
impl StatusIndicator {
// =========================================================================
// Factory methods (recommended)
// =========================================================================
/// Create an offline indicator (gray, static)
pub fn offline() -> Self {
Self::new(Status::Offline)
}
/// Create an idle indicator (dim green, static)
pub fn idle() -> Self {
Self::new(Status::Idle)
}
/// Create an active indicator (bright green, pulse)
pub fn active() -> Self {
Self::new(Status::Active)
}
/// Create a busy indicator (blue, pulse)
pub fn busy() -> Self {
Self::new(Status::Busy)
}
/// Create a warning indicator (yellow, slow blink)
pub fn warning() -> Self {
Self::new(Status::Warning)
}
/// Create an error indicator (red, fast blink)
pub fn error() -> Self {
Self::new(Status::Error)
}
// =========================================================================
// Generic constructor
// =========================================================================
/// Create a status indicator with specified status
pub fn new(status: Status) -> Self {
Self {
status,
size: None,
label: None,
custom_color: None,
custom_animation: None,
}
}
/// Create a custom status indicator with specified color
pub fn custom(color: Color32) -> Self {
Self {
status: Status::Idle,
size: None,
label: None,
custom_color: Some(color),
custom_animation: None,
}
}
// =========================================================================
// Builder methods
// =========================================================================
/// Set indicator size in pixels (default: theme.spacing_sm)
pub fn size(mut self, size: f32) -> Self {
self.size = Some(size);
self
}
/// Set label text to display next to indicator
pub fn label(mut self, label: impl Into<String>) -> Self {
self.label = Some(label.into());
self
}
/// Override the default color for this status
pub fn color(mut self, color: Color32) -> Self {
self.custom_color = Some(color);
self
}
/// Override the default animation for this status
pub fn animation(mut self, animation: Animation) -> Self {
self.custom_animation = Some(animation);
self
}
// =========================================================================
// Display methods
// =========================================================================
/// Show the status indicator (egui style)
pub fn show(self, ui: &mut Ui) -> Response {
self.show_internal(ui)
}
/// Show the status indicator with click handler (TEA style)
pub fn show_with<Msg>(self, ctx: &mut ViewCtx<'_, Msg>, on_click: impl FnOnce() -> Msg) {
let response = self.show_internal(ctx.ui);
if response.clicked() {
ctx.emit(on_click());
}
}
/// Show the status indicator and emit message on click (TEA style)
pub fn on_click<Msg: Clone>(self, ctx: &mut ViewCtx<'_, Msg>, msg: Msg) -> bool {
let response = self.show_internal(ctx.ui);
if response.clicked() {
ctx.emit(msg);
true
} else {
false
}
}
fn show_internal(self, ui: &mut Ui) -> Response {
let theme = Theme::current(ui.ctx());
let time = ui.input(|i| i.time) as f32;
// Get size from builder or use theme default
let size = self.size.unwrap_or(theme.spacing_sm);
// Determine color and animation based on status
let (base_color, default_animation) = match self.status {
Status::Offline => (theme.text_muted, Animation::None),
Status::Idle => (theme.state_success.gamma_multiply(0.6), Animation::None),
Status::Active => (theme.state_success, Animation::Pulse { speed: 1.0 }),
Status::Busy => (theme.state_info, Animation::Pulse { speed: 1.5 }),
Status::Warning => (theme.state_warning, Animation::Blink { speed: 2.0 }),
Status::Error => (theme.state_danger, Animation::Blink { speed: 4.0 }),
};
let color = self.custom_color.unwrap_or(base_color);
let animation = self.custom_animation.unwrap_or(default_animation);
// Calculate animation multiplier
let alpha_multiplier = match animation {
Animation::None => 1.0,
Animation::Pulse { speed } => {
// Smooth sine wave: 0.5 to 1.0
0.5 + 0.5 * (time * speed * std::f32::consts::TAU).sin()
}
Animation::Blink { speed } => {
// Sharp on/off: 1.0 or 0.3
if (time * speed).fract() < 0.5 {
1.0
} else {
0.3
}
}
};
let animated_color = color.gamma_multiply(alpha_multiplier);
// Allocate space
let desired_size = if self.label.is_some() {
// Will be handled by horizontal layout
Vec2::new(size, size)
} else {
Vec2::new(size, size)
};
// Draw with or without label
let response = if let Some(ref label_text) = self.label {
ui.horizontal(|ui| {
let (rect, response) = ui.allocate_exact_size(desired_size, Sense::click());
if ui.is_rect_visible(rect) {
let center = rect.center();
let radius = size / 2.0;
// Draw glow effect for animated states
if !matches!(animation, Animation::None) {
let glow_radius = radius * (1.0 + 0.3 * alpha_multiplier);
let glow_color = animated_color.gamma_multiply(0.3);
ui.painter().circle_filled(center, glow_radius, glow_color);
}
// Draw main circle
ui.painter()
.circle_filled(center, radius * 0.8, animated_color);
// Draw outline for offline state
if matches!(self.status, Status::Offline) {
ui.painter().circle_stroke(
center,
radius * 0.8,
egui::Stroke::new(1.0, theme.border),
);
}
}
ui.add_space(theme.spacing_xs / 2.0);
ui.label(label_text);
response
})
.inner
} else {
let (rect, response) = ui.allocate_exact_size(desired_size, Sense::click());
if ui.is_rect_visible(rect) {
let center = rect.center();
let radius = size / 2.0;
// Draw glow effect for animated states
if !matches!(animation, Animation::None) {
let glow_radius = radius * (1.0 + 0.3 * alpha_multiplier);
let glow_color = animated_color.gamma_multiply(0.3);
ui.painter().circle_filled(center, glow_radius, glow_color);
}
// Draw main circle
ui.painter()
.circle_filled(center, radius * 0.8, animated_color);
// Draw outline for offline state
if matches!(self.status, Status::Offline) {
ui.painter().circle_stroke(
center,
radius * 0.8,
egui::Stroke::new(1.0, theme.border),
);
}
}
response
};
// Request repaint if animating
if !matches!(animation, Animation::None) {
ui.ctx().request_repaint();
}
response
}
}