Skip to main content

Module latest

Module latest 

Source
Expand description

A slow load that follows a selection: the latest asked wins.

Experimental: may change in any release (docs/stability.md).

A panel previewing what is selected elsewhere asks for the current key each time it draws (Latest::want). A key that changed starts a load on a worker thread and the panel goes on drawing; a load asked before it and not started yet is dropped, one running is told through its Cancel and its result is dropped. One load runs at a time, so going down a list with an arrow key does not start one thread per row.

Nothing runs behind a hidden panel: only want starts a load, and a hidden panel’s ui() does not run. A panel shown again asks for what is selected then, and only that. A load already running when the panel is closed goes on to its end; the panel keeps its result if the key is still the one selected when it comes back.

use egui_pop_core::latest::{Latest, Loaded};

fn preview(ui: &mut egui::Ui, latest: &mut Latest<String, String>, selected: Option<&String>) {
    let shown = latest.want(ui.ctx(), selected, |key, cancel| {
        // On a worker thread; a long load looks at `cancel` now and then.
        if cancel.cancelled() {
            return String::new();
        }
        format!("the preview of {key}")
    });
    match shown {
        Loaded::Nothing => ui.weak("Nothing selected."),
        Loaded::Loading { .. } => ui.weak("Loading…"),
        Loaded::Ready(text) => ui.label(text.as_str()),
        Loaded::Failed(reason) => ui.label(reason),
    };
}

On the web there are no threads: the load runs in the frame that asks for it. The file picker’s previews have a worker of their own, tied to files (egui-pop-api’s files::preview); this one knows nothing of what it loads. See docs/linked-panels.md.

Structs§

Cancel
Whether a load is still wanted: a long one looks now and then, and may stop. What it returns then is dropped.
Latest
One slow value following a key; see the module.

Enums§

Loaded
What Latest::want has to show.