BlueGit

ict rusty-teams

Files

rusty-teams / crates / app / src / settings / notifications.rs

notifications.rs

notifications.rs 10.3 KiB Download Raw
1use gpui_kit::component::{h_flex, v_flex};
2use gpui_kit::*;
3
4use teams_core::ChatSettingChange;
5
6use super::account::{
7 ACTIVITY_OR_OFF, CHAT_MESSAGES, MEETING_CHATS, MENTIONS, PRESETS, account_select,
8 meeting_chats_detail, preset_detail,
9};
10use super::rows::{Choice, caption, control_row, select, text_button, toggle_row};
11use crate::app_state::AppState;
12use crate::i18n::tr;
13use crate::notify::NotificationCenter;
14use crate::notify::rules::{Corner, Settings};
15use crate::notify::sounds::AlertSound;
16use crate::presence_follow::{
17 MANAGE_STATUS_NOTIFICATIONS, STATUS_NOTIFICATIONS_DESCRIPTION, TURN_OFF,
18};
19use crate::theme;
20
21type Accessor = fn(&mut Settings) -> &mut bool;
22type SoundAccessor = fn(&mut Settings) -> &mut AlertSound;
23
24struct Toggle {
25 id: &'static str,
26 title_key: &'static str,
27 detail_key: Option<&'static str>,
28 field: Accessor,
29}
30
31const FLASH: Toggle = Toggle {
32 id: "settings-flash",
33 title_key: "settings.notifications.flash_taskbar",
34 detail_key: Some("settings.notifications.flash_taskbar_detail"),
35 field: |settings| &mut settings.flash,
36};
37
38const NOTIFICATION_SOUND: Toggle = Toggle {
39 id: "settings-notification-sound",
40 title_key: "settings.notifications.play_sounds",
41 detail_key: None,
42 field: |settings| &mut settings.sounds.notification_sound,
43};
44
45const URGENT_SOUND: Toggle = Toggle {
46 id: "settings-urgent-sound",
47 title_key: "settings.notifications.urgent_sounds",
48 detail_key: None,
49 field: |settings| &mut settings.sounds.urgent_sound,
50};
51
52const CALL_SOUNDS: [Toggle; 2] = [
53 Toggle {
54 id: "settings-call-sound",
55 title_key: "settings.notifications.call_sounds",
56 detail_key: None,
57 field: |settings| &mut settings.sounds.call_and_meeting_sound,
58 },
59 Toggle {
60 id: "settings-busy-sound",
61 title_key: "settings.notifications.busy_sounds",
62 detail_key: None,
63 field: |settings| &mut settings.sounds.sound_when_busy,
64 },
65];
66
67const HIDE_IN_CALLS: Toggle = Toggle {
68 id: "settings-hide-in-calls",
69 title_key: "settings.notifications.hide_in_calls",
70 detail_key: Some("settings.notifications.hide_in_calls_detail"),
71 field: |settings| &mut settings.mute_in_calls,
72};
73
74pub fn render(center: &Entity<NotificationCenter>, app: &Entity<AppState>, cx: &App) -> Div {
75 let account = app.read(cx).account_chat_settings().notifications;
76 let mut current = center.read(cx).settings().clone();
77 let corner = current.corner;
78 let sounds = current.sounds.clone();
79 let mut toggle = |toggle: &Toggle| {
80 let on = *(toggle.field)(&mut current);
81 toggle_element(center, toggle, on)
82 };
83 let flash = toggle(&FLASH);
84 let notification_sound = toggle(&NOTIFICATION_SOUND);
85 let urgent_sound = toggle(&URGENT_SOUND);
86 let call_sounds: Vec<Stateful<Div>> = CALL_SOUNDS.iter().map(&mut toggle).collect();
87 let hide_in_calls = toggle(&HIDE_IN_CALLS);
88 let test_center = center.clone();
89 v_flex()
90 .w_full()
91 .child(caption(tr("settings.caption.appearance_and_sound")))
92 .child(preview_toggle(app, account.show_preview))
93 .child(flash)
94 .child(control_row(
95 tr("settings.notifications.position"),
96 tr("settings.notifications.position_detail"),
97 select(
98 "settings-position",
99 corner.label(),
100 false,
101 corner_choices(center, corner),
102 ),
103 ))
104 .child(control_row(
105 tr("settings.notifications.test"),
106 tr("settings.notifications.test_detail"),
107 text_button(
108 "settings-test-notification",
109 tr("settings.notifications.send_test"),
110 )
111 .on_click(move |_, _, cx| {
112 test_center.update(cx, |center, cx| center.send_test_notification(cx))
113 }),
114 ))
115 .child(caption(tr("settings.caption.sound")))
116 .child(notification_sound)
117 .child(sound_row(
118 center,
119 (
120 "settings-notification-sound-type",
121 "settings-notification-sound-preview",
122 ),
123 tr("settings.notifications.notification_sound"),
124 &AlertSound::NORMAL,
125 sounds.notification_sound_type,
126 sounds.notification_sound,
127 |settings| &mut settings.sounds.notification_sound_type,
128 ))
129 .child(urgent_sound)
130 .child(sound_row(
131 center,
132 (
133 "settings-urgent-sound-type",
134 "settings-urgent-sound-preview",
135 ),
136 tr("settings.notifications.urgent_sound"),
137 &AlertSound::URGENT,
138 sounds.urgent_sound_type,
139 sounds.urgent_sound,
140 |settings| &mut settings.sounds.urgent_sound_type,
141 ))
142 .children(call_sounds)
143 .child(caption("TEAMS AND CHANNELS"))
144 .child(account_select(
145 app,
146 (
147 "settings-notification-preset",
148 tr("settings.notifications.teams_channels"),
149 preset_detail(account.preset),
150 ),
151 &PRESETS,
152 account.preset,
153 ChatSettingChange::Preset,
154 ))
155 .child(caption("CHAT"))
156 .child(account_select(
157 app,
158 ("settings-chat-messages", tr("settings.notifications.chat_messages"), ""),
159 &CHAT_MESSAGES,
160 account.chat_messages,
161 ChatSettingChange::ChatMessages,
162 ))
163 .child(account_select(
164 app,
165 ("settings-personal-mentions", "Personal @mentions", ""),
166 &MENTIONS,
167 account.personal_mentions,
168 ChatSettingChange::PersonalMentions,
169 ))
170 .child(account_select(
171 app,
172 (
173 "settings-everyone-mentions",
174 "Everyone in chat @mentions",
175 "",
176 ),
177 &ACTIVITY_OR_OFF,
178 account.everyone_mentions,
179 ChatSettingChange::EveryoneMentions,
180 ))
181 .child(account_select(
182 app,
183 ("settings-likes", tr("settings.notifications.likes_reactions"), ""),
184 &ACTIVITY_OR_OFF,
185 account.likes,
186 ChatSettingChange::Likes,
187 ))
188 .child(caption("MEETINGS AND CALLS"))
189 .child(account_select(
190 app,
191 (
192 "settings-meeting-chats",
193 tr("settings.notifications.meeting_chats"),
194 meeting_chats_detail(account.meeting_chats),
195 ),
196 &MEETING_CHATS,
197 account.meeting_chats,
198 ChatSettingChange::MeetingChats,
199 ))
200 .child(hide_in_calls)
201 .child(caption("PEOPLE"))
202 .child(control_row(
203 MANAGE_STATUS_NOTIFICATIONS,
204 STATUS_NOTIFICATIONS_DESCRIPTION,
205 div(),
206 ))
207 .children(followed_rows(app, cx))
208}
209
210fn followed_rows(app: &Entity<AppState>, cx: &App) -> Vec<Div> {
211 app.read(cx)
212 .followed_presence
213 .iter()
214 .enumerate()
215 .map(|(index, person)| {
216 let (app, user_id, name) = (app.clone(), person.user_id.clone(), person.name.clone());
217 control_row(
218 person.name.clone(),
219 "",
220 text_button(("settings-unfollow-presence", index), TURN_OFF).on_click(
221 move |_, _, cx| {
222 app.update(cx, |state, cx| {
223 state.set_presence_followed(&user_id, &name, false, cx)
224 })
225 },
226 ),
227 )
228 })
229 .collect()
230}
231
232fn preview_toggle(app: &Entity<AppState>, on: bool) -> Stateful<Div> {
233 let app = app.clone();
234 toggle_row(
235 "settings-preview",
236 tr("settings.notifications.show_preview"),
237 "Off: only \"New message\"",
238 theme::text_muted(),
239 on,
240 )
241 .on_click(move |_, _, cx| {
242 app.update(cx, |state, cx| {
243 state.save_chat_setting(ChatSettingChange::ShowPreview(!on), cx)
244 })
245 })
246}
247
248fn toggle_element(center: &Entity<NotificationCenter>, toggle: &Toggle, on: bool) -> Stateful<Div> {
249 let center = center.clone();
250 let field = toggle.field;
251 let detail = toggle
252 .detail_key
253 .map(tr)
254 .unwrap_or_default();
255 toggle_row(
256 toggle.id,
257 tr(toggle.title_key),
258 detail,
259 theme::text_muted(),
260 on,
261 )
262 .on_click(move |_, _, cx| {
263 center.update(cx, |center, cx| {
264 center.update_settings(cx, |settings| {
265 let value = field(settings);
266 *value = !*value;
267 })
268 })
269 })
270}
271
272fn corner_choices(center: &Entity<NotificationCenter>, current: Corner) -> Vec<Choice> {
273 Corner::ALL
274 .into_iter()
275 .map(|corner| {
276 let center = center.clone();
277 Choice::new(corner.label(), corner == current, move |cx| {
278 center.update(cx, |center, cx| {
279 center.update_settings(cx, |settings| settings.corner = corner)
280 })
281 })
282 })
283 .collect()
284}
285
286fn sound_row(
287 center: &Entity<NotificationCenter>,
288 (id, preview_id): (&'static str, &'static str),
289 title: impl Into<SharedString>,
290 options: &'static [AlertSound],
291 current: AlertSound,
292 enabled: bool,
293 field: SoundAccessor,
294) -> Div {
295 let choices = options
296 .iter()
297 .map(|sound| {
298 let (center, sound) = (center.clone(), *sound);
299 Choice::new(sound.label(), sound == current, move |cx| {
300 center.update(cx, |center, cx| {
301 center.update_settings(cx, |settings| *field(settings) = sound);
302 center.preview_sound(sound.file_name(), cx);
303 })
304 })
305 })
306 .collect();
307 let preview_center = center.clone();
308 control_row(
309 title,
310 "",
311 h_flex()
312 .gap(px(8.))
313 .items_center()
314 .child(select(id, current.label(), !enabled, choices))
315 .child(
316 text_button(preview_id, "Preview").on_click(move |_, _, cx| {
317 preview_center.update(cx, |center, cx| {
318 center.preview_sound(current.file_name(), cx)
319 })
320 }),
321 ),
322 )
323}