BlueGit

ict rusty-teams

Files

rusty-teams / crates / app / src / views / customize_reactions.rs

customize_reactions.rs

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