liveplot 2.0.1

Realtime interactive plotting library using egui/eframe, with optional gRPC and Parquet export support.
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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
use super::panel_trait::{Panel, PanelState};
use crate::data::scope::ScopeType;
use crate::data::{data::LivePlotData, traces::TraceRef};
use eframe::egui;
use egui::{Id, Ui};
use egui_phosphor::regular::{BROOM, DOTS_SIX_VERTICAL};
use egui_table::{HeaderRow as EgHeaderRow, Table, TableDelegate};

use super::scope_settings_ui::{DragPayload, ScopeSettingsUiPanel};
use super::trace_look_ui::render_trace_look_editor;

pub struct TracesPanel {
    pub state: PanelState,
    pub look_editor_trace: Option<TraceRef>,
    pub look_editor_xy_pair: Option<(usize, usize)>,
    pub hover_trace: Option<TraceRef>,
    pub dragging_trace: Option<DragPayload>,

    scope_settings_ui: ScopeSettingsUiPanel,
}

impl Default for TracesPanel {
    fn default() -> Self {
        Self {
            state: PanelState::new("Traces", "📈"),
            look_editor_trace: None,
            look_editor_xy_pair: None,
            hover_trace: None,
            dragging_trace: None,

            scope_settings_ui: ScopeSettingsUiPanel::default(),
        }
    }
}

impl Panel for TracesPanel {
    fn state(&self) -> &PanelState {
        &self.state
    }
    fn state_mut(&mut self) -> &mut PanelState {
        &mut self.state
    }

