BlueGit

ict rusty-teams

Files

rusty-teams / crates / app / src / views / attachments.rs

attachments.rs

eba4ad94 History
attachments.rs 8.4 KiB Download Raw
1use gpui_kit::assets::IconName;
2use gpui_kit::component::{h_flex, v_flex};
3use gpui_kit::prelude::FluentBuilder as _;
4use gpui_kit::*;
5use teams_core::{FileCard, FileKind, ImageRef};
6
7use super::widgets::icon;
8use crate::data::Directory;
9use crate::format;
10use crate::rows::LocalImage;
11use crate::theme;
12
13pub const IMAGE_MAX_WIDTH: f32 = 360.;
14const IMAGE_MAX_HEIGHT: f32 = 300.;
15const IMAGE_MIN_SIDE: f32 = 48.;
16const IMAGE_FALLBACK: (f32, f32) = (240., 160.);
17const IMAGE_RADIUS: f32 = 10.;
18const FILE_CARD_WIDTH: f32 = 300.;
19const FILE_BADGE_SIZE: f32 = 34.;
20
21pub fn fit_image(size: Option<(u32, u32)>) -> (f32, f32) {
22 let Some((width, height)) = size.filter(|(width, height)| *width > 0 && *height > 0) else {
23 return IMAGE_FALLBACK;
24 };
25 let (width, height) = (width as f32, height as f32);
26 let scale = (IMAGE_MAX_WIDTH / width)
27 .min(IMAGE_MAX_HEIGHT / height)
28 .min(1.);
29 (
30 (width * scale).max(IMAGE_MIN_SIDE).round(),
31 (height * scale).max(IMAGE_MIN_SIDE).round(),
32 )
33}
34
35fn declared_size(image: &ImageRef) -> Option<(u32, u32)> {
36 image.width.zip(image.height)
37}
38
39pub fn image_size(image: &ImageRef, directory: &Directory) -> (f32, f32) {
40 let loaded = directory.image(&image.url).and_then(|entry| entry.size);
41 fit_image(declared_size(image).or(loaded))
42}
43
44fn image_view(image: &ImageRef, id: String, directory: &Directory) -> AnyElement {
45 let (width, height) = image_size(image, directory);
46 let frame = div()
47 .id(ElementId::Name(id.into()))
48 .relative()
49 .w(px(width))
50 .h(px(height))
51 .max_w(relative(1.))
52 .flex_none()
53 .rounded(px(IMAGE_RADIUS))
54 .overflow_hidden();
55 let Some(entry) = directory.image(&image.url) else {
56 return frame
57 .flex()
58 .flex_col()
59 .gap(px(6.))
60 .items_center()
61 .justify_center()
62 .bg(theme::background())
63 .border_1()
64 .border_color(theme::border_strong())
65 .child(icon(IconName::Image, 24., theme::text_muted()))
66 .child(
67 div()
68 .text_size(px(11.))
69 .text_color(theme::text_muted())
70 .child("Loading image"),
71 )
72 .into_any_element();
73 };
74 let path = entry.path.clone();
75 frame
76 .cursor_pointer()
77 .border_1()
78 .border_color(theme::border())
79 .hover(|frame| frame.border_color(theme::accent()))
80 .child(
81 img(entry.path.clone())
82 .size_full()
83 .rounded(px(IMAGE_RADIUS))
84 .object_fit(ObjectFit::Cover),
85 )
86 .on_click(move |_, _, cx| cx.open_with_system(&path))
87 .into_any_element()
88}
89
90struct KindStyle {
91 label: &'static str,
92 description: &'static str,
93 color: u32,
94}
95
96fn kind_style(kind: FileKind) -> KindStyle {
97 let (label, description, color) = match kind {
98 FileKind::Word => ("DOC", "Word document", 0x2563EB),
99 FileKind::Excel => ("XLS", "Excel spreadsheet", 0x15803D),
100 FileKind::PowerPoint => ("PPT", "PowerPoint", 0xC2410C),
101 FileKind::Pdf => ("PDF", "PDF document", 0xB91C1C),
102 FileKind::Image => ("IMG", "Image", 0x7C3AED),
103 FileKind::Archive => ("ZIP", "Archive", 0xB45309),
104 FileKind::Other => ("", "File", 0x525252),
105 };
106 KindStyle {
107 label,
108 description,
109 color,
110 }
111}
112
113pub fn file_subtitle(card: &FileCard) -> String {
114 let description = kind_style(card.kind).description;
115 match card.size {
116 Some(size) => format!("{description} \u{b7} {}", format::file_size_label(size)),
117 None => description.to_owned(),
118 }
119}
120
121pub fn file_badge(kind: FileKind, size: f32) -> Div {
122 let style = kind_style(kind);
123 div()
124 .size(px(size))
125 .flex_none()
126 .flex()
127 .items_center()
128 .justify_center()
129 .rounded(px(7.))
130 .bg(theme::color(style.color))
131 .text_color(gpui_kit::white())
132 .text_size(px(10.5))
133 .font_weight(FontWeight::BOLD)
134 .when(style.label.is_empty(), |badge| {
135 badge.child(icon(IconName::File, 16., gpui_kit::white()))
136 })
137 .when(!style.label.is_empty(), |badge| badge.child(style.label))
138}
139
140fn local_image_view(image: &LocalImage, id: String) -> AnyElement {
141 let (width, height) = fit_image(image.size);
142 div()
143 .id(ElementId::Name(id.into()))
144 .w(px(width))
145 .h(px(height))
146 .max_w(relative(1.))
147 .flex_none()
148 .rounded(px(IMAGE_RADIUS))
149 .overflow_hidden()
150 .border_1()
151 .border_color(theme::border())
152 .child(
153 img(image.image.clone())
154 .size_full()
155 .rounded(px(IMAGE_RADIUS))
156 .object_fit(ObjectFit::Cover),
157 )
158 .into_any_element()
159}
160
161fn file_view(card: &FileCard, id: String) -> AnyElement {
162 let badge = file_badge(card.kind, FILE_BADGE_SIZE);
163 let url = card.open_url.clone();
164 h_flex()
165 .id(ElementId::Name(id.into()))
166 .w(px(FILE_CARD_WIDTH))
167 .max_w(relative(1.))
168 .gap(px(10.))
169 .px(px(8.))
170 .py(px(7.))
171 .items_center()
172 .rounded(px(9.))
173 .bg(theme::surface())
174 .border_1()
175 .border_color(theme::border_strong())
176 .cursor_pointer()
177 .hover(|card| card.bg(theme::row_hover()).border_color(theme::accent()))
178 .child(badge)
179 .child(
180 v_flex()
181 .flex_1()
182 .min_w_0()
183 .child(
184 div()
185 .truncate()
186 .text_size(px(13.))
187 .text_color(theme::text())
188 .child(card.name.clone()),
189 )
190 .child(
191 div()
192 .truncate()
193 .text_size(px(11.))
194 .text_color(theme::text_muted())
195 .child(file_subtitle(card)),
196 ),
197 )
198 .child(icon(IconName::ExternalLink, 14., theme::text_muted()))
199 .on_click(move |_, _, cx| cx.open_url(&url))
200 .into_any_element()
201}
202
203pub fn attachments_view(
204 images: &[ImageRef],
205 local_images: &[LocalImage],
206 files: &[FileCard],
207 id: &str,
208 directory: &Directory,
209) -> Option<Div> {
210 if images.is_empty() && local_images.is_empty() && files.is_empty() {
211 return None;
212 }
213 Some(
214 v_flex()
215 .gap(px(6.))
216 .items_start()
217 .children(
218 images.iter().enumerate().map(|(index, image)| {
219 image_view(image, format!("{id}-image-{index}"), directory)
220 }),
221 )
222 .children(
223 local_images.iter().enumerate().map(|(index, image)| {
224 local_image_view(image, format!("{id}-local-image-{index}"))
225 }),
226 )
227 .children(
228 files
229 .iter()
230 .enumerate()
231 .map(|(index, card)| file_view(card, format!("{id}-file-{index}"))),
232 ),
233 )
234}
235
236#[cfg(test)]
237mod tests {
238 use super::{IMAGE_FALLBACK, file_subtitle, fit_image};
239 use teams_core::{FileCard, FileKind};
240
241 #[test]
242 fn wide_images_shrink_to_the_max_width_keeping_aspect() {
243 assert_eq!(fit_image(Some((720, 360))), (360., 180.));
244 }
245
246 #[test]
247 fn tall_images_shrink_to_the_max_height() {
248 assert_eq!(fit_image(Some((300, 600))), (150., 300.));
249 }
250
251 #[test]
252 fn small_images_keep_their_size() {
253 assert_eq!(fit_image(Some((200, 100))), (200., 100.));
254 }
255
256 #[test]
257 fn unknown_or_empty_sizes_use_the_fallback() {
258 assert_eq!(fit_image(None), IMAGE_FALLBACK);
259 assert_eq!(fit_image(Some((0, 10))), IMAGE_FALLBACK);
260 }
261
262 #[test]
263 fn thin_images_keep_a_clickable_side() {
264 assert_eq!(fit_image(Some((3000, 10))), (360., 48.));
265 }
266
267 #[test]
268 fn subtitle_adds_the_size_when_known() {
269 let card = FileCard {
270 name: "a.pdf".into(),
271 kind: FileKind::Pdf,
272 content_type: None,
273 size: Some(2048),
274 open_url: String::new(),
275 };
276 assert_eq!(file_subtitle(&card), "PDF document \u{b7} 2 KB");
277 let unknown = FileCard { size: None, ..card };
278 assert_eq!(file_subtitle(&unknown), "PDF document");
279 }
280}