egui_tabular 0.1.0

Customizable egui table viewer and editor
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
pub mod config;
mod interaction;
mod state;
mod tool_column;

use crate::frontend::TableFrontend;
use crate::table_view::state::SelectedRange;
pub use config::TableViewConfig;
use egui::epaint::{PathShape, PathStroke};
use egui::scroll_area::ScrollSource;
use egui::{
    CornerRadius, CursorIcon, Id, Key, Label, Painter, PointerButton, PopupAnchor, Pos2, Response,
    RichText, ScrollArea, Sense, Stroke, TextWrapMode, Tooltip, Ui, UiKind, Vec2,
};
use egui_extras::{Column, TableBody};
use std::collections::HashMap;
use tabular_core::backend::{BackendColumn, OneShotFlags, TableBackend, VisualRowIdx};
use tabular_core::{CellCoord, ColumnUid};
use tap::Tap;

pub struct TableView {
    state: state::State,
}

impl TableView {
    pub fn new() -> Self {
        TableView {
            state: state::State::default(),
        }
    }

    pub fn show<T: TableFrontend + TableBackend>(
        &mut self,
        table: &mut T,
        config: &mut TableViewConfig,
        max_height: Option<f32>,
        ui: &mut Ui,
        id: Id,
    ) -> Response {
        let mut is_no_columns = self.state.columns_ordered.is_empty();
        let prev_selected_range = self.state.selected_range;
        let pointer_over_table = ui.rect_contains_pointer(ui.max_rect());
        let is_read_only = table.persistent_flags().is_read_only;
        if pointer_over_table && !is_read_only && !self.state.is_editing() {
            self.handle_paste(is_no_columns, table, ui);
        }

        self.check_col_set_updated(table, &mut is_no_columns);
        self.check_row_set_updated(table, config);

        if is_no_columns {
            table.one_shot_flags_archive();
            *table.one_shot_flags_internal_mut() = OneShotFlags::zero();
            if ui.button("Create column").clicked() {
                table.create_column();
            }
            return ui.label("No columns, but can paste tabular data from clipboard");
        }

        if pointer_over_table && !self.state.is_editing() {
            self.handle_key_input(table, ui);
        } else if pointer_over_table && self.state.is_editing() {
            self.handle_key_input_when_editing(table, ui);
        }
        self.handle_paste_continue(table, id, ui);

        let ctx = &ui.ctx().clone();
        let style = ui.style().clone();
        let painter = ui.painter().clone();
        let visual = &style.visuals;
        let ui_layer_id = ui.layer_id();
        let mut resp_total = None::<Response>;
        let mut resp_ret = None::<Response>;
        // Temporarily take out columns Vec, to satisfy the borrow checker.
        let columns = core::mem::take(&mut self.state.columns_ordered);
        let mut swap_columns = None;
        let show_tool_column = true;

        ScrollArea::horizontal()
            .scroll_source(ScrollSource::MOUSE_WHEEL)
            .show(ui, |ui| {
                let mut builder = egui_extras::TableBuilder::new(ui);
                builder = if show_tool_column {
                    builder.column(
                        Column::initial(48.0)
                            .at_least(20.0)
                            .clip(!config.use_heterogeneous_row_heights),
                    )
                } else {
                    builder
                };
                let mut builder = if let Some(m) = max_height {
                    builder.max_scroll_height(m)
                } else {
                    builder
                };
                for _column in &columns {
                    // Note on clip: At least labels won't try to enlarge cell's area,
                    // effectively rendering heterogeneous row heights logic useless.
                    // So disable clipping if heterogeneous row heights are used.
                    builder =
                        builder.column(Column::auto().clip(!config.use_heterogeneous_row_heights));
                    // builder = builder.column(
                    //     Column::initial(column.name.len() as f32 * 8.0)
                    //         .at_least(36.0)
                    //         .clip(true),
                    // );
                }
                builder
                    // .drag_to_scroll(false) // Drag is used for selection
                    .striped(true)
                    .resizable(true)
                    .stick_to_bottom(true)
                    // .sense(Sense::click())
                    .sense(Sense::click_and_drag().tap_mut(|s| s.set(Sense::FOCUSABLE, true)))
                    .header(20., |mut h| {
                        if show_tool_column {
                            let (_, r) = h.col(|ui| {
                                Self::draw_table_icon(ui);
                            });
                            r.context_menu(|ui| {
                                tool_column::tool_column_header_menu_ui(ui, table);
                            });
                            r.on_hover_text("Tool column, right click for actions");
                        }
                        for column_uid in columns.iter().copied() {
                            let Some(backend_column) = self.state.columns.get(&column_uid) else {
                                continue;
                            };
                            let mut painter = None;
                            let (_, resp) = h.col(|ui| {
                                // ui.horizontal_centered(|ui| {
                                table.custom_column_ui(column_uid, ui, id);
                                let changed = Self::column_mapping_ui(
                                    table.column_mapping_choices(),
                                    column_uid,
                                    &mut config.column_mapped_to,
                                    ui,
                                    id,
                                );
                                if changed {
                                    table.one_shot_flags_internal_mut().column_mapping_changed =
                                        Some(column_uid);
                                }
                                let col_name = if backend_column.name.is_empty() {
                                    "No name"
                                } else {
                                    backend_column.name.as_str()
                                };
                                let col_name =
                                    Label::new(RichText::new(col_name).strong().monospace())
                                        .selectable(false)
                                        .wrap_mode(TextWrapMode::Extend);
                                ui.add(col_name)
                                    .on_hover_cursor(CursorIcon::Grab)
                                    .on_hover_ui(|ui| {
                                        Self::column_name_hover_ui(&backend_column, ui);
                                    });
                                // });
                                if !backend_column.ty.is_empty() {
                                    ui.add(
                                        Label::new(backend_column.ty.as_str())
                                            .wrap_mode(TextWrapMode::Extend),
                                    );
                                }

                                if painter.is_none() {
                                    painter = Some(ui.painter().clone());
                                }
                            });

                            // Set drag payload for column reordering.
                            resp.dnd_set_drag_payload(column_uid);

                            if resp.dragged() {
                                Tooltip::always_open(
                                    ctx.clone(),
                                    ui_layer_id,
                                    "_egui_tabular_column_move".into(),
                                    PopupAnchor::Pointer,
                                )
                                .gap(12.0)
                                .show(|ui| {
                                    ui.label(backend_column.name.as_str());
                                });
                            }

                            let mut rect_fix = resp.rect;
                            rect_fix.set_height(rect_fix.height() * 0.66);
                            // if resp.hovered() && backend_column.is_sortable {
                            //     if let Some(p) = &painter {
                            //         p.rect_filled(
                            //             rect_fix,
                            //             CornerRadius::ZERO,
                            //             visual.selection.bg_fill.gamma_multiply(0.2),
                            //         );
                            //     }
                            // }

                            if resp.dnd_hover_payload::<ColumnUid>().is_some() {
                                if let Some(p) = &painter {
                                    p.rect_filled(
                                        rect_fix,
                                        CornerRadius::ZERO,
                                        visual.selection.bg_fill.gamma_multiply(0.5),
                                    );
                                }
                            }

                            if let Some(payload) = resp.dnd_release_payload::<ColumnUid>() {
                                swap_columns = Some((column_uid, *payload));
                            }

                            Self::column_context_menu(backend_column, column_uid, resp, table);
                        }

                        // Account for header response to calculate total response.
                        resp_total = Some(h.response());
                    })
                    .tap_mut(|table| {
                        table.ui_mut().separator();
                    })
                    .body(|body| {
                        resp_ret = self.show_body(
                            table,
                            config,
                            body,
                            painter,
                            (),
                            ctx,
                            &style,
                            id,
                            &columns,
                            resp_total,
                            show_tool_column,
                        );
                    });
            });

        self.state.columns_ordered = columns.tap_mut(|columns| {
            if let Some((c1, c2)) = swap_columns {
                Self::swap_columns(columns, c1, c2, &mut self.state.selected_range);
            }
        });

        self.check_col_set_updated(table, &mut is_no_columns);

        if table.row_count() == 0 {
            let create_row = ui.button("Add row");
            if create_row.clicked() {
                table.create_row([]);
            }
            create_row.on_hover_text(
                "When table is not empty, right click tool column cell to create more rows",
            );
        }
        self.check_row_set_updated(table, config); // if modified rows during this render cycle

        if self.state.selected_range != prev_selected_range {
            let rows_selected = if let Some(r) = self.state.selected_range {
                let mut rows_selected = vec![];
                for row_idx in r.row_start()..=r.row_end() {
                    if let Some(row_uid) = table.row_uid(VisualRowIdx(row_idx)) {
                        rows_selected.push(row_uid);
                    }
                }
                rows_selected
            } else {
                vec![]
            };
            table.one_shot_flags_internal_mut().rows_selected = Some(rows_selected);
        }

        table.one_shot_flags_archive();
        *table.one_shot_flags_internal_mut() = OneShotFlags::zero();
        resp_ret.unwrap_or_else(|| ui.label("??"))
    }