    fn hotkey_name(&self) -> Option<crate::data::hotkeys::HotkeyName> {
        Some(crate::data::hotkeys::HotkeyName::Traces)
    }

    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 mr = ui.menu_button(label, |ui| {
            // Show Traces: open the Traces panel and focus dock
            if ui.button("Show Traces").clicked() {
                let st = self.state_mut();
                st.visible = true;
                st.request_focus = true;
                ui.close();
            }

            ui.separator();

            // Data Points slider (mirror of panel control)
            ui.horizontal(|ui| {
                ui.label("Data Points:");
                ui.add(egui::Slider::new(
                    &mut data.traces.max_points,
                    data.traces.points_bounds.0..=data.traces.points_bounds.1,
                ));
            });

            ui.separator();

            // Visibility control: All Visible / All Hidden
            if ui.button("All Visible").clicked() {
                for (_name, tr) in data.traces.traces_iter_mut() {
                    tr.look.visible = true;
                }
                ui.close();
            }
            if ui.button("All Hidden").clicked() {
                for (_name, tr) in data.traces.traces_iter_mut() {
                    tr.look.visible = false;
                }
                ui.close();
            }

            ui.separator();

            if ui.button(format!("{BROOM} Clear All")).clicked() {
                data.traces.clear_all();
                ui.close();
            }
        });
        if !tooltip.is_empty() {
            mr.response.on_hover_text(tooltip);
        }
    }

    fn render_panel(&mut self, ui: &mut Ui, data: &mut LivePlotData<'_>) {
        if data.scope_data.is_empty() {
            ui.label("No scopes available.");
            return;
        }

        // Render the trace look editor in a bottom panel so it stays visible
        // without scrolling.
        let has_editor = self.look_editor_trace.is_some() || self.look_editor_xy_pair.is_some();
        if has_editor {
            egui::TopBottomPanel::bottom("traces_look_editor_panel")
                .resizable(true)
                .show_inside(ui, |ui| {
                    if let Some(tn) = self.look_editor_trace.clone() {
                        self.look_editor_xy_pair = None;
                        if let Some(tr) = data.traces.get_trace_mut(&tn) {
                            egui::Frame::group(ui.style()).show(ui, |ui| {
                                ui.horizontal(|ui| {
                                    ui.strong(format!("Style: {}", tn));
                                    ui.with_layout(
                                        egui::Layout::right_to_left(egui::Align::Center),
                                        |ui| {
                                            if ui.small_button("✖ Close").clicked() {
                                                self.look_editor_trace = None;
                                            }
                                        },
                                    );
                                });
                                ui.separator();
                                render_trace_look_editor(&mut tr.look, ui, true);
                            });
                        } else {
                            ui.label("Trace not found.");
                            self.look_editor_trace = None;
                        }
                    } else if let Some((scope_id, pair_idx)) = self.look_editor_xy_pair {
                        egui::Frame::group(ui.style()).show(ui, |ui| {
                            ui.horizontal(|ui| {
                                ui.strong(format!("XY Pair Style: #{}", pair_idx + 1));
                                ui.with_layout(
                                    egui::Layout::right_to_left(egui::Align::Center),
                                    |ui| {
                                        if ui.small_button("✖ Close").clicked() {
                                            self.look_editor_xy_pair = None;
                                        }
                                    },
                                );
                            });
                            ui.separator();

                            if let Some(scope) =
                                data.scope_data.iter_mut().find(|s| s.id == scope_id)
                            {
                                if let Some((_x, _y, look)) = scope.xy_pairs.get_mut(pair_idx) {
                                    render_trace_look_editor(look, ui, true);
                                } else {
                                    ui.label("XY pair not found.");
                                }
                            } else {
                                ui.label("Scope not found.");
                            }
                        });
                    }
                });
        }

        egui::ScrollArea::vertical()
            .id_salt("traces_panel_scroll")
            .show(ui, |ui| {
                self.render_scope_assignments(ui, data);
                ui.separator();

                ui.label("Data Points:");
                ui.add(egui::Slider::new(
                    &mut data.traces.max_points,
                    data.traces.points_bounds.0..=data.traces.points_bounds.1,
                ));

                ui.separator();

                ui.horizontal(|ui| {
                    ui.button("All Visible").clicked().then(|| {
                        for (_name, tr) in data.traces.traces_iter_mut() {
                            tr.look.visible = true;
                        }
                    });
                    ui.button("All Hidden").clicked().then(|| {
                        for (_name, tr) in data.traces.traces_iter_mut() {
                            tr.look.visible = false;
                        }
                    });
                });

                ui.separator();

                self.hover_trace = None;

                #[derive(Clone)]
                struct Row {
                    name: TraceRef,
                }
                let rows: Vec<Row> = data
                    .traces
                    .all_trace_names()
                    .into_iter()
                    .map(|name| Row { name })
                    .collect();

                struct TracesDelegate<'a> {
                    traces: &'a mut crate::data::traces::TracesCollection,
                    hover_out: &'a mut Option<TraceRef>,
                    look_toggle: &'a mut Option<TraceRef>,
                    drag_out: &'a mut Option<DragPayload>,
                    rows: Vec<Row>,
                }
                impl<'a> TableDelegate for TracesDelegate<'a> {
                    fn header_cell_ui(
                        &mut self,
                        ui: &mut egui::Ui,
                        cell: &egui_table::HeaderCellInfo,
                    ) {
                        let col = cell.col_range.start;
                        let text = match col {
                            0 => "",
                            1 => "",
                            2 => "Trace",
                            3 => "Visible",
                            4 => "Offset",
                            5 => "Info",
                            _ => "",
                        };

                        // Center certain headers; keep Trace/Info left-aligned.
                        let centered_cols = [0usize, 1, 3, 4];
                        if centered_cols.contains(&col) {
                            ui.with_layout(
                                egui::Layout::centered_and_justified(egui::Direction::LeftToRight),
                                |ui| {
                                    ui.strong(text);
                                },
                            );
                        } else {
                            ui.add_space(4.0);
                            ui.strong(text);
                        }
                    }
                    fn cell_ui(&mut self, ui: &mut egui::Ui, cell: &egui_table::CellInfo) {
                        let row = cell.row_nr as usize;
                        let col = cell.col_nr;
                        if row >= self.rows.len() {
                            return;
                        }
                        let r = &self.rows[row];

                        match col {
                            0 => {
                                // Drag handle centered.
                                ui.with_layout(
                                    egui::Layout::centered_and_justified(
                                        egui::Direction::LeftToRight,
                                    ),
                                    |ui| {
                                        let resp = ui
                                            .add(
                                                egui::Label::new(DOTS_SIX_VERTICAL)
                                                    .sense(egui::Sense::click_and_drag()),
                                            )
                                            .on_hover_text("Drag into a scope / XY slot");

                                        if resp.hovered() {
                                            *self.hover_out = Some(r.name.clone());
                                            ui.output_mut(|o| {
                                                o.cursor_icon = egui::CursorIcon::Grab
                                            });
                                        }
                                        if resp.drag_started() || resp.dragged() {
                                            *self.drag_out = Some(DragPayload {
                                                trace: r.name.clone(),
                                                origin_scope_id: None,
                                            });
                                            ui.output_mut(|o| {
                                                o.cursor_icon = egui::CursorIcon::Grabbing
                                            });
                                        }
                                    },
                                );
                            }
                            1 => {
                                // Color editor centered.
                                ui.with_layout(
                                    egui::Layout::centered_and_justified(
                                        egui::Direction::LeftToRight,
                                    ),
                                    |ui| {
                                        if let Some(tr) = self.traces.get_trace_mut(&r.name) {
                                            let mut c = tr.look.color;
                                            let resp = ui
                                                .color_edit_button_srgba(&mut c)
                                                .on_hover_text("Change trace color");
                                            if resp.hovered() {
                                                *self.hover_out = Some(r.name.clone());
                                            }
                                            if resp.changed() {
                                                tr.look.color = c;
                                            }
                                        }
                                    },
                                );
                            }
                            2 => {
                                ui.add_space(4.0);
                                let resp = ui.add(
                                    egui::Label::new(r.name.0.clone())
                                        .truncate()
                                        .show_tooltip_when_elided(true)
                                        .sense(egui::Sense::click_and_drag()),
                                );
                                if resp.hovered() {
                                    *self.hover_out = Some(r.name.clone());
                                }
                                if resp.drag_started() || resp.dragged() {
                                    *self.drag_out = Some(DragPayload {
                                        trace: r.name.clone(),
                                        origin_scope_id: None,
                                    });
                                    ui.output_mut(|o| o.cursor_icon = egui::CursorIcon::Grabbing);
                                }
                                if resp.clicked() {
                                    *self.look_toggle = Some(r.name.clone());
                                }
                            }
                            3 => {
                                // Visible checkbox centered.
                                ui.with_layout(
                                    egui::Layout::centered_and_justified(
                                        egui::Direction::LeftToRight,
                                    ),
                                    |ui| {
                                        if let Some(tr) = self.traces.get_trace_mut(&r.name) {
                                            let mut vis = tr.look.visible;
                                            let resp = ui.checkbox(&mut vis, "");
                                            if resp.hovered() {
                                                *self.hover_out = Some(r.name.clone());
                                            }
                                            if resp.changed() {
                                                tr.look.visible = vis;
                                            }
                                        }
                                    },
                                );
                            }
                            4 => {
                                // Offset DragValue centered.
                                ui.with_layout(
                                    egui::Layout::centered_and_justified(
                                        egui::Direction::LeftToRight,
                                    ),
                                    |ui| {
                                        if let Some(tr) = self.traces.get_trace_mut(&r.name) {
                                            let mut off = tr.offset;
                                            let resp = ui.add(
                                                egui::DragValue::new(&mut off)
                                                    .speed(0.01)
                                                    .range(-1.0e12..=1.0e12),
                                            );
                                            if resp.hovered() {
                                                *self.hover_out = Some(r.name.clone());
                                            }
                                            if resp.changed() {
                                                tr.offset = off;
                                            }
                                        }
                                    },
                                );
                            }
                            5 => {
                                ui.add_space(4.0);
                                if let Some(tr) = self.traces.get_trace(&r.name) {
                                    let text = tr.info.clone();
                                    let resp = ui.add(
                                        egui::Label::new(text.clone())
                                            .truncate()
                                            .show_tooltip_when_elided(true)
                                            .sense(egui::Sense::click()),
                                    );
                                    if resp.hovered() {
                                        *self.hover_out = Some(r.name.clone());
                                    }
                                    if resp.clicked() {
                                        ui.ctx().copy_text(text);
                                    }
                                }
                            }
                            _ => {}
                        }
                    }
                }

                // Auto-size columns, but constrain them with min/max ranges.
                // Columns: Drag, Color, Trace, Visible, Offset, Info.
                let cols = vec![
                    // Drag handle: very compact
                    egui_table::Column::new(18.0).range(egui::Rangef::new(18.0, 18.0)),
                    // Color editor: compact
                    egui_table::Column::new(28.0).range(egui::Rangef::new(22.0, 40.0)),
                    // Trace name: flexible
                    egui_table::Column::new(140.0).range(egui::Rangef::new(80.0, 600.0)),
                    // Visible checkbox: small
                    egui_table::Column::new(50.0).range(egui::Rangef::new(50.0, 50.0)),
                    // Offset: medium
                    egui_table::Column::new(50.0).range(egui::Rangef::new(50.0, 50.0)),
                    // Info: flexible (large max so the table can still fill wide panels)
                    egui_table::Column::new(260.0).range(egui::Rangef::new(100.0, 2000.0)),
                ];
                // Compute a preferred height for the table; size it relative to available height
                let header_h = 24.0_f32;
                let row_h = 22.0_f32;
                let rows_len = rows.len() as f32;
                let editor_open =
                    self.look_editor_trace.is_some() || self.look_editor_xy_pair.is_some();
                let preferred = header_h + row_h * rows_len + 8.0;
                let avail_h = ui.available_height();
                // With the editor placed below the table, give the table a larger share when open.
                let max_h = if editor_open {
                    (avail_h * 0.65).max(200.0)
                } else {
                    (avail_h * 0.85).max(200.0)
                };
                let table_h = preferred.clamp(120.0, max_h);

                // Draw the table first (style editor is rendered below).
                let rows_clone = rows.clone();
                {
                    let mut hover_tmp: Option<TraceRef> = None;
                    let mut look_toggle_req: Option<TraceRef> = None;
                    let mut drag_from_table: Option<DragPayload> = None;
                    // Borrow traces mutably for the table drawing scope only.
                    let traces_ref = &mut *data.traces;
                    let mut delegate = TracesDelegate {
                        traces: traces_ref,
                        hover_out: &mut hover_tmp,
                        look_toggle: &mut look_toggle_req,
                        drag_out: &mut drag_from_table,
                        rows: rows_clone,
                    };

                    let avail_w = ui.available_width();
                    let (rect, _resp) =
                        ui.allocate_exact_size(egui::vec2(avail_w, table_h), egui::Sense::hover());
                    let ui_builder = egui::UiBuilder::new()
                        .max_rect(rect)
                        .layout(egui::Layout::left_to_right(egui::Align::Min));
                    let mut table_ui = ui.new_child(ui_builder);

                    // Avoid text selection/marking during drag gestures inside the table.
                    table_ui.style_mut().interaction.selectable_labels = false;

                    Table::new()
                        .id_salt("traces_table")
                        .auto_size_mode(egui_table::AutoSizeMode::OnParentResize)
                        .num_rows(delegate.rows.len() as u64)
                        .columns(cols)
                        .headers(vec![EgHeaderRow::new(24.0)])
                        .show(&mut table_ui, &mut delegate);

                    // Write back hover and selection state after drawing table.
                    self.hover_trace = hover_tmp;
                    if let Some(dragged) = drag_from_table {
                        self.dragging_trace = Some(dragged);
                    }
                    if let Some(tn) = look_toggle_req {
                        if self.look_editor_trace.as_deref() == Some(tn.as_str()) {
                            self.look_editor_trace = None;
                        } else {
                            self.look_editor_trace = Some(tn);
                            self.hover_trace = None;
                        }
                    }
                }

                // Drag preview for drags originating from the main traces list.
                if let Some(dragging) = self.dragging_trace.clone() {
                    // Store the active drag payload in egui's temp data so scope
                    // panels (rendered later in the central panel) can detect a
                    // drop on their plot area.
                    ui.ctx().data_mut(|d| {
                        d.insert_temp(Id::new("liveplot_active_trace_drag"), dragging.clone());
                    });

                    if let Some(pos) = ui.ctx().pointer_latest_pos() {
                        egui::Area::new(Id::new(("trace_drag_preview", dragging.trace.0.clone())))
                            .order(egui::Order::Foreground)
                            .fixed_pos(pos + egui::vec2(16.0, 16.0))
                            .interactable(false)
                            .show(ui.ctx(), |ui| {
                                egui::Frame::popup(ui.style()).show(ui, |ui| {
                                    ui.horizontal(|ui| {
                                        ui.label(DOTS_SIX_VERTICAL);
                                        ui.label(dragging.trace.0.clone());
                                    });
                                });
                            });
                    }
                } else {
                    // Drag was consumed (e.g. by a drop slot) or released —
                    // remove stale temp data so scope panels don't see it.
                    ui.ctx().data_mut(|d| {
                        d.remove::<DragPayload>(Id::new("liveplot_active_trace_drag"));
                    });
                }

                if let Some(hover_trace) = self.hover_trace.clone() {
                    data.traces.hover_trace = Some(hover_trace.clone());
                }

                if ui.ctx().input(|i| i.pointer.any_released()) {
                    self.dragging_trace = None;
                }
            }); // end ScrollArea
    }
}

