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