BlueGit

ict rusty-teams

Files

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

notifications.rs

notifications.rs 8.9 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::notify::NotificationCenter;
13use crate::notify::rules::{Corner, Settings};
14use crate::notify::sounds::AlertSound;
15use crate::theme;
16
17type Accessor = fn(&mut Settings) -> &mut bool;
18type SoundAccessor = fn(&mut Settings) -> &mut AlertSound;
19
20struct Toggle {
21 id: &'static str,
22 title: &'static str,
23 detail: &'static str,
24 field: Accessor,
25}
26
27const FLASH: Toggle = Toggle {
28 id: "settings-flash",
29 title: "Flash taskbar when notifications are received",
30 detail: "On new messages, until you open the window",
31 field: |settings| &mut settings.flash,
32};
33
34const NOTIFICATION_SOUND: Toggle = Toggle {
35 id: "settings-notification-sound",
36 title: "Play sounds with notifications",
37 detail: "",
38 field: |settings| &mut settings.sounds.notification_sound,
39};
40
41const URGENT_SOUND: Toggle = Toggle {
42 id: "settings-urgent-sound",
43 title: "Play sounds with urgent and priority contact notifications",
44 detail: "",
45 field: |settings| &mut settings.sounds.urgent_sound,
46};
47
48const CALL_SOUNDS: [Toggle; 2] = [
49 Toggle {
50 id: "settings-call-sound",
51 title: "Play sounds with incoming calls, requests to join, and meeting start notifications",
52 detail: "",
53 field: |settings| &mut settings.sounds.call_and_meeting_sound,
54 },
55 Toggle {
56 id: "settings-busy-sound",
57 title: "Play sounds when I'm busy or in a meeting",
58 detail: "",
59 field: |settings| &mut settings.sounds.sound_when_busy,
60 },
61];
62
63const HIDE_IN_CALLS: Toggle = Toggle {
64 id: "settings-hide-in-calls",
65 title: "Hide notifications during calls and meetings",
66 detail: "No message toasts while a call is active",
67 field: |settings| &mut settings.mute_in_calls,
68};
69
70pub fn render(center: &Entity<NotificationCenter>, app: &Entity<AppState>, cx: &App) -> Div {
71 let account = app.read(cx).account_chat_settings().notifications;
72 let mut current = center.read(cx).settings().clone();
73 let corner = current.corner;
74 let sounds = current.sounds.clone();
75 let mut toggle = |toggle: &Toggle| {
76 let on = *(toggle.field)(&mut current);
77 toggle_element(center, toggle, on)
78 };
79 let flash = toggle(&FLASH);
80 let notification_sound = toggle(&NOTIFICATION_SOUND);
81 let urgent_sound = toggle(&URGENT_SOUND);
82 let call_sounds: Vec<Stateful<Div>> = CALL_SOUNDS.iter().map(&mut toggle).collect();
83 let hide_in_calls = toggle(&HIDE_IN_CALLS);
84 let test_center = center.clone();
85 v_flex()
86 .w_full()
87 .child(caption("APPEARANCE AND SOUND"))
88 .child(preview_toggle(app, account.show_preview))
89 .child(flash)
90 .child(control_row(
91 "Position on the screen",
92 "Corner of the main screen",
93 select(
94 "settings-position",
95 corner.label(),
96 false,
97 corner_choices(center, corner),
98 ),
99 ))
100 .child(control_row(
101 "Test notification",
102 "Shows a toast with the settings above",
103 text_button("settings-test-notification", "Send test notification").on_click(
104 move |_, _, cx| {
105 test_center.update(cx, |center, cx| center.send_test_notification(cx))
106 },
107 ),
108 ))
109 .child(caption("SOUND"))
110 .child(notification_sound)
111 .child(sound_row(
112 center,
113 (
114 "settings-notification-sound-type",
115 "settings-notification-sound-preview",
116 ),
117 "Notification sound",
118 &AlertSound::NORMAL,
119 sounds.notification_sound_type,
120 sounds.notification_sound,
121 |settings| &mut settings.sounds.notification_sound_type,
122 ))
123 .child(urgent_sound)
124 .child(sound_row(
125 center,
126 (
127 "settings-urgent-sound-type",
128 "settings-urgent-sound-preview",
129 ),
130 "Urgent sound",
131 &AlertSound::URGENT,
132 sounds.urgent_sound_type,
133 sounds.urgent_sound,
134 |settings| &mut settings.sounds.urgent_sound_type,
135 ))
136 .children(call_sounds)
137 .child(caption("TEAMS AND CHANNELS"))
138 .child(account_select(
139 app,
140 (
141 "settings-notification-preset",
142 "Teams and channels",
143 preset_detail(account.preset),
144 ),
145 &PRESETS,
146 account.preset,
147 ChatSettingChange::Preset,
148 ))
149 .child(caption("CHAT"))
150 .child(account_select(
151 app,
152 ("settings-chat-messages", "Chat message notifications", ""),
153 &CHAT_MESSAGES,
154 account.chat_messages,
155 ChatSettingChange::ChatMessages,
156 ))
157 .child(account_select(
158 app,
159 ("settings-personal-mentions", "Personal @mentions", ""),
160 &MENTIONS,
161 account.personal_mentions,
162 ChatSettingChange::PersonalMentions,
163 ))
164 .child(account_select(
165 app,
166 (
167 "settings-everyone-mentions",
168 "Everyone in chat @mentions",
169 "",
170 ),
171 &ACTIVITY_OR_OFF,
172 account.everyone_mentions,
173 ChatSettingChange::EveryoneMentions,
174 ))
175 .child(account_select(
176 app,
177 ("settings-likes", "Likes and reactions", ""),
178 &ACTIVITY_OR_OFF,
179 account.likes,
180 ChatSettingChange::Likes,
181 ))
182 .child(caption("MEETINGS AND CALLS"))
183 .child(account_select(
184 app,
185 (
186 "settings-meeting-chats",
187 "Meeting chat notifications",
188 meeting_chats_detail(account.meeting_chats),
189 ),
190 &MEETING_CHATS,
191 account.meeting_chats,
192 ChatSettingChange::MeetingChats,
193 ))
194 .child(hide_in_calls)
195}
196
197fn preview_toggle(app: &Entity<AppState>, on: bool) -> Stateful<Div> {
198 let app = app.clone();
199 toggle_row(
200 "settings-preview",
201 "Show message preview",
202 "Off: only \"New message\"",
203 theme::text_muted(),
204 on,
205 )
206 .on_click(move |_, _, cx| {
207 app.update(cx, |state, cx| {
208 state.save_chat_setting(ChatSettingChange::ShowPreview(!on), cx)
209 })
210 })
211}
212
213fn toggle_element(center: &Entity<NotificationCenter>, toggle: &Toggle, on: bool) -> Stateful<Div> {
214 let center = center.clone();
215 let field = toggle.field;
216 toggle_row(
217 toggle.id,
218 toggle.title,
219 toggle.detail,
220 theme::text_muted(),
221 on,
222 )
223 .on_click(move |_, _, cx| {
224 center.update(cx, |center, cx| {
225 center.update_settings(cx, |settings| {
226 let value = field(settings);
227 *value = !*value;
228 })
229 })
230 })
231}
232
233fn corner_choices(center: &Entity<NotificationCenter>, current: Corner) -> Vec<Choice> {
234 Corner::ALL
235 .into_iter()
236 .map(|corner| {
237 let center = center.clone();
238 Choice::new(corner.label(), corner == current, move |cx| {
239 center.update(cx, |center, cx| {
240 center.update_settings(cx, |settings| settings.corner = corner)
241 })
242 })
243 })
244 .collect()
245}
246
247fn sound_row(
248 center: &Entity<NotificationCenter>,
249 (id, preview_id): (&'static str, &'static str),
250 title: &'static str,
251 options: &'static [AlertSound],
252 current: AlertSound,
253 enabled: bool,
254 field: SoundAccessor,
255) -> Div {
256 let choices = options
257 .iter()
258 .map(|sound| {
259 let (center, sound) = (center.clone(), *sound);
260 Choice::new(sound.label(), sound == current, move |cx| {
261 center.update(cx, |center, cx| {
262 center.update_settings(cx, |settings| *field(settings) = sound);
263 center.preview_sound(sound.file_name(), cx);
264 })
265 })
266 })
267 .collect();
268 let preview_center = center.clone();
269 control_row(
270 title,
271 "",
272 h_flex()
273 .gap(px(8.))
274 .items_center()
275 .child(select(id, current.label(), !enabled, choices))
276 .child(
277 text_button(preview_id, "Preview").on_click(move |_, _, cx| {
278 preview_center.update(cx, |center, cx| {
279 center.preview_sound(current.file_name(), cx)
280 })
281 }),
282 ),
283 )
284}