| 1 | use crate::views::image_memory::preview as scaled_preview; |
| 2 | use std::rc::Rc; |
| 3 | |
| 4 | use gpui_kit::assets::IconName; |
| 5 | use gpui_kit::component::{h_flex, v_flex}; |
| 6 | use gpui_kit::prelude::FluentBuilder as _; |
| 7 | use gpui_kit::*; |
| 8 | use teams_core::{LinkPreview, first_public_link}; |
| 9 | |
| 10 | use super::widgets::icon; |
| 11 | use crate::app_state::AppHandle; |
| 12 | use crate::data::Directory; |
| 13 | use crate::theme; |
| 14 | |
| 15 | const CARD_WIDTH: f32 = 360.; |
| 16 | const THUMB_WIDTH: f32 = 112.; |
| 17 | const THUMB_WIDTH_COMPACT: f32 = 72.; |
| 18 | const THUMB_MAX_WIDTH_RATIO: f32 = 0.4; |
| 19 | const THUMB_MIN_HEIGHT: f32 = 48.; |
| 20 | const THUMB_MAX_HEIGHT: f32 = 112.; |
| 21 | const THUMB_RADIUS: f32 = 6.; |
| 22 | const CLOSE_SIZE: f32 = 28.; |
| 23 | const JOIN_CHIP_HEIGHT: f32 = 24.; |
| 24 | |
| 25 | pub type CloseHandler = Rc<dyn Fn(&mut Window, &mut App)>; |
| 26 | |
| 27 | pub fn thumbnail_size( |
| 28 | declared: Option<(u32, u32)>, |
| 29 | loaded: Option<(u32, u32)>, |
| 30 | width: f32, |
| 31 | ) -> (f32, f32) { |
| 32 | let aspect = declared |
| 33 | .or(loaded) |
| 34 | .filter(|(width, height)| *width > 0 && *height > 0) |
| 35 | .map_or(0.6, |(image_width, image_height)| { |
| 36 | image_height as f32 / image_width as f32 |
| 37 | }); |
| 38 | ( |
| 39 | width, |
| 40 | (width * aspect) |
| 41 | .clamp(THUMB_MIN_HEIGHT, THUMB_MAX_HEIGHT) |
| 42 | .round(), |
| 43 | ) |
| 44 | } |
| 45 | |
| 46 | fn thumbnail( |
| 47 | preview: &LinkPreview, |
| 48 | id: &str, |
| 49 | directory: &Directory, |
| 50 | width: f32, |
| 51 | ) -> Option<AnyElement> { |
| 52 | let image = preview.image()?; |
| 53 | let entry = directory.image(&image.url); |
| 54 | let (width, height) = thumbnail_size( |
| 55 | image.width.zip(image.height), |
| 56 | entry.and_then(|entry| entry.size), |
| 57 | width, |
| 58 | ); |
| 59 | let frame = div() |
| 60 | .id(ElementId::Name(format!("{id}-thumbnail").into())) |
| 61 | .w(px(width)) |
| 62 | .h(px(height)) |
| 63 | .flex_none() |
| 64 | .max_w(relative(THUMB_MAX_WIDTH_RATIO)) |
| 65 | .rounded(px(THUMB_RADIUS)) |
| 66 | .overflow_hidden() |
| 67 | .bg(theme::background()); |
| 68 | Some(match entry { |
| 69 | Some(entry) => frame |
| 70 | .child( |
| 71 | img(scaled_preview(entry.path.clone())) |
| 72 | .size_full() |
| 73 | .object_fit(ObjectFit::Cover), |
| 74 | ) |
| 75 | .into_any_element(), |
| 76 | None => frame |
| 77 | .flex() |
| 78 | .items_center() |
| 79 | .justify_center() |
| 80 | .child(icon(IconName::Image, 18., theme::text_muted())) |
| 81 | .into_any_element(), |
| 82 | }) |
| 83 | } |
| 84 | |
| 85 | pub fn link_preview_card( |
| 86 | preview: &LinkPreview, |
| 87 | id: String, |
| 88 | directory: &Directory, |
| 89 | compact: bool, |
| 90 | close: Option<CloseHandler>, |
| 91 | ) -> AnyElement { |
| 92 | let thumb_width = if compact { |
| 93 | THUMB_WIDTH_COMPACT |
| 94 | } else { |
| 95 | THUMB_WIDTH |
| 96 | }; |
| 97 | let url = preview.url.clone(); |
| 98 | let title = preview.title.clone().unwrap_or_else(|| preview.domain()); |
| 99 | let close_button = close.map(|close| { |
| 100 | div() |
| 101 | .id(ElementId::Name(format!("{id}-close").into())) |
| 102 | .absolute() |
| 103 | .top(px(8.)) |
| 104 | .right(px(8.)) |
| 105 | .size(px(CLOSE_SIZE)) |
| 106 | .flex() |
| 107 | .items_center() |
| 108 | .justify_center() |
| 109 | .rounded(px(7.)) |
| 110 | .cursor_pointer() |
| 111 | .hover(|button| button.bg(theme::popup_hover())) |
| 112 | .child(icon(IconName::Close, 14., theme::text_muted())) |
| 113 | .on_click(move |_, window, cx| { |
| 114 | cx.stop_propagation(); |
| 115 | close(window, cx); |
| 116 | }) |
| 117 | }); |
| 118 | h_flex() |
| 119 | .id(ElementId::Name(id.clone().into())) |
| 120 | .relative() |
| 121 | .overflow_hidden() |
| 122 | .w_full() |
| 123 | .when(!compact, |card| card.max_w(px(CARD_WIDTH))) |
| 124 | .gap(px(12.)) |
| 125 | .p(px(8.)) |
| 126 | .items_start() |
| 127 | .rounded(px(10.)) |
| 128 | .bg(theme::surface()) |
| 129 | .cursor_pointer() |
| 130 | .hover(|card| card.bg(theme::row_hover())) |
| 131 | .children(thumbnail(preview, &id, directory, thumb_width)) |
| 132 | .child( |
| 133 | v_flex() |
| 134 | .flex_1() |
| 135 | .min_w_0() |
| 136 | .gap(px(2.)) |
| 137 | .when(close_button.is_some(), |column| column.pr(px(CLOSE_SIZE))) |
| 138 | .child( |
| 139 | div() |
| 140 | .line_clamp(2) |
| 141 | .text_ellipsis() |
| 142 | .text_size(px(13.)) |
| 143 | .font_weight(FontWeight::SEMIBOLD) |
| 144 | .text_color(theme::text_strong()) |
| 145 | .child(title), |
| 146 | ) |
| 147 | .children(preview.description.clone().map(|description| { |
| 148 | div() |
| 149 | .line_clamp(2) |
| 150 | .text_ellipsis() |
| 151 | .text_size(px(12.)) |
| 152 | .text_color(theme::text_muted()) |
| 153 | .child(description) |
| 154 | })) |
| 155 | .child( |
| 156 | div() |
| 157 | .truncate() |
| 158 | .text_size(px(11.)) |
| 159 | .text_color(theme::text_muted()) |
| 160 | .child(preview.domain()), |
| 161 | ), |
| 162 | ) |
| 163 | .children(close_button) |
| 164 | .on_click(move |_, _, cx| app_ui_kit::render::hooks::open_link(&url, cx)) |
| 165 | .into_any_element() |
| 166 | } |
| 167 | |
| 168 | pub fn meeting_join_chip(id: String, url: String) -> AnyElement { |
| 169 | h_flex() |
| 170 | .id(ElementId::Name(id.into())) |
| 171 | .flex_none() |
| 172 | .self_start() |
| 173 | .h(px(JOIN_CHIP_HEIGHT)) |
| 174 | .px(px(12.)) |
| 175 | .gap(px(4.)) |
| 176 | .items_center() |
| 177 | .rounded_full() |
| 178 | .bg(theme::accent()) |
| 179 | .text_size(px(12.)) |
| 180 | .font_weight(FontWeight::SEMIBOLD) |
| 181 | .text_color(theme::on_accent()) |
| 182 | .cursor_pointer() |
| 183 | .hover(|chip| chip.opacity(0.85)) |
| 184 | .child(icon(IconName::Video, 13., theme::on_accent())) |
| 185 | .child("Join") |
| 186 | .on_click(move |_, _, cx| { |
| 187 | cx.stop_propagation(); |
| 188 | let Some(handle) = cx.try_global::<AppHandle>() else { |
| 189 | return; |
| 190 | }; |
| 191 | let app = handle.0.clone(); |
| 192 | app.update(cx, |state, cx| state.join_meeting_link(&url, cx)); |
| 193 | }) |
| 194 | .into_any_element() |
| 195 | } |
| 196 | |
| 197 | #[derive(Default)] |
| 198 | pub struct ComposeLink { |
| 199 | url: Option<String>, |
| 200 | preview: Option<LinkPreview>, |
| 201 | dismissed: Option<String>, |
| 202 | pub meeting: Option<String>, |
| 203 | pub lookup: Option<Task<()>>, |
| 204 | } |
| 205 | |
| 206 | impl ComposeLink { |
| 207 | pub fn shown(&self) -> Option<&LinkPreview> { |
| 208 | self.preview.as_ref() |
| 209 | } |
| 210 | |
| 211 | /// The link to look up when the draft's link changed. |
| 212 | pub fn observe(&mut self, link: Option<String>, draft_blank: bool) -> Option<String> { |
| 213 | if draft_blank { |
| 214 | self.reset(); |
| 215 | return None; |
| 216 | } |
| 217 | let Some(link) = link else { |
| 218 | self.url = None; |
| 219 | self.preview = None; |
| 220 | self.lookup = None; |
| 221 | return None; |
| 222 | }; |
| 223 | if self.url.as_deref() == Some(link.as_str()) { |
| 224 | return None; |
| 225 | } |
| 226 | self.preview = None; |
| 227 | self.lookup = None; |
| 228 | self.url = Some(link.clone()); |
| 229 | (self.dismissed.as_deref() != Some(link.as_str())).then_some(link) |
| 230 | } |
| 231 | |
| 232 | pub fn apply(&mut self, url: &str, preview: Option<LinkPreview>) -> bool { |
| 233 | let current = self.url.as_deref() == Some(url) && self.dismissed.as_deref() != Some(url); |
| 234 | if current { |
| 235 | self.preview = preview; |
| 236 | } |
| 237 | current |
| 238 | } |
| 239 | |
| 240 | pub fn dismiss(&mut self) { |
| 241 | self.dismissed = self.url.clone(); |
| 242 | self.preview = None; |
| 243 | self.lookup = None; |
| 244 | } |
| 245 | |
| 246 | pub fn restore(&mut self, preview: Option<LinkPreview>) { |
| 247 | self.dismissed = None; |
| 248 | self.lookup = None; |
| 249 | self.url = preview.as_ref().map(|preview| preview.url.clone()); |
| 250 | self.preview = preview; |
| 251 | } |
| 252 | |
| 253 | pub fn reset(&mut self) { |
| 254 | *self = ComposeLink::default(); |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | pub fn draft_link(draft: &teams_core::Draft) -> Option<String> { |
| 259 | first_public_link(&draft.to_html()).or_else(|| { |
| 260 | draft |
| 261 | .plain_text() |
| 262 | .split_whitespace() |
| 263 | .map(|word| word.trim_end_matches(['.', ',', ';', ':', '!', '?', ')'])) |
| 264 | .find(|word| { |
| 265 | let lower = word.to_ascii_lowercase(); |
| 266 | (lower.starts_with("http://") || lower.starts_with("https://")) |
| 267 | && teams_core::is_public_link(word) |
| 268 | }) |
| 269 | .map(str::to_owned) |
| 270 | }) |
| 271 | } |
| 272 | |
| 273 | #[cfg(test)] |
| 274 | mod tests { |
| 275 | use teams_core::LinkPreview; |
| 276 | |
| 277 | use super::{ComposeLink, THUMB_MAX_HEIGHT, THUMB_MIN_HEIGHT, draft_link, thumbnail_size}; |
| 278 | |
| 279 | fn preview(url: &str) -> LinkPreview { |
| 280 | LinkPreview { |
| 281 | url: url.to_owned(), |
| 282 | title: Some("Title".to_owned()), |
| 283 | description: None, |
| 284 | image_url: None, |
| 285 | image_width: None, |
| 286 | image_height: None, |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | #[test] |
| 291 | fn thumbnails_keep_the_aspect_within_bounds() { |
| 292 | assert_eq!(thumbnail_size(Some((320, 160)), None, 112.), (112., 56.)); |
| 293 | assert_eq!( |
| 294 | thumbnail_size(Some((100, 400)), None, 112.), |
| 295 | (112., THUMB_MAX_HEIGHT) |
| 296 | ); |
| 297 | assert_eq!( |
| 298 | thumbnail_size(Some((400, 20)), None, 112.), |
| 299 | (112., THUMB_MIN_HEIGHT) |
| 300 | ); |
| 301 | assert_eq!(thumbnail_size(None, Some((200, 100)), 100.), (100., 50.)); |
| 302 | assert_eq!(thumbnail_size(None, None, 100.), (100., 60.)); |
| 303 | } |
| 304 | |
| 305 | #[test] |
| 306 | fn a_new_link_is_looked_up_once() { |
| 307 | let mut link = ComposeLink::default(); |
| 308 | assert_eq!( |
| 309 | link.observe(Some("https://a.example".into()), false), |
| 310 | Some("https://a.example".into()) |
| 311 | ); |
| 312 | assert_eq!(link.observe(Some("https://a.example".into()), false), None); |
| 313 | assert!(link.apply("https://a.example", Some(preview("https://a.example")))); |
| 314 | assert!(link.shown().is_some()); |
| 315 | assert_eq!( |
| 316 | link.observe(Some("https://b.example".into()), false), |
| 317 | Some("https://b.example".into()) |
| 318 | ); |
| 319 | assert!(link.shown().is_none()); |
| 320 | } |
| 321 | |
| 322 | #[test] |
| 323 | fn a_late_answer_for_an_old_link_is_ignored() { |
| 324 | let mut link = ComposeLink::default(); |
| 325 | link.observe(Some("https://a.example".into()), false); |
| 326 | link.observe(Some("https://b.example".into()), false); |
| 327 | assert!(!link.apply("https://a.example", Some(preview("https://a.example")))); |
| 328 | assert!(link.shown().is_none()); |
| 329 | } |
| 330 | |
| 331 | #[test] |
| 332 | fn a_dismissed_link_stays_hidden_until_the_link_changes_or_the_draft_clears() { |
| 333 | let mut link = ComposeLink::default(); |
| 334 | link.observe(Some("https://a.example".into()), false); |
| 335 | link.apply("https://a.example", Some(preview("https://a.example"))); |
| 336 | link.dismiss(); |
| 337 | assert!(link.shown().is_none()); |
| 338 | assert_eq!(link.observe(Some("https://a.example".into()), false), None); |
| 339 | assert_eq!(link.observe(None, false), None); |
| 340 | assert_eq!(link.observe(Some("https://a.example".into()), false), None); |
| 341 | assert!(!link.apply("https://a.example", Some(preview("https://a.example")))); |
| 342 | assert_eq!( |
| 343 | link.observe(Some("https://b.example".into()), false), |
| 344 | Some("https://b.example".into()) |
| 345 | ); |
| 346 | link.dismiss(); |
| 347 | assert_eq!(link.observe(None, true), None); |
| 348 | assert_eq!( |
| 349 | link.observe(Some("https://b.example".into()), false), |
| 350 | Some("https://b.example".into()) |
| 351 | ); |
| 352 | } |
| 353 | |
| 354 | #[test] |
| 355 | fn removing_the_link_removes_the_card() { |
| 356 | let mut link = ComposeLink::default(); |
| 357 | link.observe(Some("https://a.example".into()), false); |
| 358 | link.apply("https://a.example", Some(preview("https://a.example"))); |
| 359 | assert_eq!(link.observe(None, false), None); |
| 360 | assert!(link.shown().is_none()); |
| 361 | } |
| 362 | |
| 363 | #[test] |
| 364 | fn restoring_a_sent_preview_shows_it_without_a_new_lookup() { |
| 365 | let mut link = ComposeLink::default(); |
| 366 | link.restore(Some(preview("https://a.example"))); |
| 367 | assert!(link.shown().is_some()); |
| 368 | assert_eq!(link.observe(Some("https://a.example".into()), false), None); |
| 369 | assert!(link.shown().is_some()); |
| 370 | } |
| 371 | |
| 372 | #[test] |
| 373 | fn finds_anchors_and_bare_urls_in_a_draft() { |
| 374 | let marked = teams_core::Draft::from_markdown("see [docs](https://docs.example/a) now"); |
| 375 | assert_eq!( |
| 376 | draft_link(&marked).as_deref(), |
| 377 | Some("https://docs.example/a") |
| 378 | ); |
| 379 | let bare = teams_core::Draft::plain("go to https://b.example/x, ok"); |
| 380 | assert_eq!(draft_link(&bare).as_deref(), Some("https://b.example/x")); |
| 381 | let internal = teams_core::Draft::plain("https://contoso.sharepoint.com/x"); |
| 382 | assert_eq!(draft_link(&internal), None); |
| 383 | } |
| 384 | } |