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