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