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
use abstutil::prettyprint_usize;
use geom::Polygon;

use crate::{
    include_labeled_bytes, Color, ControlState, EventCtx, GeomBatch, Key, Line, Panel, Text,
    TextExt, Widget,
};

const ROWS: usize = 8;

pub struct Table<A, T, F> {
    id: String,
    data: Vec<T>,
    label_per_row: Box<dyn Fn(&T) -> String>,
    columns: Vec<Column<A, T>>,
    filter: Filter<A, T, F>,

    sort_by: String,
    descending: bool,
    skip: usize,
}

pub enum Col<T> {
    Static,
    Sortable(Box<dyn Fn(&mut Vec<&T>)>),
}

struct Column<A, T> {
    name: String,
    render: Box<dyn Fn(&EventCtx, &A, &T) -> GeomBatch>,
    col: Col<T>,
}

pub struct Filter<A, T, F> {
    pub state: F,
    pub to_controls: Box<dyn Fn(&mut EventCtx, &A, &F) -> Widget>,
    pub from_controls: Box<dyn Fn(&Panel) -> F>,
    pub apply: Box<dyn Fn(&F, &T, &A) -> bool>,
}

impl<A, T, F> Table<A, T, F> {
    pub fn new(
        id: impl Into<String>,
        data: Vec<T>,
        label_per_row: Box<dyn Fn(&T) -> String>,
        default_sort_by: &str,
        filter: Filter<A, T, F>,
    ) -> Table<A, T, F> {
        Table {
            id: id.into(),
            data,
            label_per_row,
            columns: Vec::new(),
            filter,

            sort_by: default_sort_by.to_string(),
            descending: true,
            skip: 0,
        }
    }

    pub fn column(
        &mut self,
        name: &str,
        render: Box<dyn Fn(&EventCtx, &A, &T) -> GeomBatch>,
        col: Col<T>,
    ) {
        self.columns.push(Column {
            name: name.to_string(),
            render,
            col,
        });
    }

    pub fn replace_render(&self, ctx: &mut EventCtx, app: &A, panel: &mut Panel) {
        let new_widget = self.render(ctx, app);
        panel.replace(ctx, &self.id, new_widget);
    }

    /// Get all entries, filtered and sorted according to the current settings.
    pub fn get_filtered_data(&self, app: &A) -> Vec<&T> {
        let mut data: Vec<&T> = Vec::new();

        // Filter
        for row in &self.data {
            if (self.filter.apply)(&self.filter.state, row, app) {
                data.push(row);
            }
        }

        // Sort
        for col in &self.columns {
            if col.name == self.sort_by {
                if let Col::Sortable(ref sorter) = col.col {
                    (sorter)(&mut data);
                    break;
                }
                // TODO Error handling
            }
        }
        if self.descending {
            data.reverse();
        }

        data
    }

    pub fn render(&self, ctx: &mut EventCtx, app: &A) -> Widget {
        let data = self.get_filtered_data(app);
        let num_filtered = data.len();

        // Render the headers
        let headers = self
            .columns
            .iter()
            .map(|col| {
                if self.sort_by == col.name {
                    ctx.style()
                        .btn_outline
                        .icon_text("tmp", &col.name)
                        .image_bytes(if self.descending {
                            include_labeled_bytes!("../../icons/arrow_down.svg")
                        } else {
                            include_labeled_bytes!("../../icons/arrow_up.svg")
                        })
                        .label_first()
                        .build_widget(ctx, &col.name)
                } else if let Col::Sortable(_) = col.col {
                    ctx.style().btn_outline.text(&col.name).build_def(ctx)
                } else {
                    Line(&col.name).into_widget(ctx).centered_vert()
                }
            })
            .collect();

        // Render data
        let mut rows = Vec::new();
        for row in data.into_iter().skip(self.skip).take(ROWS) {
            rows.push((
                (self.label_per_row)(row),
                self.columns
                    .iter()
                    .map(|col| (col.render)(ctx, app, row))
                    .collect(),
            ));
        }

        // Put together the UI
        Widget::col(vec![
            (self.filter.to_controls)(ctx, app, &self.filter.state),
            render_table(ctx, headers, rows, 0.88 * ctx.canvas.window_width),
            make_pagination(ctx, num_filtered, self.skip),
        ])
        .named(&self.id)
        // return in separate container in case caller want to apply an outer-name
        .container()
    }

