gif_viewer.rs

 1#![cfg_attr(target_family = "wasm", no_main)]
 2
 3use gpui::{App, Context, Render, Window, WindowOptions, div, img, prelude::*};
 4use gpui_platform::application;
 5use std::path::PathBuf;
 6
 7struct GifViewer {
 8    gif_path: PathBuf,
 9}
10
11impl GifViewer {
12    fn new(gif_path: PathBuf) -> Self {
13        Self { gif_path }
14    }
15}
16
17impl Render for GifViewer {
18    fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
19        div().size_full().child(
20            img(self.gif_path.clone())
21                .size_full()
22                .object_fit(gpui::ObjectFit::Contain)
23                .id("gif"),
24        )
25    }
26}
27
28fn run_example() {
29    application().run(|cx: &mut App| {
30        let gif_path =
31            PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("examples/image/black-cat-typing.gif");
32
33        cx.open_window(
34            WindowOptions {
35                focus: true,
36                ..Default::default()
37            },
38            |_, cx| cx.new(|_| GifViewer::new(gif_path)),
39        )
40        .unwrap();
41        cx.activate(true);
42    });
43}
44
45#[cfg(not(target_family = "wasm"))]
46fn main() {
47    env_logger::init();
48    run_example();
49}
50
51#[cfg(target_family = "wasm")]
52#[wasm_bindgen::prelude::wasm_bindgen(start)]
53pub fn start() {
54    gpui_platform::web_init();
55    run_example();
56}