gif_viewer.rs

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