BlueGit

ict rusty-teams

Files

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

attachments.rs

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