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::app_state::AppState;
16use crate::default_reactions::{
17 filled, place_in_slot, quick_reaction_glyphs, reaction_id_for_glyph, slots_for,
18};
19use crate::emoji;
20use crate::emoticons::{SKIN_TONE_COUNT, with_tone};
21use crate::theme;
22
23pub const CUSTOMIZE_TOOLTIP: &str = "Customise default reactions";
24const TITLE: &str = "Customise your reactions and appearance in Teams";
25const DESCRIPTION: &str = "Click on a reaction below, then choose an emoji to replace it with.";
26const SKIN_TONE_TEXT: &str =
27 "Choose a skin tone. After selection, reactions and emoji will appear with this skin tone.";
28const CHOOSE_EMOJI: &str = "Choose an emoji";
29const REMOVE_REACTION: &str = "Remove reaction";
30const TONE_SAMPLE: &str = "\u{1F44D}";
31const DIALOG_WIDTH: f32 = 440.;
32const SLOT_SIZE: f32 = 48.;
33const SLOT_GLYPH_SIZE: f32 = 26.;
34const TONE_SIZE: f32 = 36.;
35const TONE_GLYPH_SIZE: f32 = 20.;
36const REMOVE_SIZE: f32 = 18.;
37const REMOVE_OVERHANG: f32 = 5.;
38const SECTION_GAP: f32 = 16.;
39const TEXT_SIZE: f32 = 13.;
40
41struct 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
50impl 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) => button.child(
128 div()
129 .text_size(px(SLOT_GLYPH_SIZE))
130 .font_family(theme::EMOJI_FONT_FAMILY)
131 .child(glyph.clone()),
132 ),
133 None => button.child(icon(IconName::Plus, 18., theme::text_muted())),
134 });
135 let (picker, view) = (self.picker.clone(), cx.entity().downgrade());
136 let selected: HashSet<String> = glyph.iter().cloned().collect();
137 let popover = Popover::new(("customize-reaction-picker", index))
138 .anchor(Anchor::TopLeft)
139 .trigger(trigger)
140 .on_open_change({
141 let picker = self.picker.clone();
142 move |open, window, cx| {
143 if *open {
144 picker.update(cx, |picker, cx| picker.reset(window, cx));
145 }
146 }
147 })
148 .content(move |_, _, cx| {
149 let popover = cx.entity().downgrade();
150 let view = view.clone();
151 picker.update(cx, |picker, _| {
152 picker.set_target(
153 Rc::new(move |glyph, window, cx| {
154 view.update(cx, |form, cx| form.place(index, glyph, window, cx))
155 .ok();
156 }),
157 Rc::new(|_, _| {}),
158 popover,
159 selected.clone(),
160 )
161 });
162 picker.clone()
163 });
164 let remove = glyph.is_some().then(|| {
165 corner_badge(Corner::TopRight, REMOVE_OVERHANG).child(
166 div()
167 .id(("customize-reaction-remove", index))
168 .size(px(REMOVE_SIZE))
169 .flex()
170 .items_center()
171 .justify_center()
172 .rounded_full()
173 .cursor_pointer()
174 .bg(theme::popup_hover())
175 .hover(|button| button.bg(theme::row_hover()))
176 .child(icon(IconName::X, 10., theme::text_soft()))
177 .tooltip(|window, cx| Tooltip::new(REMOVE_REACTION).build(window, cx))
178 .on_click(cx.listener(move |form, _, window, cx| {
179 cx.stop_propagation();
180 form.remove(index, window, cx);
181 })),
182 )
183 });
184 div()
185 .relative()
186 .child(popover)
187 .children(remove)
188 .into_any_element()
189 }
190
191 fn tone_choice(&self, tone: u8, cx: &mut Context<Self>) -> AnyElement {
192 let selected = tone == self.skin_tone;
193 div()
194 .id(("customize-skin-tone", usize::from(tone)))
195 .size(px(TONE_SIZE))
196 .flex()
197 .items_center()
198 .justify_center()
199 .rounded_full()
200 .cursor_pointer()
201 .text_size(px(TONE_GLYPH_SIZE))
202 .font_family(theme::EMOJI_FONT_FAMILY)
203 .when(selected, |choice| choice.bg(theme::reaction_on_own()))
204 .when(!selected, |choice| {
205 choice.hover(|choice| choice.bg(theme::popup_hover()))
206 })
207 .child(with_tone(TONE_SAMPLE, tone))
208 .on_click(cx.listener(move |form, _, window, cx| {
209 form.skin_tone = tone;
210 changed(window, cx);
211 }))
212 .into_any_element()
213 }
214}
215
216impl Render for CustomizeForm {
217 fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
218 let slots: Vec<AnyElement> = (0..self.slots.len())
219 .map(|index| self.slot(index, cx))
220 .collect();
221 let tones: Vec<AnyElement> = (0..=SKIN_TONE_COUNT)
222 .map(|tone| self.tone_choice(tone, cx))
223 .collect();
224 v_flex()
225 .gap(px(SECTION_GAP))
226 .text_size(px(TEXT_SIZE))
227 .text_color(theme::text())
228 .child(div().text_color(theme::text_soft()).child(DESCRIPTION))
229 .child(
230 h_flex()
231 .justify_center()
232 .gap(px(10.))
233 .py(px(4.))
234 .children(slots),
235 )
236 .child(div().text_color(theme::text_soft()).child(SKIN_TONE_TEXT))
237 .child(h_flex().gap(px(6.)).children(tones))
238 }
239}
240
241/// The dialog footer is built by the window root, so Save only re-evaluates `unchanged` on a window refresh.
242fn changed(window: &mut Window, cx: &mut Context<CustomizeForm>) {
243 cx.notify();
244 window.refresh();
245}
246
247pub fn open_customize_reactions(state: Entity<AppState>, window: &mut Window, cx: &mut App) {
248 let form = cx.new(|cx| CustomizeForm::new(state, window, cx));
249 window.open_dialog(cx, move |dialog: Dialog, _, cx| {
250 let submit_form = form.clone();
251 let unchanged = form.read(cx).unchanged();
252 let footer = DialogFooter::new()
253 .child(
254 Button::new("customize-reactions-cancel")
255 .label("Cancel")
256 .on_click(|_, window, cx| window.close_dialog(cx)),
257 )
258 .child(
259 Button::new("customize-reactions-save")
260 .primary()
261 .label("Save")
262 .disabled(unchanged)
263 .on_click(move |_, window, cx| {
264 submit_form.update(cx, |form, cx| form.save(window, cx));
265 }),
266 );
267 dialog
268 .title(TITLE)
269 .w(px(DIALOG_WIDTH))
270 .child(form.clone())
271 .footer(footer)
272 });
273}