| 1 | use std::time::{Duration, Instant}; |
| 2 | |
| 3 | use calling::camera::{DEFAULT_CAMERA_LABEL, list_cameras}; |
| 4 | use calling::{DeviceChoice, DeviceEntry, camera_entries}; |
| 5 | use gpui_kit::assets::IconName; |
| 6 | use gpui_kit::component::{ |
| 7 | button::Button, |
| 8 | h_flex, |
| 9 | menu::{DropdownMenu as _, PopupMenuItem}, |
| 10 | v_flex, |
| 11 | }; |
| 12 | use gpui_kit::prelude::FluentBuilder as _; |
| 13 | use gpui_kit::*; |
| 14 | |
| 15 | use super::center::NotificationCenter; |
| 16 | use super::platform; |
| 17 | use super::ring_tone::Ringtone; |
| 18 | use super::rules::Settings; |
| 19 | use crate::app_state::AppState; |
| 20 | use crate::call::{BackgroundPick, CameraPreview, preview_backgrounds}; |
| 21 | use crate::theme; |
| 22 | |
| 23 | const FPS_TICK: Duration = Duration::from_millis(500); |
| 24 | const SELECT_WIDTH: f32 = 170.; |
| 25 | const PREVIEW_WIDTH: f32 = 380.; |
| 26 | const PREVIEW_HEIGHT: f32 = 214.; |
| 27 | const PREVIEW_RADIUS: f32 = 10.; |
| 28 | const CAMERA_IN_USE: &str = "Camera is in use by the call"; |
| 29 | |
| 30 | fn camera_in_call(state: &AppState) -> bool { |
| 31 | state.call.is_some() |
| 32 | } |
| 33 | |
| 34 | #[derive(Clone, Copy, PartialEq, Eq)] |
| 35 | enum Section { |
| 36 | Notifications, |
| 37 | Devices, |
| 38 | } |
| 39 | |
| 40 | impl Section { |
| 41 | const ALL: [Section; 2] = [Section::Notifications, Section::Devices]; |
| 42 | |
| 43 | fn label(self) -> &'static str { |
| 44 | match self { |
| 45 | Section::Notifications => "Notifications", |
| 46 | Section::Devices => "Devices", |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | type Accessor = fn(&mut Settings) -> &mut bool; |
| 52 | |
| 53 | struct Row { |
| 54 | title: &'static str, |
| 55 | detail: &'static str, |
| 56 | field: Accessor, |
| 57 | } |
| 58 | |
| 59 | const ROWS: [Row; 5] = [ |
| 60 | Row { |
| 61 | title: "Sound", |
| 62 | detail: "Messages and mentions", |
| 63 | field: |settings| &mut settings.sound, |
| 64 | }, |
| 65 | Row { |
| 66 | title: "Mentions only", |
| 67 | detail: "Direct messages and @mentions", |
| 68 | field: |settings| &mut settings.mentions_only, |
| 69 | }, |
| 70 | Row { |
| 71 | title: "Show preview", |
| 72 | detail: "Off: only \"New message\"", |
| 73 | field: |settings| &mut settings.preview, |
| 74 | }, |
| 75 | Row { |
| 76 | title: "Flash taskbar", |
| 77 | detail: "On new messages, until you open the window", |
| 78 | field: |settings| &mut settings.flash, |
| 79 | }, |
| 80 | Row { |
| 81 | title: "Minimize to tray on close", |
| 82 | detail: "The title bar X hides the window", |
| 83 | field: |settings| &mut settings.close_to_tray, |
| 84 | }, |
| 85 | ]; |
| 86 | |
| 87 | pub struct SettingsView { |
| 88 | center: Entity<NotificationCenter>, |
| 89 | section: Section, |
| 90 | preview: CameraPreview, |
| 91 | cameras: Vec<DeviceEntry>, |
| 92 | cameras_requested: bool, |
| 93 | camera: DeviceChoice, |
| 94 | background: BackgroundPick, |
| 95 | preview_blocked: bool, |
| 96 | _subscription: Subscription, |
| 97 | _app_subscription: Subscription, |
| 98 | _release: Subscription, |
| 99 | } |
| 100 | |
| 101 | impl SettingsView { |
| 102 | pub const WIDTH: f32 = 420.; |
| 103 | pub const HEIGHT: f32 = 540.; |
| 104 | |
| 105 | pub fn new(center: Entity<NotificationCenter>, cx: &mut Context<Self>) -> Self { |
| 106 | let subscription = cx.observe(¢er, |_, _, cx| cx.notify()); |
| 107 | let app = center.read(cx).app().clone(); |
| 108 | let app_subscription = cx.observe(&app, |view, _, cx| view.sync_preview_block(cx)); |
| 109 | let release = cx.on_release(|view, cx| view.preview.stop(cx)); |
| 110 | let (preview, mut updates) = CameraPreview::new(); |
| 111 | let state = app.read(cx); |
| 112 | let camera = state.call_camera.clone(); |
| 113 | let preview_blocked = camera_in_call(state); |
| 114 | let background = Some(state.call_background.clone()) |
| 115 | .filter(|pick| preview_backgrounds(&state.call_backgrounds).iter().any(|(known, _)| known == pick)) |
| 116 | .unwrap_or_default(); |
| 117 | cx.spawn(async move |this, cx| { |
| 118 | while updates.recv().await.is_some() { |
| 119 | if this.update(cx, |view, cx| view.take_frames(cx)).is_err() { |
| 120 | return; |
| 121 | } |
| 122 | } |
| 123 | }) |
| 124 | .detach(); |
| 125 | cx.spawn(async move |this, cx| { |
| 126 | loop { |
| 127 | cx.background_executor().timer(FPS_TICK).await; |
| 128 | if this.update(cx, |view, cx| view.tick(cx)).is_err() { |
| 129 | break; |
| 130 | } |
| 131 | } |
| 132 | }) |
| 133 | .detach(); |
| 134 | SettingsView { |
| 135 | center, |
| 136 | section: Section::Notifications, |
| 137 | preview, |
| 138 | cameras: camera_entries(&[]), |
| 139 | cameras_requested: false, |
| 140 | camera, |
| 141 | background, |
| 142 | preview_blocked, |
| 143 | _subscription: subscription, |
| 144 | _app_subscription: app_subscription, |
| 145 | _release: release, |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | fn take_frames(&mut self, cx: &mut Context<Self>) { |
| 150 | self.preview.take_frames(cx); |
| 151 | cx.notify(); |
| 152 | } |
| 153 | |
| 154 | fn tick(&mut self, cx: &mut Context<Self>) { |
| 155 | if self.section == Section::Devices { |
| 156 | self.preview.tick(Instant::now()); |
| 157 | cx.notify(); |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | fn select_section(&mut self, section: Section, cx: &mut Context<Self>) { |
| 162 | if self.section == section { |
| 163 | return; |
| 164 | } |
| 165 | self.section = section; |
| 166 | match section { |
| 167 | Section::Devices => { |
| 168 | self.load_cameras(cx); |
| 169 | self.apply_background(cx); |
| 170 | self.preview_blocked = camera_in_call(self.center.read(cx).app().read(cx)); |
| 171 | self.start_preview(); |
| 172 | } |
| 173 | Section::Notifications => self.preview.stop(cx), |
| 174 | } |
| 175 | cx.notify(); |
| 176 | } |
| 177 | |
| 178 | fn load_cameras(&mut self, cx: &mut Context<Self>) { |
| 179 | if std::mem::replace(&mut self.cameras_requested, true) { |
| 180 | return; |
| 181 | } |
| 182 | cx.spawn(async move |this, cx| { |
| 183 | let cameras = cx.background_executor().spawn(async { list_cameras() }).await; |
| 184 | this.update(cx, |view, cx| { |
| 185 | view.cameras = camera_entries(&cameras); |
| 186 | cx.notify(); |
| 187 | }) |
| 188 | .ok(); |
| 189 | }) |
| 190 | .detach(); |
| 191 | } |
| 192 | |
| 193 | fn start_preview(&mut self) { |
| 194 | if !self.preview_blocked { |
| 195 | self.preview.start(&self.camera); |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | fn sync_preview_block(&mut self, cx: &mut Context<Self>) { |
| 200 | let blocked = camera_in_call(self.center.read(cx).app().read(cx)); |
| 201 | if blocked == self.preview_blocked { |
| 202 | return; |
| 203 | } |
| 204 | self.preview_blocked = blocked; |
| 205 | if blocked { |
| 206 | self.preview.stop(cx); |
| 207 | } else if self.section == Section::Devices { |
| 208 | self.start_preview(); |
| 209 | } |
| 210 | cx.notify(); |
| 211 | } |
| 212 | |
| 213 | fn select_camera(&mut self, camera: DeviceChoice, cx: &mut Context<Self>) { |
| 214 | self.camera = camera.clone(); |
| 215 | let app = self.center.read(cx).app().clone(); |
| 216 | app.update(cx, |state, cx| state.select_call_camera(camera, cx)); |
| 217 | self.start_preview(); |
| 218 | cx.notify(); |
| 219 | } |
| 220 | |
| 221 | fn select_background(&mut self, pick: BackgroundPick, cx: &mut Context<Self>) { |
| 222 | self.background = pick.clone(); |
| 223 | let app = self.center.read(cx).app().clone(); |
| 224 | app.update(cx, |state, cx| state.set_call_background(pick, cx)); |
| 225 | self.apply_background(cx); |
| 226 | cx.notify(); |
| 227 | } |
| 228 | |
| 229 | fn apply_background(&self, cx: &App) { |
| 230 | let state = self.center.read(cx).app().read(cx); |
| 231 | if let Some(choice) = state.call_backgrounds.choice_for(&self.background) { |
| 232 | self.preview.set_background(choice); |
| 233 | } |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | fn switch(on: bool) -> Div { |
| 238 | div() |
| 239 | .w(px(36.)) |
| 240 | .h(px(20.)) |
| 241 | .flex_none() |
| 242 | .rounded_full() |
| 243 | .relative() |
| 244 | .bg(if on { |
| 245 | theme::accent() |
| 246 | } else { |
| 247 | theme::border_strong() |
| 248 | }) |
| 249 | .child( |
| 250 | div() |
| 251 | .absolute() |
| 252 | .top(px(2.)) |
| 253 | .left(px(if on { 18. } else { 2. })) |
| 254 | .size(px(16.)) |
| 255 | .rounded_full() |
| 256 | .bg(theme::text()), |
| 257 | ) |
| 258 | } |
| 259 | |
| 260 | fn button(label: &'static str) -> Stateful<Div> { |
| 261 | div() |
| 262 | .id(label) |
| 263 | .h(px(32.)) |
| 264 | .px(px(14.)) |
| 265 | .flex() |
| 266 | .items_center() |
| 267 | .rounded(px(8.)) |
| 268 | .border_1() |
| 269 | .border_color(theme::border_strong()) |
| 270 | .cursor_pointer() |
| 271 | .text_size(px(13.)) |
| 272 | .font_weight(FontWeight::SEMIBOLD) |
| 273 | .text_color(theme::text()) |
| 274 | .hover(|button| button.bg(theme::row_hover())) |
| 275 | .child(label) |
| 276 | } |
| 277 | |
| 278 | fn titled(title: &'static str, detail: &'static str) -> Div { |
| 279 | v_flex() |
| 280 | .flex_1() |
| 281 | .child( |
| 282 | div() |
| 283 | .text_size(px(14.)) |
| 284 | .font_weight(FontWeight::SEMIBOLD) |
| 285 | .text_color(theme::text()) |
| 286 | .child(title), |
| 287 | ) |
| 288 | .child( |
| 289 | div() |
| 290 | .text_size(px(12.)) |
| 291 | .text_color(theme::text_muted()) |
| 292 | .child(detail), |
| 293 | ) |
| 294 | } |
| 295 | |
| 296 | fn control_row(title: &'static str, detail: &'static str, control: impl IntoElement) -> Div { |
| 297 | h_flex() |
| 298 | .w_full() |
| 299 | .px(px(20.)) |
| 300 | .py(px(10.)) |
| 301 | .gap(px(12.)) |
| 302 | .items_center() |
| 303 | .child(titled(title, detail)) |
| 304 | .child(control) |
| 305 | } |
| 306 | |
| 307 | fn section_tab(section: Section, active: bool, view: Entity<SettingsView>) -> Stateful<Div> { |
| 308 | div() |
| 309 | .id(section.label()) |
| 310 | .h(px(32.)) |
| 311 | .px(px(14.)) |
| 312 | .flex() |
| 313 | .items_center() |
| 314 | .rounded(px(8.)) |
| 315 | .cursor_pointer() |
| 316 | .text_size(px(13.)) |
| 317 | .font_weight(FontWeight::SEMIBOLD) |
| 318 | .text_color(if active { theme::on_accent() } else { theme::text() }) |
| 319 | .when(active, |tab| tab.bg(theme::accent())) |
| 320 | .when(!active, |tab| tab.hover(|tab| tab.bg(theme::row_hover()))) |
| 321 | .child(section.label()) |
| 322 | .on_click(move |_, _, cx| view.update(cx, |view, cx| view.select_section(section, cx))) |
| 323 | } |
| 324 | |
| 325 | impl SettingsView { |
| 326 | fn ringtone_row(&self, current: Ringtone) -> Div { |
| 327 | let center = self.center.clone(); |
| 328 | control_row( |
| 329 | "Ringtone", |
| 330 | "Incoming calls", |
| 331 | h_flex() |
| 332 | .gap(px(8.)) |
| 333 | .items_center() |
| 334 | .child( |
| 335 | Button::new("ringtone-select") |
| 336 | .outline() |
| 337 | .w(px(110.)) |
| 338 | .label(current.label()) |
| 339 | .icon(IconName::ChevronDown) |
| 340 | .dropdown_menu(move |menu, _, _| { |
| 341 | Ringtone::ALL.iter().fold(menu, |menu, ringtone| { |
| 342 | let (center, ringtone) = (center.clone(), *ringtone); |
| 343 | menu.item(PopupMenuItem::new(ringtone.label()).checked(ringtone == current).on_click( |
| 344 | move |_, _, cx| { |
| 345 | center.update(cx, |center, cx| { |
| 346 | center.update_settings(cx, |settings| settings.ringtone = ringtone) |
| 347 | }); |
| 348 | platform::play_preview(ringtone); |
| 349 | }, |
| 350 | )) |
| 351 | }) |
| 352 | }), |
| 353 | ) |
| 354 | .child(button("Play").on_click(move |_, _, _| platform::play_preview(current))), |
| 355 | ) |
| 356 | } |
| 357 | |
| 358 | fn devices_section(&self, cx: &mut Context<Self>) -> Div { |
| 359 | let view = cx.entity(); |
| 360 | let camera_label = self |
| 361 | .cameras |
| 362 | .iter() |
| 363 | .find(|entry| entry.choice == self.camera) |
| 364 | .map_or(DEFAULT_CAMERA_LABEL.to_owned(), |entry| entry.label.clone()); |
| 365 | let cameras = self.cameras.clone(); |
| 366 | let current_camera = self.camera.clone(); |
| 367 | let camera_view = view.clone(); |
| 368 | let backgrounds = preview_backgrounds(&self.center.read(cx).app().read(cx).call_backgrounds); |
| 369 | let background_label = backgrounds |
| 370 | .iter() |
| 371 | .find(|(pick, _)| *pick == self.background) |
| 372 | .map_or("None".to_owned(), |(_, label)| label.clone()); |
| 373 | let current_background = self.background.clone(); |
| 374 | let image = self.preview.image(Instant::now()); |
| 375 | let status = if self.preview_blocked { |
| 376 | CAMERA_IN_USE.to_owned() |
| 377 | } else { |
| 378 | self.preview.failure().map_or_else( |
| 379 | || { |
| 380 | self.preview |
| 381 | .fps() |
| 382 | .map_or("Starting camera...".to_owned(), |fps| format!("{fps:.0} fps")) |
| 383 | }, |
| 384 | |reason| format!("Camera could not start: {reason}"), |
| 385 | ) |
| 386 | }; |
| 387 | v_flex() |
| 388 | .w_full() |
| 389 | .child(control_row( |
| 390 | "Camera", |
| 391 | "Preview stays on this computer", |
| 392 | Button::new("camera-select") |
| 393 | .outline() |
| 394 | .w(px(SELECT_WIDTH)) |
| 395 | .label(camera_label) |
| 396 | .icon(IconName::ChevronDown) |
| 397 | .dropdown_menu(move |menu, _, _| { |
| 398 | cameras.iter().fold(menu, |menu, entry| { |
| 399 | let (view, choice) = (camera_view.clone(), entry.choice.clone()); |
| 400 | menu.item( |
| 401 | PopupMenuItem::new(entry.label.clone()) |
| 402 | .checked(entry.choice == current_camera) |
| 403 | .on_click(move |_, _, cx| { |
| 404 | view.update(cx, |view, cx| view.select_camera(choice.clone(), cx)) |
| 405 | }), |
| 406 | ) |
| 407 | }) |
| 408 | }), |
| 409 | )) |
| 410 | .child(control_row( |
| 411 | "Background", |
| 412 | "Blur or a picture behind you", |
| 413 | Button::new("background-select") |
| 414 | .outline() |
| 415 | .w(px(SELECT_WIDTH)) |
| 416 | .label(background_label) |
| 417 | .icon(IconName::ChevronDown) |
| 418 | .dropdown_menu(move |menu, _, _| { |
| 419 | backgrounds.iter().fold(menu, |menu, (pick, label)| { |
| 420 | let (view, pick) = (view.clone(), pick.clone()); |
| 421 | menu.item( |
| 422 | PopupMenuItem::new(label.clone()) |
| 423 | .checked(pick == current_background) |
| 424 | .on_click(move |_, _, cx| { |
| 425 | view.update(cx, |view, cx| view.select_background(pick.clone(), cx)) |
| 426 | }), |
| 427 | ) |
| 428 | }) |
| 429 | }), |
| 430 | )) |
| 431 | .child( |
| 432 | v_flex() |
| 433 | .w_full() |
| 434 | .px(px(20.)) |
| 435 | .pt(px(8.)) |
| 436 | .gap(px(8.)) |
| 437 | .child( |
| 438 | div() |
| 439 | .w(px(PREVIEW_WIDTH)) |
| 440 | .h(px(PREVIEW_HEIGHT)) |
| 441 | .flex() |
| 442 | .items_center() |
| 443 | .justify_center() |
| 444 | .overflow_hidden() |
| 445 | .rounded(px(PREVIEW_RADIUS)) |
| 446 | .bg(theme::surface()) |
| 447 | .children(image.map(|image| { |
| 448 | img(ImageSource::Render(image)) |
| 449 | .size_full() |
| 450 | .object_fit(ObjectFit::Cover) |
| 451 | .rounded(px(PREVIEW_RADIUS)) |
| 452 | })), |
| 453 | ) |
| 454 | .child(div().text_size(px(12.)).text_color(theme::text_muted()).child(status)), |
| 455 | ) |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | impl Render for SettingsView { |
| 460 | fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement { |
| 461 | let view = cx.entity(); |
| 462 | let tabs = h_flex() |
| 463 | .w_full() |
| 464 | .px(px(20.)) |
| 465 | .gap(px(8.)) |
| 466 | .children(Section::ALL.map(|section| section_tab(section, section == self.section, view.clone()))); |
| 467 | let body = match self.section { |
| 468 | Section::Notifications => self.notifications_section(cx).into_any_element(), |
| 469 | Section::Devices => self.devices_section(cx).into_any_element(), |
| 470 | }; |
| 471 | v_flex() |
| 472 | .size_full() |
| 473 | .bg(theme::background()) |
| 474 | .pt(px(12.)) |
| 475 | .child(tabs) |
| 476 | .child(body) |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | impl SettingsView { |
| 481 | fn notifications_section(&self, cx: &App) -> Div { |
| 482 | let mut current = self.center.read(cx).settings().clone(); |
| 483 | let corner = current.corner; |
| 484 | let ringtone = current.ringtone; |
| 485 | let rows = ROWS.iter().enumerate().map(|(index, row)| { |
| 486 | let on = *(row.field)(&mut current); |
| 487 | let center = self.center.clone(); |
| 488 | let field = row.field; |
| 489 | h_flex() |
| 490 | .id(("setting", index)) |
| 491 | .w_full() |
| 492 | .px(px(20.)) |
| 493 | .py(px(10.)) |
| 494 | .gap(px(12.)) |
| 495 | .items_center() |
| 496 | .cursor_pointer() |
| 497 | .hover(|row| row.bg(theme::row_hover())) |
| 498 | .on_click(move |_, _, cx| { |
| 499 | center.update(cx, |center, cx| { |
| 500 | center.update_settings(cx, |settings| { |
| 501 | let value = field(settings); |
| 502 | *value = !*value; |
| 503 | }) |
| 504 | }); |
| 505 | }) |
| 506 | .child( |
| 507 | v_flex() |
| 508 | .flex_1() |
| 509 | .child( |
| 510 | div() |
| 511 | .text_size(px(14.)) |
| 512 | .font_weight(FontWeight::SEMIBOLD) |
| 513 | .text_color(theme::text()) |
| 514 | .child(row.title), |
| 515 | ) |
| 516 | .child( |
| 517 | div() |
| 518 | .text_size(px(12.)) |
| 519 | .text_color(theme::text_muted()) |
| 520 | .child(row.detail), |
| 521 | ), |
| 522 | ) |
| 523 | .child(switch(on)) |
| 524 | }); |
| 525 | let position_center = self.center.clone(); |
| 526 | let test_center = self.center.clone(); |
| 527 | v_flex() |
| 528 | .w_full() |
| 529 | .pt(px(8.)) |
| 530 | .children(rows) |
| 531 | .child(self.ringtone_row(ringtone)) |
| 532 | .child(control_row( |
| 533 | "Position", |
| 534 | "Corner of the main screen", |
| 535 | button(corner.label()).on_click(move |_, _, cx| { |
| 536 | position_center.update(cx, |center, cx| { |
| 537 | center.update_settings(cx, |settings| { |
| 538 | settings.corner = settings.corner.next() |
| 539 | }) |
| 540 | }); |
| 541 | }), |
| 542 | )) |
| 543 | .child( |
| 544 | h_flex() |
| 545 | .w_full() |
| 546 | .px(px(20.)) |
| 547 | .pt(px(12.)) |
| 548 | .child(button("Send test notification").on_click(move |_, _, cx| { |
| 549 | test_center.update(cx, |center, cx| center.send_test_notification(cx)); |
| 550 | })), |
| 551 | ) |
| 552 | } |
| 553 | } |