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
61actions!(image, [Quit]);
62
63fn main() {
64 env_logger::init();
65
66 App::new().run(|cx: &mut AppContext| {
67 cx.activate(true);
68 cx.on_action(|_: &Quit, cx| cx.quit());
69 cx.bind_keys([KeyBinding::new("cmd-q", Quit, None)]);
70 cx.set_menus(vec![Menu {
71 name: "Image".into(),
72 items: vec![MenuItem::action("Quit", Quit)],
73 }]);
74
75 let window_options = WindowOptions {
76 titlebar: Some(TitlebarOptions {
77 title: Some(SharedString::from("Image Example")),
78 appears_transparent: false,
79 ..Default::default()
80 }),
81
82 window_bounds: Some(WindowBounds::Windowed(Bounds {
83 size: size(px(1100.), px(600.)),
84 origin: Point::new(px(200.), px(200.)),
85 })),
86
87 ..Default::default()
88 };
89
90 cx.open_window(window_options, |cx| {
91 cx.new_view(|_cx| ImageShowcase {
92 // Relative path to your root project path
93 local_resource: Arc::new(PathBuf::from_str("examples/image/app-icon.png").unwrap()),
94 remote_resource: "https://picsum.photos/512/512".into(),
95 })
96 })
97 .unwrap();
98 });
99}