liveplot 2.4.0

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
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
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
use crate::data::data::LivePlotData;
use crate::data::math::{FilterKind, FormulaTimeMode, MathKind, MathTrace, MinMaxMode};
use crate::data::traces::TraceRef;
use eframe::egui;
use egui::{Color32, Ui};
use std::collections::HashMap;

use crate::data::trace_look::TraceLook;
use crate::panels::panel_trait::{Panel, PanelState};
use crate::panels::trace_look_ui::render_trace_look_editor;
use egui_phosphor_icons::icons::{BROOM, PLUS, RECYCLE};

#[derive(Debug, Clone)]
pub struct MathPanel {
    state: PanelState,
    builder: MathTrace,
    builder_look: TraceLook,
    editing: Option<TraceRef>,
    error: Option<String>,
    creating: bool,

    math_traces: Vec<MathTrace>,
    /// Persistent copies of the traces referenced by math defs (live pass).
    /// Kept in sync by tail-extension so `update_data` doesn't clone whole
    /// buffers every frame.
    sources: HashMap<TraceRef, Vec<[f64; 2]>>,
    /// Same as [`Self::sources`] but for the snapshot pass while paused.
    snap_sources: HashMap<TraceRef, Vec<[f64; 2]>>,
}

/// Sync `buf` to mirror `data` (an x-sorted point buffer), assuming `buf`
/// previously mirrored `data` and `data` only changed by front-pops (pruning)
/// and back-pushes (streaming). Costs O(popped + appended) instead of a full
/// clone; falls back to a full rebuild when the cheap path can't apply
/// (e.g. wholesale data replacement).
fn sync_source_buffer(buf: &mut Vec<[f64; 2]>, data: &std::collections::VecDeque<[f64; 2]>) {
    if buf.len() == data.len() && buf.first() == data.front() && buf.last() == data.back() {
        return;
    }
    // Index of the first point in `data` not yet mirrored into `buf`.
    let tail_start = match buf.last() {
        Some(&last) => data.partition_point(|p| p[0] <= last[0]),
        None => 0,
    };
    let tail_len = data.len() - tail_start;
    // `data = buf[dropped..] + data[tail_start..]`  ⇒  dropped = buf + tail - data
    let dropped = buf.len() as isize + tail_len as isize - data.len() as isize;
    if dropped >= 0 {
        buf.drain(..dropped as usize);
        buf.extend(data.iter().skip(tail_start).copied());
        // Sanity-check the cheap sync: ends must match. If data changed in a
        // way that isn't "pop front / push back" (SetData, clear+refill),
        // fall back to a full copy.
        if buf.first() == data.front() && buf.last() == data.back() {
            return;
        }
    }
    buf.clear();
    buf.extend(data.iter().copied());
}

impl Default for MathPanel {
    fn default() -> Self {
        Self {
            state: PanelState::new("Math", "∫"), // split into icon + title
            builder: MathTrace::new(TraceRef::default(), MathKind::Add { inputs: Vec::new() }),
            builder_look: TraceLook::default(),
            editing: None,
            error: None,
            creating: false,

            math_traces: Vec::new(),
            sources: HashMap::new(),
            snap_sources: HashMap::new(),
        }
    }
}