    fn draw_table_icon(ui: &mut Ui) {
        let rect = ui.max_rect().translate(Vec2::new(0.0, 8.0));
        let stroke = Stroke::new(1.0_f32, ui.visuals().text_color());
        let p = ui.painter();
        p.rect_stroke(
            rect,
            CornerRadius::same(3),
            stroke,
            egui::StrokeKind::Middle,
        );
        p.line_segment([rect.center_top(), rect.center_bottom()], stroke);
        p.line_segment([rect.left_center(), rect.right_center()], stroke);
    }

    fn check_col_set_updated(&mut self, table: &mut impl TableBackend, is_no_columns: &mut bool) {
        if table.one_shot_flags_internal().columns_reset {
            // log::trace!("Updating col info");
            self.state.columns_ordered = table.used_columns().collect();
            self.state.columns_ordered.sort();
            *is_no_columns = self.state.columns_ordered.is_empty();
        }
        if table.one_shot_flags_internal().columns_reset
            || table.one_shot_flags_internal().columns_changed
        {
            self.state.columns.clear();
            for col_uid in self.state.columns_ordered.iter() {
                if let Some(info) = table.column_info(*col_uid) {
                    self.state.columns.insert(*col_uid, info.clone());
                }
            }
        }
    }

    fn check_row_set_updated(
        &mut self,
        table: &mut impl TableBackend,
        config: &mut TableViewConfig,
    ) {
        if table.one_shot_flags_internal().row_set_updated {
            self.state
                .row_heights
                .resize(table.row_count(), config.minimum_row_height);
            self.state.row_heights.fill(config.minimum_row_height);
        }
    }