    // Recalculate if true
    pub fn clicked(&mut self, action: &str) -> bool {
        if action == "previous" {
            self.skip -= ROWS;
            return true;
        }
        if action == "next" {
            self.skip += ROWS;
            return true;
        }
        for col in &self.columns {
            if col.name == action {
                self.skip = 0;
                if self.sort_by == action {
                    self.descending = !self.descending;
                } else {
                    self.sort_by = action.to_string();
                    self.descending = true;
                }
                return true;
            }
        }
        false
    }

    pub fn panel_changed(&mut self, panel: &Panel) {
        self.filter.state = (self.filter.from_controls)(panel);
        self.skip = 0;
    }
}

impl<A, T> Filter<A, T, ()> {
    pub fn empty() -> Filter<A, T, ()> {
        Filter {
            state: (),
            to_controls: Box::new(|_, _, _| Widget::nothing()),
            from_controls: Box::new(|_| ()),
            apply: Box::new(|_, _, _| true),
        }
    }
}

// Simpler wrappers than column(). The more generic case exists to allow for icons and non-text
// things.
impl<A, T: 'static, F> Table<A, T, F> {
    pub fn static_col(&mut self, name: &str, to_str: Box<dyn Fn(&T) -> String>) {
        self.column(
            name,
            Box::new(move |ctx, _, x| Text::from((to_str)(x)).render(ctx)),
            Col::Static,
        );
    }
}

fn make_pagination(ctx: &mut EventCtx, total: usize, skip: usize) -> Widget {
    let next = ctx
        .style()
        .btn_next()
        .disabled(skip + 1 + ROWS >= total)
        .hotkey(Key::RightArrow);
    let prev = ctx
        .style()
        .btn_prev()
        .disabled(skip == 0)
        .hotkey(Key::LeftArrow);

    Widget::row(vec![
        prev.build_widget(ctx, "previous"),
        format!(
            "{}-{} of {}",
            if total > 0 {
                prettyprint_usize(skip + 1)
            } else {
                "0".to_string()
            },
            prettyprint_usize((skip + 1 + ROWS).min(total)),
            prettyprint_usize(total)
        )
        .text_widget(ctx)
        .centered_vert(),
        next.build_widget(ctx, "next"),
    ])
}

/// Render a table with the specified headers and rows. Each row will be a clickable button with a
/// string label.
pub fn render_table(
    ctx: &mut EventCtx,
    headers: Vec<Widget>,
    rows: Vec<(String, Vec<GeomBatch>)>,
    total_width: f64,
) -> Widget {
    let total_width = total_width;
    let mut width_per_col: Vec<f64> = headers.iter().map(|w| w.get_width_for_forcing()).collect();
    for (_, row) in &rows {
        for (col, width) in row.iter().zip(width_per_col.iter_mut()) {
            *width = width.max(col.get_dims().width);
        }
    }
    let extra_margin = ((total_width - width_per_col.clone().into_iter().sum::<f64>())
        / (width_per_col.len() - 1) as f64)
        .max(0.0);

    let mut col = vec![Widget::custom_row(
        headers
            .into_iter()
            .enumerate()
            .map(|(idx, w)| {
                let margin = extra_margin + width_per_col[idx] - w.get_width_for_forcing();
                if idx == width_per_col.len() - 1 {
                    w.margin_right((margin - extra_margin) as usize)
                } else {
                    w.margin_right(margin as usize)
                }
            })
            .collect(),
    )];

    // TODO Maybe can do this now simpler with to_geom
    for (label, row) in rows {
        let mut batch = GeomBatch::new();
        batch.autocrop_dims = false;
        let mut x1 = 0.0;
        for (col, width) in row.into_iter().zip(width_per_col.iter()) {
            batch.append(col.translate(x1, 0.0));
            x1 += *width + extra_margin;
        }

        let rect = Polygon::rectangle(total_width, batch.get_dims().height);
        let mut hovered = GeomBatch::new();
        hovered.push(Color::hex("#7C7C7C"), rect.clone());
        hovered.append(batch.clone());

        col.push(
            ctx.style()
                .btn_plain
                .btn()
                .custom_batch(batch, ControlState::Default)
                .custom_batch(hovered, ControlState::Hovered)
                .no_tooltip()
                .build_widget(ctx, &label),
        );
    }

    Widget::custom_col(col)
}