impl Panel for MathPanel {
    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::Math)
    }

    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 Math").clicked() {
                    let st = self.state_mut();
                    st.visible = true;
                    st.request_focus = true;
                    ui.close();
                }

                ui.separator();

                if ui.button(format!("{} New", PLUS.as_str())).clicked() {
                    self.builder =
                        MathTrace::new(TraceRef::default(), MathKind::Add { inputs: Vec::new() });
                    self.editing = None;
                    self.creating = true;
                    self.error = None;
                    let st = self.state_mut();
                    st.visible = true;
                    st.request_focus = true;
                    ui.close();
                }
                if ui
                    .button(format!("{} Reset All Storage", RECYCLE.as_str()))
                    .clicked()
                {
                    for def in self.math_traces.iter_mut() {
                        data.traces.clear_trace(&def.name);
                        def.reset_runtime_state();
                    }
                    ui.close();
                }
                if ui.button(format!("{} Clear All", BROOM.as_str())).clicked() {
                    // Remove math traces & their underlying live data
                    for def in self.math_traces.iter() {
                        // Emit MATH_TRACE_REMOVED for each
                        if let Some(ctrl) = &data.event_ctrl {
                            let mut evt = crate::events::PlotEvent::new(
                                crate::events::EventKind::MATH_TRACE_REMOVED,
                            );
                            evt.math_trace = Some(crate::events::MathTraceMeta {
                                name: def.name.0.clone(),
                                formula: None,
                            });
                            ctrl.emit_filtered(evt);
                        }
                        data.traces.remove_trace(&def.name);
                    }
                    self.math_traces.clear();
                    self.editing = None;
                    self.creating = false;
                    ui.close();
                }
            });
        if !tooltip.is_empty() {
            mr.0.on_hover_text(tooltip);
        }
    }

    fn update_data(&mut self, data: &mut LivePlotData<'_>) {
        if data.pending_requests.clear_math {
            for def in self.math_traces.iter_mut() {
                data.traces.clear_trace(&def.name);
                def.reset_runtime_state();
            }
            data.pending_requests.clear_math = false;
        }

        if self.math_traces.is_empty() {
            // No defs → the persistent buffers will never be read; free them.
            if !self.sources.is_empty() || !self.snap_sources.is_empty() {
                self.sources.clear();
                self.snap_sources.clear();
            }
            return;
        }

        // Collect only the traces that are actually referenced as inputs by
        // any math trace definition, plus the math traces' own previous output.
        let mut needed: std::collections::HashSet<TraceRef> = std::collections::HashSet::new();
        for def in &self.math_traces {
            let inputs = def.input_trace_names();
            // A formula without trace references (e.g. `sin(2*pi*t)`) still
            // needs a time grid — feed every existing trace into the sources
            // map so the union of timestamps spans the timeline.
            if inputs.is_empty() && matches!(def.kind, MathKind::Formula { .. }) {
                needed.extend(data.traces.all_trace_names());
            }
            needed.extend(inputs);
            needed.insert(def.name.clone());
        }

        // ── Live data pass ───────────────────────────────────────────────
        // Sync the persistent source copies instead of cloning whole buffers
        // every frame; `sync_source_buffer` only touches points that were
        // pruned at the front or appended at the back.
        self.sources.retain(|name, _| needed.contains(name));
        for (name, tr) in data.traces.traces_iter() {
            if needed.contains(name) {
                let buf = self.sources.entry(name.clone()).or_default();
                sync_source_buffer(buf, &tr.live);
            }
        }

        for def in self.math_traces.iter_mut() {
            let out = def.compute_math_trace(&self.sources);

            let tr = data.get_trace_or_new(&def.name);
            tr.set_live_points(&out);
            tr.info = def.math_formula_string();

            self.sources.insert(def.name.clone(), out);
        }

        // ── Snapshot data pass ───────────────────────────────────────────
        // Only run while a snapshot exists (i.e. a scope is paused). Writing
        // `snap` unconditionally would keep `TracesCollection::has_snapshot`
        // permanently true for math traces and make the render caches track a
        // stale buffer instead of `live`.
        if !data.traces.has_snapshot() {
            return;
        }
        self.snap_sources.retain(|name, _| needed.contains(name));
        for (name, tr) in data.traces.traces_iter() {
            if needed.contains(name) {
                if let Some(d) = tr.snap.as_ref() {
                    let buf = self.snap_sources.entry(name.clone()).or_default();
                    sync_source_buffer(buf, d);
                } else {
                    self.snap_sources.remove(name);
                }
            }
        }

        for def in self.math_traces.iter_mut() {
            let out = def.compute_math_trace(&self.snap_sources);

            let tr = data.get_trace_or_new(&def.name);
            tr.set_snap_points(&out);
            tr.info = def.math_formula_string();

            self.snap_sources.insert(def.name.clone(), out);
        }
    }

    fn render_panel(&mut self, ui: &mut Ui, data: &mut LivePlotData<'_>) {
        ui.label("Create virtual traces from existing ones.");
        if let Some(err) = &self.error {
            ui.colored_label(Color32::LIGHT_RED, err);
        }

        ui.separator();
        // Global storage reset for all stateful math traces
        ui.horizontal(|ui| {
            if ui
                .button(format!("{} Reset All Storage", RECYCLE.as_str()))
                .on_hover_text("Reset integrators, filters, min/max for all math traces")
                .clicked()
            {
                for def in self.math_traces.iter_mut() {
                    data.traces.clear_trace(&def.name);
                    def.reset_runtime_state();
                }
            }
        });
        ui.add_space(6.0);
        // Existing math traces list with color editor, name, info, and Remove (right-aligned)
        // Reset hover before drawing; rows will set it when hovered

        let mut hover_trace_intern: Option<Vec<TraceRef>> = None;
        // Set when a formula trace's reset button is clicked — the def being
        // iterated is a clone, so the origin update is applied afterwards.
        let mut pending_formula_reset: Option<TraceRef> = None;
        for def in self.math_traces.clone().iter_mut() {
            let row = ui.horizontal(|ui| {
                // Color editor like in traces_ui
                if let Some(tr) = data.traces.get_trace_mut(&def.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() {
                        hover_trace_intern = Some(vec![def.name.clone()]);
                    }
                    if resp.changed() {
                        tr.look.color = c;
                    }
                } else {
                    ui.label("");
                }

                // Name (click to edit)
                let name_resp = ui.add(
                    egui::Label::new(def.name.0.clone())
                        .truncate()
                        .show_tooltip_when_elided(true)
                        .sense(egui::Sense::click()),
                );
                if name_resp.hovered() {
                    hover_trace_intern = Some(vec![def.name.clone()]);
                }
                if name_resp.clicked() {
                    self.builder = def.clone();
                    self.editing = Some(def.name.clone());
                    self.error = None;
                    self.creating = false;
                }

                // Info string (formula) - clickable to edit
                let info_text = if let Some(tr) = data.traces.get_trace(&def.name) {
                    tr.info.clone()
                } else {
                    String::new()
                };
                let info_resp = ui.add(
                    egui::Label::new(info_text)
                        .truncate()
                        .show_tooltip_when_elided(true)
                        .sense(egui::Sense::click()),
                );
                if info_resp.hovered() {
                    hover_trace_intern = Some(vec![def.name.clone()]);
                }
                if info_resp.clicked() {
                    self.builder = def.clone();
                    self.editing = Some(def.name.clone());
                    self.error = None;
                    self.creating = false;
                }
                // Right-aligned per-trace actions: Reset and Remove
                ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
                    // Remove button with hover highlight
                    let remove_resp = ui
                        .button(egui_phosphor_icons::icons::TRASH)
                        .on_hover_text("Remove");
                    if remove_resp.hovered() {
                        hover_trace_intern = Some(vec![def.name.clone()]);
                    }
                    if remove_resp.clicked() {
                        let removing = def.name.clone();
                        // Emit MATH_TRACE_REMOVED
                        if let Some(ctrl) = &data.event_ctrl {
                            let mut evt = crate::events::PlotEvent::new(
                                crate::events::EventKind::MATH_TRACE_REMOVED,
                            );
                            evt.math_trace = Some(crate::events::MathTraceMeta {
                                name: removing.0.clone(),
                                formula: None,
                            });
                            ctrl.emit_filtered(evt);
                        }
                        data.remove_trace(&removing);
                        self.math_traces.retain(|d| d.name != removing);
                        if self.editing.as_deref() == Some(&removing) {
                            self.editing = None;
                            self.creating = false;
                            self.builder = MathTrace::new(
                                TraceRef::default(),
                                MathKind::Add { inputs: Vec::new() },
                            );
                            self.builder_look = TraceLook::default();
                            self.error = None;
                        }
                    }
                    // Show Reset for kinds that have internal storage, for
                    // formula traces in resettable-t mode (moves t=0 to the
                    // newest sample, like the integrator reset), and for
                    // formulas using `minh`/`maxh` (running extremes, like the
                    // MinMax kind).
                    let is_stateful = matches!(
                        def.kind,
                        MathKind::Integrate { .. }
                            | MathKind::Filter { .. }
                            | MathKind::MinMax { .. }
                    );
                    let resettable_t = matches!(def.kind, MathKind::Formula { .. })
                        && matches!(def.time_mode, FormulaTimeMode::Resettable);
                    let formula_hist = matches!(&def.kind, MathKind::Formula { expr } if {
                        crate::data::expr::parse(expr)
                            .map(|ast| ast.uses_history())
                            .unwrap_or(false)
                    });
                    if is_stateful || resettable_t || formula_hist {
                        let reset_resp = ui
                            .button(egui_phosphor_icons::icons::ARROW_CLOCKWISE)
                            .on_hover_text(if resettable_t {
                                "Reset t to 0 at the newest sample and clear running min/max history"
                            } else if formula_hist {
                                "Reset running min/max history for this trace"
                            } else {
                                "Reset integrator/filter/min/max state for this trace"
                            });
                        if reset_resp.hovered() {
                            hover_trace_intern = Some(vec![def.name.clone()]);
                        }
                        if reset_resp.clicked() {
                            if resettable_t || formula_hist {
                                pending_formula_reset = Some(def.name.clone());
                            }
                            data.traces.clear_trace(&def.name);
                        }
                    }
                });
            });
            if row.response.hovered() {
                hover_trace_intern = Some(vec![def.name.clone()]);
            }
        }
        if let Some(nm) = hover_trace_intern {
            data.traces.hover_trace = Some(nm);
        }
        // Apply a formula trace reset: clear the runtime state (minh/maxh
        // accumulators + t origin), then — for resettable-t formulas — move
        // the origin to the newest sample (or leave None → auto-init at the
        // first sample when no data exists).
        if let Some(name) = pending_formula_reset {
            if let Some(d) = self.math_traces.iter_mut().find(|d| d.name == name) {
                d.reset_runtime_state();
                if matches!(d.time_mode, FormulaTimeMode::Resettable) {
                    d.t_origin = data
                        .traces
                        .traces_iter()
                        .filter_map(|(_, tr)| tr.live.back().map(|p| p[0]))
                        .reduce(f64::max);
                }
            }
        }

        // Style popup removed; the editor is part of the new/edit dialog above

        // Full-width New button after the list
        ui.add_space(6.0);
        let new_clicked = ui
            .add_sized(
                [ui.available_width(), 24.0],
                egui::Button::new(format!("{} New", PLUS.as_str())),
            )
            .on_hover_text("Create a new math trace")
            .clicked();
        if new_clicked {
            self.builder =
                MathTrace::new(TraceRef::default(), MathKind::Add { inputs: Vec::new() });
            self.editing = None;
            self.error = None;
            self.creating = true;
            self.builder_look = TraceLook::default();
        }

        // Settings panel (hidden unless creating or editing)
        let is_editing = self.editing.is_some();
        let is_creating = self.creating;
        if is_editing || is_creating {
            ui.add_space(12.0);
            ui.separator();
            if is_editing {
                ui.strong("Edit math trace");
            } else {
                ui.strong("New math trace");
            }
            // Name first, then Operation (no label; tooltip on combobox)
            // Duplicate name when creating, or when editing and changing to an existing different name
            let duplicate_name = {
                let same_as_editing = self.editing.as_deref() == Some(self.builder.name.as_str());
                let exists_in_math = self.math_traces.iter().any(|d| d.name == self.builder.name);
                let exists_in_data = data.traces.contains_key(&self.builder.name);
                (exists_in_math || exists_in_data)
                    && !same_as_editing
                    && !self.builder.name.is_empty()
            };

            ui.horizontal(|ui| {
                ui.label("Name");
                if duplicate_name {
                    egui::Frame::default()
                        .stroke(egui::Stroke::new(1.5_f32, egui::Color32::RED))
                        .show(ui, |ui| {
                            let resp = ui.add(egui::TextEdit::singleline(&mut self.builder.name.0));
                            let _ = resp.on_hover_text(
                                "A trace with this name already exists. Please choose another.",
                            );
                        });
                } else {
                    let resp = ui.add(egui::TextEdit::singleline(&mut self.builder.name.0));
                    let _ = resp.on_hover_text("Enter a unique name for this trace");
                }
            });
            // Operation selection
            let kinds = [
                "Add/Subtract",
                "Multiply",
                "Divide",
                "Differentiate",
                "Integrate",
                "Filter",
                "Min",
                "Max",
                "Formula",
            ];
            let mut kind_idx: usize = match &self.builder.kind {
                MathKind::Add { .. } => 0,
                MathKind::Multiply { .. } => 1,
                MathKind::Divide { .. } => 2,
                MathKind::Differentiate { .. } => 3,
                MathKind::Integrate { .. } => 4,
                MathKind::Filter { .. } => 5,
                MathKind::MinMax { mode, .. } => match mode {
                    MinMaxMode::Min => 6,
                    MinMaxMode::Max => 7,
                },
                MathKind::Formula { .. } => 8,
            };

            let prev_kind_idx = kind_idx;
            let ir = egui::ComboBox::from_id_salt("math_op")
                .selected_text(kinds[kind_idx])
                .show_ui(ui, |ui| {
                    for (i, k) in kinds.iter().enumerate() {
                        ui.selectable_value(&mut kind_idx, i, *k);
                    }
                });
            ir.response.on_hover_text("Operation");
            // Available source names, excluding the math trace's own name to avoid self-references
            let trace_names: Vec<TraceRef> = data
                .traces
                .all_trace_names()
                .into_iter()
                .filter(|n| *n != self.builder.name)
                .collect();

            if kind_idx != prev_kind_idx {
                // Switch to a new kind with sensible defaults
                let first = trace_names.first().cloned().unwrap_or_default();
                let second = trace_names.get(1).cloned().unwrap_or_else(|| first.clone());
                self.builder.kind = match kind_idx {
                    0 => MathKind::Add {
                        inputs: vec![(first.clone(), 1.0), (second.clone(), 1.0)],
                    },
                    1 => MathKind::Multiply {
                        a: first.clone(),
                        b: second.clone(),
                    },
                    2 => MathKind::Divide {
                        a: first.clone(),
                        b: second.clone(),
                    },
                    3 => MathKind::Differentiate {
                        input: first.clone(),
                    },
                    4 => MathKind::Integrate {
                        input: first.clone(),
                        y0: 0.0,
                    },
                    5 => MathKind::Filter {
                        input: first.clone(),
                        kind: FilterKind::Lowpass { cutoff_hz: 1.0 },
                    },
                    6 => MathKind::MinMax {
                        input: first.clone(),
                        decay_per_sec: Some(0.0),
                        mode: MinMaxMode::Min,
                    },
                    7 => MathKind::MinMax {
                        input: first.clone(),
                        decay_per_sec: Some(0.0),
                        mode: MinMaxMode::Max,
                    },
                    8 => MathKind::Formula {
                        expr: String::new(),
                    },
                    _ => MathKind::Add { inputs: vec![] },
                };
            }

            // Initialize builder look color if blank name changed to a new one (use palette color based on future index)
            // Compute default color index for this potential new trace name
            if is_creating {
                let future_idx = if data.traces.contains_key(&self.builder.name) {
                    None
                } else {
                    Some(data.traces.len())
                };
                if let Some(idx) = future_idx {
                    self.builder_look.color = TraceLook::alloc_color(idx);
                }
            }

            match &mut self.builder.kind {
                MathKind::Add { inputs } => {
                    // Ensure at least one input row for UX
                    if inputs.is_empty() {
                        if let Some(nm) = trace_names.first() {
                            inputs.push((nm.clone(), 1.0));
                        }
                    }
                    // Draw rows
                    for (idx, (trace_ref, gain)) in inputs.iter_mut().enumerate() {
                        let current_name = trace_ref.0.clone();
                        ui.horizontal(|ui| {
                            let mut selected = current_name.clone();
                            egui::ComboBox::from_id_salt(format!("add_sel_{}", idx))
                                .selected_text(selected.clone())
                                .show_ui(ui, |ui| {
                                    for n in trace_names.iter() {
                                        ui.selectable_value(
                                            &mut selected,
                                            n.0.clone(),
                                            n.0.clone(),
                                        );
                                    }
                                });
                            if selected != current_name {
                                *trace_ref = TraceRef(selected);
                            }
                            ui.label("gain");
                            ui.add(egui::DragValue::new(gain).speed(0.1));
                        });
                    }
                    ui.horizontal(|ui| {
                        if ui.button(format!("{} Add input", PLUS.as_str())).clicked() {
                            let nm = trace_names.first().cloned().unwrap_or_default();
                            inputs.push((nm, 1.0));
                        }
                        if ui.button("Remove input").clicked() && inputs.len() > 1 {
                            inputs.pop();
                        }
                    });
                }
                MathKind::Multiply { a, b } | MathKind::Divide { a, b } => {
                    ui.horizontal(|ui| {
                        let mut sel_a = a.0.clone();
                        egui::ComboBox::from_label("A")
                            .selected_text(sel_a.clone())
                            .show_ui(ui, |ui| {
                                for n in trace_names.iter() {
                                    ui.selectable_value(&mut sel_a, n.0.clone(), n.0.clone());
                                }
                            });
                        if sel_a != a.0 {
                            a.0 = sel_a;
                        }
                        let mut sel_b = b.0.clone();
                        egui::ComboBox::from_label("B")
                            .selected_text(sel_b.clone())
                            .show_ui(ui, |ui| {
                                for n in trace_names.iter() {
                                    ui.selectable_value(&mut sel_b, n.0.clone(), n.0.clone());
                                }
                            });
                        if sel_b != b.0 {
                            b.0 = sel_b;
                        }
                    });
                }
                MathKind::Differentiate { input } => {
                    let mut sel = input.0.clone();
                    egui::ComboBox::from_label("Input")
                        .selected_text(sel.clone())
                        .show_ui(ui, |ui| {
                            for n in trace_names.iter() {
                                ui.selectable_value(&mut sel, n.0.clone(), n.0.clone());
                            }
                        });
                    if sel != input.0 {
                        input.0 = sel;
                    }
                }
                MathKind::Integrate { input, y0 } => {
                    let mut sel = input.0.clone();
                    egui::ComboBox::from_label("Input")
                        .selected_text(sel.clone())
                        .show_ui(ui, |ui| {
                            for n in trace_names.iter() {
                                ui.selectable_value(&mut sel, n.0.clone(), n.0.clone());
                            }
                        });
                    if sel != input.0 {
                        input.0 = sel;
                    }
                    ui.horizontal(|ui| {
                        ui.label("y0");
                        ui.add(egui::DragValue::new(y0).speed(0.1));
                    });
                }
                MathKind::Filter { input, kind } => {
                    let mut sel = input.0.clone();
                    egui::ComboBox::from_label("Input")
                        .selected_text(sel.clone())
                        .show_ui(ui, |ui| {
                            for n in trace_names.iter() {
                                ui.selectable_value(&mut sel, n.0.clone(), n.0.clone());
                            }
                        });
                    if sel != input.0 {
                        input.0 = sel;
                    }
                    // Map kind to index and editable params
                    let fk = [
                        "Lowpass (1st)",
                        "Highpass (1st)",
                        "Bandpass (1st)",
                        "Biquad LP",
                        "Biquad HP",
                        "Biquad BP",
                    ];
                    let mut which: usize = match kind {
                        FilterKind::Lowpass { .. } => 0,
                        FilterKind::Highpass { .. } => 1,
                        FilterKind::Bandpass { .. } => 2,
                        FilterKind::BiquadLowpass { .. } => 3,
                        FilterKind::BiquadHighpass { .. } => 4,
                        FilterKind::BiquadBandpass { .. } => 5,
                        FilterKind::Custom { .. } => 0,
                    };
                    let (mut f1, mut f2, mut q) = match kind {
                        FilterKind::Lowpass { cutoff_hz } => (*cutoff_hz, 0.0, 0.707),
                        FilterKind::Highpass { cutoff_hz } => (*cutoff_hz, 0.0, 0.707),
                        FilterKind::Bandpass {
                            low_cut_hz,
                            high_cut_hz,
                        } => (*low_cut_hz, *high_cut_hz, 0.707),
                        FilterKind::BiquadLowpass { cutoff_hz, q } => (*cutoff_hz, 0.0, *q),
                        FilterKind::BiquadHighpass { cutoff_hz, q } => (*cutoff_hz, 0.0, *q),
                        FilterKind::BiquadBandpass { center_hz, q } => (*center_hz, 0.0, *q),
                        FilterKind::Custom { params: _ } => (1.0, 0.0, 0.707),
                    };
                    egui::ComboBox::from_label("Filter")
                        .selected_text(fk[which])
                        .show_ui(ui, |ui| {
                            for (i, n) in fk.iter().enumerate() {
                                ui.selectable_value(&mut which, i, *n);
                            }
                        });
                    match which {
                        0 | 1 => {
                            ui.horizontal(|ui| {
                                ui.label("Cutoff Hz");
                                ui.add(egui::DragValue::new(&mut f1).speed(0.1));
                            });
                        }
                        2 => {
                            ui.horizontal(|ui| {
                                ui.label("Low cut Hz");
                                ui.add(egui::DragValue::new(&mut f1).speed(0.1));
                            });
                            ui.horizontal(|ui| {
                                ui.label("High cut Hz");
                                ui.add(egui::DragValue::new(&mut f2).speed(0.1));
                            });
                        }
                        3..=5 => {
                            let label = if which == 5 { "Center Hz" } else { "Cutoff Hz" };
                            ui.horizontal(|ui| {
                                ui.label(label);
                                ui.add(egui::DragValue::new(&mut f1).speed(0.1));
                            });
                            ui.horizontal(|ui| {
                                ui.label("Q");
                                ui.add(egui::DragValue::new(&mut q).speed(0.01));
                            });
                        }
                        _ => {}
                    }
                    // Write back updated kind
                    *kind = match which {
                        0 => FilterKind::Lowpass { cutoff_hz: f1 },
                        1 => FilterKind::Highpass { cutoff_hz: f1 },
                        2 => FilterKind::Bandpass {
                            low_cut_hz: f1,
                            high_cut_hz: f2,
                        },
                        3 => FilterKind::BiquadLowpass { cutoff_hz: f1, q },
                        4 => FilterKind::BiquadHighpass { cutoff_hz: f1, q },
                        5 => FilterKind::BiquadBandpass { center_hz: f1, q },
                        _ => FilterKind::Lowpass { cutoff_hz: f1 },
                    };
                }
                MathKind::MinMax {
                    input,
                    decay_per_sec,
                    mode: _,
                } => {
                    let mut sel = input.0.clone();
                    egui::ComboBox::from_label("Input")
                        .selected_text(sel.clone())
                        .show_ui(ui, |ui| {
                            for n in trace_names.iter() {
                                ui.selectable_value(&mut sel, n.0.clone(), n.0.clone());
                            }
                        });
                    if sel != input.0 {
                        input.0 = sel;
                    }
                    ui.horizontal(|ui| {
                        ui.label("Decay (1/s, 0=none)");
                        let mut decay = decay_per_sec.unwrap_or(0.0);
                        ui.add(egui::DragValue::new(&mut decay).speed(0.1));
                        *decay_per_sec = Some(decay);
                    });
                }
                MathKind::Formula { expr } => {
                    // Insert chips: one per available trace (in trace color),
                    // plus t / pi / e and a function menu. Clicking inserts at
                    // the text cursor of the formula edit below.
                    let edit_id = egui::Id::new("math_formula_edit");
                    let mut pending_insert: Option<(String, usize)> = None;
                    ui.horizontal_wrapped(|ui| {
                        for n in &trace_names {
                            let color = data
                                .traces
                                .get_trace(n)
                                .map(|t| t.look.color)
                                .unwrap_or_else(|| ui.visuals().text_color());
                            if ui
                                .button(egui::RichText::new(n.0.clone()).color(color))
                                .on_hover_text(format!("Insert {{{}}}", n.0))
                                .clicked()
                            {
                                pending_insert = Some((format!("{{{}}}", n.0), 0));
                            }
                        }
                        for (label, snip) in [("t", "t"), ("π", "pi"), ("e", "e")] {
                            if ui
                                .button(label)
                                .on_hover_text(format!("Insert {snip}"))
                                .clicked()
                            {
                                pending_insert = Some((snip.to_string(), 0));
                            }
                        }
                        let menu_cfg = egui::containers::menu::MenuConfig::new()
                            .close_behavior(egui::PopupCloseBehavior::CloseOnClickOutside);
                        let mb = egui::containers::menu::MenuButton::new("ƒx")
                            .config(menu_cfg)
                            .ui(ui, |ui| {
                                for f in crate::data::expr::Func::ALL {
                                    if ui.button(f.name()).clicked() {
                                        // `name()` with the cursor between the
                                        // parens (one char back from the end).
                                        pending_insert = Some((format!("{}()", f.name()), 1));
                                        ui.close();
                                    }
                                }
                            });
                        mb.0.on_hover_text("Insert function");
                    });

                    // `t` semantics for this formula: absolute timestamp vs.
                    // seconds since a resettable origin (per-trace state on
                    // the builder; applied to the trace on save).
                    ui.horizontal(|ui| {
                        ui.label("t:");
                        ui.selectable_value(
                            &mut self.builder.time_mode,
                            FormulaTimeMode::Absolute,
                            "absolute",
                        )
                        .on_hover_text("t = absolute timestamp in seconds");
                        ui.selectable_value(
                            &mut self.builder.time_mode,
                            FormulaTimeMode::Resettable,
                            "since reset",
                        )
                        .on_hover_text("t = seconds since reset (auto-starts at the first sample)");
                    });

                    // Syntax-highlight {trace} refs in the trace's color.
                    let edit_colors: HashMap<String, Color32> = data
                        .traces
                        .traces_iter()
                        .map(|(n, tr)| (n.0.clone(), tr.look.color))
                        .collect();
                    let edit_font = egui::FontSelection::Default.resolve(ui.style());
                    let edit_text_color = ui
                        .visuals()
                        .override_text_color
                        .unwrap_or_else(|| ui.visuals().widgets.inactive.text_color());
                    let edit_line_height = ui.fonts_mut(|f| f.row_height(&edit_font))
                        + ui.spacing().extra_text_line_spacing;
                    let mut layouter = formula_layouter(
                        &edit_colors,
                        edit_font,
                        edit_text_color,
                        edit_line_height,
                    );
                    let output = egui::TextEdit::multiline(expr)
                        .id(edit_id)
                        .desired_rows(2)
                        .desired_width(f32::INFINITY)
                        .hint_text("e.g. sqrt({a}^2 + {b}^2)")
                        .layouter(&mut layouter)
                        .show(ui);
                    if let Some((snippet, back)) = pending_insert.take() {
                        insert_snippet(ui.ctx(), edit_id, expr, &snippet, back);
                        output.response.request_focus();
                    }

                    // Live preview / error display
                    if !expr.trim().is_empty() {
                        match crate::data::expr::parse(expr) {
                            Ok(ast) => {
                                let color_map: HashMap<TraceRef, Color32> = data
                                    .traces
                                    .traces_iter()
                                    .map(|(n, tr)| (n.clone(), tr.look.color))
                                    .collect();
                                egui::Frame::default()
                                    .stroke(ui.visuals().widgets.noninteractive.bg_stroke)
                                    .inner_margin(egui::Margin::same(4))
                                    .show(ui, |ui| {
                                        crate::panels::formula_render::show_formula(
                                            ui,
                                            &ast,
                                            &color_map,
                                            ui.visuals().text_color(),
                                        );
                                    });
                                let unknown: Vec<String> = ast
                                    .referenced_traces()
                                    .into_iter()
                                    .filter(|n| !data.traces.contains_key(n))
                                    .map(|n| n.0)
                                    .collect();
                                if !unknown.is_empty() {
                                    ui.colored_label(
                                        Color32::YELLOW,
                                        format!("unknown trace(s): {}", unknown.join(", ")),
                                    );
                                }
                            }
                            Err(e) => {
                                ui.colored_label(Color32::LIGHT_RED, e.to_string());
                            }
                        }
                    }
                }
            }

            // Unified Style and Save section
            ui.add_space(8.0);
            egui::CollapsingHeader::new("Style")
                .default_open(false)
                .show(ui, |ui| {
                    if is_editing {
                        if let Some(editing_name) = self.editing.clone() {
                            if let Some(tr) = data.traces.get_trace_mut(&editing_name) {
                                render_trace_look_editor(&mut tr.look, ui, true);
                            } else {
                                ui.label("Trace not found.");
                            }
                        }
                    } else {
                        render_trace_look_editor(&mut self.builder_look, ui, true);
                    }
                });

            ui.horizontal(|ui| {
                let save_label = if is_editing {
                    "Save".to_string()
                } else {
                    format!("{} Add trace", PLUS.as_str())
                };
                let formula_ok = match &self.builder.kind {
                    MathKind::Formula { expr } => {
                        !expr.trim().is_empty() && crate::data::expr::parse(expr).is_ok()
                    }
                    _ => true,
                };
                let can_save = !self.builder.name.0.is_empty() && !duplicate_name && formula_ok;
                // Don't treat Enter as Save while typing a multiline formula —
                // it should insert a newline instead.
                let formula_has_focus = matches!(self.builder.kind, MathKind::Formula { .. })
                    && ui
                        .ctx()
                        .memory(|m| m.has_focus(egui::Id::new("math_formula_edit")));
                let enter_pressed = can_save
                    && !formula_has_focus
                    && ui.ctx().input(|i| i.key_pressed(egui::Key::Enter));
                if ui
                    .add_enabled(can_save, egui::Button::new(save_label))
                    .clicked()
                    || enter_pressed
                {
                    // Handle save: builder already holds the full MathTrace
                    let mut tr = self.builder.clone();
                    if self.error.is_none() {
                        if !is_creating {
                            // Preserve look if renaming and replace in-place to keep position
                            let mut prev_look: Option<TraceLook> = None;
                            let mut replace_idx: Option<usize> = None;

                            if let Some(orig) = self.editing.clone() {
                                replace_idx = self.math_traces.iter().position(|d| d.name == orig);

                                // A changed formula invalidates the runtime
                                // state (t origin, `minh`/`maxh` accumulator
                                // slots are indexed by AST position) — reset it
                                // on the saved copy. Look-only edits keep it.
                                if let Some(i) = replace_idx {
                                    if let (
                                        MathKind::Formula { expr: old },
                                        MathKind::Formula { expr: new },
                                    ) = (&self.math_traces[i].kind, &tr.kind)
                                    {
                                        if old != new {
                                            tr.reset_runtime_state();
                                        }
                                    }
                                }

                                if orig != tr.name {
                                    // Grab previous look, then remove the old backing trace
                                    prev_look =
                                        data.traces.get_trace(&orig).map(|t| t.look.clone());
                                    data.remove_trace(&orig);
                                } else {
                                    // Keep current look when not renaming
                                    prev_look =
                                        data.traces.get_trace(&orig).map(|t| t.look.clone());
                                }
                            }

                            // Ensure backing trace exists and carry over look/info
                            let trace = data.get_trace_or_new(&tr.name);
                            if let Some(l) = prev_look {
                                trace.look = l;
                            }
                            trace.info = tr.math_formula_string();
                            trace.clear_all();

                            // Replace the math trace at the same index (keep position)
                            if let Some(i) = replace_idx {
                                self.math_traces[i] = tr.clone();
                            } else {
                                // Fallback (shouldn't happen), keep previous behavior
                                self.math_traces.push(tr.clone());
                            }
                        } else {
                            // Creating new
                            let trace = data.get_trace_or_new(&tr.name);
                            trace.look = self.builder_look.clone();
                            trace.info = tr.math_formula_string();
                            self.math_traces.push(tr.clone());
                            // Emit MATH_TRACE_ADDED
                            if let Some(ctrl) = &data.event_ctrl {
                                let mut evt = crate::events::PlotEvent::new(
                                    crate::events::EventKind::MATH_TRACE_ADDED,
                                );
                                evt.math_trace = Some(crate::events::MathTraceMeta {
                                    name: tr.name.0.clone(),
                                    formula: Some(tr.math_formula_string()),
                                });
                                ctrl.emit_filtered(evt);
                            }
                        }
                        self.editing = None;
                        self.creating = false;
                        self.builder = MathTrace::new(
                            TraceRef::default(),
                            MathKind::Add { inputs: Vec::new() },
                        );
                        self.builder_look = TraceLook::default();
                        self.error = None;
                    }
                }

                ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
                    if ui.button("Cancel").clicked() {
                        self.editing = None;
                        self.creating = false;
                        self.builder = MathTrace::new(
                            TraceRef::default(),
                            MathKind::Add { inputs: Vec::new() },
                        );
                        self.builder_look = TraceLook::default();
                        self.error = None;
                    }
                });
            });
        }
    }

    fn settings_snapshot(&self, data: &LivePlotData<'_>) -> Option<String> {
        let looks: Vec<(String, crate::persistence::TraceLookSerde)> = self
            .math_traces
            .iter()
            .filter_map(|mt| {
                data.traces.get_trace(&mt.name).map(|tr| {
                    (
                        mt.name.0.clone(),
                        crate::persistence::TraceLookSerde::from(&tr.look),
                    )
                })
            })
            .collect();
        serde_json::to_string(&(self.math_traces.clone(), looks)).ok()
    }
}