    fn column_context_menu(
        col: &BackendColumn,
        col_uid: ColumnUid,
        resp: Response,
        data: &mut impl TableBackend,
    ) {
        resp.context_menu(|ui| {
            if col.is_sortable {
                if ui.button("Sort ascending").clicked() {
                    ui.close_kind(UiKind::Menu);
                }
                if ui.button("Sort descending").clicked() {
                    ui.close_kind(UiKind::Menu);
                }
                if ui.button("Add column").clicked() {
                    data.create_column();
                    ui.close_kind(UiKind::Menu);
                }
            }
            if ui.button("Hide").clicked() {
                ui.close_kind(UiKind::Menu);
            }
            if data.persistent_flags().are_cols_skippable {
                let mut skipped = data.is_col_skipped(col_uid);
                if ui.checkbox(&mut skipped, "Skip").changed() {
                    data.skip_col(col_uid, skipped);
                    ui.close_kind(UiKind::Menu);
                }
            }
        });
    }

    fn column_name_hover_ui(col: &BackendColumn, ui: &mut Ui) {
        if col.is_required {
            ui.label("Required column, synonyms:");
            for synonym_name in &col.synonyms {
                ui.horizontal(|ui| {
                    ui.label(synonym_name);
                    ui.label("or");
                    ui.label(synonym_name.to_lowercase());
                });
            }
        } else {
            if col.is_used {
                ui.label("Additional column, used");
            } else {
                ui.label("Additional column, not used");
            }
        }
    }

    fn swap_columns(
        columns: &mut Vec<ColumnUid>,
        c1: ColumnUid,
        c2: ColumnUid,
        selected_range: &mut Option<SelectedRange>,
    ) {
        let c1_idx = columns
            .iter()
            .enumerate()
            .find(|(_, uid)| **uid == c1)
            .map(|(idx, _)| idx);
        let c2_idx = columns
            .iter()
            .enumerate()
            .find(|(_, uid)| **uid == c2)
            .map(|(idx, _)| idx);
        if let (Some(c1_idx), Some(c2_idx)) = (c1_idx, c2_idx) {
            columns.swap(c1_idx, c2_idx);
            if let Some(r) = selected_range {
                if r.is_single_cell() {
                    // Keep selection if only one cell was selected, but adjust accordingly
                    r.swap_col(c1_idx, c2_idx);
                } else if !(r.contains_col(c1_idx) && r.contains_col(c2_idx)) {
                    // Deselect if column was dragged outside current selection
                    *selected_range = None;
                }
            }
        }
    }

