liveplot 2.1.1

Realtime interactive plotting library using egui/eframe, with optional gRPC and Parquet export support.
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
//! Color scheme panel for managing trace color palettes.

use eframe::egui;
use eframe::egui::Color32;
use egui_phosphor_icons::icons::{
    ARROW_DOWN, ARROW_UP, CHECK, FLOPPY_DISK, MINUS, PALETTE, PLUS, STAR, TRASH, WARNING,
};
use serde::{Deserialize, Serialize};

use super::panel_trait::{Panel, PanelState};
use crate::color_scheme::{set_global_palette, ColorScheme, CustomColorScheme};
use crate::data::data::LivePlotData;

/// A user-defined custom color scheme that can be serialized.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NamedCustomScheme {
    pub name: String,
    /// Colors stored as `[r, g, b]` triples.
    pub colors: Vec<[u8; 3]>,
}

impl NamedCustomScheme {
    pub fn to_color32_vec(&self) -> Vec<Color32> {
        self.colors
            .iter()
            .map(|c| Color32::from_rgb(c[0], c[1], c[2]))
            .collect()
    }

    pub fn from_palette(name: &str, palette: &[Color32]) -> Self {
        Self {
            name: name.to_string(),
            colors: palette.iter().map(|c| [c.r(), c.g(), c.b()]).collect(),
        }
    }
}

pub struct ColorSchemePanel {
    state: PanelState,
    /// Index of the currently selected scheme in the dropdown.
    /// 0..N = built-in schemes, N.. = custom schemes.
    pub selected_index: usize,
    /// Working copy of the palette being edited.
    pub editing_palette: Vec<Color32>,
    /// Text input for the custom scheme name.
    pub editing_name: String,
    /// User-saved custom schemes.
    pub custom_schemes: Vec<NamedCustomScheme>,
    /// Whether the editing palette has been modified since loading.
    dirty: bool,
}

impl Default for ColorSchemePanel {
    fn default() -> Self {
        let default_scheme = ColorScheme::Dark;
        let palette = default_scheme.trace_colors();
        Self {
            state: PanelState::new("Color Scheme", PALETTE.as_str()),
            selected_index: 0,
            editing_palette: palette,
            editing_name: String::new(),
            custom_schemes: Vec::new(),
            dirty: false,
        }
    }
}

impl ColorSchemePanel {
    /// Get the list of all scheme names (built-in + custom).
    pub fn scheme_labels(&self) -> Vec<String> {
        let mut labels: Vec<String> = ColorScheme::all().iter().map(|s| s.label()).collect();
        for cs in &self.custom_schemes {
            labels.push(cs.name.clone());
        }
        labels
    }

    /// Get the palette for a given scheme index.
    fn palette_for_index(&self, index: usize) -> Vec<Color32> {
        let builtins = ColorScheme::all();
        if index < builtins.len() {
            builtins[index].trace_colors()
        } else {
            let ci = index - builtins.len();
            if ci < self.custom_schemes.len() {
                self.custom_schemes[ci].to_color32_vec()
            } else {
                ColorScheme::Dark.trace_colors()
            }
        }
    }

    /// Apply the current editing palette to the global state.
    fn apply_palette(&self, ui: &mut egui::Ui, palette: &[Color32]) {
        let ctx = ui.ctx().clone();
        let custom = CustomColorScheme {
            visuals: None,
            palette: palette.to_vec(),
            label: Some("Custom".to_string()),
        };
        let scheme = ColorScheme::Custom(custom);
        scheme.apply(&ctx);
        // Also recolor existing traces via the global palette.
        set_global_palette(palette.to_vec());
    }

    /// Apply a built-in or custom scheme by index.
    fn apply_scheme_by_index(&self, ui: &mut egui::Ui, index: usize) {
        let builtins = ColorScheme::all();
        if index < builtins.len() {
            let ctx = ui.ctx().clone();
            builtins[index].apply(&ctx);
        } else {
            let palette = self.palette_for_index(index);
            self.apply_palette(ui, &palette);
        }
    }

    /// Set custom schemes from loaded state (used by session restore).
    pub fn set_custom_schemes(&mut self, schemes: Vec<NamedCustomScheme>) {
        self.custom_schemes = schemes;
    }

    /// Get custom schemes for saving (used by session save).
    pub fn get_custom_schemes(&self) -> &[NamedCustomScheme] {
        &self.custom_schemes
    }

    /// Get the current editing palette as RGB triples for persistence.
    pub fn get_active_palette(&self) -> Vec<[u8; 3]> {
        self.editing_palette
            .iter()
            .map(|c| [c.r(), c.g(), c.b()])
            .collect()
    }

