| 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, external_image_url}; |
| 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::remote_image; |
| 14 | use crate::render::{Block, render_blocks}; |
| 15 | use crate::rows::LocalImage; |
| 16 | use crate::theme; |
| 17 | |
| 18 | pub const IMAGE_MAX_WIDTH: f32 = 360.; |
| 19 | const IMAGE_MAX_HEIGHT: f32 = 300.; |
| 20 | const IMAGE_MIN_SIDE: f32 = 48.; |
| 21 | const IMAGE_FALLBACK: (f32, f32) = (240., 160.); |
| 22 | const IMAGE_RADIUS: f32 = 10.; |
| 23 | const FILE_CARD_WIDTH: f32 = 300.; |
| 24 | const FILE_BADGE_SIZE: f32 = 34.; |
| 25 | const SAVE_BUTTON_SIZE: f32 = 28.; |
| 26 | const SAVE_BUTTON_RADIUS: f32 = 6.; |
| 27 | const SAVE_PROGRESS_HEIGHT: f32 = 2.; |
| 28 | const LOCAL_IMAGE_PREFIX: &str = "../hostedContents/"; |
| 29 | const LOCAL_IMAGE_SUFFIX: &str = "/$value"; |
| 30 | |
| 31 | pub type FileActivate = Rc<dyn Fn(usize, &mut App)>; |
| 32 | pub type ImageOpen = Rc<dyn Fn(&str, &mut App)>; |
| 33 | |
| 34 | #[derive(Clone)] |
| 35 | pub struct FileActions { |
| 36 | pub states: Vec<Option<DownloadState>>, |
| 37 | pub activate: FileActivate, |
| 38 | } |
| 39 | |
| 40 | pub fn fit_image(size: Option<(u32, u32)>) -> (f32, f32) { |
| 41 | fit_image_within(size, IMAGE_MAX_HEIGHT) |
| 42 | } |
| 43 | |
| 44 | fn fit_image_within(size: Option<(u32, u32)>, max_height: f32) -> (f32, f32) { |
| 45 | let Some((width, height)) = size.filter(|(width, height)| *width > 0 && *height > 0) else { |
| 46 | return IMAGE_FALLBACK; |
| 47 | }; |
| 48 | let (width, height) = (width as f32, height as f32); |
| 49 | let scale = (IMAGE_MAX_WIDTH / width).min(max_height / height).min(1.); |
| 50 | ( |
| 51 | (width * scale).max(IMAGE_MIN_SIDE).round(), |
| 52 | (height * scale).max(IMAGE_MIN_SIDE).round(), |
| 53 | ) |
| 54 | } |
| 55 | |
| 56 | fn declared_size(image: &ImageRef) -> Option<(u32, u32)> { |
| 57 | image.width.zip(image.height) |
| 58 | } |
| 59 | |
| 60 | pub fn image_size(image: &ImageRef, directory: &Directory) -> (f32, f32) { |
| 61 | let loaded = directory.image(&image.url).and_then(|entry| entry.size); |
| 62 | let max_height = if external_image_url(&image.url).is_some() { |
| 63 | remote_image::MAX_HEIGHT as f32 |
| 64 | } else { |
| 65 | IMAGE_MAX_HEIGHT |
| 66 | }; |
| 67 | fit_image_within(declared_size(image).or(loaded), max_height) |
| 68 | } |
| 69 | |
| 70 | fn image_view( |
| 71 | image: &ImageRef, |
| 72 | id: String, |
| 73 | directory: &Directory, |
| 74 | open_image: Option<&ImageOpen>, |
| 75 | ) -> AnyElement { |
| 76 | let (width, height) = image_size(image, directory); |
| 77 | let frame = div() |
| 78 | .id(ElementId::Name(id.into())) |
| 79 | .relative() |
| 80 | .w(px(width)) |
| 81 | .h(px(height)) |
| 82 | .max_w(relative(1.)) |
| 83 | .flex_none() |
| 84 | .rounded(px(IMAGE_RADIUS)) |
| 85 | .overflow_hidden(); |
| 86 | let Some(entry) = directory.image(&image.url) else { |
| 87 | return frame |
| 88 | .flex() |
| 89 | .flex_col() |
| 90 | .gap(px(6.)) |
| 91 | .items_center() |
| 92 | .justify_center() |
| 93 | .bg(theme::background()) |
| 94 | .child(icon(IconName::Image, 24., theme::text_muted())) |
| 95 | .child( |
| 96 | div() |
| 97 | .text_size(px(11.)) |
| 98 | .text_color(theme::text_muted()) |
| 99 | .child("Loading image"), |
| 100 | ) |
| 101 | .into_any_element(); |
| 102 | }; |
| 103 | let path = entry.path.clone(); |
| 104 | let (url, open_image) = (image.url.clone(), open_image.cloned()); |
| 105 | frame |
| 106 | .cursor_pointer() |
| 107 | .child( |
| 108 | img(entry.path.clone()) |
| 109 | .size_full() |
| 110 | .rounded(px(IMAGE_RADIUS)) |
| 111 | .object_fit(ObjectFit::Cover), |
| 112 | ) |
| 113 | .on_click(move |_, _, cx| match &open_image { |
| 114 | Some(open_image) => open_image(&url, cx), |
| 115 | None => cx.open_with_system(&path), |
| 116 | }) |
| 117 | .into_any_element() |
| 118 | } |
| 119 | |
| 120 | struct KindStyle { |
| 121 | label: &'static str, |
| 122 | description: &'static str, |
| 123 | color: u32, |
| 124 | } |
| 125 | |
| 126 | fn kind_style(kind: FileKind) -> KindStyle { |
| 127 | let (label, description, color) = match kind { |
| 128 | FileKind::Word => ("DOC", "Word document", 0x2563EB), |
| 129 | FileKind::Excel => ("XLS", "Excel spreadsheet", 0x15803D), |
| 130 | FileKind::PowerPoint => ("PPT", "PowerPoint", 0xC2410C), |
| 131 | FileKind::Pdf => ("PDF", "PDF document", 0xB91C1C), |
| 132 | FileKind::Image => ("IMG", "Image", 0x7C3AED), |
| 133 | FileKind::Archive => ("ZIP", "Archive", 0xB45309), |
| 134 | FileKind::Other => ("", "File", 0x525252), |
| 135 | }; |
| 136 | KindStyle { |
| 137 | label, |
| 138 | description, |
| 139 | color, |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | pub fn file_subtitle(card: &FileCard) -> String { |
| 144 | let description = kind_style(card.kind).description; |
| 145 | match card.size { |
| 146 | Some(size) => format!("{description} \u{b7} {}", format::file_size_label(size)), |
| 147 | None => description.to_owned(), |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | pub fn file_badge(kind: FileKind, size: f32) -> Div { |
| 152 | let style = kind_style(kind); |
| 153 | div() |
| 154 | .size(px(size)) |
| 155 | .flex_none() |
| 156 | .flex() |
| 157 | .items_center() |
| 158 | .justify_center() |
| 159 | .rounded(px(7.)) |
| 160 | .bg(theme::color(style.color)) |
| 161 | .text_color(gpui_kit::white()) |
| 162 | .text_size(px(10.5)) |
| 163 | .font_weight(FontWeight::BOLD) |
| 164 | .when(style.label.is_empty(), |badge| { |
| 165 | badge.child(icon(IconName::File, 16., gpui_kit::white())) |
| 166 | }) |
| 167 | .when(!style.label.is_empty(), |badge| badge.child(style.label)) |
| 168 | } |
| 169 | |
| 170 | fn local_image_view(image: &LocalImage, id: String) -> AnyElement { |
| 171 | let (width, height) = fit_image(image.size); |
| 172 | div() |
| 173 | .id(ElementId::Name(id.into())) |
| 174 | .w(px(width)) |
| 175 | .h(px(height)) |
| 176 | .max_w(relative(1.)) |
| 177 | .flex_none() |
| 178 | .rounded(px(IMAGE_RADIUS)) |
| 179 | .overflow_hidden() |
| 180 | .child( |
| 181 | img(image.image.clone()) |
| 182 | .size_full() |
| 183 | .rounded(px(IMAGE_RADIUS)) |
| 184 | .object_fit(ObjectFit::Cover), |
| 185 | ) |
| 186 | .into_any_element() |
| 187 | } |
| 188 | |
| 189 | fn save_button( |
| 190 | state: Option<&DownloadState>, |
| 191 | id: String, |
| 192 | index: usize, |
| 193 | activate: FileActivate, |
| 194 | ) -> Stateful<Div> { |
| 195 | let look = downloads::button_look(state); |
| 196 | let tooltip = look.tooltip.clone(); |
| 197 | let clickable = look.clickable; |
| 198 | let button = div() |
| 199 | .id(ElementId::Name(id.into())) |
| 200 | .size(px(SAVE_BUTTON_SIZE)) |
| 201 | .flex_none() |
| 202 | .flex() |
| 203 | .items_center() |
| 204 | .justify_center() |
| 205 | .rounded(px(SAVE_BUTTON_RADIUS)) |
| 206 | .tooltip(move |window, cx| Tooltip::new(tooltip.clone()).build(window, cx)) |
| 207 | .when(look.clickable, |button| { |
| 208 | button |
| 209 | .cursor_pointer() |
| 210 | .hover(|button| button.bg(theme::row_hover())) |
| 211 | }) |
| 212 | .on_click(move |_, _, cx| { |
| 213 | cx.stop_propagation(); |
| 214 | if clickable { |
| 215 | activate(index, cx); |
| 216 | } |
| 217 | }); |
| 218 | match look.percent { |
| 219 | Some(percent) => button.child( |
| 220 | div() |
| 221 | .text_size(px(11.)) |
| 222 | .text_color(theme::text_muted()) |
| 223 | .child(downloads::percent_label(percent)), |
| 224 | ), |
| 225 | None => button.child(symbol(look.symbol, 16., theme::text_muted())), |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | fn file_view( |
| 230 | card: &FileCard, |
| 231 | id: String, |
| 232 | index: usize, |
| 233 | actions: Option<&FileActions>, |
| 234 | ) -> AnyElement { |
| 235 | let badge = file_badge(card.kind, FILE_BADGE_SIZE); |
| 236 | let url = card.open_url.clone(); |
| 237 | let state = actions |
| 238 | .and_then(|actions| actions.states.get(index)) |
| 239 | .and_then(Option::as_ref); |
| 240 | let progress = downloads::progress_fraction(state); |
| 241 | let button = actions |
| 242 | .filter(|_| !card.open_url.is_empty()) |
| 243 | .map(|actions| save_button(state, format!("{id}-save"), index, actions.activate.clone())); |
| 244 | h_flex() |
| 245 | .id(ElementId::Name(id.into())) |
| 246 | .relative() |
| 247 | .overflow_hidden() |
| 248 | .w_full() |
| 249 | .max_w(px(FILE_CARD_WIDTH)) |
| 250 | .gap(px(12.)) |
| 251 | .px(px(8.)) |
| 252 | .py(px(8.)) |
| 253 | .items_center() |
| 254 | .rounded(px(10.)) |
| 255 | .bg(theme::surface()) |
| 256 | .cursor_pointer() |
| 257 | .hover(|card| card.bg(theme::row_hover())) |
| 258 | .child(badge) |
| 259 | .child( |
| 260 | v_flex() |
| 261 | .flex_1() |
| 262 | .min_w_0() |
| 263 | .child( |
| 264 | div() |
| 265 | .truncate() |
| 266 | .text_size(px(13.)) |
| 267 | .text_color(theme::text()) |
| 268 | .child(card.name.clone()), |
| 269 | ) |
| 270 | .child( |
| 271 | div() |
| 272 | .truncate() |
| 273 | .text_size(px(11.)) |
| 274 | .text_color(theme::text_muted()) |
| 275 | .child(file_subtitle(card)), |
| 276 | ), |
| 277 | ) |
| 278 | .child(icon(IconName::ExternalLink, 14., theme::text_muted())) |
| 279 | .children(button) |
| 280 | .when_some(progress, |card, fraction| { |
| 281 | card.child( |
| 282 | div() |
| 283 | .absolute() |
| 284 | .left_0() |
| 285 | .bottom_0() |
| 286 | .h(px(SAVE_PROGRESS_HEIGHT)) |
| 287 | .w(relative(fraction)) |
| 288 | .bg(theme::accent()), |
| 289 | ) |
| 290 | }) |
| 291 | .on_click(move |_, _, cx| cx.open_url(&url)) |
| 292 | .into_any_element() |
| 293 | } |
| 294 | |
| 295 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 296 | enum Placement { |
| 297 | Remote(usize), |
| 298 | Local(usize), |
| 299 | } |
| 300 | |
| 301 | fn local_image_index(url: &str) -> Option<usize> { |
| 302 | url.strip_prefix(LOCAL_IMAGE_PREFIX)? |
| 303 | .strip_suffix(LOCAL_IMAGE_SUFFIX)? |
| 304 | .parse::<usize>() |
| 305 | .ok()? |
| 306 | .checked_sub(1) |
| 307 | } |
| 308 | |
| 309 | fn placement(url: &str, images: &[ImageRef], local_count: usize) -> Option<Placement> { |
| 310 | if let Some(index) = images.iter().position(|image| image.url == url) { |
| 311 | return Some(Placement::Remote(index)); |
| 312 | } |
| 313 | local_image_index(url) |
| 314 | .filter(|index| *index < local_count) |
| 315 | .map(Placement::Local) |
| 316 | } |
| 317 | |
| 318 | fn placements(blocks: &[Block], images: &[ImageRef], local_count: usize) -> Vec<Placement> { |
| 319 | blocks |
| 320 | .iter() |
| 321 | .filter_map(|block| match block { |
| 322 | Block::Image { url } => placement(url, images, local_count), |
| 323 | _ => None, |
| 324 | }) |
| 325 | .collect() |
| 326 | } |
| 327 | |
| 328 | pub fn message_body( |
| 329 | blocks: &[Block], |
| 330 | images: &[ImageRef], |
| 331 | local_images: &[LocalImage], |
| 332 | id: &str, |
| 333 | own: bool, |
| 334 | directory: &Directory, |
| 335 | open_image: Option<&ImageOpen>, |
| 336 | cx: &App, |
| 337 | ) -> Vec<AnyElement> { |
| 338 | if !blocks |
| 339 | .iter() |
| 340 | .any(|block| matches!(block, Block::Image { .. })) |
| 341 | { |
| 342 | return vec![render_blocks(blocks, id, own, cx)]; |
| 343 | } |
| 344 | let mut elements = Vec::new(); |
| 345 | let mut run_start = 0; |
| 346 | let render_run = |start: usize, end: usize, elements: &mut Vec<AnyElement>| { |
| 347 | if end > start { |
| 348 | let part_id = format!("{id}-part-{start}"); |
| 349 | elements.push(render_blocks(&blocks[start..end], &part_id, own, cx)); |
| 350 | } |
| 351 | }; |
| 352 | for (index, block) in blocks.iter().enumerate() { |
| 353 | let Block::Image { url } = block else { |
| 354 | continue; |
| 355 | }; |
| 356 | render_run(run_start, index, &mut elements); |
| 357 | run_start = index + 1; |
| 358 | match placement(url, images, local_images.len()) { |
| 359 | Some(Placement::Remote(position)) => elements.push(image_view( |
| 360 | &images[position], |
| 361 | format!("{id}-image-{position}"), |
| 362 | directory, |
| 363 | open_image, |
| 364 | )), |
| 365 | Some(Placement::Local(position)) => elements.push(local_image_view( |
| 366 | &local_images[position], |
| 367 | format!("{id}-local-image-{position}"), |
| 368 | )), |
| 369 | None => {} |
| 370 | } |
| 371 | } |
| 372 | render_run(run_start, blocks.len(), &mut elements); |
| 373 | elements |
| 374 | } |
| 375 | |
| 376 | pub fn attachments_view( |
| 377 | blocks: &[Block], |
| 378 | images: &[ImageRef], |
| 379 | local_images: &[LocalImage], |
| 380 | files: &[FileCard], |
| 381 | id: &str, |
| 382 | directory: &Directory, |
| 383 | file_actions: Option<&FileActions>, |
| 384 | open_image: Option<&ImageOpen>, |
| 385 | ) -> Option<Div> { |
| 386 | let placed = placements(blocks, images, local_images.len()); |
| 387 | let unplaced_images: Vec<(usize, &ImageRef)> = images |
| 388 | .iter() |
| 389 | .enumerate() |
| 390 | .filter(|(index, _)| !placed.contains(&Placement::Remote(*index))) |
| 391 | .collect(); |
| 392 | let unplaced_local: Vec<(usize, &LocalImage)> = local_images |
| 393 | .iter() |
| 394 | .enumerate() |
| 395 | .filter(|(index, _)| !placed.contains(&Placement::Local(*index))) |
| 396 | .collect(); |
| 397 | if unplaced_images.is_empty() && unplaced_local.is_empty() && files.is_empty() { |
| 398 | return None; |
| 399 | } |
| 400 | Some( |
| 401 | v_flex() |
| 402 | .gap(px(6.)) |
| 403 | .items_start() |
| 404 | .children(unplaced_images.into_iter().map(|(index, image)| { |
| 405 | image_view(image, format!("{id}-image-{index}"), directory, open_image) |
| 406 | })) |
| 407 | .children( |
| 408 | unplaced_local.into_iter().map(|(index, image)| { |
| 409 | local_image_view(image, format!("{id}-local-image-{index}")) |
| 410 | }), |
| 411 | ) |
| 412 | .children(files.iter().enumerate().map(|(index, card)| { |
| 413 | file_view(card, format!("{id}-file-{index}"), index, file_actions) |
| 414 | })), |
| 415 | ) |
| 416 | } |
| 417 | |
| 418 | #[cfg(test)] |
| 419 | mod tests { |
| 420 | use super::{ |
| 421 | IMAGE_FALLBACK, Placement, file_subtitle, fit_image, fit_image_within, local_image_index, |
| 422 | placement, placements, |
| 423 | }; |
| 424 | use crate::render::Block; |
| 425 | use teams_core::{FileCard, FileKind, ImageRef}; |
| 426 | |
| 427 | fn image_ref(url: &str) -> ImageRef { |
| 428 | ImageRef { |
| 429 | id: url.into(), |
| 430 | url: url.into(), |
| 431 | width: None, |
| 432 | height: None, |
| 433 | alt: None, |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | fn image_block(url: &str) -> Block { |
| 438 | Block::Image { url: url.into() } |
| 439 | } |
| 440 | |
| 441 | #[test] |
| 442 | fn hosted_content_urls_map_to_zero_based_local_indices() { |
| 443 | assert_eq!(local_image_index("../hostedContents/2/$value"), Some(1)); |
| 444 | assert_eq!(local_image_index("../hostedContents/0/$value"), None); |
| 445 | assert_eq!(local_image_index("https://x/y"), None); |
| 446 | } |
| 447 | |
| 448 | #[test] |
| 449 | fn placed_images_are_found_by_url_before_local_indices() { |
| 450 | let images = [image_ref("a"), image_ref("b")]; |
| 451 | let blocks = [ |
| 452 | image_block("b"), |
| 453 | image_block("../hostedContents/2/$value"), |
| 454 | image_block("../hostedContents/9/$value"), |
| 455 | image_block("missing"), |
| 456 | ]; |
| 457 | assert_eq!( |
| 458 | placements(&blocks, &images, 2), |
| 459 | vec![Placement::Remote(1), Placement::Local(1)] |
| 460 | ); |
| 461 | assert_eq!(placement("a", &images, 0), Some(Placement::Remote(0))); |
| 462 | } |
| 463 | |
| 464 | #[test] |
| 465 | fn wide_images_shrink_to_the_max_width_keeping_aspect() { |
| 466 | assert_eq!(fit_image(Some((720, 360))), (360., 180.)); |
| 467 | } |
| 468 | |
| 469 | #[test] |
| 470 | fn gifs_and_stickers_are_capped_at_250_high() { |
| 471 | assert_eq!(fit_image_within(Some((300, 500)), 250.), (150., 250.)); |
| 472 | } |
| 473 | |
| 474 | #[test] |
| 475 | fn tall_images_shrink_to_the_max_height() { |
| 476 | assert_eq!(fit_image(Some((300, 600))), (150., 300.)); |
| 477 | } |
| 478 | |
| 479 | #[test] |
| 480 | fn small_images_keep_their_size() { |
| 481 | assert_eq!(fit_image(Some((200, 100))), (200., 100.)); |
| 482 | } |
| 483 | |
| 484 | #[test] |
| 485 | fn unknown_or_empty_sizes_use_the_fallback() { |
| 486 | assert_eq!(fit_image(None), IMAGE_FALLBACK); |
| 487 | assert_eq!(fit_image(Some((0, 10))), IMAGE_FALLBACK); |
| 488 | } |
| 489 | |
| 490 | #[test] |
| 491 | fn thin_images_keep_a_clickable_side() { |
| 492 | assert_eq!(fit_image(Some((3000, 10))), (360., 48.)); |
| 493 | } |
| 494 | |
| 495 | #[test] |
| 496 | fn subtitle_adds_the_size_when_known() { |
| 497 | let card = FileCard { |
| 498 | name: "a.pdf".into(), |
| 499 | kind: FileKind::Pdf, |
| 500 | content_type: None, |
| 501 | size: Some(2048), |
| 502 | open_url: String::new(), |
| 503 | }; |
| 504 | assert_eq!(file_subtitle(&card), "PDF document \u{b7} 2 KB"); |
| 505 | let unknown = FileCard { size: None, ..card }; |
| 506 | assert_eq!(file_subtitle(&unknown), "PDF document"); |
| 507 | } |
| 508 | } |