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