| 1 | use std::time::Duration; |
| 2 | |
| 3 | use gpui_kit::base::PopoverState; |
| 4 | use gpui_kit::component::{ |
| 5 | h_flex, |
| 6 | input::{Input, InputEvent, InputState}, |
| 7 | v_flex, |
| 8 | }; |
| 9 | use gpui_kit::prelude::FluentBuilder as _; |
| 10 | use gpui_kit::*; |
| 11 | use teams_core::Gif; |
| 12 | |
| 13 | use crate::app_state::AppState; |
| 14 | use crate::emoji; |
| 15 | use crate::gifs::{self, GifError}; |
| 16 | use crate::remote_image::{RemoteImage, image_source}; |
| 17 | use crate::stickers::{self, Sticker}; |
| 18 | use crate::theme; |
| 19 | |
| 20 | const TAB_KEY: &str = "fun_picker_tab"; |
| 21 | const WIDTH: f32 = 360.; |
| 22 | const HEIGHT: f32 = 380.; |
| 23 | const PADDING: f32 = 8.; |
| 24 | const CONTENT_WIDTH: f32 = WIDTH - 2. * PADDING; |
| 25 | const EMOJI_COLUMNS: usize = 8; |
| 26 | const EMOJI_CELL: f32 = 32.; |
| 27 | const EMOJI_GAP: f32 = 2.; |
| 28 | const EMOJI_SEARCH_LIMIT: usize = 240; |
| 29 | const GIF_COLUMNS: usize = 3; |
| 30 | const GIF_GAP: f32 = 6.; |
| 31 | const GIF_DEBOUNCE: Duration = Duration::from_millis(300); |
| 32 | const GIF_MIN_HEIGHT: f32 = 48.; |
| 33 | const GIF_MAX_HEIGHT: f32 = 200.; |
| 34 | const GIF_PLACEHOLDER_HEIGHTS: [f32; 9] = [90., 70., 110., 80., 100., 70., 100., 90., 80.]; |
| 35 | const STICKER_COLUMNS: usize = 4; |
| 36 | const STICKER_GAP: f32 = 6.; |
| 37 | const POPULAR_LABEL: &str = "Beliebt"; |
| 38 | const TILE_RADIUS: f32 = 6.; |
| 39 | |
| 40 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 41 | enum Tab { |
| 42 | Emoji, |
| 43 | Gif, |
| 44 | Sticker, |
| 45 | } |
| 46 | |
| 47 | impl Tab { |
| 48 | const ALL: [Tab; 3] = [Tab::Emoji, Tab::Gif, Tab::Sticker]; |
| 49 | |
| 50 | fn key(self) -> &'static str { |
| 51 | match self { |
| 52 | Tab::Emoji => "emoji", |
| 53 | Tab::Gif => "gif", |
| 54 | Tab::Sticker => "sticker", |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | fn from_key(key: &str) -> Option<Tab> { |
| 59 | Tab::ALL.into_iter().find(|tab| tab.key() == key) |
| 60 | } |
| 61 | |
| 62 | fn label(self) -> &'static str { |
| 63 | match self { |
| 64 | Tab::Emoji => "Emoji", |
| 65 | Tab::Gif => "GIF", |
| 66 | Tab::Sticker => "Sticker", |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | fn placeholder(self) -> &'static str { |
| 71 | match self { |
| 72 | Tab::Emoji => "Emoji suchen", |
| 73 | Tab::Gif => "GIFs suchen", |
| 74 | Tab::Sticker => "Sticker suchen", |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 80 | enum StickerChip { |
| 81 | Popular, |
| 82 | Category(&'static str), |
| 83 | } |
| 84 | |
| 85 | enum GifState { |
| 86 | Loading, |
| 87 | Ready(Vec<Gif>), |
| 88 | Failed, |
| 89 | Disabled, |
| 90 | } |
| 91 | |
| 92 | enum EmojiRow { |
| 93 | Header(&'static str), |
| 94 | Cells(Vec<String>), |
| 95 | } |
| 96 | |
| 97 | pub enum FunPickerEvent { |
| 98 | Emoji(String), |
| 99 | Image(RemoteImage), |
| 100 | } |
| 101 | |
| 102 | pub struct FunPicker { |
| 103 | app: Entity<AppState>, |
| 104 | input: Entity<InputState>, |
| 105 | tab: Tab, |
| 106 | query: String, |
| 107 | recent: emoji::Recent, |
| 108 | popover: Option<WeakEntity<PopoverState>>, |
| 109 | emoji_rows: Vec<EmojiRow>, |
| 110 | emoji_scroll: UniformListScrollHandle, |
| 111 | gifs: GifState, |
| 112 | gif_generation: u64, |
| 113 | gif_task: Option<Task<()>>, |
| 114 | chip: StickerChip, |
| 115 | sticker_rows: Vec<Vec<&'static Sticker>>, |
| 116 | sticker_scroll: UniformListScrollHandle, |
| 117 | _subscription: Subscription, |
| 118 | } |
| 119 | |
| 120 | impl EventEmitter<FunPickerEvent> for FunPicker {} |
| 121 | |
| 122 | fn emoji_rows(query: &str, recent: &[String]) -> Vec<EmojiRow> { |
| 123 | let chunked = |glyphs: Vec<String>| -> Vec<EmojiRow> { |
| 124 | glyphs |
| 125 | .chunks(EMOJI_COLUMNS) |
| 126 | .map(|chunk| EmojiRow::Cells(chunk.to_vec())) |
| 127 | .collect() |
| 128 | }; |
| 129 | if !query.is_empty() { |
| 130 | let found = emoji::search(query, recent, EMOJI_SEARCH_LIMIT) |
| 131 | .into_iter() |
| 132 | .map(|found| found.glyph.to_owned()) |
| 133 | .collect(); |
| 134 | return chunked(found); |
| 135 | } |
| 136 | let mut rows = Vec::new(); |
| 137 | if !recent.is_empty() { |
| 138 | rows.push(EmojiRow::Header("Zuletzt verwendet")); |
| 139 | rows.extend(chunked(recent.to_vec())); |
| 140 | } |
| 141 | rows.push(EmojiRow::Header("Alle Emojis")); |
| 142 | rows.extend(chunked(emoji::glyphs().map(str::to_owned).collect())); |
| 143 | rows |
| 144 | } |
| 145 | |
| 146 | fn sticker_rows(query: &str, chip: StickerChip) -> Vec<Vec<&'static Sticker>> { |
| 147 | let stickers = if !query.is_empty() { |
| 148 | stickers::search(query) |
| 149 | } else { |
| 150 | match chip { |
| 151 | StickerChip::Popular => stickers::popular(), |
| 152 | StickerChip::Category(category) => stickers::in_category(category), |
| 153 | } |
| 154 | }; |
| 155 | stickers |
| 156 | .chunks(STICKER_COLUMNS) |
| 157 | .map(<[&Sticker]>::to_vec) |
| 158 | .collect() |
| 159 | } |
| 160 | |
| 161 | /// Indices per column; each item goes to the currently shortest column. |
| 162 | fn masonry(heights: &[f32], columns: usize) -> Vec<Vec<usize>> { |
| 163 | let mut layout: Vec<Vec<usize>> = vec![Vec::new(); columns]; |
| 164 | let mut totals = vec![0_f32; columns]; |
| 165 | for (index, height) in heights.iter().enumerate() { |
| 166 | let shortest = totals |
| 167 | .iter() |
| 168 | .enumerate() |
| 169 | .min_by(|left, right| left.1.total_cmp(right.1)) |
| 170 | .map_or(0, |(column, _)| column); |
| 171 | layout[shortest].push(index); |
| 172 | totals[shortest] += height + GIF_GAP; |
| 173 | } |
| 174 | layout |
| 175 | } |
| 176 | |
| 177 | fn gif_tile_width() -> f32 { |
| 178 | (CONTENT_WIDTH - GIF_GAP * (GIF_COLUMNS - 1) as f32) / GIF_COLUMNS as f32 |
| 179 | } |
| 180 | |
| 181 | fn gif_tile_height(gif: &Gif) -> f32 { |
| 182 | let ratio = gif.preview_height as f32 / gif.preview_width.max(1) as f32; |
| 183 | (gif_tile_width() * ratio).clamp(GIF_MIN_HEIGHT, GIF_MAX_HEIGHT) |
| 184 | } |
| 185 | |
| 186 | fn sticker_tile_size() -> f32 { |
| 187 | (CONTENT_WIDTH - STICKER_GAP * (STICKER_COLUMNS - 1) as f32) / STICKER_COLUMNS as f32 |
| 188 | } |
| 189 | |
| 190 | impl FunPicker { |
| 191 | pub fn new(app: Entity<AppState>, window: &mut Window, cx: &mut Context<Self>) -> Self { |
| 192 | let input = cx.new(|cx| InputState::new(window, cx).placeholder(Tab::Emoji.placeholder())); |
| 193 | let subscription = cx.subscribe_in(&input, window, Self::on_input_event); |
| 194 | let recent = emoji::Recent::load(&app.read(cx).store); |
| 195 | FunPicker { |
| 196 | app, |
| 197 | input, |
| 198 | tab: Tab::Emoji, |
| 199 | query: String::new(), |
| 200 | emoji_rows: emoji_rows("", &recent.glyphs()), |
| 201 | recent, |
| 202 | popover: None, |
| 203 | emoji_scroll: UniformListScrollHandle::new(), |
| 204 | gifs: GifState::Loading, |
| 205 | gif_generation: 0, |
| 206 | gif_task: None, |
| 207 | chip: StickerChip::Popular, |
| 208 | sticker_rows: sticker_rows("", StickerChip::Popular), |
| 209 | sticker_scroll: UniformListScrollHandle::new(), |
| 210 | _subscription: subscription, |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | pub fn set_popover(&mut self, popover: WeakEntity<PopoverState>) { |
| 215 | self.popover = Some(popover); |
| 216 | } |
| 217 | |
| 218 | pub fn reset(&mut self, window: &mut Window, cx: &mut Context<Self>) { |
| 219 | let store = self.app.read(cx).store.clone(); |
| 220 | self.recent.reload(&store); |
| 221 | self.tab = store |
| 222 | .meta(TAB_KEY) |
| 223 | .ok() |
| 224 | .flatten() |
| 225 | .and_then(|key| Tab::from_key(&key)) |
| 226 | .unwrap_or(Tab::Emoji); |
| 227 | self.show_tab(window, cx); |
| 228 | } |
| 229 | |
| 230 | fn set_tab(&mut self, tab: Tab, window: &mut Window, cx: &mut Context<Self>) { |
| 231 | if tab == self.tab { |
| 232 | return; |
| 233 | } |
| 234 | self.tab = tab; |
| 235 | let _ = self.app.read(cx).store.set_meta(TAB_KEY, tab.key()); |
| 236 | self.show_tab(window, cx); |
| 237 | } |
| 238 | |
| 239 | fn show_tab(&mut self, window: &mut Window, cx: &mut Context<Self>) { |
| 240 | self.query.clear(); |
| 241 | let placeholder = self.tab.placeholder(); |
| 242 | self.input.update(cx, |state, cx| { |
| 243 | state.set_value("", window, cx); |
| 244 | state.set_placeholder(placeholder, window, cx); |
| 245 | state.focus(window, cx); |
| 246 | }); |
| 247 | self.refresh(false, cx); |
| 248 | } |
| 249 | |
| 250 | fn refresh(&mut self, debounce: bool, cx: &mut Context<Self>) { |
| 251 | match self.tab { |
| 252 | Tab::Emoji => { |
| 253 | self.emoji_rows = emoji_rows(&self.query, &self.recent.glyphs()); |
| 254 | self.emoji_scroll.scroll_to_item(0, ScrollStrategy::Top); |
| 255 | } |
| 256 | Tab::Gif => self.search_gifs(debounce, cx), |
| 257 | Tab::Sticker => { |
| 258 | self.sticker_rows = sticker_rows(&self.query, self.chip); |
| 259 | self.sticker_scroll.scroll_to_item(0, ScrollStrategy::Top); |
| 260 | } |
| 261 | } |
| 262 | cx.notify(); |
| 263 | } |
| 264 | |
| 265 | fn on_input_event( |
| 266 | &mut self, |
| 267 | input: &Entity<InputState>, |
| 268 | event: &InputEvent, |
| 269 | _: &mut Window, |
| 270 | cx: &mut Context<Self>, |
| 271 | ) { |
| 272 | match event { |
| 273 | InputEvent::Change => { |
| 274 | let query = input.read(cx).value().trim().to_owned(); |
| 275 | if query != self.query { |
| 276 | self.query = query; |
| 277 | self.refresh(true, cx); |
| 278 | } |
| 279 | } |
| 280 | InputEvent::PressEnter { .. } if self.tab == Tab::Emoji => { |
| 281 | let first = self.emoji_rows.iter().find_map(|row| match row { |
| 282 | EmojiRow::Cells(glyphs) => glyphs.first().cloned(), |
| 283 | EmojiRow::Header(_) => None, |
| 284 | }); |
| 285 | if let Some(glyph) = first.filter(|_| !self.query.is_empty()) { |
| 286 | self.pick_emoji(&glyph, cx); |
| 287 | } |
| 288 | } |
| 289 | _ => {} |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | fn search_gifs(&mut self, debounce: bool, cx: &mut Context<Self>) { |
| 294 | self.gif_generation += 1; |
| 295 | let generation = self.gif_generation; |
| 296 | self.gifs = GifState::Loading; |
| 297 | let query = self.query.clone(); |
| 298 | let (demo, engine) = { |
| 299 | let state = self.app.read(cx); |
| 300 | (state.mode.demo, state.engine.clone()) |
| 301 | }; |
| 302 | self.gif_task = Some(cx.spawn(async move |this, cx| { |
| 303 | if debounce { |
| 304 | cx.background_executor().timer(GIF_DEBOUNCE).await; |
| 305 | } |
| 306 | let outcome = gifs::search(engine, demo, query) |
| 307 | .await |
| 308 | .unwrap_or(Err(GifError::Failed)); |
| 309 | this.update(cx, |this, cx| { |
| 310 | if this.gif_generation != generation { |
| 311 | return; |
| 312 | } |
| 313 | this.gifs = match outcome { |
| 314 | Ok(found) => GifState::Ready(found), |
| 315 | Err(GifError::Disabled) => GifState::Disabled, |
| 316 | Err(GifError::Failed) => GifState::Failed, |
| 317 | }; |
| 318 | cx.notify(); |
| 319 | }) |
| 320 | .ok(); |
| 321 | })); |
| 322 | } |
| 323 | |
| 324 | fn pick_emoji(&mut self, glyph: &str, cx: &mut Context<Self>) { |
| 325 | self.recent.push(glyph); |
| 326 | self.refresh(false, cx); |
| 327 | cx.emit(FunPickerEvent::Emoji(glyph.to_owned())); |
| 328 | } |
| 329 | |
| 330 | fn pick_image(&mut self, image: RemoteImage, window: &mut Window, cx: &mut Context<Self>) { |
| 331 | if let Some(popover) = &self.popover { |
| 332 | popover |
| 333 | .update(cx, |state, cx| state.dismiss(window, cx)) |
| 334 | .ok(); |
| 335 | } |
| 336 | cx.emit(FunPickerEvent::Image(image)); |
| 337 | } |
| 338 | |
| 339 | fn select_chip(&mut self, chip: StickerChip, cx: &mut Context<Self>) { |
| 340 | self.chip = chip; |
| 341 | self.refresh(false, cx); |
| 342 | } |
| 343 | |
| 344 | fn tab_button(&self, tab: Tab, cx: &mut Context<Self>) -> Stateful<Div> { |
| 345 | let selected = tab == self.tab; |
| 346 | div() |
| 347 | .id(("fun-picker-tab", tab as usize)) |
| 348 | .h(px(32.)) |
| 349 | .px(px(14.)) |
| 350 | .flex() |
| 351 | .items_center() |
| 352 | .cursor_pointer() |
| 353 | .text_size(px(13.)) |
| 354 | .font_weight(FontWeight::SEMIBOLD) |
| 355 | .border_b_2() |
| 356 | .border_color(if selected { |
| 357 | theme::accent() |
| 358 | } else { |
| 359 | transparent_black() |
| 360 | }) |
| 361 | .text_color(if selected { |
| 362 | theme::accent_text() |
| 363 | } else { |
| 364 | theme::text_muted() |
| 365 | }) |
| 366 | .hover(|button| button.text_color(theme::text_strong())) |
| 367 | .child(tab.label()) |
| 368 | .on_click(cx.listener(move |this, _, window, cx| this.set_tab(tab, window, cx))) |
| 369 | } |
| 370 | |
| 371 | fn search_field(&self) -> Div { |
| 372 | div() |
| 373 | .h(px(32.)) |
| 374 | .px(px(8.)) |
| 375 | .flex() |
| 376 | .flex_none() |
| 377 | .items_center() |
| 378 | .rounded(px(8.)) |
| 379 | .bg(theme::popup_hover()) |
| 380 | .child(Input::new(&self.input).appearance(false).bordered(false)) |
| 381 | } |
| 382 | |
| 383 | fn message(text: String) -> Div { |
| 384 | div() |
| 385 | .size_full() |
| 386 | .flex() |
| 387 | .flex_col() |
| 388 | .gap(px(8.)) |
| 389 | .items_center() |
| 390 | .justify_center() |
| 391 | .text_size(px(12.5)) |
| 392 | .text_color(theme::text_muted()) |
| 393 | .child(text) |
| 394 | } |
| 395 | |
| 396 | fn emoji_row(&self, index: usize, cx: &mut Context<Self>) -> AnyElement { |
| 397 | let row = match &self.emoji_rows[index] { |
| 398 | EmojiRow::Header(title) => { |
| 399 | return div() |
| 400 | .h(px(EMOJI_CELL + EMOJI_GAP)) |
| 401 | .flex() |
| 402 | .items_end() |
| 403 | .pb(px(4.)) |
| 404 | .text_size(px(11.)) |
| 405 | .text_color(theme::text_muted()) |
| 406 | .child(*title) |
| 407 | .into_any_element(); |
| 408 | } |
| 409 | EmojiRow::Cells(glyphs) => glyphs, |
| 410 | }; |
| 411 | let cells = row.iter().enumerate().map(|(column, glyph)| { |
| 412 | let picked = glyph.clone(); |
| 413 | div() |
| 414 | .id(("fun-picker-emoji", index * EMOJI_COLUMNS + column)) |
| 415 | .size(px(EMOJI_CELL)) |
| 416 | .flex_none() |
| 417 | .flex() |
| 418 | .items_center() |
| 419 | .justify_center() |
| 420 | .rounded(px(6.)) |
| 421 | .text_size(px(20.)) |
| 422 | .cursor_pointer() |
| 423 | .hover(|cell| cell.bg(theme::popup_hover())) |
| 424 | .child(glyph.clone()) |
| 425 | .on_click(cx.listener(move |this, _, _, cx| this.pick_emoji(&picked, cx))) |
| 426 | }); |
| 427 | h_flex() |
| 428 | .h(px(EMOJI_CELL + EMOJI_GAP)) |
| 429 | .gap(px(EMOJI_GAP)) |
| 430 | .children(cells) |
| 431 | .into_any_element() |
| 432 | } |
| 433 | |
| 434 | fn render_emoji(&self, cx: &mut Context<Self>) -> AnyElement { |
| 435 | if self.emoji_rows.is_empty() { |
| 436 | return Self::message(format!("Keine Emojis zu \"{}\"", self.query)).into_any_element(); |
| 437 | } |
| 438 | uniform_list( |
| 439 | "fun-picker-emoji-list", |
| 440 | self.emoji_rows.len(), |
| 441 | cx.processor(|this, range: std::ops::Range<usize>, _, cx| { |
| 442 | range |
| 443 | .map(|index| this.emoji_row(index, cx)) |
| 444 | .collect::<Vec<_>>() |
| 445 | }), |
| 446 | ) |
| 447 | .track_scroll(&self.emoji_scroll) |
| 448 | .size_full() |
| 449 | .into_any_element() |
| 450 | } |
| 451 | |
| 452 | fn gif_tile(&self, gif: &Gif, index: usize, cx: &mut Context<Self>) -> AnyElement { |
| 453 | let picked = RemoteImage::gif(gif); |
| 454 | div() |
| 455 | .id(("fun-picker-gif", index)) |
| 456 | .w(px(gif_tile_width())) |
| 457 | .h(px(gif_tile_height(gif))) |
| 458 | .flex_none() |
| 459 | .rounded(px(TILE_RADIUS)) |
| 460 | .overflow_hidden() |
| 461 | .bg(theme::surface_raised()) |
| 462 | .cursor_pointer() |
| 463 | .hover(|tile| tile.opacity(0.85)) |
| 464 | .child( |
| 465 | img(image_source(&gif.preview_url)) |
| 466 | .size_full() |
| 467 | .object_fit(ObjectFit::Cover), |
| 468 | ) |
| 469 | .on_click( |
| 470 | cx.listener(move |this, _, window, cx| this.pick_image(picked.clone(), window, cx)), |
| 471 | ) |
| 472 | .into_any_element() |
| 473 | } |
| 474 | |
| 475 | fn gif_columns(columns: Vec<Vec<AnyElement>>) -> AnyElement { |
| 476 | h_flex() |
| 477 | .w_full() |
| 478 | .items_start() |
| 479 | .gap(px(GIF_GAP)) |
| 480 | .children( |
| 481 | columns |
| 482 | .into_iter() |
| 483 | .map(|column| v_flex().gap(px(GIF_GAP)).children(column)), |
| 484 | ) |
| 485 | .into_any_element() |
| 486 | } |
| 487 | |
| 488 | fn render_gifs(&self, cx: &mut Context<Self>) -> AnyElement { |
| 489 | let body = match &self.gifs { |
| 490 | GifState::Loading => { |
| 491 | let layout = masonry(&GIF_PLACEHOLDER_HEIGHTS, GIF_COLUMNS); |
| 492 | Self::gif_columns( |
| 493 | layout |
| 494 | .into_iter() |
| 495 | .map(|column| { |
| 496 | column |
| 497 | .into_iter() |
| 498 | .map(|index| { |
| 499 | div() |
| 500 | .w(px(gif_tile_width())) |
| 501 | .h(px(GIF_PLACEHOLDER_HEIGHTS[index])) |
| 502 | .flex_none() |
| 503 | .rounded(px(TILE_RADIUS)) |
| 504 | .bg(theme::surface_raised()) |
| 505 | .into_any_element() |
| 506 | }) |
| 507 | .collect() |
| 508 | }) |
| 509 | .collect(), |
| 510 | ) |
| 511 | } |
| 512 | GifState::Ready(found) if found.is_empty() => { |
| 513 | let text = if self.query.is_empty() { |
| 514 | "Keine GIFs gefunden".to_owned() |
| 515 | } else { |
| 516 | format!("Keine GIFs zu \"{}\"", self.query) |
| 517 | }; |
| 518 | return Self::message(text).into_any_element(); |
| 519 | } |
| 520 | GifState::Ready(found) => { |
| 521 | let heights: Vec<f32> = found.iter().map(gif_tile_height).collect(); |
| 522 | let layout = masonry(&heights, GIF_COLUMNS); |
| 523 | Self::gif_columns( |
| 524 | layout |
| 525 | .into_iter() |
| 526 | .map(|column| { |
| 527 | column |
| 528 | .into_iter() |
| 529 | .map(|index| self.gif_tile(&found[index], index, cx)) |
| 530 | .collect() |
| 531 | }) |
| 532 | .collect(), |
| 533 | ) |
| 534 | } |
| 535 | GifState::Failed => { |
| 536 | return Self::message("GIFs konnten nicht geladen werden.".to_owned()) |
| 537 | .child( |
| 538 | div() |
| 539 | .id("fun-picker-gif-retry") |
| 540 | .h(px(28.)) |
| 541 | .px(px(12.)) |
| 542 | .flex() |
| 543 | .items_center() |
| 544 | .rounded(px(6.)) |
| 545 | .bg(theme::popup_hover()) |
| 546 | .text_color(theme::text()) |
| 547 | .cursor_pointer() |
| 548 | .hover(|button| button.bg(theme::border_strong())) |
| 549 | .child("Erneut versuchen") |
| 550 | .on_click(cx.listener(|this, _, _, cx| this.refresh(false, cx))), |
| 551 | ) |
| 552 | .into_any_element(); |
| 553 | } |
| 554 | GifState::Disabled => { |
| 555 | return Self::message("GIFs sind in deiner Organisation deaktiviert.".to_owned()) |
| 556 | .into_any_element(); |
| 557 | } |
| 558 | }; |
| 559 | div() |
| 560 | .id("fun-picker-gifs") |
| 561 | .size_full() |
| 562 | .overflow_y_scroll() |
| 563 | .child(body) |
| 564 | .into_any_element() |
| 565 | } |
| 566 | |
| 567 | fn sticker_row(&self, index: usize, cx: &mut Context<Self>) -> AnyElement { |
| 568 | let size = sticker_tile_size(); |
| 569 | let tiles = self.sticker_rows[index] |
| 570 | .iter() |
| 571 | .enumerate() |
| 572 | .map(|(column, sticker)| { |
| 573 | let picked = RemoteImage::sticker(sticker); |
| 574 | div() |
| 575 | .id(("fun-picker-sticker", index * STICKER_COLUMNS + column)) |
| 576 | .size(px(size)) |
| 577 | .flex_none() |
| 578 | .p(px(4.)) |
| 579 | .rounded(px(TILE_RADIUS)) |
| 580 | .bg(theme::popup_hover()) |
| 581 | .cursor_pointer() |
| 582 | .hover(|tile| tile.bg(theme::border_strong())) |
| 583 | .child( |
| 584 | img(image_source(&sticker.url())) |
| 585 | .size_full() |
| 586 | .object_fit(ObjectFit::Contain), |
| 587 | ) |
| 588 | .on_click(cx.listener(move |this, _, window, cx| { |
| 589 | this.pick_image(picked.clone(), window, cx) |
| 590 | })) |
| 591 | }); |
| 592 | h_flex() |
| 593 | .h(px(size + STICKER_GAP)) |
| 594 | .gap(px(STICKER_GAP)) |
| 595 | .children(tiles) |
| 596 | .into_any_element() |
| 597 | } |
| 598 | |
| 599 | fn chip_button(&self, chip: StickerChip, cx: &mut Context<Self>) -> Stateful<Div> { |
| 600 | let (label, id) = match chip { |
| 601 | StickerChip::Popular => (POPULAR_LABEL.to_owned(), "popular"), |
| 602 | StickerChip::Category(category) => (stickers::category_label(category), category), |
| 603 | }; |
| 604 | let selected = self.query.is_empty() && chip == self.chip; |
| 605 | div() |
| 606 | .id(ElementId::Name(format!("fun-picker-chip-{id}").into())) |
| 607 | .h(px(26.)) |
| 608 | .px(px(10.)) |
| 609 | .flex_none() |
| 610 | .flex() |
| 611 | .items_center() |
| 612 | .rounded_full() |
| 613 | .cursor_pointer() |
| 614 | .text_size(px(12.)) |
| 615 | .when(selected, |button| { |
| 616 | button |
| 617 | .bg(theme::accent_soft()) |
| 618 | .text_color(theme::accent_text()) |
| 619 | }) |
| 620 | .when(!selected, |button| { |
| 621 | button |
| 622 | .bg(theme::surface_raised()) |
| 623 | .text_color(theme::text_muted()) |
| 624 | .hover(|button| button.text_color(theme::text_strong())) |
| 625 | }) |
| 626 | .child(label) |
| 627 | .on_click(cx.listener(move |this, _, _, cx| this.select_chip(chip, cx))) |
| 628 | } |
| 629 | |
| 630 | fn render_stickers(&self, cx: &mut Context<Self>) -> AnyElement { |
| 631 | let chips = std::iter::once(StickerChip::Popular) |
| 632 | .chain( |
| 633 | stickers::categories() |
| 634 | .into_iter() |
| 635 | .map(StickerChip::Category), |
| 636 | ) |
| 637 | .map(|chip| self.chip_button(chip, cx)) |
| 638 | .collect::<Vec<_>>(); |
| 639 | let grid = if self.sticker_rows.is_empty() { |
| 640 | Self::message(format!("Keine Sticker zu \"{}\"", self.query)).into_any_element() |
| 641 | } else { |
| 642 | uniform_list( |
| 643 | "fun-picker-sticker-list", |
| 644 | self.sticker_rows.len(), |
| 645 | cx.processor(|this, range: std::ops::Range<usize>, _, cx| { |
| 646 | range |
| 647 | .map(|index| this.sticker_row(index, cx)) |
| 648 | .collect::<Vec<_>>() |
| 649 | }), |
| 650 | ) |
| 651 | .track_scroll(&self.sticker_scroll) |
| 652 | .size_full() |
| 653 | .into_any_element() |
| 654 | }; |
| 655 | v_flex() |
| 656 | .size_full() |
| 657 | .gap(px(8.)) |
| 658 | .child( |
| 659 | h_flex() |
| 660 | .id("fun-picker-chips") |
| 661 | .w_full() |
| 662 | .flex_none() |
| 663 | .gap(px(6.)) |
| 664 | .overflow_x_scroll() |
| 665 | .children(chips), |
| 666 | ) |
| 667 | .child(div().flex_1().min_h_0().child(grid)) |
| 668 | .into_any_element() |
| 669 | } |
| 670 | } |
| 671 | |
| 672 | impl Render for FunPicker { |
| 673 | fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement { |
| 674 | let tabs = Tab::ALL.map(|tab| self.tab_button(tab, cx)); |
| 675 | let body = match self.tab { |
| 676 | Tab::Emoji => self.render_emoji(cx), |
| 677 | Tab::Gif => self.render_gifs(cx), |
| 678 | Tab::Sticker => self.render_stickers(cx), |
| 679 | }; |
| 680 | v_flex() |
| 681 | .w(px(WIDTH)) |
| 682 | .h(px(HEIGHT)) |
| 683 | .gap(px(8.)) |
| 684 | .p(px(PADDING)) |
| 685 | .child(h_flex().flex_none().gap(px(2.)).children(tabs)) |
| 686 | .child(self.search_field()) |
| 687 | .child(div().flex_1().min_h_0().child(body)) |
| 688 | .when(self.tab == Tab::Gif, |picker| { |
| 689 | picker.child( |
| 690 | div() |
| 691 | .flex_none() |
| 692 | .flex() |
| 693 | .justify_end() |
| 694 | .text_size(px(10.5)) |
| 695 | .text_color(theme::text_faint()) |
| 696 | .child("Powered by GIPHY"), |
| 697 | ) |
| 698 | }) |
| 699 | } |
| 700 | } |
| 701 | |
| 702 | #[cfg(test)] |
| 703 | mod tests { |
| 704 | use super::{EmojiRow, STICKER_COLUMNS, StickerChip, Tab, emoji_rows, masonry, sticker_rows}; |
| 705 | |
| 706 | #[test] |
| 707 | fn tabs_round_trip_through_their_keys() { |
| 708 | for tab in Tab::ALL { |
| 709 | assert_eq!(Tab::from_key(tab.key()), Some(tab)); |
| 710 | } |
| 711 | assert_eq!(Tab::from_key("unknown"), None); |
| 712 | } |
| 713 | |
| 714 | #[test] |
| 715 | fn masonry_fills_the_shortest_column() { |
| 716 | let layout = masonry(&[100., 50., 50., 50.], 2); |
| 717 | assert_eq!(layout, vec![vec![0, 3], vec![1, 2]]); |
| 718 | } |
| 719 | |
| 720 | #[test] |
| 721 | fn emoji_rows_lead_with_recent_and_filter_by_query() { |
| 722 | let recent = vec!["\u{1F44D}".to_owned()]; |
| 723 | let rows = emoji_rows("", &recent); |
| 724 | assert!(matches!(rows[0], EmojiRow::Header("Zuletzt verwendet"))); |
| 725 | assert!(matches!(&rows[1], EmojiRow::Cells(cells) if cells == &recent)); |
| 726 | let found = emoji_rows("thumbs", &recent); |
| 727 | assert!(!found.is_empty()); |
| 728 | assert!(found.iter().all(|row| matches!(row, EmojiRow::Cells(_)))); |
| 729 | } |
| 730 | |
| 731 | #[test] |
| 732 | fn sticker_rows_hold_four_and_search_spans_categories() { |
| 733 | let popular = sticker_rows("", StickerChip::Popular); |
| 734 | assert_eq!(popular.len(), 2); |
| 735 | assert_eq!(popular[0].len(), STICKER_COLUMNS); |
| 736 | let found = sticker_rows("coffee", StickerChip::Category("Clippy")); |
| 737 | assert!( |
| 738 | found |
| 739 | .iter() |
| 740 | .flatten() |
| 741 | .any(|sticker| sticker.id == "Octocorn_Coffee") |
| 742 | ); |
| 743 | } |
| 744 | } |