re_dataframe_ui/
table_utils.rs

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
use ahash::HashSet;
use egui::{Context, Frame, Id, Margin, RichText, Stroke, Style};
use re_ui::{design_tokens, icons, Scale, UiExt as _};

pub const CELL_MARGIN: Margin = Margin::symmetric(8, 6);

/// This applies some fixes so that the column resize bar is correctly displayed.
///
/// TODO(lucasmerlin): this might affect widgets within the table, and should probably be reverted
/// within the cell. Also should be properly fixed via `egui_table`.
pub fn apply_table_style_fixes(style: &mut Style) {
    style.visuals.widgets.hovered.bg_stroke =
        Stroke::new(1.0, design_tokens().color_table.gray(Scale::S300));
    style.visuals.widgets.active.bg_stroke =
        Stroke::new(1.0, design_tokens().color_table.gray(Scale::S350));
    style.visuals.widgets.noninteractive.bg_stroke =
        Stroke::new(1.0, design_tokens().color_table.gray(Scale::S200));
}

pub fn header_title(ui: &mut egui::Ui, title: impl Into<RichText>) -> egui::Response {
    header_ui(ui, |ui| {
        ui.monospace(title.into().strong());
    })
    .response
}

pub fn header_ui<R>(
    ui: &mut egui::Ui,
    content: impl FnOnce(&mut egui::Ui) -> R,
) -> egui::InnerResponse<R> {
    let response = Frame::new()
        .inner_margin(CELL_MARGIN)
        .fill(design_tokens().color_table.gray(Scale::S150))
        .show(ui, |ui| {
            ui.set_width(ui.available_width());
            content(ui)
        });

    let rect = response.response.rect;

    ui.painter().hline(
        rect.x_range(),
        rect.max.y - 1.0, // - 1.0 prevents it from being overdrawn by the following row
        Stroke::new(1.0, design_tokens().color_table.gray(Scale::S300)),
    );

    response
}

pub fn cell_ui<R>(
    ui: &mut egui::Ui,
    content: impl FnOnce(&mut egui::Ui) -> R,
) -> egui::InnerResponse<R> {
    let response = Frame::new().inner_margin(CELL_MARGIN).show(ui, |ui| {
        ui.set_width(ui.available_width());
        content(ui)
    });

    let rect = response.response.rect;

    ui.painter().hline(
        rect.x_range(),
        rect.max.y - 1.0, // - 1.0 prevents it from being overdrawn by the following row
        Stroke::new(1.0, design_tokens().color_table.gray(Scale::S200)),
    );

    response
}

#[derive(Debug, Clone, Hash, serde::Serialize, serde::Deserialize)]
pub struct ColumnConfig {
    id: Id,
    name: String,
    visible: bool,
}

impl ColumnConfig {
    pub fn new(id: Id, name: String) -> Self {
        Self {
            id,
            name,
            visible: true,
        }
    }
}

// TODO(lucasmerlin): It would be nice to have this in egui_table, so egui_table could do the work
// of showing / hiding columns based on the config.
// https://github.com/rerun-io/egui_table/issues/27
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct TableConfig {
    id: Id,
    columns: Vec<ColumnConfig>,
}

impl TableConfig {
    fn new(id: Id) -> Self {
        Self {
            id,
            columns: Vec::new(),
        }
    }

    /// Get a table config, creating it if it doesn't exist.
    ///
    /// Columns is an iterator of default [`ColumnConfig`]s that will be added to the table config.
    /// Any columns that are not in the iterator will be removed from the table config.
    /// New columns will be added in order at the end.
    ///
    /// Don't forget to call [`Self::store`] to persist the changes.
    pub fn get_with_columns(
        ctx: &Context,
        persisted_id: Id,
        columns: impl Iterator<Item = ColumnConfig>,
    ) -> Self {
        ctx.data_mut(|data| {
            let config: &mut Self =
                data.get_persisted_mut_or_insert_with(persisted_id, || Self::new(persisted_id));

            let mut has_cols = HashSet::default();

            for col in columns {
                has_cols.insert(col.id);
                if !config.columns.iter().any(|c| c.name == col.name) {
                    config.columns.push(col);
                }
            }

            config.columns.retain(|col| has_cols.contains(&col.id));

            config.clone()
        })
    }

    pub fn store(self, ctx: &Context) {
        ctx.data_mut(|data| {
            data.insert_persisted(self.id, self);
        });
    }

    pub fn visible_columns(&self) -> impl Iterator<Item = &ColumnConfig> {
        self.columns.iter().filter(|col| col.visible)
    }

    pub fn visible_column_names(&self) -> impl Iterator<Item = &str> {
        self.visible_columns().map(|col| col.name.as_str())
    }

    pub fn visible_column_ids(&self) -> impl Iterator<Item = Id> + use<'_> {
        self.visible_columns().map(|col| col.id)
    }

    pub fn ui(&mut self, ui: &mut egui::Ui) {
        let response = egui_dnd::dnd(ui, "Columns").show(
            self.columns.iter_mut(),
            |ui, item, handle, _state| {
                let visible = item.visible;
                egui::Sides::new().show(
                    ui,
                    |ui| {
                        handle.ui(ui, |ui| {
                            ui.small_icon(&icons::DND_HANDLE, None);
                        });
                        let mut label = RichText::new(&item.name);
                        if visible {
                            label = label.strong();
                        } else {
                            label = label.weak();
                        }
                        ui.label(label);
                    },
                    |ui| {
                        if ui
                            .small_icon_button(if item.visible {
                                &icons::VISIBLE
                            } else {
                                &icons::INVISIBLE
                            })
                            .clicked()
                        {
                            item.visible = !item.visible;
                        }
                    },
                );
            },
        );
        if response.is_drag_finished() {
            response.update_vec(self.columns.as_mut_slice());
        }
    }

    pub fn button_ui(&mut self, ui: &mut egui::Ui) {
        ui.menu_image_text_button(icons::SETTINGS.as_image(), "Columns", |ui| {
            self.ui(ui);
        });
    }
}