| 1 | use std::rc::Rc; |
| 2 | |
| 3 | use gpui_kit::base::POPUP_PRIORITY; |
| 4 | use gpui_kit::component::h_flex; |
| 5 | use gpui_kit::prelude::FluentBuilder as _; |
| 6 | use gpui_kit::*; |
| 7 | |
| 8 | use super::widgets::{Corner, corner_badge}; |
| 9 | use crate::emoticons::{SKIN_TONE_COUNT, with_tone}; |
| 10 | use crate::theme::{self, ElevatedSurface as _}; |
| 11 | |
| 12 | const CHOICE_SIZE: f32 = 32.; |
| 13 | const CHOICE_GLYPH_SIZE: f32 = 20.; |
| 14 | const PANEL_PADDING: f32 = 4.; |
| 15 | const PANEL_GAP: f32 = 4.; |
| 16 | const BADGE_SIZE: f32 = 4.; |
| 17 | const BADGE_INSET: f32 = 3.; |
| 18 | const MENU_GAP: f32 = 12.; |
| 19 | |
| 20 | pub type TonePick = Rc<dyn Fn(u8, &mut Window, &mut App)>; |
| 21 | pub type ToneDismiss = Rc<dyn Fn(&mut Window, &mut App)>; |
| 22 | |
| 23 | pub fn tone_badge() -> Div { |
| 24 | corner_badge(Corner::BottomRight, -BADGE_INSET).child( |
| 25 | div() |
| 26 | .size(px(BADGE_SIZE)) |
| 27 | .rounded_full() |
| 28 | .bg(theme::text_muted()), |
| 29 | ) |
| 30 | } |
| 31 | |
| 32 | pub fn tone_choices( |
| 33 | id: impl Into<SharedString>, |
| 34 | position: Point<Pixels>, |
| 35 | glyph: &str, |
| 36 | current: u8, |
| 37 | pick: TonePick, |
| 38 | dismiss: ToneDismiss, |
| 39 | ) -> AnyElement { |
| 40 | let id: SharedString = id.into(); |
| 41 | let choices: Vec<_> = (0..=SKIN_TONE_COUNT) |
| 42 | .map(|tone| { |
| 43 | let pick = pick.clone(); |
| 44 | div() |
| 45 | .id(ElementId::Name(format!("{id}-{tone}").into())) |
| 46 | .size(px(CHOICE_SIZE)) |
| 47 | .flex() |
| 48 | .items_center() |
| 49 | .justify_center() |
| 50 | .rounded(px(6.)) |
| 51 | .text_size(px(CHOICE_GLYPH_SIZE)) |
| 52 | .cursor_pointer() |
| 53 | .when(tone == current, |choice| choice.bg(theme::popup_hover())) |
| 54 | .hover(|choice| choice.bg(theme::popup_hover())) |
| 55 | .child(with_tone(glyph, tone)) |
| 56 | .on_click(move |_, window, cx| { |
| 57 | cx.stop_propagation(); |
| 58 | pick(tone, window, cx); |
| 59 | }) |
| 60 | }) |
| 61 | .collect(); |
| 62 | deferred( |
| 63 | anchored() |
| 64 | .position(position - point(px(0.), px(MENU_GAP))) |
| 65 | .anchor(Anchor::BottomLeft) |
| 66 | .snap_to_window_with_margin(px(8.)) |
| 67 | .child( |
| 68 | h_flex() |
| 69 | .id(ElementId::Name(id.clone())) |
| 70 | .gap(px(PANEL_GAP)) |
| 71 | .p(px(PANEL_PADDING)) |
| 72 | .rounded(px(8.)) |
| 73 | .nested_flyout_surface() |
| 74 | .occlude() |
| 75 | .on_mouse_down_out(move |_, window, cx| dismiss(window, cx)) |
| 76 | .children(choices), |
| 77 | ), |
| 78 | ) |
| 79 | .with_priority(POPUP_PRIORITY + 1) |
| 80 | .into_any_element() |
| 81 | } |