    /// Restore the active palette from loaded state and apply it.
    pub fn restore_active_palette(&mut self, colors: &[[u8; 3]]) {
        self.editing_palette = colors
            .iter()
            .map(|c| Color32::from_rgb(c[0], c[1], c[2]))
            .collect();
        self.dirty = false;
    }

    /// Set the initial scheme from config (called after construction).
    pub fn set_initial_scheme(&mut self, scheme: &ColorScheme) {
        let builtins = ColorScheme::all();
        for (i, s) in builtins.iter().enumerate() {
            if s == scheme {
                self.selected_index = i;
                self.editing_palette = s.trace_colors();
                return;
            }
        }
        // If it's a custom scheme, try to match by palette.
        if let ColorScheme::Custom(custom) = scheme {
            self.editing_palette = custom.palette.clone();
        }
    }
}

impl Panel for ColorSchemePanel {
    fn state(&self) -> &PanelState {
        &self.state
    }

    fn state_mut(&mut self) -> &mut PanelState {
        &mut self.state
    }

    fn render_menu(
        &mut self,
        ui: &mut egui::Ui,
        data: &mut LivePlotData<'_>,
        collapsed: bool,
        tooltip: &str,
    ) {
        let label = if collapsed {
            self.icon_only()
                .map(|s| s.to_string())
                .unwrap_or_else(|| self.title().to_string())
        } else {
            self.title_and_icon()
        };
        let menu_cfg = egui::containers::menu::MenuConfig::new()
            .close_behavior(egui::PopupCloseBehavior::CloseOnClickOutside);
        let mr = egui::containers::menu::MenuButton::new(label)
            .config(menu_cfg)
            .ui(ui, |ui| {
                if ui.button("Show Color Scheme").clicked() {
                    let st = self.state_mut();
                    st.visible = true;
                    st.request_focus = true;
                    ui.close();
                }

                ui.separator();

                let builtins = ColorScheme::all();
                let labels = self.scheme_labels();
                let prev_index = self.selected_index;
                for (i, label) in labels.iter().enumerate() {
                    let is_custom = i >= builtins.len();
                    let indicator = if i == self.selected_index {
                        CHECK.as_str()
                    } else {
                        "  "
                    };
                    let custom_prefix = if is_custom {
                        format!("{} ", STAR.as_str())
                    } else {
                        String::new()
                    };
                    if ui
                        .button(format!("{} {}{}", indicator, custom_prefix, label))
                        .clicked()
                    {
                        self.selected_index = i;
                    }
                }
                if self.selected_index != prev_index {
                    self.editing_palette = self.palette_for_index(self.selected_index);
                    self.dirty = false;
                    self.apply_scheme_by_index(ui, self.selected_index);
                    data.traces.recolor_using_palette();
                    data.settings_changed = true;
                    ui.close();
                }
            });
        if !tooltip.is_empty() {
            mr.0.on_hover_text(tooltip);
        }
    }

