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