1use std::{fs, path::PathBuf, time::Duration};
2
3use anyhow::Result;
4use gpui::{
5 App, Application, AssetSource, Bounds, BoxShadow, ClickEvent, Context, SharedString, Task,
6 Timer, Window, WindowBounds, WindowOptions, div, hsla, img, point, prelude::*, px, rgb, size,
7 svg,
8};
9
10struct Assets {
11 base: PathBuf,
12}
13
14impl AssetSource for Assets {
15 fn load(&self, path: &str) -> Result<Option<std::borrow::Cow<'static, [u8]>>> {
16 fs::read(self.base.join(path))
17 .map(|data| Some(std::borrow::Cow::Owned(data)))
18 .map_err(|e| e.into())
19 }
20
21 fn list(&self, path: &str) -> Result<Vec<SharedString>> {
22 fs::read_dir(self.base.join(path))
23 .map(|entries| {
24 entries
25 .filter_map(|entry| {
26 entry
27 .ok()
28 .and_then(|entry| entry.file_name().into_string().ok())
29 .map(SharedString::from)
30 })
31 .collect()
32 })
33 .map_err(|e| e.into())
34 }
35}
36
37struct HelloWorld {
38 _task: Option<Task<()>>,
39 opacity: f32,
40}
41
42impl HelloWorld {
43 fn new(_window: &mut Window, _: &mut Context<Self>) -> Self {
44 Self {
45 _task: None,
46 opacity: 0.5,
47 }
48 }
49
50 fn change_opacity(&mut self, _: &ClickEvent, window: &mut Window, cx: &mut Context<Self>) {
51 self.opacity = 0.0;
52 cx.notify();
53
54 self._task = Some(cx.spawn_in(window, async move |view, cx| {
55 loop {
56 Timer::after(Duration::from_secs_f32(0.05)).await;
57 let mut stop = false;
58 let _ = cx.update(|_, cx| {
59 view.update(cx, |view, cx| {
60 if view.opacity >= 1.0 {
61 stop = true;
62 return;
63 }
64
65 view.opacity += 0.1;
66 cx.notify();
67 })
68 });
69
70 if stop {
71 break;
72 }
73 }
74 }));
75 }
76}
77
78impl Render for HelloWorld {
79 fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
80 div()
81 .flex()
82 .flex_row()
83 .size_full()
84 .bg(rgb(0xe0e0e0))
85 .text_xl()
86 .child(
87 div()
88 .flex()
89 .size_full()
90 .justify_center()
91 .items_center()
92 .border_1()
93 .text_color(gpui::blue())
94 .child(div().child("This is background text.")),
95 )
96 .child(
97 div()
98 .id("panel")
99 .on_click(cx.listener(Self::change_opacity))
100 .absolute()
101 .top_8()
102 .left_8()
103 .right_8()
104 .bottom_8()
105 .opacity(self.opacity)
106 .flex()
107 .justify_center()
108 .items_center()
109 .bg(gpui::white())
110 .border_3()
111 .border_color(gpui::red())
112 .text_color(gpui::yellow())
113 .child(
114 div()
115 .flex()
116 .flex_col()
117 .gap_2()
118 .justify_center()
119 .items_center()
120 .size(px(300.))
121 .bg(gpui::blue())
122 .border_3()
123 .border_color(gpui::black())
124 .shadow(vec![BoxShadow {
125 color: hsla(0.0, 0.0, 0.0, 0.5),
126 blur_radius: px(1.0),
127 spread_radius: px(5.0),
128 offset: point(px(10.0), px(10.0)),
129 }])
130 .child(img("image/app-icon.png").size_8())
131 .child("Opacity Panel (Click to test)")
132 .child(
133 div()
134 .id("deep-level-text")
135 .flex()
136 .justify_center()
137 .items_center()
138 .p_4()
139 .bg(gpui::black())
140 .text_color(gpui::white())
141 .text_decoration_2()
142 .text_decoration_wavy()
143 .text_decoration_color(gpui::red())
144 .child(format!("opacity: {:.1}", self.opacity)),
145 )
146 .child(
147 svg()
148 .path("image/arrow_circle.svg")
149 .text_color(gpui::black())
150 .text_2xl()
151 .size_8(),
152 )
153 .child("🎊✈️🎉🎈🎁🎂")
154 .child(img("image/black-cat-typing.gif").size_12()),
155 ),
156 )
157 }
158}
159
160fn main() {
161 Application::new()
162 .with_assets(Assets {
163 base: PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("examples"),
164 })
165 .run(|cx: &mut App| {
166 let bounds = Bounds::centered(None, size(px(500.0), px(500.0)), cx);
167 cx.open_window(
168 WindowOptions {
169 window_bounds: Some(WindowBounds::Windowed(bounds)),
170 ..Default::default()
171 },
172 |window, cx| cx.new(|cx| HelloWorld::new(window, cx)),
173 )
174 .unwrap();
175 cx.activate(true);
176 });
177}