1use std::path::PathBuf;
2use std::str::FromStr;
3use std::sync::Arc;
4
5use gpui::*;
6
7#[derive(IntoElement)]
8struct ImageContainer {
9 text: SharedString,
10 src: ImageSource,
11}
12
13impl ImageContainer {
14 pub fn new(text: impl Into<SharedString>, src: impl Into<ImageSource>) -> Self {
15 Self {
16 text: text.into(),
17 src: src.into(),
18 }
19 }
20}
21
22impl RenderOnce for ImageContainer {
23 fn render(self, _: &mut WindowContext) -> impl IntoElement {
24 div().child(
25 div()
26 .flex_row()
27 .size_full()
28 .gap_4()
29 .child(self.text)
30 .child(img(self.src).w(px(512.0)).h(px(512.0))),
31 )
32 }
33}
34
35struct ImageShowcase {
36 local_resource: Arc<PathBuf>,
37 remote_resource: SharedUri,
38}
39
40impl Render for ImageShowcase {
41 fn render(&mut self, _cx: &mut ViewContext<Self>) -> impl IntoElement {
42 div()
43 .flex()
44 .flex_row()
45 .size_full()
46 .justify_center()
47 .items_center()
48 .gap_8()
49 .bg(rgb(0xFFFFFF))
50 .child(ImageContainer::new(
51 "Image loaded from a local file",
52 self.local_resource.clone(),
53 ))
54 .child(ImageContainer::new(
55 "Image loaded from a remote resource",
56 self.remote_resource.clone(),
57 ))
58 }
59}
60
61fn main() {
62 env_logger::init();
63
64 App::new().run(|cx: &mut AppContext| {
65 cx.open_window(WindowOptions::default(), |cx| {
66 cx.new_view(|_cx| ImageShowcase {
67 // Relative path to your root project path
68 local_resource: Arc::new(PathBuf::from_str("examples/image/app-icon.png").unwrap()),
69 remote_resource: "https://picsum.photos/512/512".into(),
70 })
71 });
72 });
73}