| 1 | use std::rc::Rc; |
| 2 | |
| 3 | use gpui_kit::assets::IconName; |
| 4 | use gpui_kit::component::{h_flex, tooltip::Tooltip, v_flex}; |
| 5 | use gpui_kit::prelude::FluentBuilder as _; |
| 6 | use gpui_kit::*; |
| 7 | use teams_core::{FileCard, FileKind, ImageRef}; |
| 8 | |
| 9 | use super::widgets::{icon, symbol}; |
| 10 | use crate::data::Directory; |
| 11 | use crate::downloads::{self, DownloadState}; |
| 12 | use crate::format; |
| 13 | use crate::rows::LocalImage; |
| 14 | use crate::theme; |
| 15 | |
| 16 | pub const IMAGE_MAX_WIDTH: f32 = 360.; |
| 17 | const IMAGE_MAX_HEIGHT: f32 = 300.; |
| 18 | const IMAGE_MIN_SIDE: f32 = 48.; |
| 19 | const IMAGE_FALLBACK: (f32, f32) = (240., 160.); |
| 20 | const IMAGE_RADIUS: f32 = 10.; |
| 21 | const FILE_CARD_WIDTH: f32 = 300.; |
| 22 | const FILE_BADGE_SIZE: f32 = 34.; |
| 23 | const SAVE_BUTTON_SIZE: f32 = 28.; |
| 24 | const SAVE_BUTTON_RADIUS: f32 = 6.; |
| 25 | const SAVE_PROGRESS_HEIGHT: f32 = 2.; |
| 26 | |
| 27 | pub type FileActivate = Rc<dyn Fn(usize, &mut App)>; |
| 28 | |
| 29 | #[derive(Clone)] |
| 30 | pub struct FileActions { |
| 31 | pub states: Vec<Option<DownloadState>>, |
| 32 | pub activate: FileActivate, |
| 33 | } |
| 34 | |
| 35 | pub fn fit_image(size: Option<(u32, u32)>) -> (f32, f32) { |
| 36 | let Some((width, height)) = size.filter(|(width, height)| *width > 0 && *height > 0) else { |
| 37 | return IMAGE_FALLBACK; |
| 38 | }; |
| 39 | let (width, height) = (width as f32, height as f32); |
| 40 | let scale = (IMAGE_MAX_WIDTH / width) |
| 41 | .min(IMAGE_MAX_HEIGHT / height) |
| 42 | .min(1.); |
| 43 | ( |
| 44 | (width * scale).max(IMAGE_MIN_SIDE).round(), |
| 45 | (height * scale).max(IMAGE_MIN_SIDE).round(), |
| 46 | ) |
| 47 | } |
| 48 | |
| 49 | fn declared_size(image: &ImageRef) -> Option<(u32, u32)> { |
| 50 | image.width.zip(image.height) |
| 51 | } |
| 52 | |
| 53 | pub fn image_size(image: &ImageRef, directory: &Directory) -> (f32, f32) { |
| 54 | let loaded = directory.image(&image.url).and_then(|entry| entry.size); |
| 55 | fit_image(declared_size(image).or(loaded)) |
| 56 | } |
| 57 | |
| 58 | fn image_view(image: &ImageRef, id: String, directory: &Directory) -> AnyElement { |
| 59 | let (width, height) = image_size(image, directory); |
| 60 | let frame = div() |
| 61 | .id(ElementId::Name(id.into())) |
| 62 | .relative() |
| 63 | .w(px(width)) |
| 64 | .h(px(height)) |
| 65 | .max_w(relative(1.)) |
| 66 | .flex_none() |
| 67 | .rounded(px(IMAGE_RADIUS)) |
| 68 | .overflow_hidden(); |
| 69 | let Some(entry) = directory.image(&image.url) else { |
| 70 | return frame |
| 71 | .flex() |
| 72 | .flex_col() |
| 73 | .gap(px(6.)) |
| 74 | .items_center() |
| 75 | .justify_center() |
| 76 | .bg(theme::background()) |
| 77 | .border_1() |
| 78 | .border_color(theme::border_strong()) |
| 79 | .child(icon(IconName::Image, 24., theme::text_muted())) |
| 80 | .child( |
| 81 | div() |
| 82 | .text_size(px(11.)) |
| 83 | .text_color(theme::text_muted()) |
| 84 | .child("Loading image"), |
| 85 | ) |
| 86 | .into_any_element(); |
| 87 | }; |
| 88 | let path = entry.path.clone(); |
| 89 | frame |
| 90 | .cursor_pointer() |
| 91 | .border_1() |
| 92 | .border_color(theme::border()) |
| 93 | .hover(|frame| frame.border_color(theme::accent())) |
| 94 | .child( |
| 95 | img(entry.path.clone()) |
| 96 | .size_full() |
| 97 | .rounded(px(IMAGE_RADIUS)) |
| 98 | .object_fit(ObjectFit::Cover), |
| 99 | ) |
| 100 | .on_click(move |_, _, cx| cx.open_with_system(&path)) |
| 101 | .into_any_element() |
| 102 | } |
| 103 | |
| 104 | struct KindStyle { |
| 105 | label: &'static str, |
| 106 | description: &'static str, |
| 107 | color: u32, |
| 108 | } |
| 109 | |
| 110 | fn kind_style(kind: FileKind) -> KindStyle { |
| 111 | let (label, description, color) = match kind { |
| 112 | FileKind::Word => ("DOC", "Word document", 0x2563EB), |
| 113 | FileKind::Excel => ("XLS", "Excel spreadsheet", 0x15803D), |
| 114 | FileKind::PowerPoint => ("PPT", "PowerPoint", 0xC2410C), |
| 115 | FileKind::Pdf => ("PDF", "PDF document", 0xB91C1C), |
| 116 | FileKind::Image => ("IMG", "Image", 0x7C3AED), |
| 117 | FileKind::Archive => ("ZIP", "Archive", 0xB45309), |
| 118 | FileKind::Other => ("", "File", 0x525252), |
| 119 | }; |
| 120 | KindStyle { |
| 121 | label, |
| 122 | description, |
| 123 | color, |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | pub fn file_subtitle(card: &FileCard) -> String { |
| 128 | let description = kind_style(card.kind).description; |
| 129 | match card.size { |
| 130 | Some(size) => format!("{description} \u{b7} {}", format::file_size_label(size)), |
| 131 | None => description.to_owned(), |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | pub fn file_badge(kind: FileKind, size: f32) -> Div { |
| 136 | let style = kind_style(kind); |
| 137 | div() |
| 138 | .size(px(size)) |
| 139 | .flex_none() |
| 140 | .flex() |
| 141 | .items_center() |
| 142 | .justify_center() |
| 143 | .rounded(px(7.)) |
| 144 | .bg(theme::color(style.color)) |
| 145 | .text_color(gpui_kit::white()) |
| 146 | .text_size(px(10.5)) |
| 147 | .font_weight(FontWeight::BOLD) |
| 148 | .when(style.label.is_empty(), |badge| { |
| 149 | badge.child(icon(IconName::File, 16., gpui_kit::white())) |
| 150 | }) |
| 151 | .when(!style.label.is_empty(), |badge| badge.child(style.label)) |
| 152 | } |
| 153 | |
| 154 | fn local_image_view(image: &LocalImage, id: String) -> AnyElement { |
| 155 | let (width, height) = fit_image(image.size); |
| 156 | div() |
| 157 | .id(ElementId::Name(id.into())) |
| 158 | .w(px(width)) |
| 159 | .h(px(height)) |
| 160 | .max_w(relative(1.)) |
| 161 | .flex_none() |
| 162 | .rounded(px(IMAGE_RADIUS)) |
| 163 | .overflow_hidden() |
| 164 | .border_1() |
| 165 | .border_color(theme::border()) |
| 166 | .child( |
| 167 | img(image.image.clone()) |
| 168 | .size_full() |
| 169 | .rounded(px(IMAGE_RADIUS)) |
| 170 | .object_fit(ObjectFit::Cover), |
| 171 | ) |
| 172 | .into_any_element() |
| 173 | } |
| 174 | |
| 175 | fn save_button( |
| 176 | state: Option<&DownloadState>, |
| 177 | id: String, |
| 178 | index: usize, |
| 179 | activate: FileActivate, |
| 180 | ) -> Stateful<Div> { |
| 181 | let look = downloads::button_look(state); |
| 182 | let tooltip = look.tooltip.clone(); |
| 183 | let clickable = look.clickable; |
| 184 | let button = div() |
| 185 | .id(ElementId::Name(id.into())) |
| 186 | .size(px(SAVE_BUTTON_SIZE)) |
| 187 | .flex_none() |
| 188 | .flex() |
| 189 | .items_center() |
| 190 | .justify_center() |
| 191 | .rounded(px(SAVE_BUTTON_RADIUS)) |
| 192 | .tooltip(move |window, cx| Tooltip::new(tooltip.clone()).build(window, cx)) |
| 193 | .when(look.clickable, |button| { |
| 194 | button |
| 195 | .cursor_pointer() |
| 196 | .hover(|button| button.bg(theme::row_hover())) |
| 197 | }) |
| 198 | .on_click(move |_, _, cx| { |
| 199 | cx.stop_propagation(); |
| 200 | if clickable { |
| 201 | activate(index, cx); |
| 202 | } |
| 203 | }); |
| 204 | match look.percent { |
| 205 | Some(percent) => button.child( |
| 206 | div() |
| 207 | .text_size(px(11.)) |
| 208 | .text_color(theme::text_muted()) |
| 209 | .child(downloads::percent_label(percent)), |
| 210 | ), |
| 211 | None => button.child(symbol(look.symbol, 16., theme::text_muted())), |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | fn file_view( |
| 216 | card: &FileCard, |
| 217 | id: String, |
| 218 | index: usize, |
| 219 | actions: Option<&FileActions>, |
| 220 | ) -> AnyElement { |
| 221 | let badge = file_badge(card.kind, FILE_BADGE_SIZE); |
| 222 | let url = card.open_url.clone(); |
| 223 | let state = actions |
| 224 | .and_then(|actions| actions.states.get(index)) |
| 225 | .and_then(Option::as_ref); |
| 226 | let progress = downloads::progress_fraction(state); |
| 227 | let button = actions |
| 228 | .filter(|_| !card.open_url.is_empty()) |
| 229 | .map(|actions| save_button(state, format!("{id}-save"), index, actions.activate.clone())); |
| 230 | h_flex() |
| 231 | .id(ElementId::Name(id.into())) |
| 232 | .relative() |
| 233 | .overflow_hidden() |
| 234 | .w(px(FILE_CARD_WIDTH)) |
| 235 | .max_w(relative(1.)) |
| 236 | .gap(px(10.)) |
| 237 | .px(px(8.)) |
| 238 | .py(px(7.)) |
| 239 | .items_center() |
| 240 | .rounded(px(9.)) |
| 241 | .bg(theme::surface()) |
| 242 | .border_1() |
| 243 | .border_color(theme::border_strong()) |
| 244 | .cursor_pointer() |
| 245 | .hover(|card| card.bg(theme::row_hover()).border_color(theme::accent())) |
| 246 | .child(badge) |
| 247 | .child( |
| 248 | v_flex() |
| 249 | .flex_1() |
| 250 | .min_w_0() |
| 251 | .child( |
| 252 | div() |
| 253 | .truncate() |
| 254 | .text_size(px(13.)) |
| 255 | .text_color(theme::text()) |
| 256 | .child(card.name.clone()), |
| 257 | ) |
| 258 | .child( |
| 259 | div() |
| 260 | .truncate() |
| 261 | .text_size(px(11.)) |
| 262 | .text_color(theme::text_muted()) |
| 263 | .child(file_subtitle(card)), |
| 264 | ), |
| 265 | ) |
| 266 | .child(icon(IconName::ExternalLink, 14., theme::text_muted())) |
| 267 | .children(button) |
| 268 | .when_some(progress, |card, fraction| { |
| 269 | card.child( |
| 270 | div() |
| 271 | .absolute() |
| 272 | .left_0() |
| 273 | .bottom_0() |
| 274 | .h(px(SAVE_PROGRESS_HEIGHT)) |
| 275 | .w(relative(fraction)) |
| 276 | .bg(theme::accent()), |
| 277 | ) |
| 278 | }) |
| 279 | .on_click(move |_, _, cx| cx.open_url(&url)) |
| 280 | .into_any_element() |
| 281 | } |
| 282 | |
| 283 | pub fn attachments_view( |
| 284 | images: &[ImageRef], |
| 285 | local_images: &[LocalImage], |
| 286 | files: &[FileCard], |
| 287 | id: &str, |
| 288 | directory: &Directory, |
| 289 | file_actions: Option<&FileActions>, |
| 290 | ) -> Option<Div> { |
| 291 | if images.is_empty() && local_images.is_empty() && files.is_empty() { |
| 292 | return None; |
| 293 | } |
| 294 | Some( |
| 295 | v_flex() |
| 296 | .gap(px(6.)) |
| 297 | .items_start() |
| 298 | .children( |
| 299 | images.iter().enumerate().map(|(index, image)| { |
| 300 | image_view(image, format!("{id}-image-{index}"), directory) |
| 301 | }), |
| 302 | ) |
| 303 | .children( |
| 304 | local_images.iter().enumerate().map(|(index, image)| { |
| 305 | local_image_view(image, format!("{id}-local-image-{index}")) |
| 306 | }), |
| 307 | ) |
| 308 | .children(files.iter().enumerate().map(|(index, card)| { |
| 309 | file_view(card, format!("{id}-file-{index}"), index, file_actions) |
| 310 | })), |
| 311 | ) |
| 312 | } |
| 313 | |
| 314 | #[cfg(test)] |
| 315 | mod tests { |
| 316 | use super::{IMAGE_FALLBACK, file_subtitle, fit_image}; |
| 317 | use teams_core::{FileCard, FileKind}; |
| 318 | |
| 319 | #[test] |
| 320 | fn wide_images_shrink_to_the_max_width_keeping_aspect() { |
| 321 | assert_eq!(fit_image(Some((720, 360))), (360., 180.)); |
| 322 | } |
| 323 | |
| 324 | #[test] |
| 325 | fn tall_images_shrink_to_the_max_height() { |
| 326 | assert_eq!(fit_image(Some((300, 600))), (150., 300.)); |
| 327 | } |
| 328 | |
| 329 | #[test] |
| 330 | fn small_images_keep_their_size() { |
| 331 | assert_eq!(fit_image(Some((200, 100))), (200., 100.)); |
| 332 | } |
| 333 | |
| 334 | #[test] |
| 335 | fn unknown_or_empty_sizes_use_the_fallback() { |
| 336 | assert_eq!(fit_image(None), IMAGE_FALLBACK); |
| 337 | assert_eq!(fit_image(Some((0, 10))), IMAGE_FALLBACK); |
| 338 | } |
| 339 | |
| 340 | #[test] |
| 341 | fn thin_images_keep_a_clickable_side() { |
| 342 | assert_eq!(fit_image(Some((3000, 10))), (360., 48.)); |
| 343 | } |
| 344 | |
| 345 | #[test] |
| 346 | fn subtitle_adds_the_size_when_known() { |
| 347 | let card = FileCard { |
| 348 | name: "a.pdf".into(), |
| 349 | kind: FileKind::Pdf, |
| 350 | content_type: None, |
| 351 | size: Some(2048), |
| 352 | open_url: String::new(), |
| 353 | }; |
| 354 | assert_eq!(file_subtitle(&card), "PDF document \u{b7} 2 KB"); |
| 355 | let unknown = FileCard { size: None, ..card }; |
| 356 | assert_eq!(file_subtitle(&unknown), "PDF document"); |
| 357 | } |
| 358 | } |