| 1 | use std::collections::HashSet; |
| 2 | use std::rc::Rc; |
| 3 | |
| 4 | use gpui_kit::assets::IconName; |
| 5 | use gpui_kit::component::button::{Button, ButtonVariants as _}; |
| 6 | use gpui_kit::component::dialog::{Dialog, DialogFooter}; |
| 7 | use gpui_kit::component::popover::Popover; |
| 8 | use gpui_kit::component::tooltip::Tooltip; |
| 9 | use gpui_kit::component::{Disableable as _, WindowExt as _, h_flex, v_flex}; |
| 10 | use gpui_kit::prelude::FluentBuilder as _; |
| 11 | use gpui_kit::*; |
| 12 | |
| 13 | use super::reaction_picker::ReactionPicker; |
| 14 | use super::widgets::{Corner, corner_badge, icon}; |
| 15 | use crate::app_state::AppState; |
| 16 | use crate::default_reactions::{ |
| 17 | filled, place_in_slot, quick_reaction_glyphs, reaction_id_for_glyph, slots_for, |
| 18 | }; |
| 19 | use crate::emoji; |
| 20 | use crate::emoticons::{SKIN_TONE_COUNT, with_tone}; |
| 21 | use crate::theme; |
| 22 | |
| 23 | pub const CUSTOMIZE_TOOLTIP: &str = "Customise default reactions"; |
| 24 | const TITLE: &str = "Customise your reactions and appearance in Teams"; |
| 25 | const DESCRIPTION: &str = "Click on a reaction below, then choose an emoji to replace it with."; |
| 26 | const SKIN_TONE_TEXT: &str = |
| 27 | "Choose a skin tone. After selection, reactions and emoji will appear with this skin tone."; |
| 28 | const CHOOSE_EMOJI: &str = "Choose an emoji"; |
| 29 | const REMOVE_REACTION: &str = "Remove reaction"; |
| 30 | const TONE_SAMPLE: &str = "\u{1F44D}"; |
| 31 | const DIALOG_WIDTH: f32 = 440.; |
| 32 | const SLOT_SIZE: f32 = 48.; |
| 33 | const SLOT_GLYPH_SIZE: f32 = 26.; |
| 34 | const TONE_SIZE: f32 = 36.; |
| 35 | const TONE_GLYPH_SIZE: f32 = 20.; |
| 36 | const REMOVE_SIZE: f32 = 18.; |
| 37 | const REMOVE_OVERHANG: f32 = 5.; |
| 38 | const SECTION_GAP: f32 = 16.; |
| 39 | const TEXT_SIZE: f32 = 13.; |
| 40 | |
| 41 | struct CustomizeForm { |
| 42 | state: Entity<AppState>, |
| 43 | picker: Entity<ReactionPicker>, |
| 44 | saved_ids: Vec<String>, |
| 45 | saved_tone: u8, |
| 46 | slots: Vec<Option<String>>, |
| 47 | skin_tone: u8, |
| 48 | } |
| 49 | |
| 50 | impl CustomizeForm { |
| 51 | fn new(state: Entity<AppState>, window: &mut Window, cx: &mut Context<Self>) -> Self { |
| 52 | let (saved_ids, saved_tone, store) = { |
| 53 | let app = state.read(cx); |
| 54 | ( |
| 55 | app.default_reactions(), |
| 56 | teams_core::skin_tone(&app.store), |
| 57 | app.store.clone(), |
| 58 | ) |
| 59 | }; |
| 60 | let recent = cx.new(|_| emoji::Recent::load(&store)); |
| 61 | let picker_state = state.clone(); |
| 62 | let picker = |
| 63 | cx.new(|cx| ReactionPicker::new(picker_state, recent, window, cx).for_customizing()); |
| 64 | CustomizeForm { |
| 65 | state, |
| 66 | picker, |
| 67 | slots: slots_for(&saved_ids), |
| 68 | saved_ids, |
| 69 | saved_tone, |
| 70 | skin_tone: saved_tone, |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | fn unchanged(&self) -> bool { |
| 75 | filled(&self.slots) == self.saved_ids && self.skin_tone == self.saved_tone |
| 76 | } |
| 77 | |
| 78 | fn place(&mut self, index: usize, glyph: &str, window: &mut Window, cx: &mut Context<Self>) { |
| 79 | if let Some(reaction_id) = reaction_id_for_glyph(glyph) { |
| 80 | place_in_slot(&mut self.slots, index, reaction_id); |
| 81 | changed(window, cx); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | fn remove(&mut self, index: usize, window: &mut Window, cx: &mut Context<Self>) { |
| 86 | if let Some(slot) = self.slots.get_mut(index) { |
| 87 | *slot = None; |
| 88 | changed(window, cx); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | fn save(&mut self, window: &mut Window, cx: &mut Context<Self>) { |
| 93 | let reaction_ids = filled(&self.slots); |
| 94 | let (tone_changed, tone) = (self.skin_tone != self.saved_tone, self.skin_tone); |
| 95 | self.state.update(cx, |state, cx| { |
| 96 | if tone_changed { |
| 97 | state.set_skin_tone(tone, cx); |
| 98 | } |
| 99 | state.set_default_reactions(reaction_ids, cx); |
| 100 | }); |
| 101 | window.close_dialog(cx); |
| 102 | } |
| 103 | |
| 104 | fn slot_glyph(&self, reaction_id: &str) -> Option<String> { |
| 105 | quick_reaction_glyphs(&[reaction_id.to_owned()], self.skin_tone) |
| 106 | .into_iter() |
| 107 | .next() |
| 108 | } |
| 109 | |
| 110 | fn slot(&self, index: usize, cx: &mut Context<Self>) -> AnyElement { |
| 111 | let glyph = self |
| 112 | .slots |
| 113 | .get(index) |
| 114 | .cloned() |
| 115 | .flatten() |
| 116 | .and_then(|reaction_id| self.slot_glyph(&reaction_id)); |
| 117 | let label = if glyph.is_some() { |
| 118 | REMOVE_REACTION |
| 119 | } else { |
| 120 | CHOOSE_EMOJI |
| 121 | }; |
| 122 | let trigger = Button::new(("customize-reaction-slot", index)) |
| 123 | .size(px(SLOT_SIZE)) |
| 124 | .rounded(px(10.)) |
| 125 | .tooltip(label) |
| 126 | .map(|button| match &glyph { |
| 127 | Some(glyph) => { |
| 128 | button.child(div().text_size(px(SLOT_GLYPH_SIZE)).child(glyph.clone())) |
| 129 | } |
| 130 | None => button.child(icon(IconName::Plus, 18., theme::text_muted())), |
| 131 | }); |
| 132 | let (picker, view) = (self.picker.clone(), cx.entity().downgrade()); |
| 133 | let selected: HashSet<String> = glyph.iter().cloned().collect(); |
| 134 | let popover = Popover::new(("customize-reaction-picker", index)) |
| 135 | .anchor(Anchor::TopLeft) |
| 136 | .trigger(trigger) |
| 137 | .on_open_change({ |
| 138 | let picker = self.picker.clone(); |
| 139 | move |open, window, cx| { |
| 140 | if *open { |
| 141 | picker.update(cx, |picker, cx| picker.reset(window, cx)); |
| 142 | } |
| 143 | } |
| 144 | }) |
| 145 | .content(move |_, _, cx| { |
| 146 | let popover = cx.entity().downgrade(); |
| 147 | let view = view.clone(); |
| 148 | picker.update(cx, |picker, _| { |
| 149 | picker.set_target( |
| 150 | Rc::new(move |glyph, window, cx| { |
| 151 | view.update(cx, |form, cx| form.place(index, glyph, window, cx)) |
| 152 | .ok(); |
| 153 | }), |
| 154 | Rc::new(|_, _| {}), |
| 155 | popover, |
| 156 | selected.clone(), |
| 157 | ) |
| 158 | }); |
| 159 | picker.clone() |
| 160 | }); |
| 161 | let remove = glyph.is_some().then(|| { |
| 162 | corner_badge(Corner::TopRight, REMOVE_OVERHANG).child( |
| 163 | div() |
| 164 | .id(("customize-reaction-remove", index)) |
| 165 | .size(px(REMOVE_SIZE)) |
| 166 | .flex() |
| 167 | .items_center() |
| 168 | .justify_center() |
| 169 | .rounded_full() |
| 170 | .cursor_pointer() |
| 171 | .bg(theme::popup_hover()) |
| 172 | .hover(|button| button.bg(theme::row_hover())) |
| 173 | .child(icon(IconName::X, 10., theme::text_soft())) |
| 174 | .tooltip(|window, cx| Tooltip::new(REMOVE_REACTION).build(window, cx)) |
| 175 | .on_click(cx.listener(move |form, _, window, cx| { |
| 176 | cx.stop_propagation(); |
| 177 | form.remove(index, window, cx); |
| 178 | })), |
| 179 | ) |
| 180 | }); |
| 181 | div() |
| 182 | .relative() |
| 183 | .child(popover) |
| 184 | .children(remove) |
| 185 | .into_any_element() |
| 186 | } |
| 187 | |
| 188 | fn tone_choice(&self, tone: u8, cx: &mut Context<Self>) -> AnyElement { |
| 189 | let selected = tone == self.skin_tone; |
| 190 | div() |
| 191 | .id(("customize-skin-tone", usize::from(tone))) |
| 192 | .size(px(TONE_SIZE)) |
| 193 | .flex() |
| 194 | .items_center() |
| 195 | .justify_center() |
| 196 | .rounded_full() |
| 197 | .cursor_pointer() |
| 198 | .text_size(px(TONE_GLYPH_SIZE)) |
| 199 | .when(selected, |choice| choice.bg(theme::reaction_on_own())) |
| 200 | .when(!selected, |choice| { |
| 201 | choice.hover(|choice| choice.bg(theme::popup_hover())) |
| 202 | }) |
| 203 | .child(with_tone(TONE_SAMPLE, tone)) |
| 204 | .on_click(cx.listener(move |form, _, window, cx| { |
| 205 | form.skin_tone = tone; |
| 206 | changed(window, cx); |
| 207 | })) |
| 208 | .into_any_element() |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | impl Render for CustomizeForm { |
| 213 | fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement { |
| 214 | let slots: Vec<AnyElement> = (0..self.slots.len()) |
| 215 | .map(|index| self.slot(index, cx)) |
| 216 | .collect(); |
| 217 | let tones: Vec<AnyElement> = (0..=SKIN_TONE_COUNT) |
| 218 | .map(|tone| self.tone_choice(tone, cx)) |
| 219 | .collect(); |
| 220 | v_flex() |
| 221 | .gap(px(SECTION_GAP)) |
| 222 | .text_size(px(TEXT_SIZE)) |
| 223 | .text_color(theme::text()) |
| 224 | .child(div().text_color(theme::text_soft()).child(DESCRIPTION)) |
| 225 | .child( |
| 226 | h_flex() |
| 227 | .justify_center() |
| 228 | .gap(px(10.)) |
| 229 | .py(px(4.)) |
| 230 | .children(slots), |
| 231 | ) |
| 232 | .child(div().text_color(theme::text_soft()).child(SKIN_TONE_TEXT)) |
| 233 | .child(h_flex().gap(px(6.)).children(tones)) |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | /// The dialog footer is built by the window root, so Save only re-evaluates `unchanged` on a window refresh. |
| 238 | fn changed(window: &mut Window, cx: &mut Context<CustomizeForm>) { |
| 239 | cx.notify(); |
| 240 | window.refresh(); |
| 241 | } |
| 242 | |
| 243 | pub fn open_customize_reactions(state: Entity<AppState>, window: &mut Window, cx: &mut App) { |
| 244 | let form = cx.new(|cx| CustomizeForm::new(state, window, cx)); |
| 245 | window.open_dialog(cx, move |dialog: Dialog, _, cx| { |
| 246 | let submit_form = form.clone(); |
| 247 | let unchanged = form.read(cx).unchanged(); |
| 248 | let footer = DialogFooter::new() |
| 249 | .child( |
| 250 | Button::new("customize-reactions-cancel") |
| 251 | .label("Cancel") |
| 252 | .on_click(|_, window, cx| window.close_dialog(cx)), |
| 253 | ) |
| 254 | .child( |
| 255 | Button::new("customize-reactions-save") |
| 256 | .primary() |
| 257 | .label("Save") |
| 258 | .disabled(unchanged) |
| 259 | .on_click(move |_, window, cx| { |
| 260 | submit_form.update(cx, |form, cx| form.save(window, cx)); |
| 261 | }), |
| 262 | ); |
| 263 | dialog |
| 264 | .title(TITLE) |
| 265 | .w(px(DIALOG_WIDTH)) |
| 266 | .child(form.clone()) |
| 267 | .footer(footer) |
| 268 | }); |
| 269 | } |