| 1 | use std::collections::{HashMap, HashSet}; |
| 2 | use std::time::{Duration, Instant}; |
| 3 | |
| 4 | use chrono::Utc; |
| 5 | use gpui_kit::*; |
| 6 | use store::Sidebar; |
| 7 | |
| 8 | use super::badge::{Badge, badge_for}; |
| 9 | use super::incoming::IncomingTracker; |
| 10 | use super::layout::{self, MEETING_HEIGHT, Slot, ring_height}; |
| 11 | use super::meeting_view::MeetingToastView; |
| 12 | use super::platform::{self, NativeHandle, Tray, TrayCommand, WorkArea}; |
| 13 | use super::ring_view::RingView; |
| 14 | use super::rules::{ |
| 15 | ChatKind, Decision, Environment, Incoming, Preview, Settings, SoundThrottle, decide, in_call, |
| 16 | meeting_start_sound, ringtone_for, should_flash, should_ringback, |
| 17 | }; |
| 18 | use super::settings; |
| 19 | use super::sound_cache::{self, SoundHandle}; |
| 20 | use super::sounds::{CallTone, Ringtone}; |
| 21 | use super::settings_view::{Section, SettingsView}; |
| 22 | use super::stack::{ |
| 23 | AgentToast, FADE_IN, FAST_FADE, ReplyState, SENT_DURATION, SLOW_FADE, ToastModel, ToastStack, |
| 24 | }; |
| 25 | use super::text::describe; |
| 26 | use super::toast::{PillView, ToastView}; |
| 27 | use crate::agents::{AgentRun, AgentRunsEvent, RunState}; |
| 28 | use crate::app_state::{AppEvent, AppState, selection_for}; |
| 29 | use crate::call::DemoScene; |
| 30 | use crate::data::{self, Directory, PresenceKind}; |
| 31 | use crate::runtime; |
| 32 | use crate::single_instance; |
| 33 | |
| 34 | const TICK: Duration = Duration::from_millis(33); |
| 35 | const SLIDE_DURATION: Duration = Duration::from_millis(180); |
| 36 | const SLIDE_DISTANCE: f32 = 48.; |
| 37 | const DEMO_SPEC_DELAY: Duration = Duration::from_secs(3); |
| 38 | const DEMO_RING_ENV: &str = "TEAMS_DEMO_RING"; |
| 39 | const DEMO_RING_KIND_ENV: &str = "TEAMS_DEMO_RING_KIND"; |
| 40 | const DEMO_CALL_ENV: &str = "TEAMS_DEMO_CALL"; |
| 41 | const DEMO_SCENE_DELAY: Duration = Duration::from_millis(1500); |
| 42 | const AGENT_BODY_LINES: usize = 2; |
| 43 | |
| 44 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
| 45 | enum CallToast { |
| 46 | Ring(u64), |
| 47 | Meeting(u64), |
| 48 | } |
| 49 | |
| 50 | struct OpenWindow { |
| 51 | handle: AnyWindowHandle, |
| 52 | native: Option<NativeHandle>, |
| 53 | opened: Instant, |
| 54 | } |
| 55 | |
| 56 | impl OpenWindow { |
| 57 | fn place(&self, native: NativeHandle, slot: Slot, x: i32, cx: &mut App) { |
| 58 | platform::place(native, x, slot.y, slot.width, slot.height); |
| 59 | // WM_SIZE arrives while the app is borrowed, so GPUI drops its own resize report. |
| 60 | self.handle |
| 61 | .update(cx, |_, window, cx| window.bounds_changed(cx)) |
| 62 | .ok(); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | pub struct NotificationCenter { |
| 67 | app: Entity<AppState>, |
| 68 | main_window: AnyWindowHandle, |
| 69 | main_native: Option<NativeHandle>, |
| 70 | settings: Settings, |
| 71 | stack: ToastStack, |
| 72 | tracker: IncomingTracker, |
| 73 | throttle: SoundThrottle, |
| 74 | windows: HashMap<u64, OpenWindow>, |
| 75 | opening: HashSet<u64>, |
| 76 | call_windows: HashMap<CallToast, OpenWindow>, |
| 77 | call_opening: HashSet<CallToast>, |
| 78 | ring_tone: Option<SoundHandle>, |
| 79 | ringback: Option<SoundHandle>, |
| 80 | reconnecting: Option<SoundHandle>, |
| 81 | preview: Option<SoundHandle>, |
| 82 | demo_mode: bool, |
| 83 | pill: Option<OpenWindow>, |
| 84 | pill_opening: bool, |
| 85 | hover_hold: bool, |
| 86 | settings_window: Option<AnyWindowHandle>, |
| 87 | settings_opening: bool, |
| 88 | unread_mentions: HashSet<String>, |
| 89 | tray: Option<Tray>, |
| 90 | shown_badge: Option<Badge>, |
| 91 | animations: bool, |
| 92 | _subscription: Subscription, |
| 93 | _agent_subscription: Option<Subscription>, |
| 94 | } |
| 95 | |
| 96 | impl NotificationCenter { |
| 97 | pub fn new(app: Entity<AppState>, window: &mut Window, cx: &mut Context<Self>) -> Self { |
| 98 | let settings = settings::load(&app.read(cx).store); |
| 99 | let tray = if app.read(cx).mode.demo { |
| 100 | None |
| 101 | } else { |
| 102 | Tray::new(settings.sounds.notification_sound, settings.do_not_disturb) |
| 103 | }; |
| 104 | let subscription = cx.subscribe(&app, |this, _, event: &AppEvent, cx| { |
| 105 | this.on_app_event(event, cx); |
| 106 | }); |
| 107 | let agent_subscription = crate::agents::runs_handle(cx).map(|runs| { |
| 108 | cx.subscribe(&runs, |this, runs, event: &AgentRunsEvent, cx| { |
| 109 | if let AgentRunsEvent::Finished(run_id) = event |
| 110 | && let Some(run) = runs.read(cx).get(*run_id).cloned() |
| 111 | { |
| 112 | this.present_agent_run(&run, cx); |
| 113 | } |
| 114 | }) |
| 115 | }); |
| 116 | let center = NotificationCenter { |
| 117 | main_window: window.window_handle(), |
| 118 | main_native: platform::native_handle(window), |
| 119 | settings, |
| 120 | stack: ToastStack::default(), |
| 121 | tracker: IncomingTracker::new(Utc::now()), |
| 122 | throttle: SoundThrottle::default(), |
| 123 | windows: HashMap::new(), |
| 124 | opening: HashSet::new(), |
| 125 | call_windows: HashMap::new(), |
| 126 | call_opening: HashSet::new(), |
| 127 | ring_tone: None, |
| 128 | ringback: None, |
| 129 | reconnecting: None, |
| 130 | preview: None, |
| 131 | demo_mode: app.read(cx).mode.demo, |
| 132 | pill: None, |
| 133 | pill_opening: false, |
| 134 | hover_hold: false, |
| 135 | settings_window: None, |
| 136 | settings_opening: false, |
| 137 | unread_mentions: HashSet::new(), |
| 138 | tray, |
| 139 | shown_badge: None, |
| 140 | animations: platform::animations_enabled(), |
| 141 | app, |
| 142 | _subscription: subscription, |
| 143 | _agent_subscription: agent_subscription, |
| 144 | }; |
| 145 | cx.spawn(async move |this, cx| { |
| 146 | loop { |
| 147 | cx.background_executor().timer(TICK).await; |
| 148 | if this.update(cx, |center, cx| center.tick(cx)).is_err() { |
| 149 | break; |
| 150 | } |
| 151 | } |
| 152 | }) |
| 153 | .detach(); |
| 154 | if !center.demo_mode { |
| 155 | center.prefetch_sounds(cx); |
| 156 | } |
| 157 | if center.app.read(cx).mode.demo { |
| 158 | if demo_scene().is_none() { |
| 159 | cx.spawn(async move |this, cx| { |
| 160 | cx.background_executor().timer(DEMO_SPEC_DELAY).await; |
| 161 | this.update(cx, |center, cx| center.show_demo_toasts(cx)) |
| 162 | .ok(); |
| 163 | }) |
| 164 | .detach(); |
| 165 | } |
| 166 | if let Some(delay) = demo_ring_delay() { |
| 167 | let app = center.app.clone(); |
| 168 | let video = |
| 169 | std::env::var(DEMO_RING_KIND_ENV).is_ok_and(|kind| kind.trim() == "video"); |
| 170 | cx.spawn(async move |_, cx| { |
| 171 | cx.background_executor().timer(delay).await; |
| 172 | app.update(cx, |state, cx| { |
| 173 | if video { |
| 174 | state.demo_incoming_video_ring(cx) |
| 175 | } else { |
| 176 | state.demo_incoming_ring(cx) |
| 177 | } |
| 178 | }); |
| 179 | }) |
| 180 | .detach(); |
| 181 | } |
| 182 | if let Some(scene) = demo_scene() { |
| 183 | let app = center.app.clone(); |
| 184 | cx.spawn(async move |_, cx| { |
| 185 | cx.background_executor().timer(DEMO_SCENE_DELAY).await; |
| 186 | app.update(cx, |state, cx| state.start_demo_scene(scene, cx)); |
| 187 | }) |
| 188 | .detach(); |
| 189 | } |
| 190 | } |
| 191 | center |
| 192 | } |
| 193 | |
| 194 | pub fn app(&self) -> &Entity<AppState> { |
| 195 | &self.app |
| 196 | } |
| 197 | |
| 198 | pub fn settings(&self) -> &Settings { |
| 199 | &self.settings |
| 200 | } |
| 201 | |
| 202 | pub fn model(&self, id: u64) -> Option<&ToastModel> { |
| 203 | self.stack.get(id) |
| 204 | } |
| 205 | |
| 206 | pub fn queued_count(&self) -> usize { |
| 207 | self.stack.queued_count() |
| 208 | } |
| 209 | |
| 210 | pub fn stack_opacity(&self) -> f32 { |
| 211 | let now = Instant::now(); |
| 212 | self.stack |
| 213 | .visible() |
| 214 | .iter() |
| 215 | .map(|toast| toast.opacity(now)) |
| 216 | .fold(0., f32::max) |
| 217 | } |
| 218 | |
| 219 | fn animated(&self, duration: Duration) -> Duration { |
| 220 | if self.animations { |
| 221 | duration |
| 222 | } else { |
| 223 | Duration::ZERO |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | pub fn directory<'a>(&self, cx: &'a App) -> &'a Directory { |
| 228 | &self.app.read(cx).directory |
| 229 | } |
| 230 | |
| 231 | pub fn reply_text(&self, id: u64) -> Option<String> { |
| 232 | self.stack.get(id).map(|toast| toast.reply_text.clone()) |
| 233 | } |
| 234 | |
| 235 | pub fn intercept_close(&self, _cx: &App) -> bool { |
| 236 | match (self.settings.close_to_tray, &self.tray, self.main_native) { |
| 237 | (true, Some(_), Some(native)) => { |
| 238 | platform::hide_window(native); |
| 239 | true |
| 240 | } |
| 241 | _ => false, |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | pub fn update_settings(&mut self, cx: &mut Context<Self>, change: impl FnOnce(&mut Settings)) { |
| 246 | let previous = self.settings.sounds.clone(); |
| 247 | change(&mut self.settings); |
| 248 | settings::save(&self.app.read(cx).store, &self.settings); |
| 249 | self.sync_tray(); |
| 250 | if self.settings.sounds != previous { |
| 251 | let updated = self.settings.sounds.clone(); |
| 252 | self.app |
| 253 | .update(cx, |state, cx| state.save_sound_settings(previous, updated, cx)); |
| 254 | self.prefetch_sounds(cx); |
| 255 | } |
| 256 | cx.notify(); |
| 257 | } |
| 258 | |
| 259 | fn on_account_sounds(&mut self, cx: &mut Context<Self>) { |
| 260 | let Some(account) = self.app.read(cx).account_sounds.clone() else { |
| 261 | return; |
| 262 | }; |
| 263 | if account == self.settings.sounds { |
| 264 | return; |
| 265 | } |
| 266 | self.settings.sounds = account; |
| 267 | settings::save(&self.app.read(cx).store, &self.settings); |
| 268 | self.sync_tray(); |
| 269 | if !self.demo_mode { |
| 270 | self.prefetch_sounds(cx); |
| 271 | } |
| 272 | cx.notify(); |
| 273 | } |
| 274 | |
| 275 | fn prefetch_sounds(&self, cx: &App) { |
| 276 | let sounds = &self.settings.sounds; |
| 277 | let chosen = [ |
| 278 | sounds.notification_sound_type.file_name(), |
| 279 | sounds.urgent_sound_type.file_name(), |
| 280 | sounds.ringtones.regular.file_name(), |
| 281 | Ringtone::Beep.file_name(), |
| 282 | ]; |
| 283 | let call_tones = CallTone::ALL.map(CallTone::file_name); |
| 284 | sound_cache::prefetch(&chosen, cx); |
| 285 | sound_cache::prefetch(&call_tones, cx); |
| 286 | } |
| 287 | |
| 288 | pub fn preview_sound(&mut self, file_name: &'static str, cx: &mut Context<Self>) { |
| 289 | self.preview = Some(sound_cache::start(file_name, false, cx)); |
| 290 | } |
| 291 | |
| 292 | fn play_notification_sound(&self, cx: &App) { |
| 293 | let file_name = self.settings.sounds.notification_sound_type.file_name(); |
| 294 | sound_cache::play_or(file_name, platform::play_sound, cx); |
| 295 | } |
| 296 | |
| 297 | fn on_app_event(&mut self, event: &AppEvent, cx: &mut Context<Self>) { |
| 298 | match event { |
| 299 | AppEvent::Messages(conversation_id) => self.on_messages(conversation_id, cx), |
| 300 | AppEvent::Selection => self.on_selection(cx), |
| 301 | AppEvent::Sidebar => self.refresh_badge(cx), |
| 302 | AppEvent::Ring => self.on_rings_changed(cx), |
| 303 | AppEvent::MeetingReminder => self.on_meeting_reminder(cx), |
| 304 | AppEvent::MeetingChime => self.on_meeting_chime(cx), |
| 305 | AppEvent::CallTone(tone) => self.on_call_tone(*tone, cx), |
| 306 | AppEvent::Call => self.sync_call_sounds(cx), |
| 307 | AppEvent::SoundSettings => self.on_account_sounds(cx), |
| 308 | _ => {} |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | fn on_selection(&mut self, cx: &mut Context<Self>) { |
| 313 | let selected = self |
| 314 | .app |
| 315 | .read(cx) |
| 316 | .selection |
| 317 | .as_ref() |
| 318 | .map(|selection| selection.conversation_id().to_owned()); |
| 319 | if let Some(conversation_id) = selected { |
| 320 | self.unread_mentions.remove(&conversation_id); |
| 321 | self.stack.remove_conversation(&conversation_id); |
| 322 | } |
| 323 | self.refresh_badge(cx); |
| 324 | } |
| 325 | |
| 326 | fn on_messages(&mut self, conversation_id: &str, cx: &mut Context<Self>) { |
| 327 | let state = self.app.read(cx); |
| 328 | let my_user_id = state.directory.me.as_ref().map(|me| me.user_id.clone()); |
| 329 | let found = self.tracker.collect( |
| 330 | &state.store, |
| 331 | &state.sidebar, |
| 332 | my_user_id.as_deref(), |
| 333 | &state.late_arrivals, |
| 334 | conversation_id, |
| 335 | ); |
| 336 | for incoming in found { |
| 337 | self.handle_incoming(incoming, cx); |
| 338 | } |
| 339 | self.refresh_badge(cx); |
| 340 | } |
| 341 | |
| 342 | fn environment(&self, conversation_id: &str, cx: &mut Context<Self>) -> Environment { |
| 343 | let state = self.app.read(cx); |
| 344 | let selected = state |
| 345 | .selection |
| 346 | .as_ref() |
| 347 | .is_some_and(|selection| selection.conversation_id() == conversation_id); |
| 348 | let own_do_not_disturb = own_do_not_disturb(state); |
| 349 | let own_busy = own_presence(state) == PresenceKind::Busy; |
| 350 | let server_in_call = match (state.engine.as_ref(), state.directory.me.as_ref()) { |
| 351 | (Some(engine), Some(me)) => data::in_call(engine, &me.user_id), |
| 352 | _ => false, |
| 353 | }; |
| 354 | let in_call = in_call(server_in_call, state.call.as_ref().map(|call| &call.model)); |
| 355 | Environment { |
| 356 | chat_in_foreground: selected && self.main_window_active(cx), |
| 357 | system_quiet: platform::system_quiet(), |
| 358 | own_do_not_disturb, |
| 359 | own_busy, |
| 360 | in_call, |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | fn main_window_active(&self, cx: &mut Context<Self>) -> bool { |
| 365 | self.main_window |
| 366 | .update(cx, |_, window, _| window.is_window_active()) |
| 367 | .unwrap_or(false) |
| 368 | } |
| 369 | |
| 370 | pub fn handle_incoming(&mut self, incoming: Incoming, cx: &mut Context<Self>) { |
| 371 | let bubble_text = match &incoming.preview { |
| 372 | Preview::Text(text) => text.replace('\n', " "), |
| 373 | Preview::Image => "Sent a picture".to_owned(), |
| 374 | }; |
| 375 | self.app.update(cx, |state, cx| { |
| 376 | state.note_call_chat_message( |
| 377 | &incoming.conversation_id, |
| 378 | &incoming.message_id, |
| 379 | incoming.sender_id.clone(), |
| 380 | &incoming.sender_name, |
| 381 | &bubble_text, |
| 382 | cx, |
| 383 | ) |
| 384 | }); |
| 385 | let environment = self.environment(&incoming.conversation_id, cx); |
| 386 | if !environment.chat_in_foreground && incoming.mentions_me { |
| 387 | self.unread_mentions |
| 388 | .insert(incoming.conversation_id.clone()); |
| 389 | } |
| 390 | let decision = decide(&self.settings, &incoming, environment); |
| 391 | self.present(&incoming, decision, cx); |
| 392 | if should_flash(&self.settings, decision.toast, self.main_window_active(cx)) |
| 393 | && let Some(native) = self.main_native |
| 394 | { |
| 395 | platform::flash(native); |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | fn present(&mut self, incoming: &Incoming, decision: Decision, cx: &mut Context<Self>) { |
| 400 | if !decision.toast { |
| 401 | return; |
| 402 | } |
| 403 | let now = Instant::now(); |
| 404 | let outcome = self.stack.push(incoming, now); |
| 405 | if let Some(model) = self.stack.get_mut(outcome.id) { |
| 406 | model.time = chrono::Local::now().format("%H:%M").to_string(); |
| 407 | } |
| 408 | let throttle_allows = self.throttle.allow(&incoming.conversation_id, now); |
| 409 | if decision.sound && outcome.plays_sound(throttle_allows) && !self.app.read(cx).mode.demo { |
| 410 | self.play_notification_sound(cx); |
| 411 | } |
| 412 | if let Some(user_id) = incoming.sender_id.clone() { |
| 413 | self.app |
| 414 | .update(cx, |state, cx| state.request_avatars(vec![user_id], cx)); |
| 415 | } |
| 416 | self.sync_windows(cx); |
| 417 | } |
| 418 | |
| 419 | pub fn present_agent_run(&mut self, run: &AgentRun, cx: &mut Context<Self>) { |
| 420 | let failed = matches!(run.state, RunState::Failed { .. }); |
| 421 | let verb = if failed { "failed" } else { "finished" }; |
| 422 | let body = if failed { |
| 423 | run.current_step.clone() |
| 424 | } else { |
| 425 | first_lines(&run.final_text, AGENT_BODY_LINES) |
| 426 | }; |
| 427 | let toast = AgentToast { |
| 428 | run_id: run.id, |
| 429 | session_id: run.session_id.clone(), |
| 430 | header: format!( |
| 431 | "{} - {} - {}", |
| 432 | run.action_label, |
| 433 | run.agent_name, |
| 434 | run.elapsed_text() |
| 435 | ), |
| 436 | title: format!( |
| 437 | "{} {verb}: {} for {}", |
| 438 | run.agent_name, run.action_label, run.sender_name |
| 439 | ), |
| 440 | body: if body.is_empty() { |
| 441 | "Done".to_owned() |
| 442 | } else { |
| 443 | body |
| 444 | }, |
| 445 | failed, |
| 446 | }; |
| 447 | let now = Instant::now(); |
| 448 | let id = self |
| 449 | .stack |
| 450 | .push_agent(toast, &run.conversation_id, &run.message_id, now); |
| 451 | if let Some(model) = self.stack.get_mut(id) { |
| 452 | model.time = chrono::Local::now().format("%H:%M").to_string(); |
| 453 | } |
| 454 | let quiet = self.settings.do_not_disturb || self.app.read(cx).mode.demo; |
| 455 | if self.settings.sounds.notification_sound && !quiet { |
| 456 | self.play_notification_sound(cx); |
| 457 | } |
| 458 | self.sync_windows(cx); |
| 459 | } |
| 460 | |
| 461 | pub fn agent_toast_follow_up(&mut self, id: u64, cx: &mut Context<Self>) { |
| 462 | let Some(agent) = self.stack.get(id).and_then(|toast| toast.agent.clone()) else { |
| 463 | return; |
| 464 | }; |
| 465 | self.drop_toast(id, cx); |
| 466 | if agent.failed { |
| 467 | crate::agents::retry_run(agent.run_id, cx); |
| 468 | } else { |
| 469 | crate::agents::open_session(&agent.session_id, cx); |
| 470 | } |
| 471 | } |
| 472 | |
| 473 | fn visible_ids(&self) -> Vec<u64> { |
| 474 | self.stack.visible().iter().map(|toast| toast.id).collect() |
| 475 | } |
| 476 | |
| 477 | fn work_area(&self, cx: &App) -> WorkArea { |
| 478 | if let Some(area) = self.main_native.and_then(platform::work_area) { |
| 479 | return area; |
| 480 | } |
| 481 | let bounds = cx |
| 482 | .primary_display() |
| 483 | .map(|display| display.bounds()) |
| 484 | .unwrap_or_else(|| Bounds::new(point(px(0.), px(0.)), size(px(1280.), px(720.)))); |
| 485 | WorkArea { |
| 486 | left: f32::from(bounds.origin.x) as i32, |
| 487 | top: f32::from(bounds.origin.y) as i32, |
| 488 | right: f32::from(bounds.origin.x + bounds.size.width) as i32, |
| 489 | bottom: f32::from(bounds.origin.y + bounds.size.height) as i32, |
| 490 | scale: 1., |
| 491 | } |
| 492 | } |
| 493 | |
| 494 | fn call_toasts(&self, cx: &App) -> Vec<(CallToast, f32)> { |
| 495 | let state = self.app.read(cx); |
| 496 | let in_call = state.call.is_some(); |
| 497 | let rings = state |
| 498 | .rings |
| 499 | .ringing() |
| 500 | .map(|entry| (CallToast::Ring(entry.ring.ring_id), ring_height(in_call))); |
| 501 | let meetings = state |
| 502 | .meeting_toasts |
| 503 | .entries() |
| 504 | .iter() |
| 505 | .map(|toast| (CallToast::Meeting(toast.id), MEETING_HEIGHT)); |
| 506 | rings.chain(meetings).collect() |
| 507 | } |
| 508 | |
| 509 | fn sync_windows(&mut self, cx: &mut Context<Self>) { |
| 510 | let visible = self.visible_ids(); |
| 511 | let stale: Vec<u64> = self |
| 512 | .windows |
| 513 | .keys() |
| 514 | .filter(|id| !visible.contains(id)) |
| 515 | .copied() |
| 516 | .collect(); |
| 517 | for id in stale { |
| 518 | self.close_window(id, cx); |
| 519 | } |
| 520 | let call_toasts = self.call_toasts(cx); |
| 521 | let stale_call_toasts: Vec<CallToast> = self |
| 522 | .call_windows |
| 523 | .keys() |
| 524 | .filter(|key| !call_toasts.iter().any(|(toast, _)| toast == *key)) |
| 525 | .copied() |
| 526 | .collect(); |
| 527 | for key in stale_call_toasts { |
| 528 | self.close_call_window(key, cx); |
| 529 | } |
| 530 | let area = self.work_area(cx); |
| 531 | let mut heights: Vec<f32> = self |
| 532 | .stack |
| 533 | .visible() |
| 534 | .iter() |
| 535 | .map(|toast| { |
| 536 | let lines = match &toast.agent { |
| 537 | Some(agent) => wrapped_lines(&agent.body, layout::AGENT_BODY_WIDTH, cx), |
| 538 | None => preview_lines(&describe(toast, self.settings.preview).preview, cx), |
| 539 | }; |
| 540 | layout::toast_height(toast, lines) |
| 541 | }) |
| 542 | .collect(); |
| 543 | heights.extend(call_toasts.iter().map(|(_, height)| *height)); |
| 544 | let with_hide_all = visible.len() > 1 || self.stack.queued_count() > 0; |
| 545 | let slots = layout::stack_slots(area, self.settings.corner, &heights, with_hide_all); |
| 546 | for (id, slot) in visible.iter().zip(slots.toasts.iter()) { |
| 547 | self.place_toast(*id, *slot, area, cx); |
| 548 | } |
| 549 | for ((key, _), slot) in call_toasts |
| 550 | .iter() |
| 551 | .zip(slots.toasts.iter().skip(visible.len())) |
| 552 | { |
| 553 | self.place_call_toast(*key, *slot, area, cx); |
| 554 | } |
| 555 | self.sync_pill(slots.pill, area, cx); |
| 556 | } |
| 557 | |
| 558 | fn place_call_toast( |
| 559 | &mut self, |
| 560 | key: CallToast, |
| 561 | slot: Slot, |
| 562 | area: WorkArea, |
| 563 | cx: &mut Context<Self>, |
| 564 | ) { |
| 565 | let Some(window) = self.call_windows.get(&key) else { |
| 566 | self.open_call_window(key, slot, area, cx); |
| 567 | return; |
| 568 | }; |
| 569 | if let Some(native) = window.native { |
| 570 | let offset = self.slide_offset(window.opened, area); |
| 571 | window.place(native, slot, slot.x + offset, cx); |
| 572 | } |
| 573 | } |
| 574 | |
| 575 | fn open_call_window( |
| 576 | &mut self, |
| 577 | key: CallToast, |
| 578 | slot: Slot, |
| 579 | area: WorkArea, |
| 580 | cx: &mut Context<Self>, |
| 581 | ) { |
| 582 | if !self.call_opening.insert(key) { |
| 583 | return; |
| 584 | } |
| 585 | open_popup( |
| 586 | popup_options(slot, area), |
| 587 | cx, |
| 588 | move |center, _, cx| match key { |
| 589 | CallToast::Ring(ring_id) => { |
| 590 | AnyView::from(cx.new(|cx| RingView::new(center, ring_id, cx))) |
| 591 | } |
| 592 | CallToast::Meeting(toast_id) => { |
| 593 | AnyView::from(cx.new(|cx| MeetingToastView::new(center, toast_id, cx))) |
| 594 | } |
| 595 | }, |
| 596 | move |this, window| { |
| 597 | this.call_opening.remove(&key); |
| 598 | if let Some(window) = window { |
| 599 | this.call_windows.insert(key, window); |
| 600 | } |
| 601 | }, |
| 602 | ); |
| 603 | } |
| 604 | |
| 605 | fn close_call_window(&mut self, key: CallToast, cx: &mut Context<Self>) { |
| 606 | let closed = self.call_windows.get(&key).is_some_and(|window| { |
| 607 | window |
| 608 | .handle |
| 609 | .update(cx, |_, window, _| window.remove_window()) |
| 610 | .is_ok() |
| 611 | }); |
| 612 | if closed { |
| 613 | self.call_windows.remove(&key); |
| 614 | } |
| 615 | } |
| 616 | |
| 617 | fn on_meeting_reminder(&mut self, cx: &mut Context<Self>) { |
| 618 | if !self.app.read(cx).meeting_toasts.is_empty() |
| 619 | && !self.main_window_active(cx) |
| 620 | && let Some(native) = self.main_native |
| 621 | { |
| 622 | platform::flash(native); |
| 623 | } |
| 624 | self.sync_windows(cx); |
| 625 | cx.notify(); |
| 626 | } |
| 627 | |
| 628 | fn on_meeting_chime(&mut self, cx: &mut Context<Self>) { |
| 629 | let busy = self.environment("", cx).busy(); |
| 630 | if meeting_start_sound(&self.settings, busy) && !self.reminders_quiet(cx) && !self.demo_mode |
| 631 | { |
| 632 | sound_cache::play(CallTone::MeetingReminder.file_name(), cx); |
| 633 | } |
| 634 | } |
| 635 | |
| 636 | fn on_call_tone(&mut self, tone: CallTone, cx: &mut Context<Self>) { |
| 637 | if !self.demo_mode { |
| 638 | sound_cache::play(tone.file_name(), cx); |
| 639 | } |
| 640 | } |
| 641 | |
| 642 | pub fn join_meeting_toast(&mut self, id: u64, cx: &mut Context<Self>) { |
| 643 | self.raise_main_window(cx); |
| 644 | self.app |
| 645 | .update(cx, |state, cx| state.join_meeting_toast(id, cx)); |
| 646 | } |
| 647 | |
| 648 | fn reminders_quiet(&self, cx: &App) -> bool { |
| 649 | self.settings.do_not_disturb |
| 650 | || platform::system_quiet() |
| 651 | || own_do_not_disturb(self.app.read(cx)) |
| 652 | } |
| 653 | |
| 654 | fn on_rings_changed(&mut self, cx: &mut Context<Self>) { |
| 655 | let ringing = self.app.read(cx).rings.is_ringing(); |
| 656 | if ringing |
| 657 | && !self.main_window_active(cx) |
| 658 | && let Some(native) = self.main_native |
| 659 | { |
| 660 | platform::flash(native); |
| 661 | } |
| 662 | self.sync_windows(cx); |
| 663 | self.sync_ring_tone(ringing, cx); |
| 664 | cx.notify(); |
| 665 | } |
| 666 | |
| 667 | fn sync_ring_tone(&mut self, ringing: bool, cx: &mut Context<Self>) { |
| 668 | if !ringing || self.demo_mode { |
| 669 | self.ring_tone = None; |
| 670 | return; |
| 671 | } |
| 672 | if self.ring_tone.is_some() { |
| 673 | return; |
| 674 | } |
| 675 | let environment = self.environment("", cx); |
| 676 | let native_call = self.app.read(cx).call.is_some(); |
| 677 | if let Some(ringtone) = ringtone_for(&self.settings, native_call, environment.busy()) { |
| 678 | self.ring_tone = Some(sound_cache::start(ringtone.file_name(), true, cx)); |
| 679 | } |
| 680 | } |
| 681 | |
| 682 | fn sync_call_sounds(&mut self, cx: &Context<Self>) { |
| 683 | let model = self.app.read(cx).call.as_ref().map(|call| &call.model); |
| 684 | let ringback = should_ringback(model) && !self.demo_mode; |
| 685 | let reconnecting = |
| 686 | model.is_some_and(|model| model.is_reconnecting() && !model.lobby) && !self.demo_mode; |
| 687 | sync_loop(&mut self.ringback, ringback, CallTone::OutgoingRinging, cx); |
| 688 | sync_loop(&mut self.reconnecting, reconnecting, CallTone::Reconnecting, cx); |
| 689 | } |
| 690 | |
| 691 | fn slide_offset(&self, opened: Instant, area: WorkArea) -> i32 { |
| 692 | if !self.animations { |
| 693 | return 0; |
| 694 | } |
| 695 | let progress = (opened.elapsed().as_secs_f32() / SLIDE_DURATION.as_secs_f32()).min(1.); |
| 696 | let eased = 1. - (1. - progress).powi(3); |
| 697 | let distance = ((1. - eased) * SLIDE_DISTANCE * area.scale) as i32; |
| 698 | if self.settings.corner.is_right() { |
| 699 | distance |
| 700 | } else { |
| 701 | -distance |
| 702 | } |
| 703 | } |
| 704 | |
| 705 | fn place_toast(&mut self, id: u64, slot: Slot, area: WorkArea, cx: &mut Context<Self>) { |
| 706 | if !self.windows.contains_key(&id) { |
| 707 | self.open_toast_window(id, slot, area, cx); |
| 708 | return; |
| 709 | } |
| 710 | let offset = self |
| 711 | .windows |
| 712 | .get(&id) |
| 713 | .map(|window| self.slide_offset(window.opened, area)) |
| 714 | .unwrap_or(0); |
| 715 | if let Some(window) = self.windows.get(&id) |
| 716 | && let Some(native) = window.native |
| 717 | { |
| 718 | window.place(native, slot, slot.x + offset, cx); |
| 719 | } |
| 720 | } |
| 721 | |
| 722 | fn open_toast_window(&mut self, id: u64, slot: Slot, area: WorkArea, cx: &mut Context<Self>) { |
| 723 | if !self.opening.insert(id) { |
| 724 | return; |
| 725 | } |
| 726 | open_popup( |
| 727 | popup_options(slot, area), |
| 728 | cx, |
| 729 | move |center, window, cx| cx.new(|cx| ToastView::new(center, id, window, cx)), |
| 730 | move |this, window| { |
| 731 | this.opening.remove(&id); |
| 732 | if let Some(window) = window { |
| 733 | this.windows.insert(id, window); |
| 734 | } |
| 735 | }, |
| 736 | ); |
| 737 | } |
| 738 | |
| 739 | fn sync_pill(&mut self, slot: Option<Slot>, area: WorkArea, cx: &mut Context<Self>) { |
| 740 | let Some(slot) = slot else { |
| 741 | if let Some(pill) = self.pill.take() { |
| 742 | pill.handle |
| 743 | .update(cx, |_, window, _| window.remove_window()) |
| 744 | .ok(); |
| 745 | } |
| 746 | return; |
| 747 | }; |
| 748 | if self.pill.is_none() && !self.pill_opening { |
| 749 | self.pill_opening = true; |
| 750 | open_popup( |
| 751 | popup_options(slot, area), |
| 752 | cx, |
| 753 | |center, _, cx| cx.new(|cx| PillView::new(center, cx)), |
| 754 | |this, window| { |
| 755 | this.pill_opening = false; |
| 756 | this.pill = window; |
| 757 | }, |
| 758 | ); |
| 759 | } |
| 760 | if let Some(pill) = self.pill.as_ref() |
| 761 | && let Some(native) = pill.native |
| 762 | { |
| 763 | pill.place(native, slot, slot.x, cx); |
| 764 | } |
| 765 | } |
| 766 | |
| 767 | fn close_window(&mut self, id: u64, cx: &mut Context<Self>) { |
| 768 | let closed = self.windows.get(&id).is_some_and(|window| { |
| 769 | window |
| 770 | .handle |
| 771 | .update(cx, |_, window, _| window.remove_window()) |
| 772 | .is_ok() |
| 773 | }); |
| 774 | if closed { |
| 775 | self.windows.remove(&id); |
| 776 | } |
| 777 | } |
| 778 | |
| 779 | fn drop_toast(&mut self, id: u64, cx: &mut Context<Self>) { |
| 780 | let fast = self.animated(FAST_FADE); |
| 781 | self.stack.dismiss(id, Instant::now(), fast); |
| 782 | cx.notify(); |
| 783 | } |
| 784 | |
| 785 | pub fn hide_all(&mut self, cx: &mut Context<Self>) { |
| 786 | for id in self.stack.ids() { |
| 787 | self.drop_toast(id, cx); |
| 788 | } |
| 789 | } |
| 790 | |
| 791 | fn sync_hover_hold(&mut self, now: Instant) { |
| 792 | let any_hovered = self.stack.any_hovered(); |
| 793 | if !any_hovered && !self.hover_hold { |
| 794 | return; |
| 795 | } |
| 796 | if any_hovered { |
| 797 | let fast = self.animated(FAST_FADE); |
| 798 | self.stack.restore(now, fast); |
| 799 | } |
| 800 | for id in self.stack.ids() { |
| 801 | if let Some(toast) = self.stack.get_mut(id) |
| 802 | && !toast.queued |
| 803 | { |
| 804 | if any_hovered { |
| 805 | toast.timer.pause(now); |
| 806 | } else if !toast.holds_open() { |
| 807 | toast.timer.resume(now); |
| 808 | } |
| 809 | } |
| 810 | } |
| 811 | self.hover_hold = any_hovered; |
| 812 | } |
| 813 | |
| 814 | fn is_dismissed(&self, id: u64) -> bool { |
| 815 | self.stack.get(id).is_none_or(|toast| toast.dismissed) |
| 816 | } |
| 817 | |
| 818 | fn tick(&mut self, cx: &mut Context<Self>) { |
| 819 | let now = Instant::now(); |
| 820 | let (fade_in, slow) = (self.animated(FADE_IN), self.animated(SLOW_FADE)); |
| 821 | for id in self.stack.expired(now) { |
| 822 | self.stack.fade_out(id, now, slow); |
| 823 | } |
| 824 | for id in self.stack.gone(now) { |
| 825 | self.stack.remove(id); |
| 826 | } |
| 827 | self.stack.promote(now, fade_in); |
| 828 | self.sync_hover_hold(now); |
| 829 | if !self.app.read(cx).rings.is_empty() { |
| 830 | self.app.update(cx, |state, cx| state.tick_rings(cx)); |
| 831 | } |
| 832 | if self |
| 833 | .app |
| 834 | .read(cx) |
| 835 | .meeting_toasts |
| 836 | .is_due_for_check(Utc::now()) |
| 837 | { |
| 838 | let show = !self.reminders_quiet(cx); |
| 839 | self.app |
| 840 | .update(cx, |state, cx| state.tick_meeting_reminders(show, cx)); |
| 841 | } |
| 842 | if !self.stack.is_empty() |
| 843 | || !self.windows.is_empty() |
| 844 | || self.pill.is_some() |
| 845 | || !self.call_windows.is_empty() |
| 846 | { |
| 847 | self.sync_windows(cx); |
| 848 | cx.notify(); |
| 849 | } |
| 850 | self.poll_tray(cx); |
| 851 | if single_instance::take_activation() { |
| 852 | self.raise_main_window(cx); |
| 853 | } |
| 854 | } |
| 855 | |
| 856 | fn poll_tray(&mut self, cx: &mut Context<Self>) { |
| 857 | let commands = self.tray.as_ref().map(Tray::poll).unwrap_or_default(); |
| 858 | for command in commands { |
| 859 | match command { |
| 860 | TrayCommand::Toggle => self.toggle_main_window(cx), |
| 861 | TrayCommand::Open => self.raise_main_window(cx), |
| 862 | TrayCommand::ToggleSound => self.update_settings(cx, |settings| { |
| 863 | settings.sounds.notification_sound = !settings.sounds.notification_sound |
| 864 | }), |
| 865 | TrayCommand::ToggleDoNotDisturb => self.update_settings(cx, |settings| { |
| 866 | settings.do_not_disturb = !settings.do_not_disturb |
| 867 | }), |
| 868 | TrayCommand::Settings => self.open_settings(cx), |
| 869 | TrayCommand::Quit => cx.quit(), |
| 870 | } |
| 871 | } |
| 872 | } |
| 873 | |
| 874 | fn toggle_main_window(&mut self, cx: &mut Context<Self>) { |
| 875 | if self.main_window_active(cx) { |
| 876 | if let Some(native) = self.main_native { |
| 877 | platform::hide_window(native); |
| 878 | } |
| 879 | } else { |
| 880 | self.raise_main_window(cx); |
| 881 | } |
| 882 | } |
| 883 | |
| 884 | fn raise_main_window(&mut self, cx: &mut Context<Self>) { |
| 885 | if let Some(native) = self.main_native { |
| 886 | platform::restore_and_raise(native); |
| 887 | } |
| 888 | self.main_window |
| 889 | .update(cx, |_, window, _| window.activate_window()) |
| 890 | .ok(); |
| 891 | } |
| 892 | |
| 893 | fn sync_tray(&self) { |
| 894 | if let Some(tray) = &self.tray { |
| 895 | tray.sync( |
| 896 | self.settings.sounds.notification_sound, |
| 897 | self.settings.do_not_disturb, |
| 898 | !self.unread_mentions.is_empty(), |
| 899 | ); |
| 900 | } |
| 901 | } |
| 902 | |
| 903 | fn refresh_badge(&mut self, cx: &mut Context<Self>) { |
| 904 | let (count, still_unread) = |
| 905 | unread_summary(&self.app.read(cx).sidebar, &self.unread_mentions); |
| 906 | self.unread_mentions |
| 907 | .retain(|conversation_id| still_unread.contains(conversation_id)); |
| 908 | let badge = badge_for(count); |
| 909 | if badge == self.shown_badge { |
| 910 | return; |
| 911 | } |
| 912 | if let Some(native) = self.main_native { |
| 913 | platform::set_badge(native, badge.as_ref()); |
| 914 | if badge.is_none() && self.shown_badge.is_some() { |
| 915 | platform::stop_flash(native); |
| 916 | } |
| 917 | } |
| 918 | self.shown_badge = badge; |
| 919 | self.sync_tray(); |
| 920 | } |
| 921 | |
| 922 | pub fn set_hover(&mut self, id: u64, hovered: bool, cx: &mut Context<Self>) { |
| 923 | let now = Instant::now(); |
| 924 | let Some(toast) = self.stack.get_mut(id) else { |
| 925 | return; |
| 926 | }; |
| 927 | if toast.hovered == hovered { |
| 928 | return; |
| 929 | } |
| 930 | toast.hovered = hovered; |
| 931 | self.sync_hover_hold(now); |
| 932 | cx.notify(); |
| 933 | } |
| 934 | |
| 935 | pub fn dismiss(&mut self, id: u64, cx: &mut Context<Self>) { |
| 936 | self.drop_toast(id, cx); |
| 937 | } |
| 938 | |
| 939 | pub fn open_reply(&mut self, id: u64, cx: &mut Context<Self>) -> Option<String> { |
| 940 | let now = Instant::now(); |
| 941 | let toast = self.stack.get_mut(id)?; |
| 942 | toast.reply = ReplyState::Open; |
| 943 | toast.timer.pause(now); |
| 944 | let draft = toast.reply_text.clone(); |
| 945 | cx.notify(); |
| 946 | Some(draft) |
| 947 | } |
| 948 | |
| 949 | pub fn close_reply(&mut self, id: u64, cx: &mut Context<Self>) { |
| 950 | if let Some(toast) = self.stack.get_mut(id) { |
| 951 | toast.reply = ReplyState::Closed; |
| 952 | toast.reply_text.clear(); |
| 953 | toast.hovered = false; |
| 954 | } |
| 955 | cx.notify(); |
| 956 | } |
| 957 | |
| 958 | pub fn submit_reply(&mut self, id: u64, text: String, cx: &mut Context<Self>) { |
| 959 | let Some(toast) = self.stack.get_mut(id) else { |
| 960 | return; |
| 961 | }; |
| 962 | toast.reply_text = text.clone(); |
| 963 | let conversation_id = toast.conversation_id.clone(); |
| 964 | let message_id = toast.message_id.clone(); |
| 965 | let is_channel = matches!(toast.kind, ChatKind::Channel { .. }); |
| 966 | let state = self.app.read(cx); |
| 967 | let (mode, engine) = (state.mode, state.engine.clone()); |
| 968 | let outcome: Result<(), ()> = if mode.demo { |
| 969 | Ok(()) |
| 970 | } else if mode.read_only { |
| 971 | Err(()) |
| 972 | } else if let Some(engine) = engine { |
| 973 | let receiver = runtime::spawn(async move { |
| 974 | if is_channel { |
| 975 | engine.reply_to(&conversation_id, &message_id, &text).await |
| 976 | } else { |
| 977 | engine.send_message(&conversation_id, &text, None).await |
| 978 | } |
| 979 | .map(|_| ()) |
| 980 | }); |
| 981 | cx.spawn(async move |this, cx| { |
| 982 | let sent = matches!(receiver.await, Ok(Ok(()))); |
| 983 | this.update(cx, |center, cx| center.finish_reply(id, sent, cx)) |
| 984 | .ok(); |
| 985 | }) |
| 986 | .detach(); |
| 987 | return; |
| 988 | } else { |
| 989 | Err(()) |
| 990 | }; |
| 991 | self.finish_reply(id, outcome.is_ok(), cx); |
| 992 | } |
| 993 | |
| 994 | fn finish_reply(&mut self, id: u64, sent: bool, cx: &mut Context<Self>) { |
| 995 | let Some(toast) = self.stack.get_mut(id) else { |
| 996 | return; |
| 997 | }; |
| 998 | toast.reply = if sent { |
| 999 | ReplyState::Sent |
| 1000 | } else { |
| 1001 | ReplyState::Failed |
| 1002 | }; |
| 1003 | let conversation_id = toast.conversation_id.clone(); |
| 1004 | cx.notify(); |
| 1005 | if sent { |
| 1006 | cx.spawn(async move |this, cx| { |
| 1007 | cx.background_executor().timer(SENT_DURATION).await; |
| 1008 | this.update(cx, |center, cx| { |
| 1009 | center.mark_conversation_read(&conversation_id, cx); |
| 1010 | center.drop_toast(id, cx); |
| 1011 | }) |
| 1012 | .ok(); |
| 1013 | }) |
| 1014 | .detach(); |
| 1015 | } |
| 1016 | } |
| 1017 | |
| 1018 | fn mark_conversation_read(&mut self, conversation_id: &str, cx: &mut Context<Self>) { |
| 1019 | self.unread_mentions.remove(conversation_id); |
| 1020 | let conversation_id = conversation_id.to_owned(); |
| 1021 | self.app |
| 1022 | .update(cx, |state, cx| state.mark_chat_read(&conversation_id, cx)); |
| 1023 | self.refresh_badge(cx); |
| 1024 | } |
| 1025 | |
| 1026 | pub fn mark_read(&mut self, id: u64, cx: &mut Context<Self>) { |
| 1027 | if self.is_dismissed(id) { |
| 1028 | return; |
| 1029 | } |
| 1030 | let Some(conversation_id) = self |
| 1031 | .stack |
| 1032 | .get(id) |
| 1033 | .map(|toast| toast.conversation_id.clone()) |
| 1034 | else { |
| 1035 | return; |
| 1036 | }; |
| 1037 | self.mark_conversation_read(&conversation_id, cx); |
| 1038 | self.drop_toast(id, cx); |
| 1039 | } |
| 1040 | |
| 1041 | pub fn activate(&mut self, id: u64, cx: &mut Context<Self>) { |
| 1042 | if self.is_dismissed(id) { |
| 1043 | return; |
| 1044 | } |
| 1045 | let Some((conversation_id, message_id)) = self |
| 1046 | .stack |
| 1047 | .get(id) |
| 1048 | .map(|toast| (toast.conversation_id.clone(), toast.message_id.clone())) |
| 1049 | else { |
| 1050 | return; |
| 1051 | }; |
| 1052 | self.drop_toast(id, cx); |
| 1053 | self.raise_main_window(cx); |
| 1054 | let selection = selection_for(&self.app.read(cx).sidebar, &conversation_id); |
| 1055 | if let Some(selection) = selection { |
| 1056 | self.app.update(cx, |state, cx| { |
| 1057 | state.jump_to_message(selection, message_id, cx) |
| 1058 | }); |
| 1059 | } |
| 1060 | } |
| 1061 | |
| 1062 | pub fn open_settings(&mut self, cx: &mut Context<Self>) { |
| 1063 | self.show_settings(Section::Notifications, false, cx); |
| 1064 | } |
| 1065 | |
| 1066 | pub fn open_agent_settings(&mut self, cx: &mut Context<Self>) { |
| 1067 | self.show_settings(Section::Agents, true, cx); |
| 1068 | } |
| 1069 | |
| 1070 | fn show_settings( |
| 1071 | &mut self, |
| 1072 | section: Section, |
| 1073 | switch_open_window: bool, |
| 1074 | cx: &mut Context<Self>, |
| 1075 | ) { |
| 1076 | if let Some(handle) = self.settings_window { |
| 1077 | let shown = handle.update(cx, |view, window, cx| { |
| 1078 | window.activate_window(); |
| 1079 | if switch_open_window && let Ok(view) = view.downcast::<SettingsView>() { |
| 1080 | view.update(cx, |view, cx| view.select_section(section, cx)); |
| 1081 | } |
| 1082 | }); |
| 1083 | if shown.is_ok() { |
| 1084 | return; |
| 1085 | } |
| 1086 | self.settings_window = None; |
| 1087 | } |
| 1088 | if self.settings_opening { |
| 1089 | return; |
| 1090 | } |
| 1091 | self.settings_opening = true; |
| 1092 | let center = cx.entity(); |
| 1093 | let options = WindowOptions { |
| 1094 | window_bounds: Some(WindowBounds::Windowed(Bounds::centered( |
| 1095 | None, |
| 1096 | size(px(SettingsView::WIDTH), px(SettingsView::HEIGHT)), |
| 1097 | cx, |
| 1098 | ))), |
| 1099 | titlebar: Some(TitlebarOptions { |
| 1100 | title: Some("Settings".into()), |
| 1101 | ..Default::default() |
| 1102 | }), |
| 1103 | is_resizable: false, |
| 1104 | is_minimizable: false, |
| 1105 | ..Default::default() |
| 1106 | }; |
| 1107 | // open_window draws once right away, and SettingsView reads this entity while rendering. |
| 1108 | cx.defer(move |cx| { |
| 1109 | let view_center = center.clone(); |
| 1110 | let opened = gpui_kit::open_window(options, cx, move |window, cx| { |
| 1111 | cx.new(|cx| SettingsView::new(view_center, section, window, cx)) |
| 1112 | }); |
| 1113 | center.update(cx, |this, _| { |
| 1114 | this.settings_opening = false; |
| 1115 | this.settings_window = opened.ok().map(|(handle, _)| handle); |
| 1116 | }); |
| 1117 | }); |
| 1118 | } |
| 1119 | |
| 1120 | pub fn send_test_notification(&mut self, cx: &mut Context<Self>) { |
| 1121 | let incoming = Incoming { |
| 1122 | conversation_id: "test-notification".to_owned(), |
| 1123 | message_id: "test".to_owned(), |
| 1124 | kind: ChatKind::Direct, |
| 1125 | chat_title: "Mara Lindqvist".to_owned(), |
| 1126 | sender_id: None, |
| 1127 | sender_name: "Mara Lindqvist".to_owned(), |
| 1128 | preview: Preview::Text("This is a test notification.".to_owned()), |
| 1129 | mentions_me: false, |
| 1130 | muted: false, |
| 1131 | signals: Default::default(), |
| 1132 | created_at: Utc::now(), |
| 1133 | }; |
| 1134 | let decision = Decision { |
| 1135 | toast: true, |
| 1136 | sound: self.settings.sounds.notification_sound, |
| 1137 | }; |
| 1138 | self.throttle = SoundThrottle::default(); |
| 1139 | self.present(&incoming, decision, cx); |
| 1140 | } |
| 1141 | |
| 1142 | fn show_demo_toasts(&mut self, cx: &mut Context<Self>) { |
| 1143 | for incoming in demo_incoming() { |
| 1144 | let decision = Decision { |
| 1145 | toast: true, |
| 1146 | sound: false, |
| 1147 | }; |
| 1148 | self.present(&incoming, decision, cx); |
| 1149 | } |
| 1150 | } |
| 1151 | } |
| 1152 | |
| 1153 | /// Opens a window outside the current update: `open_window` draws right away, and every |
| 1154 | /// notification view reads the center while rendering. |
| 1155 | fn open_popup<V: Into<AnyView>>( |
| 1156 | options: WindowOptions, |
| 1157 | cx: &mut Context<NotificationCenter>, |
| 1158 | build: impl FnOnce(Entity<NotificationCenter>, &mut Window, &mut App) -> V + 'static, |
| 1159 | opened: impl FnOnce(&mut NotificationCenter, Option<OpenWindow>) + 'static, |
| 1160 | ) { |
| 1161 | let center = cx.entity(); |
| 1162 | cx.defer(move |cx| { |
| 1163 | let view_center = center.clone(); |
| 1164 | let Ok(handle) = cx.open_window(options, move |window, cx| { |
| 1165 | let view = build(view_center, window, cx); |
| 1166 | cx.new(|cx| base::Root::new(view, window, cx).bg(transparent_black())) |
| 1167 | }) else { |
| 1168 | center.update(cx, |this, _| opened(this, None)); |
| 1169 | return; |
| 1170 | }; |
| 1171 | let handle = AnyWindowHandle::from(handle); |
| 1172 | let native = handle |
| 1173 | .update(cx, |_, window, _| platform::native_handle(window)) |
| 1174 | .ok() |
| 1175 | .flatten(); |
| 1176 | if let Some(native) = native { |
| 1177 | platform::prepare_toast_window(native); |
| 1178 | } |
| 1179 | let window = OpenWindow { |
| 1180 | handle, |
| 1181 | native, |
| 1182 | opened: Instant::now(), |
| 1183 | }; |
| 1184 | center.update(cx, |this, _| opened(this, Some(window))); |
| 1185 | }); |
| 1186 | } |
| 1187 | |
| 1188 | fn own_presence(state: &AppState) -> PresenceKind { |
| 1189 | state |
| 1190 | .directory |
| 1191 | .me |
| 1192 | .as_ref() |
| 1193 | .map_or(PresenceKind::Unknown, |me| { |
| 1194 | state.directory.presence_of(&me.user_id).kind() |
| 1195 | }) |
| 1196 | } |
| 1197 | |
| 1198 | fn own_do_not_disturb(state: &AppState) -> bool { |
| 1199 | own_presence(state) == PresenceKind::DoNotDisturb |
| 1200 | } |
| 1201 | |
| 1202 | fn sync_loop(handle: &mut Option<SoundHandle>, audible: bool, tone: CallTone, cx: &App) { |
| 1203 | match (audible, handle.is_some()) { |
| 1204 | (true, false) => *handle = Some(sound_cache::start(tone.file_name(), true, cx)), |
| 1205 | (false, true) => *handle = None, |
| 1206 | _ => {} |
| 1207 | } |
| 1208 | } |
| 1209 | |
| 1210 | fn demo_scene() -> Option<DemoScene> { |
| 1211 | DemoScene::from_name(&std::env::var(DEMO_CALL_ENV).ok()?) |
| 1212 | } |
| 1213 | |
| 1214 | /// `TEAMS_DEMO_RING=<seconds>` rings a fake incoming call that long after the demo starts. |
| 1215 | fn demo_ring_delay() -> Option<Duration> { |
| 1216 | let seconds: f32 = std::env::var(DEMO_RING_ENV).ok()?.trim().parse().ok()?; |
| 1217 | (seconds >= 0.).then(|| Duration::from_secs_f32(seconds)) |
| 1218 | } |
| 1219 | |
| 1220 | fn first_lines(text: &str, count: usize) -> String { |
| 1221 | text.lines() |
| 1222 | .map(str::trim) |
| 1223 | .filter(|line| !line.is_empty()) |
| 1224 | .take(count) |
| 1225 | .collect::<Vec<_>>() |
| 1226 | .join("\n") |
| 1227 | } |
| 1228 | |
| 1229 | fn preview_lines(text: &str, cx: &App) -> usize { |
| 1230 | wrapped_lines(text, layout::PREVIEW_WIDTH, cx) |
| 1231 | } |
| 1232 | |
| 1233 | fn wrapped_lines(text: &str, width: f32, cx: &App) -> usize { |
| 1234 | let mut wrapper = cx |
| 1235 | .text_system() |
| 1236 | .line_wrapper(font(crate::theme::font_family()), px(layout::PREVIEW_SIZE)); |
| 1237 | text.split('\n') |
| 1238 | .map(|line| { |
| 1239 | let fragments = [LineFragment::text(line)]; |
| 1240 | wrapper |
| 1241 | .wrap_line(&fragments, px(width), IndentAdjustment::NoIndent) |
| 1242 | .count() |
| 1243 | + 1 |
| 1244 | }) |
| 1245 | .sum() |
| 1246 | } |
| 1247 | |
| 1248 | fn popup_options(slot: Slot, area: WorkArea) -> WindowOptions { |
| 1249 | let scale = area.scale; |
| 1250 | let bounds = Bounds::new( |
| 1251 | point(px(slot.x as f32 / scale), px(slot.y as f32 / scale)), |
| 1252 | size( |
| 1253 | px(slot.width as f32 / scale), |
| 1254 | px(slot.height as f32 / scale), |
| 1255 | ), |
| 1256 | ); |
| 1257 | WindowOptions { |
| 1258 | window_bounds: Some(WindowBounds::Windowed(bounds)), |
| 1259 | titlebar: None, |
| 1260 | focus: false, |
| 1261 | show: true, |
| 1262 | kind: WindowKind::PopUp, |
| 1263 | is_movable: false, |
| 1264 | is_resizable: false, |
| 1265 | is_minimizable: false, |
| 1266 | window_background: WindowBackgroundAppearance::Transparent, |
| 1267 | ..Default::default() |
| 1268 | } |
| 1269 | } |
| 1270 | |
| 1271 | pub fn unread_summary( |
| 1272 | sidebar: &Sidebar, |
| 1273 | unread_mentions: &HashSet<String>, |
| 1274 | ) -> (usize, HashSet<String>) { |
| 1275 | let chats = sidebar.chats.iter().filter(|chat| chat.unread); |
| 1276 | let channels = sidebar |
| 1277 | .teams |
| 1278 | .iter() |
| 1279 | .flat_map(|team| team.channels.iter()) |
| 1280 | .filter(|channel| channel.unread); |
| 1281 | let silenced_chat_ids: HashSet<&str> = sidebar |
| 1282 | .chats |
| 1283 | .iter() |
| 1284 | .filter(|chat| chat.muted && !unread_mentions.contains(&chat.id)) |
| 1285 | .map(|chat| chat.id.as_str()) |
| 1286 | .collect(); |
| 1287 | let ids: HashSet<String> = chats |
| 1288 | .map(|chat| chat.id.clone()) |
| 1289 | .chain(channels.map(|channel| channel.id.clone())) |
| 1290 | .collect(); |
| 1291 | let counted = ids |
| 1292 | .iter() |
| 1293 | .filter(|id| !silenced_chat_ids.contains(id.as_str())) |
| 1294 | .count(); |
| 1295 | (counted, ids) |
| 1296 | } |
| 1297 | |
| 1298 | fn demo_incoming() -> Vec<Incoming> { |
| 1299 | let item = |conversation: &str, |
| 1300 | kind: ChatKind, |
| 1301 | title: &str, |
| 1302 | sender: &str, |
| 1303 | text: &str, |
| 1304 | mention: bool| Incoming { |
| 1305 | conversation_id: conversation.to_owned(), |
| 1306 | message_id: format!("{conversation}-message"), |
| 1307 | kind, |
| 1308 | chat_title: title.to_owned(), |
| 1309 | sender_id: None, |
| 1310 | sender_name: sender.to_owned(), |
| 1311 | preview: Preview::Text(text.to_owned()), |
| 1312 | mentions_me: mention, |
| 1313 | muted: false, |
| 1314 | signals: Default::default(), |
| 1315 | created_at: Utc::now(), |
| 1316 | }; |
| 1317 | vec![ |
| 1318 | item( |
| 1319 | "demo-toast-direct", |
| 1320 | ChatKind::Direct, |
| 1321 | "Tobias Klein", |
| 1322 | "Tobias Klein", |
| 1323 | "Sounds good, I will look at it tomorrow.", |
| 1324 | false, |
| 1325 | ), |
| 1326 | item( |
| 1327 | "demo-toast-group", |
| 1328 | ChatKind::Group { member_count: 5 }, |
| 1329 | "Retro-Team", |
| 1330 | "Priya Nair", |
| 1331 | "Please add topics by 5 pm.", |
| 1332 | false, |
| 1333 | ), |
| 1334 | item( |
| 1335 | "demo-toast-mention", |
| 1336 | ChatKind::Channel { |
| 1337 | team: "Platform".to_owned(), |
| 1338 | channel: "Releases".to_owned(), |
| 1339 | }, |
| 1340 | "Releases", |
| 1341 | "Mara Lindqvist", |
| 1342 | "@Jonas can you approve the merge? The pipeline is waiting.", |
| 1343 | true, |
| 1344 | ), |
| 1345 | item( |
| 1346 | "demo-toast-lea", |
| 1347 | ChatKind::Direct, |
| 1348 | "Lea Schneider", |
| 1349 | "Lea Schneider", |
| 1350 | "Lunch at 12?", |
| 1351 | false, |
| 1352 | ), |
| 1353 | item( |
| 1354 | "demo-toast-jonas", |
| 1355 | ChatKind::Direct, |
| 1356 | "Jonas Ortega", |
| 1357 | "Jonas Ortega", |
| 1358 | "Deploy window moved to Thursday 6 pm.", |
| 1359 | false, |
| 1360 | ), |
| 1361 | ] |
| 1362 | } |
| 1363 | |
| 1364 | #[cfg(test)] |
| 1365 | mod tests { |
| 1366 | use store::{ChannelRecord, ChatRecord, SidebarTeam, TeamRecord}; |
| 1367 | |
| 1368 | use std::collections::HashSet; |
| 1369 | |
| 1370 | use super::{preview_lines, unread_summary}; |
| 1371 | use store::Sidebar; |
| 1372 | |
| 1373 | fn sidebar() -> Sidebar { |
| 1374 | Sidebar { |
| 1375 | chats: vec![ |
| 1376 | ChatRecord { |
| 1377 | id: "a".into(), |
| 1378 | unread: true, |
| 1379 | ..Default::default() |
| 1380 | }, |
| 1381 | ChatRecord { |
| 1382 | id: "b".into(), |
| 1383 | unread: false, |
| 1384 | ..Default::default() |
| 1385 | }, |
| 1386 | ], |
| 1387 | teams: vec![SidebarTeam { |
| 1388 | team: TeamRecord { |
| 1389 | id: "t".into(), |
| 1390 | name: "Team".into(), |
| 1391 | }, |
| 1392 | channels: vec![ChannelRecord { |
| 1393 | id: "ch".into(), |
| 1394 | team_id: "t".into(), |
| 1395 | name: "General".into(), |
| 1396 | membership_type: None, |
| 1397 | last_message_at: None, |
| 1398 | unread: true, |
| 1399 | }], |
| 1400 | hidden: false, |
| 1401 | hidden_channel_ids: Vec::new(), |
| 1402 | notifications: Default::default(), |
| 1403 | }], |
| 1404 | } |
| 1405 | } |
| 1406 | |
| 1407 | #[test] |
| 1408 | fn unread_summary_counts_chats_and_channels() { |
| 1409 | let (count, ids) = unread_summary(&sidebar(), &HashSet::new()); |
| 1410 | assert_eq!(count, 2); |
| 1411 | assert!(ids.contains("a") && ids.contains("ch")); |
| 1412 | } |
| 1413 | |
| 1414 | #[test] |
| 1415 | fn muted_chats_only_count_with_an_unread_mention() { |
| 1416 | let mut quiet = sidebar(); |
| 1417 | quiet.chats[0].muted = true; |
| 1418 | let (count, ids) = unread_summary(&quiet, &HashSet::new()); |
| 1419 | assert_eq!(count, 1); |
| 1420 | assert!(ids.contains("a")); |
| 1421 | let (count, _) = unread_summary(&quiet, &HashSet::from(["a".to_owned()])); |
| 1422 | assert_eq!(count, 2); |
| 1423 | } |
| 1424 | |
| 1425 | #[gpui_kit::test] |
| 1426 | fn preview_lines_follow_the_wrapped_text(cx: &mut gpui_kit::TestAppContext) { |
| 1427 | cx.update(|cx| { |
| 1428 | assert_eq!(preview_lines("ok", cx), 1); |
| 1429 | assert_eq!(preview_lines("a\nb", cx), 2); |
| 1430 | let long = "Cristina: ok, thanks for the info! I think not now ".repeat(3); |
| 1431 | assert!(preview_lines(&long, cx) >= 2); |
| 1432 | }); |
| 1433 | } |
| 1434 | } |