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
use super::panel::{DockPanel, DockState};
use eframe::egui;
use egui_table::{HeaderRow as EgHeaderRow, Table, TableDelegate};
use std::cell::{Cell, RefCell};
// Feature-gated debug logging for traces table widths.
// Enable prints with: cargo run --features traces_table_debug --example sine
// or for your binary accordingly. When the feature is disabled, logs are compiled out.
#[cfg(feature = "traces_table_debug")]
#[allow(unused_macros)]
macro_rules! traces_debug { ($($arg:tt)*) => { eprintln!($($arg)*); } }
#[cfg(not(feature = "traces_table_debug"))]
#[allow(unused_macros)]
macro_rules! traces_debug {
($($arg:tt)*) => {{ /* no-op */ }};
}
thread_local! {
static LAST_AVAIL_W: Cell<f32> = Cell::new(0.0);
static LAST_COL_HDR_W: RefCell<[f32; 6]> = RefCell::new([0.0; 6]);
static LAST_COL_ROW0_W: RefCell<[f32; 6]> = RefCell::new([0.0; 6]);
}
use super::app::LivePlotApp;
pub(super) fn traces_panel_contents(app: &mut LivePlotApp, ui: &mut egui::Ui) {
ui.label("Configure traces: marker selection, visibility, colors, offsets, Y axis options, and legend info.");
ui.horizontal(|ui| {
let mut v = app.show_info_in_legend;
if ui
.checkbox(&mut v, "Show info in Legend")
.on_hover_text("Append each trace's info text to its legend label")
.changed()
{
app.show_info_in_legend = v;
}
});
ui.separator();
ui.horizontal(|ui| {
let mut ylog = app.y_log;
if ui
.checkbox(&mut ylog, "Y axis log scale")
.on_hover_text("Use base-10 log of (value + offset). Non-positive values are omitted.")
.changed()
{
app.y_log = ylog;
}
ui.label("Y unit:");
let mut unit = app.y_unit.clone().unwrap_or_default();
if ui.text_edit_singleline(&mut unit).changed() {
app.y_unit = if unit.trim().is_empty() {
None
} else {
Some(unit)
};
}
});
ui.separator();
// Build rows: include a synthetic "Free" row for marker selection only
#[derive(Clone)]
struct Row {
name: String,
is_free: bool,
}
let mut rows: Vec<Row> = Vec::new();
rows.push(Row {
name: "Free".to_string(),
is_free: true,
});
for n in app.trace_order.iter() {
rows.push(Row {
name: n.clone(),
is_free: false,
});
}
// Delegate for table rendering
struct TracesDelegate<'a> {
app: &'a mut LivePlotApp,
rows: Vec<Row>,
col_w: [f32; 6],
}
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;
// Reserve exact width for this column and render content within
let (rect, _resp) =
ui.allocate_exact_size(egui::vec2(self.col_w[col], 20.0), egui::Sense::hover());
ui.scope_builder(
egui::UiBuilder::new()
.max_rect(rect)
.layout(egui::Layout::left_to_right(egui::Align::Center)),
|inner| {
// Debug: actual header cell allocated width per column
let w = inner.max_rect().width();
LAST_COL_HDR_W.with(|arr| {
let mut a = arr.borrow_mut();
if (a[col] - w).abs() > 0.5 {
a[col] = w;
//traces_debug!("[traces_ui] header col{} width={:.1}", col, w);
}
});
let text = match col {
0 => "",
1 => "Trace",
2 => "Marker",
3 => "Visible",
4 => "Offset",
5 => "Info",
_ => "",
};
// Center certain headers; keep Name/Info left-aligned
let centered_cols = [0usize, 2, 3, 4];
if centered_cols.contains(&col) {
// Use a full-width centered-and-justified layout to ensure true horizontal centering
let layout =
egui::Layout::centered_and_justified(egui::Direction::LeftToRight);
inner.allocate_ui_with_layout(inner.max_rect().size(), layout, |ui2| {
ui2.strong(text);
});
} else {
inner.add_space(4.0);
inner.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];
// Reserve exact width and render within this rect
let (rect, _resp) =
ui.allocate_exact_size(egui::vec2(self.col_w[col], 20.0), egui::Sense::hover());
ui.scope_builder(
egui::UiBuilder::new()
.max_rect(rect)
.layout(egui::Layout::left_to_right(egui::Align::Center)),
|inner| {
// Debug: first row cell allocated width per column
if row == 0 {
let w = inner.max_rect().width();
LAST_COL_ROW0_W.with(|arr| {
let mut a = arr.borrow_mut();
if (a[col] - w).abs() > 0.5 {
a[col] = w;
//traces_debug!("[traces_ui] row0 col{} width={:.1}", col, w);
}
});
}
match col {
0 => {
// Color editor centered (moved from former column 5)
inner.with_layout(
egui::Layout::centered_and_justified(egui::Direction::LeftToRight),
|cui| {
if r.is_free {
cui.label("");
} else if let Some(tr) = self.app.traces.get_mut(&r.name) {
let mut c = tr.look.color;
let resp = cui
.color_edit_button_srgba(&mut c)
.on_hover_text("Change trace color");
if resp.hovered() {
self.app.hover_trace = Some(r.name.clone());
}
if resp.changed() {
tr.look.color = c;
}
}
},
);
}
1 => {
inner.add_space(4.0);
let resp = inner.add(
egui::Label::new(&r.name)
.truncate()
.show_tooltip_when_elided(true)
.sense(egui::Sense::click()),
);
if resp.hovered() {
if !r.is_free {
self.app.hover_trace = Some(r.name.clone());
}
}
if resp.clicked() {
if !r.is_free {
let cur = self.app.traces_panel.look_editor_trace.clone();
self.app.traces_panel.look_editor_trace =
if cur.as_deref() == Some(&r.name) {
None
} else {
Some(r.name.clone())
};
// Clear hover so highlight doesn't obscure editor
self.app.hover_trace = None;
}
}
}
2 => {
// Marker radio centered: Free or exactly one name
inner.with_layout(
egui::Layout::centered_and_justified(egui::Direction::LeftToRight),
|cui| {
let mut sel = self.app.selection_trace.clone();
let is_selected = (r.is_free && sel.is_none())
|| (!r.is_free && sel.as_ref() == Some(&r.name));
let resp = cui.selectable_label(
is_selected,
if r.is_free { "Free" } else { "Use" },
);
if resp.hovered() && !r.is_free {
self.app.hover_trace = Some(r.name.clone());
}
if resp.clicked() {
sel = if r.is_free {
None
} else {
Some(r.name.clone())
};
self.app.selection_trace = sel;
}
},
);
}
3 => {
// Visible checkbox centered
inner.with_layout(
egui::Layout::centered_and_justified(egui::Direction::LeftToRight),
|cui| {
if r.is_free {
cui.label("");
} else if let Some(tr) = self.app.traces.get_mut(&r.name) {
let mut vis = tr.look.visible;
let resp = cui.checkbox(&mut vis, "");
if resp.hovered() {
self.app.hover_trace = Some(r.name.clone());
}
if resp.changed() {
tr.look.visible = vis;
}
}
},
);
}
4 => {
// Offset DragValue centered
inner.with_layout(
egui::Layout::centered_and_justified(egui::Direction::LeftToRight),
|cui| {
if r.is_free {
cui.label("");
} else if let Some(tr) = self.app.traces.get_mut(&r.name) {
let mut off = tr.offset;
let resp = cui.add(
egui::DragValue::new(&mut off)
.speed(0.01)
.range(-1.0e12..=1.0e12),
);
if resp.hovered() {
self.app.hover_trace = Some(r.name.clone());
}
if resp.changed() {
tr.offset = off;
}
}
},
);
}
5 => {
inner.add_space(4.0);
if r.is_free {
inner.label("");
} else if let Some(tr) = self.app.traces.get(&r.name) {
let text = tr.info.clone();
let resp = inner.add(
egui::Label::new(text.clone())
.truncate()
.show_tooltip_when_elided(true)
.sense(egui::Sense::click()),
);
if resp.hovered() {
self.app.hover_trace = Some(r.name.clone());
}
if resp.clicked() {
inner.ctx().copy_text(text.clone());
}
}
}
_ => {}
}
},
);
}
}
// Compute dynamic column widths
// Policy:
// - All columns have a minimum width (min_w)
// - If available width is less than the sum of minima, we DO NOT shrink below minima;
// instead we enable horizontal scrolling so fixed columns never get smaller.
// - If there's extra space, only Name (1) and Info (5) expand using weights.
let avail_w = ui.available_width();
let avail_w_f32: f32 = avail_w;
// Preferred minima for each column [0..5]
let min_w = [12.0, 70.0, 42.0, 42.0, 32.0, 300.0];
let mut w = min_w;
// Current total at minima
let sum_min: f32 = w.iter().sum();
let name_weight = 0.45_f32;
let info_weight = 0.55_f32;
let weight_sum = name_weight + info_weight;
if avail_w_f32 > sum_min {
// We have extra space beyond all minima: distribute only to Name/Info by weights
let extra = avail_w_f32 - sum_min;
w[1] = min_w[1] + extra * (name_weight / weight_sum);
w[5] = min_w[5] + extra * (info_weight / weight_sum);
// Optional: ensure we fill the available width exactly (avoid tiny rounding gaps)
let sum_now: f32 = w.iter().sum();
let delta = avail_w_f32 - sum_now;
if delta.abs() > 0.5 {
w[5] = (w[5] + delta).max(0.0);
}
} else {
// Narrow panel: keep all columns at their minima; we'll scroll horizontally.
// Intentionally do nothing here so total width remains sum_min.
}
// // Debug: print when width changes notably
// LAST_AVAIL_W.with(|c| {
// let prev = c.get();
// if (avail_w_f32 - prev).abs() > 1.0 {
// c.set(avail_w_f32);
// traces_debug!(
// "[traces_ui] avail_w={:.1} sum_min={:.1} widths={:?}",
// avail_w_f32,
// min_w.iter().sum::<f32>(),
// &w
// );
// }
// });
// Reset hover before drawing; cells will set it when hovered
app.hover_trace = None;
let cols = vec![
egui_table::Column::new(w[0]), // color edit
egui_table::Column::new(w[1]), // name (stretches)
egui_table::Column::new(w[2]), // marker
egui_table::Column::new(w[3]), // visible
egui_table::Column::new(w[4]), // offset
egui_table::Column::new(w[5]), // info (stretches)
];
// 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 = app.traces_panel.look_editor_trace.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);
egui::ScrollArea::vertical()
.auto_shrink([false, true])
.show(ui, |ui| {
// Clone rows and draw the table first (style editor is rendered below)
let rows_clone = rows.clone();
{
let mut delegate = TracesDelegate {
app,
rows: rows_clone,
col_w: w,
};
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);
Table::new()
.id_salt(("traces_table", avail_w.to_bits()))
.num_rows(delegate.rows.len() as u64)
.columns(cols)
.headers(vec![EgHeaderRow::new(24.0)])
.show(&mut table_ui, &mut delegate);
}
// Render inline style editor beneath the table
if let Some(tn) = app.traces_panel.look_editor_trace.clone() {
if let Some(tr) = app.traces.get_mut(&tn) {
ui.add_space(8.0);
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() {
app.traces_panel.look_editor_trace = None;
}
},
);
});
ui.separator();
tr.look.render_editor(ui, true, None, false, None);
});
} else {
ui.label("Trace not found.");
app.traces_panel.look_editor_trace = None;
}
}
});
}
// Removed unused show_traces_dialog helper; dialogs are shown via DockPanel::show_detached_dialog
#[derive(Debug, Clone)]
pub struct TracesPanel {
pub dock: DockState,
pub look_editor_trace: Option<String>,
}
impl Default for TracesPanel {
fn default() -> Self {
Self {
dock: DockState::new("🗠Traces"),
look_editor_trace: None,
}
}
}
impl DockPanel for TracesPanel {
fn dock_mut(&mut self) -> &mut DockState {
&mut self.dock
}
fn panel_contents(&mut self, app: &mut LivePlotApp, ui: &mut egui::Ui) {
traces_panel_contents(app, ui);
}
}