| 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::i18n::tr; |
| 13 | use crate::notify::NotificationCenter; |
| 14 | use crate::notify::rules::{Corner, Settings}; |
| 15 | use crate::notify::sounds::AlertSound; |
| 16 | use crate::presence_follow::{ |
| 17 | MANAGE_STATUS_NOTIFICATIONS, STATUS_NOTIFICATIONS_DESCRIPTION, TURN_OFF, |
| 18 | }; |
| 19 | use crate::theme; |
| 20 | |
| 21 | type Accessor = fn(&mut Settings) -> &mut bool; |
| 22 | type SoundAccessor = fn(&mut Settings) -> &mut AlertSound; |
| 23 | |
| 24 | struct Toggle { |
| 25 | id: &'static str, |
| 26 | title_key: &'static str, |
| 27 | detail_key: Option<&'static str>, |
| 28 | field: Accessor, |
| 29 | } |
| 30 | |
| 31 | const 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 | |
| 38 | const 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 | |
| 45 | const 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 | |
| 52 | const 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 | |
| 67 | const 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 | |
| 74 | pub 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 | ( |
| 159 | "settings-chat-messages", |
| 160 | tr("settings.notifications.chat_messages"), |
| 161 | "", |
| 162 | ), |
| 163 | &CHAT_MESSAGES, |
| 164 | account.chat_messages, |
| 165 | ChatSettingChange::ChatMessages, |
| 166 | )) |
| 167 | .child(account_select( |
| 168 | app, |
| 169 | ("settings-personal-mentions", "Personal @mentions", ""), |
| 170 | &MENTIONS, |
| 171 | account.personal_mentions, |
| 172 | ChatSettingChange::PersonalMentions, |
| 173 | )) |
| 174 | .child(account_select( |
| 175 | app, |
| 176 | ( |
| 177 | "settings-everyone-mentions", |
| 178 | "Everyone in chat @mentions", |
| 179 | "", |
| 180 | ), |
| 181 | &ACTIVITY_OR_OFF, |
| 182 | account.everyone_mentions, |
| 183 | ChatSettingChange::EveryoneMentions, |
| 184 | )) |
| 185 | .child(account_select( |
| 186 | app, |
| 187 | ( |
| 188 | "settings-likes", |
| 189 | tr("settings.notifications.likes_reactions"), |
| 190 | "", |
| 191 | ), |
| 192 | &ACTIVITY_OR_OFF, |
| 193 | account.likes, |
| 194 | ChatSettingChange::Likes, |
| 195 | )) |
| 196 | .child(caption("MEETINGS AND CALLS")) |
| 197 | .child(account_select( |
| 198 | app, |
| 199 | ( |
| 200 | "settings-meeting-chats", |
| 201 | tr("settings.notifications.meeting_chats"), |
| 202 | meeting_chats_detail(account.meeting_chats), |
| 203 | ), |
| 204 | &MEETING_CHATS, |
| 205 | account.meeting_chats, |
| 206 | ChatSettingChange::MeetingChats, |
| 207 | )) |
| 208 | .child(hide_in_calls) |
| 209 | .child(caption("PEOPLE")) |
| 210 | .child(control_row( |
| 211 | MANAGE_STATUS_NOTIFICATIONS, |
| 212 | STATUS_NOTIFICATIONS_DESCRIPTION, |
| 213 | div(), |
| 214 | )) |
| 215 | .children(followed_rows(app, cx)) |
| 216 | } |
| 217 | |
| 218 | fn followed_rows(app: &Entity<AppState>, cx: &App) -> Vec<Div> { |
| 219 | app.read(cx) |
| 220 | .followed_presence |
| 221 | .iter() |
| 222 | .enumerate() |
| 223 | .map(|(index, person)| { |
| 224 | let (app, user_id, name) = (app.clone(), person.user_id.clone(), person.name.clone()); |
| 225 | control_row( |
| 226 | person.name.clone(), |
| 227 | "", |
| 228 | text_button(("settings-unfollow-presence", index), TURN_OFF).on_click( |
| 229 | move |_, _, cx| { |
| 230 | app.update(cx, |state, cx| { |
| 231 | state.set_presence_followed(&user_id, &name, false, cx) |
| 232 | }) |
| 233 | }, |
| 234 | ), |
| 235 | ) |
| 236 | }) |
| 237 | .collect() |
| 238 | } |
| 239 | |
| 240 | fn preview_toggle(app: &Entity<AppState>, on: bool) -> Stateful<Div> { |
| 241 | let app = app.clone(); |
| 242 | toggle_row( |
| 243 | "settings-preview", |
| 244 | tr("settings.notifications.show_preview"), |
| 245 | "Off: only \"New message\"", |
| 246 | theme::text_muted(), |
| 247 | on, |
| 248 | ) |
| 249 | .on_click(move |_, _, cx| { |
| 250 | app.update(cx, |state, cx| { |
| 251 | state.save_chat_setting(ChatSettingChange::ShowPreview(!on), cx) |
| 252 | }) |
| 253 | }) |
| 254 | } |
| 255 | |
| 256 | fn toggle_element(center: &Entity<NotificationCenter>, toggle: &Toggle, on: bool) -> Stateful<Div> { |
| 257 | let center = center.clone(); |
| 258 | let field = toggle.field; |
| 259 | let detail = toggle.detail_key.map(tr).unwrap_or_default(); |
| 260 | toggle_row( |
| 261 | toggle.id, |
| 262 | tr(toggle.title_key), |
| 263 | detail, |
| 264 | theme::text_muted(), |
| 265 | on, |
| 266 | ) |
| 267 | .on_click(move |_, _, cx| { |
| 268 | center.update(cx, |center, cx| { |
| 269 | center.update_settings(cx, |settings| { |
| 270 | let value = field(settings); |
| 271 | *value = !*value; |
| 272 | }) |
| 273 | }) |
| 274 | }) |
| 275 | } |
| 276 | |
| 277 | fn corner_choices(center: &Entity<NotificationCenter>, current: Corner) -> Vec<Choice> { |
| 278 | Corner::ALL |
| 279 | .into_iter() |
| 280 | .map(|corner| { |
| 281 | let center = center.clone(); |
| 282 | Choice::new(corner.label(), corner == current, move |cx| { |
| 283 | center.update(cx, |center, cx| { |
| 284 | center.update_settings(cx, |settings| settings.corner = corner) |
| 285 | }) |
| 286 | }) |
| 287 | }) |
| 288 | .collect() |
| 289 | } |
| 290 | |
| 291 | fn sound_row( |
| 292 | center: &Entity<NotificationCenter>, |
| 293 | (id, preview_id): (&'static str, &'static str), |
| 294 | title: impl Into<SharedString>, |
| 295 | options: &'static [AlertSound], |
| 296 | current: AlertSound, |
| 297 | enabled: bool, |
| 298 | field: SoundAccessor, |
| 299 | ) -> Div { |
| 300 | let choices = options |
| 301 | .iter() |
| 302 | .map(|sound| { |
| 303 | let (center, sound) = (center.clone(), *sound); |
| 304 | Choice::new(sound.label(), sound == current, move |cx| { |
| 305 | center.update(cx, |center, cx| { |
| 306 | center.update_settings(cx, |settings| *field(settings) = sound); |
| 307 | center.preview_sound(sound.file_name(), cx); |
| 308 | }) |
| 309 | }) |
| 310 | }) |
| 311 | .collect(); |
| 312 | let preview_center = center.clone(); |
| 313 | control_row( |
| 314 | title, |
| 315 | "", |
| 316 | h_flex() |
| 317 | .gap(px(8.)) |
| 318 | .items_center() |
| 319 | .child(select(id, current.label(), !enabled, choices)) |
| 320 | .child( |
| 321 | text_button(preview_id, "Preview").on_click(move |_, _, cx| { |
| 322 | preview_center.update(cx, |center, cx| { |
| 323 | center.preview_sound(current.file_name(), cx) |
| 324 | }) |
| 325 | }), |
| 326 | ), |
| 327 | ) |
| 328 | } |