BlueGit

ict rusty-teams

Files

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

settings_view.rs

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