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