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