egui-cha-ds 0.6.0

Design System for egui-cha (Atoms, Molecules, Theme)
Documentation
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
//! Waveform atom - Audio waveform visualization for EDM/VJ applications
//!
//! Displays audio sample data as a waveform. Uses egui_plot internally for
//! smooth, anti-aliased rendering at 60fps.
//!
//! # Features
//! - Line or filled waveform styles (smooth via egui_plot)
//! - Bars style for discrete visualization
//! - Configurable height and color
//! - Theme-aware styling
//! - Supports mono and stereo display
//!
//! # Example
//! ```ignore
//! // Basic waveform
//! Waveform::new(&audio_samples)
//!     .show(ui);
//!
//! // With custom styling
//! Waveform::new(&samples)
//!     .height(80.0)
//!     .filled()
//!     .color(theme.primary)
//!     .show(ui);
//!
//! // Stereo waveform
//! Waveform::stereo(&left_samples, &right_samples)
//!     .show(ui);
//! ```

use crate::Theme;
use egui::{Color32, Pos2, Rect, Response, Sense, Stroke, Ui, Vec2, Widget};

#[cfg(feature = "plot")]
use egui_plot::{Line, Plot, PlotPoints};

/// Waveform display style
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum WaveformStyle {
    /// Line connecting sample points (smooth, uses egui_plot)
    #[default]
    Line,
    /// Filled area from center line (smooth, uses egui_plot)
    Filled,
    /// Bars for each sample (discrete, custom drawing)
    Bars,
}

/// A waveform visualization component
pub struct Waveform<'a> {
    samples: &'a [f32],
    samples_right: Option<&'a [f32]>,
    height: Option<f32>,
    style: WaveformStyle,
    color: Option<Color32>,
    color_right: Option<Color32>,
    show_center_line: bool,
    show_grid: bool,
    line_width: f32,
}

impl<'a> Waveform<'a> {
    /// Create a new waveform from sample data
    ///
    /// Samples should be normalized to -1.0..1.0 range
    pub fn new(samples: &'a [f32]) -> Self {
        Self {
            samples,
            samples_right: None,
            height: None,
            style: WaveformStyle::default(),
            color: None,
            color_right: None,
            show_center_line: true,
            show_grid: false,
            line_width: 1.5,
        }
    }

    /// Create a stereo waveform (top: left, bottom: right)
    pub fn stereo(left: &'a [f32], right: &'a [f32]) -> Self {
        Self {
            samples: left,
            samples_right: Some(right),
            height: None,
            style: WaveformStyle::default(),
            color: None,
            color_right: None,
            show_center_line: true,
            show_grid: false,
            line_width: 1.5,
        }
    }

    /// Set the height (default: uses theme spacing)
    pub fn height(mut self, height: f32) -> Self {
        self.height = Some(height);
        self
    }

    /// Set display style
    pub fn style(mut self, style: WaveformStyle) -> Self {
        self.style = style;
        self
    }

    /// Use filled style
    pub fn filled(mut self) -> Self {
        self.style = WaveformStyle::Filled;
        self
    }

    /// Use bars style
    pub fn bars(mut self) -> Self {
        self.style = WaveformStyle::Bars;
        self
    }

    /// Set waveform color (default: theme.primary)
    pub fn color(mut self, color: Color32) -> Self {
        self.color = Some(color);
        self
    }

    /// Set right channel color for stereo (default: theme.secondary)
    pub fn color_right(mut self, color: Color32) -> Self {
        self.color_right = Some(color);
        self
    }

    /// Show/hide center line
    pub fn center_line(mut self, show: bool) -> Self {
        self.show_center_line = show;
        self
    }

    /// Show/hide background grid
    pub fn grid(mut self, show: bool) -> Self {
        self.show_grid = show;
        self
    }

    /// Set line width (default: 1.5)
    pub fn line_width(mut self, width: f32) -> Self {
        self.line_width = width;
        self
    }

