Help rendering paintable animations (.gif) through MediaFile + Picture in GTK4

Hey all,

I’m looking to render a gif using GTK4 (rust bindings). I’m looking at gtk4::MediaFile and gtk4::Picture to render the IsA<Paintable>. My minimum reproducible example is here (full example on github):

const SIZE : i32 = 400;

fn main() {
    let application = Application::new(Some("gif.example"), Default::default());

    application.connect_activate(move |app: &Application| {
        //
        build_ui(app);
    });

    application.run();
}

/// put a MediaFile inside a Picture thats attached directly to the application window
///
/// Behavior: The animation "twitches" through the first few frames without playing the
/// full duration
fn build_ui(application: &Application) {
    let window = ApplicationWindow::builder()
        .application(application)
        .title("gif-display-example")
        .build();

    let media = MediaFile::for_filename("catJAM.gif");
    media.set_loop(true);
    media.set_playing(true);

    let picture = Picture::builder()
        .paintable(&media)
        .height_request(SIZE)
        .width_request(SIZE)
        .build();

    window.set_child(Some(&picture));

    window.show()
}

I would expect that the gif renders like the original file:

catJAM

but instead it only renders the first few frames (new users cant embed more than one attachment)

Can anyone point me in the right direction for rendering an animation like this?

GTK doesn’t know how to render gifs by default. You would need to write a custom paintable that draws the gif frames. I started an example at https://github.com/gtk-rs/gtk4-rs/pull/152 but I never got the time to finish it.

If you manage to do so, feel free to leave a comment on the PR or open a new one with the updated example.

Thanks for the info!

This is a bit surprising for me but I suppose there’s not a ton of use for gifs in a UI. I will probably figure out some sort of solution modeled after your work and leave an example in a PR.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.