BlueGit

ict rusty-teams

Files

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

settings_view.rs

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