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 .min_w(px(0.))
414 .child(
415 div()
416 .text_size(px(14.))
417 .font_weight(FontWeight::SEMIBOLD)
418 .text_color(theme::text())
419 .child(title),
420 )
421 .child(
422 div()
423 .text_size(px(12.))
424 .text_color(detail_color)
425 .child(detail.into()),
426 )
427}
428
429pub(super) fn control_row(
430 title: &'static str,
431 detail: impl Into<SharedString>,
432 control: impl IntoElement,
433) -> Div {
434 h_flex()
435 .w_full()
436 .px(px(20.))
437 .py(px(10.))
438 .gap(px(12.))
439 .items_center()
440 .child(titled(title, detail))
441 .child(control)
442}
443
444fn section_tab(section: Section, active: bool, view: Entity<SettingsView>) -> Stateful<Div> {
445 div()
446 .id(section.label())
447 .h(px(32.))
448 .px(px(14.))
449 .flex()
450 .items_center()
451 .rounded(px(8.))
452 .cursor_pointer()
453 .text_size(px(13.))
454 .font_weight(FontWeight::SEMIBOLD)
455 .text_color(if active {
456 theme::on_accent()
457 } else {
458 theme::text()
459 })
460 .when(active, |tab| tab.bg(theme::accent()))
461 .when(!active, |tab| tab.hover(|tab| tab.bg(theme::row_hover())))
462 .child(section.label())
463 .on_click(move |_, _, cx| view.update(cx, |view, cx| view.select_section(section, cx)))
464}
465
466impl SettingsView {
467 fn ringtone_row(&self, current: Ringtone) -> Div {
468 let center = self.center.clone();
469 control_row(
470 "Ringtone",
471 "Incoming calls",
472 h_flex()
473 .gap(px(8.))
474 .items_center()
475 .child(
476 Button::new("ringtone-select")
477 .outline()
478 .w(px(110.))
479 .label(current.label())
480 .icon(IconName::ChevronDown)
481 .dropdown_menu(move |menu, _, _| {
482 Ringtone::ALL.iter().fold(menu, |menu, ringtone| {
483 let (center, ringtone) = (center.clone(), *ringtone);
484 menu.item(
485 PopupMenuItem::new(ringtone.label())
486 .checked(ringtone == current)
487 .on_click(move |_, _, cx| {
488 center.update(cx, |center, cx| {
489 center.update_settings(cx, |settings| {
490 settings.ringtone = ringtone
491 })
492 });
493 platform::play_preview(ringtone);
494 }),
495 )
496 })
497 }),
498 )
499 .child(button("Play").on_click(move |_, _, _| platform::play_preview(current))),
500 )
501 }
502
503 fn devices_section(&self, cx: &mut Context<Self>) -> Div {
504 let view = cx.entity();
505 let camera_label = self
506 .cameras
507 .iter()
508 .find(|entry| entry.choice == self.camera)
509 .map_or(DEFAULT_CAMERA_LABEL.to_owned(), |entry| entry.label.clone());
510 let cameras = self.cameras.clone();
511 let current_camera = self.camera.clone();
512 let camera_view = view.clone();
513 let backgrounds =
514 preview_backgrounds(&self.center.read(cx).app().read(cx).call_backgrounds);
515 let background_label = backgrounds
516 .iter()
517 .find(|(pick, _)| *pick == self.background)
518 .map_or("None".to_owned(), |(_, label)| label.clone());
519 let current_background = self.background.clone();
520 let mic = self.center.read(cx).app().read(cx).call_mic;
521 let noise_view = view.clone();
522 let music_view = view.clone();
523 let effects = self.center.read(cx).app().read(cx).call_effects;
524 let image = self.preview.image(Instant::now());
525 let status = if self.preview_blocked {
526 CAMERA_IN_USE.to_owned()
527 } else {
528 self.preview.failure().map_or_else(
529 || {
530 self.preview
531 .fps()
532 .map_or("Starting camera...".to_owned(), |fps| {
533 format!("{fps:.0} fps")
534 })
535 },
536 |reason| format!("Camera could not start: {reason}"),
537 )
538 };
539 v_flex()
540 .w_full()
541 .child(control_row(
542 "Camera",
543 "Preview stays on this computer",
544 Button::new("camera-select")
545 .outline()
546 .w(px(SELECT_WIDTH))
547 .label(camera_label)
548 .icon(IconName::ChevronDown)
549 .dropdown_menu(move |menu, _, _| {
550 cameras.iter().fold(menu, |menu, entry| {
551 let (view, choice) = (camera_view.clone(), entry.choice.clone());
552 menu.item(
553 PopupMenuItem::new(entry.label.clone())
554 .checked(entry.choice == current_camera)
555 .on_click(move |_, _, cx| {
556 view.update(cx, |view, cx| {
557 view.select_camera(choice.clone(), cx)
558 })
559 }),
560 )
561 })
562 }),
563 ))
564 .child(control_row(
565 "Background",
566 "Blur or a picture behind you",
567 Button::new("background-select")
568 .outline()
569 .w(px(SELECT_WIDTH))
570 .label(background_label)
571 .icon(IconName::ChevronDown)
572 .dropdown_menu(move |menu, _, _| {
573 backgrounds.iter().fold(menu, |menu, (pick, label)| {
574 let (view, pick) = (view.clone(), pick.clone());
575 menu.item(
576 PopupMenuItem::new(label.clone())
577 .checked(pick == current_background)
578 .on_click(move |_, _, cx| {
579 view.update(cx, |view, cx| {
580 view.select_background(pick.clone(), cx)
581 })
582 }),
583 )
584 })
585 }),
586 ))
587 .child(control_row(
588 "Noise suppression",
589 if mic.music_mode {
590 "Off while music mode is on"
591 } else {
592 "Echo, hum and background noise"
593 },
594 Button::new("noise-suppression-select")
595 .outline()
596 .w(px(SELECT_WIDTH))
597 .label(mic.noise_suppression.label())
598 .icon(IconName::ChevronDown)
599 .disabled(mic.music_mode)
600 .dropdown_menu(move |menu, _, _| {
601 NoiseSuppression::ALL.iter().fold(menu, |menu, level| {
602 let (view, level) = (noise_view.clone(), *level);
603 menu.item(
604 PopupMenuItem::new(level.label())
605 .checked(level == mic.noise_suppression)
606 .on_click(move |_, _, cx| {
607 view.update(cx, |view, cx| {
608 view.set_mic(
609 MicSettings {
610 noise_suppression: level,
611 ..mic
612 },
613 cx,
614 )
615 })
616 }),
617 )
618 })
619 }),
620 ))
621 .child(
622 control_row(
623 "Music mode",
624 "Keeps echo cancellation, turns off the rest",
625 switch(mic.music_mode),
626 )
627 .id("music-mode-row")
628 .cursor_pointer()
629 .hover(|row| row.bg(theme::row_hover()))
630 .on_click(move |_, _, cx| {
631 music_view.update(cx, |view, cx| {
632 view.set_mic(
633 MicSettings {
634 music_mode: !mic.music_mode,
635 ..mic
636 },
637 cx,
638 )
639 })
640 }),
641 )
642 .child(
643 v_flex()
644 .w_full()
645 .px(px(20.))
646 .pt(px(8.))
647 .gap(px(8.))
648 .child(
649 div()
650 .w(px(PREVIEW_WIDTH))
651 .h(px(PREVIEW_HEIGHT))
652 .flex()
653 .items_center()
654 .justify_center()
655 .overflow_hidden()
656 .rounded(px(PREVIEW_RADIUS))
657 .bg(theme::surface())
658 .children(image.map(|image| {
659 img(ImageSource::Render(image))
660 .size_full()
661 .object_fit(ObjectFit::Cover)
662 .rounded(px(PREVIEW_RADIUS))
663 })),
664 )
665 .child(
666 div()
667 .text_size(px(12.))
668 .text_color(theme::text_muted())
669 .child(status),
670 ),
671 )
672 .child(
673 v_flex()
674 .id("camera-effects")
675 .w_full()
676 .pt(px(8.))
677 .child(self.effect_row(
678 "effect-mirror",
679 "Mirror my video",
680 effects.mirror,
681 |effects| effects.mirror = !effects.mirror,
682 cx,
683 ))
684 .child(self.effect_row(
685 "effect-framing",
686 "Auto framing",
687 effects.auto_framing,
688 |effects| effects.auto_framing = !effects.auto_framing,
689 cx,
690 ))
691 .child(self.effect_row(
692 "effect-brightness",
693 "Adjust brightness",
694 effects.adjust_brightness,
695 |effects| effects.adjust_brightness = !effects.adjust_brightness,
696 cx,
697 ))
698 .child(self.effect_row(
699 "effect-soft-focus",
700 "Soft focus",
701 effects.soft_focus,
702 |effects| effects.soft_focus = !effects.soft_focus,
703 cx,
704 ))
705 .when(effects.soft_focus, |column| {
706 column.child(self.strength_row(effects.soft_focus_strength, cx))
707 }),
708 )
709 }
710}
711
712impl Render for SettingsView {
713 fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
714 let view = cx.entity();
715 let tabs = h_flex().w_full().px(px(20.)).gap(px(8.)).children(
716 Section::ALL.map(|section| section_tab(section, section == self.section, view.clone())),
717 );
718 let body = match self.section {
719 Section::Notifications => self.notifications_section(cx).into_any_element(),
720 Section::Devices => self.devices_section(cx).into_any_element(),
721 Section::Agents => div()
722 .id("agents-scroll")
723 .flex_1()
724 .min_h(px(0.))
725 .overflow_y_scroll()
726 .children(self.agents.clone())
727 .into_any_element(),
728 Section::Mcp => self.mcp.clone().into_any_element(),
729 };
730 v_flex()
731 .size_full()
732 .bg(theme::background())
733 .pt(px(12.))
734 .child(tabs)
735 .child(body)
736 }
737}
738
739impl SettingsView {
740 fn notifications_section(&self, cx: &App) -> Div {
741 let mut current = self.center.read(cx).settings().clone();
742 let corner = current.corner;
743 let ringtone = current.ringtone;
744 let rows = ROWS.iter().enumerate().map(|(index, row)| {
745 let on = *(row.field)(&mut current);
746 let center = self.center.clone();
747 let field = row.field;
748 h_flex()
749 .id(("setting", index))
750 .w_full()
751 .px(px(20.))
752 .py(px(10.))
753 .gap(px(12.))
754 .items_center()
755 .cursor_pointer()
756 .hover(|row| row.bg(theme::row_hover()))
757 .on_click(move |_, _, cx| {
758 center.update(cx, |center, cx| {
759 center.update_settings(cx, |settings| {
760 let value = field(settings);
761 *value = !*value;
762 })
763 });
764 })
765 .child(
766 v_flex()
767 .flex_1()
768 .child(
769 div()
770 .text_size(px(14.))
771 .font_weight(FontWeight::SEMIBOLD)
772 .text_color(theme::text())
773 .child(row.title),
774 )
775 .child(
776 div()
777 .text_size(px(12.))
778 .text_color(theme::text_muted())
779 .child(row.detail),
780 ),
781 )
782 .child(switch(on))
783 });
784 let position_center = self.center.clone();
785 let test_center = self.center.clone();
786 v_flex()
787 .w_full()
788 .pt(px(8.))
789 .children(rows)
790 .child(self.ringtone_row(ringtone))
791 .child(control_row(
792 "Position",
793 "Corner of the main screen",
794 button(corner.label()).on_click(move |_, _, cx| {
795 position_center.update(cx, |center, cx| {
796 center.update_settings(cx, |settings| {
797 settings.corner = settings.corner.next()
798 })
799 });
800 }),
801 ))
802 .child(h_flex().w_full().px(px(20.)).pt(px(12.)).child(
803 button("Send test notification").on_click(move |_, _, cx| {
804 test_center.update(cx, |center, cx| center.send_test_notification(cx));
805 }),
806 ))
807 }
808}