| 1 | use crate::views::image_memory::{bounded, preview}; |
| 2 | use std::rc::Rc; |
| 3 | |
| 4 | use gpui_kit::assets::IconName; |
| 5 | use gpui_kit::component::{h_flex, tooltip::Tooltip, v_flex}; |
| 6 | use gpui_kit::prelude::FluentBuilder as _; |
| 7 | use gpui_kit::*; |
| 8 | use teams_core::{FileCard, FileKind, ImageRef, external_image_url}; |
| 9 | |
| 10 | use super::image_menu::{ImageShare, with_image_menu}; |
| 11 | use super::widgets::{icon, symbol}; |
| 12 | use crate::data::Directory; |
| 13 | use crate::downloads::{self, DownloadState}; |
| 14 | use crate::format; |
| 15 | use crate::inline_video; |
| 16 | use crate::remote_image; |
| 17 | use crate::render::{Block, render_blocks, render_table, table_images}; |
| 18 | use crate::rows::LocalImage; |
| 19 | use crate::theme; |
| 20 | |
| 21 | pub const IMAGE_MAX_WIDTH: f32 = 360.; |
| 22 | const IMAGE_MAX_HEIGHT: f32 = 300.; |
| 23 | const IMAGE_MIN_SIDE: f32 = 48.; |
| 24 | const IMAGE_FALLBACK: (f32, f32) = (240., 160.); |
| 25 | const IMAGE_RADIUS: f32 = 10.; |
| 26 | const FILE_CARD_WIDTH: f32 = 300.; |
| 27 | const FILE_BADGE_SIZE: f32 = 34.; |
| 28 | const SAVE_BUTTON_SIZE: f32 = 28.; |
| 29 | const SAVE_BUTTON_RADIUS: f32 = 6.; |
| 30 | const SAVE_PROGRESS_HEIGHT: f32 = 2.; |
| 31 | const FILE_PREVIEW_HEIGHT: f32 = FILE_CARD_WIDTH * 9. / 16.; |
| 32 | const PORTRAIT_PAGE_RATIO: f32 = 1.414; |
| 33 | const PLAY_BUTTON_SIZE: f32 = 40.; |
| 34 | const LOCAL_IMAGE_PREFIX: &str = "../hostedContents/"; |
| 35 | const LOCAL_IMAGE_SUFFIX: &str = "/$value"; |
| 36 | |
| 37 | pub type FileActivate = Rc<dyn Fn(usize, &mut App)>; |
| 38 | /// Opens the image viewer, or plays the clip for a video thumbnail. |
| 39 | pub type ImageOpen = Rc<dyn Fn(&ImageRef, &mut App)>; |
| 40 | #[derive(Clone)] |
| 41 | pub struct ImageActions { |
| 42 | pub open: ImageOpen, |
| 43 | pub share: Option<ImageShare>, |
| 44 | } |
| 45 | |
| 46 | #[derive(Clone)] |
| 47 | pub struct FileActions { |
| 48 | pub states: Vec<Option<DownloadState>>, |
| 49 | pub activate: FileActivate, |
| 50 | } |
| 51 | |
| 52 | pub fn fit_image(size: Option<(u32, u32)>) -> (f32, f32) { |
| 53 | fit_image_within(size, IMAGE_MAX_HEIGHT) |
| 54 | } |
| 55 | |
| 56 | fn fit_image_within(size: Option<(u32, u32)>, max_height: f32) -> (f32, f32) { |
| 57 | let Some((width, height)) = size.filter(|(width, height)| *width > 0 && *height > 0) else { |
| 58 | return IMAGE_FALLBACK; |
| 59 | }; |
| 60 | let (width, height) = (width as f32, height as f32); |
| 61 | let scale = (IMAGE_MAX_WIDTH / width).min(max_height / height).min(1.); |
| 62 | ( |
| 63 | (width * scale).max(IMAGE_MIN_SIDE).round(), |
| 64 | (height * scale).max(IMAGE_MIN_SIDE).round(), |
| 65 | ) |
| 66 | } |
| 67 | |
| 68 | fn declared_size(image: &ImageRef) -> Option<(u32, u32)> { |
| 69 | image.width.zip(image.height) |
| 70 | } |
| 71 | |
| 72 | fn image_size( |
| 73 | image: &ImageRef, |
| 74 | directory: &Directory, |
| 75 | sent_preview: Option<&LocalImage>, |
| 76 | ) -> (f32, f32) { |
| 77 | let loaded = directory |
| 78 | .image(&image.url) |
| 79 | .and_then(|entry| entry.size) |
| 80 | .or_else(|| sent_preview.and_then(|preview| preview.size)); |
| 81 | let max_height = if external_image_url(&image.url).is_some() { |
| 82 | remote_image::MAX_HEIGHT as f32 |
| 83 | } else { |
| 84 | IMAGE_MAX_HEIGHT |
| 85 | }; |
| 86 | fit_image_within(declared_size(image).or(loaded), max_height) |
| 87 | } |
| 88 | |
| 89 | fn sent_preview<'a>( |
| 90 | images: &[ImageRef], |
| 91 | position: usize, |
| 92 | sent_previews: &'a [LocalImage], |
| 93 | ) -> Option<&'a LocalImage> { |
| 94 | if external_image_url(&images[position].url).is_some() { |
| 95 | return None; |
| 96 | } |
| 97 | let uploaded_before = images[..position] |
| 98 | .iter() |
| 99 | .filter(|image| external_image_url(&image.url).is_none()) |
| 100 | .count(); |
| 101 | sent_previews.get(uploaded_before) |
| 102 | } |
| 103 | |
| 104 | fn local_image(image: &LocalImage, id: ElementId) -> Stateful<Img> { |
| 105 | img(bounded(image.image.clone())) |
| 106 | .id(id) |
| 107 | .size_full() |
| 108 | .rounded(px(IMAGE_RADIUS)) |
| 109 | .object_fit(ObjectFit::Cover) |
| 110 | } |
| 111 | |
| 112 | fn image_view( |
| 113 | image: &ImageRef, |
| 114 | id: String, |
| 115 | directory: &Directory, |
| 116 | open_image: Option<&ImageActions>, |
| 117 | sent_preview: Option<&LocalImage>, |
| 118 | ) -> AnyElement { |
| 119 | let (width, height) = image_size(image, directory, sent_preview); |
| 120 | let image_id = ElementId::Name(format!("{id}-image").into()); |
| 121 | let clip = image.video_url().is_some(); |
| 122 | let frame = div() |
| 123 | .id(ElementId::Name(id.into())) |
| 124 | .relative() |
| 125 | .w(px(width)) |
| 126 | .h(px(height)) |
| 127 | .max_w(relative(1.)) |
| 128 | .flex_none() |
| 129 | .rounded(px(IMAGE_RADIUS)) |
| 130 | .overflow_hidden(); |
| 131 | let Some(entry) = directory.image(&image.url) else { |
| 132 | if let Some(sent) = sent_preview { |
| 133 | return frame.child(local_image(sent, image_id)).into_any_element(); |
| 134 | } |
| 135 | if clip { |
| 136 | let (image, open_image) = (image.clone(), open_image.cloned()); |
| 137 | return frame |
| 138 | .bg(theme::code_surface()) |
| 139 | .cursor_pointer() |
| 140 | .child(play_badge()) |
| 141 | .when_some(open_image, |frame, actions| { |
| 142 | frame.on_click(move |_, _, cx| (actions.open)(&image, cx)) |
| 143 | }) |
| 144 | .into_any_element(); |
| 145 | } |
| 146 | return frame |
| 147 | .flex() |
| 148 | .flex_col() |
| 149 | .gap(px(6.)) |
| 150 | .items_center() |
| 151 | .justify_center() |
| 152 | .bg(theme::background()) |
| 153 | .child(icon(IconName::Image, 24., theme::text_muted())) |
| 154 | .child( |
| 155 | div() |
| 156 | .text_size(px(11.)) |
| 157 | .text_color(theme::text_muted()) |
| 158 | .child("Loading image"), |
| 159 | ) |
| 160 | .into_any_element(); |
| 161 | }; |
| 162 | let path = entry.path.clone(); |
| 163 | let share = open_image.and_then(|actions| actions.share.clone()); |
| 164 | let (image, open_image) = (image.clone(), open_image.cloned()); |
| 165 | let frame = frame |
| 166 | .cursor_pointer() |
| 167 | .when_some(sent_preview, |frame, sent| { |
| 168 | frame.child( |
| 169 | div() |
| 170 | .absolute() |
| 171 | .inset_0() |
| 172 | .child(local_image(sent, format!("{image_id}-sent").into())), |
| 173 | ) |
| 174 | }) |
| 175 | .child( |
| 176 | img(preview(entry.path.clone())) |
| 177 | .id(image_id) |
| 178 | .size_full() |
| 179 | .rounded(px(IMAGE_RADIUS)) |
| 180 | .object_fit(ObjectFit::Cover), |
| 181 | ) |
| 182 | .when(clip, |frame| frame.child(play_badge())) |
| 183 | .on_click({ |
| 184 | let path = path.clone(); |
| 185 | move |_, _, cx| match &open_image { |
| 186 | Some(actions) => (actions.open)(&image, cx), |
| 187 | None => cx.open_with_system(&path), |
| 188 | } |
| 189 | }); |
| 190 | with_image_menu(frame, path, share).into_any_element() |
| 191 | } |
| 192 | |
| 193 | struct KindStyle { |
| 194 | label: &'static str, |
| 195 | description: &'static str, |
| 196 | color: u32, |
| 197 | } |
| 198 | |
| 199 | fn kind_style(kind: FileKind) -> KindStyle { |
| 200 | let (label, description, color) = match kind { |
| 201 | FileKind::Word => ("DOC", "Word document", 0x2563EB), |
| 202 | FileKind::Excel => ("XLS", "Excel spreadsheet", 0x15803D), |
| 203 | FileKind::PowerPoint => ("PPT", "PowerPoint", 0xC2410C), |
| 204 | FileKind::Pdf => ("PDF", "PDF document", 0xB91C1C), |
| 205 | FileKind::Image => ("IMG", "Image", 0x7C3AED), |
| 206 | FileKind::Video => ("VID", "Video", 0xBE185D), |
| 207 | FileKind::Archive => ("ZIP", "Archive", 0xB45309), |
| 208 | FileKind::Loop => ("", "Loop component", 0x7C3AED), |
| 209 | FileKind::Other => ("", "File", 0x525252), |
| 210 | }; |
| 211 | KindStyle { |
| 212 | label, |
| 213 | description, |
| 214 | color, |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | pub fn kind_description(kind: FileKind) -> &'static str { |
| 219 | kind_style(kind).description |
| 220 | } |
| 221 | |
| 222 | pub fn file_subtitle(card: &FileCard) -> String { |
| 223 | let description = kind_style(card.kind).description; |
| 224 | match card.size { |
| 225 | Some(size) => format!("{description} \u{b7} {}", format::file_size_label(size)), |
| 226 | None => description.to_owned(), |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | pub fn file_badge(kind: FileKind, size: f32) -> Div { |
| 231 | let style = kind_style(kind); |
| 232 | div() |
| 233 | .size(px(size)) |
| 234 | .flex_none() |
| 235 | .flex() |
| 236 | .items_center() |
| 237 | .justify_center() |
| 238 | .rounded(px(7.)) |
| 239 | .bg(theme::color(style.color)) |
| 240 | .text_color(gpui_kit::white()) |
| 241 | .text_size(px(10.5)) |
| 242 | .font_weight(FontWeight::BOLD) |
| 243 | .when(style.label.is_empty(), |badge| { |
| 244 | badge.child(icon(IconName::File, 16., gpui_kit::white())) |
| 245 | }) |
| 246 | .when(!style.label.is_empty(), |badge| badge.child(style.label)) |
| 247 | } |
| 248 | |
| 249 | fn local_image_view(image: &LocalImage, id: String) -> AnyElement { |
| 250 | let (width, height) = fit_image(image.size); |
| 251 | let image_id = ElementId::Name(format!("{id}-image").into()); |
| 252 | div() |
| 253 | .id(ElementId::Name(id.into())) |
| 254 | .w(px(width)) |
| 255 | .h(px(height)) |
| 256 | .max_w(relative(1.)) |
| 257 | .flex_none() |
| 258 | .rounded(px(IMAGE_RADIUS)) |
| 259 | .overflow_hidden() |
| 260 | .child(local_image(image, image_id)) |
| 261 | .into_any_element() |
| 262 | } |
| 263 | |
| 264 | fn save_button( |
| 265 | state: Option<&DownloadState>, |
| 266 | id: String, |
| 267 | index: usize, |
| 268 | activate: FileActivate, |
| 269 | ) -> Stateful<Div> { |
| 270 | let look = downloads::button_look(state); |
| 271 | let tooltip = look.tooltip.clone(); |
| 272 | let clickable = look.clickable; |
| 273 | let button = div() |
| 274 | .id(ElementId::Name(id.into())) |
| 275 | .size(px(SAVE_BUTTON_SIZE)) |
| 276 | .flex_none() |
| 277 | .flex() |
| 278 | .items_center() |
| 279 | .justify_center() |
| 280 | .rounded(px(SAVE_BUTTON_RADIUS)) |
| 281 | .tooltip(move |window, cx| Tooltip::new(tooltip.clone()).build(window, cx)) |
| 282 | .when(look.clickable, |button| { |
| 283 | button |
| 284 | .cursor_pointer() |
| 285 | .hover(|button| button.bg(theme::row_hover())) |
| 286 | }) |
| 287 | .on_click(move |_, _, cx| { |
| 288 | cx.stop_propagation(); |
| 289 | if clickable { |
| 290 | activate(index, cx); |
| 291 | } |
| 292 | }); |
| 293 | match look.percent { |
| 294 | Some(percent) => button.child( |
| 295 | div() |
| 296 | .text_size(px(11.)) |
| 297 | .text_color(theme::text_muted()) |
| 298 | .child(downloads::percent_label(percent)), |
| 299 | ), |
| 300 | None => button.child(symbol(look.symbol, 16., theme::text_muted())), |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | fn file_preview(card: &FileCard, id: &str, directory: &Directory) -> Option<Stateful<Div>> { |
| 305 | let image = card.preview().and_then(|preview| { |
| 306 | directory |
| 307 | .image(&preview.url) |
| 308 | .map(|entry| entry.path.clone()) |
| 309 | }); |
| 310 | if image.is_none() && card.kind != FileKind::Video { |
| 311 | return None; |
| 312 | } |
| 313 | let frame = div() |
| 314 | .id(SharedString::from(format!("{id}-preview"))) |
| 315 | .relative() |
| 316 | .w_full() |
| 317 | .h(px(FILE_PREVIEW_HEIGHT)) |
| 318 | .overflow_hidden() |
| 319 | .bg(theme::code_surface()); |
| 320 | let frame = match image { |
| 321 | Some(path) if card.kind.is_document() && card.kind != FileKind::PowerPoint => { |
| 322 | frame.bg(theme::white()).child( |
| 323 | img(bounded(path)) |
| 324 | .w_full() |
| 325 | .h(px(FILE_CARD_WIDTH * PORTRAIT_PAGE_RATIO)) |
| 326 | .object_fit(ObjectFit::Cover), |
| 327 | ) |
| 328 | } |
| 329 | Some(path) => frame.child(img(bounded(path)).size_full().object_fit(match card.kind { |
| 330 | FileKind::Video | FileKind::PowerPoint => ObjectFit::Cover, |
| 331 | _ => ObjectFit::Contain, |
| 332 | })), |
| 333 | None => frame, |
| 334 | }; |
| 335 | let player_key = SharedString::from(format!("{id}-player")); |
| 336 | let content_url = card.open_url.clone(); |
| 337 | Some(frame.when(card.kind == FileKind::Video, |frame| { |
| 338 | let play_key = player_key.clone(); |
| 339 | frame |
| 340 | .child(inline_video::slot(player_key)) |
| 341 | .on_click(move |_, window, cx| { |
| 342 | if inline_video::play(play_key.clone(), &content_url, window, cx) { |
| 343 | cx.stop_propagation(); |
| 344 | } |
| 345 | }) |
| 346 | .child(play_badge()) |
| 347 | })) |
| 348 | } |
| 349 | |
| 350 | /// A round play button centered over a video preview. |
| 351 | fn play_badge() -> Div { |
| 352 | h_flex() |
| 353 | .absolute() |
| 354 | .inset_0() |
| 355 | .items_center() |
| 356 | .justify_center() |
| 357 | .child( |
| 358 | h_flex() |
| 359 | .size(px(PLAY_BUTTON_SIZE)) |
| 360 | .rounded_full() |
| 361 | .items_center() |
| 362 | .justify_center() |
| 363 | .bg(black().opacity(0.5)) |
| 364 | .child(icon(IconName::Play, 18., theme::white())), |
| 365 | ) |
| 366 | } |
| 367 | |
| 368 | fn file_view( |
| 369 | card: &FileCard, |
| 370 | id: String, |
| 371 | index: usize, |
| 372 | actions: Option<&FileActions>, |
| 373 | directory: &Directory, |
| 374 | ) -> AnyElement { |
| 375 | let badge = file_badge(card.kind, FILE_BADGE_SIZE); |
| 376 | let url = card.open_url.clone(); |
| 377 | let state = actions |
| 378 | .and_then(|actions| actions.states.get(index)) |
| 379 | .and_then(Option::as_ref); |
| 380 | let progress = downloads::progress_fraction(state); |
| 381 | let button = actions |
| 382 | .filter(|_| !card.open_url.is_empty() && card.kind != FileKind::Loop) |
| 383 | .map(|actions| save_button(state, format!("{id}-save"), index, actions.activate.clone())); |
| 384 | let preview = file_preview(card, &id, directory); |
| 385 | let row = h_flex() |
| 386 | .w_full() |
| 387 | .gap(px(12.)) |
| 388 | .px(px(8.)) |
| 389 | .py(px(8.)) |
| 390 | .items_center() |
| 391 | .child(badge) |
| 392 | .child( |
| 393 | v_flex() |
| 394 | .flex_1() |
| 395 | .min_w_0() |
| 396 | .child( |
| 397 | div() |
| 398 | .truncate() |
| 399 | .text_size(px(13.)) |
| 400 | .text_color(theme::text()) |
| 401 | .child(card.name.clone()), |
| 402 | ) |
| 403 | .child( |
| 404 | div() |
| 405 | .truncate() |
| 406 | .text_size(px(11.)) |
| 407 | .text_color(theme::text_muted()) |
| 408 | .child(file_subtitle(card)), |
| 409 | ), |
| 410 | ) |
| 411 | .child(icon(IconName::ExternalLink, 14., theme::text_muted())) |
| 412 | .children(button); |
| 413 | v_flex() |
| 414 | .id(ElementId::Name(id.into())) |
| 415 | .relative() |
| 416 | .overflow_hidden() |
| 417 | .w_full() |
| 418 | .max_w(px(FILE_CARD_WIDTH)) |
| 419 | .rounded(px(10.)) |
| 420 | .bg(theme::surface()) |
| 421 | .cursor_pointer() |
| 422 | .hover(|card| card.bg(theme::row_hover())) |
| 423 | .map(|card_view| { |
| 424 | if card.kind.is_document() { |
| 425 | card_view.child(row).children(preview) |
| 426 | } else { |
| 427 | card_view.children(preview).child(row) |
| 428 | } |
| 429 | }) |
| 430 | .when_some(progress, |card, fraction| { |
| 431 | card.child( |
| 432 | div() |
| 433 | .absolute() |
| 434 | .left_0() |
| 435 | .bottom_0() |
| 436 | .h(px(SAVE_PROGRESS_HEIGHT)) |
| 437 | .w(relative(fraction)) |
| 438 | .bg(theme::accent()), |
| 439 | ) |
| 440 | }) |
| 441 | .on_click(move |_, _, cx| app_base::open_link::open_url(cx, &url)) |
| 442 | .into_any_element() |
| 443 | } |
| 444 | |
| 445 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 446 | enum Placement { |
| 447 | Remote(usize), |
| 448 | Local(usize), |
| 449 | } |
| 450 | |
| 451 | fn local_image_index(url: &str) -> Option<usize> { |
| 452 | url.strip_prefix(LOCAL_IMAGE_PREFIX)? |
| 453 | .strip_suffix(LOCAL_IMAGE_SUFFIX)? |
| 454 | .parse::<usize>() |
| 455 | .ok()? |
| 456 | .checked_sub(1) |
| 457 | } |
| 458 | |
| 459 | fn placement(url: &str, images: &[ImageRef], local_count: usize) -> Option<Placement> { |
| 460 | if let Some(index) = images.iter().position(|image| image.url == url) { |
| 461 | return Some(Placement::Remote(index)); |
| 462 | } |
| 463 | local_image_index(url) |
| 464 | .filter(|index| *index < local_count) |
| 465 | .map(Placement::Local) |
| 466 | } |
| 467 | |
| 468 | fn placements(blocks: &[Block], images: &[ImageRef], local_count: usize) -> Vec<Placement> { |
| 469 | blocks |
| 470 | .iter() |
| 471 | .flat_map(|block| match block { |
| 472 | Block::Image { url } => vec![url.as_str()], |
| 473 | table => table_images(table), |
| 474 | }) |
| 475 | .filter_map(|url| placement(url, images, local_count)) |
| 476 | .collect() |
| 477 | } |
| 478 | |
| 479 | fn draws_images(block: &Block) -> bool { |
| 480 | matches!(block, Block::Image { .. }) || !table_images(block).is_empty() |
| 481 | } |
| 482 | |
| 483 | pub fn message_body( |
| 484 | blocks: &[Block], |
| 485 | images: &[ImageRef], |
| 486 | local_images: &[LocalImage], |
| 487 | sent_previews: &[LocalImage], |
| 488 | id: &str, |
| 489 | own: bool, |
| 490 | directory: &Directory, |
| 491 | open_image: Option<&ImageActions>, |
| 492 | cx: &App, |
| 493 | ) -> Vec<AnyElement> { |
| 494 | if !blocks.iter().any(draws_images) { |
| 495 | return vec![render_blocks(blocks, id, own, cx)]; |
| 496 | } |
| 497 | let mut elements = Vec::new(); |
| 498 | let mut run_start = 0; |
| 499 | let render_run = |start: usize, end: usize, elements: &mut Vec<AnyElement>| { |
| 500 | if end > start { |
| 501 | let part_id = format!("{id}-part-{start}"); |
| 502 | elements.push(render_blocks(&blocks[start..end], &part_id, own, cx)); |
| 503 | } |
| 504 | }; |
| 505 | let placed_view = |url: &str, key: &str| -> Option<AnyElement> { |
| 506 | match placement(url, images, local_images.len())? { |
| 507 | Placement::Remote(position) => Some(image_view( |
| 508 | &images[position], |
| 509 | format!("{key}-image-{position}"), |
| 510 | directory, |
| 511 | open_image, |
| 512 | sent_preview(images, position, sent_previews), |
| 513 | )), |
| 514 | Placement::Local(position) => Some(local_image_view( |
| 515 | &local_images[position], |
| 516 | format!("{key}-local-image-{position}"), |
| 517 | )), |
| 518 | } |
| 519 | }; |
| 520 | for (index, block) in blocks.iter().enumerate() { |
| 521 | if !draws_images(block) { |
| 522 | continue; |
| 523 | } |
| 524 | render_run(run_start, index, &mut elements); |
| 525 | run_start = index + 1; |
| 526 | match block { |
| 527 | Block::Image { url } => elements.extend(placed_view(url, id)), |
| 528 | Block::Table { header, rows } => { |
| 529 | let table_id = format!("{id}-table-{index}"); |
| 530 | elements.push(render_table(*header, rows, &table_id, own, cx, &|url| { |
| 531 | placed_view(url, &table_id) |
| 532 | })); |
| 533 | } |
| 534 | _ => {} |
| 535 | } |
| 536 | } |
| 537 | render_run(run_start, blocks.len(), &mut elements); |
| 538 | elements |
| 539 | } |
| 540 | |
| 541 | pub fn attachments_view( |
| 542 | blocks: &[Block], |
| 543 | images: &[ImageRef], |
| 544 | local_images: &[LocalImage], |
| 545 | sent_previews: &[LocalImage], |
| 546 | files: &[FileCard], |
| 547 | id: &str, |
| 548 | directory: &Directory, |
| 549 | file_actions: Option<&FileActions>, |
| 550 | open_image: Option<&ImageActions>, |
| 551 | ) -> Option<Div> { |
| 552 | let placed = placements(blocks, images, local_images.len()); |
| 553 | let unplaced_images: Vec<(usize, &ImageRef)> = images |
| 554 | .iter() |
| 555 | .enumerate() |
| 556 | .filter(|(index, _)| !placed.contains(&Placement::Remote(*index))) |
| 557 | .collect(); |
| 558 | let unplaced_local: Vec<(usize, &LocalImage)> = local_images |
| 559 | .iter() |
| 560 | .enumerate() |
| 561 | .filter(|(index, _)| !placed.contains(&Placement::Local(*index))) |
| 562 | .collect(); |
| 563 | if unplaced_images.is_empty() && unplaced_local.is_empty() && files.is_empty() { |
| 564 | return None; |
| 565 | } |
| 566 | Some( |
| 567 | v_flex() |
| 568 | .gap(px(6.)) |
| 569 | .items_start() |
| 570 | .children(unplaced_images.into_iter().map(|(index, image)| { |
| 571 | image_view( |
| 572 | image, |
| 573 | format!("{id}-image-{index}"), |
| 574 | directory, |
| 575 | open_image, |
| 576 | sent_preview(images, index, sent_previews), |
| 577 | ) |
| 578 | })) |
| 579 | .children( |
| 580 | unplaced_local.into_iter().map(|(index, image)| { |
| 581 | local_image_view(image, format!("{id}-local-image-{index}")) |
| 582 | }), |
| 583 | ) |
| 584 | .children(files.iter().enumerate().map(|(index, card)| { |
| 585 | file_view( |
| 586 | card, |
| 587 | format!("{id}-file-{index}"), |
| 588 | index, |
| 589 | file_actions, |
| 590 | directory, |
| 591 | ) |
| 592 | })), |
| 593 | ) |
| 594 | } |
| 595 | |
| 596 | #[cfg(test)] |
| 597 | mod tests { |
| 598 | use super::{ |
| 599 | IMAGE_FALLBACK, Placement, file_subtitle, fit_image, fit_image_within, local_image_index, |
| 600 | placement, placements, sent_preview, |
| 601 | }; |
| 602 | use crate::render::{Block, TableCell}; |
| 603 | use crate::rows::LocalImage; |
| 604 | use teams_core::{FileCard, FileKind, ImageRef}; |
| 605 | |
| 606 | fn image_ref(url: &str) -> ImageRef { |
| 607 | ImageRef { |
| 608 | id: url.into(), |
| 609 | url: url.into(), |
| 610 | width: None, |
| 611 | height: None, |
| 612 | alt: None, |
| 613 | } |
| 614 | } |
| 615 | |
| 616 | fn image_block(url: &str) -> Block { |
| 617 | Block::Image { url: url.into() } |
| 618 | } |
| 619 | |
| 620 | #[test] |
| 621 | fn sent_previews_follow_the_uploaded_images_and_skip_gifs() { |
| 622 | let gif = "https://media.giphy.com/media/x/giphy.gif"; |
| 623 | let images = [image_ref("hosted-a"), image_ref(gif), image_ref("hosted-b")]; |
| 624 | let previews: Vec<LocalImage> = [(1, 1), (2, 2)] |
| 625 | .into_iter() |
| 626 | .map(|size| LocalImage { |
| 627 | image: std::sync::Arc::new(gpui_kit::Image::empty()), |
| 628 | size: Some(size), |
| 629 | }) |
| 630 | .collect(); |
| 631 | let size_at = |
| 632 | |position| sent_preview(&images, position, &previews).and_then(|preview| preview.size); |
| 633 | assert_eq!(size_at(0), Some((1, 1))); |
| 634 | assert_eq!(size_at(1), None); |
| 635 | assert_eq!(size_at(2), Some((2, 2))); |
| 636 | } |
| 637 | |
| 638 | #[test] |
| 639 | fn hosted_content_urls_map_to_zero_based_local_indices() { |
| 640 | assert_eq!(local_image_index("../hostedContents/2/$value"), Some(1)); |
| 641 | assert_eq!(local_image_index("../hostedContents/0/$value"), None); |
| 642 | assert_eq!(local_image_index("https://x/y"), None); |
| 643 | } |
| 644 | |
| 645 | #[test] |
| 646 | fn placed_images_are_found_by_url_before_local_indices() { |
| 647 | let images = [image_ref("a"), image_ref("b")]; |
| 648 | let blocks = [ |
| 649 | image_block("b"), |
| 650 | image_block("../hostedContents/2/$value"), |
| 651 | image_block("../hostedContents/9/$value"), |
| 652 | image_block("missing"), |
| 653 | ]; |
| 654 | assert_eq!( |
| 655 | placements(&blocks, &images, 2), |
| 656 | vec![Placement::Remote(1), Placement::Local(1)] |
| 657 | ); |
| 658 | assert_eq!(placement("a", &images, 0), Some(Placement::Remote(0))); |
| 659 | } |
| 660 | |
| 661 | #[test] |
| 662 | fn an_image_in_a_table_cell_counts_as_placed() { |
| 663 | let images = [image_ref("a"), image_ref("b")]; |
| 664 | let table = Block::Table { |
| 665 | header: false, |
| 666 | rows: vec![vec![TableCell { |
| 667 | inline: Default::default(), |
| 668 | images: vec!["b".into()], |
| 669 | }]], |
| 670 | }; |
| 671 | assert_eq!(placements(&[table], &images, 0), vec![Placement::Remote(1)]); |
| 672 | } |
| 673 | |
| 674 | #[test] |
| 675 | fn wide_images_shrink_to_the_max_width_keeping_aspect() { |
| 676 | assert_eq!(fit_image(Some((720, 360))), (360., 180.)); |
| 677 | } |
| 678 | |
| 679 | #[test] |
| 680 | fn gifs_and_stickers_are_capped_at_250_high() { |
| 681 | assert_eq!(fit_image_within(Some((300, 500)), 250.), (150., 250.)); |
| 682 | } |
| 683 | |
| 684 | #[test] |
| 685 | fn tall_images_shrink_to_the_max_height() { |
| 686 | assert_eq!(fit_image(Some((300, 600))), (150., 300.)); |
| 687 | } |
| 688 | |
| 689 | #[test] |
| 690 | fn small_images_keep_their_size() { |
| 691 | assert_eq!(fit_image(Some((200, 100))), (200., 100.)); |
| 692 | } |
| 693 | |
| 694 | #[test] |
| 695 | fn unknown_or_empty_sizes_use_the_fallback() { |
| 696 | assert_eq!(fit_image(None), IMAGE_FALLBACK); |
| 697 | assert_eq!(fit_image(Some((0, 10))), IMAGE_FALLBACK); |
| 698 | } |
| 699 | |
| 700 | #[test] |
| 701 | fn thin_images_keep_a_clickable_side() { |
| 702 | assert_eq!(fit_image(Some((3000, 10))), (360., 48.)); |
| 703 | } |
| 704 | |
| 705 | #[test] |
| 706 | fn subtitle_adds_the_size_when_known() { |
| 707 | let card = FileCard { |
| 708 | name: "a.pdf".into(), |
| 709 | kind: FileKind::Pdf, |
| 710 | content_type: None, |
| 711 | size: Some(2048), |
| 712 | open_url: String::new(), |
| 713 | }; |
| 714 | assert_eq!(file_subtitle(&card), "PDF document \u{b7} 2 KB"); |
| 715 | let unknown = FileCard { size: None, ..card }; |
| 716 | assert_eq!(file_subtitle(&unknown), "PDF document"); |
| 717 | } |
| 718 | } |