impl TracesPanel {
    fn render_scope_assignments(&mut self, ui: &mut Ui, data: &mut LivePlotData<'_>) {
        let can_remove_scope = data.scope_data.len() > 1;
        if ui.button("âž• Add scope").clicked() {
            data.pending_requests.add_scope = true;
        }
        ui.add_space(4.0);
        let mut moved: Vec<(usize, TraceRef)> = Vec::new();
        for scope in data.scope_data.iter_mut() {
            ui.separator();
            ui.add_space(4.0);

            let settings_resp = self.scope_settings_ui.render_scope_assignment(
                ui,
                scope,
                can_remove_scope,
                &mut data.pending_requests,
                &mut self.dragging_trace,
                &mut *data.traces,
                &mut self.look_editor_trace,
                &mut self.look_editor_xy_pair,
            );

            if let Some(m) = settings_resp.moved_from_scope {
                moved.push(m);
            }

            if settings_resp.type_changed {
                match scope.scope_type {
                    ScopeType::TimeScope => scope.fit_y_bounds(&*data.traces),
                    ScopeType::XYScope => scope.fit_bounds(&*data.traces),
                }
            }

            if settings_resp.type_changed || settings_resp.scope_changed {
                ui.ctx().request_repaint();
            }
        }

        if !moved.is_empty() {
            for (src_id, tr) in moved {
                if let Some(src_scope) = data.scope_data.iter_mut().find(|s| s.id == src_id) {
                    src_scope.remove_trace(&tr);
                }
            }
            ui.ctx().request_repaint();
        }
    }
}