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