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