BlueGit

ict rusty-teams

Files

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

settings_view.rs

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