/builds/framezero/productivity/src/desktop/assets.rs
Line | Count | Source |
1 | | use std::borrow::Cow; |
2 | | |
3 | | use anyhow::Result; |
4 | | use gpui::{AssetSource, SharedString}; |
5 | | |
6 | | /// Embed UI icons so packaged apps do not depend on a working directory. |
7 | | pub struct Assets; |
8 | | |
9 | | /// Formatting and note-action icons are from Lucide (ISC); see `assets/icons/LICENSE-LUCIDE`. |
10 | | /// The Linux title bar's window buttons reuse Lucide glyphs under gpui-component's names. |
11 | | const ICONS: [(&str, &[u8]); 46] = [ |
12 | | ( |
13 | | "icons/window-close.svg", |
14 | | include_bytes!("../../assets/icons/x.svg"), |
15 | | ), |
16 | | ( |
17 | | "icons/window-minimize.svg", |
18 | | include_bytes!("../../assets/icons/minus.svg"), |
19 | | ), |
20 | | ( |
21 | | "icons/window-maximize.svg", |
22 | | include_bytes!("../../assets/icons/square.svg"), |
23 | | ), |
24 | | ( |
25 | | "icons/window-restore.svg", |
26 | | include_bytes!("../../assets/icons/copy.svg"), |
27 | | ), |
28 | | ( |
29 | | "icons/clock-plus.svg", |
30 | | include_bytes!("../../assets/icons/clock-plus.svg"), |
31 | | ), |
32 | | ( |
33 | | "icons/minus.svg", |
34 | | include_bytes!("../../assets/icons/minus.svg"), |
35 | | ), |
36 | | ( |
37 | | "icons/plus.svg", |
38 | | include_bytes!("../../assets/icons/plus.svg"), |
39 | | ), |
40 | | ("icons/x.svg", include_bytes!("../../assets/icons/x.svg")), |
41 | | ( |
42 | | "icons/calendar.svg", |
43 | | include_bytes!("../../assets/icons/calendar.svg"), |
44 | | ), |
45 | | ( |
46 | | "icons/arrow-left.svg", |
47 | | include_bytes!("../../assets/icons/arrow-left.svg"), |
48 | | ), |
49 | | ( |
50 | | "icons/arrow-right.svg", |
51 | | include_bytes!("../../assets/icons/arrow-right.svg"), |
52 | | ), |
53 | | ( |
54 | | "icons/circle-x.svg", |
55 | | include_bytes!("../../assets/icons/circle-x.svg"), |
56 | | ), |
57 | | ( |
58 | | "icons/chevron-down.svg", |
59 | | include_bytes!("../../assets/icons/chevron-down.svg"), |
60 | | ), |
61 | | ( |
62 | | "icons/chevron-right.svg", |
63 | | include_bytes!("../../assets/icons/chevron-right.svg"), |
64 | | ), |
65 | | ( |
66 | | "icons/eye.svg", |
67 | | include_bytes!("../../assets/icons/eye.svg"), |
68 | | ), |
69 | | ( |
70 | | "icons/eye-off.svg", |
71 | | include_bytes!("../../assets/icons/eye-off.svg"), |
72 | | ), |
73 | | ( |
74 | | "icons/chevrons-down-up.svg", |
75 | | include_bytes!("../../assets/icons/chevrons-down-up.svg"), |
76 | | ), |
77 | | ( |
78 | | "icons/chevrons-up-down.svg", |
79 | | include_bytes!("../../assets/icons/chevrons-up-down.svg"), |
80 | | ), |
81 | | ( |
82 | | "icons/settings.svg", |
83 | | include_bytes!("../../assets/icons/settings.svg"), |
84 | | ), |
85 | | ( |
86 | | "icons/info.svg", |
87 | | include_bytes!("../../assets/icons/info.svg"), |
88 | | ), |
89 | | ( |
90 | | "icons/chart-pie.svg", |
91 | | include_bytes!("../../assets/icons/chart-pie.svg"), |
92 | | ), |
93 | | ( |
94 | | "icons/star.svg", |
95 | | include_bytes!("../../assets/icons/star.svg"), |
96 | | ), |
97 | | ( |
98 | | "icons/star-filled.svg", |
99 | | include_bytes!("../../assets/icons/star-filled.svg"), |
100 | | ), |
101 | | ( |
102 | | "icons/pencil.svg", |
103 | | include_bytes!("../../assets/icons/pencil.svg"), |
104 | | ), |
105 | | ( |
106 | | "icons/task.svg", |
107 | | include_bytes!("../../assets/icons/task.svg"), |
108 | | ), |
109 | | ( |
110 | | "icons/idea.svg", |
111 | | include_bytes!("../../assets/icons/idea.svg"), |
112 | | ), |
113 | | ( |
114 | | "icons/bug.svg", |
115 | | include_bytes!("../../assets/icons/bug.svg"), |
116 | | ), |
117 | | ( |
118 | | "icons/bold.svg", |
119 | | include_bytes!("../../assets/icons/bold.svg"), |
120 | | ), |
121 | | ( |
122 | | "icons/code.svg", |
123 | | include_bytes!("../../assets/icons/code.svg"), |
124 | | ), |
125 | | ( |
126 | | "icons/text-quote.svg", |
127 | | include_bytes!("../../assets/icons/text-quote.svg"), |
128 | | ), |
129 | | ( |
130 | | "icons/square-code.svg", |
131 | | include_bytes!("../../assets/icons/square-code.svg"), |
132 | | ), |
133 | | ( |
134 | | "icons/italic.svg", |
135 | | include_bytes!("../../assets/icons/italic.svg"), |
136 | | ), |
137 | | ( |
138 | | "icons/link.svg", |
139 | | include_bytes!("../../assets/icons/link.svg"), |
140 | | ), |
141 | | ( |
142 | | "icons/heading-1.svg", |
143 | | include_bytes!("../../assets/icons/heading-1.svg"), |
144 | | ), |
145 | | ( |
146 | | "icons/heading-2.svg", |
147 | | include_bytes!("../../assets/icons/heading-2.svg"), |
148 | | ), |
149 | | ( |
150 | | "icons/list.svg", |
151 | | include_bytes!("../../assets/icons/list.svg"), |
152 | | ), |
153 | | ( |
154 | | "icons/list-todo.svg", |
155 | | include_bytes!("../../assets/icons/list-todo.svg"), |
156 | | ), |
157 | | ( |
158 | | "icons/image-plus.svg", |
159 | | include_bytes!("../../assets/icons/image-plus.svg"), |
160 | | ), |
161 | | ( |
162 | | "icons/trash.svg", |
163 | | include_bytes!("../../assets/icons/trash.svg"), |
164 | | ), |
165 | | ( |
166 | | "icons/undo-2.svg", |
167 | | include_bytes!("../../assets/icons/undo-2.svg"), |
168 | | ), |
169 | | ( |
170 | | "icons/copy.svg", |
171 | | include_bytes!("../../assets/icons/copy.svg"), |
172 | | ), |
173 | | ( |
174 | | "icons/refresh-cw.svg", |
175 | | include_bytes!("../../assets/icons/refresh-cw.svg"), |
176 | | ), |
177 | | ( |
178 | | "icons/git-pull-request.svg", |
179 | | include_bytes!("../../assets/icons/git-pull-request.svg"), |
180 | | ), |
181 | | ( |
182 | | "icons/circle-dot.svg", |
183 | | include_bytes!("../../assets/icons/circle-dot.svg"), |
184 | | ), |
185 | | ( |
186 | | "icons/ticket.svg", |
187 | | include_bytes!("../../assets/icons/ticket.svg"), |
188 | | ), |
189 | | ( |
190 | | "icons/external-link.svg", |
191 | | include_bytes!("../../assets/icons/external-link.svg"), |
192 | | ), |
193 | | ]; |
194 | | |
195 | | impl AssetSource for Assets { |
196 | 72 | fn load(&self, path: &str) -> Result<Option<Cow<'static, [u8]>>> { |
197 | 72 | Ok(ICONS |
198 | 72 | .iter() |
199 | 1.77k | .find(|(name, _)| *name == path) |
200 | 71 | .map(|(_, bytes)| Cow::Borrowed(*bytes))) |
201 | 72 | } |
202 | | |
203 | 1 | fn list(&self, path: &str) -> Result<Vec<SharedString>> { |
204 | 1 | Ok(ICONS |
205 | 1 | .iter() |
206 | 46 | .filter(|(name, _)| name.starts_with(path)) |
207 | 46 | .map(|(name, _)| (*name).into()) |
208 | | .collect()) |
209 | 1 | } |
210 | | } |
211 | | |
212 | | #[cfg(test)] |
213 | | mod tests { |
214 | | use super::*; |
215 | | use gpui_component::IconNamed; |
216 | | |
217 | | #[test] |
218 | 1 | fn ui_icons_are_embedded() { |
219 | 1 | let assets = Assets; |
220 | 1 | assert_eq!(assets.list("icons/").unwrap().len(), ICONS.len()); |
221 | 1 | for icon in [ |
222 | 1 | gpui_component::IconName::Calendar, |
223 | 1 | gpui_component::IconName::ArrowLeft, |
224 | 1 | gpui_component::IconName::ArrowRight, |
225 | 1 | gpui_component::IconName::CircleX, |
226 | 1 | gpui_component::IconName::ChevronDown, |
227 | 1 | gpui_component::IconName::Settings, |
228 | 1 | gpui_component::IconName::Info, |
229 | 1 | gpui_component::IconName::Star, |
230 | 1 | gpui_component::IconName::WindowClose, |
231 | 1 | gpui_component::IconName::WindowMinimize, |
232 | 1 | gpui_component::IconName::WindowMaximize, |
233 | 1 | gpui_component::IconName::WindowRestore, |
234 | 1 | ] { |
235 | 12 | assert!(!assets.load(&icon.path()).unwrap().unwrap().is_empty()); |
236 | | } |
237 | 1 | assert!(assets.load("icons/pencil.svg").unwrap().is_some()); |
238 | 1 | assert!(assets.load("icons/star-filled.svg").unwrap().is_some()); |
239 | 1 | assert!(assets.load("icons/missing.svg").unwrap().is_none()); |
240 | 1 | } |
241 | | |
242 | | /// Every `"icons/….svg"` literal in the interface must be embedded, or the icon is blank. |
243 | | #[test] |
244 | 1 | fn icons_used_by_the_interface_are_embedded() { |
245 | 5 | fn visit(dir: &std::path::Path, used: &mut Vec<(String, std::path::PathBuf)>) { |
246 | 5 | for entry in std::fs::read_dir(dir).unwrap() { |
247 | 32 | let path = entry.unwrap().path(); |
248 | 32 | if path.is_dir() { |
249 | 4 | visit(&path, used); |
250 | 28 | } else if path.extension().is_some_and(|ext| ext == "rs") { |
251 | 28 | let source = std::fs::read_to_string(&path).unwrap(); |
252 | 28 | for (start, _) in source.match_indices("\"icons/") { |
253 | 57 | let rest = &source[start + 1..]; |
254 | 57 | if let Some(end) = rest.find('"') |
255 | 57 | && rest[..end].ends_with(".svg") |
256 | | { |
257 | 57 | used.push((rest[..end].to_owned(), path.clone())); |
258 | 0 | } |
259 | | } |
260 | 0 | } |
261 | | } |
262 | 5 | } |
263 | 1 | let mut used = Vec::new(); |
264 | 1 | visit( |
265 | 1 | &std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("src/ui"), |
266 | 1 | &mut used, |
267 | 1 | ); |
268 | 1 | assert!(!used.is_empty()); |
269 | 1 | for (icon, file) in used { |
270 | 57 | assert!( |
271 | 57 | Assets.load(&icon).unwrap().is_some(), |
272 | | "{icon} used in {} is not embedded", |
273 | | file.display() |
274 | | ); |
275 | | } |
276 | 1 | } |
277 | | } |