    /// Show the waveform
    pub fn show(self, ui: &mut Ui) -> Response {
        let theme = Theme::current(ui.ctx());

        // Calculate dimensions
        let height = self.height.unwrap_or(theme.spacing_xl * 2.0);
        let total_height = if self.samples_right.is_some() {
            height * 2.0 + theme.spacing_xs
        } else {
            height
        };
        let width = ui.available_width();

        let (rect, response) =
            ui.allocate_exact_size(Vec2::new(width, total_height), Sense::hover());

        if ui.is_rect_visible(rect) {
            // Colors
            let primary_color = self.color.unwrap_or(theme.primary);
            let secondary_color = self.color_right.unwrap_or(theme.secondary);

            if self.samples_right.is_some() {
                // Stereo: draw two waveforms
                let left_rect = Rect::from_min_size(rect.min, Vec2::new(width, height));
                let right_rect = Rect::from_min_size(
                    rect.min + Vec2::new(0.0, height + theme.spacing_xs),
                    Vec2::new(width, height),
                );

                self.draw_waveform_in_rect(ui, left_rect, self.samples, primary_color, &theme);

                if let Some(right_samples) = self.samples_right {
                    self.draw_waveform_in_rect(
                        ui,
                        right_rect,
                        right_samples,
                        secondary_color,
                        &theme,
                    );
                }
            } else {
                // Mono: single waveform
                self.draw_waveform_in_rect(ui, rect, self.samples, primary_color, &theme);
            }
        }

        response
    }

    fn draw_waveform_in_rect(
        &self,
        ui: &mut Ui,
        rect: Rect,
        samples: &[f32],
        color: Color32,
        theme: &Theme,
    ) {
        if samples.is_empty() {
            return;
        }

        // For Bars style, use custom drawing
        if self.style == WaveformStyle::Bars {
            self.draw_bars_waveform_custom(ui, rect, samples, color, theme);
            return;
        }

        // For Line and Filled styles, use egui_plot
        #[cfg(feature = "plot")]
        {
            self.draw_plot_waveform(ui, rect, samples, color, theme);
        }

        #[cfg(not(feature = "plot"))]
        {
            // Fallback to custom drawing if plot feature is not enabled
            self.draw_line_waveform_custom(ui, rect, samples, color, theme);
        }
    }

    #[cfg(feature = "plot")]
    fn draw_plot_waveform(
        &self,
        ui: &mut Ui,
        rect: Rect,
        samples: &[f32],
        color: Color32,
        theme: &Theme,
    ) {
        // Draw background first
        ui.painter()
            .rect_filled(rect, theme.radius_sm, theme.bg_secondary);

        // Convert samples to plot points
        let plot_points: PlotPoints = samples
            .iter()
            .enumerate()
            .map(|(i, &sample)| {
                let x = i as f64 / samples.len().max(1) as f64;
                let y = sample.clamp(-1.0, 1.0) as f64;
                [x, y]
            })
            .collect();

        // Use unique ID based on rect position
        let plot_id_str = format!("waveform_{}_{}", rect.min.x as i32, rect.min.y as i32);

        let mut line = Line::new(&plot_id_str, plot_points)
            .color(color)
            .width(self.line_width);

        if self.style == WaveformStyle::Filled {
            line = line.fill(0.0);
        }

        // Clone values needed in closure
        let show_center_line = self.show_center_line;
        let center_line_color = theme.text_muted;

        // Create a child UI positioned at rect
        let mut child_ui = ui.new_child(
            egui::UiBuilder::new()
                .max_rect(rect)
                .layout(egui::Layout::left_to_right(egui::Align::Center)),
        );

        Plot::new(&plot_id_str)
            .height(rect.height())
            .width(rect.width())
            .show_axes(false)
            .show_grid(self.show_grid)
            .allow_zoom(false)
            .allow_drag(false)
            .allow_scroll(false)
            .allow_boxed_zoom(false)
            .allow_double_click_reset(false)
            .show_background(false)
            .include_y(-1.0)
            .include_y(1.0)
            .include_x(0.0)
            .include_x(1.0)
            .show(&mut child_ui, |plot_ui| {
                // Center line
                if show_center_line {
                    let center_line_id = format!("{}_center", plot_id_str);
                    let center_line = Line::new(
                        center_line_id,
                        PlotPoints::from_iter([[0.0, 0.0], [1.0, 0.0]]),
                    )
                    .color(center_line_color)
                    .width(0.5);
                    plot_ui.line(center_line);
                }

                plot_ui.line(line);
            });

        // Border (drawn after plot)
        ui.painter().rect_stroke(
            rect,
            theme.radius_sm,
            Stroke::new(theme.border_width, theme.border),
            egui::StrokeKind::Outside,
        );
    }