// Public helpers for persistence/state management
impl MathPanel {
    pub fn get_math_traces(&self) -> &Vec<crate::data::math::MathTrace> {
        &self.math_traces
    }
    pub fn set_math_traces(&mut self, v: Vec<crate::data::math::MathTrace>) {
        self.math_traces = v;
    }
}

/// Insert `snippet` into `expr` at the cursor of the `TextEdit` identified by
/// `edit_id`, replacing any active selection. The cursor is placed `back`
/// characters before the end of the inserted text (e.g. `back = 1` lands inside
/// the parens of an inserted `f()`).
fn insert_snippet(
    ctx: &egui::Context,
    edit_id: egui::Id,
    expr: &mut String,
    snippet: &str,
    back: usize,
) {
    let char_to_byte = |ci: usize| -> usize {
        expr.char_indices()
            .nth(ci)
            .map(|(i, _)| i)
            .unwrap_or(expr.len())
    };
    let mut state = egui::TextEdit::load_state(ctx, edit_id).unwrap_or_default();
    let end = expr.chars().count();
    let (lo, hi) = state
        .cursor
        .char_range()
        .map(|r| {
            (
                r.primary.index.0.min(r.secondary.index.0).min(end),
                r.primary.index.0.max(r.secondary.index.0).min(end),
            )
        })
        .unwrap_or((end, end));
    expr.replace_range(char_to_byte(lo)..char_to_byte(hi), snippet);
    let new_pos = lo + snippet.chars().count().saturating_sub(back);
    let c = egui::text::CCursor::new(new_pos);
    state
        .cursor
        .set_char_range(Some(egui::text::CCursorRange::two(c, c)));
    egui::TextEdit::store_state(ctx, edit_id, state);
}

