Skip to main content

Module preview

Module preview 

Source
Expand description

Previews of files, from pluggable providers.

A PreviewProvider says which files it handles (Handles: file extensions, MIME types, magic bytes, folders) and with what priority; plugins register theirs with Previews::register (the service is Files::previews). For a path, the providers that handle it are tried from the highest priority down: the first to give a preview wins, one may pass (Ok(None)) to the next, and a failure (an error, or a panic) is shown with its reason.

  • Off the interface thread: Previews::get answers from the cache, else hands the path to a worker thread of its own and says Shown::Loading; the frame never waits. Asking for another path cancels the one asked before (PreviewRequest::cancelled). On the web, which has no threads, the preview is made in the frame.
  • Many at once: a grid says which paths are in view each frame (Previews::want) and reads what is made (Previews::thumbnail). The worker makes them in that order, after the preview asked with Previews::get; a path scrolled out of view leaves the queue, or is cancelled in the making.
  • Cached by path, size and modification time, up to a size in bytes (Previews::set_cache_bytes, 64 MiB by default; the oldest go first). Providers read at most PreviewRequest::max_bytes.
use std::path::Path;
use std::sync::Arc;
use egui_pop_api::files::fs::{MemoryFs, SharedFs};
use egui_pop_api::files::preview::{
    Body, Handles, Preview, PreviewProvider, PreviewRequest, Previews, Shown,
};

#[derive(Debug)]
struct Shout;
impl PreviewProvider for Shout {
    fn id(&self) -> &str {
        "shout"
    }
    fn handles(&self) -> Handles {
        Handles::new().extensions(&["txt"])
    }
    fn preview(&self, request: &PreviewRequest) -> Result<Option<Preview>, String> {
        let text = String::from_utf8_lossy(&request.read()?).to_uppercase();
        Ok(Some(Preview::new(Body::Text { text, truncated: false })))
    }
}

let previews = Previews::default();
previews.register(Arc::new(Shout));
let fs: SharedFs = Arc::new(MemoryFs::new().file("/a.txt", b"hi".to_vec()));
let Shown::Ready(ready) = previews.compute(&fs, Path::new("/a.txt")) else { panic!() };
assert!(matches!(&ready.preview.body, Body::Text { text, .. } if text == "HI"));
assert_eq!(ready.provider, "shout");

Structs§

Handles
Which files a provider handles: any of its extensions, MIME types or magic numbers, and folders when it says so.
PhotoFacts
What a photo’s metadata says (EXIF, XMP), as a provider reads it: the preview pane shows it in one order, as Dolphin’s information panel and a photo application’s loupe do, whichever provider read it. Made with PhotoFacts::default and its builders.
Preview
A preview: what to draw and the facts to say. Made with Preview::new and its builders.
PreviewRequest
What a provider is asked for.
Previews
The preview providers, their cache and their worker, as a service of Files.
Probe
What a provider is asked whether it handles: the path, its facts, its MIME type from the extension, and its first bytes.
Ready
A preview made, with the facts of its file and its textures once drawn.

Enums§

Block
A block of a document drawn simply.
Body
What a preview draws.
Shown
Where a path’s preview is.

Constants§

CACHE_BYTES
The cache’s size, by default, in bytes.
MAX_BYTES
The most bytes a provider reads, by default.
MAX_SIDE
The longest side of a preview’s image, by default, in pixels.
PANE_WIDTH
The width of the picker’s preview pane (it shrinks when the list would get too narrow).

Traits§

PreviewProvider
A provider of previews.

Functions§

extension
A path’s extension, lower case.
mime_of
A file’s MIME type, from its extension (the common ones).
pane
Draws a preview pane: the entry’s name, its preview and its facts (a file’s size and modification time, a folder’s items, dimensions, pages, the provider’s).
size_text
A size in bytes, as people read it (powers of 1000).
time_text
A time as 2026-01-15 10:04 UTC.