emilk / egui

egui: an easy-to-use immediate mode GUI in Rust that runs on both web and native
https://www.egui.rs/
Apache License 2.0
21.67k stars 1.56k forks source link

`egui_extras::Table`: add an API which provide the row range to draw upfront #5024

Closed abey79 closed 1 week ago

abey79 commented 2 weeks ago

The current API is the following:

table_builder
    // ...
    .header(re_ui::DesignTokens::table_line_height(), header_ui)
    .body(|body| {
        body.rows(line_height, row_count, |row| /* draw `row_nr = row.index()` */);
    });

When displaying data that must be prefetched from some datastore with a paginated api(e.g. provide up to limit rows starting at offset), it would be useful to have an API that provide upfront the range of row indices that will be called.

emilk commented 1 week ago

One solution is to simply add the full range as a second parameter to the closure:

let mut batch = vec![];

table_builder
    .header(re_ui::DesignTokens::table_line_height(), header_ui)
    .body(|body| {
        body.rows(line_height, row_count, |row, row_range: Range<usize>| {
            if row.index() == row_range.start {
                batch = load_batch(row_range);
            }
            show(row, batch);
        });
    });

However, that's maybe not the most ergonomic.

An alternative is

table_builder
    .header(re_ui::DesignTokens::table_line_height(), header_ui)
    .body(|body| {
        body.rows_ext(line_height, row_count,
            |row_range: Range<usize>| load_batch(row_range),
            |batch, row| show(row, batch)
        );
    });
abey79 commented 1 week ago

Either would be fine for me. The former needs to guarantee that the closure is called in the correct order. The latter is slightly weird wrt to ownership (I assume body take ownership of the first closure's returned value, and only provides a mutable(?) ref to the second closure)?

Wonder if a trait based API could be more ergonomics, as it could encompass everything (header row(s), on_row_batch(), on_row(), cell-based callback to accommodate for https://github.com/emilk/egui/issues/5001).

emilk commented 1 week ago

Done in https://github.com/rerun-io/egui_table