    fn render_panel(&mut self, ui: &mut egui::Ui, data: &mut LivePlotData<'_>) {
        let builtins = ColorScheme::all();
        let labels = self.scheme_labels();

        // ── Scheme selector ──────────────────────────────────────────────
        ui.horizontal(|ui| {
            ui.label("Scheme:");
            let prev_index = self.selected_index;
            egui::ComboBox::from_id_salt("color_scheme_selector")
                .selected_text(
                    labels
                        .get(self.selected_index)
                        .cloned()
                        .unwrap_or_else(|| "".to_string()),
                )
                .show_ui(ui, |ui| {
                    for (i, label) in labels.iter().enumerate() {
                        let is_custom = i >= builtins.len();
                        let prefix = if is_custom {
                            format!("{} ", STAR.as_str())
                        } else {
                            "".to_string()
                        };
                        ui.selectable_value(
                            &mut self.selected_index,
                            i,
                            format!("{}{}", prefix, label),
                        );
                    }
                });
            // If selection changed, load the palette.
            if self.selected_index != prev_index {
                self.editing_palette = self.palette_for_index(self.selected_index);
                self.dirty = false;
                self.apply_scheme_by_index(ui, self.selected_index);
                data.traces.recolor_using_palette();
                data.settings_changed = true;
            }
        });

        ui.separator();

        // ── Color list ───────────────────────────────────────────────────
        ui.label("Trace colors:");
        let mut to_remove: Option<usize> = None;
        let mut to_move_up: Option<usize> = None;
        let mut to_move_down: Option<usize> = None;
        let mut color_changed = false;

        for (i, color) in self.editing_palette.iter_mut().enumerate() {
            ui.horizontal(|ui| {
                let mut color_arr = [color.r(), color.g(), color.b(), color.a()];
                let resp = ui.color_edit_button_srgba_unmultiplied(&mut color_arr);
                *color = Color32::from_rgba_unmultiplied(
                    color_arr[0],
                    color_arr[1],
                    color_arr[2],
                    color_arr[3],
                );
                if resp.changed() {
                    color_changed = true;
                }

                ui.label(format!("#{}", i + 1));

                if ui.button(ARROW_UP).on_hover_text("Move up").clicked() {
                    to_move_up = Some(i);
                }
                if ui.button(ARROW_DOWN).on_hover_text("Move down").clicked() {
                    to_move_down = Some(i);
                }
                if ui.button(MINUS).on_hover_text("Remove color").clicked() {
                    to_remove = Some(i);
                }
            });
        }

        // Apply moves.
        if color_changed {
            self.dirty = true;
        }
        if let Some(i) = to_move_up {
            if i > 0 {
                self.editing_palette.swap(i, i - 1);
                self.dirty = true;
            }
        }
        if let Some(i) = to_move_down {
            if i + 1 < self.editing_palette.len() {
                self.editing_palette.swap(i, i + 1);
                self.dirty = true;
            }
        }
        // Apply removal.
        if let Some(i) = to_remove {
            if self.editing_palette.len() > 1 {
                self.editing_palette.remove(i);
                self.dirty = true;
            }
        }

        // Mark dirty if any color changed (we can't easily detect this per-widget,
        // so we set dirty on any interaction in the color list area).
        ui.horizontal(|ui| {
            if ui.button(format!("{} Add color", PLUS.as_str())).clicked() {
                // Add a contrasting color.
                let new_color = Color32::from_rgb(200, 200, 80);
                self.editing_palette.push(new_color);
                self.dirty = true;
            }
            if ui.button(format!("{} Apply", CHECK.as_str())).clicked() {
                self.apply_palette(ui, &self.editing_palette);
                data.traces.recolor_using_palette();
                self.dirty = false;
                data.settings_changed = true;
            }
        });

        ui.separator();

        // ── Save as custom scheme ────────────────────────────────────────
        ui.label("Save current palette as:");
        ui.horizontal(|ui| {
            ui.text_edit_singleline(&mut self.editing_name);
            ui.add_space(4.0);
            if ui
                .button(format!("{} Save", FLOPPY_DISK.as_str()))
                .clicked()
                && !self.editing_name.trim().is_empty()
            {
                let name = self.editing_name.trim().to_string();
                // Check if name already exists — if so, replace.
                if let Some(pos) = self.custom_schemes.iter().position(|s| s.name == name) {
                    self.custom_schemes[pos] =
                        NamedCustomScheme::from_palette(&name, &self.editing_palette);
                } else {
                    self.custom_schemes.push(NamedCustomScheme::from_palette(
                        &name,
                        &self.editing_palette,
                    ));
                }
                // Select the newly saved scheme.
                self.selected_index = builtins.len() + self.custom_schemes.len() - 1;
                self.dirty = false;
                // Keep the name in the text field so the user can see what was saved.
            }
        });

        // ── Delete custom scheme ─────────────────────────────────────────
        let is_custom = self.selected_index >= builtins.len();
        if is_custom {
            ui.add_space(4.0);
            if ui
                .button(format!("{} Delete this custom scheme", TRASH.as_str()))
                .clicked()
            {
                let ci = self.selected_index - builtins.len();
                if ci < self.custom_schemes.len() {
                    self.custom_schemes.remove(ci);
                    self.selected_index = 0;
                    self.editing_palette = self.palette_for_index(0);
                    self.apply_scheme_by_index(ui, 0);
                    data.traces.recolor_using_palette();
                    data.settings_changed = true;
                }
            }
        }

        // ── Unsaved changes warning ───────────────────────────────────
        if self.dirty {
            ui.add_space(4.0);
            ui.horizontal(|ui| {
                ui.colored_label(
                    Color32::from_rgb(200, 160, 0),
                    format!(
                        "{} Unsaved changes — click Apply to preview, or save as a custom scheme",
                        WARNING.as_str()
                    ),
                );
            });
        }
    }

    fn settings_snapshot(&self, _data: &LivePlotData<'_>) -> Option<String> {
        let snap: (Vec<NamedCustomScheme>, Vec<[u8; 3]>) =
            (self.custom_schemes.clone(), self.get_active_palette());
        serde_json::to_string(&snap).ok()
    }
}