    fn show_body<T: TableFrontend + TableBackend>(
        &mut self,
        table: &mut T,
        config: &TableViewConfig,
        body: TableBody<'_>,
        _painter: Painter,
        _commands: (),
        ctx: &egui::Context,
        style: &egui::Style,
        id: Id,
        columns: &[ColumnUid],
        mut resp_total: Option<Response>,
        show_tool_column: bool,
    ) -> Option<Response> {
        let visual = &style.visuals;
        let s = &mut self.state;
        let row_heights = &mut s.row_heights;
        let mut row_heights_updates = Vec::new();
        // let pointer_primary_down = ctx.input(|i| i.pointer.button_down(PointerButton::Primary));
        let mut commit_edit = None;
        let row_count = table.row_count();

        let render_fn = |mut row: egui_extras::TableRow| {
            let row_idx = row.index();
            let row_uid = table.row_uid(VisualRowIdx(row_idx)).unwrap();
            let is_editing_cell_on_this_row = s
                .selected_range
                .map(|r| r.is_editing() && r.contains_row(row_idx))
                .unwrap_or(false);
            if is_editing_cell_on_this_row {
                row.set_selected(true);
            }

            if show_tool_column {
                let (_, resp) = row.col(|ui| {
                    ui.add(Label::new(format!("{row_idx}")).selectable(false));
                });
                resp.context_menu(|ui| {
                    tool_column::tool_column_row_menu_ui(ui, table, row_uid);
                });
                if resp.clicked() {
                    if let Some(r) = &mut s.selected_range {
                        if ctx.input(|i| i.modifiers.shift) {
                            r.stretch_multi_row(row_idx, columns.len());
                        } else {
                            *r = SelectedRange::single_row(row_idx, columns.len());
                        }
                    } else {
                        s.selected_range = Some(SelectedRange::single_row(row_idx, columns.len()));
                    }
                }
            }

            let mut next_frame_row_height = config.minimum_row_height;
            for (col_idx, col_uid) in columns.iter().copied().enumerate() {
                let current_cell = SelectedRange::single_cell(row_idx, col_idx);
                let (
                    is_first_row_in_selection,
                    is_last_row_in_selection,
                    is_current_cell_in_selection,
                    is_editing_current_cell,
                ) = s
                    .selected_range
                    .map(|r| {
                        (
                            r.row_start() == row_idx,
                            r.row_end() == row_idx,
                            r.contains(row_idx, col_idx),
                            r.is_editing() && r == current_cell,
                        )
                    })
                    .unwrap_or((false, false, false, false));

                let coord = CellCoord { row_uid, col_uid };
                let (rect, resp) = row.col(|ui| {
                    let ui_max_rect = ui.max_rect();
                    const EXPAND_X: f32 = 2.0;

                    let color = if let Some(backend_color) = table.cell_color(coord) {
                        Some(
                            if is_current_cell_in_selection && !is_editing_cell_on_this_row {
                                backend_color.gamma_multiply(0.4)
                            } else {
                                backend_color.gamma_multiply(0.2)
                            },
                        )
                    } else if is_current_cell_in_selection && !is_editing_cell_on_this_row {
                        // Light orange background inside selection
                        Some(visual.warn_fg_color.gamma_multiply(0.2))
                    } else {
                        None
                    };
                    if let Some(color) = color {
                        ui.painter().rect_filled(
                            ui_max_rect.expand2([EXPAND_X, 0.0].into()),
                            CornerRadius::ZERO,
                            color,
                        );
                    }

                    if let Some(corner) = table.cell_corner(coord) {
                        let r = ui_max_rect.right_top();
                        ui.painter().add(PathShape {
                            points: vec![Pos2::new(r.x - 10.0, r.y), r, Pos2::new(r.x, r.y + 10.0)],
                            closed: true,
                            fill: corner,
                            stroke: PathStroke::NONE,
                        });
                    }

                    // Lines on the first and last row of selection
                    let st = Stroke {
                        width: 1.,
                        color: visual.warn_fg_color.gamma_multiply(0.5),
                    };
                    let xr = ui_max_rect.x_range().expand(EXPAND_X);
                    let yr = ui_max_rect.y_range();
                    if is_first_row_in_selection {
                        ui.painter().hline(xr, yr.min, st);
                    }
                    if is_last_row_in_selection {
                        ui.painter().hline(xr, yr.max, st);
                    }
                    // Vertical lines
                    // if is_current_cell_in_selection && !is_editing_cell_on_this_row {
                    //     ui.painter().vline(xr.min, yr, st);
                    //     ui.painter().vline(xr.max, yr, st);
                    // }

                    ui.style_mut()
                        .visuals
                        .widgets
                        .noninteractive
                        .fg_stroke
                        .color = visual.strong_text_color();

                    if is_editing_current_cell {
                        let _resp = table.show_cell_editor(coord, ui, id);
                        if ui.input(|i| i.key_pressed(Key::Enter)) {
                            commit_edit = Some(coord);
                        }
                        if ui.input(|i| i.key_pressed(Key::Escape)) {
                            if let Some(r) = &mut s.selected_range {
                                r.set_editing(None);
                            }
                        }
                    } else {
                        ui.add_enabled_ui(true, |ui| {
                            table.show_cell_view(coord, ui, id);
                        });
                    }
                });
                next_frame_row_height = rect.height().max(next_frame_row_height);

                if resp.clicked_by(PointerButton::Primary) {
                    if let Some(r) = &mut s.selected_range {
                        if ctx.input(|i| i.modifiers.shift) {
                            r.stretch_to(row_idx, col_idx);
                        } else {
                            if *r == current_cell {
                                if !table.persistent_flags().is_read_only {
                                    r.set_editing(Some(coord));
                                }
                            } else {
                                if let Some(coord) = r.editing() {
                                    commit_edit = Some(coord);
                                }
                                s.selected_range = Some(current_cell);
                            }
                        }
                    } else {
                        s.selected_range = Some(current_cell);
                    }
                }
                if resp.double_clicked_by(PointerButton::Primary) {}
                let tooltips = table.cell_tooltips(coord);
                if !tooltips.is_empty() {
                    resp.on_hover_ui(|ui| {
                        ui.vertical(|ui| {
                            for msg in tooltips {
                                ui.label(msg);
                            }
                        });
                    });
                }
            } // for col_uid in used_columns

            if config.use_heterogeneous_row_heights {
                if let Some(prev_row_height) = row_heights.get(row_idx) {
                    if (next_frame_row_height - *prev_row_height).abs() > 0.1 {
                        row_heights_updates.push((row_idx, next_frame_row_height));
                    }
                } else {
                    log::warn!("Row heights wrong len");
                }
            }

            // Accumulate response
            if let Some(resp) = &mut resp_total {
                *resp = resp.union(row.response());
            } else {
                resp_total = Some(row.response());
            }
        };

        if config.use_heterogeneous_row_heights {
            body.heterogeneous_rows(row_heights.iter().copied(), render_fn);
            for (row_idx, next_frame_row_height) in row_heights_updates {
                row_heights[row_idx] = next_frame_row_height;
            }
        } else {
            body.rows(config.minimum_row_height, row_count, render_fn);
        }

        if let Some(coord) = commit_edit {
            table.commit_cell_edit(coord);
            if let Some(r) = &mut s.selected_range {
                r.set_editing(None);
            }
        }

        resp_total
    }

