BlueGit

ict rusty-teams

Files

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

settings_view.rs

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