    #[cfg(not(feature = "plot"))]
    fn draw_line_waveform_custom(
        &self,
        ui: &mut Ui,
        rect: Rect,
        samples: &[f32],
        color: Color32,
        theme: &Theme,
    ) {
        let painter = ui.painter();

        // Background
        painter.rect_filled(rect, theme.radius_sm, theme.bg_secondary);

        let center_y = rect.center().y;
        let half_height = rect.height() / 2.0 - theme.spacing_xs;

        // Center line
        if self.show_center_line {
            painter.line_segment(
                [
                    Pos2::new(rect.min.x, center_y),
                    Pos2::new(rect.max.x, center_y),
                ],
                Stroke::new(theme.stroke_width * 0.5, theme.text_muted),
            );
        }

        // Draw waveform
        let stroke = Stroke::new(self.line_width, color);
        let step = rect.width() / samples.len().max(1) as f32;

        let points: Vec<Pos2> = samples
            .iter()
            .enumerate()
            .map(|(i, &sample)| {
                let x = rect.min.x + step * i as f32 + step / 2.0;
                let y = center_y - sample.clamp(-1.0, 1.0) * half_height;
                Pos2::new(x, y)
            })
            .collect();

        for window in points.windows(2) {
            painter.line_segment([window[0], window[1]], stroke);
        }

        // Border
        painter.rect_stroke(
            rect,
            theme.radius_sm,
            Stroke::new(theme.border_width, theme.border),
            egui::StrokeKind::Outside,
        );
    }

    fn draw_bars_waveform_custom(
        &self,
        ui: &mut Ui,
        rect: Rect,
        samples: &[f32],
        color: Color32,
        theme: &Theme,
    ) {
        let painter = ui.painter();

        // Background
        painter.rect_filled(rect, theme.radius_sm, theme.bg_secondary);

        let center_y = rect.center().y;
        let half_height = rect.height() / 2.0 - theme.spacing_xs;

        // Center line
        if self.show_center_line {
            painter.line_segment(
                [
                    Pos2::new(rect.min.x, center_y),
                    Pos2::new(rect.max.x, center_y),
                ],
                Stroke::new(theme.stroke_width * 0.5, theme.text_muted),
            );
        }

        let num_bars = samples.len().min(64); // Max 64 bars
        let samples_per_bar = samples.len() / num_bars.max(1);
        let bar_width = rect.width() / num_bars as f32;
        let gap = theme.spacing_xs * 0.5;

        for i in 0..num_bars {
            // Average samples for this bar
            let start = i * samples_per_bar;
            let end = ((i + 1) * samples_per_bar).min(samples.len());
            let slice = &samples[start..end];

            if slice.is_empty() {
                continue;
            }

            // Use RMS for better visual representation
            let rms: f32 = (slice.iter().map(|s| s * s).sum::<f32>() / slice.len() as f32).sqrt();
            let bar_height = rms.clamp(0.0, 1.0) * half_height;

            let x = rect.min.x + bar_width * i as f32 + gap / 2.0;
            let bar_rect = Rect::from_min_max(
                Pos2::new(x, center_y - bar_height),
                Pos2::new(x + bar_width - gap, center_y + bar_height),
            );

            painter.rect_filled(bar_rect, theme.radius_sm * 0.5, color);
        }

        // Border
        painter.rect_stroke(
            rect,
            theme.radius_sm,
            Stroke::new(theme.border_width, theme.border),
            egui::StrokeKind::Outside,
        );
    }
}

impl Widget for Waveform<'_> {
    fn ui(self, ui: &mut Ui) -> Response {
        self.show(ui)
    }
}