    fn column_mapping_ui(
        choices: &[String],
        col_uid: ColumnUid,
        column_mapped_to: &mut HashMap<ColumnUid, String>,
        ui: &mut Ui,
        id: Id,
    ) -> bool {
        if choices.is_empty() {
            return false;
        }
        let is_used_elsewhere = if let Some(selected) = column_mapped_to.get(&col_uid) {
            if selected.is_empty() {
                false
            } else {
                column_mapped_to
                    .iter()
                    .any(|(col, value)| *col != col_uid && value == selected)
            }
        } else {
            false
        };
        let selected = column_mapped_to.entry(col_uid).or_default();
        let selected_text = if selected.is_empty() {
            RichText::new("Skip")
        } else {
            if is_used_elsewhere {
                RichText::new(selected.as_str()).color(ui.visuals().warn_fg_color)
            } else {
                RichText::new(selected.as_str())
            }
        };
        let mut changed = false;
        let resp = egui::ComboBox::from_id_salt(id.with(col_uid.0))
            .selected_text(selected_text)
            .show_ui(ui, |ui| {
                changed |= ui
                    .selectable_value(selected, String::new(), "Skip")
                    .changed();
                for m in choices {
                    changed |= ui
                        .selectable_value(selected, m.clone(), m.as_str())
                        .changed();
                }
            })
            .response;
        if is_used_elsewhere {
            resp.on_hover_text("Cannot map more than one column to the same entity");
        }
        changed
    }
}