gif_viewer.rs

 1use gpui::{
 2    div, img, prelude::*, App, AppContext, ImageSource, Render, ViewContext, WindowOptions,
 3};
 4use std::path::PathBuf;
 5
 6struct GifViewer {
 7    gif_path: PathBuf,
 8}
 9
10impl GifViewer {
11    fn new(gif_path: PathBuf) -> Self {
12        Self { gif_path }
13    }
14}
15
16impl Render for GifViewer {
17    fn render(&mut self, _cx: &mut ViewContext<Self>) -> impl IntoElement {
18        div().size_full().child(
19            img(ImageSource::File(self.gif_path.clone().into()))
20                .size_full()
21                .object_fit(gpui::ObjectFit::Contain)
22                .id("gif"),
23        )
24    }
25}
26
27fn main() {
28    env_logger::init();
29    App::new().run(|cx: &mut AppContext| {
30        let cwd = std::env::current_dir().expect("Failed to get current working directory");
31        let gif_path = cwd.join("crates/gpui/examples/image/black-cat-typing.gif");
32
33        if !gif_path.exists() {
34            eprintln!("Image file not found at {:?}", gif_path);
35            eprintln!("Make sure you're running this example from the root of the gpui crate");
36            cx.quit();
37            return;
38        }
39
40        cx.open_window(
41            WindowOptions {
42                focus: true,
43                ..Default::default()
44            },
45            |cx| cx.new_view(|_cx| GifViewer::new(gif_path)),
46        )
47        .unwrap();
48        cx.activate(true);
49    });
50}