BlueGit

ict rusty-teams

Files

rusty-teams / crates / app / src / notify / settings_view.rs

settings_view.rs

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