| 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, Slot}; |
| 11 | use super::platform::{self, NativeHandle, Tray, TrayCommand, WorkArea}; |
| 12 | use super::rules::{ |
| 13 | ChatKind, Decision, Environment, Incoming, Preview, Settings, SoundThrottle, decide, |
| 14 | should_flash, |
| 15 | }; |
| 16 | use super::settings; |
| 17 | use super::stack::{ReplyState, SENT_DURATION, ToastModel, ToastStack}; |
| 18 | use super::toast::{PillView, ToastView}; |
| 19 | use super::settings_view::SettingsView; |
| 20 | use crate::app_state::{AppEvent, AppState, Selection}; |
| 21 | use crate::data::{Directory, PresenceKind}; |
| 22 | use crate::runtime; |
| 23 | |
| 24 | const TICK: Duration = Duration::from_millis(33); |
| 25 | const SLIDE_DURATION: Duration = Duration::from_millis(180); |
| 26 | const SLIDE_DISTANCE: f32 = 48.; |
| 27 | const DEMO_SPEC_DELAY: Duration = Duration::from_secs(3); |
| 28 | const FALLBACK_BORDER: u32 = 0x3f3f42; |
| 29 | const MENTION_BORDER: u32 = 0xce6a3b; |
| 30 | |
| 31 | struct OpenWindow { |
| 32 | handle: AnyWindowHandle, |
| 33 | native: Option<NativeHandle>, |
| 34 | opened: Instant, |
| 35 | mention: bool, |
| 36 | } |
| 37 | |
| 38 | pub struct NotificationCenter { |
| 39 | app: Entity<AppState>, |
| 40 | main_window: AnyWindowHandle, |
| 41 | main_native: Option<NativeHandle>, |
| 42 | settings: Settings, |
| 43 | stack: ToastStack, |
| 44 | tracker: IncomingTracker, |
| 45 | throttle: SoundThrottle, |
| 46 | windows: HashMap<u64, OpenWindow>, |
| 47 | pill: Option<OpenWindow>, |
| 48 | settings_window: Option<AnyWindowHandle>, |
| 49 | unread_mentions: HashSet<String>, |
| 50 | tray: Option<Tray>, |
| 51 | shown_badge: Option<Badge>, |
| 52 | animations: bool, |
| 53 | _subscription: Subscription, |
| 54 | } |
| 55 | |
| 56 | impl NotificationCenter { |
| 57 | pub fn new( |
| 58 | app: Entity<AppState>, |
| 59 | window: &mut Window, |
| 60 | cx: &mut Context<Self>, |
| 61 | ) -> Self { |
| 62 | let settings = settings::load(&app.read(cx).store); |
| 63 | let tray = if app.read(cx).mode.demo { |
| 64 | None |
| 65 | } else { |
| 66 | Tray::new(settings.sound, settings.do_not_disturb) |
| 67 | }; |
| 68 | let subscription = cx.subscribe(&app, |this, _, event: &AppEvent, cx| { |
| 69 | this.on_app_event(event, cx); |
| 70 | }); |
| 71 | let center = NotificationCenter { |
| 72 | main_window: window.window_handle(), |
| 73 | main_native: platform::native_handle(window), |
| 74 | settings, |
| 75 | stack: ToastStack::default(), |
| 76 | tracker: IncomingTracker::new(Utc::now()), |
| 77 | throttle: SoundThrottle::default(), |
| 78 | windows: HashMap::new(), |
| 79 | pill: None, |
| 80 | settings_window: None, |
| 81 | unread_mentions: HashSet::new(), |
| 82 | tray, |
| 83 | shown_badge: None, |
| 84 | animations: platform::animations_enabled(), |
| 85 | app, |
| 86 | _subscription: subscription, |
| 87 | }; |
| 88 | cx.spawn(async move |this, cx| { |
| 89 | loop { |
| 90 | cx.background_executor().timer(TICK).await; |
| 91 | if this.update(cx, |center, cx| center.tick(cx)).is_err() { |
| 92 | break; |
| 93 | } |
| 94 | } |
| 95 | }) |
| 96 | .detach(); |
| 97 | if center.app.read(cx).mode.demo { |
| 98 | cx.spawn(async move |this, cx| { |
| 99 | cx.background_executor().timer(DEMO_SPEC_DELAY).await; |
| 100 | this.update(cx, |center, cx| center.show_demo_toasts(cx)).ok(); |
| 101 | }) |
| 102 | .detach(); |
| 103 | } |
| 104 | center |
| 105 | } |
| 106 | |
| 107 | pub fn settings(&self) -> &Settings { |
| 108 | &self.settings |
| 109 | } |
| 110 | |
| 111 | pub fn model(&self, id: u64) -> Option<&ToastModel> { |
| 112 | self.stack.get(id) |
| 113 | } |
| 114 | |
| 115 | pub fn hidden_count(&self) -> usize { |
| 116 | self.stack.hidden_count() |
| 117 | } |
| 118 | |
| 119 | pub fn directory<'a>(&self, cx: &'a App) -> &'a Directory { |
| 120 | &self.app.read(cx).directory |
| 121 | } |
| 122 | |
| 123 | pub fn reply_text(&self, id: u64) -> Option<String> { |
| 124 | self.stack.get(id).map(|toast| toast.reply_text.clone()) |
| 125 | } |
| 126 | |
| 127 | pub fn intercept_close(&self, _cx: &App) -> bool { |
| 128 | match (self.settings.close_to_tray, &self.tray, self.main_native) { |
| 129 | (true, Some(_), Some(native)) => { |
| 130 | platform::hide_window(native); |
| 131 | true |
| 132 | } |
| 133 | _ => false, |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | pub fn update_settings(&mut self, cx: &mut Context<Self>, change: impl FnOnce(&mut Settings)) { |
| 138 | change(&mut self.settings); |
| 139 | settings::save(&self.app.read(cx).store, &self.settings); |
| 140 | self.sync_tray(); |
| 141 | cx.notify(); |
| 142 | } |
| 143 | |
| 144 | fn on_app_event(&mut self, event: &AppEvent, cx: &mut Context<Self>) { |
| 145 | match event { |
| 146 | AppEvent::Messages(conversation_id) => self.on_messages(conversation_id, cx), |
| 147 | AppEvent::Selection => self.on_selection(cx), |
| 148 | AppEvent::Sidebar => self.refresh_badge(cx), |
| 149 | _ => {} |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | fn on_selection(&mut self, cx: &mut Context<Self>) { |
| 154 | let selected = self |
| 155 | .app |
| 156 | .read(cx) |
| 157 | .selection |
| 158 | .as_ref() |
| 159 | .map(|selection| selection.conversation_id().to_owned()); |
| 160 | if let Some(conversation_id) = selected { |
| 161 | self.unread_mentions.remove(&conversation_id); |
| 162 | self.stack.remove_conversation(&conversation_id); |
| 163 | } |
| 164 | self.refresh_badge(cx); |
| 165 | } |
| 166 | |
| 167 | fn on_messages(&mut self, conversation_id: &str, cx: &mut Context<Self>) { |
| 168 | let state = self.app.read(cx); |
| 169 | let my_user_id = state.directory.me.as_ref().map(|me| me.user_id.clone()); |
| 170 | let found = self.tracker.collect( |
| 171 | &state.store, |
| 172 | &state.sidebar, |
| 173 | my_user_id.as_deref(), |
| 174 | conversation_id, |
| 175 | ); |
| 176 | for incoming in found { |
| 177 | self.handle_incoming(incoming, cx); |
| 178 | } |
| 179 | self.refresh_badge(cx); |
| 180 | } |
| 181 | |
| 182 | fn environment(&self, conversation_id: &str, cx: &mut Context<Self>) -> Environment { |
| 183 | let state = self.app.read(cx); |
| 184 | let selected = state |
| 185 | .selection |
| 186 | .as_ref() |
| 187 | .is_some_and(|selection| selection.conversation_id() == conversation_id); |
| 188 | let own_do_not_disturb = state.directory.me.as_ref().is_some_and(|me| { |
| 189 | state.directory.presence_of(&me.user_id) == PresenceKind::DoNotDisturb |
| 190 | }); |
| 191 | Environment { |
| 192 | chat_in_foreground: selected && self.main_window_active(cx), |
| 193 | system_quiet: platform::system_quiet(), |
| 194 | own_do_not_disturb, |
| 195 | in_call: false, |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | fn main_window_active(&self, cx: &mut Context<Self>) -> bool { |
| 200 | self.main_window |
| 201 | .update(cx, |_, window, _| window.is_window_active()) |
| 202 | .unwrap_or(false) |
| 203 | } |
| 204 | |
| 205 | pub fn handle_incoming(&mut self, incoming: Incoming, cx: &mut Context<Self>) { |
| 206 | let environment = self.environment(&incoming.conversation_id, cx); |
| 207 | if !environment.chat_in_foreground && incoming.mentions_me { |
| 208 | self.unread_mentions |
| 209 | .insert(incoming.conversation_id.clone()); |
| 210 | } |
| 211 | let decision = decide(&self.settings, &incoming, environment); |
| 212 | self.present(&incoming, decision, cx); |
| 213 | if should_flash( |
| 214 | &self.settings, |
| 215 | incoming.mentions_me && decision.toast, |
| 216 | self.main_window_active(cx), |
| 217 | ) && let Some(native) = self.main_native |
| 218 | { |
| 219 | platform::flash(native); |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | fn present(&mut self, incoming: &Incoming, decision: Decision, cx: &mut Context<Self>) { |
| 224 | if !decision.toast { |
| 225 | return; |
| 226 | } |
| 227 | let now = Instant::now(); |
| 228 | let outcome = self.stack.push(incoming, now); |
| 229 | if let Some(model) = self.stack.get_mut(outcome.id) { |
| 230 | model.time = chrono::Local::now().format("%H:%M").to_string(); |
| 231 | } |
| 232 | let throttle_allows = self.throttle.allow(&incoming.conversation_id, now); |
| 233 | if decision.sound && outcome.plays_sound(throttle_allows) && !self.app.read(cx).mode.demo { |
| 234 | platform::play_sound(); |
| 235 | } |
| 236 | if let Some(user_id) = incoming.sender_id.clone() { |
| 237 | self.app |
| 238 | .update(cx, |state, cx| state.request_avatars(vec![user_id], cx)); |
| 239 | } |
| 240 | self.sync_windows(cx); |
| 241 | } |
| 242 | |
| 243 | fn visible_ids(&self) -> Vec<u64> { |
| 244 | self.stack.visible().iter().map(|toast| toast.id).collect() |
| 245 | } |
| 246 | |
| 247 | fn work_area(&self, cx: &App) -> WorkArea { |
| 248 | if let Some(area) = self.main_native.and_then(platform::work_area) { |
| 249 | return area; |
| 250 | } |
| 251 | let bounds = cx |
| 252 | .primary_display() |
| 253 | .map(|display| display.bounds()) |
| 254 | .unwrap_or_else(|| Bounds::new(point(px(0.), px(0.)), size(px(1280.), px(720.)))); |
| 255 | WorkArea { |
| 256 | left: f32::from(bounds.origin.x) as i32, |
| 257 | top: f32::from(bounds.origin.y) as i32, |
| 258 | right: f32::from(bounds.origin.x + bounds.size.width) as i32, |
| 259 | bottom: f32::from(bounds.origin.y + bounds.size.height) as i32, |
| 260 | scale: 1., |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | fn sync_windows(&mut self, cx: &mut Context<Self>) { |
| 265 | let visible = self.visible_ids(); |
| 266 | let stale: Vec<u64> = self |
| 267 | .windows |
| 268 | .keys() |
| 269 | .filter(|id| !visible.contains(id)) |
| 270 | .copied() |
| 271 | .collect(); |
| 272 | for id in stale { |
| 273 | self.close_window(id, cx); |
| 274 | } |
| 275 | let area = self.work_area(cx); |
| 276 | let heights: Vec<f32> = self |
| 277 | .stack |
| 278 | .visible() |
| 279 | .iter() |
| 280 | .map(|toast| layout::toast_height(toast, toast.hovered, self.settings.preview)) |
| 281 | .collect(); |
| 282 | let hidden = self.stack.hidden_count(); |
| 283 | let slots = layout::stack_slots(area, self.settings.corner, &heights, hidden > 0); |
| 284 | for (id, slot) in visible.iter().zip(slots.toasts.iter()) { |
| 285 | self.place_toast(*id, *slot, area, cx); |
| 286 | } |
| 287 | self.sync_pill(slots.pill, area, hidden, cx); |
| 288 | } |
| 289 | |
| 290 | fn slide_offset(&self, opened: Instant, area: WorkArea) -> i32 { |
| 291 | if !self.animations { |
| 292 | return 0; |
| 293 | } |
| 294 | let progress = (opened.elapsed().as_secs_f32() / SLIDE_DURATION.as_secs_f32()).min(1.); |
| 295 | let eased = 1. - (1. - progress).powi(3); |
| 296 | let distance = ((1. - eased) * SLIDE_DISTANCE * area.scale) as i32; |
| 297 | if self.settings.corner.is_right() { |
| 298 | distance |
| 299 | } else { |
| 300 | -distance |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | fn place_toast(&mut self, id: u64, slot: Slot, area: WorkArea, cx: &mut Context<Self>) { |
| 305 | let mention = self.stack.get(id).is_some_and(|toast| toast.mentions_me); |
| 306 | if !self.windows.contains_key(&id) { |
| 307 | let Some(window) = self.open_toast_window(id, slot, area, cx) else { |
| 308 | return; |
| 309 | }; |
| 310 | self.windows.insert(id, window); |
| 311 | } |
| 312 | let offset = self |
| 313 | .windows |
| 314 | .get(&id) |
| 315 | .map(|window| self.slide_offset(window.opened, area)) |
| 316 | .unwrap_or(0); |
| 317 | if let Some(window) = self.windows.get_mut(&id) |
| 318 | && let Some(native) = window.native |
| 319 | { |
| 320 | platform::place(native, slot.x + offset, slot.y, slot.width, slot.height); |
| 321 | if window.mention != mention { |
| 322 | window.mention = mention; |
| 323 | platform::set_border_color(native, border_color(mention)); |
| 324 | } |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | fn open_toast_window( |
| 329 | &mut self, |
| 330 | id: u64, |
| 331 | slot: Slot, |
| 332 | area: WorkArea, |
| 333 | cx: &mut Context<Self>, |
| 334 | ) -> Option<OpenWindow> { |
| 335 | let mention = self.stack.get(id).is_some_and(|toast| toast.mentions_me); |
| 336 | let options = popup_options(slot, area); |
| 337 | let center = cx.entity(); |
| 338 | let (handle, _view) = gpui_kit::open_window(options, cx, move |window, cx| { |
| 339 | cx.new(|cx| ToastView::new(center, id, window, cx)) |
| 340 | }) |
| 341 | .ok()?; |
| 342 | let native = handle |
| 343 | .update(cx, |_, window, _| platform::native_handle(window)) |
| 344 | .ok() |
| 345 | .flatten(); |
| 346 | if let Some(native) = native { |
| 347 | platform::prepare_toast_window(native); |
| 348 | platform::set_border_color(native, border_color(mention)); |
| 349 | } |
| 350 | Some(OpenWindow { |
| 351 | handle, |
| 352 | native, |
| 353 | opened: Instant::now(), |
| 354 | mention, |
| 355 | }) |
| 356 | } |
| 357 | |
| 358 | fn sync_pill( |
| 359 | &mut self, |
| 360 | slot: Option<Slot>, |
| 361 | area: WorkArea, |
| 362 | hidden: usize, |
| 363 | cx: &mut Context<Self>, |
| 364 | ) { |
| 365 | let Some(slot) = slot.filter(|_| hidden > 0) else { |
| 366 | if let Some(pill) = self.pill.take() { |
| 367 | pill.handle.update(cx, |_, window, _| window.remove_window()).ok(); |
| 368 | } |
| 369 | return; |
| 370 | }; |
| 371 | if self.pill.is_none() { |
| 372 | let options = popup_options(slot, area); |
| 373 | let center = cx.entity(); |
| 374 | let opened = gpui_kit::open_window(options, cx, move |_, cx| { |
| 375 | cx.new(|cx| PillView::new(center, cx)) |
| 376 | }); |
| 377 | if let Ok((handle, _)) = opened { |
| 378 | let native = handle |
| 379 | .update(cx, |_, window, _| platform::native_handle(window)) |
| 380 | .ok() |
| 381 | .flatten(); |
| 382 | if let Some(native) = native { |
| 383 | platform::prepare_toast_window(native); |
| 384 | platform::set_border_color(native, border_color(false)); |
| 385 | } |
| 386 | self.pill = Some(OpenWindow { |
| 387 | handle, |
| 388 | native, |
| 389 | opened: Instant::now(), |
| 390 | mention: false, |
| 391 | }); |
| 392 | } |
| 393 | } |
| 394 | if let Some(native) = self.pill.as_ref().and_then(|pill| pill.native) { |
| 395 | platform::place(native, slot.x, slot.y, slot.width, slot.height); |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | fn close_window(&mut self, id: u64, cx: &mut Context<Self>) { |
| 400 | let closed = self.windows.get(&id).is_some_and(|window| { |
| 401 | window |
| 402 | .handle |
| 403 | .update(cx, |_, window, _| window.remove_window()) |
| 404 | .is_ok() |
| 405 | }); |
| 406 | if closed { |
| 407 | self.windows.remove(&id); |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | fn drop_toast(&mut self, id: u64, cx: &mut Context<Self>) { |
| 412 | self.stack.remove(id); |
| 413 | cx.notify(); |
| 414 | } |
| 415 | |
| 416 | fn tick(&mut self, cx: &mut Context<Self>) { |
| 417 | let now = Instant::now(); |
| 418 | for id in self.stack.expired(now) { |
| 419 | self.stack.remove(id); |
| 420 | } |
| 421 | if !self.stack.is_empty() || !self.windows.is_empty() || self.pill.is_some() { |
| 422 | self.sync_windows(cx); |
| 423 | cx.notify(); |
| 424 | } |
| 425 | self.poll_tray(cx); |
| 426 | } |
| 427 | |
| 428 | fn poll_tray(&mut self, cx: &mut Context<Self>) { |
| 429 | let commands = self.tray.as_ref().map(Tray::poll).unwrap_or_default(); |
| 430 | for command in commands { |
| 431 | match command { |
| 432 | TrayCommand::Toggle => self.toggle_main_window(cx), |
| 433 | TrayCommand::Open => self.raise_main_window(cx), |
| 434 | TrayCommand::ToggleSound => { |
| 435 | self.update_settings(cx, |settings| settings.sound = !settings.sound) |
| 436 | } |
| 437 | TrayCommand::ToggleDoNotDisturb => self.update_settings(cx, |settings| { |
| 438 | settings.do_not_disturb = !settings.do_not_disturb |
| 439 | }), |
| 440 | TrayCommand::Settings => self.open_settings(cx), |
| 441 | TrayCommand::Quit => cx.quit(), |
| 442 | } |
| 443 | } |
| 444 | } |
| 445 | |
| 446 | fn toggle_main_window(&mut self, cx: &mut Context<Self>) { |
| 447 | if self.main_window_active(cx) { |
| 448 | if let Some(native) = self.main_native { |
| 449 | platform::hide_window(native); |
| 450 | } |
| 451 | } else { |
| 452 | self.raise_main_window(cx); |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | fn raise_main_window(&mut self, cx: &mut Context<Self>) { |
| 457 | if let Some(native) = self.main_native { |
| 458 | platform::restore_and_raise(native); |
| 459 | } |
| 460 | self.main_window |
| 461 | .update(cx, |_, window, _| window.activate_window()) |
| 462 | .ok(); |
| 463 | } |
| 464 | |
| 465 | fn sync_tray(&self) { |
| 466 | if let Some(tray) = &self.tray { |
| 467 | tray.sync( |
| 468 | self.settings.sound, |
| 469 | self.settings.do_not_disturb, |
| 470 | !self.unread_mentions.is_empty(), |
| 471 | ); |
| 472 | } |
| 473 | } |
| 474 | |
| 475 | fn refresh_badge(&mut self, cx: &mut Context<Self>) { |
| 476 | let (count, still_unread) = unread_summary(&self.app.read(cx).sidebar); |
| 477 | self.unread_mentions |
| 478 | .retain(|conversation_id| still_unread.contains(conversation_id)); |
| 479 | let badge = badge_for(count, !self.unread_mentions.is_empty()); |
| 480 | if badge == self.shown_badge { |
| 481 | return; |
| 482 | } |
| 483 | if let Some(native) = self.main_native { |
| 484 | platform::set_badge(native, badge.as_ref()); |
| 485 | } |
| 486 | self.shown_badge = badge; |
| 487 | self.sync_tray(); |
| 488 | } |
| 489 | |
| 490 | pub fn set_hover(&mut self, id: u64, hovered: bool, cx: &mut Context<Self>) { |
| 491 | let now = Instant::now(); |
| 492 | let Some(toast) = self.stack.get_mut(id) else { |
| 493 | return; |
| 494 | }; |
| 495 | if toast.hovered == hovered { |
| 496 | return; |
| 497 | } |
| 498 | toast.hovered = hovered; |
| 499 | if hovered { |
| 500 | toast.timer.pause(now); |
| 501 | } else if !toast.holds_open() { |
| 502 | toast.timer.resume(now); |
| 503 | } |
| 504 | cx.notify(); |
| 505 | } |
| 506 | |
| 507 | pub fn dismiss(&mut self, id: u64, cx: &mut Context<Self>) { |
| 508 | self.drop_toast(id, cx); |
| 509 | } |
| 510 | |
| 511 | pub fn open_reply(&mut self, id: u64, cx: &mut Context<Self>) -> Option<String> { |
| 512 | let now = Instant::now(); |
| 513 | let toast = self.stack.get_mut(id)?; |
| 514 | toast.reply = ReplyState::Open; |
| 515 | toast.timer.pause(now); |
| 516 | let draft = toast.reply_text.clone(); |
| 517 | cx.notify(); |
| 518 | Some(draft) |
| 519 | } |
| 520 | |
| 521 | pub fn close_reply(&mut self, id: u64, cx: &mut Context<Self>) { |
| 522 | if let Some(toast) = self.stack.get_mut(id) { |
| 523 | toast.reply = ReplyState::Closed; |
| 524 | toast.reply_text.clear(); |
| 525 | toast.hovered = false; |
| 526 | } |
| 527 | cx.notify(); |
| 528 | } |
| 529 | |
| 530 | pub fn submit_reply(&mut self, id: u64, text: String, cx: &mut Context<Self>) { |
| 531 | let Some(toast) = self.stack.get_mut(id) else { |
| 532 | return; |
| 533 | }; |
| 534 | toast.reply_text = text.clone(); |
| 535 | let conversation_id = toast.conversation_id.clone(); |
| 536 | let message_id = toast.message_id.clone(); |
| 537 | let is_channel = matches!(toast.kind, ChatKind::Channel { .. }); |
| 538 | let state = self.app.read(cx); |
| 539 | let (mode, engine) = (state.mode, state.engine.clone()); |
| 540 | let outcome: Result<(), ()> = if mode.demo { |
| 541 | Ok(()) |
| 542 | } else if mode.read_only { |
| 543 | Err(()) |
| 544 | } else if let Some(engine) = engine { |
| 545 | let receiver = runtime::spawn(async move { |
| 546 | if is_channel { |
| 547 | engine.reply_to(&conversation_id, &message_id, &text).await |
| 548 | } else { |
| 549 | engine.send_message(&conversation_id, &text, None).await |
| 550 | } |
| 551 | .map(|_| ()) |
| 552 | }); |
| 553 | cx.spawn(async move |this, cx| { |
| 554 | let sent = matches!(receiver.await, Ok(Ok(()))); |
| 555 | this.update(cx, |center, cx| center.finish_reply(id, sent, cx)).ok(); |
| 556 | }) |
| 557 | .detach(); |
| 558 | return; |
| 559 | } else { |
| 560 | Err(()) |
| 561 | }; |
| 562 | self.finish_reply(id, outcome.is_ok(), cx); |
| 563 | } |
| 564 | |
| 565 | fn finish_reply(&mut self, id: u64, sent: bool, cx: &mut Context<Self>) { |
| 566 | let Some(toast) = self.stack.get_mut(id) else { |
| 567 | return; |
| 568 | }; |
| 569 | toast.reply = if sent { |
| 570 | ReplyState::Sent |
| 571 | } else { |
| 572 | ReplyState::Failed |
| 573 | }; |
| 574 | let conversation_id = toast.conversation_id.clone(); |
| 575 | cx.notify(); |
| 576 | if sent { |
| 577 | cx.spawn(async move |this, cx| { |
| 578 | cx.background_executor().timer(SENT_DURATION).await; |
| 579 | this.update(cx, |center, cx| { |
| 580 | center.mark_conversation_read(&conversation_id, cx); |
| 581 | center.drop_toast(id, cx); |
| 582 | }) |
| 583 | .ok(); |
| 584 | }) |
| 585 | .detach(); |
| 586 | } |
| 587 | } |
| 588 | |
| 589 | fn mark_conversation_read(&mut self, conversation_id: &str, cx: &mut Context<Self>) { |
| 590 | self.unread_mentions.remove(conversation_id); |
| 591 | let conversation_id = conversation_id.to_owned(); |
| 592 | self.app |
| 593 | .update(cx, |state, cx| state.mark_chat_read(&conversation_id, cx)); |
| 594 | self.refresh_badge(cx); |
| 595 | } |
| 596 | |
| 597 | pub fn mark_read(&mut self, id: u64, cx: &mut Context<Self>) { |
| 598 | let Some(conversation_id) = self.stack.get(id).map(|toast| toast.conversation_id.clone()) |
| 599 | else { |
| 600 | return; |
| 601 | }; |
| 602 | self.mark_conversation_read(&conversation_id, cx); |
| 603 | self.drop_toast(id, cx); |
| 604 | } |
| 605 | |
| 606 | pub fn activate(&mut self, id: u64, cx: &mut Context<Self>) { |
| 607 | let Some((conversation_id, message_id)) = self |
| 608 | .stack |
| 609 | .get(id) |
| 610 | .map(|toast| (toast.conversation_id.clone(), toast.message_id.clone())) |
| 611 | else { |
| 612 | return; |
| 613 | }; |
| 614 | self.drop_toast(id, cx); |
| 615 | self.raise_main_window(cx); |
| 616 | let selection = selection_for(&self.app.read(cx).sidebar, &conversation_id); |
| 617 | if let Some(selection) = selection { |
| 618 | self.app.update(cx, |state, cx| { |
| 619 | state.jump_to_message(selection, message_id, cx) |
| 620 | }); |
| 621 | } |
| 622 | } |
| 623 | |
| 624 | pub fn open_chat_list(&mut self, cx: &mut Context<Self>) { |
| 625 | self.raise_main_window(cx); |
| 626 | } |
| 627 | |
| 628 | pub fn open_settings(&mut self, cx: &mut Context<Self>) { |
| 629 | if let Some(handle) = self.settings_window { |
| 630 | if handle.update(cx, |_, window, _| window.activate_window()).is_ok() { |
| 631 | return; |
| 632 | } |
| 633 | self.settings_window = None; |
| 634 | } |
| 635 | let center = cx.entity(); |
| 636 | let options = WindowOptions { |
| 637 | window_bounds: Some(WindowBounds::Windowed(Bounds::centered( |
| 638 | None, |
| 639 | size(px(SettingsView::WIDTH), px(SettingsView::HEIGHT)), |
| 640 | cx, |
| 641 | ))), |
| 642 | titlebar: Some(TitlebarOptions { |
| 643 | title: Some("Notifications".into()), |
| 644 | ..Default::default() |
| 645 | }), |
| 646 | is_resizable: false, |
| 647 | is_minimizable: false, |
| 648 | ..Default::default() |
| 649 | }; |
| 650 | if let Ok((handle, _)) = gpui_kit::open_window(options, cx, move |_, cx| { |
| 651 | cx.new(|cx| SettingsView::new(center, cx)) |
| 652 | }) { |
| 653 | self.settings_window = Some(handle); |
| 654 | } |
| 655 | } |
| 656 | |
| 657 | pub fn send_test_notification(&mut self, cx: &mut Context<Self>) { |
| 658 | let incoming = Incoming { |
| 659 | conversation_id: "test-notification".to_owned(), |
| 660 | message_id: "test".to_owned(), |
| 661 | kind: ChatKind::Direct, |
| 662 | chat_title: "Mara Lindqvist".to_owned(), |
| 663 | sender_id: None, |
| 664 | sender_name: "Mara Lindqvist".to_owned(), |
| 665 | preview: Preview::Text("This is a test notification.".to_owned()), |
| 666 | mentions_me: false, |
| 667 | muted: false, |
| 668 | }; |
| 669 | let decision = Decision { |
| 670 | toast: true, |
| 671 | sound: self.settings.sound, |
| 672 | }; |
| 673 | self.throttle = SoundThrottle::default(); |
| 674 | self.present(&incoming, decision, cx); |
| 675 | } |
| 676 | |
| 677 | fn show_demo_toasts(&mut self, cx: &mut Context<Self>) { |
| 678 | for incoming in demo_incoming() { |
| 679 | let decision = Decision { |
| 680 | toast: true, |
| 681 | sound: false, |
| 682 | }; |
| 683 | self.present(&incoming, decision, cx); |
| 684 | } |
| 685 | } |
| 686 | } |
| 687 | |
| 688 | fn border_color(mention: bool) -> u32 { |
| 689 | if mention { MENTION_BORDER } else { FALLBACK_BORDER } |
| 690 | } |
| 691 | |
| 692 | fn popup_options(slot: Slot, area: WorkArea) -> WindowOptions { |
| 693 | let scale = area.scale; |
| 694 | let bounds = Bounds::new( |
| 695 | point(px(slot.x as f32 / scale), px(slot.y as f32 / scale)), |
| 696 | size(px(slot.width as f32 / scale), px(slot.height as f32 / scale)), |
| 697 | ); |
| 698 | WindowOptions { |
| 699 | window_bounds: Some(WindowBounds::Windowed(bounds)), |
| 700 | titlebar: None, |
| 701 | focus: false, |
| 702 | show: true, |
| 703 | kind: WindowKind::PopUp, |
| 704 | is_movable: false, |
| 705 | is_resizable: false, |
| 706 | is_minimizable: false, |
| 707 | window_background: WindowBackgroundAppearance::Opaque, |
| 708 | ..Default::default() |
| 709 | } |
| 710 | } |
| 711 | |
| 712 | pub fn selection_for(sidebar: &Sidebar, conversation_id: &str) -> Option<Selection> { |
| 713 | if sidebar.chats.iter().any(|chat| chat.id == conversation_id) { |
| 714 | return Some(Selection::Chat(conversation_id.to_owned())); |
| 715 | } |
| 716 | sidebar |
| 717 | .teams |
| 718 | .iter() |
| 719 | .flat_map(|team| team.channels.iter()) |
| 720 | .any(|channel| channel.id == conversation_id) |
| 721 | .then(|| Selection::Channel(conversation_id.to_owned())) |
| 722 | } |
| 723 | |
| 724 | pub fn unread_summary(sidebar: &Sidebar) -> (usize, HashSet<String>) { |
| 725 | let chats = sidebar.chats.iter().filter(|chat| chat.unread); |
| 726 | let channels = sidebar |
| 727 | .teams |
| 728 | .iter() |
| 729 | .flat_map(|team| team.channels.iter()) |
| 730 | .filter(|channel| channel.unread); |
| 731 | let ids: HashSet<String> = chats |
| 732 | .map(|chat| chat.id.clone()) |
| 733 | .chain(channels.map(|channel| channel.id.clone())) |
| 734 | .collect(); |
| 735 | (ids.len(), ids) |
| 736 | } |
| 737 | |
| 738 | fn demo_incoming() -> Vec<Incoming> { |
| 739 | let item = |conversation: &str, kind: ChatKind, title: &str, sender: &str, text: &str, mention: bool| Incoming { |
| 740 | conversation_id: conversation.to_owned(), |
| 741 | message_id: format!("{conversation}-message"), |
| 742 | kind, |
| 743 | chat_title: title.to_owned(), |
| 744 | sender_id: None, |
| 745 | sender_name: sender.to_owned(), |
| 746 | preview: Preview::Text(text.to_owned()), |
| 747 | mentions_me: mention, |
| 748 | muted: false, |
| 749 | }; |
| 750 | vec![ |
| 751 | item( |
| 752 | "demo-toast-direct", |
| 753 | ChatKind::Direct, |
| 754 | "Tobias Klein", |
| 755 | "Tobias Klein", |
| 756 | "Sounds good, I will look at it tomorrow.", |
| 757 | false, |
| 758 | ), |
| 759 | item( |
| 760 | "demo-toast-group", |
| 761 | ChatKind::Group { member_count: 5 }, |
| 762 | "Retro-Team", |
| 763 | "Priya Nair", |
| 764 | "Please add topics by 5 pm.", |
| 765 | false, |
| 766 | ), |
| 767 | item( |
| 768 | "demo-toast-mention", |
| 769 | ChatKind::Channel { |
| 770 | team: "Platform".to_owned(), |
| 771 | channel: "Releases".to_owned(), |
| 772 | }, |
| 773 | "Releases", |
| 774 | "Mara Lindqvist", |
| 775 | "@Jonas can you approve the merge? The pipeline is waiting.", |
| 776 | true, |
| 777 | ), |
| 778 | ] |
| 779 | } |
| 780 | |
| 781 | #[cfg(test)] |
| 782 | mod tests { |
| 783 | use store::{ChannelRecord, ChatRecord, SidebarTeam, TeamRecord}; |
| 784 | |
| 785 | use super::{selection_for, unread_summary}; |
| 786 | use crate::app_state::Selection; |
| 787 | use store::Sidebar; |
| 788 | |
| 789 | fn sidebar() -> Sidebar { |
| 790 | Sidebar { |
| 791 | chats: vec![ |
| 792 | ChatRecord { |
| 793 | id: "a".into(), |
| 794 | unread: true, |
| 795 | ..Default::default() |
| 796 | }, |
| 797 | ChatRecord { |
| 798 | id: "b".into(), |
| 799 | unread: false, |
| 800 | ..Default::default() |
| 801 | }, |
| 802 | ], |
| 803 | teams: vec![SidebarTeam { |
| 804 | team: TeamRecord { |
| 805 | id: "t".into(), |
| 806 | name: "Team".into(), |
| 807 | }, |
| 808 | channels: vec![ChannelRecord { |
| 809 | id: "ch".into(), |
| 810 | team_id: "t".into(), |
| 811 | name: "General".into(), |
| 812 | membership_type: None, |
| 813 | last_message_at: None, |
| 814 | unread: true, |
| 815 | }], |
| 816 | hidden: false, |
| 817 | hidden_channel_ids: Vec::new(), |
| 818 | }], |
| 819 | } |
| 820 | } |
| 821 | |
| 822 | #[test] |
| 823 | fn unread_summary_counts_chats_and_channels() { |
| 824 | let (count, ids) = unread_summary(&sidebar()); |
| 825 | assert_eq!(count, 2); |
| 826 | assert!(ids.contains("a") && ids.contains("ch")); |
| 827 | } |
| 828 | |
| 829 | #[test] |
| 830 | fn selection_kind_follows_sidebar() { |
| 831 | assert_eq!(selection_for(&sidebar(), "a"), Some(Selection::Chat("a".into()))); |
| 832 | assert_eq!(selection_for(&sidebar(), "ch"), Some(Selection::Channel("ch".into()))); |
| 833 | assert_eq!(selection_for(&sidebar(), "x"), None); |
| 834 | } |
| 835 | } |