/// Custom [`egui::TextEdit`] layouter for the formula editor: renders
/// `{trace}` references in the trace's color, everything else in the default
/// text color. Mirrors `TextEdit`'s default multiline layouter (font,
/// `line_height`, wrapping) so the text looks identical apart from colors.
fn formula_layouter<'a>(
    trace_colors: &'a HashMap<String, Color32>,
    font_id: egui::FontId,
    text_color: Color32,
    line_height: f32,
) -> impl FnMut(&Ui, &dyn egui::TextBuffer, f32) -> std::sync::Arc<egui::Galley> + 'a {
    move |ui, buf, wrap_width| {
        let text = buf.as_str();
        let mut job = egui::text::LayoutJob::default();
        job.wrap.max_width = wrap_width;
        job.break_on_newline = true;
        job.keep_trailing_whitespace = true;
        let format = |color: Color32| {
            let mut f = egui::text::TextFormat::simple(font_id.clone(), color);
            f.line_height = Some(line_height);
            f
        };
        let mut rest = text;
        while let Some(open) = rest.find('{') {
            let (plain, after_open) = rest.split_at(open);
            if !plain.is_empty() {
                job.append(plain, 0.0, format(text_color));
            }
            match after_open.find('}') {
                Some(close) => {
                    let (tok, tail) = after_open.split_at(close + 1);
                    let name = &tok[1..tok.len() - 1];
                    let color = trace_colors.get(name).copied().unwrap_or(text_color);
                    job.append(tok, 0.0, format(color));
                    rest = tail;
                }
                None => {
                    // Unterminated `{` — leave as plain text.
                    job.append(after_open, 0.0, format(text_color));
                    rest = "";
                }
            }
        }
        if !rest.is_empty() {
            job.append(rest, 0.0, format(text_color));
        }
        ui.fonts_mut(|f| f.layout_job(job))
    }
}